OSDN Git Service

Merge pull request #201 from Bytom/v0.1
[bytom/vapor.git] / blockchain / pseudohsm / image.go
index 7a13a72..1c691ca 100644 (file)
@@ -2,11 +2,18 @@
 package pseudohsm
 
 import (
+       "encoding/hex"
        "encoding/json"
        "io/ioutil"
        "path/filepath"
+
+       log "github.com/sirupsen/logrus"
+
+       "github.com/vapor/crypto/ed25519/chainkd"
 )
 
+const logModule = "pseudohsm"
+
 // KeyImage is the struct for hold export key data
 type KeyImage struct {
        XKeys []*encryptedKeyJSON `json:"xkeys"`
@@ -38,6 +45,23 @@ func (h *HSM) Restore(image *KeyImage) error {
        defer h.cacheMu.Unlock()
 
        for _, xKey := range image.XKeys {
+               data, err := hex.DecodeString(xKey.XPub)
+               if err != nil {
+                       return ErrXPubFormat
+               }
+
+               var xPub chainkd.XPub
+               copy(xPub[:], data)
+               if h.cache.hasKey(xPub) {
+                       log.WithFields(log.Fields{
+                               "module": logModule,
+                               "alias":  xKey.Alias,
+                               "id":     xKey.ID,
+                               "xPub":   xKey.XPub,
+                       }).Warning("skip restore key due to already existed")
+                       continue
+               }
+
                if ok := h.cache.hasAlias(xKey.Alias); ok {
                        return ErrDuplicateKeyAlias
                }
@@ -52,7 +76,8 @@ func (h *HSM) Restore(image *KeyImage) error {
                if err := writeKeyFile(file, rawKey); err != nil {
                        return err
                }
+
+               h.cache.reload()
        }
-       h.cache.maybeReload()
        return nil
 }