forked from aurora-is-near/near-api-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccount.go
198 lines (176 loc) · 4.69 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package near
import (
"crypto/ed25519"
"encoding/json"
"math/big"
"strconv"
"github.com/aurora-is-near/near-api-go/keystore"
"github.com/aurora-is-near/near-api-go/utils"
"github.com/btcsuite/btcutil/base58"
"github.com/near/borsh-go"
)
const ed25519Prefix = "ed25519:"
// Default number of retries with different nonce before giving up on a transaction.
const txNonceRetryNumber = 12
// Default wait until next retry in milli seconds.
const txNonceRetryWait = 500
// Exponential back off for waiting to retry.
const txNonceRetryWaitBackoff = 1.5
// Account defines access credentials for a NEAR account.
type Account struct {
conn *Connection
kp *keystore.Ed25519KeyPair
accessKeyByPublicKeyCache map[string]map[string]interface{}
}
// LoadAccount loads the credential for the receiverID account, to be used via
// connection c, and returns it.
func LoadAccount(c *Connection, cfg *Config, receiverID string) (*Account, error) {
var (
a Account
err error
)
a.conn = c
if cfg.KeyPath != "" {
a.kp, err = keystore.LoadKeyPairFromPath(cfg.KeyPath, receiverID)
} else {
a.kp, err = keystore.LoadKeyPair(cfg.NetworkID, receiverID)
}
if err != nil {
return nil, err
}
a.accessKeyByPublicKeyCache = make(map[string]map[string]interface{})
return &a, nil
}
// SendMoney sends amount NEAR from account to receiverID.
func (a *Account) SendMoney(
receiverID string,
amount big.Int,
) (map[string]interface{}, error) {
return a.SignAndSendTransaction(receiverID, []Action{
{
Enum: 3,
Transfer: Transfer{
Deposit: amount,
},
},
})
}
// CreateAccount creates the newAccountID with the given publicKey and amount.
func (a *Account) CreateAccount(
newAccountID string,
publicKey utils.PublicKey,
amount big.Int,
) (map[string]interface{}, error) {
return a.SignAndSendTransaction(newAccountID, []Action{
{
Enum: 0,
CreateAccount: 0,
},
{
Enum: 3,
Transfer: Transfer{
Deposit: amount,
},
},
{
Enum: 5,
AddKey: AddKey{
PublicKey: publicKey,
AccessKey: fullAccessKey(),
},
},
})
}
// DeleteAccount deletes the account and sends the remaining Ⓝ balance to the
// account beneficiaryID.
func (a *Account) DeleteAccount(
beneficiaryID string,
) (map[string]interface{}, error) {
return a.SignAndSendTransaction(a.kp.AccountID, []Action{
{
Enum: 7,
DeleteAccount: DeleteAccount{
BeneficiaryID: beneficiaryID,
},
},
})
}
// SignAndSendTransaction signs the given actions and sends them as a transaction to receiverID.
func (a *Account) SignAndSendTransaction(
receiverID string,
actions []Action,
) (map[string]interface{}, error) {
return utils.ExponentialBackoff(txNonceRetryWait, txNonceRetryNumber, txNonceRetryWaitBackoff,
func() (map[string]interface{}, error) {
_, signedTx, err := a.signTransaction(receiverID, actions)
if err != nil {
return nil, err
}
buf, err := borsh.Serialize(*signedTx)
if err != nil {
return nil, err
}
return a.conn.SendTransaction(buf)
})
}
func (a *Account) signTransaction(
receiverID string,
actions []Action,
) (txHash []byte, signedTx *SignedTransaction, err error) {
_, ak, err := a.findAccessKey()
if err != nil {
return nil, nil, err
}
// get current block hash
block, err := a.conn.Block()
if err != nil {
return nil, nil, err
}
blockHash := block["header"].(map[string]interface{})["hash"].(string)
// create next nonce
var nonce int64
jsonNonce, ok := ak["nonce"].(json.Number)
if ok {
nonce, err = jsonNonce.Int64()
if err != nil {
return nil, nil, err
}
nonce++
}
// save nonce
ak["nonce"] = json.Number(strconv.FormatInt(nonce, 10))
// sign transaction
return signTransaction(receiverID, uint64(nonce), actions, base58.Decode(blockHash),
a.kp.Ed25519PubKey, a.kp.Ed25519PrivKey, a.kp.AccountID)
}
func (a *Account) findAccessKey() (publicKey ed25519.PublicKey, accessKey map[string]interface{}, err error) {
// TODO: Find matching access key based on transaction
// TODO: use accountId and networkId?
pk := a.kp.Ed25519PubKey
if ak := a.accessKeyByPublicKeyCache[string(publicKey)]; ak != nil {
return pk, ak, nil
}
ak, err := a.conn.ViewAccessKey(a.kp.AccountID, a.kp.PublicKey)
if err != nil {
return nil, nil, err
}
a.accessKeyByPublicKeyCache[string(publicKey)] = ak
return pk, ak, nil
}
// FunctionCall performs a NEAR function call.
func (a *Account) FunctionCall(
contractID, methodName string,
args []byte,
gas uint64,
amount big.Int,
) (map[string]interface{}, error) {
return a.SignAndSendTransaction(contractID, []Action{{
Enum: 2,
FunctionCall: FunctionCall{
MethodName: methodName,
Args: args,
Gas: gas,
Deposit: amount,
},
}})
}