This repository was archived by the owner on Apr 22, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 328
/
Copy pathhooked-wallet.js
704 lines (609 loc) · 22.2 KB
/
hooked-wallet.js
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
/*
* Emulate 'eth_accounts' / 'eth_sendTransaction' using 'eth_sendRawTransaction'
*
* The two callbacks a user needs to implement are:
* - getAccounts() -- array of addresses supported
* - signTransaction(tx) -- sign a raw transaction object
*/
const waterfall = require('async/waterfall')
const parallel = require('async/parallel')
const inherits = require('util').inherits
const ethUtil = require('@ethereumjs/util')
const sigUtil = require('@metamask/eth-sig-util')
const extend = require('xtend')
const Semaphore = require('semaphore')
const Subprovider = require('./subprovider.js')
const estimateGas = require('../util/estimate-gas.js')
const hexRegex = /^[0-9A-Fa-f]+$/g
module.exports = HookedWalletSubprovider
// handles the following RPC methods:
// eth_coinbase
// eth_accounts
// eth_sendTransaction
// eth_sign
// eth_signTypedData
// eth_signTypedData_v3
// eth_signTypedData_v4
// personal_sign
// eth_decryptMessage
// encryption_public_key
// personal_ecRecover
// parity_postTransaction
// parity_checkRequest
// parity_defaultAccount
//
// Tx Signature Flow
//
// handleRequest: eth_sendTransaction
// validateTransaction (basic validity check)
// validateSender (checks that sender is in accounts)
// processTransaction (sign tx and submit to network)
// approveTransaction (UI approval hook)
// checkApproval
// finalizeAndSubmitTx (tx signing)
// nonceLock.take (bottle neck to ensure atomic nonce)
// fillInTxExtras (set fallback gasPrice, nonce, etc)
// signTransaction (perform the signature)
// publishTransaction (publish signed tx to network)
//
inherits(HookedWalletSubprovider, Subprovider)
function HookedWalletSubprovider(opts){
const self = this
// control flow
self.nonceLock = Semaphore(1)
// data lookup
if (opts.getAccounts) self.getAccounts = opts.getAccounts
// high level override
if (opts.processTransaction) self.processTransaction = opts.processTransaction
if (opts.processMessage) self.processMessage = opts.processMessage
if (opts.processPersonalMessage) self.processPersonalMessage = opts.processPersonalMessage
if (opts.processTypedMessage) self.processTypedMessage = opts.processTypedMessage
// approval hooks
self.approveTransaction = opts.approveTransaction || self.autoApprove
self.approveMessage = opts.approveMessage || self.autoApprove
self.approvePersonalMessage = opts.approvePersonalMessage || self.autoApprove
self.approveDecryptMessage = opts.approveDecryptMessage || self.autoApprove
self.approveEncryptionPublicKey = opts.approveEncryptionPublicKey || self.autoApprove
self.approveTypedMessage = opts.approveTypedMessage || self.autoApprove
// actually perform the signature
if (opts.signTransaction) self.signTransaction = opts.signTransaction || mustProvideInConstructor('signTransaction')
if (opts.signMessage) self.signMessage = opts.signMessage || mustProvideInConstructor('signMessage')
if (opts.signPersonalMessage) self.signPersonalMessage = opts.signPersonalMessage || mustProvideInConstructor('signPersonalMessage')
if (opts.decryptMessage) self.decryptMessage = opts.decryptMessage || mustProvideInConstructor('decryptMessage')
if (opts.encryptionPublicKey) self.encryptionPublicKey = opts.encryptionPublicKey || mustProvideInConstructor('encryptionPublicKey')
if (opts.signTypedMessage) self.signTypedMessage = opts.signTypedMessage || mustProvideInConstructor('signTypedMessage')
if (opts.recoverPersonalSignature) self.recoverPersonalSignature = opts.recoverPersonalSignature
// publish to network
if (opts.publishTransaction) self.publishTransaction = opts.publishTransaction
// gas options
self.estimateGas = opts.estimateGas || self.estimateGas
self.getGasPrice = opts.getGasPrice || self.getGasPrice
}
HookedWalletSubprovider.prototype.handleRequest = function(payload, next, end){
const self = this
self._parityRequests = {}
self._parityRequestCount = 0
// switch statement is not block scoped
// sp we cant repeat var declarations
let txParams, msgParams, extraParams
let message, address
switch(payload.method) {
case 'eth_coinbase':
// process normally
self.getAccounts(function(err, accounts){
if (err) return end(err)
let result = accounts[0] || null
end(null, result)
})
return
case 'eth_accounts':
// process normally
self.getAccounts(function(err, accounts){
if (err) return end(err)
end(null, accounts)
})
return
case 'eth_sendTransaction':
txParams = payload.params[0]
waterfall([
(cb) => self.validateTransaction(txParams, cb),
(cb) => self.processTransaction(txParams, cb),
], end)
return
case 'eth_signTransaction':
txParams = payload.params[0]
waterfall([
(cb) => self.validateTransaction(txParams, cb),
(cb) => self.processSignTransaction(txParams, cb),
], end)
return
case 'eth_sign':
// process normally
address = payload.params[0]
message = payload.params[1]
// non-standard "extraParams" to be appended to our "msgParams" obj
// good place for metadata
extraParams = payload.params[2] || {}
msgParams = extend(extraParams, {
from: address,
data: message,
})
waterfall([
(cb) => self.validateMessage(msgParams, cb),
(cb) => self.processMessage(msgParams, cb),
], end)
return
case 'personal_sign':
return (function(){
// process normally
const first = payload.params[0]
const second = payload.params[1]
// We initially incorrectly ordered these parameters.
// To gracefully respect users who adopted this API early,
// we are currently gracefully recovering from the wrong param order
// when it is clearly identifiable.
//
// That means when the first param is definitely an address,
// and the second param is definitely not, but is hex.
if (resemblesData(second) && resemblesAddress(first)) {
let warning = `The eth_personalSign method requires params ordered `
warning += `[message, address]. This was previously handled incorrectly, `
warning += `and has been corrected automatically. `
warning += `Please switch this param order for smooth behavior in the future.`
console.warn(warning)
address = payload.params[0]
message = payload.params[1]
} else {
message = payload.params[0]
address = payload.params[1]
}
// non-standard "extraParams" to be appended to our "msgParams" obj
// good place for metadata
extraParams = payload.params[2] || {}
msgParams = extend(extraParams, {
from: address,
data: message,
})
waterfall([
(cb) => self.validatePersonalMessage(msgParams, cb),
(cb) => self.processPersonalMessage(msgParams, cb),
], end)
})()
case 'eth_decryptMessage':
return (function(){
// process normally
const first = payload.params[0]
const second = payload.params[1]
// We initially incorrectly ordered these parameters.
// To gracefully respect users who adopted this API early,
// we are currently gracefully recovering from the wrong param order
// when it is clearly identifiable.
//
// That means when the first param is definitely an address,
// and the second param is definitely not, but is hex.
if (resemblesData(second) && resemblesAddress(first)) {
let warning = `The eth_decryptMessage method requires params ordered `
warning += `[message, address]. This was previously handled incorrectly, `
warning += `and has been corrected automatically. `
warning += `Please switch this param order for smooth behavior in the future.`
console.warn(warning)
address = payload.params[0]
message = payload.params[1]
} else {
message = payload.params[0]
address = payload.params[1]
}
// non-standard "extraParams" to be appended to our "msgParams" obj
// good place for metadata
extraParams = payload.params[2] || {}
msgParams = extend(extraParams, {
from: address,
data: message,
})
waterfall([
(cb) => self.validateDecryptMessage(msgParams, cb),
(cb) => self.processDecryptMessage(msgParams, cb),
], end)
})()
case 'encryption_public_key':
return (function(){
const address = payload.params[0]
waterfall([
(cb) => self.validateEncryptionPublicKey(address, cb),
(cb) => self.processEncryptionPublicKey(address, cb),
], end)
})()
case 'personal_ecRecover':
return (function(){
message = payload.params[0]
let signature = payload.params[1]
// non-standard "extraParams" to be appended to our "msgParams" obj
// good place for metadata
extraParams = payload.params[2] || {}
msgParams = extend(extraParams, {
signature,
data: message,
})
self.recoverPersonalSignature(msgParams, end)
})()
case 'eth_signTypedData':
case 'eth_signTypedData_v3':
case 'eth_signTypedData_v4':
return (function(){
// process normally
const first = payload.params[0]
const second = payload.params[1]
if (resemblesAddress(first)) {
address = first
message = second
} else {
message = first
address = second
}
extraParams = payload.params[2] || {}
msgParams = extend(extraParams, {
from: address,
data: message,
})
waterfall([
(cb) => self.validateTypedMessage(msgParams, cb),
(cb) => self.processTypedMessage(msgParams, cb),
], end)
})()
case 'parity_postTransaction':
txParams = payload.params[0]
self.parityPostTransaction(txParams, end)
return
case 'parity_postSign':
address = payload.params[0]
message = payload.params[1]
self.parityPostSign(address, message, end)
return
case 'parity_checkRequest':
return (function(){
const requestId = payload.params[0]
self.parityCheckRequest(requestId, end)
})()
case 'parity_defaultAccount':
self.getAccounts(function(err, accounts){
if (err) return end(err)
const account = accounts[0] || null
end(null, account)
})
return
default:
next()
return
}
}
//
// data lookup
//
HookedWalletSubprovider.prototype.getAccounts = function(cb) {
cb(null, [])
}
//
// "process" high level flow
//
HookedWalletSubprovider.prototype.processTransaction = function(txParams, cb) {
const self = this
waterfall([
(cb) => self.approveTransaction(txParams, cb),
(didApprove, cb) => self.checkApproval('transaction', didApprove, cb),
(cb) => self.finalizeAndSubmitTx(txParams, cb),
], cb)
}
HookedWalletSubprovider.prototype.processSignTransaction = function(txParams, cb) {
const self = this
waterfall([
(cb) => self.approveTransaction(txParams, cb),
(didApprove, cb) => self.checkApproval('transaction', didApprove, cb),
(cb) => self.finalizeTx(txParams, cb),
], cb)
}
HookedWalletSubprovider.prototype.processMessage = function(msgParams, cb) {
const self = this
waterfall([
(cb) => self.approveMessage(msgParams, cb),
(didApprove, cb) => self.checkApproval('message', didApprove, cb),
(cb) => self.signMessage(msgParams, cb),
], cb)
}
HookedWalletSubprovider.prototype.processPersonalMessage = function(msgParams, cb) {
const self = this
waterfall([
(cb) => self.approvePersonalMessage(msgParams, cb),
(didApprove, cb) => self.checkApproval('message', didApprove, cb),
(cb) => self.signPersonalMessage(msgParams, cb),
], cb)
}
HookedWalletSubprovider.prototype.processDecryptMessage = function(msgParams, cb) {
const self = this
waterfall([
(cb) => self.approveDecryptMessage(msgParams, cb),
(didApprove, cb) => self.checkApproval('decryptMessage', didApprove, cb),
(cb) => self.decryptMessage(msgParams, cb),
], cb)
}
HookedWalletSubprovider.prototype.processEncryptionPublicKey = function(msgParams, cb) {
const self = this
waterfall([
(cb) => self.approveEncryptionPublicKey(msgParams, cb),
(didApprove, cb) => self.checkApproval('encryptionPublicKey', didApprove, cb),
(cb) => self.encryptionPublicKey(msgParams, cb),
], cb)
}
HookedWalletSubprovider.prototype.processTypedMessage = function(msgParams, cb) {
const self = this
waterfall([
(cb) => self.approveTypedMessage(msgParams, cb),
(didApprove, cb) => self.checkApproval('message', didApprove, cb),
(cb) => self.signTypedMessage(msgParams, cb),
], cb)
}
//
// approval
//
HookedWalletSubprovider.prototype.autoApprove = function(txParams, cb) {
cb(null, true)
}
HookedWalletSubprovider.prototype.checkApproval = function(type, didApprove, cb) {
cb( didApprove ? null : new Error('User denied '+type+' signature.') )
}
//
// parity
//
HookedWalletSubprovider.prototype.parityPostTransaction = function(txParams, cb) {
const self = this
// get next id
const count = self._parityRequestCount
const reqId = `0x${count.toString(16)}`
self._parityRequestCount++
self.emitPayload({
method: 'eth_sendTransaction',
params: [txParams],
}, function(error, res){
if (error) {
self._parityRequests[reqId] = { error }
return
}
const txHash = res.result
self._parityRequests[reqId] = txHash
})
cb(null, reqId)
}
HookedWalletSubprovider.prototype.parityPostSign = function(address, message, cb) {
const self = this
// get next id
const count = self._parityRequestCount
const reqId = `0x${count.toString(16)}`
self._parityRequestCount++
self.emitPayload({
method: 'eth_sign',
params: [address, message],
}, function(error, res){
if (error) {
self._parityRequests[reqId] = { error }
return
}
const result = res.result
self._parityRequests[reqId] = result
})
cb(null, reqId)
}
HookedWalletSubprovider.prototype.parityCheckRequest = function(reqId, cb) {
const self = this
const result = self._parityRequests[reqId] || null
// tx not handled yet
if (!result) return cb(null, null)
// tx was rejected (or other error)
if (result.error) return cb(result.error)
// tx sent
cb(null, result)
}
//
// signature and recovery
//
HookedWalletSubprovider.prototype.recoverPersonalSignature = function(msgParams, cb) {
let senderHex
try {
senderHex = sigUtil.recoverPersonalSignature(msgParams)
} catch (err) {
return cb(err)
}
cb(null, senderHex)
}
//
// validation
//
HookedWalletSubprovider.prototype.validateTransaction = function(txParams, cb){
const self = this
// shortcut: undefined sender is invalid
if (txParams.from === undefined) return cb(new Error(`Undefined address - from address required to sign transaction.`))
self.validateSender(txParams.from, function(err, senderIsValid){
if (err) return cb(err)
if (!senderIsValid) return cb(new Error(`Unknown address - unable to sign transaction for this address: "${txParams.from}"`))
cb()
})
}
HookedWalletSubprovider.prototype.validateMessage = function(msgParams, cb){
const self = this
if (msgParams.from === undefined) return cb(new Error(`Undefined address - from address required to sign message.`))
self.validateSender(msgParams.from, function(err, senderIsValid){
if (err) return cb(err)
if (!senderIsValid) return cb(new Error(`Unknown address - unable to sign message for this address: "${msgParams.from}"`))
cb()
})
}
HookedWalletSubprovider.prototype.validatePersonalMessage = function(msgParams, cb){
const self = this
if (msgParams.from === undefined) return cb(new Error(`Undefined address - from address required to sign personal message.`))
if (msgParams.data === undefined) return cb(new Error(`Undefined message - message required to sign personal message.`))
if (!isValidHex(msgParams.data)) return cb(new Error(`HookedWalletSubprovider - validateMessage - message was not encoded as hex.`))
self.validateSender(msgParams.from, function(err, senderIsValid){
if (err) return cb(err)
if (!senderIsValid) return cb(new Error(`Unknown address - unable to sign message for this address: "${msgParams.from}"`))
cb()
})
}
HookedWalletSubprovider.prototype.validateDecryptMessage = function(msgParams, cb){
const self = this
if (msgParams.from === undefined) return cb(new Error(`Undefined address - from address required to decrypt message.`))
if (msgParams.data === undefined) return cb(new Error(`Undefined message - message required to decrypt message.`))
if (!isValidHex(msgParams.data)) return cb(new Error(`HookedWalletSubprovider - validateDecryptMessage - message was not encoded as hex.`))
self.validateSender(msgParams.from, function(err, senderIsValid){
if (err) return cb(err)
if (!senderIsValid) return cb(new Error(`Unknown address - unable to decrypt message for this address: "${msgParams.from}"`))
cb()
})
}
HookedWalletSubprovider.prototype.validateEncryptionPublicKey = function(address, cb){
const self = this
self.validateSender(address, function(err, senderIsValid){
if (err) return cb(err)
if (!senderIsValid) return cb(new Error(`Unknown address - unable to obtain encryption public key for this address: "${address}"`))
cb()
})
}
HookedWalletSubprovider.prototype.validateTypedMessage = function(msgParams, cb){
if (msgParams.from === undefined) return cb(new Error(`Undefined address - from address required to sign typed data.`))
if (msgParams.data === undefined) return cb(new Error(`Undefined data - message required to sign typed data.`))
this.validateSender(msgParams.from, function(err, senderIsValid){
if (err) return cb(err)
if (!senderIsValid) return cb(new Error(`Unknown address - unable to sign message for this address: "${msgParams.from}"`))
cb()
})
}
HookedWalletSubprovider.prototype.validateSender = function(senderAddress, cb){
const self = this
// shortcut: undefined sender is invalid
if (!senderAddress) return cb(null, false)
self.getAccounts(function(err, accounts){
if (err) return cb(err)
const senderIsValid = (accounts.map(toLowerCase).indexOf(senderAddress.toLowerCase()) !== -1)
cb(null, senderIsValid)
})
}
//
// tx helpers
//
HookedWalletSubprovider.prototype.finalizeAndSubmitTx = function(txParams, cb) {
const self = this
// can only allow one tx to pass through this flow at a time
// so we can atomically consume a nonce
self.nonceLock.take(function(){
waterfall([
self.fillInTxExtras.bind(self, txParams),
self.signTransaction.bind(self),
self.publishTransaction.bind(self),
], function(err, txHash){
self.nonceLock.leave()
if (err) return cb(err)
cb(null, txHash)
})
})
}
HookedWalletSubprovider.prototype.finalizeTx = function(txParams, cb) {
const self = this
// can only allow one tx to pass through this flow at a time
// so we can atomically consume a nonce
self.nonceLock.take(function(){
waterfall([
self.fillInTxExtras.bind(self, txParams),
self.signTransaction.bind(self),
], function(err, signedTx){
self.nonceLock.leave()
if (err) return cb(err)
cb(null, {raw: signedTx, tx: txParams})
})
})
}
HookedWalletSubprovider.prototype.publishTransaction = function(rawTx, cb) {
const self = this
self.emitPayload({
method: 'eth_sendRawTransaction',
params: [rawTx],
}, function(err, res){
if (err) return cb(err)
cb(null, res.result)
})
}
HookedWalletSubprovider.prototype.estimateGas = function(txParams, cb) {
const self = this
estimateGas(self.engine, txParams, cb)
}
HookedWalletSubprovider.prototype.getGasPrice = function(cb) {
const self = this
self.emitPayload({ method: 'eth_gasPrice', params: [] }, function (err, res) {
if (err) return cb(err)
cb(null, res.result)
})
}
HookedWalletSubprovider.prototype.fillInTxExtras = function(txParams, cb){
const self = this
const address = txParams.from
// console.log('fillInTxExtras - address:', address)
const tasks = {}
if (txParams.gasPrice === undefined) {
// console.log("need to get gasprice")
tasks.gasPrice = self.getGasPrice.bind(self)
}
if (txParams.nonce === undefined) {
// console.log("need to get nonce")
tasks.nonce = self.emitPayload.bind(self, { method: 'eth_getTransactionCount', params: [address, 'pending'] })
}
if (txParams.gas === undefined) {
// console.log("need to get gas")
tasks.gas = self.estimateGas.bind(self, cloneTxParams(txParams))
}
parallel(tasks, function(err, taskResults) {
if (err) return cb(err)
const result = {}
if (taskResults.gasPrice) result.gasPrice = taskResults.gasPrice
if (taskResults.nonce) result.nonce = taskResults.nonce.result
if (taskResults.gas) result.gas = taskResults.gas
cb(null, extend(txParams, result))
})
}
// util
// we use this to clean any custom params from the txParams
function cloneTxParams(txParams){
return {
from: txParams.from,
to: txParams.to,
value: txParams.value,
data: txParams.data,
gas: txParams.gas,
gasPrice: txParams.gasPrice,
nonce: txParams.nonce,
}
}
function toLowerCase(string){
return string.toLowerCase()
}
function resemblesAddress (string) {
const fixed = ethUtil.addHexPrefix(string)
const isValid = ethUtil.isValidAddress(fixed)
return isValid
}
// Returns true if resembles hex data
// but definitely not a valid address.
function resemblesData (string) {
const fixed = ethUtil.addHexPrefix(string)
const isValidAddress = ethUtil.isValidAddress(fixed)
return !isValidAddress && isValidHex(string)
}
function isValidHex(data) {
const isString = typeof data === 'string'
if (!isString) return false
const isHexPrefixed = data.slice(0,2) === '0x'
if (!isHexPrefixed) return false
const nonPrefixed = data.slice(2)
const isValid = nonPrefixed.match(hexRegex)
return isValid
}
function mustProvideInConstructor(methodName) {
return function(params, cb) {
cb(new Error('ProviderEngine - HookedWalletSubprovider - Must provide "' + methodName + '" fn in constructor options'))
}
}