-
Notifications
You must be signed in to change notification settings - Fork 2
/
util.go
305 lines (270 loc) · 8.47 KB
/
util.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
package ethereum
import (
"bytes"
"context"
"crypto/ecdsa"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"math/big"
"os"
"strings"
ethereum "github.com/ethereum/go-ethereum"
accounts "github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
rpc "github.com/ethereum/go-ethereum/rpc"
"github.com/hashicorp/vault/logical"
)
const (
ProtocolKeystore string = "keystore://"
MaxKeystoreSize int64 = 1024 // Just a heuristic to prevent reading stupid big files
RequestPathImport string = "import"
RequestPathAccounts string = "accounts"
PassphraseWords int = 9
PassphraseSeparator string = "-"
)
func prettyPrint(v interface{}) string {
jsonString, _ := json.Marshal(v)
var out bytes.Buffer
json.Indent(&out, jsonString, "", " ")
return out.String()
}
func (b *backend) writeTemporaryKeystoreFile(path string, filename string, data []byte) (string, error) {
keystorePath := path + "/" + filename
err := ioutil.WriteFile(keystorePath, data, 0644)
return keystorePath, err
}
func (b *backend) createTemporaryKeystoreDirectory() (string, error) {
dir, err := ioutil.TempDir("", "keystore")
return dir, err
}
func (b *backend) removeTemporaryKeystore(path string) error {
return os.RemoveAll(path)
}
func convertMapToStringValue(initial map[string]interface{}) map[string]string {
result := map[string]string{}
for key, value := range initial {
result[key] = fmt.Sprintf("%v", value)
}
return result
}
func parseURL(url string) (accounts.URL, error) {
parts := strings.Split(url, "://")
if len(parts) != 2 || parts[0] == "" {
return accounts.URL{}, errors.New("protocol scheme missing")
}
return accounts.URL{
Scheme: parts[0],
Path: parts[1],
}, nil
}
func (b *backend) rekeyJSONKeystore(keystorePath string, passphrase string, newPassphrase string) ([]byte, error) {
var key *keystore.Key
jsonKeystore, err := b.readJSONKeystore(keystorePath)
if err != nil {
return nil, err
}
key, _ = keystore.DecryptKey(jsonKeystore, passphrase)
if key != nil && key.PrivateKey != nil {
defer zeroKey(key.PrivateKey)
}
jsonBytes, err := keystore.EncryptKey(key, newPassphrase, keystore.StandardScryptN, keystore.StandardScryptP)
return jsonBytes, err
}
func (b *backend) readKeyFromJSONKeystore(keystorePath string, passphrase string) (*keystore.Key, error) {
var key *keystore.Key
jsonKeystore, err := b.readJSONKeystore(keystorePath)
if err != nil {
return nil, err
}
key, _ = keystore.DecryptKey(jsonKeystore, passphrase)
if key != nil && key.PrivateKey != nil {
return key, nil
} else {
return nil, fmt.Errorf("failed to read key from keystore")
}
}
func zeroKey(k *ecdsa.PrivateKey) {
b := k.D.Bits()
for i := range b {
b[i] = 0
}
}
func (b *backend) importJSONKeystore(ctx context.Context, keystorePath string, passphrase string) (string, []byte, error) {
var key *keystore.Key
jsonKeystore, err := b.readJSONKeystore(keystorePath)
if err != nil {
return "", nil, err
}
key, err = keystore.DecryptKey(jsonKeystore, passphrase)
if err != nil {
return "", nil, err
}
if key != nil && key.PrivateKey != nil {
defer zeroKey(key.PrivateKey)
}
return key.Address.Hex(), jsonKeystore, err
}
func pathExists(ctx context.Context, req *logical.Request, path string) (bool, error) {
out, err := req.Storage.Get(ctx, path)
if err != nil {
return false, fmt.Errorf("existence check failed for %s: %v", path, err)
}
return out != nil, nil
}
func (b *backend) readJSONKeystore(keystorePath string) ([]byte, error) {
var jsonKeystore []byte
file, err := os.Open(keystorePath)
defer file.Close()
stat, err := file.Stat()
if err != nil {
return nil, err
}
if stat.Size() > MaxKeystoreSize {
err = fmt.Errorf("keystore is suspiciously large at %d bytes", stat.Size())
return nil, err
} else {
jsonKeystore, err = ioutil.ReadFile(keystorePath)
if err != nil {
return nil, err
}
return jsonKeystore, nil
}
}
func (b *backend) NewTransactor(key *ecdsa.PrivateKey) *bind.TransactOpts {
keyAddr := crypto.PubkeyToAddress(key.PublicKey)
return &bind.TransactOpts{
From: keyAddr,
Signer: func(signer types.Signer, address common.Address, tx *types.Transaction) (*types.Transaction, error) {
if address != keyAddr {
return nil, errors.New("not authorized to sign this account")
}
signature, err := crypto.Sign(signer.Hash(tx).Bytes(), key)
if err != nil {
return nil, err
}
return tx.WithSignature(signer, signature)
},
}
}
func (b *backend) getAccountPrivateKey(path string, account Account) (*keystore.Key, error) {
tmpDir, err := b.createTemporaryKeystoreDirectory()
if err != nil {
return nil, err
}
keystorePath, err := b.writeTemporaryKeystoreFile(tmpDir, account.KeystoreName, account.JSONKeystore)
if err != nil {
return nil, err
}
key, err := b.readKeyFromJSONKeystore(keystorePath, account.Passphrase)
if err != nil {
return nil, err
}
err = b.removeTemporaryKeystore(tmpDir)
return key, err
}
func (b *backend) exportKeystore(path string, account *Account) (string, error) {
directory, err := b.writeTemporaryKeystoreFile(path, account.KeystoreName, account.JSONKeystore)
return directory, err
}
func (b *backend) getEthereumClient(ctx context.Context, rpcURL string) (*ethclient.Client, error) {
client, err := rpc.Dial(rpcURL)
if err != nil {
return nil, err
}
ethClient := ethclient.NewClient(client)
return ethClient, nil
}
func (b *backend) readBalance(ctx context.Context, client *ethclient.Client, account *Account) (*Account, error) {
address := common.HexToAddress(account.Address)
accountBalance := account
pendingBalance, err := client.PendingBalanceAt(ctx, address)
if err != nil {
return nil, err
}
pendingNonce, err := client.PendingNonceAt(ctx, address)
if err != nil {
return nil, err
}
pendingTxCount, err := client.PendingTransactionCount(ctx)
if err != nil {
return nil, err
}
accountBalance.PendingNonce = pendingNonce
accountBalance.PendingBalance = pendingBalance
accountBalance.PendingTxCount = pendingTxCount
return accountBalance, nil
}
func (b *backend) readAccount(ctx context.Context, req *logical.Request, path string) (*Account, error) {
entry, err := req.Storage.Get(ctx, path)
if err != nil {
return nil, fmt.Errorf("failed to find account at %s", path)
}
var account Account
err = entry.DecodeJSON(&account)
if entry == nil {
return nil, fmt.Errorf("failed to deserialize account at %s", path)
}
return &account, nil
}
func (b *backend) contains(stringSlice []string, searchString string) bool {
for _, value := range stringSlice {
if value == searchString {
return true
}
}
return false
}
func contains(stringSlice []string, searchString string) bool {
for _, value := range stringSlice {
if value == searchString {
return true
}
}
return false
}
func (b *backend) getEstimates(client *ethclient.Client, ctx context.Context, fromAddress common.Address, toAddress *common.Address, data []byte, value *big.Int) (uint64, *big.Int, error) {
msg := ethereum.CallMsg{From: fromAddress, To: toAddress, Data: data, Value: value}
gasLimit, err := client.EstimateGas(ctx, msg)
if err != nil {
return 0, nil, err
}
gasPrice, err := client.SuggestGasPrice(ctx)
if err != nil {
return 0, nil, err
}
return gasLimit, gasPrice, nil
}
func dedup(stringSlice []string) []string {
var returnSlice []string
for _, value := range stringSlice {
if !contains(returnSlice, value) {
returnSlice = append(returnSlice, value)
}
}
return returnSlice
}
func (b *backend) isDebitAllowed(account *Account, toAddress string, amount *big.Int) (bool, error) {
if b.contains(account.Blacklist, toAddress) {
return false, fmt.Errorf("%s is blacklisted", toAddress)
}
if len(account.Whitelist) > 0 && !b.contains(account.Whitelist, toAddress) {
return false, fmt.Errorf("%s is not in the whitelist", toAddress)
}
if amount.Cmp(account.PendingBalance) > 0 {
return false, fmt.Errorf("Insufficient funds to debit %v because the current account balance is %v", amount, account.PendingBalance)
}
return true, nil
}
func (b *backend) isDeployAllowed(account *Account, amount *big.Int) (bool, error) {
if amount.Cmp(account.PendingBalance) > 0 {
return false, fmt.Errorf("Insufficient funds to fund contract with %v because the current account balance is %v", amount, account.PendingBalance)
}
return true, nil
}