rename libraries 'basend-*' to 'wo-base-*', 'corend-*' to 'wo-core-*', 'usend-*' to 'wo-user-*'

This commit is contained in:
陆柯 2022-08-17 09:00:15 +08:00
parent bb8b1da5f5
commit 9e7850e49b

View File

@ -1,97 +1,111 @@
'use strict'; 'use strict'
var bigNumberify = require('./bignumber').bigNumberify; var bigNumberify = require('./bignumber').bigNumberify
var convert = require('./convert'); var convert = require('./convert')
var getAddress = require('./address').getAddress; var getAddress = require('./address').getAddress
var utf8 = require('./utf8'); var utf8 = require('./utf8')
var hashKeccak256 = require('./keccak256'); var hashKeccak256 = require('./keccak256')
var hashSha256 = require('./sha2').sha256; var hashSha256 = require('./sha2').sha256
var regexBytes = new RegExp("^bytes([0-9]+)$"); var regexBytes = new RegExp('^bytes([0-9]+)$')
var regexNumber = new RegExp("^(u?int)([0-9]*)$"); var regexNumber = new RegExp('^(u?int)([0-9]*)$')
var regexArray = new RegExp("^(.*)\\[([0-9]*)\\]$"); var regexArray = new RegExp('^(.*)\\[([0-9]*)\\]$')
var Zeros = '0000000000000000000000000000000000000000000000000000000000000000'; var Zeros = '0000000000000000000000000000000000000000000000000000000000000000'
function _pack(type, value, isArray) { function _pack (type, value, isArray) {
switch(type) { switch (type) {
case 'address': case 'address':
if (isArray) { return convert.padZeros(value, 32); } if (isArray) {
return convert.arrayify(value); return convert.padZeros(value, 32)
case 'string': }
return utf8.toUtf8Bytes(value); return convert.arrayify(value)
case 'bytes': case 'string':
return convert.arrayify(value); return utf8.toUtf8Bytes(value)
case 'bool': case 'bytes':
value = (value ? '0x01': '0x00'); return convert.arrayify(value)
if (isArray) { return convert.padZeros(value, 32); } case 'bool':
return convert.arrayify(value); 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 (isArray) {
if (match) { size = 256
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);
} }
match = type.match(regexBytes); value = bigNumberify(value).toTwos(size)
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); return convert.padZeros(value, size / 8)
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);
}
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) { function pack (types, values) {
if (types.length != values.length) { throw new Error('type/value count mismatch'); } if (types.length != values.length) {
var tight = []; throw new Error('type/value count mismatch')
types.forEach(function(type, index) { }
tight.push(_pack(type, values[index])); var tight = []
}); types.forEach(function (type, index) {
return convert.hexlify(convert.concat(tight)); tight.push(_pack(type, values[index]))
})
return convert.hexlify(convert.concat(tight))
} }
function keccak256(types, values) { function keccak256 (types, values) {
return hashKeccak256(pack(types, values)); return hashKeccak256(pack(types, values))
} }
function sha256(types, values) { function sha256 (types, values) {
return hashSha256(pack(types, values)); return hashSha256(pack(types, values))
} }
module.exports = { module.exports = {
pack: pack, pack: pack,
keccak256: keccak256, keccak256: keccak256,
sha256: sha256, sha256: sha256
} }