-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathwallet.go
More file actions
301 lines (254 loc) · 8.5 KB
/
Copy pathwallet.go
File metadata and controls
301 lines (254 loc) · 8.5 KB
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
package main
import (
"bytes"
"crypto/ed25519"
"encoding/base64"
"encoding/hex"
"fmt"
"syscall/js"
"go.sia.tech/core/consensus"
"go.sia.tech/core/types"
"go.sia.tech/coreutils/wallet"
)
// encodeToBytes encodes a value to bytes.
func encodeToBytes(v types.EncoderTo) []byte {
var buf bytes.Buffer
e := types.NewEncoder(&buf)
v.EncodeTo(e)
e.Flush()
return buf.Bytes()
}
// generateSeedPhrase returns a new seed phrase.
func generateSeedPhrase(this js.Value, args []js.Value) result {
if err := checkArgs(args); err != nil {
return resultErr(err)
}
return result(map[string]any{
"phrase": wallet.NewSeedPhrase(),
})
}
// generateKeyPair returns a new ed25519 key pair.
func generateKeyPair(this js.Value, args []js.Value) result {
if err := checkArgs(args); err != nil {
return resultErr(err)
}
pk := types.GeneratePrivateKey()
return result(map[string]any{
"privateKey": hex.EncodeToString(pk),
"publicKey": pk.PublicKey().String(),
})
}
// keyPairFromSeedPhrase returns the key pair corresponding to the given seed phrase and index.
func keyPairFromSeedPhrase(this js.Value, args []js.Value) result {
if err := checkArgs(args, js.TypeString, js.TypeNumber); err != nil {
return resultErr(err)
}
var seed [32]byte
if err := wallet.SeedFromPhrase(&seed, args[0].String()); err != nil {
return resultErr(err)
}
priv := wallet.KeyFromSeed(&seed, uint64(args[1].Int()))
return result(map[string]any{
"privateKey": hex.EncodeToString(priv),
"publicKey": priv.PublicKey().String(),
})
}
// standardUnlockConditions returns the unlock conditions for a standard v1 unlockhash.
func standardUnlockConditions(this js.Value, args []js.Value) result {
if err := checkArgs(args, js.TypeString); err != nil {
return resultErr(err)
}
var pub types.PublicKey
if pub.UnmarshalText([]byte(args[0].String())) != nil {
return resultErrStr("invalid public key")
}
uc := types.StandardUnlockConditions(pub)
obj, err := marshalStruct(uc)
if err != nil {
return resultErrStr(fmt.Sprintf("error marshaling unlock conditions: %s", err))
}
return result(map[string]any{
"unlockConditions": obj,
})
}
// standardUnlockHash returns the v1 unlockhash corresponding to the given public key.
func standardUnlockHash(this js.Value, args []js.Value) result {
if err := checkArgs(args, js.TypeString); err != nil {
return resultErr(err)
}
var pub types.PublicKey
if pub.UnmarshalText([]byte(args[0].String())) != nil {
return resultErrStr("invalid public key")
}
return result(map[string]any{
"address": types.StandardUnlockHash(pub).String(),
})
}
// addressFromUnlockConditions returns the address corresponding to the given unlock conditions.
func addressFromUnlockConditions(this js.Value, args []js.Value) result {
if err := checkArgs(args, js.TypeObject); err != nil {
return resultErr(err)
}
var uc types.UnlockConditions
if err := unmarshalStruct(args[0], &uc); err != nil {
return resultErr(err)
}
return result(map[string]any{
"address": uc.UnlockHash().String(),
})
}
// addressFromSpendPolicy returns the address of a spend policy.
func addressFromSpendPolicy(this js.Value, args []js.Value) result {
if err := checkArgs(args, js.TypeObject); err != nil {
return resultErr(err)
}
var sp types.SpendPolicy
if err := unmarshalStruct(args[0], &sp); err != nil {
return resultErr(err)
}
return result(map[string]any{
"address": sp.Address().String(),
})
}
// encodeTransaction returns the binary encoding of a transaction.
func encodeTransaction(this js.Value, args []js.Value) result {
if err := checkArgs(args, js.TypeObject); err != nil {
return resultErr(err)
}
var txn types.Transaction
if err := unmarshalStruct(args[0], &txn); err != nil {
return resultErrStr(fmt.Sprintf("error decoding transaction: %s", err))
}
encoded := encodeToBytes(txn)
return result(map[string]any{
"encodedTransaction": marshalUint8Array(encoded),
})
}
// encodeV2Transaction returns the V2TransactionSemantics encoding of a V2
// transaction.
func encodeV2Transaction(this js.Value, args []js.Value) result {
if err := checkArgs(args, js.TypeObject); err != nil {
return resultErr(err)
}
var txn types.V2Transaction
if err := unmarshalStruct(args[0], &txn); err != nil {
return resultErrStr(fmt.Sprintf("error decoding transaction: %s", err))
}
var buf bytes.Buffer
e := types.NewEncoder(&buf)
types.V2TransactionSemantics(txn).EncodeTo(e)
e.Flush()
return result(map[string]any{
"encodedTransaction": marshalUint8Array(buf.Bytes()),
})
}
// signTransactionV1 returns the signature of a transaction.
func signTransactionV1(this js.Value, args []js.Value) result {
if err := checkArgs(args, js.TypeObject, js.TypeObject, js.TypeObject, js.TypeNumber, js.TypeString); err != nil {
return resultErr(err)
}
var cs consensus.State
if err := unmarshalStruct(args[0], &cs); err != nil {
return resultErrStr(fmt.Sprintf("error decoding consensus state: %s", err))
}
var cn consensus.Network
if err := unmarshalStruct(args[1], &cn); err != nil {
return resultErrStr(fmt.Sprintf("error decoding consensus network: %s", err))
}
var txn types.Transaction
if err := unmarshalStruct(args[2], &txn); err != nil {
return resultErrStr(fmt.Sprintf("error decoding transaction: %s", err))
}
cs.Network = &cn
sigIndex := args[3].Int()
buf, err := hex.DecodeString(args[4].String())
if err != nil {
return resultErrStr(fmt.Sprintf("error decoding private key: %s", err))
} else if len(buf) != ed25519.PrivateKeySize {
return resultErrStr(fmt.Sprintf("invalid private key length: %d", len(buf)))
}
privateKey := types.PrivateKey(buf)
if sigIndex < 0 || sigIndex >= len(txn.Signatures) {
return resultErrStr(fmt.Sprintf("signature index out of range: %d", sigIndex))
}
tsig := &txn.Signatures[sigIndex]
var sigHash types.Hash256
if tsig.CoveredFields.WholeTransaction {
sigHash = cs.WholeSigHash(txn, tsig.ParentID, tsig.PublicKeyIndex, tsig.Timelock, tsig.CoveredFields.Signatures)
} else {
sigHash = cs.PartialSigHash(txn, tsig.CoveredFields)
}
sig := privateKey.SignHash(sigHash)
return result(map[string]any{
"signature": base64.StdEncoding.EncodeToString(sig[:]),
})
}
// TransactionID returns the ID of a transaction.
func transactionID(this js.Value, args []js.Value) result {
if err := checkArgs(args, js.TypeObject); err != nil {
return resultErr(err)
}
var txn types.Transaction
if err := unmarshalStruct(args[0], &txn); err != nil {
return resultErrStr(fmt.Sprintf("error decoding transaction: %s", err))
}
return result(map[string]any{
"id": txn.ID().String(),
})
}
// v2TransactionInputSigHash returns the input sigHash of a v2 transaction.
func v2TransactionInputSigHash(_ js.Value, args []js.Value) result {
if err := checkArgs(args, js.TypeObject, js.TypeObject, js.TypeObject); err != nil {
return resultErr(err)
}
var cs consensus.State
if err := unmarshalStruct(args[0], &cs); err != nil {
return resultErrStr(fmt.Sprintf("error decoding consensus state: %s", err))
}
var cn consensus.Network
if err := unmarshalStruct(args[1], &cn); err != nil {
return resultErrStr(fmt.Sprintf("error decoding consensus network: %s", err))
}
var txn types.V2Transaction
if err := unmarshalStruct(args[2], &txn); err != nil {
return resultErrStr(fmt.Sprintf("error decoding transaction: %s", err))
}
cs.Network = &cn
return result(map[string]any{
"sigHash": cs.InputSigHash(txn).String(),
})
}
// signHash returns the signature for a sig hash.
func signHash(_ js.Value, args []js.Value) result {
if err := checkArgs(args, js.TypeString, js.TypeString); err != nil {
return resultErr(err)
}
buf, err := hex.DecodeString(args[0].String())
if err != nil {
return resultErrStr(fmt.Sprintf("error decoding private key: %s", err))
} else if len(buf) != ed25519.PrivateKeySize {
return resultErrStr(fmt.Sprintf("invalid private key length: %d", len(buf)))
}
privateKey := types.PrivateKey(buf)
var sigHash types.Hash256
if err := sigHash.UnmarshalText([]byte(args[1].String())); err != nil {
return resultErrStr(fmt.Sprintf("error decoding sig hash: %s", err))
}
sig := privateKey.SignHash(sigHash)
return result(map[string]any{
"signature": sig.String(),
})
}
// v2TransactionID returns the ID of a transaction.
func v2TransactionID(_ js.Value, args []js.Value) result {
if err := checkArgs(args, js.TypeObject); err != nil {
return resultErr(err)
}
var txn types.V2Transaction
if err := unmarshalStruct(args[0], &txn); err != nil {
return resultErrStr(fmt.Sprintf("error decoding transaction: %s", err))
}
return result(map[string]any{
"id": txn.ID().String(),
})
}