github.com/Bytom/bytom@v1.1.2-0.20210127130405-ae40204c0b09/blockchain/pseudohsm/pseudohsm_test.go (about) 1 package pseudohsm 2 3 import ( 4 "fmt" 5 "io/ioutil" 6 "os" 7 "strings" 8 "testing" 9 10 "github.com/bytom/bytom/crypto/ed25519" 11 "github.com/bytom/bytom/errors" 12 ) 13 14 const dirPath = "testdata/pseudo" 15 16 func TestCreateKeyWithUpperCase(t *testing.T) { 17 hsm, _ := New(dirPath) 18 19 alias := "UPPER" 20 21 xpub, _, err := hsm.XCreate(alias, "password", "en") 22 if err != nil { 23 t.Fatal(err) 24 } 25 26 if xpub.Alias != strings.ToLower(alias) { 27 t.Fatal("the created key alias should be lowercase") 28 } 29 30 err = hsm.XDelete(xpub.XPub, "password") 31 if err != nil { 32 t.Fatal(err) 33 } 34 } 35 36 func TestCreateKeyWithWhiteSpaceTrimed(t *testing.T) { 37 hsm, _ := New(dirPath) 38 39 alias := " with space surrounding " 40 41 xpub, _, err := hsm.XCreate(alias, "password", "en") 42 if err != nil { 43 t.Fatal(err) 44 } 45 46 if xpub.Alias != strings.TrimSpace(alias) { 47 t.Fatal("the created key alias should be lowercase") 48 } 49 50 if err = hsm.XDelete(xpub.XPub, "password"); err != nil { 51 t.Fatal(err) 52 } 53 } 54 55 func TestUpdateKeyAlias(t *testing.T) { 56 hsm, _ := New(dirPath) 57 oldAlias := "old_alias" 58 newAlias := "new_alias" 59 60 xpub, _, err := hsm.XCreate(oldAlias, "password", "en") 61 if err != nil { 62 t.Fatal(err) 63 } 64 65 if xpub.Alias != strings.TrimSpace(oldAlias) { 66 t.Fatal("the created key alias should be lowercase") 67 } 68 69 if err = hsm.UpdateKeyAlias(xpub.XPub, oldAlias); err != ErrDuplicateKeyAlias { 70 t.Fatal("got error:", err, "want error:", ErrDuplicateKeyAlias) 71 } 72 73 if err = hsm.UpdateKeyAlias(xpub.XPub, newAlias); err != nil { 74 t.Fatal(err) 75 } 76 77 if err = hsm.XDelete(xpub.XPub, "password"); err != nil { 78 t.Fatal(err) 79 } 80 } 81 82 func TestPseudoHSMChainKDKeys(t *testing.T) { 83 84 hsm, _ := New(dirPath) 85 xpub, _, err := hsm.XCreate("bbs", "password", "en") 86 87 if err != nil { 88 t.Fatal(err) 89 } 90 xpub2, _, err := hsm.XCreate("bytom", "nopassword", "en") 91 if err != nil { 92 t.Fatal(err) 93 } 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") 96 if err != nil { 97 t.Fatal(err) 98 } 99 if !xpub.XPub.Verify(msg, sig) { 100 t.Error("expected verify to succeed") 101 } 102 if xpub2.XPub.Verify(msg, sig) { 103 t.Error("expected verify with wrong pubkey to fail") 104 } 105 path := [][]byte{{3, 2, 6, 3, 8, 2, 7}} 106 sig, err = hsm.XSign(xpub2.XPub, path, msg, "nopassword") 107 if err != nil { 108 t.Fatal(err) 109 } 110 if xpub2.XPub.Verify(msg, sig) { 111 t.Error("expected verify with underived pubkey of sig from derived privkey to fail") 112 } 113 if !xpub2.XPub.Derive(path).Verify(msg, sig) { 114 t.Error("expected verify with derived pubkey of sig from derived privkey to succeed") 115 } 116 117 xpubs := hsm.ListKeys() 118 if len(xpubs) != 2 { 119 t.Error("expected 2 entries in the db") 120 } 121 err = hsm.ResetPassword(xpub2.XPub, "nopassword", "1password") 122 if err != nil { 123 t.Fatal(err) 124 } 125 err = hsm.XDelete(xpub.XPub, "password") 126 if err != nil { 127 t.Fatal(err) 128 } 129 err = hsm.XDelete(xpub2.XPub, "1password") 130 if err != nil { 131 t.Fatal(err) 132 } 133 } 134 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 { 140 t.Fatal(err) 141 } 142 err = hsm.XDelete(xpub.XPub, "xx") 143 if err != nil { 144 t.Fatal(err) 145 } 146 } 147 } 148 149 func TestSignAndVerifyMessage(t *testing.T) { 150 hsm, _ := New(dirPath) 151 xpub, _, err := hsm.XCreate("TESTKEY", "password", "en") 152 if err != nil { 153 t.Fatal(err) 154 } 155 156 path := [][]byte{{3, 2, 6, 3, 8, 2, 7}} 157 derivedXPub := xpub.XPub.Derive(path) 158 159 msg := "this is a test message" 160 sig, err := hsm.XSign(xpub.XPub, path, []byte(msg), "password") 161 if err != nil { 162 t.Fatal(err) 163 } 164 165 // derivedXPub verify success 166 if !ed25519.Verify(derivedXPub.PublicKey(), []byte(msg), sig) { 167 t.Fatal("right derivedXPub verify sign failed") 168 } 169 170 // rootXPub verify failed 171 if ed25519.Verify(xpub.XPub.PublicKey(), []byte(msg), sig) { 172 t.Fatal("right rootXPub verify derivedXPub sign succeed") 173 } 174 175 err = hsm.XDelete(xpub.XPub, "password") 176 if err != nil { 177 t.Fatal(err) 178 } 179 } 180 181 func TestImportKeyFromMnemonic(t *testing.T) { 182 dirPath, err := ioutil.TempDir(".", "") 183 if err != nil { 184 t.Fatal(err) 185 } 186 defer os.RemoveAll(dirPath) 187 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) 193 if err != nil { 194 t.Fatal(err) 195 } 196 importKey := fmt.Sprintf("IMPORTKEY%x", i) 197 newXPub, err := hsm.ImportKeyFromMnemonic(importKey, "password", *mnemonic, language) 198 if err != nil { 199 t.Fatal(err) 200 } 201 if xpub.XPub != newXPub.XPub { 202 t.Fatal("import key from mnemonic failed") 203 } 204 } 205 } 206 207 func BenchmarkSign(b *testing.B) { 208 b.StopTimer() 209 auth := "nowpasswd" 210 211 hsm, _ := New(dirPath) 212 xpub, _, err := hsm.XCreate("TESTKEY", auth, "en") 213 if err != nil { 214 b.Fatal(err) 215 } 216 217 msg := []byte("In the face of ignorance and resistance I wrote financial systems into existence") 218 219 b.StartTimer() 220 for i := 0; i < b.N; i++ { 221 _, err := hsm.XSign(xpub.XPub, nil, msg, auth) 222 if err != nil { 223 b.Fatal(err) 224 } 225 } 226 err = hsm.XDelete(xpub.XPub, auth) 227 if err != nil { 228 b.Fatal(err) 229 } 230 }