orders_test.go raw
1 package orders
2
3 import (
4 "testing"
5 "time"
6
7 "git.smesh.lol/orly/pkg/musiquay/deed"
8 "git.smesh.lol/orly/pkg/nostr/crypto/keys"
9 "git.smesh.lol/orly/pkg/nostr/encoders/event"
10 "git.smesh.lol/orly/pkg/nostr/encoders/hex"
11 "git.smesh.lol/orly/pkg/nostr/encoders/kind"
12 "git.smesh.lol/orly/pkg/nostr/encoders/tag"
13 "git.smesh.lol/orly/pkg/nostr/interfaces/signer"
14 "git.smesh.lol/orly/pkg/nostr/interfaces/signer/p8k"
15 )
16
17 func testSigner(t *testing.T) signer.I {
18 t.Helper()
19 sk, err := keys.GenerateSecretKey()
20 if err != nil {
21 t.Fatal(err)
22 }
23 s, err := p8k.New()
24 if err != nil {
25 t.Fatal(err)
26 }
27 if err = s.InitSec(sk); err != nil {
28 t.Fatal(err)
29 }
30 return s
31 }
32
33 func rumor(t *testing.T, buyer signer.I, tags ...[]string) *event.E {
34 t.Helper()
35 ev := event.New()
36 ev.Kind = kind.PurchaseOrder.ToU16()
37 ev.Pubkey = append([]byte(nil), buyer.Pub()...)
38 ev.CreatedAt = time.Now().Unix()
39 ev.Tags = tag.NewS()
40 for _, tup := range tags {
41 tt := tag.New()
42 for _, v := range tup {
43 tt.T = append(tt.T, []byte(v))
44 }
45 ev.Tags.Append(tt)
46 }
47 ev.ID = ev.GetIDBytes()
48 return ev
49 }
50
51 func TestParseAndCheck(t *testing.T) {
52 buyer := testSigner(t)
53 seller := testSigner(t)
54 coord := "32210:" + hex.Enc(seller.Pub()) + ":track"
55 assetID := "ab"
56 for len(assetID) < 64 {
57 assetID += "ab"
58 }
59
60 o, err := ParseOrder(rumor(t, buyer,
61 []string{"a", coord},
62 []string{"p", hex.Enc(seller.Pub())},
63 []string{"tier", "fan"},
64 []string{"price", "500", "sat"},
65 []string{"payment", "cashu"},
66 []string{"mint", "https://mint.example.com"},
67 []string{"token", "cashuB..."},
68 ))
69 if err != nil {
70 t.Fatal(err)
71 }
72 if o.Tier != deed.TierFan || o.PriceSats != 500 || o.Payment != PaymentCashu {
73 t.Fatalf("decoded wrong: %+v", o)
74 }
75
76 view := &AssetView{
77 AssetID: assetID, Coordinate: coord, Owner: seller.Pub(),
78 PriceSats: 500, Mints: []string{"https://mint.example.com"},
79 }
80 if err = o.Check(CheckOptions{Asset: view, Seller: seller.Pub()}); err != nil {
81 t.Fatalf("valid order refused: %v", err)
82 }
83 if err = o.Check(CheckOptions{Asset: view, Seller: buyer.Pub()}); err == nil {
84 t.Fatal("order for another seller accepted")
85 }
86 lower := *o
87 lower.PriceSats = 400
88 if err = lower.Check(CheckOptions{Asset: view, Seller: seller.Pub()}); err == nil {
89 t.Fatal("underpaid order accepted")
90 }
91 if got := o.RequiredGross(7); got != 507 {
92 t.Fatalf("RequiredGross = %d, want 507", got)
93 }
94 }
95
96 func TestSubOrderNeedsChannel(t *testing.T) {
97 buyer := testSigner(t)
98 seller := testSigner(t)
99 if _, err := ParseOrder(rumor(t, buyer,
100 []string{"a", "32210:" + hex.Enc(seller.Pub()) + ":track"},
101 []string{"tier", "sub"},
102 []string{"price", "500", "sat"},
103 )); err == nil {
104 t.Fatal("sub order with no channel accepted")
105 }
106 if _, err := ParseOrder(rumor(t, buyer,
107 []string{"tier", "fan"},
108 )); err == nil {
109 t.Fatal("fan order with no coordinate accepted")
110 }
111 }
112
113 func TestLightningNeedsVerification(t *testing.T) {
114 buyer := testSigner(t)
115 seller := testSigner(t)
116 coord := "32210:" + hex.Enc(seller.Pub()) + ":track"
117 o, err := ParseOrder(rumor(t, buyer,
118 []string{"a", coord},
119 []string{"tier", "fan"},
120 []string{"price", "100", "sat"},
121 []string{"payment", "lightning"},
122 []string{"zap", "abc"},
123 ))
124 if err != nil {
125 t.Fatal(err)
126 }
127 view := &AssetView{AssetID: assetID64(), Coordinate: coord, Owner: seller.Pub(), PriceSats: 100}
128 if err = o.Check(CheckOptions{Asset: view, Seller: seller.Pub()}); err == nil {
129 t.Fatal("lightning accepted without a verifier")
130 }
131 if err = o.Check(CheckOptions{Asset: view, Seller: seller.Pub(), AcceptLightning: true}); err != nil {
132 t.Fatalf("verified lightning refused: %v", err)
133 }
134 }
135
136 func assetID64() string {
137 s := ""
138 for len(s) < 64 {
139 s += "cd"
140 }
141 return s
142 }
143