From 9e7850e49b187f977a96b25e1d695898ebff6971 Mon Sep 17 00:00:00 2001 From: "luk.lu" Date: Wed, 17 Aug 2022 09:00:15 +0800 Subject: [PATCH] rename libraries 'basend-*' to 'wo-base-*', 'corend-*' to 'wo-core-*', 'usend-*' to 'wo-user-*' --- utils/solidity.js | 164 +++++++++++++++++++++++++--------------------- 1 file changed, 89 insertions(+), 75 deletions(-) diff --git a/utils/solidity.js b/utils/solidity.js index ee07d15..6281f14 100644 --- a/utils/solidity.js +++ b/utils/solidity.js @@ -1,97 +1,111 @@ -'use strict'; +'use strict' -var bigNumberify = require('./bignumber').bigNumberify; -var convert = require('./convert'); -var getAddress = require('./address').getAddress; -var utf8 = require('./utf8'); +var bigNumberify = require('./bignumber').bigNumberify +var convert = require('./convert') +var getAddress = require('./address').getAddress +var utf8 = require('./utf8') -var hashKeccak256 = require('./keccak256'); -var hashSha256 = require('./sha2').sha256; +var hashKeccak256 = require('./keccak256') +var hashSha256 = require('./sha2').sha256 -var regexBytes = new RegExp("^bytes([0-9]+)$"); -var regexNumber = new RegExp("^(u?int)([0-9]*)$"); -var regexArray = new RegExp("^(.*)\\[([0-9]*)\\]$"); +var regexBytes = new RegExp('^bytes([0-9]+)$') +var regexNumber = new RegExp('^(u?int)([0-9]*)$') +var regexArray = new RegExp('^(.*)\\[([0-9]*)\\]$') -var Zeros = '0000000000000000000000000000000000000000000000000000000000000000'; +var Zeros = '0000000000000000000000000000000000000000000000000000000000000000' -function _pack(type, value, isArray) { - switch(type) { - case 'address': - if (isArray) { return convert.padZeros(value, 32); } - return convert.arrayify(value); - case 'string': - return utf8.toUtf8Bytes(value); - case 'bytes': - return convert.arrayify(value); - case 'bool': - value = (value ? '0x01': '0x00'); - if (isArray) { return convert.padZeros(value, 32); } - return convert.arrayify(value); +function _pack (type, value, isArray) { + switch (type) { + case 'address': + if (isArray) { + return convert.padZeros(value, 32) + } + return convert.arrayify(value) + case 'string': + return utf8.toUtf8Bytes(value) + case 'bytes': + return convert.arrayify(value) + case 'bool': + value = value ? '0x01' : '0x00' + if (isArray) { + return convert.padZeros(value, 32) + } + return convert.arrayify(value) + } + + 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) } - 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 = bigNumberify(value).toTwos(size); - - return convert.padZeros(value, size / 8); + if (isArray) { + size = 256 } - match = type.match(regexBytes); - if (match) { - var size = match[1]; - if (size != parseInt(size) || size === 0 || size > 32) { - throw new Error('invalid number type - ' + type); - } - size = parseInt(size); - if (convert.arrayify(value).byteLength !== size) { throw new Error('invalid value for ' + type); } - if (isArray) { return (value + Zeros).substring(0, 66); } - return value; - } + value = bigNumberify(value).toTwos(size) - match = type.match(regexArray); - if (match) { - var baseType = match[1]; - var count = parseInt(match[2] || value.length); - if (count != value.length) { throw new Error('invalid value for ' + type); } - var result = []; - value.forEach(function(value) { - value = _pack(baseType, value, true); - result.push(value); - }); - return convert.concat(result); - } + return convert.padZeros(value, size / 8) + } - throw new Error('unknown type - ' + type); + match = type.match(regexBytes) + if (match) { + var size = match[1] + if (size != parseInt(size) || size === 0 || size > 32) { + throw new Error('invalid number type - ' + type) + } + size = parseInt(size) + if (convert.arrayify(value).byteLength !== size) { + throw new Error('invalid value for ' + type) + } + if (isArray) { + return (value + Zeros).substring(0, 66) + } + return value + } + + match = type.match(regexArray) + if (match) { + var valueType = match[1] + var count = parseInt(match[2] || value.length) + if (count != value.length) { + throw new Error('invalid value for ' + type) + } + var result = [] + value.forEach(function (value) { + value = _pack(valueType, value, true) + result.push(value) + }) + return convert.concat(result) + } + + throw new Error('unknown type - ' + type) } -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])); - }); - return convert.hexlify(convert.concat(tight)); +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])) + }) + return convert.hexlify(convert.concat(tight)) } -function keccak256(types, values) { - return hashKeccak256(pack(types, values)); +function keccak256 (types, values) { + return hashKeccak256(pack(types, values)) } -function sha256(types, values) { - return hashSha256(pack(types, values)); +function sha256 (types, values) { + return hashSha256(pack(types, values)) } module.exports = { - pack: pack, + pack: pack, - keccak256: keccak256, - sha256: sha256, + keccak256: keccak256, + sha256: sha256 }