/
migration.go
45 lines (39 loc) · 1.07 KB
/
migration.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package encryption
import (
"encoding/json"
"io/ioutil"
"os"
"github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/ethereum/go-ethereum/common"
"github.com/TechPay-io/go-photon/inter/validatorpk"
)
type encryptedAccountKeyJSONV3 struct {
Address string `json:"address"`
Crypto keystore.CryptoJSON `json:"crypto"`
Id string `json:"id"`
Version int `json:"version"`
}
func MigrateAccountToValidatorKey(acckeypath string, valkeypath string, pubkey validatorpk.PubKey) error {
acckeyjson, err := ioutil.ReadFile(acckeypath)
if err != nil {
return err
}
acck := new(encryptedAccountKeyJSONV3)
if err := json.Unmarshal(acckeyjson, acck); err != nil {
return err
}
valk := EncryptedKeyJSON{
Type: validatorpk.Types.Secp256k1,
PublicKey: common.Bytes2Hex(pubkey.Raw),
Crypto: acck.Crypto,
}
valkeyjson, err := json.Marshal(valk)
if err != nil {
return err
}
tmpName, err := writeTemporaryKeyFile(valkeypath, valkeyjson)
if err != nil {
return err
}
return os.Rename(tmpName, valkeypath)
}