1 // Package pseudohsm provides a pseudo HSM for development environments.
10 log "github.com/sirupsen/logrus"
12 "github.com/vapor/crypto/ed25519/chainkd"
15 const logModule = "pseudohsm"
17 // KeyImage is the struct for hold export key data
18 type KeyImage struct {
19 XKeys []*encryptedKeyJSON `json:"xkeys"`
22 // Backup export all the HSM keys into array
23 func (h *HSM) Backup() (*KeyImage, error) {
25 xpubs := h.cache.keys()
26 for _, xpub := range xpubs {
27 data, err := ioutil.ReadFile(xpub.File)
32 xKey := &encryptedKeyJSON{}
33 if err := json.Unmarshal(data, xKey); err != nil {
37 image.XKeys = append(image.XKeys, xKey)
42 // Restore import the keyImages into HSM
43 func (h *HSM) Restore(image *KeyImage) error {
45 defer h.cacheMu.Unlock()
47 for _, xKey := range image.XKeys {
48 data, err := hex.DecodeString(xKey.XPub)
55 if h.cache.hasKey(xPub) {
56 log.WithFields(log.Fields{
61 }).Warning("skip restore key due to already existed")
65 if ok := h.cache.hasAlias(xKey.Alias); ok {
66 return ErrDuplicateKeyAlias
69 rawKey, err := json.Marshal(xKey)
74 _, fileName := filepath.Split(xKey.ID)
75 file := h.keyStore.JoinPath(keyFileName(fileName))
76 if err := writeKeyFile(file, rawKey); err != nil {