10 "github.com/pborman/uuid"
11 "github.com/vapor/crypto/ed25519/chainkd"
19 // XKey struct type for keystore file
28 type keyStore interface {
29 // Loads and decrypts the key from disk.
30 GetKey(alias string, filename string, auth string) (*XKey, error)
31 // Writes and encrypts the key.
32 StoreKey(filename string, k *XKey, auth string) error
33 // Joins filename with the key directory unless it is already absolute.
34 JoinPath(filename string) string
37 type encryptedKeyJSON struct {
38 Crypto cryptoJSON `json:"crypto"`
40 Type string `json:"type"`
41 Version int `json:"version"`
42 Alias string `json:"alias"`
43 XPub string `json:"xpub"`
46 type cryptoJSON struct {
47 Cipher string `json:"cipher"`
48 CipherText string `json:"ciphertext"`
49 CipherParams cipherparamsJSON `json:"cipherparams"`
50 KDF string `json:"kdf"`
51 KDFParams map[string]interface{} `json:"kdfparams"`
52 MAC string `json:"mac"`
55 type cipherparamsJSON struct {
59 type scryptParamsJSON struct {
63 DkLen int `json:"dklen"`
64 Salt string `json:"salt"`
67 func writeKeyFile(file string, content []byte) error {
68 // Create the keystore directory with appropriate permissions
69 // in case it is not present yet.
71 if err := os.MkdirAll(filepath.Dir(file), dirPerm); err != nil {
74 // Atomic write: create a temporary hidden file first
75 // then move it into place. TempFile assigns mode 0600.
76 f, err := ioutil.TempFile(filepath.Dir(file), "."+filepath.Base(file)+".tmp")
80 if _, err := f.Write(content); err != nil {
86 return os.Rename(f.Name(), file)
89 func zeroKey(k *XKey) {
96 // keyFileName implements the naming convention for keyfiles:
97 // UTC--<created_at UTC ISO8601>-<address hex>
98 func keyFileName(keyId string) string {
99 ts := time.Now().UTC()
100 return fmt.Sprintf("UTC--%s--%s", toISO8601(ts), keyId)
103 func toISO8601(t time.Time) string {
105 name, offset := t.Zone()
109 tz = fmt.Sprintf("%03d00", offset/3600)
111 return fmt.Sprintf("%04d-%02d-%02dT%02d-%02d-%02d.%09d%s", t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second(), t.Nanosecond(), tz)