web3-eth-abi
Advanced tools
Comparing version 1.2.8-rc.0 to 1.2.8-rc.1
{ | ||
"name": "web3-eth-abi", | ||
"version": "1.2.8-rc.0", | ||
"version": "1.2.8-rc.1", | ||
"description": "Web3 module encode and decode EVM in/output.", | ||
@@ -18,3 +18,3 @@ "repository": "https://github.com/ethereum/web3.js/tree/1.x/packages/web3-eth-abi", | ||
"underscore": "1.9.1", | ||
"web3-utils": "1.2.8-rc.0" | ||
"web3-utils": "1.2.8-rc.1" | ||
}, | ||
@@ -25,3 +25,3 @@ "devDependencies": { | ||
}, | ||
"gitHead": "035d11acced0bdd43e500d4defbfdcd1b70768d7" | ||
"gitHead": "62890027ca41f9e272ba3ed83c4d83fd2a7aeced" | ||
} |
126
src/index.js
@@ -102,70 +102,84 @@ /* | ||
ABICoder.prototype.encodeParameters = function (types, params) { | ||
const bytesToEvenLength = (param) => { | ||
if (Buffer.isBuffer(param)) { | ||
param = utils.toHex(param) | ||
const paramTypeBytes = new RegExp(/^bytes([0-9]*)$/); | ||
const paramTypeBytesArray = new RegExp(/^bytes([0-9]*)\[\]$/); | ||
const paramTypeNumber = new RegExp(/^(u?int)([0-9]*)$/); | ||
const paramTypeNumberArray = new RegExp(/^(u?int)([0-9]*)\[\]$/); | ||
params = params.map(function (param, index) { | ||
let type = types[index] | ||
if (typeof type === 'object' && type.type) { | ||
// We may get a named type of shape {name, type} | ||
type = type.type | ||
} | ||
// bitwise AND operator returns true if odd | ||
if (param.length & 1) { | ||
return '0x0' + param.substring(2) | ||
// Format BN to string | ||
if (utils.isBN(param) || utils.isBigNumber(param)) { | ||
return param.toString(10); | ||
} | ||
return param | ||
} | ||
const bytes32ToFixedLength = (param) => { | ||
if (Buffer.isBuffer(param)) { | ||
param = utils.toHex(param) | ||
} | ||
if (param.length < 66) { // 66 includes `0x` | ||
return utils.rightPad(param, 64) | ||
} | ||
return param | ||
} | ||
return ethersAbiCoder.encode( | ||
this.mapTypes(types), | ||
params.map(function (param, index) { | ||
let type = types[index] | ||
if (typeof type === 'object' && type.type) { | ||
// We may get a named type of shape {name, type} | ||
type = type.type | ||
// Handle some formatting of params for backwards compatability with Ethers V4 | ||
const formatParam = (type, param) => { | ||
if (type.match(paramTypeBytesArray) || type.match(paramTypeNumberArray)) { | ||
return param.map(p => formatParam(type.replace('[]', ''), p)) | ||
} | ||
// Format BN to string | ||
if (utils.isBN(param) || utils.isBigNumber(param)) { | ||
return param.toString(10); | ||
// Format correct width for u?int[0-9]* | ||
let match = type.match(paramTypeNumber); | ||
if (match) { | ||
let size = parseInt(match[2] || "256"); | ||
if (size / 8 < param.length) { | ||
// pad to correct bit width | ||
param = utils.leftPad(param, size); | ||
} | ||
} | ||
// Convert odd-length bytes to even | ||
if (type === 'bytes') { | ||
param = bytesToEvenLength(param) | ||
} else if (type === 'bytes[]') { | ||
param = param.map(p => bytesToEvenLength(p)) | ||
} | ||
// Format correct length for bytes[0-9]+ | ||
match = type.match(paramTypeBytes); | ||
if (match) { | ||
if (Buffer.isBuffer(param)) { | ||
param = utils.toHex(param); | ||
} | ||
// Convert bytes32 to fixed length | ||
if (type === 'bytes32') { | ||
param = bytes32ToFixedLength(param) | ||
} else if (type === 'bytes32[]') { | ||
param = param.map(p => bytes32ToFixedLength(p)) | ||
// format to correct length | ||
let size = parseInt(match[1]); | ||
if (size) { | ||
let maxSize = size * 2; | ||
if (param.substring(0, 2) === '0x') { | ||
maxSize += 2; | ||
} | ||
if (param.length < maxSize) { | ||
// pad to correct length | ||
param = utils.rightPad(param, size * 2) | ||
} | ||
} | ||
// format odd-length bytes to even-length | ||
if (param.length % 2 === 1) { | ||
param = '0x0' + param.substring(2) | ||
} | ||
} | ||
// Format tuples for above rules | ||
if (typeof type === 'string' && type.includes('tuple')) { | ||
const coder = ethersAbiCoder._getCoder(ParamType.from(type)); | ||
const modifyParams = (coder, param) => { | ||
coder.coders.forEach((c, i) => { | ||
if (c.name === 'bytes') { | ||
param[i] = bytesToEvenLength(param[i]) | ||
} else if (c.name === 'bytes32') { | ||
param[i] = bytes32ToFixedLength(param[i]) | ||
} else if (c.name === 'tuple') { | ||
modifyParams(c, param[i]) | ||
} | ||
}) | ||
} | ||
modifyParams(coder, param) | ||
return param | ||
} | ||
param = formatParam(type, param) | ||
// Format tuples for above rules | ||
if (typeof type === 'string' && type.includes('tuple')) { | ||
const coder = ethersAbiCoder._getCoder(ParamType.from(type)); | ||
const modifyParams = (coder, param) => { | ||
coder.coders.forEach((c, i) => { | ||
if (c.name === 'tuple') { | ||
modifyParams(c, param[i]) | ||
} else { | ||
param[i] = formatParam(c.name, param[i]) | ||
} | ||
}) | ||
} | ||
modifyParams(coder, param) | ||
} | ||
return param; | ||
}) | ||
); | ||
return param; | ||
}) | ||
return ethersAbiCoder.encode(this.mapTypes(types), params); | ||
}; | ||
@@ -172,0 +186,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
35667
1113
33
54
12
247
+ Addedweb3-utils@1.2.8-rc.1(transitive)
- Removedweb3-utils@1.2.8-rc.0(transitive)
Updatedweb3-utils@1.2.8-rc.1