-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
288 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package nut10 | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/elnosh/gonuts/cashu" | ||
) | ||
|
||
func TestSerializeSecret(t *testing.T) { | ||
secretData := WellKnownSecret{ | ||
Nonce: "da62796403af76c80cd6ce9153ed3746", | ||
Data: "033281c37677ea273eb7183b783067f5244933ef78d8c3f15b1a77cb246099c26e", | ||
Tags: [][]string{ | ||
{"sigflag", "SIG_ALL"}, | ||
}, | ||
} | ||
|
||
serialized, err := SerializeSecret(cashu.P2PK, secretData) | ||
if err != nil { | ||
t.Fatalf("got unexpected error: %v", err) | ||
} | ||
|
||
expected := `["P2PK", {"nonce":"da62796403af76c80cd6ce9153ed3746","data":"033281c37677ea273eb7183b783067f5244933ef78d8c3f15b1a77cb246099c26e","tags":[["sigflag","SIG_ALL"]]}]` | ||
|
||
if serialized != expected { | ||
t.Fatalf("expected secret:\n%v\n\n but got:\n%v", expected, serialized) | ||
} | ||
} | ||
|
||
func TestDeserializeSecret(t *testing.T) { | ||
secret := `["P2PK", {"nonce":"da62796403af76c80cd6ce9153ed3746","data":"033281c37677ea273eb7183b783067f5244933ef78d8c3f15b1a77cb246099c26e","tags":[["sigflag","SIG_ALL"]]}]` | ||
secretData, err := DeserializeSecret(secret) | ||
if err != nil { | ||
t.Fatalf("got unexpected error: %v", err) | ||
} | ||
|
||
expectedNonce := "da62796403af76c80cd6ce9153ed3746" | ||
if secretData.Nonce != expectedNonce { | ||
t.Fatalf("expected nonce '%v' but got '%v' instead", expectedNonce, secretData.Nonce) | ||
} | ||
|
||
expectedData := "033281c37677ea273eb7183b783067f5244933ef78d8c3f15b1a77cb246099c26e" | ||
if secretData.Data != expectedData { | ||
t.Fatalf("expected data '%v' but got '%v' instead", expectedData, secretData.Data) | ||
} | ||
|
||
expectedTags := [][]string{ | ||
{"sigflag", "SIG_ALL"}, | ||
} | ||
if !reflect.DeepEqual(secretData.Tags, expectedTags) { | ||
t.Fatalf("expected tags '%v' but got '%v' instead", expectedTags, secretData.Tags) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package nut11 | ||
|
||
import ( | ||
"encoding/hex" | ||
"testing" | ||
|
||
"github.com/btcsuite/btcd/btcec/v2" | ||
"github.com/elnosh/gonuts/cashu/nuts/nut10" | ||
) | ||
|
||
func TestIsSigAll(t *testing.T) { | ||
tests := []struct { | ||
p2pkSecretData nut10.WellKnownSecret | ||
expected bool | ||
}{ | ||
{ | ||
p2pkSecretData: nut10.WellKnownSecret{ | ||
Tags: [][]string{}, | ||
}, | ||
expected: false, | ||
}, | ||
{ | ||
p2pkSecretData: nut10.WellKnownSecret{ | ||
Tags: [][]string{{"sigflag", "SIG_INPUTS"}}, | ||
}, | ||
expected: false, | ||
}, | ||
{ | ||
p2pkSecretData: nut10.WellKnownSecret{ | ||
Tags: [][]string{ | ||
{"locktime", "882912379"}, | ||
{"refund", "refundkey"}, | ||
{"sigflag", "SIG_ALL"}, | ||
}, | ||
}, | ||
expected: true, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
result := IsSigAll(test.p2pkSecretData) | ||
if result != test.expected { | ||
t.Fatalf("expected '%v' but got '%v' instead", test.expected, result) | ||
} | ||
} | ||
} | ||
|
||
func TestCanSign(t *testing.T) { | ||
privateKey, _ := btcec.NewPrivateKey() | ||
publicKey := hex.EncodeToString(privateKey.PubKey().SerializeCompressed()) | ||
|
||
tests := []struct { | ||
p2pkSecretData nut10.WellKnownSecret | ||
expected bool | ||
}{ | ||
{ | ||
p2pkSecretData: nut10.WellKnownSecret{ | ||
Data: publicKey, | ||
}, | ||
expected: true, | ||
}, | ||
|
||
{ | ||
p2pkSecretData: nut10.WellKnownSecret{ | ||
Data: "somerandomkey", | ||
}, | ||
expected: false, | ||
}, | ||
|
||
{ | ||
p2pkSecretData: nut10.WellKnownSecret{ | ||
Data: "sdjflksjdflsdjfd", | ||
}, | ||
expected: false, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
result := CanSign(test.p2pkSecretData, privateKey) | ||
if result != test.expected { | ||
t.Fatalf("expected '%v' but got '%v' instead", test.expected, result) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters