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 // derivedXPub verify success
166 if !ed25519.Verify(derivedXPub.PublicKey(), []byte(msg), sig) {
167 t.Fatal("right derivedXPub verify sign failed")
170 // rootXPub verify failed
171 if ed25519.Verify(xpub.XPub.PublicKey(), []byte(msg), sig) {
172 t.Fatal("right rootXPub verify derivedXPub sign succeed")
175 err = hsm.XDelete(xpub.XPub, "password")
181 func TestImportKeyFromMnemonic(t *testing.T) {
182 dirPath, err := ioutil.TempDir(".", "")
186 defer os.RemoveAll(dirPath)
188 hsm, _ := New(dirPath)
189 supportLanguage := []string{"zh_CN", "zh_TW", "en", "it", "ja", "ko", "es"}
190 for i, language := range supportLanguage {
191 key := fmt.Sprintf("TESTKEY%x", i)
192 xpub, mnemonic, err := hsm.XCreate(key, "password", language)
196 importKey := fmt.Sprintf("IMPORTKEY%x", i)
197 newXPub, err := hsm.ImportKeyFromMnemonic(importKey, "password", *mnemonic, language)
201 if xpub.XPub != newXPub.XPub {
202 t.Fatal("import key from mnemonic failed")
207 func BenchmarkSign(b *testing.B) {
211 hsm, _ := New(dirPath)
212 xpub, _, err := hsm.XCreate("TESTKEY", auth, "en")
217 msg := []byte("In the face of ignorance and resistance I wrote financial systems into existence")
220 for i := 0; i < b.N; i++ {
221 _, err := hsm.XSign(xpub.XPub, nil, msg, auth)
226 err = hsm.XDelete(xpub.XPub, auth)