8 "github.com/bytom/crypto/ed25519"
9 "github.com/bytom/errors"
12 const dirPath = "testdata/pseudo"
14 func TestCreateKeyWithUpperCase(t *testing.T) {
15 hsm, _ := New(dirPath)
19 xpub, err := hsm.XCreate(alias, "password")
24 if xpub.Alias != strings.ToLower(alias) {
25 t.Fatal("the created key alias should be lowercase")
28 err = hsm.XDelete(xpub.XPub, "password")
34 func TestCreateKeyWithWhiteSpaceTrimed(t *testing.T) {
35 hsm, _ := New(dirPath)
37 alias := " with space surrounding "
39 xpub, err := hsm.XCreate(alias, "password")
44 if xpub.Alias != strings.TrimSpace(alias) {
45 t.Fatal("the created key alias should be lowercase")
48 err = hsm.XDelete(xpub.XPub, "password")
54 func TestPseudoHSMChainKDKeys(t *testing.T) {
56 hsm, _ := New(dirPath)
57 xpub, err := hsm.XCreate("bbs", "password")
62 xpub2, err := hsm.XCreate("bytom", "nopassword")
66 msg := []byte("In the face of ignorance and resistance I wrote financial systems into existence")
67 sig, err := hsm.XSign(xpub.XPub, nil, msg, "password")
71 if !xpub.XPub.Verify(msg, sig) {
72 t.Error("expected verify to succeed")
74 if xpub2.XPub.Verify(msg, sig) {
75 t.Error("expected verify with wrong pubkey to fail")
77 path := [][]byte{{3, 2, 6, 3, 8, 2, 7}}
78 sig, err = hsm.XSign(xpub2.XPub, path, msg, "nopassword")
82 if xpub2.XPub.Verify(msg, sig) {
83 t.Error("expected verify with underived pubkey of sig from derived privkey to fail")
85 if !xpub2.XPub.Derive(path).Verify(msg, sig) {
86 t.Error("expected verify with derived pubkey of sig from derived privkey to succeed")
89 xpubs := hsm.ListKeys()
91 t.Error("expected 2 entries in the db")
93 err = hsm.ResetPassword(xpub2.XPub, "nopassword", "1password")
97 err = hsm.XDelete(xpub.XPub, "password")
101 err = hsm.XDelete(xpub2.XPub, "1password")
107 func TestKeyWithEmptyAlias(t *testing.T) {
108 hsm, _ := New(dirPath)
109 for i := 0; i < 2; i++ {
110 xpub, err := hsm.XCreate(fmt.Sprintf("xx%d", i), "xx")
111 if errors.Root(err) != nil {
114 err = hsm.XDelete(xpub.XPub, "xx")
121 func TestSignAndVerifyMessage(t *testing.T) {
122 hsm, _ := New(dirPath)
123 xpub, err := hsm.XCreate("TESTKEY", "password")
128 path := [][]byte{{3, 2, 6, 3, 8, 2, 7}}
129 derivedXPub := xpub.XPub.Derive(path)
131 msg := "this is a test message"
132 sig, err := hsm.XSign(xpub.XPub, path, []byte(msg), "password")
137 // derivedXPub verify success
138 if !ed25519.Verify(derivedXPub.PublicKey(), []byte(msg), sig) {
139 t.Fatal("right derivedXPub verify sign failed")
142 // rootXPub verify failed
143 if ed25519.Verify(xpub.XPub.PublicKey(), []byte(msg), sig) {
144 t.Fatal("right rootXPub verify derivedXPub sign succeed")
147 err = hsm.XDelete(xpub.XPub, "password")
153 func BenchmarkSign(b *testing.B) {
157 hsm, _ := New(dirPath)
158 xpub, err := hsm.XCreate(auth, "")
163 msg := []byte("In the face of ignorance and resistance I wrote financial systems into existence")
166 for i := 0; i < b.N; i++ {
167 _, err := hsm.XSign(xpub.XPub, nil, msg, auth)
172 err = hsm.XDelete(xpub.XPub, auth)