10 "github.com/vapor/crypto/ed25519"
11 "github.com/vapor/errors"
14 const dirPath = "testdata/pseudo"
16 func TestCreateKeyWithUpperCase(t *testing.T) {
17 hsm, _ := New(dirPath)
21 xpub, _, err := hsm.XCreate(alias, "password", "en")
26 if xpub.Alias != strings.ToLower(alias) {
27 t.Fatal("the created key alias should be lowercase")
30 err = hsm.XDelete(xpub.XPub, "password")
36 func TestCreateKeyWithWhiteSpaceTrimed(t *testing.T) {
37 hsm, _ := New(dirPath)
39 alias := " with space surrounding "
41 xpub, _, err := hsm.XCreate(alias, "password", "en")
46 if xpub.Alias != strings.TrimSpace(alias) {
47 t.Fatal("the created key alias should be lowercase")
50 if err = hsm.XDelete(xpub.XPub, "password"); err != nil {
55 func TestUpdateKeyAlias(t *testing.T) {
56 hsm, _ := New(dirPath)
57 oldAlias := "old_alias"
58 newAlias := "new_alias"
60 xpub, _, err := hsm.XCreate(oldAlias, "password", "en")
65 if xpub.Alias != strings.TrimSpace(oldAlias) {
66 t.Fatal("the created key alias should be lowercase")
69 if err = hsm.UpdateKeyAlias(xpub.XPub, oldAlias); err != ErrDuplicateKeyAlias {
70 t.Fatal("got error:", err, "want error:", ErrDuplicateKeyAlias)
73 if err = hsm.UpdateKeyAlias(xpub.XPub, newAlias); err != nil {
77 if err = hsm.XDelete(xpub.XPub, "password"); err != nil {
82 func TestPseudoHSMChainKDKeys(t *testing.T) {
84 hsm, _ := New(dirPath)
85 xpub, _, err := hsm.XCreate("bbs", "password", "en")
90 xpub2, _, err := hsm.XCreate("bytom", "nopassword", "en")
94 msg := []byte("In the face of ignorance and resistance I wrote financial systems into existence")
95 sig, err := hsm.XSign(xpub.XPub, nil, msg, "password")
99 if !xpub.XPub.Verify(msg, sig) {
100 t.Error("expected verify to succeed")
102 if xpub2.XPub.Verify(msg, sig) {
103 t.Error("expected verify with wrong pubkey to fail")
105 path := [][]byte{{3, 2, 6, 3, 8, 2, 7}}
106 sig, err = hsm.XSign(xpub2.XPub, path, msg, "nopassword")
110 if xpub2.XPub.Verify(msg, sig) {
111 t.Error("expected verify with underived pubkey of sig from derived privkey to fail")
113 if !xpub2.XPub.Derive(path).Verify(msg, sig) {
114 t.Error("expected verify with derived pubkey of sig from derived privkey to succeed")
117 xpubs := hsm.ListKeys()
119 t.Error("expected 2 entries in the db")
121 err = hsm.ResetPassword(xpub2.XPub, "nopassword", "1password")
125 err = hsm.XDelete(xpub.XPub, "password")
129 err = hsm.XDelete(xpub2.XPub, "1password")
135 func TestKeyWithEmptyAlias(t *testing.T) {
136 hsm, _ := New(dirPath)
137 for i := 0; i < 2; i++ {
138 xpub, _, err := hsm.XCreate(fmt.Sprintf("xx%d", i), "xx", "en")
139 if errors.Root(err) != nil {
142 err = hsm.XDelete(xpub.XPub, "xx")
149 func TestSignAndVerifyMessage(t *testing.T) {
150 hsm, _ := New(dirPath)
151 xpub, _, err := hsm.XCreate("TESTKEY", "password", "en")
156 path := [][]byte{{3, 2, 6, 3, 8, 2, 7}}
157 derivedXPub := xpub.XPub.Derive(path)
159 msg := "this is a test message"
160 sig, err := hsm.XSign(xpub.XPub, path, []byte(msg), "password")
165 dePublicKey := derivedXPub.PublicKey()
166 switch pubKey := dePublicKey.(type) {
167 case ed25519.PublicKey:
168 // derivedXPub verify success
169 if !ed25519.Verify(pubKey, []byte(msg), sig) {
170 t.Fatal("right derivedXPub verify sign failed")
174 roPublicKey := xpub.XPub.PublicKey()
175 switch pubKey := roPublicKey.(type) {
176 case ed25519.PublicKey:
177 // rootXPub verify failed
178 if ed25519.Verify(pubKey, []byte(msg), sig) {
179 t.Fatal("right rootXPub verify derivedXPub sign succeed")
183 err = hsm.XDelete(xpub.XPub, "password")
189 func TestImportKeyFromMnemonic(t *testing.T) {
190 dirPath, err := ioutil.TempDir(".", "")
194 defer os.RemoveAll(dirPath)
196 hsm, _ := New(dirPath)
197 supportLanguage := []string{"zh_CN", "zh_TW", "en", "it", "ja", "ko", "es"}
198 for i, language := range supportLanguage {
199 key := fmt.Sprintf("TESTKEY%x", i)
200 xpub, mnemonic, err := hsm.XCreate(key, "password", language)
204 importKey := fmt.Sprintf("IMPORTKEY%x", i)
205 newXPub, err := hsm.ImportKeyFromMnemonic(importKey, "password", *mnemonic, language)
209 if xpub.XPub != newXPub.XPub {
210 t.Fatal("import key from mnemonic failed")
215 func BenchmarkSign(b *testing.B) {
219 hsm, _ := New(dirPath)
220 xpub, _, err := hsm.XCreate("TESTKEY", auth, "en")
225 msg := []byte("In the face of ignorance and resistance I wrote financial systems into existence")
228 for i := 0; i < b.N; i++ {
229 _, err := hsm.XSign(xpub.XPub, nil, msg, auth)
234 err = hsm.XDelete(xpub.XPub, auth)