@okxweb3/coin-base
Version:
A base package for @ok/coin-*
214 lines • 7.17 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.intToUnpaddedBuffer = exports.bigIntToUnpaddedBuffer = exports.bigIntToHex = exports.bufArrToArr = exports.arrToBufArr = exports.validateNoLeadingZeroes = exports.baToJSON = exports.toUtf8 = exports.short = exports.addHexPrefix = exports.toUnsigned = exports.fromSigned = exports.bufferToInt = exports.bigIntToBuffer = exports.bufferToBigInt = exports.bufferToHex = exports.toBuffer = exports.unpadHexString = exports.unpadArray = exports.unpadBuffer = exports.setLengthRight = exports.setLengthLeft = exports.zeros = exports.intToBuffer = exports.intToHex = void 0;
const helpers_1 = require("./helpers");
const internal_1 = require("./internal");
const intToHex = function (i) {
if (!Number.isSafeInteger(i) || i < 0) {
throw new Error(`Received an invalid integer type: ${i}`);
}
return `0x${i.toString(16)}`;
};
exports.intToHex = intToHex;
const intToBuffer = function (i) {
const hex = (0, exports.intToHex)(i);
return Buffer.from((0, internal_1.padToEven)(hex.slice(2)), 'hex');
};
exports.intToBuffer = intToBuffer;
const zeros = function (bytes) {
return Buffer.allocUnsafe(bytes).fill(0);
};
exports.zeros = zeros;
const setLength = function (msg, length, right) {
const buf = (0, exports.zeros)(length);
if (right) {
if (msg.length < length) {
msg.copy(buf);
return buf;
}
return msg.slice(0, length);
}
else {
if (msg.length < length) {
msg.copy(buf, length - msg.length);
return buf;
}
return msg.slice(-length);
}
};
const setLengthLeft = function (msg, length) {
(0, helpers_1.assertIsBuffer)(msg);
return setLength(msg, length, false);
};
exports.setLengthLeft = setLengthLeft;
const setLengthRight = function (msg, length) {
(0, helpers_1.assertIsBuffer)(msg);
return setLength(msg, length, true);
};
exports.setLengthRight = setLengthRight;
const stripZeros = function (a) {
let first = a[0];
while (a.length > 0 && first.toString() === '0') {
a = a.slice(1);
first = a[0];
}
return a;
};
const unpadBuffer = function (a) {
(0, helpers_1.assertIsBuffer)(a);
return stripZeros(a);
};
exports.unpadBuffer = unpadBuffer;
const unpadArray = function (a) {
(0, helpers_1.assertIsArray)(a);
return stripZeros(a);
};
exports.unpadArray = unpadArray;
const unpadHexString = function (a) {
(0, helpers_1.assertIsHexString)(a);
a = (0, internal_1.stripHexPrefix)(a);
return ('0x' + stripZeros(a));
};
exports.unpadHexString = unpadHexString;
const toBuffer = function (v) {
if (v === null || v === undefined) {
return Buffer.allocUnsafe(0);
}
if (Buffer.isBuffer(v)) {
return Buffer.from(v);
}
if (Array.isArray(v) || v instanceof Uint8Array) {
return Buffer.from(v);
}
if (typeof v === 'string') {
if (!(0, internal_1.isHexString)(v)) {
throw new Error(`Cannot convert string to buffer. toBuffer only supports 0x-prefixed hex strings and this string was given: ${v}`);
}
return Buffer.from((0, internal_1.padToEven)((0, internal_1.stripHexPrefix)(v)), 'hex');
}
if (typeof v === 'number') {
return (0, exports.intToBuffer)(v);
}
if (typeof v === 'bigint') {
if (v < BigInt(0)) {
throw new Error(`Cannot convert negative bigint to buffer. Given: ${v}`);
}
let n = v.toString(16);
if (n.length % 2)
n = '0' + n;
return Buffer.from(n, 'hex');
}
if (v.toArray) {
return Buffer.from(v.toArray());
}
if (v.toBuffer) {
return Buffer.from(v.toBuffer());
}
throw new Error('invalid type');
};
exports.toBuffer = toBuffer;
const bufferToHex = function (buf) {
buf = (0, exports.toBuffer)(buf);
return '0x' + buf.toString('hex');
};
exports.bufferToHex = bufferToHex;
function bufferToBigInt(buf) {
const hex = (0, exports.bufferToHex)(buf);
if (hex === '0x') {
return BigInt(0);
}
return BigInt(hex);
}
exports.bufferToBigInt = bufferToBigInt;
function bigIntToBuffer(num) {
return (0, exports.toBuffer)('0x' + num.toString(16));
}
exports.bigIntToBuffer = bigIntToBuffer;
const bufferToInt = function (buf) {
const res = Number(bufferToBigInt(buf));
if (!Number.isSafeInteger(res))
throw new Error('Number exceeds 53 bits');
return res;
};
exports.bufferToInt = bufferToInt;
const fromSigned = function (num) {
return BigInt.asIntN(256, bufferToBigInt(num));
};
exports.fromSigned = fromSigned;
const toUnsigned = function (num) {
return bigIntToBuffer(BigInt.asUintN(256, num));
};
exports.toUnsigned = toUnsigned;
const addHexPrefix = function (str) {
if (typeof str !== 'string') {
return str;
}
return (0, internal_1.isHexPrefixed)(str) ? str : '0x' + str;
};
exports.addHexPrefix = addHexPrefix;
function short(buffer, maxLength = 50) {
const bufferStr = Buffer.isBuffer(buffer) ? buffer.toString('hex') : buffer;
if (bufferStr.length <= maxLength) {
return bufferStr;
}
return bufferStr.slice(0, maxLength) + '…';
}
exports.short = short;
const toUtf8 = function (hex) {
const zerosRegexp = /(^(00)+)|((00)+$)/g;
hex = (0, internal_1.stripHexPrefix)(hex);
if (hex.length % 2 !== 0) {
throw new Error('Invalid non-even hex string input for toUtf8() provided');
}
const bufferVal = Buffer.from(hex.replace(zerosRegexp, ''), 'hex');
return bufferVal.toString('utf8');
};
exports.toUtf8 = toUtf8;
const baToJSON = function (ba) {
if (Buffer.isBuffer(ba)) {
return `0x${ba.toString('hex')}`;
}
else if (ba instanceof Array) {
const array = [];
for (let i = 0; i < ba.length; i++) {
array.push((0, exports.baToJSON)(ba[i]));
}
return array;
}
};
exports.baToJSON = baToJSON;
const validateNoLeadingZeroes = function (values) {
for (const [k, v] of Object.entries(values)) {
if (v !== undefined && v.length > 0 && v[0] === 0) {
throw new Error(`${k} cannot have leading zeroes, received: ${v.toString('hex')}`);
}
}
};
exports.validateNoLeadingZeroes = validateNoLeadingZeroes;
function arrToBufArr(arr) {
if (!Array.isArray(arr)) {
return Buffer.from(arr);
}
return arr.map((a) => arrToBufArr(a));
}
exports.arrToBufArr = arrToBufArr;
function bufArrToArr(arr) {
if (!Array.isArray(arr)) {
return Uint8Array.from(arr ?? []);
}
return arr.map((a) => bufArrToArr(a));
}
exports.bufArrToArr = bufArrToArr;
const bigIntToHex = (num) => {
return '0x' + num.toString(16);
};
exports.bigIntToHex = bigIntToHex;
function bigIntToUnpaddedBuffer(value) {
return (0, exports.unpadBuffer)(bigIntToBuffer(value));
}
exports.bigIntToUnpaddedBuffer = bigIntToUnpaddedBuffer;
function intToUnpaddedBuffer(value) {
return (0, exports.unpadBuffer)((0, exports.intToBuffer)(value));
}
exports.intToUnpaddedBuffer = intToUnpaddedBuffer;
//# sourceMappingURL=bytes.js.map