2023-09-12 12:45:32 +00:00
|
|
|
package wallet
|
|
|
|
|
|
|
|
import (
|
2024-06-26 11:14:27 +00:00
|
|
|
"encoding/json"
|
2023-09-12 12:45:32 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
)
|
|
|
|
|
|
|
|
type KeycardPairings struct {
|
|
|
|
pairingsFile string
|
|
|
|
}
|
|
|
|
|
2024-06-26 11:14:27 +00:00
|
|
|
type KeycardPairing struct {
|
|
|
|
Key string `json:"key"`
|
|
|
|
Index int `json:"index"`
|
|
|
|
}
|
|
|
|
|
2023-09-12 12:45:32 +00:00
|
|
|
func NewKeycardPairings() *KeycardPairings {
|
|
|
|
return &KeycardPairings{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (kp *KeycardPairings) SetKeycardPairingsFile(filePath string) {
|
|
|
|
kp.pairingsFile = filePath
|
|
|
|
}
|
|
|
|
|
|
|
|
func (kp *KeycardPairings) GetPairingsJSONFileContent() ([]byte, error) {
|
|
|
|
_, err := os.Stat(kp.pairingsFile)
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return []byte{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return ioutil.ReadFile(kp.pairingsFile)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (kp *KeycardPairings) SetPairingsJSONFileContent(content []byte) error {
|
2023-10-12 20:44:41 +00:00
|
|
|
if len(content) == 0 {
|
|
|
|
// Nothing to write
|
|
|
|
return nil
|
|
|
|
}
|
2023-09-12 12:45:32 +00:00
|
|
|
_, err := os.Stat(kp.pairingsFile)
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
dir, _ := filepath.Split(kp.pairingsFile)
|
|
|
|
err = os.MkdirAll(dir, 0700)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ioutil.WriteFile(kp.pairingsFile, content, 0600)
|
|
|
|
}
|
2024-06-26 11:14:27 +00:00
|
|
|
|
|
|
|
func (kp *KeycardPairings) GetPairings() (map[string]KeycardPairing, error) {
|
|
|
|
content, err := kp.GetPairingsJSONFileContent()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(content) == 0 {
|
|
|
|
return nil, os.ErrNotExist
|
|
|
|
}
|
|
|
|
|
|
|
|
pairings := make(map[string]KeycardPairing)
|
|
|
|
err = json.Unmarshal(content, &pairings)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return pairings, nil
|
|
|
|
}
|