OSDN Git Service

fix for code review
authorpaladz <453256728@qq.com>
Thu, 19 Apr 2018 03:32:30 +0000 (11:32 +0800)
committerpaladz <453256728@qq.com>
Thu, 19 Apr 2018 03:32:30 +0000 (11:32 +0800)
api/wallet.go
blockchain/pseudohsm/image.go

index 78f3bb6..d249401 100644 (file)
@@ -16,9 +16,9 @@ func (a *API) walletError() Response {
 
 // WalletImage hold the ziped wallet data
 type WalletImage struct {
-       AccountImage *account.Image        `json:"account_image"`
-       AssetImage   *asset.Image          `json:"asset_image"`
-       KeyImages    []*pseudohsm.KeyImage `json:"key_images"`
+       AccountImage *account.Image      `json:"account_image"`
+       AssetImage   *asset.Image        `json:"asset_image"`
+       KeyImages    *pseudohsm.KeyImage `json:"key_images"`
 }
 
 func (a *API) restoreWalletImage(ctx context.Context, image WalletImage) Response {
index 4fecb34..4a258a3 100644 (file)
@@ -2,44 +2,52 @@
 package pseudohsm
 
 import (
+       "encoding/json"
        "io/ioutil"
-       "path/filepath"
 )
 
 // KeyImage is the struct for hold export key data
 type KeyImage struct {
-       XPub XPub   `json:"xpub"`
-       XKey []byte `json:"xkey"`
+       XKeys []*encryptedKeyJSON `json:"xkeys"`
 }
 
 // Backup export all the HSM keys into array
-func (h *HSM) Backup() ([]*KeyImage, error) {
-       images := []*KeyImage{}
+func (h *HSM) Backup() (*KeyImage, error) {
+       image := &KeyImage{}
        xpubs := h.cache.keys()
        for _, xpub := range xpubs {
-               xKey, err := ioutil.ReadFile(xpub.File)
+               data, err := ioutil.ReadFile(xpub.File)
                if err != nil {
                        return nil, err
                }
 
-               images = append(images, &KeyImage{XPub: xpub, XKey: xKey})
+               xKey := &encryptedKeyJSON{}
+               if err := json.Unmarshal(data, xKey); err != nil {
+                       return nil, err
+               }
+
+               image.XKeys = append(image.XKeys, xKey)
        }
-       return images, nil
+       return image, nil
 }
 
 // Restore import the keyImages into HSM
-func (h *HSM) Restore(images []*KeyImage) error {
-       for _, image := range images {
-               if ok := h.cache.hasAlias(image.XPub.Alias); ok {
+func (h *HSM) Restore(image *KeyImage) error {
+       for _, xKey := range image.XKeys {
+               if ok := h.cache.hasAlias(xKey.Alias); ok {
                        return ErrDuplicateKeyAlias
                }
 
-               fileName := filepath.Base(image.XPub.File)
-               image.XPub.File = h.keyStore.JoinPath(fileName)
-               if err := writeKeyFile(image.XPub.File, image.XKey); err != nil {
+               rawKey, err := json.Marshal(xKey)
+               if err != nil {
+                       return err
+               }
+
+               file := h.keyStore.JoinPath(keyFileName(xKey.ID))
+               if err := writeKeyFile(file, rawKey); err != nil {
                        return nil
                }
-               h.cache.add(image.XPub)
        }
+       h.cache.maybeReload()
        return nil
 }