1 // Package pseudohsm provides a pseudo HSM for development environments.
13 "github.com/pborman/uuid"
15 "github.com/vapor/crypto/ed25519/chainkd"
16 "github.com/vapor/errors"
17 mnem "github.com/vapor/wallet/mnemonic"
20 // pre-define errors for supporting bytom errorFormatter
22 ErrDuplicateKeyAlias = errors.New("duplicate key alias")
23 ErrLoadKey = errors.New("key not found or wrong password ")
24 ErrDecrypt = errors.New("could not decrypt key with given passphrase")
25 ErrMnemonicLength = errors.New("mnemonic length error")
28 // EntropyLength random entropy length to generate mnemonics.
29 const EntropyLength = 128
31 // HSM type for storing pubkey and privatekey
36 //kdCache map[chainkd.XPub]chainkd.XPrv
39 // XPub type for pubkey for anyone can see
41 Alias string `json:"alias"`
42 XPub chainkd.XPub `json:"xpub"`
43 File string `json:"file"`
46 // New method for HSM struct
47 func New(keypath string) (*HSM, error) {
48 keydir, _ := filepath.Abs(keypath)
50 keyStore: &keyStorePassphrase{keydir, LightScryptN, LightScryptP},
51 cache: newKeyCache(keydir),
52 //kdCache: make(map[chainkd.XPub]chainkd.XPrv),
56 // XCreate produces a new random xprv and stores it in the db.
57 func (h *HSM) XCreate(alias string, auth string, language string) (*XPub, *string, error) {
59 defer h.cacheMu.Unlock()
61 normalizedAlias := strings.ToLower(strings.TrimSpace(alias))
62 if ok := h.cache.hasAlias(normalizedAlias); ok {
63 return nil, nil, ErrDuplicateKeyAlias
66 xpub, mnemonic, err := h.createChainKDKey(normalizedAlias, auth, language)
71 return xpub, mnemonic, err
74 // ImportKeyFromMnemonic produces a xprv from mnemonic and stores it in the db.
75 func (h *HSM) ImportKeyFromMnemonic(alias string, auth string, mnemonic string, language string) (*XPub, error) {
77 defer h.cacheMu.Unlock()
79 // checksum length = entropy length /32
80 // mnemonic length = (entropy length + checksum length)/11
81 if len(strings.Fields(mnemonic)) != (EntropyLength+EntropyLength/32)/11 {
82 return nil, ErrMnemonicLength
85 normalizedAlias := strings.ToLower(strings.TrimSpace(alias))
86 if ok := h.cache.hasAlias(normalizedAlias); ok {
87 return nil, ErrDuplicateKeyAlias
90 // Pre validate that the mnemonic is well formed and only contains words that
91 // are present in the word list
92 if !mnem.IsMnemonicValid(mnemonic, language) {
93 return nil, mnem.ErrInvalidMnemonic
96 xpub, err := h.createKeyFromMnemonic(alias, auth, mnemonic)
105 func (h *HSM) createKeyFromMnemonic(alias string, auth string, mnemonic string) (*XPub, error) {
106 // Generate a Bip32 HD wallet for the mnemonic and a user supplied password
107 seed := mnem.NewSeed(mnemonic, "")
108 xprv, xpub, err := chainkd.NewXKeys(bytes.NewBuffer(seed))
112 id := uuid.NewRandom()
120 file := h.keyStore.JoinPath(keyFileName(key.ID.String()))
121 if err := h.keyStore.StoreKey(file, key, auth); err != nil {
122 return nil, errors.Wrap(err, "storing keys")
124 return &XPub{XPub: xpub, Alias: alias, File: file}, nil
127 func (h *HSM) createChainKDKey(alias string, auth string, language string) (*XPub, *string, error) {
128 // Generate a mnemonic for memorization or user-friendly seeds
129 entropy, err := mnem.NewEntropy(EntropyLength)
133 mnemonic, err := mnem.NewMnemonic(entropy, language)
137 xpub, err := h.createKeyFromMnemonic(alias, auth, mnemonic)
141 return xpub, &mnemonic, nil
144 // UpdateKeyAlias update key alias
145 func (h *HSM) UpdateKeyAlias(xpub chainkd.XPub, newAlias string) error {
147 defer h.cacheMu.Unlock()
149 h.cache.maybeReload()
151 xpb, err := h.cache.find(XPub{XPub: xpub})
157 keyjson, err := ioutil.ReadFile(xpb.File)
162 encrptKeyJSON := new(encryptedKeyJSON)
163 if err := json.Unmarshal(keyjson, encrptKeyJSON); err != nil {
167 normalizedAlias := strings.ToLower(strings.TrimSpace(newAlias))
168 if ok := h.cache.hasAlias(normalizedAlias); ok {
169 return ErrDuplicateKeyAlias
172 encrptKeyJSON.Alias = normalizedAlias
173 keyJSON, err := json.Marshal(encrptKeyJSON)
178 if err := writeKeyFile(xpb.File, keyJSON); err != nil {
184 xpb.Alias = normalizedAlias
190 // ListKeys returns a list of all xpubs from the store
191 func (h *HSM) ListKeys() []XPub {
192 xpubs := h.cache.keys()
196 // XSign looks up the xprv given the xpub, optionally derives a new
197 // xprv with the given path (but does not store the new xprv), and
198 // signs the given msg.
199 func (h *HSM) XSign(xpub chainkd.XPub, path [][]byte, msg []byte, auth string) ([]byte, error) {
200 xprv, err := h.LoadChainKDKey(xpub, auth)
205 xprv = xprv.Derive(path)
207 return xprv.Sign(msg), nil
210 //LoadChainKDKey get xprv from xpub
211 func (h *HSM) LoadChainKDKey(xpub chainkd.XPub, auth string) (xprv chainkd.XPrv, err error) {
213 defer h.cacheMu.Unlock()
215 //if xprv, ok := h.kdCache[xpub]; ok {
219 _, xkey, err := h.loadDecryptedKey(xpub, auth)
221 return xprv, ErrLoadKey
223 //h.kdCache[xpb.XPub] = xkey.XPrv
224 return xkey.XPrv, nil
227 // XDelete deletes the key matched by xpub if the passphrase is correct.
228 // If a contains no filename, the address must match a unique key.
229 func (h *HSM) XDelete(xpub chainkd.XPub, auth string) error {
230 // Decrypting the key isn't really necessary, but we do
231 // it anyway to check the password and zero out the key
232 // immediately afterwards.
234 xpb, xkey, err := h.loadDecryptedKey(xpub, auth)
243 // The order is crucial here. The key is dropped from the
244 // cache after the file is gone so that a reload happening in
245 // between won't insert it into the cache again.
246 err = os.Remove(xpb.File)
254 func (h *HSM) loadDecryptedKey(xpub chainkd.XPub, auth string) (XPub, *XKey, error) {
255 h.cache.maybeReload()
257 xpb, err := h.cache.find(XPub{XPub: xpub})
263 xkey, err := h.keyStore.GetKey(xpb.Alias, xpb.File, auth)
264 return xpb, xkey, err
267 // ResetPassword reset passphrase for an existing xpub
268 func (h *HSM) ResetPassword(xpub chainkd.XPub, oldAuth, newAuth string) error {
269 xpb, xkey, err := h.loadDecryptedKey(xpub, oldAuth)
273 return h.keyStore.StoreKey(xpb.File, xkey, newAuth)
276 // HasAlias check whether the key alias exists
277 func (h *HSM) HasAlias(alias string) bool {
278 return h.cache.hasAlias(alias)
281 // HasKey check whether the private key exists
282 func (h *HSM) HasKey(xprv chainkd.XPrv) bool {
283 return h.cache.hasKey(xprv.XPub())