-
Notifications
You must be signed in to change notification settings - Fork 50
/
account.go
104 lines (91 loc) · 2.75 KB
/
account.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Copyright (c) 2019. Baidu Inc. All Rights Reserved.
// package account is related to account operation
package account
import (
"log"
"github.com/xuperchain/xuper-sdk-go/common"
"github.com/xuperchain/xuper-sdk-go/crypto"
)
// Account account structure
type Account struct {
Address string
PrivateKey string
PublicKey string
Mnemonic string
}
// CreateAccount create an account
func CreateAccount(strength uint8, language int) (*Account, error) {
cli := crypto.GetCryptoClient()
ecdsaAccount, err := cli.CreateNewAccountWithMnemonic(language, strength)
if err != nil {
log.Printf("CreateAccount CreateNewAccountWithMnemonic err: %v", err)
return nil, err
}
account := &Account{
Address: ecdsaAccount.Address,
PublicKey: ecdsaAccount.JsonPublicKey,
PrivateKey: ecdsaAccount.JsonPrivateKey,
Mnemonic: ecdsaAccount.Mnemonic,
}
return account, nil
}
// RetrieveAccount retrieve account from mnemonic
func RetrieveAccount(mnemonic string, language int) (*Account, error) {
cli := crypto.GetCryptoClient()
ecdsaAccount, err := cli.RetrieveAccountByMnemonic(mnemonic, language)
if err != nil {
return nil, err
}
account := &Account{
Address: ecdsaAccount.Address,
PublicKey: ecdsaAccount.JsonPublicKey,
PrivateKey: ecdsaAccount.JsonPrivateKey,
Mnemonic: ecdsaAccount.Mnemonic,
}
return account, nil
}
// CreateAndSaveAccountToFile create an account and save to file
func CreateAndSaveAccountToFile(path, passwd string, strength uint8, language int) (*Account, error) {
cli := crypto.GetCryptoClient()
ecdsaAccount, err := cli.CreateNewAccountWithMnemonic(language, strength)
if err != nil {
return nil, err
}
err = common.PathExistsAndMkdir(path)
if err != nil {
return nil, err
}
_, err = cli.RetrieveAccountByMnemonicAndSavePrivKey(path, language, ecdsaAccount.Mnemonic, passwd)
if err != nil {
return nil, err
}
account := &Account{
Address: ecdsaAccount.Address,
PublicKey: ecdsaAccount.JsonPublicKey,
PrivateKey: ecdsaAccount.JsonPrivateKey,
Mnemonic: ecdsaAccount.Mnemonic,
}
return account, nil
}
// GetAccountFromFile get an account from file
func GetAccountFromFile(path, passwd string) (*Account, error) {
cryptoClient := crypto.GetCryptoClient()
ecdsaPrivateKey, err := cryptoClient.GetEcdsaPrivateKeyFromFileByPassword(path, passwd)
if err != nil {
return nil, err
}
account := &Account{}
account.PrivateKey, err = cryptoClient.GetEcdsaPrivateKeyJsonFormatStr(ecdsaPrivateKey)
if err != nil {
return nil, err
}
account.PublicKey, err = cryptoClient.GetEcdsaPublicKeyJsonFormatStr(ecdsaPrivateKey)
if err != nil {
return nil, err
}
account.Address, err = cryptoClient.GetAddressFromPublicKey(&ecdsaPrivateKey.PublicKey)
if err != nil {
return nil, err
}
return account, err
}