1 // Package pseudohsm provides a pseudo HSM for development environments.
10 // KeyImage is the struct for hold export key data
11 type KeyImage struct {
12 XKeys []*encryptedKeyJSON `json:"xkeys"`
15 // Backup export all the HSM keys into array
16 func (h *HSM) Backup() (*KeyImage, error) {
18 xpubs := h.cache.keys()
19 for _, xpub := range xpubs {
20 data, err := ioutil.ReadFile(xpub.File)
25 xKey := &encryptedKeyJSON{}
26 if err := json.Unmarshal(data, xKey); err != nil {
30 image.XKeys = append(image.XKeys, xKey)
35 // Restore import the keyImages into HSM
36 func (h *HSM) Restore(image *KeyImage) error {
38 defer h.cacheMu.Unlock()
40 for _, xKey := range image.XKeys {
41 if ok := h.cache.hasAlias(xKey.Alias); ok {
42 return ErrDuplicateKeyAlias
45 rawKey, err := json.Marshal(xKey)
50 _, fileName := filepath.Split(xKey.ID)
51 file := h.keyStore.JoinPath(keyFileName(fileName))
52 if err := writeKeyFile(file, rawKey); err != nil {