2018-06-13 15:39:39 -04:00
|
|
|
'use strict';
|
|
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
|
var bignumber_1 = require("./bignumber");
|
2018-06-17 16:47:28 -04:00
|
|
|
var bytes_1 = require("./bytes");
|
2018-06-13 15:39:39 -04:00
|
|
|
var utf8_1 = require("./utf8");
|
|
|
|
var keccak256_1 = require("./keccak256");
|
|
|
|
var sha2_1 = require("./sha2");
|
|
|
|
var regexBytes = new RegExp("^bytes([0-9]+)$");
|
|
|
|
var regexNumber = new RegExp("^(u?int)([0-9]*)$");
|
|
|
|
var regexArray = new RegExp("^(.*)\\[([0-9]*)\\]$");
|
|
|
|
var Zeros = '0000000000000000000000000000000000000000000000000000000000000000';
|
|
|
|
function _pack(type, value, isArray) {
|
|
|
|
switch (type) {
|
|
|
|
case 'address':
|
|
|
|
if (isArray) {
|
2018-06-17 16:47:28 -04:00
|
|
|
return bytes_1.padZeros(value, 32);
|
2018-06-13 15:39:39 -04:00
|
|
|
}
|
2018-06-17 16:47:28 -04:00
|
|
|
return bytes_1.arrayify(value);
|
2018-06-13 15:39:39 -04:00
|
|
|
case 'string':
|
|
|
|
return utf8_1.toUtf8Bytes(value);
|
|
|
|
case 'bytes':
|
2018-06-17 16:47:28 -04:00
|
|
|
return bytes_1.arrayify(value);
|
2018-06-13 15:39:39 -04:00
|
|
|
case 'bool':
|
|
|
|
value = (value ? '0x01' : '0x00');
|
|
|
|
if (isArray) {
|
2018-06-17 16:47:28 -04:00
|
|
|
return bytes_1.padZeros(value, 32);
|
2018-06-13 15:39:39 -04:00
|
|
|
}
|
2018-06-17 16:47:28 -04:00
|
|
|
return bytes_1.arrayify(value);
|
2018-06-13 15:39:39 -04:00
|
|
|
}
|
|
|
|
var match = type.match(regexNumber);
|
|
|
|
if (match) {
|
|
|
|
//var signed = (match[1] === 'int')
|
|
|
|
var size = parseInt(match[2] || "256");
|
|
|
|
if ((size % 8 != 0) || size === 0 || size > 256) {
|
|
|
|
throw new Error('invalid number type - ' + type);
|
|
|
|
}
|
|
|
|
if (isArray) {
|
|
|
|
size = 256;
|
|
|
|
}
|
|
|
|
value = bignumber_1.bigNumberify(value).toTwos(size);
|
2018-06-17 16:47:28 -04:00
|
|
|
return bytes_1.padZeros(value, size / 8);
|
2018-06-13 15:39:39 -04:00
|
|
|
}
|
|
|
|
match = type.match(regexBytes);
|
|
|
|
if (match) {
|
|
|
|
var size = parseInt(match[1]);
|
|
|
|
if (String(size) != match[1] || size === 0 || size > 32) {
|
|
|
|
throw new Error('invalid number type - ' + type);
|
|
|
|
}
|
2018-06-17 16:47:28 -04:00
|
|
|
if (bytes_1.arrayify(value).byteLength !== size) {
|
2018-06-13 15:39:39 -04:00
|
|
|
throw new Error('invalid value for ' + type);
|
|
|
|
}
|
|
|
|
if (isArray) {
|
2018-06-17 16:47:28 -04:00
|
|
|
return bytes_1.arrayify((value + Zeros).substring(0, 66));
|
2018-06-13 15:39:39 -04:00
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
match = type.match(regexArray);
|
2018-06-22 20:30:50 -04:00
|
|
|
if (match && Array.isArray(value)) {
|
2018-06-13 15:39:39 -04:00
|
|
|
var baseType = match[1];
|
2018-06-22 20:30:50 -04:00
|
|
|
var count = parseInt(match[2] || String(value.length));
|
2018-06-13 15:39:39 -04:00
|
|
|
if (count != value.length) {
|
|
|
|
throw new Error('invalid value for ' + type);
|
|
|
|
}
|
|
|
|
var result = [];
|
|
|
|
value.forEach(function (value) {
|
2018-06-22 20:30:50 -04:00
|
|
|
result.push(_pack(baseType, value, true));
|
2018-06-13 15:39:39 -04:00
|
|
|
});
|
2018-06-17 16:47:28 -04:00
|
|
|
return bytes_1.concat(result);
|
2018-06-13 15:39:39 -04:00
|
|
|
}
|
|
|
|
throw new Error('unknown type - ' + type);
|
|
|
|
}
|
|
|
|
// @TODO: Array Enum
|
|
|
|
function pack(types, values) {
|
|
|
|
if (types.length != values.length) {
|
|
|
|
throw new Error('type/value count mismatch');
|
|
|
|
}
|
|
|
|
var tight = [];
|
|
|
|
types.forEach(function (type, index) {
|
|
|
|
tight.push(_pack(type, values[index]));
|
|
|
|
});
|
2018-06-17 16:47:28 -04:00
|
|
|
return bytes_1.hexlify(bytes_1.concat(tight));
|
2018-06-13 15:39:39 -04:00
|
|
|
}
|
|
|
|
exports.pack = pack;
|
|
|
|
function keccak256(types, values) {
|
|
|
|
return keccak256_1.keccak256(pack(types, values));
|
|
|
|
}
|
|
|
|
exports.keccak256 = keccak256;
|
|
|
|
function sha256(types, values) {
|
|
|
|
return sha2_1.sha256(pack(types, values));
|
|
|
|
}
|
|
|
|
exports.sha256 = sha256;
|