play_test.go raw
1 package play
2
3 import (
4 "encoding/hex"
5 "strings"
6 "testing"
7 "time"
8
9 "github.com/decred/dcrd/dcrec/secp256k1/v4"
10 "github.com/elnosh/gonuts/crypto"
11
12 "git.smesh.lol/orly/pkg/nostr/crypto/keys"
13 "git.smesh.lol/orly/pkg/nostr/interfaces/signer"
14 "git.smesh.lol/orly/pkg/nostr/interfaces/signer/p8k"
15 )
16
17 func hostSigner(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 TestBlindSignatureVerifies(t *testing.T) {
34 host := hostSigner(t)
35 statement := Statement("32210:aa:track", strings.Repeat("11", 32), 100, 200)
36 if _, err := ParseStatement(statement); err != nil {
37 t.Fatal(err)
38 }
39
40 r, err := secp256k1.GeneratePrivateKey()
41 if err != nil {
42 t.Fatal(err)
43 }
44 secret := "0000000000000000000000000000000000000000000000000000000000000001"
45 B_, _, err := crypto.BlindMessage(secret, r)
46 if err != nil {
47 t.Fatal(err)
48 }
49 cert, err := Sign(host, hex.EncodeToString(B_.SerializeCompressed()))
50 if err != nil {
51 t.Fatal(err)
52 }
53 if !Verify(hex.EncodeToString(host.Pub()), hex.EncodeToString(B_.SerializeCompressed()),
54 cert.C_, cert.DLEQ.E, cert.DLEQ.S) {
55 t.Fatal("DLEQ proof did not verify")
56 }
57
58 // The blind signature unblinds to a valid signature of the statement by the
59 // host key.
60 Cb, _ := hex.DecodeString(cert.C_)
61 C_, err := secp256k1.ParsePubKey(Cb)
62 if err != nil {
63 t.Fatal(err)
64 }
65 pub, err := secp256k1.ParsePubKey(append([]byte{0x02}, host.Pub()...))
66 if err != nil {
67 t.Fatal(err)
68 }
69 C := crypto.UnblindSignature(C_, r, pub)
70 if !crypto.Verify(secret, secp256k1.PrivKeyFromBytes(host.Sec()), C) {
71 t.Fatal("unblinded signature does not verify against the host key")
72 }
73
74 // A different host key must not verify the same proof.
75 other := hostSigner(t)
76 if Verify(hex.EncodeToString(other.Pub()), hex.EncodeToString(B_.SerializeCompressed()),
77 cert.C_, cert.DLEQ.E, cert.DLEQ.S) {
78 t.Fatal("a proof verified under the wrong host key")
79 }
80 }
81
82 func TestStatementShape(t *testing.T) {
83 s := Statement("32210:aa:track", "ABCDEF", 1, 2)
84 if string(s) != "musiquay.play:v1|32210:aa:track|abcdef|1|2" {
85 t.Fatalf("unexpected statement %q", s)
86 }
87 for _, bad := range []string{
88 "",
89 "musiquay.play:v1|a|b|1|2",
90 "other:v1|a|" + "00" + "|2|1",
91 } {
92 if _, err := ParseStatement([]byte(bad)); err == nil {
93 t.Fatalf("accepted %q", bad)
94 }
95 }
96 }
97
98 func TestIssuanceLimiter(t *testing.T) {
99 l := NewIssuanceLimiter(time.Minute)
100 if !l.Allow("aa", "asset", 1, 2) {
101 t.Fatal("first issuance refused")
102 }
103 if l.Allow("aa", "asset", 1, 2) {
104 t.Fatal("second issuance allowed for the same window")
105 }
106 if !l.Allow("bb", "asset", 1, 2) {
107 t.Fatal("a different identity was refused")
108 }
109 }
110