-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcode.js
244 lines (227 loc) · 7.33 KB
/
code.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
const ethUtil = require('@ethereumjs/util')
const rlp = require('@ethereumjs/rlp')
const {Transaction, FeeMarketEIP_1559Transaction} = require('@ethereumjs/tx')
const {TypedDataUtils } = require('@metamask/eth-sig-util')
const {BcUrDecoder, } = require('@keystonehq/bc-ur-registry')
const {EthSignRequest, DataType, CryptoKeypath, PathComponent} = require('@keystonehq/bc-ur-registry-eth')
const {Common} = require('@ethereumjs/common')
const uuid = require('uuid')
const encode = (sign) => {
const signable = JSON.parse(sign)
const fromAddress = ethUtil.stripHexPrefix(signable.from)
const partFrom = fromAddress.substr(0, 4) +
fromAddress.substr(18, 2) + fromAddress.slice(-4)
const version = signable.version
const tx = signable.payload
const derivePath = CryptoKeypath.fromPathString(signable.derivePath)
var rlpEncoded
let sanitizedTypedData
let EIP_1559 = true
let common
let dataType
let xfp = "12345678";
switch (signable.type) {
case 'sign_message':
case 'sign_personal_message':
rlpEncoded = rlp.encode(Buffer.from(signable.payload))
dataType = DataType.personalMessage
break
case 'sign_typed_data':
// version V3: a
// version V4: b
// version V5: c
// ...
sanitizedTypedData = TypedDataUtils.sanitizeData(signable.payload)
rlpEncoded = TypedDataUtils.encodeData(sanitizedTypedData.primaryType, sanitizedTypedData.message, sanitizedTypedData.types, version)
dataType = DataType.typedData
break
case 'sign_transaction':
EIP_1559 = !tx.gasPrice
common = new Common({chain: tx.chainId})
if (EIP_1559) {
rlpEncoded = FeeMarketEIP_1559Transaction.fromTxData(tx, {common}).serialize()
} else {
rlpEncoded = Transaction.fromTxData(tx, {common}).serialize()
}
dataType = DataType.transaction
break
default:
throw new Error('Unknown signable type')
}
EthSignRequest.constructETHRequest(rlpEncoded, dataType, derivePath, xfp, uuid.v4(), BigInt(tx.chainId))
}
const decode = (encoded) => {
encoded = encoded.replace(/^[^Ee]*/i, '').replace(/\s*$/, '')
if (encoded.split(/ETHS:\/M/i).length > 1) {
encoded = encoded.split(/ETHS:\/M/i)[1]
return {
type: 'sign_message',
from_part: '0x' + encoded.substr(0, 4) +
'..............' + encoded.substr(4, 2) +
'................' + encoded.substr(6, 4),
payload: '0x' + ethUtil.stripHexPrefix(encoded.slice(10))}
}
if (encoded.split(/ETHS:\/P/i).length > 1) {
encoded = encoded.split(/ETHS:\/P/i)[1]
return {
type: 'sign_personal_message',
from_part: '0x' + encoded.substr(0, 4) +
'..............' + encoded.substr(4, 2) +
'................' + encoded.substr(6, 4),
payload: '0x' + ethUtil.stripHexPrefix(encoded.slice(10))}
}
if (encoded.split(/ETHS:\/[A-L]/i).length > 1) {
var version = encoded.match(/ETHS:\/([A-L])/i)[1].charCodeAt(0) - 'A'.charCodeAt(0) + 3
encoded = encoded.split(/ETHS:\/[A-L]/i)[1]
return {
type: 'sign_typed_data',
version: 'V' + version,
from_part: '0x' + encoded.substr(0, 4) +
'..............' + encoded.substr(4, 2) +
'................' + encoded.substr(6, 4),
payload: ethUtil.stripHexPrefix(encoded.slice(10))}
}
if (encoded.split(/ETHS:\/T/i).length > 1) {
encoded = encoded.split(/ETHS:\/T/i)[1]
var errorChk = encoded
.substr(0, 4)
.replace(/99/g, 'n')
.replace(/90/g, 'a')
.replace(/91/g, 'b')
.replace(/92/g, 'c')
.replace(/93/g, 'd')
.replace(/94/g, 'e')
.replace(/95/g, 'f')
.replace(/n/g, '9')
errorChk = errorChk.substr(0, 2)
var encErrChk = errorChk
.replace(/9/g, '99')
.replace(/a/g, '90')
.replace(/b/g, '91')
.replace(/c/g, '92')
.replace(/d/g, '93')
.replace(/e/g, '94')
.replace(/f/g, '95')
encoded = encoded.slice(encErrChk.length)
if (errorChk !== ethUtil.bufferToHex(ethUtil.sha3(ethUtil.toBuffer(encoded))).slice(-2)) {
console.info('Encoded:' + encoded + ' Errorchk:' + errorChk + ' Sha3: ' + ethUtil.bufferToHex(ethUtil.sha3(ethUtil.toBuffer(encoded))))
throw new Error('Encoded data is corrupted.')
}
encoded = encoded
.replace(/99/g, 'n')
.replace(/90/g, 'a')
.replace(/91/g, 'b')
.replace(/92/g, 'c')
.replace(/93/g, 'd')
.replace(/94/g, 'e')
.replace(/95/g, 'f')
.replace(/96/g, '|')
.replace(/970/g, '0000')
.replace(/971/g, '00000')
.replace(/972/g, '000000')
.replace(/973/g, '000000000000')
.replace(/974/g, '000000000000000000000000')
.replace(/975/g, '000000000000000000000000000000000000000000000000')
.replace(/n/g, '9')
var typeChainVersion = ethUtil.bufferToInt(ethUtil.toBuffer('0x' + encoded.substr(0, 2)))
version = typeChainVersion % 4
if (version !== 0) {
throw new Error('Unknown version:' + typeChainVersion % 4)
}
var type = Math.floor(typeChainVersion / 4) % 4
if (type !== 0) {
var typeStr
switch (type) {
case 1:
typeStr = 'sign_message'
break
case 2:
typeStr = 'sign_personal_message'
break
case 3:
typeStr = 'sign_typed_data'
}
throw new Error('Signing type should be sign_transaction, instead it is ' + typeStr)
}
var chainIdCode = Math.floor(typeChainVersion / 16)
var chainId
switch (chainIdCode) {
case 0:
chainId = 1
break
case 1:
chainId = 61
break
case 3:
chainId = 2 // Morden testnet
break
case 4:
chainId = 3
break
case 5:
chainId = 4
break
case 6:
chainId = 42
break
case 7:
chainId = 77
break
case 9:
chainId = 99
break
case 10:
chainId = 7762959
break
case 8:
chainId = null
break
default:
throw new Error('Unknown chainCode(' + chainIdCode + ') proveded.')
}
var tx = {}
if (chainId) tx.chainId = chainId
tx.from_part = '0x' + encoded.substr(2, 4) +
'..............' + encoded.substr(6, 2) +
'................' + encoded.substr(8, 4)
tx.to = '0x' + encoded.substr(12, 40)
encoded = encoded.slice(52)
if (!chainId) {
var delimiter = encoded.search('\\|')
chainId = encoded.substr(0, delimiter)
encoded = encoded.slice(delimiter + 1)
}
try {
tx.chainId = parseInt(chainId)
} catch (e) {
throw new Error('chainId should be a valid integer.')
}
delimiter = encoded.search('\\|')
var nonce = encoded.substr(0, delimiter)
encoded = encoded.slice(delimiter + 1)
tx.nonce = '0x' + nonce
delimiter = encoded.search('\\|')
var gasPrice = encoded.substr(0, delimiter)
encoded = encoded.slice(delimiter + 1)
tx.gasPrice = '0x' + gasPrice
delimiter = encoded.search('\\|')
var gasLimit = encoded.substr(0, delimiter)
encoded = encoded.slice(delimiter + 1)
tx.gasLimit = '0x' + gasLimit
delimiter = encoded.search('\\|')
var value = encoded.substr(0, delimiter)
encoded = encoded.slice(delimiter + 1)
tx.value = '0x' + value
tx.data = '0x' + encoded
return {
payload: tx,
type: 'sign_transaction',
from_part: tx.from_part,
}
}
throw new Error('Unknown signature type.')
}
module.exports = {
encode,
decode,
}