UNPKG

@findeth/abi

Version:

A tiny Solidity ABI encoder and decoder

85 lines (73 loc) 2.1 kB
const BUFFER_WIDTH = 32; const HEX_REGEX = /^[a-f0-9]+$/i; export const stripPrefix = value => { if (value.startsWith('0x')) { return value.substring(2); } return value; }; export const getTextEncoder = () => { if (typeof TextEncoder === 'undefined') { const Encoder = require('util').TextEncoder; return new Encoder(); } return new TextEncoder(); }; export const getTextDecoder = (encoding = 'utf8') => { if (typeof TextEncoder === 'undefined') { const Decoder = require('util').TextDecoder; return new Decoder(encoding); } return new TextDecoder(encoding); }; export const toUtf8 = data => { return getTextDecoder().decode(data); }; export const fromUtf8 = data => { return getTextEncoder().encode(data); }; export const toHex = data => { return Array.from(data).map(n => `0${n.toString(16)}`.slice(-2)).join(''); }; export const fromHex = data => { if (data.startsWith('0x')) { data = data.slice(2); } if (data.length % 2 !== 0) { throw new Error('Length must be even'); } if (!data.match(HEX_REGEX)) { throw new Error('Input must be hexadecimal'); } return new Uint8Array(data.match(/.{1,2}/g).map(byte => parseInt(byte, 16))); }; export const toBuffer = data => { if (typeof data === 'string') { return fromHex(data); } if (typeof data === 'number' || typeof data === 'bigint') { const string = data.toString(16); return fromHex(string.padStart(BUFFER_WIDTH * 2, '0')); } return new Uint8Array(data); }; export const concat = buffers => { return buffers.reduce((a, b) => { const buffer = new Uint8Array(a.length + b.length); buffer.set(a); buffer.set(b, a.length); return buffer; }, new Uint8Array(0)); }; export const addPadding = (buffer, length = BUFFER_WIDTH) => { const padding = new Uint8Array(Math.max(length - buffer.length, 0)).fill(0x00); return concat([buffer, padding]); }; export const toNumber = buffer => { const hex = toHex(buffer); if (hex.length === 0) { return BigInt(0); } return BigInt(`0x${hex}`); }; //# sourceMappingURL=buffer.js.map