forked from coinbase/mesh-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
key_storage.go
284 lines (243 loc) · 7.62 KB
/
key_storage.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
// Copyright 2020 Coinbase, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package modules
import (
"context"
"errors"
"fmt"
"math/big"
"github.com/guapcrypto/rosetta-sdk-go/keys"
"github.com/guapcrypto/rosetta-sdk-go/storage/database"
storageErrs "github.com/guapcrypto/rosetta-sdk-go/storage/errors"
"github.com/guapcrypto/rosetta-sdk-go/types"
"github.com/guapcrypto/rosetta-sdk-go/utils"
)
// WARNING: KEY STORAGE USING THIS PACKAGE IS NOT SECURE!!!! ONLY USE
// FOR TESTING!!!!
// PrefundedAccount is used to load prefunded addresses into key storage.
type PrefundedAccount struct {
PrivateKeyHex string `json:"privkey"`
AccountIdentifier *types.AccountIdentifier `json:"account_identifier"`
CurveType types.CurveType `json:"curve_type"`
Currency *types.Currency `json:"currency"`
}
const (
keyNamespace = "key"
)
func getAccountKey(account *types.AccountIdentifier) []byte {
return []byte(
fmt.Sprintf("%s/%s", keyNamespace, types.Hash(account)),
)
}
// KeyStorage implements key storage methods
// on top of a database.Database and database.Transaction interface.
type KeyStorage struct {
db database.Database
}
// NewKeyStorage returns a new KeyStorage.
func NewKeyStorage(
db database.Database,
) *KeyStorage {
return &KeyStorage{
db: db,
}
}
// Key is the struct stored in key storage. This
// is public so that accounts can be loaded from
// a configuration file.
type Key struct {
Account *types.AccountIdentifier `json:"account"`
KeyPair *keys.KeyPair `json:"keypair"`
}
// StoreTransactional stores a key in a database transaction.
func (k *KeyStorage) StoreTransactional(
ctx context.Context,
account *types.AccountIdentifier,
keyPair *keys.KeyPair,
dbTx database.Transaction,
) error {
exists, _, err := dbTx.Get(ctx, getAccountKey(account))
if err != nil {
return fmt.Errorf(
"%w: %s %v",
storageErrs.ErrAddrCheckIfExistsFailed,
types.PrintStruct(account),
err,
)
}
if exists {
return fmt.Errorf(
"%w: account %s already exists",
storageErrs.ErrAddrExists,
types.PrintStruct(account),
)
}
val, err := k.db.Encoder().Encode("", &Key{
Account: account,
KeyPair: keyPair,
})
if err != nil {
return fmt.Errorf("%w: %v", storageErrs.ErrSerializeKeyFailed, err)
}
err = dbTx.Set(ctx, getAccountKey(account), val, true)
if err != nil {
return fmt.Errorf("%w: %v", storageErrs.ErrStoreKeyFailed, err)
}
return nil
}
// Store saves a keys.KeyPair for a given address. If the address already
// exists, an error is returned.
func (k *KeyStorage) Store(
ctx context.Context,
account *types.AccountIdentifier,
keyPair *keys.KeyPair,
) error {
dbTx := k.db.Transaction(ctx)
defer dbTx.Discard(ctx)
if err := k.StoreTransactional(ctx, account, keyPair, dbTx); err != nil {
return fmt.Errorf("%w: unable to store key", err)
}
if err := dbTx.Commit(ctx); err != nil {
return fmt.Errorf("%w: %v", storageErrs.ErrCommitKeyFailed, err)
}
return nil
}
// GetTransactional returns a *keys.KeyPair for an AccountIdentifier in a
// database.Transaction, if it exists.
func (k *KeyStorage) GetTransactional(
ctx context.Context,
dbTx database.Transaction,
account *types.AccountIdentifier,
) (*keys.KeyPair, error) {
exists, rawKey, err := dbTx.Get(ctx, getAccountKey(account))
if err != nil {
return nil, fmt.Errorf(
"%w: %s %v",
storageErrs.ErrAddrGetFailed,
types.PrintStruct(account),
err,
)
}
if !exists {
return nil, fmt.Errorf("%w: %s", storageErrs.ErrAddrNotFound, types.PrintStruct(account))
}
var kp Key
if err := k.db.Encoder().Decode("", rawKey, &kp, true); err != nil {
return nil, fmt.Errorf("%w: %v", storageErrs.ErrParseSavedKeyFailed, err)
}
return kp.KeyPair, nil
}
// Get returns a *keys.KeyPair for an AccountIdentifier, if it exists.
func (k *KeyStorage) Get(
ctx context.Context,
account *types.AccountIdentifier,
) (*keys.KeyPair, error) {
transaction := k.db.ReadTransaction(ctx)
defer transaction.Discard(ctx)
return k.GetTransactional(ctx, transaction, account)
}
// GetAllAccountsTransactional returns all AccountIdentifiers in key storage.
func (k *KeyStorage) GetAllAccountsTransactional(
ctx context.Context,
dbTx database.Transaction,
) ([]*types.AccountIdentifier, error) {
accounts := []*types.AccountIdentifier{}
_, err := dbTx.Scan(
ctx,
[]byte(keyNamespace),
[]byte(keyNamespace),
func(key []byte, v []byte) error {
var kp Key
// We should not reclaim memory during a scan!!
if err := k.db.Encoder().Decode("", v, &kp, false); err != nil {
return fmt.Errorf("%w: %v", storageErrs.ErrKeyScanFailed, err)
}
accounts = append(accounts, kp.Account)
return nil
},
false,
false,
)
if err != nil {
return nil, fmt.Errorf("%w: %v", storageErrs.ErrKeyScanFailed, err)
}
return accounts, nil
}
// GetAllAccounts returns all AccountIdentifiers in key storage.
func (k *KeyStorage) GetAllAccounts(ctx context.Context) ([]*types.AccountIdentifier, error) {
dbTx := k.db.ReadTransaction(ctx)
defer dbTx.Discard(ctx)
return k.GetAllAccountsTransactional(ctx, dbTx)
}
// Sign attempts to sign a slice of *types.SigningPayload with the keys in KeyStorage.
func (k *KeyStorage) Sign(
ctx context.Context,
payloads []*types.SigningPayload,
) ([]*types.Signature, error) {
signatures := make([]*types.Signature, len(payloads))
for i, payload := range payloads {
keyPair, err := k.Get(ctx, payload.AccountIdentifier)
if err != nil {
return nil, fmt.Errorf(
"%w for %s: %v",
storageErrs.ErrKeyGetFailed,
types.PrintStruct(payload.AccountIdentifier),
err,
)
}
signer, err := keyPair.Signer()
if err != nil {
return nil, fmt.Errorf("%w: %v", storageErrs.ErrSignerCreateFailed, err)
}
if len(payload.SignatureType) == 0 {
return nil, fmt.Errorf("%w %d", storageErrs.ErrDetermineSigTypeFailed, i)
}
signature, err := signer.Sign(payload, payload.SignatureType)
if err != nil {
return nil, fmt.Errorf("%w for %d: %v", storageErrs.ErrSignPayloadFailed, i, err)
}
signatures[i] = signature
}
return signatures, nil
}
// RandomAccount returns a random account from all accounts.
func (k *KeyStorage) RandomAccount(ctx context.Context) (*types.AccountIdentifier, error) {
accounts, err := k.GetAllAccounts(ctx)
if err != nil {
return nil, fmt.Errorf("%w: %v", storageErrs.ErrAddrsGetAllFailed, err)
}
if len(accounts) == 0 {
return nil, storageErrs.ErrNoAddrAvailable
}
randomNumber := utils.RandomNumber(big.NewInt(0), big.NewInt(int64(len(accounts))))
return accounts[randomNumber.Int64()], nil
}
// ImportAccounts loads a set of prefunded accounts into key storage.
func (k *KeyStorage) ImportAccounts(ctx context.Context, accounts []*PrefundedAccount) error {
for _, acc := range accounts {
keyPair, err := keys.ImportPrivateKey(acc.PrivateKeyHex, acc.CurveType)
if err != nil {
return fmt.Errorf("%w: %v", storageErrs.ErrAddrImportFailed, err)
}
// Skip if key already exists
err = k.Store(ctx, acc.AccountIdentifier, keyPair)
if errors.Is(err, storageErrs.ErrAddrExists) {
continue
}
if err != nil {
return fmt.Errorf("%w: %v", storageErrs.ErrPrefundedAcctStoreFailed, err)
}
}
return nil
}