forked from MetaMask/eth-json-rpc-middleware
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwallet.js
200 lines (174 loc) · 6.86 KB
/
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
const createAsyncMiddleware = require('json-rpc-engine/src/createAsyncMiddleware')
const createScaffoldMiddleware = require('json-rpc-engine/src/createScaffoldMiddleware')
const sigUtil = require('eth-sig-util')
module.exports = createWalletMiddleware
function createWalletMiddleware(opts = {}) {
// parse + validate options
const getAccounts = opts.getAccounts
const processTypedMessage = opts.processTypedMessage
const processTypedMessageV0 = opts.processTypedMessageV0
const processTypedMessageV3 = opts.processTypedMessageV3
const processPersonalMessage = opts.processPersonalMessage
const processEthSignMessage = opts.processEthSignMessage
const processTransaction = opts.processTransaction
return createScaffoldMiddleware({
// account lookups
'eth_accounts': createAsyncMiddleware(lookupAccounts),
'eth_coinbase': createAsyncMiddleware(lookupDefaultAccount),
// tx signatures
'eth_sendTransaction': createAsyncMiddleware(sendTransaction),
// message signatures
'eth_sign': createAsyncMiddleware(ethSign),
'eth_signTypedData': createAsyncMiddleware(signTypedData),
'eth_signTypedData_v0': createAsyncMiddleware(signTypedDataV0),
'eth_signTypedData_v3': createAsyncMiddleware(signTypedDataV3),
'personal_sign': createAsyncMiddleware(personalSign),
'personal_ecRecover': createAsyncMiddleware(personalRecover),
})
//
// account lookups
//
async function lookupAccounts(req, res) {
if (!getAccounts) throw new Error('WalletMiddleware - opts.getAccounts not provided')
const accounts = await getAccounts(req)
res.result = accounts
}
async function lookupDefaultAccount(req, res) {
if (!getAccounts) throw new Error('WalletMiddleware - opts.getAccounts not provided')
const accounts = await getAccounts(req)
res.result = accounts[0] || null
}
//
// transaction signatures
//
async function sendTransaction(req, res) {
if (!processTransaction) throw new Error('WalletMiddleware - opts.processTransaction not provided')
const txParams = req.params[0] || {}
await validateSender(txParams.from, req)
res.result = await processTransaction(txParams, req)
}
//
// message signatures
//
async function ethSign(req, res) {
if (!processEthSignMessage) throw new Error('WalletMiddleware - opts.processEthSignMessage not provided')
// process normally
const address = req.params[0]
const message = req.params[1]
// non-standard "extraParams" to be appended to our "msgParams" obj
const extraParams = req.params[2] || {}
const msgParams = Object.assign({}, extraParams, {
from: address,
data: message,
})
await validateSender(address, req)
res.result = await processEthSignMessage(msgParams, req)
}
async function signTypedData (req, res) {
if (!processTypedMessage) throw new Error('WalletMiddleware - opts.processTypedMessage not provided')
const from = req.from
const message = req.params[0]
const address = req.params[1]
const version = 'V1'
const extraParams = req.params[2] || {}
const msgParams = Object.assign({}, extraParams, {
from: address,
data: message,
})
await validateSender(address, req)
await validateSender(from, req)
res.result = await processTypedMessage(msgParams, req, version)
}
async function signTypedDataV0 (req, res) {
if (!processTypedMessageV0) throw new Error('WalletMiddleware - opts.processTypedMessage not provided')
const from = req.from
const message = req.params[1]
const address = req.params[0]
const version = 'V0'
await validateSender(address, req)
await validateSender(from, req)
const msgParams = {
data: message,
from: address,
version
}
res.result = await processTypedMessageV0(msgParams, req, version)
}
async function signTypedDataV3 (req, res) {
if (!processTypedMessageV3) throw new Error('WalletMiddleware - opts.processTypedMessage not provided')
const from = req.from
const message = req.params[1]
const address = req.params[0]
const version = 'V3'
await validateSender(address, req)
await validateSender(from, req)
const msgParams = {
data: message,
from: address,
version
}
res.result = await processTypedMessageV3(msgParams, req, version)
}
async function personalSign (req, res) {
if (!processPersonalMessage) throw new Error('WalletMiddleware - opts.processPersonalMessage not provided')
// process normally
const firstParam = req.params[0]
const secondParam = req.params[1]
// non-standard "extraParams" to be appended to our "msgParams" obj
const extraParams = req.params[2] || {}
// 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 (resemblesAddress(firstParam) && !resemblesAddress(secondParam)) {
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.`
res.warning = warning
address = firstParam
message = secondParam
} else {
message = firstParam
address = secondParam
}
const msgParams = Object.assign({}, extraParams, {
from: address,
data: message,
})
await validateSender(address, req)
res.result = await processPersonalMessage(msgParams, req)
}
async function personalRecover(req, res) {
const message = req.params[0]
const signature = req.params[1]
// non-standard "extraParams" to be appended to our "msgParams" obj
const extraParams = req.params[2] || {}
const msgParams = Object.assign({}, extraParams, {
sig: signature,
data: message,
})
const senderHex = sigUtil.recoverPersonalSignature(msgParams)
res.result = senderHex
}
//
// utility
//
async function validateSender(address, req) {
// allow unspecified address (allow transaction signer to insert default)
if (!address) return
// ensure address is included in provided accounts
if (!getAccounts) throw new Error('WalletMiddleware - opts.getAccounts not provided')
const accounts = await getAccounts(req)
const normalizedAccounts = accounts.map(address => address.toLowerCase())
const normalizedAddress = address.toLowerCase()
if (!normalizedAccounts.includes(normalizedAddress)) throw new Error('WalletMiddleware - Invalid "from" address.')
}
}
function resemblesAddress (string) {
// hex prefix 2 + 20 bytes
return string.length === (2 + 20 * 2)
}