OSDN Git Service

check key and account before import key (#335)
author李永峰 <wliyongfeng@gmail.com>
Tue, 30 Jan 2018 07:48:21 +0000 (15:48 +0800)
committerPaladz <yzhu101@uottawa.ca>
Tue, 30 Jan 2018 07:48:21 +0000 (15:48 +0800)
* check key and account before import key

* leverate error return

* remove unused function

blockchain/pseudohsm/pseudohsm.go
blockchain/wallet/wallet.go

index 19e0133..15ee2d0 100644 (file)
@@ -171,6 +171,14 @@ func (h *HSM) ResetPassword(xpub chainkd.XPub, auth, newAuth string) error {
        return h.keyStore.StoreKey(xpb.File, xkey, newAuth)
 }
 
+func (h *HSM) HasAlias(alias string) bool {
+       return h.cache.hasAlias(alias)
+}
+
+func (h *HSM) HasKey(xprv chainkd.XPrv) bool {
+       return h.cache.hasKey(xprv.XPub())
+}
+
 //ImportXPrvKey import XPrv to chainkd
 func (h *HSM) ImportXPrvKey(auth string, alias string, xprv chainkd.XPrv) (*XPub, bool, error) {
        if ok := h.cache.hasAlias(alias); ok {
index 9230533..dd8437c 100755 (executable)
@@ -168,6 +168,17 @@ func (w *Wallet) ExportAccountPrivKey(hsm *pseudohsm.HSM, xpub chainkd.XPub, aut
 
 // ImportAccountPrivKey imports the account key in the Wallet Import Formt.
 func (w *Wallet) ImportAccountPrivKey(hsm *pseudohsm.HSM, xprv chainkd.XPrv, keyAlias, auth string, index uint64, accountAlias string) (*pseudohsm.XPub, error) {
+       if hsm.HasAlias(keyAlias) {
+               return nil, pseudohsm.ErrDuplicateKeyAlias
+       }
+       if hsm.HasKey(xprv) {
+               return nil, pseudohsm.ErrDuplicateKey
+       }
+
+       if acc, _ := w.AccountMgr.FindByAlias(nil, accountAlias); acc != nil {
+               return nil, account.ErrDuplicateAlias
+       }
+
        xpub, _, err := hsm.ImportXPrvKey(auth, keyAlias, xprv)
        if err != nil {
                return nil, err