forked from petejkim/eth-eip712-util
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathabi.js
273 lines (232 loc) · 7.63 KB
/
abi.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
// Extracted from https://github.com/ethereumjs/ethereumjs-abi and stripped out irrelevant code
// Original code licensed under the MIT License - Copyright (c) 2015 Alex Beregszaszi
const util = require('./util')
const BN = require('bn.js')
const Buffer = require('buffer/').Buffer
// Convert from short to canonical names
// FIXME: optimise or make this nicer?
function elementaryName (name) {
if (name.startsWith('int[')) {
return 'int256' + name.slice(3)
} else if (name === 'int') {
return 'int256'
} else if (name.startsWith('uint[')) {
return 'uint256' + name.slice(4)
} else if (name === 'uint') {
return 'uint256'
} else if (name.startsWith('fixed[')) {
return 'fixed128x128' + name.slice(5)
} else if (name === 'fixed') {
return 'fixed128x128'
} else if (name.startsWith('ufixed[')) {
return 'ufixed128x128' + name.slice(6)
} else if (name === 'ufixed') {
return 'ufixed128x128'
}
return name
}
// Parse N from type<N>
function parseTypeN (type) {
return parseInt(/^\D+(\d+)$/.exec(type)[1], 10)
}
// Parse N,M from type<N>x<M>
function parseTypeNxM (type) {
var tmp = /^\D+(\d+)x(\d+)$/.exec(type)
return [ parseInt(tmp[1], 10), parseInt(tmp[2], 10) ]
}
// Parse N in type[<N>] where "type" can itself be an array type.
function parseTypeArray (type) {
var tmp = type.match(/(.*)\[(.*?)\]$/)
if (tmp) {
return tmp[2] === '' ? 'dynamic' : parseInt(tmp[2], 10)
}
return null
}
function parseNumber (arg) {
var type = typeof arg
if (type === 'string') {
if (util.isHexString(arg)) {
return new BN(util.stripHexPrefix(arg), 16)
} else {
return new BN(arg, 10)
}
} else if (type === 'number') {
return new BN(arg)
} else if (arg.toArray) {
// assume this is a BN for the moment, replace with BN.isBN soon
return arg
} else {
throw new Error('Argument is not a number')
}
}
// Encodes a single item (can be dynamic array)
// @returns: Buffer
function encodeSingle (type, arg) {
var size, num, ret, i
if (type === 'address') {
return encodeSingle('uint160', parseNumber(arg))
} else if (type === 'bool') {
return encodeSingle('uint8', arg ? 1 : 0)
} else if (type === 'string') {
return encodeSingle('bytes', new Buffer(arg, 'utf8'))
} else if (isArray(type)) {
// this part handles fixed-length ([2]) and variable length ([]) arrays
// NOTE: we catch here all calls to arrays, that simplifies the rest
if (typeof arg.length === 'undefined') {
throw new Error('Not an array?')
}
size = parseTypeArray(type)
if (size !== 'dynamic' && size !== 0 && arg.length > size) {
throw new Error('Elements exceed array size: ' + size)
}
ret = []
type = type.slice(0, type.lastIndexOf('['))
if (typeof arg === 'string') {
arg = JSON.parse(arg)
}
for (i in arg) {
ret.push(encodeSingle(type, arg[i]))
}
if (size === 'dynamic') {
var length = encodeSingle('uint256', arg.length)
ret.unshift(length)
}
return Buffer.concat(ret)
} else if (type === 'bytes') {
arg = new Buffer(arg)
ret = Buffer.concat([ encodeSingle('uint256', arg.length), arg ])
if ((arg.length % 32) !== 0) {
ret = Buffer.concat([ ret, util.zeros(32 - (arg.length % 32)) ])
}
return ret
} else if (type.startsWith('bytes')) {
size = parseTypeN(type)
if (size < 1 || size > 32) {
throw new Error('Invalid bytes<N> width: ' + size)
}
return util.setLengthRight(arg, 32)
} else if (type.startsWith('uint')) {
size = parseTypeN(type)
if ((size % 8) || (size < 8) || (size > 256)) {
throw new Error('Invalid uint<N> width: ' + size)
}
num = parseNumber(arg)
if (num.bitLength() > size) {
throw new Error('Supplied uint exceeds width: ' + size + ' vs ' + num.bitLength())
}
if (num < 0) {
throw new Error('Supplied uint is negative')
}
return num.toArrayLike(Buffer, 'be', 32)
} else if (type.startsWith('int')) {
size = parseTypeN(type)
if ((size % 8) || (size < 8) || (size > 256)) {
throw new Error('Invalid int<N> width: ' + size)
}
num = parseNumber(arg)
if (num.bitLength() > size) {
throw new Error('Supplied int exceeds width: ' + size + ' vs ' + num.bitLength())
}
return num.toTwos(256).toArrayLike(Buffer, 'be', 32)
} else if (type.startsWith('ufixed')) {
size = parseTypeNxM(type)
num = parseNumber(arg)
if (num < 0) {
throw new Error('Supplied ufixed is negative')
}
return encodeSingle('uint256', num.mul(new BN(2).pow(new BN(size[1]))))
} else if (type.startsWith('fixed')) {
size = parseTypeNxM(type)
return encodeSingle('int256', parseNumber(arg).mul(new BN(2).pow(new BN(size[1]))))
}
throw new Error('Unsupported or invalid type: ' + type)
}
// Is a type dynamic?
function isDynamic (type) {
// FIXME: handle all types? I don't think anything is missing now
return (type === 'string') || (type === 'bytes') || (parseTypeArray(type) === 'dynamic')
}
// Is a type an array?
function isArray (type) {
return type.lastIndexOf(']') === type.length - 1
}
// Encode a method/event with arguments
// @types an array of string type names
// @args an array of the appropriate values
function rawEncode (types, values) {
var output = []
var data = []
var headLength = 32 * types.length
for (var i in types) {
var type = elementaryName(types[i])
var value = values[i]
var cur = encodeSingle(type, value)
// Use the head/tail method for storing dynamic data
if (isDynamic(type)) {
output.push(encodeSingle('uint256', headLength))
data.push(cur)
headLength += cur.length
} else {
output.push(cur)
}
}
return Buffer.concat(output.concat(data))
}
function solidityPack (types, values) {
if (types.length !== values.length) {
throw new Error('Number of types are not matching the values')
}
var size, num
var ret = []
for (var i = 0; i < types.length; i++) {
var type = elementaryName(types[i])
var value = values[i]
if (type === 'bytes') {
ret.push(value)
} else if (type === 'string') {
ret.push(new Buffer(value, 'utf8'))
} else if (type === 'bool') {
ret.push(new Buffer(value ? '01' : '00', 'hex'))
} else if (type === 'address') {
ret.push(util.setLength(value, 20))
} else if (type.startsWith('bytes')) {
size = parseTypeN(type)
if (size < 1 || size > 32) {
throw new Error('Invalid bytes<N> width: ' + size)
}
ret.push(util.setLengthRight(value, size))
} else if (type.startsWith('uint')) {
size = parseTypeN(type)
if ((size % 8) || (size < 8) || (size > 256)) {
throw new Error('Invalid uint<N> width: ' + size)
}
num = parseNumber(value)
if (num.bitLength() > size) {
throw new Error('Supplied uint exceeds width: ' + size + ' vs ' + num.bitLength())
}
ret.push(num.toArrayLike(Buffer, 'be', size / 8))
} else if (type.startsWith('int')) {
size = parseTypeN(type)
if ((size % 8) || (size < 8) || (size > 256)) {
throw new Error('Invalid int<N> width: ' + size)
}
num = parseNumber(value)
if (num.bitLength() > size) {
throw new Error('Supplied int exceeds width: ' + size + ' vs ' + num.bitLength())
}
ret.push(num.toTwos(size).toArrayLike(Buffer, 'be', size / 8))
} else {
// FIXME: support all other types
throw new Error('Unsupported or invalid type: ' + type)
}
}
return Buffer.concat(ret)
}
function soliditySHA3 (types, values) {
return util.keccak(solidityPack(types, values))
}
module.exports = {
rawEncode,
solidityPack,
soliditySHA3
}