/
index.js
557 lines (485 loc) · 15.5 KB
/
index.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
const utils = require('ethereumjs-util')
const BN = require('bn.js')
var ABI = function () {
}
// 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
}
ABI.eventID = function (name, types) {
// FIXME: use node.js util.format?
var sig = name + '(' + types.map(elementaryName).join(',') + ')'
return utils.sha3(new Buffer(sig))
}
ABI.methodID = function (name, types) {
return ABI.eventID(name, types).slice(0, 4)
}
// 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 (utils.isHexPrefixed(arg)) {
return new BN(utils.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')
}
}
// someMethod(bytes,uint)
// someMethod(bytes,uint):(boolean)
function parseSignature (sig) {
var tmp = /^(\w+)\((.+)\)$/.exec(sig)
if (tmp.length !== 3) {
throw new Error('Invalid method signature')
}
var args = /^(.+)\):\((.+)$/.exec(tmp[2])
if (args !== null && args.length === 3) {
return {
method: tmp[1],
args: args[1].split(','),
retargs: args[2].split(',')
}
} else {
return {
method: tmp[1],
args: tmp[2].split(',')
}
}
}
// 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, utils.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 utils.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)
}
// Decodes a single item (can be dynamic array)
// @returns: array
// FIXME: this method will need a lot of attention at checking limits and validation
function decodeSingle (parsedType, data, offset) {
if (typeof parsedType === 'string') {
parsedType = parseType(parsedType)
}
var size, num, ret, i
if (parsedType.name === 'address') {
return decodeSingle(parsedType.rawType, data, offset).toArrayLike(Buffer, 'be', 20).toString('hex')
} else if (parsedType.name === 'bool') {
return decodeSingle(parsedType.rawType, data, offset).toString() === new BN(1).toString()
} else if (parsedType.name === 'string') {
var bytes = decodeSingle(parsedType.rawType, data, offset)
return new Buffer(bytes, 'utf8').toString()
} else if (parsedType.isArray) {
// this part handles fixed-length arrays ([2]) and variable length ([]) arrays
// NOTE: we catch here all calls to arrays, that simplifies the rest
ret = []
size = parsedType.size
if (parsedType.size === 'dynamic') {
offset = decodeSingle('uint256', data, offset).toNumber()
size = decodeSingle('uint256', data, offset).toNumber()
offset = offset + 32
}
for (i = 0; i < size; i++) {
var decoded = decodeSingle(parsedType.subArray, data, offset)
ret.push(decoded)
offset += parsedType.subArray.memoryUsage
}
return ret
} else if (parsedType.name === 'bytes') {
offset = decodeSingle('uint256', data, offset).toNumber()
size = decodeSingle('uint256', data, offset).toNumber()
return data.slice(offset + 32, offset + 32 + size)
} else if (parsedType.name.startsWith('bytes')) {
return data.slice(offset, offset + parsedType.size)
} else if (parsedType.name.startsWith('uint')) {
num = new BN(data.slice(offset, offset + 32), 16, 'be')
if (num.bitLength() > parsedType.size) {
throw new Error('Decoded int exceeds width: ' + parsedType.size + ' vs ' + num.bitLength())
}
return num
} else if (parsedType.name.startsWith('int')) {
num = new BN(data.slice(offset, offset + 32), 16, 'be').fromTwos(256)
if (num.bitLength() > parsedType.size) {
throw new Error('Decoded uint exceeds width: ' + parsedType.size + ' vs ' + num.bitLength())
}
return num
} else if (parsedType.name.startsWith('ufixed')) {
size = new BN(2).pow(new BN(parsedType.size[1]))
num = decodeSingle('uint256', data, offset)
if (!num.mod(size).isZero()) {
throw new Error('Decimals not supported yet')
}
return num.div(size)
} else if (parsedType.name.startsWith('fixed')) {
size = new BN(2).pow(new BN(parsedType.size[1]))
num = decodeSingle('int256', data, offset)
if (!num.mod(size).isZero()) {
throw new Error('Decimals not supported yet')
}
return num.div(size)
}
throw new Error('Unsupported or invalid type: ' + parsedType.name)
}
// Parse the given type
// @returns: {} containing the type itself, memory usage and (including size and subArray if applicable)
function parseType (type) {
var size
var ret
if (isArray(type)) {
size = parseTypeArray(type)
var subArray = type.slice(0, type.lastIndexOf('['))
subArray = parseType(subArray)
ret = {
isArray: true,
name: type,
size: size,
memoryUsage: size === 'dynamic' ? 32 : subArray.memoryUsage * size,
subArray: subArray
}
return ret
} else {
var rawType
switch (type) {
case 'address':
rawType = 'uint160'
break
case 'bool':
rawType = 'uint8'
break
case 'string':
rawType = 'bytes'
break
}
ret = {
rawType: rawType,
name: type,
memoryUsage: 32
}
if (type.startsWith('bytes') && type !== 'bytes' || type.startsWith('uint') || type.startsWith('int')) {
ret.size = parseTypeN(type)
} else if (type.startsWith('ufixed') || type.startsWith('fixed')) {
ret.size = parseTypeNxM(type)
}
if (type.startsWith('bytes') && type !== 'bytes' && (ret.size < 1 || ret.size > 32)) {
throw new Error('Invalid bytes<N> width: ' + ret.size)
}
if ((type.startsWith('uint') || type.startsWith('int')) && (ret.size % 8 || ret.size < 8 || ret.size > 256)) {
throw new Error('Invalid int/uint<N> width: ' + ret.size)
}
return ret
}
}
// 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
ABI.rawEncode = function (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))
}
ABI.rawDecode = function (types, data) {
var ret = []
data = new Buffer(data)
var offset = 0
for (var i in types) {
var type = elementaryName(types[i])
var parsed = parseType(type, data, offset)
var decoded = decodeSingle(parsed, data, offset)
offset += parsed.memoryUsage
ret.push(decoded)
}
return ret
}
ABI.simpleEncode = function (method) {
var args = Array.prototype.slice.call(arguments).slice(1)
var sig = parseSignature(method)
// FIXME: validate/convert arguments
if (args.length !== sig.args.length) {
throw new Error('Argument count mismatch')
}
return Buffer.concat([ ABI.methodID(sig.method, sig.args), ABI.rawEncode(sig.args, args) ])
}
ABI.simpleDecode = function (method, data) {
var sig = parseSignature(method)
// FIXME: validate/convert arguments
if (!sig.retargs) {
throw new Error('No return values in method')
}
return ABI.rawDecode(sig.retargs, data)
}
function stringify (type, value) {
if (type.startsWith('address') || type.startsWith('bytes')) {
return '0x' + value.toString('hex')
} else {
return value.toString()
}
}
ABI.stringify = function (types, values) {
var ret = []
for (var i in types) {
var type = types[i]
var value = values[i]
// if it is an array type, concat the items
if (/^[^\[]+\[.*\]$/.test(type)) {
value = value.map(function (item) {
return stringify(type, item)
}).join(', ')
} else {
value = stringify(type, value)
}
ret.push(value)
}
return ret
}
ABI.solidityPack = function (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(utils.setLengthLeft(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(utils.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)
}
ABI.soliditySHA3 = function (types, values) {
return utils.sha3(ABI.solidityPack(types, values))
}
ABI.soliditySHA256 = function (types, values) {
return utils.sha256(ABI.solidityPack(types, values))
}
ABI.solidityRIPEMD160 = function (types, values) {
return utils.ripemd160(ABI.solidityPack(types, values), true)
}
// Serpent's users are familiar with this encoding
// - s: string
// - b: bytes
// - b<N>: bytes<N>
// - i: int256
// - a: int256[]
function isNumeric (c) {
// FIXME: is this correct? Seems to work
return (c >= '0') && (c <= '9')
}
// For a "documentation" refer to https://github.com/ethereum/serpent/blob/develop/preprocess.cpp
ABI.fromSerpent = function (sig) {
var ret = []
for (var i = 0; i < sig.length; i++) {
var type = sig[i]
if (type === 's') {
ret.push('bytes')
} else if (type === 'b') {
var tmp = 'bytes'
var j = i + 1
while ((j < sig.length) && isNumeric(sig[j])) {
tmp += sig[j] - '0'
j++
}
i = j - 1
ret.push(tmp)
} else if (type === 'i') {
ret.push('int256')
} else if (type === 'a') {
ret.push('int256[]')
} else {
throw new Error('Unsupported or invalid type: ' + type)
}
}
return ret
}
ABI.toSerpent = function (types) {
var ret = []
for (var i = 0; i < types.length; i++) {
var type = types[i]
if (type === 'bytes') {
ret.push('s')
} else if (type.startsWith('bytes')) {
ret.push('b' + parseTypeN(type))
} else if (type === 'int256') {
ret.push('i')
} else if (type === 'int256[]') {
ret.push('a')
} else {
throw new Error('Unsupported or invalid type: ' + type)
}
}
return ret.join('')
}
module.exports = ABI