From 48546dc8a4b4803dba88259a49160687c471ffdf Mon Sep 17 00:00:00 2001 From: Sebastian Pape Date: Mon, 20 Mar 2023 09:11:50 +0100 Subject: [PATCH] v10.6.1: fix solana package --- dist/esm/index.solana.js | 9138 +++++++++++++++++++++++++++++++ dist/umd/index.solana.js | 9152 ++++++++++++++++++++++++++++++++ package.evm.json | 2 +- package.json | 4 +- package.solana.json | 2 +- rollup.module.solana.config.js | 47 + 6 files changed, 18341 insertions(+), 4 deletions(-) create mode 100644 dist/esm/index.solana.js create mode 100644 dist/umd/index.solana.js create mode 100644 rollup.module.solana.config.js diff --git a/dist/esm/index.solana.js b/dist/esm/index.solana.js new file mode 100644 index 0000000..d61b10e --- /dev/null +++ b/dist/esm/index.solana.js @@ -0,0 +1,9138 @@ +import { Connection, Buffer, PublicKey, TransactionInstruction, Transaction, ACCOUNT_LAYOUT } from '@depay/solana-web3.js'; + +class StaticJsonRpcSequentialProvider extends Connection { + + constructor(url, network, endpoints) { + super(url); + this._network = network; + this._endpoint = url; + this._endpoints = endpoints; + } +} + +let getWindow = () => { + if (typeof global == 'object') return global + return window +}; + +const ENDPOINTS = { + solana: ['https://solana-mainnet.phantom.app/YBPpkkN4g91xDiAnTE9r0RcMkjg0sKUIWvAfoFVJ', 'https://mainnet-beta.solflare.network', 'https://solana-mainnet.rpc.extrnode.com'] +}; + +const getProviders = ()=> { + if(getWindow()._clientProviders == undefined) { + getWindow()._clientProviders = {}; + } + return getWindow()._clientProviders +}; + +const setProvider$1 = (blockchain, provider)=> { + getProviders()[blockchain] = provider; +}; + +const setProviderEndpoints$1 = async (blockchain, endpoints)=> { + + let endpoint; + let window = getWindow(); + + if( + window.fetch == undefined || + (typeof process != 'undefined' && process['env'] && process['env']['NODE_ENV'] == 'test') || + (typeof window.cy != 'undefined') + ) { + endpoint = endpoints[0]; + } else { + + let responseTimes = await Promise.all(endpoints.map((endpoint)=>{ + return new Promise(async (resolve)=>{ + let timeout = 900; + let before = new Date().getTime(); + setTimeout(()=>resolve(timeout), timeout); + const response = await fetch(endpoint, { + method: 'POST', + headers: { + 'Accept': 'application/json', + 'Content-Type': 'application/json' + }, + body: JSON.stringify({ method: 'getIdentity', id: 1, jsonrpc: '2.0' }) + }); + if(!response.ok) { return resolve(999) } + let after = new Date().getTime(); + resolve(after-before); + }) + })); + + const fastestResponse = Math.min(...responseTimes); + const fastestIndex = responseTimes.indexOf(fastestResponse); + endpoint = endpoints[fastestIndex]; + } + + setProvider$1( + blockchain, + new StaticJsonRpcSequentialProvider(endpoint, blockchain, endpoints) + ); +}; + +const getProvider$1 = async (blockchain)=> { + + let providers = getProviders(); + if(providers && providers[blockchain]){ return providers[blockchain] } + + let window = getWindow(); + if(window._getProviderPromise && window._getProviderPromise[blockchain]) { return await window._getProviderPromise[blockchain] } + + if(!window._getProviderPromise){ window._getProviderPromise = {}; } + window._getProviderPromise[blockchain] = new Promise(async(resolve)=> { + await setProviderEndpoints$1(blockchain, ENDPOINTS[blockchain]); + resolve(getWindow()._clientProviders[blockchain]); + }); + + return await window._getProviderPromise[blockchain] +}; + +var Solana = { + getProvider: getProvider$1, + setProviderEndpoints: setProviderEndpoints$1, + setProvider: setProvider$1, +}; + +let supported = ['solana']; +supported.evm = []; +supported.solana = ['solana']; + +const getProvider = async (blockchain)=>{ + + if(supported.evm.includes(blockchain)) ; else if(supported.solana.includes(blockchain)) ; else { + throw 'Unknown blockchain: ' + blockchain + } +}; + +const setProvider = (blockchain, provider)=>{ + + if(supported.evm.includes(blockchain)) ; else if(supported.solana.includes(blockchain)) ; else { + throw 'Unknown blockchain: ' + blockchain + } +}; + +const setProviderEndpoints = (blockchain, endpoints)=>{ + + if(supported.evm.includes(blockchain)) ; else if(supported.solana.includes(blockchain)) ; else { + throw 'Unknown blockchain: ' + blockchain + } +}; + +function _optionalChain$1(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; } +let simulate = async function ({ blockchain, from, to, keys, api, params }) { + if(!supported.solana.includes(blockchain)) { throw `${blockchain} not supported for simulation!` } + + const data = Buffer.alloc(api.span); + api.encode(params, data); + + keys = keys.map((key)=>{ + return({...key, + pubkey: new PublicKey(key.pubkey) + }) + }); + + const instruction = new TransactionInstruction({ + programId: new PublicKey(to), + keys, + data + }); + + let transaction = new Transaction({ feePayer: new PublicKey(from) }); + transaction.add(instruction); + + let result; + try{ + const provider = await getProvider('solana'); + result = await provider.simulateTransaction(transaction); + } catch (error) { + console.log(error); + } + + return({ + logs: _optionalChain$1([result, 'optionalAccess', _ => _.value, 'optionalAccess', _2 => _2.logs]) + }) +}; + +var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {}; + +var bn = {exports: {}}; + +(function (module) { + (function (module, exports) { + + // Utils + function assert (val, msg) { + if (!val) throw new Error(msg || 'Assertion failed'); + } + + // Could use `inherits` module, but don't want to move from single file + // architecture yet. + function inherits (ctor, superCtor) { + ctor.super_ = superCtor; + var TempCtor = function () {}; + TempCtor.prototype = superCtor.prototype; + ctor.prototype = new TempCtor(); + ctor.prototype.constructor = ctor; + } + + // BN + + function BN (number, base, endian) { + if (BN.isBN(number)) { + return number; + } + + this.negative = 0; + this.words = null; + this.length = 0; + + // Reduction context + this.red = null; + + if (number !== null) { + if (base === 'le' || base === 'be') { + endian = base; + base = 10; + } + + this._init(number || 0, base || 10, endian || 'be'); + } + } + if (typeof module === 'object') { + module.exports = BN; + } else { + exports.BN = BN; + } + + BN.BN = BN; + BN.wordSize = 26; + + var Buffer; + try { + if (typeof window !== 'undefined' && typeof window.Buffer !== 'undefined') { + Buffer = window.Buffer; + } else { + Buffer = require('buffer').Buffer; + } + } catch (e) { + } + + BN.isBN = function isBN (num) { + if (num instanceof BN) { + return true; + } + + return num !== null && typeof num === 'object' && + num.constructor.wordSize === BN.wordSize && Array.isArray(num.words); + }; + + BN.max = function max (left, right) { + if (left.cmp(right) > 0) return left; + return right; + }; + + BN.min = function min (left, right) { + if (left.cmp(right) < 0) return left; + return right; + }; + + BN.prototype._init = function init (number, base, endian) { + if (typeof number === 'number') { + return this._initNumber(number, base, endian); + } + + if (typeof number === 'object') { + return this._initArray(number, base, endian); + } + + if (base === 'hex') { + base = 16; + } + assert(base === (base | 0) && base >= 2 && base <= 36); + + number = number.toString().replace(/\s+/g, ''); + var start = 0; + if (number[0] === '-') { + start++; + this.negative = 1; + } + + if (start < number.length) { + if (base === 16) { + this._parseHex(number, start, endian); + } else { + this._parseBase(number, base, start); + if (endian === 'le') { + this._initArray(this.toArray(), base, endian); + } + } + } + }; + + BN.prototype._initNumber = function _initNumber (number, base, endian) { + if (number < 0) { + this.negative = 1; + number = -number; + } + if (number < 0x4000000) { + this.words = [number & 0x3ffffff]; + this.length = 1; + } else if (number < 0x10000000000000) { + this.words = [ + number & 0x3ffffff, + (number / 0x4000000) & 0x3ffffff + ]; + this.length = 2; + } else { + assert(number < 0x20000000000000); // 2 ^ 53 (unsafe) + this.words = [ + number & 0x3ffffff, + (number / 0x4000000) & 0x3ffffff, + 1 + ]; + this.length = 3; + } + + if (endian !== 'le') return; + + // Reverse the bytes + this._initArray(this.toArray(), base, endian); + }; + + BN.prototype._initArray = function _initArray (number, base, endian) { + // Perhaps a Uint8Array + assert(typeof number.length === 'number'); + if (number.length <= 0) { + this.words = [0]; + this.length = 1; + return this; + } + + this.length = Math.ceil(number.length / 3); + this.words = new Array(this.length); + for (var i = 0; i < this.length; i++) { + this.words[i] = 0; + } + + var j, w; + var off = 0; + if (endian === 'be') { + for (i = number.length - 1, j = 0; i >= 0; i -= 3) { + w = number[i] | (number[i - 1] << 8) | (number[i - 2] << 16); + this.words[j] |= (w << off) & 0x3ffffff; + this.words[j + 1] = (w >>> (26 - off)) & 0x3ffffff; + off += 24; + if (off >= 26) { + off -= 26; + j++; + } + } + } else if (endian === 'le') { + for (i = 0, j = 0; i < number.length; i += 3) { + w = number[i] | (number[i + 1] << 8) | (number[i + 2] << 16); + this.words[j] |= (w << off) & 0x3ffffff; + this.words[j + 1] = (w >>> (26 - off)) & 0x3ffffff; + off += 24; + if (off >= 26) { + off -= 26; + j++; + } + } + } + return this._strip(); + }; + + function parseHex4Bits (string, index) { + var c = string.charCodeAt(index); + // '0' - '9' + if (c >= 48 && c <= 57) { + return c - 48; + // 'A' - 'F' + } else if (c >= 65 && c <= 70) { + return c - 55; + // 'a' - 'f' + } else if (c >= 97 && c <= 102) { + return c - 87; + } else { + assert(false, 'Invalid character in ' + string); + } + } + + function parseHexByte (string, lowerBound, index) { + var r = parseHex4Bits(string, index); + if (index - 1 >= lowerBound) { + r |= parseHex4Bits(string, index - 1) << 4; + } + return r; + } + + BN.prototype._parseHex = function _parseHex (number, start, endian) { + // Create possibly bigger array to ensure that it fits the number + this.length = Math.ceil((number.length - start) / 6); + this.words = new Array(this.length); + for (var i = 0; i < this.length; i++) { + this.words[i] = 0; + } + + // 24-bits chunks + var off = 0; + var j = 0; + + var w; + if (endian === 'be') { + for (i = number.length - 1; i >= start; i -= 2) { + w = parseHexByte(number, start, i) << off; + this.words[j] |= w & 0x3ffffff; + if (off >= 18) { + off -= 18; + j += 1; + this.words[j] |= w >>> 26; + } else { + off += 8; + } + } + } else { + var parseLength = number.length - start; + for (i = parseLength % 2 === 0 ? start + 1 : start; i < number.length; i += 2) { + w = parseHexByte(number, start, i) << off; + this.words[j] |= w & 0x3ffffff; + if (off >= 18) { + off -= 18; + j += 1; + this.words[j] |= w >>> 26; + } else { + off += 8; + } + } + } + + this._strip(); + }; + + function parseBase (str, start, end, mul) { + var r = 0; + var b = 0; + var len = Math.min(str.length, end); + for (var i = start; i < len; i++) { + var c = str.charCodeAt(i) - 48; + + r *= mul; + + // 'a' + if (c >= 49) { + b = c - 49 + 0xa; + + // 'A' + } else if (c >= 17) { + b = c - 17 + 0xa; + + // '0' - '9' + } else { + b = c; + } + assert(c >= 0 && b < mul, 'Invalid character'); + r += b; + } + return r; + } + + BN.prototype._parseBase = function _parseBase (number, base, start) { + // Initialize as zero + this.words = [0]; + this.length = 1; + + // Find length of limb in base + for (var limbLen = 0, limbPow = 1; limbPow <= 0x3ffffff; limbPow *= base) { + limbLen++; + } + limbLen--; + limbPow = (limbPow / base) | 0; + + var total = number.length - start; + var mod = total % limbLen; + var end = Math.min(total, total - mod) + start; + + var word = 0; + for (var i = start; i < end; i += limbLen) { + word = parseBase(number, i, i + limbLen, base); + + this.imuln(limbPow); + if (this.words[0] + word < 0x4000000) { + this.words[0] += word; + } else { + this._iaddn(word); + } + } + + if (mod !== 0) { + var pow = 1; + word = parseBase(number, i, number.length, base); + + for (i = 0; i < mod; i++) { + pow *= base; + } + + this.imuln(pow); + if (this.words[0] + word < 0x4000000) { + this.words[0] += word; + } else { + this._iaddn(word); + } + } + + this._strip(); + }; + + BN.prototype.copy = function copy (dest) { + dest.words = new Array(this.length); + for (var i = 0; i < this.length; i++) { + dest.words[i] = this.words[i]; + } + dest.length = this.length; + dest.negative = this.negative; + dest.red = this.red; + }; + + function move (dest, src) { + dest.words = src.words; + dest.length = src.length; + dest.negative = src.negative; + dest.red = src.red; + } + + BN.prototype._move = function _move (dest) { + move(dest, this); + }; + + BN.prototype.clone = function clone () { + var r = new BN(null); + this.copy(r); + return r; + }; + + BN.prototype._expand = function _expand (size) { + while (this.length < size) { + this.words[this.length++] = 0; + } + return this; + }; + + // Remove leading `0` from `this` + BN.prototype._strip = function strip () { + while (this.length > 1 && this.words[this.length - 1] === 0) { + this.length--; + } + return this._normSign(); + }; + + BN.prototype._normSign = function _normSign () { + // -0 = 0 + if (this.length === 1 && this.words[0] === 0) { + this.negative = 0; + } + return this; + }; + + // Check Symbol.for because not everywhere where Symbol defined + // See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol#Browser_compatibility + if (typeof Symbol !== 'undefined' && typeof Symbol.for === 'function') { + try { + BN.prototype[Symbol.for('nodejs.util.inspect.custom')] = inspect; + } catch (e) { + BN.prototype.inspect = inspect; + } + } else { + BN.prototype.inspect = inspect; + } + + function inspect () { + return (this.red ? ''; + } + + /* + + var zeros = []; + var groupSizes = []; + var groupBases = []; + + var s = ''; + var i = -1; + while (++i < BN.wordSize) { + zeros[i] = s; + s += '0'; + } + groupSizes[0] = 0; + groupSizes[1] = 0; + groupBases[0] = 0; + groupBases[1] = 0; + var base = 2 - 1; + while (++base < 36 + 1) { + var groupSize = 0; + var groupBase = 1; + while (groupBase < (1 << BN.wordSize) / base) { + groupBase *= base; + groupSize += 1; + } + groupSizes[base] = groupSize; + groupBases[base] = groupBase; + } + + */ + + var zeros = [ + '', + '0', + '00', + '000', + '0000', + '00000', + '000000', + '0000000', + '00000000', + '000000000', + '0000000000', + '00000000000', + '000000000000', + '0000000000000', + '00000000000000', + '000000000000000', + '0000000000000000', + '00000000000000000', + '000000000000000000', + '0000000000000000000', + '00000000000000000000', + '000000000000000000000', + '0000000000000000000000', + '00000000000000000000000', + '000000000000000000000000', + '0000000000000000000000000' + ]; + + var groupSizes = [ + 0, 0, + 25, 16, 12, 11, 10, 9, 8, + 8, 7, 7, 7, 7, 6, 6, + 6, 6, 6, 6, 6, 5, 5, + 5, 5, 5, 5, 5, 5, 5, + 5, 5, 5, 5, 5, 5, 5 + ]; + + var groupBases = [ + 0, 0, + 33554432, 43046721, 16777216, 48828125, 60466176, 40353607, 16777216, + 43046721, 10000000, 19487171, 35831808, 62748517, 7529536, 11390625, + 16777216, 24137569, 34012224, 47045881, 64000000, 4084101, 5153632, + 6436343, 7962624, 9765625, 11881376, 14348907, 17210368, 20511149, + 24300000, 28629151, 33554432, 39135393, 45435424, 52521875, 60466176 + ]; + + BN.prototype.toString = function toString (base, padding) { + base = base || 10; + padding = padding | 0 || 1; + + var out; + if (base === 16 || base === 'hex') { + out = ''; + var off = 0; + var carry = 0; + for (var i = 0; i < this.length; i++) { + var w = this.words[i]; + var word = (((w << off) | carry) & 0xffffff).toString(16); + carry = (w >>> (24 - off)) & 0xffffff; + off += 2; + if (off >= 26) { + off -= 26; + i--; + } + if (carry !== 0 || i !== this.length - 1) { + out = zeros[6 - word.length] + word + out; + } else { + out = word + out; + } + } + if (carry !== 0) { + out = carry.toString(16) + out; + } + while (out.length % padding !== 0) { + out = '0' + out; + } + if (this.negative !== 0) { + out = '-' + out; + } + return out; + } + + if (base === (base | 0) && base >= 2 && base <= 36) { + // var groupSize = Math.floor(BN.wordSize * Math.LN2 / Math.log(base)); + var groupSize = groupSizes[base]; + // var groupBase = Math.pow(base, groupSize); + var groupBase = groupBases[base]; + out = ''; + var c = this.clone(); + c.negative = 0; + while (!c.isZero()) { + var r = c.modrn(groupBase).toString(base); + c = c.idivn(groupBase); + + if (!c.isZero()) { + out = zeros[groupSize - r.length] + r + out; + } else { + out = r + out; + } + } + if (this.isZero()) { + out = '0' + out; + } + while (out.length % padding !== 0) { + out = '0' + out; + } + if (this.negative !== 0) { + out = '-' + out; + } + return out; + } + + assert(false, 'Base should be between 2 and 36'); + }; + + BN.prototype.toNumber = function toNumber () { + var ret = this.words[0]; + if (this.length === 2) { + ret += this.words[1] * 0x4000000; + } else if (this.length === 3 && this.words[2] === 0x01) { + // NOTE: at this stage it is known that the top bit is set + ret += 0x10000000000000 + (this.words[1] * 0x4000000); + } else if (this.length > 2) { + assert(false, 'Number can only safely store up to 53 bits'); + } + return (this.negative !== 0) ? -ret : ret; + }; + + BN.prototype.toJSON = function toJSON () { + return this.toString(16, 2); + }; + + if (Buffer) { + BN.prototype.toBuffer = function toBuffer (endian, length) { + return this.toArrayLike(Buffer, endian, length); + }; + } + + BN.prototype.toArray = function toArray (endian, length) { + return this.toArrayLike(Array, endian, length); + }; + + var allocate = function allocate (ArrayType, size) { + if (ArrayType.allocUnsafe) { + return ArrayType.allocUnsafe(size); + } + return new ArrayType(size); + }; + + BN.prototype.toArrayLike = function toArrayLike (ArrayType, endian, length) { + this._strip(); + + var byteLength = this.byteLength(); + var reqLength = length || Math.max(1, byteLength); + assert(byteLength <= reqLength, 'byte array longer than desired length'); + assert(reqLength > 0, 'Requested array length <= 0'); + + var res = allocate(ArrayType, reqLength); + var postfix = endian === 'le' ? 'LE' : 'BE'; + this['_toArrayLike' + postfix](res, byteLength); + return res; + }; + + BN.prototype._toArrayLikeLE = function _toArrayLikeLE (res, byteLength) { + var position = 0; + var carry = 0; + + for (var i = 0, shift = 0; i < this.length; i++) { + var word = (this.words[i] << shift) | carry; + + res[position++] = word & 0xff; + if (position < res.length) { + res[position++] = (word >> 8) & 0xff; + } + if (position < res.length) { + res[position++] = (word >> 16) & 0xff; + } + + if (shift === 6) { + if (position < res.length) { + res[position++] = (word >> 24) & 0xff; + } + carry = 0; + shift = 0; + } else { + carry = word >>> 24; + shift += 2; + } + } + + if (position < res.length) { + res[position++] = carry; + + while (position < res.length) { + res[position++] = 0; + } + } + }; + + BN.prototype._toArrayLikeBE = function _toArrayLikeBE (res, byteLength) { + var position = res.length - 1; + var carry = 0; + + for (var i = 0, shift = 0; i < this.length; i++) { + var word = (this.words[i] << shift) | carry; + + res[position--] = word & 0xff; + if (position >= 0) { + res[position--] = (word >> 8) & 0xff; + } + if (position >= 0) { + res[position--] = (word >> 16) & 0xff; + } + + if (shift === 6) { + if (position >= 0) { + res[position--] = (word >> 24) & 0xff; + } + carry = 0; + shift = 0; + } else { + carry = word >>> 24; + shift += 2; + } + } + + if (position >= 0) { + res[position--] = carry; + + while (position >= 0) { + res[position--] = 0; + } + } + }; + + if (Math.clz32) { + BN.prototype._countBits = function _countBits (w) { + return 32 - Math.clz32(w); + }; + } else { + BN.prototype._countBits = function _countBits (w) { + var t = w; + var r = 0; + if (t >= 0x1000) { + r += 13; + t >>>= 13; + } + if (t >= 0x40) { + r += 7; + t >>>= 7; + } + if (t >= 0x8) { + r += 4; + t >>>= 4; + } + if (t >= 0x02) { + r += 2; + t >>>= 2; + } + return r + t; + }; + } + + BN.prototype._zeroBits = function _zeroBits (w) { + // Short-cut + if (w === 0) return 26; + + var t = w; + var r = 0; + if ((t & 0x1fff) === 0) { + r += 13; + t >>>= 13; + } + if ((t & 0x7f) === 0) { + r += 7; + t >>>= 7; + } + if ((t & 0xf) === 0) { + r += 4; + t >>>= 4; + } + if ((t & 0x3) === 0) { + r += 2; + t >>>= 2; + } + if ((t & 0x1) === 0) { + r++; + } + return r; + }; + + // Return number of used bits in a BN + BN.prototype.bitLength = function bitLength () { + var w = this.words[this.length - 1]; + var hi = this._countBits(w); + return (this.length - 1) * 26 + hi; + }; + + function toBitArray (num) { + var w = new Array(num.bitLength()); + + for (var bit = 0; bit < w.length; bit++) { + var off = (bit / 26) | 0; + var wbit = bit % 26; + + w[bit] = (num.words[off] >>> wbit) & 0x01; + } + + return w; + } + + // Number of trailing zero bits + BN.prototype.zeroBits = function zeroBits () { + if (this.isZero()) return 0; + + var r = 0; + for (var i = 0; i < this.length; i++) { + var b = this._zeroBits(this.words[i]); + r += b; + if (b !== 26) break; + } + return r; + }; + + BN.prototype.byteLength = function byteLength () { + return Math.ceil(this.bitLength() / 8); + }; + + BN.prototype.toTwos = function toTwos (width) { + if (this.negative !== 0) { + return this.abs().inotn(width).iaddn(1); + } + return this.clone(); + }; + + BN.prototype.fromTwos = function fromTwos (width) { + if (this.testn(width - 1)) { + return this.notn(width).iaddn(1).ineg(); + } + return this.clone(); + }; + + BN.prototype.isNeg = function isNeg () { + return this.negative !== 0; + }; + + // Return negative clone of `this` + BN.prototype.neg = function neg () { + return this.clone().ineg(); + }; + + BN.prototype.ineg = function ineg () { + if (!this.isZero()) { + this.negative ^= 1; + } + + return this; + }; + + // Or `num` with `this` in-place + BN.prototype.iuor = function iuor (num) { + while (this.length < num.length) { + this.words[this.length++] = 0; + } + + for (var i = 0; i < num.length; i++) { + this.words[i] = this.words[i] | num.words[i]; + } + + return this._strip(); + }; + + BN.prototype.ior = function ior (num) { + assert((this.negative | num.negative) === 0); + return this.iuor(num); + }; + + // Or `num` with `this` + BN.prototype.or = function or (num) { + if (this.length > num.length) return this.clone().ior(num); + return num.clone().ior(this); + }; + + BN.prototype.uor = function uor (num) { + if (this.length > num.length) return this.clone().iuor(num); + return num.clone().iuor(this); + }; + + // And `num` with `this` in-place + BN.prototype.iuand = function iuand (num) { + // b = min-length(num, this) + var b; + if (this.length > num.length) { + b = num; + } else { + b = this; + } + + for (var i = 0; i < b.length; i++) { + this.words[i] = this.words[i] & num.words[i]; + } + + this.length = b.length; + + return this._strip(); + }; + + BN.prototype.iand = function iand (num) { + assert((this.negative | num.negative) === 0); + return this.iuand(num); + }; + + // And `num` with `this` + BN.prototype.and = function and (num) { + if (this.length > num.length) return this.clone().iand(num); + return num.clone().iand(this); + }; + + BN.prototype.uand = function uand (num) { + if (this.length > num.length) return this.clone().iuand(num); + return num.clone().iuand(this); + }; + + // Xor `num` with `this` in-place + BN.prototype.iuxor = function iuxor (num) { + // a.length > b.length + var a; + var b; + if (this.length > num.length) { + a = this; + b = num; + } else { + a = num; + b = this; + } + + for (var i = 0; i < b.length; i++) { + this.words[i] = a.words[i] ^ b.words[i]; + } + + if (this !== a) { + for (; i < a.length; i++) { + this.words[i] = a.words[i]; + } + } + + this.length = a.length; + + return this._strip(); + }; + + BN.prototype.ixor = function ixor (num) { + assert((this.negative | num.negative) === 0); + return this.iuxor(num); + }; + + // Xor `num` with `this` + BN.prototype.xor = function xor (num) { + if (this.length > num.length) return this.clone().ixor(num); + return num.clone().ixor(this); + }; + + BN.prototype.uxor = function uxor (num) { + if (this.length > num.length) return this.clone().iuxor(num); + return num.clone().iuxor(this); + }; + + // Not ``this`` with ``width`` bitwidth + BN.prototype.inotn = function inotn (width) { + assert(typeof width === 'number' && width >= 0); + + var bytesNeeded = Math.ceil(width / 26) | 0; + var bitsLeft = width % 26; + + // Extend the buffer with leading zeroes + this._expand(bytesNeeded); + + if (bitsLeft > 0) { + bytesNeeded--; + } + + // Handle complete words + for (var i = 0; i < bytesNeeded; i++) { + this.words[i] = ~this.words[i] & 0x3ffffff; + } + + // Handle the residue + if (bitsLeft > 0) { + this.words[i] = ~this.words[i] & (0x3ffffff >> (26 - bitsLeft)); + } + + // And remove leading zeroes + return this._strip(); + }; + + BN.prototype.notn = function notn (width) { + return this.clone().inotn(width); + }; + + // Set `bit` of `this` + BN.prototype.setn = function setn (bit, val) { + assert(typeof bit === 'number' && bit >= 0); + + var off = (bit / 26) | 0; + var wbit = bit % 26; + + this._expand(off + 1); + + if (val) { + this.words[off] = this.words[off] | (1 << wbit); + } else { + this.words[off] = this.words[off] & ~(1 << wbit); + } + + return this._strip(); + }; + + // Add `num` to `this` in-place + BN.prototype.iadd = function iadd (num) { + var r; + + // negative + positive + if (this.negative !== 0 && num.negative === 0) { + this.negative = 0; + r = this.isub(num); + this.negative ^= 1; + return this._normSign(); + + // positive + negative + } else if (this.negative === 0 && num.negative !== 0) { + num.negative = 0; + r = this.isub(num); + num.negative = 1; + return r._normSign(); + } + + // a.length > b.length + var a, b; + if (this.length > num.length) { + a = this; + b = num; + } else { + a = num; + b = this; + } + + var carry = 0; + for (var i = 0; i < b.length; i++) { + r = (a.words[i] | 0) + (b.words[i] | 0) + carry; + this.words[i] = r & 0x3ffffff; + carry = r >>> 26; + } + for (; carry !== 0 && i < a.length; i++) { + r = (a.words[i] | 0) + carry; + this.words[i] = r & 0x3ffffff; + carry = r >>> 26; + } + + this.length = a.length; + if (carry !== 0) { + this.words[this.length] = carry; + this.length++; + // Copy the rest of the words + } else if (a !== this) { + for (; i < a.length; i++) { + this.words[i] = a.words[i]; + } + } + + return this; + }; + + // Add `num` to `this` + BN.prototype.add = function add (num) { + var res; + if (num.negative !== 0 && this.negative === 0) { + num.negative = 0; + res = this.sub(num); + num.negative ^= 1; + return res; + } else if (num.negative === 0 && this.negative !== 0) { + this.negative = 0; + res = num.sub(this); + this.negative = 1; + return res; + } + + if (this.length > num.length) return this.clone().iadd(num); + + return num.clone().iadd(this); + }; + + // Subtract `num` from `this` in-place + BN.prototype.isub = function isub (num) { + // this - (-num) = this + num + if (num.negative !== 0) { + num.negative = 0; + var r = this.iadd(num); + num.negative = 1; + return r._normSign(); + + // -this - num = -(this + num) + } else if (this.negative !== 0) { + this.negative = 0; + this.iadd(num); + this.negative = 1; + return this._normSign(); + } + + // At this point both numbers are positive + var cmp = this.cmp(num); + + // Optimization - zeroify + if (cmp === 0) { + this.negative = 0; + this.length = 1; + this.words[0] = 0; + return this; + } + + // a > b + var a, b; + if (cmp > 0) { + a = this; + b = num; + } else { + a = num; + b = this; + } + + var carry = 0; + for (var i = 0; i < b.length; i++) { + r = (a.words[i] | 0) - (b.words[i] | 0) + carry; + carry = r >> 26; + this.words[i] = r & 0x3ffffff; + } + for (; carry !== 0 && i < a.length; i++) { + r = (a.words[i] | 0) + carry; + carry = r >> 26; + this.words[i] = r & 0x3ffffff; + } + + // Copy rest of the words + if (carry === 0 && i < a.length && a !== this) { + for (; i < a.length; i++) { + this.words[i] = a.words[i]; + } + } + + this.length = Math.max(this.length, i); + + if (a !== this) { + this.negative = 1; + } + + return this._strip(); + }; + + // Subtract `num` from `this` + BN.prototype.sub = function sub (num) { + return this.clone().isub(num); + }; + + function smallMulTo (self, num, out) { + out.negative = num.negative ^ self.negative; + var len = (self.length + num.length) | 0; + out.length = len; + len = (len - 1) | 0; + + // Peel one iteration (compiler can't do it, because of code complexity) + var a = self.words[0] | 0; + var b = num.words[0] | 0; + var r = a * b; + + var lo = r & 0x3ffffff; + var carry = (r / 0x4000000) | 0; + out.words[0] = lo; + + for (var k = 1; k < len; k++) { + // Sum all words with the same `i + j = k` and accumulate `ncarry`, + // note that ncarry could be >= 0x3ffffff + var ncarry = carry >>> 26; + var rword = carry & 0x3ffffff; + var maxJ = Math.min(k, num.length - 1); + for (var j = Math.max(0, k - self.length + 1); j <= maxJ; j++) { + var i = (k - j) | 0; + a = self.words[i] | 0; + b = num.words[j] | 0; + r = a * b + rword; + ncarry += (r / 0x4000000) | 0; + rword = r & 0x3ffffff; + } + out.words[k] = rword | 0; + carry = ncarry | 0; + } + if (carry !== 0) { + out.words[k] = carry | 0; + } else { + out.length--; + } + + return out._strip(); + } + + // TODO(indutny): it may be reasonable to omit it for users who don't need + // to work with 256-bit numbers, otherwise it gives 20% improvement for 256-bit + // multiplication (like elliptic secp256k1). + var comb10MulTo = function comb10MulTo (self, num, out) { + var a = self.words; + var b = num.words; + var o = out.words; + var c = 0; + var lo; + var mid; + var hi; + var a0 = a[0] | 0; + var al0 = a0 & 0x1fff; + var ah0 = a0 >>> 13; + var a1 = a[1] | 0; + var al1 = a1 & 0x1fff; + var ah1 = a1 >>> 13; + var a2 = a[2] | 0; + var al2 = a2 & 0x1fff; + var ah2 = a2 >>> 13; + var a3 = a[3] | 0; + var al3 = a3 & 0x1fff; + var ah3 = a3 >>> 13; + var a4 = a[4] | 0; + var al4 = a4 & 0x1fff; + var ah4 = a4 >>> 13; + var a5 = a[5] | 0; + var al5 = a5 & 0x1fff; + var ah5 = a5 >>> 13; + var a6 = a[6] | 0; + var al6 = a6 & 0x1fff; + var ah6 = a6 >>> 13; + var a7 = a[7] | 0; + var al7 = a7 & 0x1fff; + var ah7 = a7 >>> 13; + var a8 = a[8] | 0; + var al8 = a8 & 0x1fff; + var ah8 = a8 >>> 13; + var a9 = a[9] | 0; + var al9 = a9 & 0x1fff; + var ah9 = a9 >>> 13; + var b0 = b[0] | 0; + var bl0 = b0 & 0x1fff; + var bh0 = b0 >>> 13; + var b1 = b[1] | 0; + var bl1 = b1 & 0x1fff; + var bh1 = b1 >>> 13; + var b2 = b[2] | 0; + var bl2 = b2 & 0x1fff; + var bh2 = b2 >>> 13; + var b3 = b[3] | 0; + var bl3 = b3 & 0x1fff; + var bh3 = b3 >>> 13; + var b4 = b[4] | 0; + var bl4 = b4 & 0x1fff; + var bh4 = b4 >>> 13; + var b5 = b[5] | 0; + var bl5 = b5 & 0x1fff; + var bh5 = b5 >>> 13; + var b6 = b[6] | 0; + var bl6 = b6 & 0x1fff; + var bh6 = b6 >>> 13; + var b7 = b[7] | 0; + var bl7 = b7 & 0x1fff; + var bh7 = b7 >>> 13; + var b8 = b[8] | 0; + var bl8 = b8 & 0x1fff; + var bh8 = b8 >>> 13; + var b9 = b[9] | 0; + var bl9 = b9 & 0x1fff; + var bh9 = b9 >>> 13; + + out.negative = self.negative ^ num.negative; + out.length = 19; + /* k = 0 */ + lo = Math.imul(al0, bl0); + mid = Math.imul(al0, bh0); + mid = (mid + Math.imul(ah0, bl0)) | 0; + hi = Math.imul(ah0, bh0); + var w0 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; + c = (((hi + (mid >>> 13)) | 0) + (w0 >>> 26)) | 0; + w0 &= 0x3ffffff; + /* k = 1 */ + lo = Math.imul(al1, bl0); + mid = Math.imul(al1, bh0); + mid = (mid + Math.imul(ah1, bl0)) | 0; + hi = Math.imul(ah1, bh0); + lo = (lo + Math.imul(al0, bl1)) | 0; + mid = (mid + Math.imul(al0, bh1)) | 0; + mid = (mid + Math.imul(ah0, bl1)) | 0; + hi = (hi + Math.imul(ah0, bh1)) | 0; + var w1 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; + c = (((hi + (mid >>> 13)) | 0) + (w1 >>> 26)) | 0; + w1 &= 0x3ffffff; + /* k = 2 */ + lo = Math.imul(al2, bl0); + mid = Math.imul(al2, bh0); + mid = (mid + Math.imul(ah2, bl0)) | 0; + hi = Math.imul(ah2, bh0); + lo = (lo + Math.imul(al1, bl1)) | 0; + mid = (mid + Math.imul(al1, bh1)) | 0; + mid = (mid + Math.imul(ah1, bl1)) | 0; + hi = (hi + Math.imul(ah1, bh1)) | 0; + lo = (lo + Math.imul(al0, bl2)) | 0; + mid = (mid + Math.imul(al0, bh2)) | 0; + mid = (mid + Math.imul(ah0, bl2)) | 0; + hi = (hi + Math.imul(ah0, bh2)) | 0; + var w2 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; + c = (((hi + (mid >>> 13)) | 0) + (w2 >>> 26)) | 0; + w2 &= 0x3ffffff; + /* k = 3 */ + lo = Math.imul(al3, bl0); + mid = Math.imul(al3, bh0); + mid = (mid + Math.imul(ah3, bl0)) | 0; + hi = Math.imul(ah3, bh0); + lo = (lo + Math.imul(al2, bl1)) | 0; + mid = (mid + Math.imul(al2, bh1)) | 0; + mid = (mid + Math.imul(ah2, bl1)) | 0; + hi = (hi + Math.imul(ah2, bh1)) | 0; + lo = (lo + Math.imul(al1, bl2)) | 0; + mid = (mid + Math.imul(al1, bh2)) | 0; + mid = (mid + Math.imul(ah1, bl2)) | 0; + hi = (hi + Math.imul(ah1, bh2)) | 0; + lo = (lo + Math.imul(al0, bl3)) | 0; + mid = (mid + Math.imul(al0, bh3)) | 0; + mid = (mid + Math.imul(ah0, bl3)) | 0; + hi = (hi + Math.imul(ah0, bh3)) | 0; + var w3 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; + c = (((hi + (mid >>> 13)) | 0) + (w3 >>> 26)) | 0; + w3 &= 0x3ffffff; + /* k = 4 */ + lo = Math.imul(al4, bl0); + mid = Math.imul(al4, bh0); + mid = (mid + Math.imul(ah4, bl0)) | 0; + hi = Math.imul(ah4, bh0); + lo = (lo + Math.imul(al3, bl1)) | 0; + mid = (mid + Math.imul(al3, bh1)) | 0; + mid = (mid + Math.imul(ah3, bl1)) | 0; + hi = (hi + Math.imul(ah3, bh1)) | 0; + lo = (lo + Math.imul(al2, bl2)) | 0; + mid = (mid + Math.imul(al2, bh2)) | 0; + mid = (mid + Math.imul(ah2, bl2)) | 0; + hi = (hi + Math.imul(ah2, bh2)) | 0; + lo = (lo + Math.imul(al1, bl3)) | 0; + mid = (mid + Math.imul(al1, bh3)) | 0; + mid = (mid + Math.imul(ah1, bl3)) | 0; + hi = (hi + Math.imul(ah1, bh3)) | 0; + lo = (lo + Math.imul(al0, bl4)) | 0; + mid = (mid + Math.imul(al0, bh4)) | 0; + mid = (mid + Math.imul(ah0, bl4)) | 0; + hi = (hi + Math.imul(ah0, bh4)) | 0; + var w4 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; + c = (((hi + (mid >>> 13)) | 0) + (w4 >>> 26)) | 0; + w4 &= 0x3ffffff; + /* k = 5 */ + lo = Math.imul(al5, bl0); + mid = Math.imul(al5, bh0); + mid = (mid + Math.imul(ah5, bl0)) | 0; + hi = Math.imul(ah5, bh0); + lo = (lo + Math.imul(al4, bl1)) | 0; + mid = (mid + Math.imul(al4, bh1)) | 0; + mid = (mid + Math.imul(ah4, bl1)) | 0; + hi = (hi + Math.imul(ah4, bh1)) | 0; + lo = (lo + Math.imul(al3, bl2)) | 0; + mid = (mid + Math.imul(al3, bh2)) | 0; + mid = (mid + Math.imul(ah3, bl2)) | 0; + hi = (hi + Math.imul(ah3, bh2)) | 0; + lo = (lo + Math.imul(al2, bl3)) | 0; + mid = (mid + Math.imul(al2, bh3)) | 0; + mid = (mid + Math.imul(ah2, bl3)) | 0; + hi = (hi + Math.imul(ah2, bh3)) | 0; + lo = (lo + Math.imul(al1, bl4)) | 0; + mid = (mid + Math.imul(al1, bh4)) | 0; + mid = (mid + Math.imul(ah1, bl4)) | 0; + hi = (hi + Math.imul(ah1, bh4)) | 0; + lo = (lo + Math.imul(al0, bl5)) | 0; + mid = (mid + Math.imul(al0, bh5)) | 0; + mid = (mid + Math.imul(ah0, bl5)) | 0; + hi = (hi + Math.imul(ah0, bh5)) | 0; + var w5 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; + c = (((hi + (mid >>> 13)) | 0) + (w5 >>> 26)) | 0; + w5 &= 0x3ffffff; + /* k = 6 */ + lo = Math.imul(al6, bl0); + mid = Math.imul(al6, bh0); + mid = (mid + Math.imul(ah6, bl0)) | 0; + hi = Math.imul(ah6, bh0); + lo = (lo + Math.imul(al5, bl1)) | 0; + mid = (mid + Math.imul(al5, bh1)) | 0; + mid = (mid + Math.imul(ah5, bl1)) | 0; + hi = (hi + Math.imul(ah5, bh1)) | 0; + lo = (lo + Math.imul(al4, bl2)) | 0; + mid = (mid + Math.imul(al4, bh2)) | 0; + mid = (mid + Math.imul(ah4, bl2)) | 0; + hi = (hi + Math.imul(ah4, bh2)) | 0; + lo = (lo + Math.imul(al3, bl3)) | 0; + mid = (mid + Math.imul(al3, bh3)) | 0; + mid = (mid + Math.imul(ah3, bl3)) | 0; + hi = (hi + Math.imul(ah3, bh3)) | 0; + lo = (lo + Math.imul(al2, bl4)) | 0; + mid = (mid + Math.imul(al2, bh4)) | 0; + mid = (mid + Math.imul(ah2, bl4)) | 0; + hi = (hi + Math.imul(ah2, bh4)) | 0; + lo = (lo + Math.imul(al1, bl5)) | 0; + mid = (mid + Math.imul(al1, bh5)) | 0; + mid = (mid + Math.imul(ah1, bl5)) | 0; + hi = (hi + Math.imul(ah1, bh5)) | 0; + lo = (lo + Math.imul(al0, bl6)) | 0; + mid = (mid + Math.imul(al0, bh6)) | 0; + mid = (mid + Math.imul(ah0, bl6)) | 0; + hi = (hi + Math.imul(ah0, bh6)) | 0; + var w6 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; + c = (((hi + (mid >>> 13)) | 0) + (w6 >>> 26)) | 0; + w6 &= 0x3ffffff; + /* k = 7 */ + lo = Math.imul(al7, bl0); + mid = Math.imul(al7, bh0); + mid = (mid + Math.imul(ah7, bl0)) | 0; + hi = Math.imul(ah7, bh0); + lo = (lo + Math.imul(al6, bl1)) | 0; + mid = (mid + Math.imul(al6, bh1)) | 0; + mid = (mid + Math.imul(ah6, bl1)) | 0; + hi = (hi + Math.imul(ah6, bh1)) | 0; + lo = (lo + Math.imul(al5, bl2)) | 0; + mid = (mid + Math.imul(al5, bh2)) | 0; + mid = (mid + Math.imul(ah5, bl2)) | 0; + hi = (hi + Math.imul(ah5, bh2)) | 0; + lo = (lo + Math.imul(al4, bl3)) | 0; + mid = (mid + Math.imul(al4, bh3)) | 0; + mid = (mid + Math.imul(ah4, bl3)) | 0; + hi = (hi + Math.imul(ah4, bh3)) | 0; + lo = (lo + Math.imul(al3, bl4)) | 0; + mid = (mid + Math.imul(al3, bh4)) | 0; + mid = (mid + Math.imul(ah3, bl4)) | 0; + hi = (hi + Math.imul(ah3, bh4)) | 0; + lo = (lo + Math.imul(al2, bl5)) | 0; + mid = (mid + Math.imul(al2, bh5)) | 0; + mid = (mid + Math.imul(ah2, bl5)) | 0; + hi = (hi + Math.imul(ah2, bh5)) | 0; + lo = (lo + Math.imul(al1, bl6)) | 0; + mid = (mid + Math.imul(al1, bh6)) | 0; + mid = (mid + Math.imul(ah1, bl6)) | 0; + hi = (hi + Math.imul(ah1, bh6)) | 0; + lo = (lo + Math.imul(al0, bl7)) | 0; + mid = (mid + Math.imul(al0, bh7)) | 0; + mid = (mid + Math.imul(ah0, bl7)) | 0; + hi = (hi + Math.imul(ah0, bh7)) | 0; + var w7 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; + c = (((hi + (mid >>> 13)) | 0) + (w7 >>> 26)) | 0; + w7 &= 0x3ffffff; + /* k = 8 */ + lo = Math.imul(al8, bl0); + mid = Math.imul(al8, bh0); + mid = (mid + Math.imul(ah8, bl0)) | 0; + hi = Math.imul(ah8, bh0); + lo = (lo + Math.imul(al7, bl1)) | 0; + mid = (mid + Math.imul(al7, bh1)) | 0; + mid = (mid + Math.imul(ah7, bl1)) | 0; + hi = (hi + Math.imul(ah7, bh1)) | 0; + lo = (lo + Math.imul(al6, bl2)) | 0; + mid = (mid + Math.imul(al6, bh2)) | 0; + mid = (mid + Math.imul(ah6, bl2)) | 0; + hi = (hi + Math.imul(ah6, bh2)) | 0; + lo = (lo + Math.imul(al5, bl3)) | 0; + mid = (mid + Math.imul(al5, bh3)) | 0; + mid = (mid + Math.imul(ah5, bl3)) | 0; + hi = (hi + Math.imul(ah5, bh3)) | 0; + lo = (lo + Math.imul(al4, bl4)) | 0; + mid = (mid + Math.imul(al4, bh4)) | 0; + mid = (mid + Math.imul(ah4, bl4)) | 0; + hi = (hi + Math.imul(ah4, bh4)) | 0; + lo = (lo + Math.imul(al3, bl5)) | 0; + mid = (mid + Math.imul(al3, bh5)) | 0; + mid = (mid + Math.imul(ah3, bl5)) | 0; + hi = (hi + Math.imul(ah3, bh5)) | 0; + lo = (lo + Math.imul(al2, bl6)) | 0; + mid = (mid + Math.imul(al2, bh6)) | 0; + mid = (mid + Math.imul(ah2, bl6)) | 0; + hi = (hi + Math.imul(ah2, bh6)) | 0; + lo = (lo + Math.imul(al1, bl7)) | 0; + mid = (mid + Math.imul(al1, bh7)) | 0; + mid = (mid + Math.imul(ah1, bl7)) | 0; + hi = (hi + Math.imul(ah1, bh7)) | 0; + lo = (lo + Math.imul(al0, bl8)) | 0; + mid = (mid + Math.imul(al0, bh8)) | 0; + mid = (mid + Math.imul(ah0, bl8)) | 0; + hi = (hi + Math.imul(ah0, bh8)) | 0; + var w8 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; + c = (((hi + (mid >>> 13)) | 0) + (w8 >>> 26)) | 0; + w8 &= 0x3ffffff; + /* k = 9 */ + lo = Math.imul(al9, bl0); + mid = Math.imul(al9, bh0); + mid = (mid + Math.imul(ah9, bl0)) | 0; + hi = Math.imul(ah9, bh0); + lo = (lo + Math.imul(al8, bl1)) | 0; + mid = (mid + Math.imul(al8, bh1)) | 0; + mid = (mid + Math.imul(ah8, bl1)) | 0; + hi = (hi + Math.imul(ah8, bh1)) | 0; + lo = (lo + Math.imul(al7, bl2)) | 0; + mid = (mid + Math.imul(al7, bh2)) | 0; + mid = (mid + Math.imul(ah7, bl2)) | 0; + hi = (hi + Math.imul(ah7, bh2)) | 0; + lo = (lo + Math.imul(al6, bl3)) | 0; + mid = (mid + Math.imul(al6, bh3)) | 0; + mid = (mid + Math.imul(ah6, bl3)) | 0; + hi = (hi + Math.imul(ah6, bh3)) | 0; + lo = (lo + Math.imul(al5, bl4)) | 0; + mid = (mid + Math.imul(al5, bh4)) | 0; + mid = (mid + Math.imul(ah5, bl4)) | 0; + hi = (hi + Math.imul(ah5, bh4)) | 0; + lo = (lo + Math.imul(al4, bl5)) | 0; + mid = (mid + Math.imul(al4, bh5)) | 0; + mid = (mid + Math.imul(ah4, bl5)) | 0; + hi = (hi + Math.imul(ah4, bh5)) | 0; + lo = (lo + Math.imul(al3, bl6)) | 0; + mid = (mid + Math.imul(al3, bh6)) | 0; + mid = (mid + Math.imul(ah3, bl6)) | 0; + hi = (hi + Math.imul(ah3, bh6)) | 0; + lo = (lo + Math.imul(al2, bl7)) | 0; + mid = (mid + Math.imul(al2, bh7)) | 0; + mid = (mid + Math.imul(ah2, bl7)) | 0; + hi = (hi + Math.imul(ah2, bh7)) | 0; + lo = (lo + Math.imul(al1, bl8)) | 0; + mid = (mid + Math.imul(al1, bh8)) | 0; + mid = (mid + Math.imul(ah1, bl8)) | 0; + hi = (hi + Math.imul(ah1, bh8)) | 0; + lo = (lo + Math.imul(al0, bl9)) | 0; + mid = (mid + Math.imul(al0, bh9)) | 0; + mid = (mid + Math.imul(ah0, bl9)) | 0; + hi = (hi + Math.imul(ah0, bh9)) | 0; + var w9 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; + c = (((hi + (mid >>> 13)) | 0) + (w9 >>> 26)) | 0; + w9 &= 0x3ffffff; + /* k = 10 */ + lo = Math.imul(al9, bl1); + mid = Math.imul(al9, bh1); + mid = (mid + Math.imul(ah9, bl1)) | 0; + hi = Math.imul(ah9, bh1); + lo = (lo + Math.imul(al8, bl2)) | 0; + mid = (mid + Math.imul(al8, bh2)) | 0; + mid = (mid + Math.imul(ah8, bl2)) | 0; + hi = (hi + Math.imul(ah8, bh2)) | 0; + lo = (lo + Math.imul(al7, bl3)) | 0; + mid = (mid + Math.imul(al7, bh3)) | 0; + mid = (mid + Math.imul(ah7, bl3)) | 0; + hi = (hi + Math.imul(ah7, bh3)) | 0; + lo = (lo + Math.imul(al6, bl4)) | 0; + mid = (mid + Math.imul(al6, bh4)) | 0; + mid = (mid + Math.imul(ah6, bl4)) | 0; + hi = (hi + Math.imul(ah6, bh4)) | 0; + lo = (lo + Math.imul(al5, bl5)) | 0; + mid = (mid + Math.imul(al5, bh5)) | 0; + mid = (mid + Math.imul(ah5, bl5)) | 0; + hi = (hi + Math.imul(ah5, bh5)) | 0; + lo = (lo + Math.imul(al4, bl6)) | 0; + mid = (mid + Math.imul(al4, bh6)) | 0; + mid = (mid + Math.imul(ah4, bl6)) | 0; + hi = (hi + Math.imul(ah4, bh6)) | 0; + lo = (lo + Math.imul(al3, bl7)) | 0; + mid = (mid + Math.imul(al3, bh7)) | 0; + mid = (mid + Math.imul(ah3, bl7)) | 0; + hi = (hi + Math.imul(ah3, bh7)) | 0; + lo = (lo + Math.imul(al2, bl8)) | 0; + mid = (mid + Math.imul(al2, bh8)) | 0; + mid = (mid + Math.imul(ah2, bl8)) | 0; + hi = (hi + Math.imul(ah2, bh8)) | 0; + lo = (lo + Math.imul(al1, bl9)) | 0; + mid = (mid + Math.imul(al1, bh9)) | 0; + mid = (mid + Math.imul(ah1, bl9)) | 0; + hi = (hi + Math.imul(ah1, bh9)) | 0; + var w10 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; + c = (((hi + (mid >>> 13)) | 0) + (w10 >>> 26)) | 0; + w10 &= 0x3ffffff; + /* k = 11 */ + lo = Math.imul(al9, bl2); + mid = Math.imul(al9, bh2); + mid = (mid + Math.imul(ah9, bl2)) | 0; + hi = Math.imul(ah9, bh2); + lo = (lo + Math.imul(al8, bl3)) | 0; + mid = (mid + Math.imul(al8, bh3)) | 0; + mid = (mid + Math.imul(ah8, bl3)) | 0; + hi = (hi + Math.imul(ah8, bh3)) | 0; + lo = (lo + Math.imul(al7, bl4)) | 0; + mid = (mid + Math.imul(al7, bh4)) | 0; + mid = (mid + Math.imul(ah7, bl4)) | 0; + hi = (hi + Math.imul(ah7, bh4)) | 0; + lo = (lo + Math.imul(al6, bl5)) | 0; + mid = (mid + Math.imul(al6, bh5)) | 0; + mid = (mid + Math.imul(ah6, bl5)) | 0; + hi = (hi + Math.imul(ah6, bh5)) | 0; + lo = (lo + Math.imul(al5, bl6)) | 0; + mid = (mid + Math.imul(al5, bh6)) | 0; + mid = (mid + Math.imul(ah5, bl6)) | 0; + hi = (hi + Math.imul(ah5, bh6)) | 0; + lo = (lo + Math.imul(al4, bl7)) | 0; + mid = (mid + Math.imul(al4, bh7)) | 0; + mid = (mid + Math.imul(ah4, bl7)) | 0; + hi = (hi + Math.imul(ah4, bh7)) | 0; + lo = (lo + Math.imul(al3, bl8)) | 0; + mid = (mid + Math.imul(al3, bh8)) | 0; + mid = (mid + Math.imul(ah3, bl8)) | 0; + hi = (hi + Math.imul(ah3, bh8)) | 0; + lo = (lo + Math.imul(al2, bl9)) | 0; + mid = (mid + Math.imul(al2, bh9)) | 0; + mid = (mid + Math.imul(ah2, bl9)) | 0; + hi = (hi + Math.imul(ah2, bh9)) | 0; + var w11 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; + c = (((hi + (mid >>> 13)) | 0) + (w11 >>> 26)) | 0; + w11 &= 0x3ffffff; + /* k = 12 */ + lo = Math.imul(al9, bl3); + mid = Math.imul(al9, bh3); + mid = (mid + Math.imul(ah9, bl3)) | 0; + hi = Math.imul(ah9, bh3); + lo = (lo + Math.imul(al8, bl4)) | 0; + mid = (mid + Math.imul(al8, bh4)) | 0; + mid = (mid + Math.imul(ah8, bl4)) | 0; + hi = (hi + Math.imul(ah8, bh4)) | 0; + lo = (lo + Math.imul(al7, bl5)) | 0; + mid = (mid + Math.imul(al7, bh5)) | 0; + mid = (mid + Math.imul(ah7, bl5)) | 0; + hi = (hi + Math.imul(ah7, bh5)) | 0; + lo = (lo + Math.imul(al6, bl6)) | 0; + mid = (mid + Math.imul(al6, bh6)) | 0; + mid = (mid + Math.imul(ah6, bl6)) | 0; + hi = (hi + Math.imul(ah6, bh6)) | 0; + lo = (lo + Math.imul(al5, bl7)) | 0; + mid = (mid + Math.imul(al5, bh7)) | 0; + mid = (mid + Math.imul(ah5, bl7)) | 0; + hi = (hi + Math.imul(ah5, bh7)) | 0; + lo = (lo + Math.imul(al4, bl8)) | 0; + mid = (mid + Math.imul(al4, bh8)) | 0; + mid = (mid + Math.imul(ah4, bl8)) | 0; + hi = (hi + Math.imul(ah4, bh8)) | 0; + lo = (lo + Math.imul(al3, bl9)) | 0; + mid = (mid + Math.imul(al3, bh9)) | 0; + mid = (mid + Math.imul(ah3, bl9)) | 0; + hi = (hi + Math.imul(ah3, bh9)) | 0; + var w12 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; + c = (((hi + (mid >>> 13)) | 0) + (w12 >>> 26)) | 0; + w12 &= 0x3ffffff; + /* k = 13 */ + lo = Math.imul(al9, bl4); + mid = Math.imul(al9, bh4); + mid = (mid + Math.imul(ah9, bl4)) | 0; + hi = Math.imul(ah9, bh4); + lo = (lo + Math.imul(al8, bl5)) | 0; + mid = (mid + Math.imul(al8, bh5)) | 0; + mid = (mid + Math.imul(ah8, bl5)) | 0; + hi = (hi + Math.imul(ah8, bh5)) | 0; + lo = (lo + Math.imul(al7, bl6)) | 0; + mid = (mid + Math.imul(al7, bh6)) | 0; + mid = (mid + Math.imul(ah7, bl6)) | 0; + hi = (hi + Math.imul(ah7, bh6)) | 0; + lo = (lo + Math.imul(al6, bl7)) | 0; + mid = (mid + Math.imul(al6, bh7)) | 0; + mid = (mid + Math.imul(ah6, bl7)) | 0; + hi = (hi + Math.imul(ah6, bh7)) | 0; + lo = (lo + Math.imul(al5, bl8)) | 0; + mid = (mid + Math.imul(al5, bh8)) | 0; + mid = (mid + Math.imul(ah5, bl8)) | 0; + hi = (hi + Math.imul(ah5, bh8)) | 0; + lo = (lo + Math.imul(al4, bl9)) | 0; + mid = (mid + Math.imul(al4, bh9)) | 0; + mid = (mid + Math.imul(ah4, bl9)) | 0; + hi = (hi + Math.imul(ah4, bh9)) | 0; + var w13 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; + c = (((hi + (mid >>> 13)) | 0) + (w13 >>> 26)) | 0; + w13 &= 0x3ffffff; + /* k = 14 */ + lo = Math.imul(al9, bl5); + mid = Math.imul(al9, bh5); + mid = (mid + Math.imul(ah9, bl5)) | 0; + hi = Math.imul(ah9, bh5); + lo = (lo + Math.imul(al8, bl6)) | 0; + mid = (mid + Math.imul(al8, bh6)) | 0; + mid = (mid + Math.imul(ah8, bl6)) | 0; + hi = (hi + Math.imul(ah8, bh6)) | 0; + lo = (lo + Math.imul(al7, bl7)) | 0; + mid = (mid + Math.imul(al7, bh7)) | 0; + mid = (mid + Math.imul(ah7, bl7)) | 0; + hi = (hi + Math.imul(ah7, bh7)) | 0; + lo = (lo + Math.imul(al6, bl8)) | 0; + mid = (mid + Math.imul(al6, bh8)) | 0; + mid = (mid + Math.imul(ah6, bl8)) | 0; + hi = (hi + Math.imul(ah6, bh8)) | 0; + lo = (lo + Math.imul(al5, bl9)) | 0; + mid = (mid + Math.imul(al5, bh9)) | 0; + mid = (mid + Math.imul(ah5, bl9)) | 0; + hi = (hi + Math.imul(ah5, bh9)) | 0; + var w14 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; + c = (((hi + (mid >>> 13)) | 0) + (w14 >>> 26)) | 0; + w14 &= 0x3ffffff; + /* k = 15 */ + lo = Math.imul(al9, bl6); + mid = Math.imul(al9, bh6); + mid = (mid + Math.imul(ah9, bl6)) | 0; + hi = Math.imul(ah9, bh6); + lo = (lo + Math.imul(al8, bl7)) | 0; + mid = (mid + Math.imul(al8, bh7)) | 0; + mid = (mid + Math.imul(ah8, bl7)) | 0; + hi = (hi + Math.imul(ah8, bh7)) | 0; + lo = (lo + Math.imul(al7, bl8)) | 0; + mid = (mid + Math.imul(al7, bh8)) | 0; + mid = (mid + Math.imul(ah7, bl8)) | 0; + hi = (hi + Math.imul(ah7, bh8)) | 0; + lo = (lo + Math.imul(al6, bl9)) | 0; + mid = (mid + Math.imul(al6, bh9)) | 0; + mid = (mid + Math.imul(ah6, bl9)) | 0; + hi = (hi + Math.imul(ah6, bh9)) | 0; + var w15 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; + c = (((hi + (mid >>> 13)) | 0) + (w15 >>> 26)) | 0; + w15 &= 0x3ffffff; + /* k = 16 */ + lo = Math.imul(al9, bl7); + mid = Math.imul(al9, bh7); + mid = (mid + Math.imul(ah9, bl7)) | 0; + hi = Math.imul(ah9, bh7); + lo = (lo + Math.imul(al8, bl8)) | 0; + mid = (mid + Math.imul(al8, bh8)) | 0; + mid = (mid + Math.imul(ah8, bl8)) | 0; + hi = (hi + Math.imul(ah8, bh8)) | 0; + lo = (lo + Math.imul(al7, bl9)) | 0; + mid = (mid + Math.imul(al7, bh9)) | 0; + mid = (mid + Math.imul(ah7, bl9)) | 0; + hi = (hi + Math.imul(ah7, bh9)) | 0; + var w16 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; + c = (((hi + (mid >>> 13)) | 0) + (w16 >>> 26)) | 0; + w16 &= 0x3ffffff; + /* k = 17 */ + lo = Math.imul(al9, bl8); + mid = Math.imul(al9, bh8); + mid = (mid + Math.imul(ah9, bl8)) | 0; + hi = Math.imul(ah9, bh8); + lo = (lo + Math.imul(al8, bl9)) | 0; + mid = (mid + Math.imul(al8, bh9)) | 0; + mid = (mid + Math.imul(ah8, bl9)) | 0; + hi = (hi + Math.imul(ah8, bh9)) | 0; + var w17 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; + c = (((hi + (mid >>> 13)) | 0) + (w17 >>> 26)) | 0; + w17 &= 0x3ffffff; + /* k = 18 */ + lo = Math.imul(al9, bl9); + mid = Math.imul(al9, bh9); + mid = (mid + Math.imul(ah9, bl9)) | 0; + hi = Math.imul(ah9, bh9); + var w18 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; + c = (((hi + (mid >>> 13)) | 0) + (w18 >>> 26)) | 0; + w18 &= 0x3ffffff; + o[0] = w0; + o[1] = w1; + o[2] = w2; + o[3] = w3; + o[4] = w4; + o[5] = w5; + o[6] = w6; + o[7] = w7; + o[8] = w8; + o[9] = w9; + o[10] = w10; + o[11] = w11; + o[12] = w12; + o[13] = w13; + o[14] = w14; + o[15] = w15; + o[16] = w16; + o[17] = w17; + o[18] = w18; + if (c !== 0) { + o[19] = c; + out.length++; + } + return out; + }; + + // Polyfill comb + if (!Math.imul) { + comb10MulTo = smallMulTo; + } + + function bigMulTo (self, num, out) { + out.negative = num.negative ^ self.negative; + out.length = self.length + num.length; + + var carry = 0; + var hncarry = 0; + for (var k = 0; k < out.length - 1; k++) { + // Sum all words with the same `i + j = k` and accumulate `ncarry`, + // note that ncarry could be >= 0x3ffffff + var ncarry = hncarry; + hncarry = 0; + var rword = carry & 0x3ffffff; + var maxJ = Math.min(k, num.length - 1); + for (var j = Math.max(0, k - self.length + 1); j <= maxJ; j++) { + var i = k - j; + var a = self.words[i] | 0; + var b = num.words[j] | 0; + var r = a * b; + + var lo = r & 0x3ffffff; + ncarry = (ncarry + ((r / 0x4000000) | 0)) | 0; + lo = (lo + rword) | 0; + rword = lo & 0x3ffffff; + ncarry = (ncarry + (lo >>> 26)) | 0; + + hncarry += ncarry >>> 26; + ncarry &= 0x3ffffff; + } + out.words[k] = rword; + carry = ncarry; + ncarry = hncarry; + } + if (carry !== 0) { + out.words[k] = carry; + } else { + out.length--; + } + + return out._strip(); + } + + function jumboMulTo (self, num, out) { + // Temporary disable, see https://github.com/indutny/bn.js/issues/211 + // var fftm = new FFTM(); + // return fftm.mulp(self, num, out); + return bigMulTo(self, num, out); + } + + BN.prototype.mulTo = function mulTo (num, out) { + var res; + var len = this.length + num.length; + if (this.length === 10 && num.length === 10) { + res = comb10MulTo(this, num, out); + } else if (len < 63) { + res = smallMulTo(this, num, out); + } else if (len < 1024) { + res = bigMulTo(this, num, out); + } else { + res = jumboMulTo(this, num, out); + } + + return res; + }; + + // Multiply `this` by `num` + BN.prototype.mul = function mul (num) { + var out = new BN(null); + out.words = new Array(this.length + num.length); + return this.mulTo(num, out); + }; + + // Multiply employing FFT + BN.prototype.mulf = function mulf (num) { + var out = new BN(null); + out.words = new Array(this.length + num.length); + return jumboMulTo(this, num, out); + }; + + // In-place Multiplication + BN.prototype.imul = function imul (num) { + return this.clone().mulTo(num, this); + }; + + BN.prototype.imuln = function imuln (num) { + var isNegNum = num < 0; + if (isNegNum) num = -num; + + assert(typeof num === 'number'); + assert(num < 0x4000000); + + // Carry + var carry = 0; + for (var i = 0; i < this.length; i++) { + var w = (this.words[i] | 0) * num; + var lo = (w & 0x3ffffff) + (carry & 0x3ffffff); + carry >>= 26; + carry += (w / 0x4000000) | 0; + // NOTE: lo is 27bit maximum + carry += lo >>> 26; + this.words[i] = lo & 0x3ffffff; + } + + if (carry !== 0) { + this.words[i] = carry; + this.length++; + } + + return isNegNum ? this.ineg() : this; + }; + + BN.prototype.muln = function muln (num) { + return this.clone().imuln(num); + }; + + // `this` * `this` + BN.prototype.sqr = function sqr () { + return this.mul(this); + }; + + // `this` * `this` in-place + BN.prototype.isqr = function isqr () { + return this.imul(this.clone()); + }; + + // Math.pow(`this`, `num`) + BN.prototype.pow = function pow (num) { + var w = toBitArray(num); + if (w.length === 0) return new BN(1); + + // Skip leading zeroes + var res = this; + for (var i = 0; i < w.length; i++, res = res.sqr()) { + if (w[i] !== 0) break; + } + + if (++i < w.length) { + for (var q = res.sqr(); i < w.length; i++, q = q.sqr()) { + if (w[i] === 0) continue; + + res = res.mul(q); + } + } + + return res; + }; + + // Shift-left in-place + BN.prototype.iushln = function iushln (bits) { + assert(typeof bits === 'number' && bits >= 0); + var r = bits % 26; + var s = (bits - r) / 26; + var carryMask = (0x3ffffff >>> (26 - r)) << (26 - r); + var i; + + if (r !== 0) { + var carry = 0; + + for (i = 0; i < this.length; i++) { + var newCarry = this.words[i] & carryMask; + var c = ((this.words[i] | 0) - newCarry) << r; + this.words[i] = c | carry; + carry = newCarry >>> (26 - r); + } + + if (carry) { + this.words[i] = carry; + this.length++; + } + } + + if (s !== 0) { + for (i = this.length - 1; i >= 0; i--) { + this.words[i + s] = this.words[i]; + } + + for (i = 0; i < s; i++) { + this.words[i] = 0; + } + + this.length += s; + } + + return this._strip(); + }; + + BN.prototype.ishln = function ishln (bits) { + // TODO(indutny): implement me + assert(this.negative === 0); + return this.iushln(bits); + }; + + // Shift-right in-place + // NOTE: `hint` is a lowest bit before trailing zeroes + // NOTE: if `extended` is present - it will be filled with destroyed bits + BN.prototype.iushrn = function iushrn (bits, hint, extended) { + assert(typeof bits === 'number' && bits >= 0); + var h; + if (hint) { + h = (hint - (hint % 26)) / 26; + } else { + h = 0; + } + + var r = bits % 26; + var s = Math.min((bits - r) / 26, this.length); + var mask = 0x3ffffff ^ ((0x3ffffff >>> r) << r); + var maskedWords = extended; + + h -= s; + h = Math.max(0, h); + + // Extended mode, copy masked part + if (maskedWords) { + for (var i = 0; i < s; i++) { + maskedWords.words[i] = this.words[i]; + } + maskedWords.length = s; + } + + if (s === 0) ; else if (this.length > s) { + this.length -= s; + for (i = 0; i < this.length; i++) { + this.words[i] = this.words[i + s]; + } + } else { + this.words[0] = 0; + this.length = 1; + } + + var carry = 0; + for (i = this.length - 1; i >= 0 && (carry !== 0 || i >= h); i--) { + var word = this.words[i] | 0; + this.words[i] = (carry << (26 - r)) | (word >>> r); + carry = word & mask; + } + + // Push carried bits as a mask + if (maskedWords && carry !== 0) { + maskedWords.words[maskedWords.length++] = carry; + } + + if (this.length === 0) { + this.words[0] = 0; + this.length = 1; + } + + return this._strip(); + }; + + BN.prototype.ishrn = function ishrn (bits, hint, extended) { + // TODO(indutny): implement me + assert(this.negative === 0); + return this.iushrn(bits, hint, extended); + }; + + // Shift-left + BN.prototype.shln = function shln (bits) { + return this.clone().ishln(bits); + }; + + BN.prototype.ushln = function ushln (bits) { + return this.clone().iushln(bits); + }; + + // Shift-right + BN.prototype.shrn = function shrn (bits) { + return this.clone().ishrn(bits); + }; + + BN.prototype.ushrn = function ushrn (bits) { + return this.clone().iushrn(bits); + }; + + // Test if n bit is set + BN.prototype.testn = function testn (bit) { + assert(typeof bit === 'number' && bit >= 0); + var r = bit % 26; + var s = (bit - r) / 26; + var q = 1 << r; + + // Fast case: bit is much higher than all existing words + if (this.length <= s) return false; + + // Check bit and return + var w = this.words[s]; + + return !!(w & q); + }; + + // Return only lowers bits of number (in-place) + BN.prototype.imaskn = function imaskn (bits) { + assert(typeof bits === 'number' && bits >= 0); + var r = bits % 26; + var s = (bits - r) / 26; + + assert(this.negative === 0, 'imaskn works only with positive numbers'); + + if (this.length <= s) { + return this; + } + + if (r !== 0) { + s++; + } + this.length = Math.min(s, this.length); + + if (r !== 0) { + var mask = 0x3ffffff ^ ((0x3ffffff >>> r) << r); + this.words[this.length - 1] &= mask; + } + + return this._strip(); + }; + + // Return only lowers bits of number + BN.prototype.maskn = function maskn (bits) { + return this.clone().imaskn(bits); + }; + + // Add plain number `num` to `this` + BN.prototype.iaddn = function iaddn (num) { + assert(typeof num === 'number'); + assert(num < 0x4000000); + if (num < 0) return this.isubn(-num); + + // Possible sign change + if (this.negative !== 0) { + if (this.length === 1 && (this.words[0] | 0) <= num) { + this.words[0] = num - (this.words[0] | 0); + this.negative = 0; + return this; + } + + this.negative = 0; + this.isubn(num); + this.negative = 1; + return this; + } + + // Add without checks + return this._iaddn(num); + }; + + BN.prototype._iaddn = function _iaddn (num) { + this.words[0] += num; + + // Carry + for (var i = 0; i < this.length && this.words[i] >= 0x4000000; i++) { + this.words[i] -= 0x4000000; + if (i === this.length - 1) { + this.words[i + 1] = 1; + } else { + this.words[i + 1]++; + } + } + this.length = Math.max(this.length, i + 1); + + return this; + }; + + // Subtract plain number `num` from `this` + BN.prototype.isubn = function isubn (num) { + assert(typeof num === 'number'); + assert(num < 0x4000000); + if (num < 0) return this.iaddn(-num); + + if (this.negative !== 0) { + this.negative = 0; + this.iaddn(num); + this.negative = 1; + return this; + } + + this.words[0] -= num; + + if (this.length === 1 && this.words[0] < 0) { + this.words[0] = -this.words[0]; + this.negative = 1; + } else { + // Carry + for (var i = 0; i < this.length && this.words[i] < 0; i++) { + this.words[i] += 0x4000000; + this.words[i + 1] -= 1; + } + } + + return this._strip(); + }; + + BN.prototype.addn = function addn (num) { + return this.clone().iaddn(num); + }; + + BN.prototype.subn = function subn (num) { + return this.clone().isubn(num); + }; + + BN.prototype.iabs = function iabs () { + this.negative = 0; + + return this; + }; + + BN.prototype.abs = function abs () { + return this.clone().iabs(); + }; + + BN.prototype._ishlnsubmul = function _ishlnsubmul (num, mul, shift) { + var len = num.length + shift; + var i; + + this._expand(len); + + var w; + var carry = 0; + for (i = 0; i < num.length; i++) { + w = (this.words[i + shift] | 0) + carry; + var right = (num.words[i] | 0) * mul; + w -= right & 0x3ffffff; + carry = (w >> 26) - ((right / 0x4000000) | 0); + this.words[i + shift] = w & 0x3ffffff; + } + for (; i < this.length - shift; i++) { + w = (this.words[i + shift] | 0) + carry; + carry = w >> 26; + this.words[i + shift] = w & 0x3ffffff; + } + + if (carry === 0) return this._strip(); + + // Subtraction overflow + assert(carry === -1); + carry = 0; + for (i = 0; i < this.length; i++) { + w = -(this.words[i] | 0) + carry; + carry = w >> 26; + this.words[i] = w & 0x3ffffff; + } + this.negative = 1; + + return this._strip(); + }; + + BN.prototype._wordDiv = function _wordDiv (num, mode) { + var shift = this.length - num.length; + + var a = this.clone(); + var b = num; + + // Normalize + var bhi = b.words[b.length - 1] | 0; + var bhiBits = this._countBits(bhi); + shift = 26 - bhiBits; + if (shift !== 0) { + b = b.ushln(shift); + a.iushln(shift); + bhi = b.words[b.length - 1] | 0; + } + + // Initialize quotient + var m = a.length - b.length; + var q; + + if (mode !== 'mod') { + q = new BN(null); + q.length = m + 1; + q.words = new Array(q.length); + for (var i = 0; i < q.length; i++) { + q.words[i] = 0; + } + } + + var diff = a.clone()._ishlnsubmul(b, 1, m); + if (diff.negative === 0) { + a = diff; + if (q) { + q.words[m] = 1; + } + } + + for (var j = m - 1; j >= 0; j--) { + var qj = (a.words[b.length + j] | 0) * 0x4000000 + + (a.words[b.length + j - 1] | 0); + + // NOTE: (qj / bhi) is (0x3ffffff * 0x4000000 + 0x3ffffff) / 0x2000000 max + // (0x7ffffff) + qj = Math.min((qj / bhi) | 0, 0x3ffffff); + + a._ishlnsubmul(b, qj, j); + while (a.negative !== 0) { + qj--; + a.negative = 0; + a._ishlnsubmul(b, 1, j); + if (!a.isZero()) { + a.negative ^= 1; + } + } + if (q) { + q.words[j] = qj; + } + } + if (q) { + q._strip(); + } + a._strip(); + + // Denormalize + if (mode !== 'div' && shift !== 0) { + a.iushrn(shift); + } + + return { + div: q || null, + mod: a + }; + }; + + // NOTE: 1) `mode` can be set to `mod` to request mod only, + // to `div` to request div only, or be absent to + // request both div & mod + // 2) `positive` is true if unsigned mod is requested + BN.prototype.divmod = function divmod (num, mode, positive) { + assert(!num.isZero()); + + if (this.isZero()) { + return { + div: new BN(0), + mod: new BN(0) + }; + } + + var div, mod, res; + if (this.negative !== 0 && num.negative === 0) { + res = this.neg().divmod(num, mode); + + if (mode !== 'mod') { + div = res.div.neg(); + } + + if (mode !== 'div') { + mod = res.mod.neg(); + if (positive && mod.negative !== 0) { + mod.iadd(num); + } + } + + return { + div: div, + mod: mod + }; + } + + if (this.negative === 0 && num.negative !== 0) { + res = this.divmod(num.neg(), mode); + + if (mode !== 'mod') { + div = res.div.neg(); + } + + return { + div: div, + mod: res.mod + }; + } + + if ((this.negative & num.negative) !== 0) { + res = this.neg().divmod(num.neg(), mode); + + if (mode !== 'div') { + mod = res.mod.neg(); + if (positive && mod.negative !== 0) { + mod.isub(num); + } + } + + return { + div: res.div, + mod: mod + }; + } + + // Both numbers are positive at this point + + // Strip both numbers to approximate shift value + if (num.length > this.length || this.cmp(num) < 0) { + return { + div: new BN(0), + mod: this + }; + } + + // Very short reduction + if (num.length === 1) { + if (mode === 'div') { + return { + div: this.divn(num.words[0]), + mod: null + }; + } + + if (mode === 'mod') { + return { + div: null, + mod: new BN(this.modrn(num.words[0])) + }; + } + + return { + div: this.divn(num.words[0]), + mod: new BN(this.modrn(num.words[0])) + }; + } + + return this._wordDiv(num, mode); + }; + + // Find `this` / `num` + BN.prototype.div = function div (num) { + return this.divmod(num, 'div', false).div; + }; + + // Find `this` % `num` + BN.prototype.mod = function mod (num) { + return this.divmod(num, 'mod', false).mod; + }; + + BN.prototype.umod = function umod (num) { + return this.divmod(num, 'mod', true).mod; + }; + + // Find Round(`this` / `num`) + BN.prototype.divRound = function divRound (num) { + var dm = this.divmod(num); + + // Fast case - exact division + if (dm.mod.isZero()) return dm.div; + + var mod = dm.div.negative !== 0 ? dm.mod.isub(num) : dm.mod; + + var half = num.ushrn(1); + var r2 = num.andln(1); + var cmp = mod.cmp(half); + + // Round down + if (cmp < 0 || (r2 === 1 && cmp === 0)) return dm.div; + + // Round up + return dm.div.negative !== 0 ? dm.div.isubn(1) : dm.div.iaddn(1); + }; + + BN.prototype.modrn = function modrn (num) { + var isNegNum = num < 0; + if (isNegNum) num = -num; + + assert(num <= 0x3ffffff); + var p = (1 << 26) % num; + + var acc = 0; + for (var i = this.length - 1; i >= 0; i--) { + acc = (p * acc + (this.words[i] | 0)) % num; + } + + return isNegNum ? -acc : acc; + }; + + // WARNING: DEPRECATED + BN.prototype.modn = function modn (num) { + return this.modrn(num); + }; + + // In-place division by number + BN.prototype.idivn = function idivn (num) { + var isNegNum = num < 0; + if (isNegNum) num = -num; + + assert(num <= 0x3ffffff); + + var carry = 0; + for (var i = this.length - 1; i >= 0; i--) { + var w = (this.words[i] | 0) + carry * 0x4000000; + this.words[i] = (w / num) | 0; + carry = w % num; + } + + this._strip(); + return isNegNum ? this.ineg() : this; + }; + + BN.prototype.divn = function divn (num) { + return this.clone().idivn(num); + }; + + BN.prototype.egcd = function egcd (p) { + assert(p.negative === 0); + assert(!p.isZero()); + + var x = this; + var y = p.clone(); + + if (x.negative !== 0) { + x = x.umod(p); + } else { + x = x.clone(); + } + + // A * x + B * y = x + var A = new BN(1); + var B = new BN(0); + + // C * x + D * y = y + var C = new BN(0); + var D = new BN(1); + + var g = 0; + + while (x.isEven() && y.isEven()) { + x.iushrn(1); + y.iushrn(1); + ++g; + } + + var yp = y.clone(); + var xp = x.clone(); + + while (!x.isZero()) { + for (var i = 0, im = 1; (x.words[0] & im) === 0 && i < 26; ++i, im <<= 1); + if (i > 0) { + x.iushrn(i); + while (i-- > 0) { + if (A.isOdd() || B.isOdd()) { + A.iadd(yp); + B.isub(xp); + } + + A.iushrn(1); + B.iushrn(1); + } + } + + for (var j = 0, jm = 1; (y.words[0] & jm) === 0 && j < 26; ++j, jm <<= 1); + if (j > 0) { + y.iushrn(j); + while (j-- > 0) { + if (C.isOdd() || D.isOdd()) { + C.iadd(yp); + D.isub(xp); + } + + C.iushrn(1); + D.iushrn(1); + } + } + + if (x.cmp(y) >= 0) { + x.isub(y); + A.isub(C); + B.isub(D); + } else { + y.isub(x); + C.isub(A); + D.isub(B); + } + } + + return { + a: C, + b: D, + gcd: y.iushln(g) + }; + }; + + // This is reduced incarnation of the binary EEA + // above, designated to invert members of the + // _prime_ fields F(p) at a maximal speed + BN.prototype._invmp = function _invmp (p) { + assert(p.negative === 0); + assert(!p.isZero()); + + var a = this; + var b = p.clone(); + + if (a.negative !== 0) { + a = a.umod(p); + } else { + a = a.clone(); + } + + var x1 = new BN(1); + var x2 = new BN(0); + + var delta = b.clone(); + + while (a.cmpn(1) > 0 && b.cmpn(1) > 0) { + for (var i = 0, im = 1; (a.words[0] & im) === 0 && i < 26; ++i, im <<= 1); + if (i > 0) { + a.iushrn(i); + while (i-- > 0) { + if (x1.isOdd()) { + x1.iadd(delta); + } + + x1.iushrn(1); + } + } + + for (var j = 0, jm = 1; (b.words[0] & jm) === 0 && j < 26; ++j, jm <<= 1); + if (j > 0) { + b.iushrn(j); + while (j-- > 0) { + if (x2.isOdd()) { + x2.iadd(delta); + } + + x2.iushrn(1); + } + } + + if (a.cmp(b) >= 0) { + a.isub(b); + x1.isub(x2); + } else { + b.isub(a); + x2.isub(x1); + } + } + + var res; + if (a.cmpn(1) === 0) { + res = x1; + } else { + res = x2; + } + + if (res.cmpn(0) < 0) { + res.iadd(p); + } + + return res; + }; + + BN.prototype.gcd = function gcd (num) { + if (this.isZero()) return num.abs(); + if (num.isZero()) return this.abs(); + + var a = this.clone(); + var b = num.clone(); + a.negative = 0; + b.negative = 0; + + // Remove common factor of two + for (var shift = 0; a.isEven() && b.isEven(); shift++) { + a.iushrn(1); + b.iushrn(1); + } + + do { + while (a.isEven()) { + a.iushrn(1); + } + while (b.isEven()) { + b.iushrn(1); + } + + var r = a.cmp(b); + if (r < 0) { + // Swap `a` and `b` to make `a` always bigger than `b` + var t = a; + a = b; + b = t; + } else if (r === 0 || b.cmpn(1) === 0) { + break; + } + + a.isub(b); + } while (true); + + return b.iushln(shift); + }; + + // Invert number in the field F(num) + BN.prototype.invm = function invm (num) { + return this.egcd(num).a.umod(num); + }; + + BN.prototype.isEven = function isEven () { + return (this.words[0] & 1) === 0; + }; + + BN.prototype.isOdd = function isOdd () { + return (this.words[0] & 1) === 1; + }; + + // And first word and num + BN.prototype.andln = function andln (num) { + return this.words[0] & num; + }; + + // Increment at the bit position in-line + BN.prototype.bincn = function bincn (bit) { + assert(typeof bit === 'number'); + var r = bit % 26; + var s = (bit - r) / 26; + var q = 1 << r; + + // Fast case: bit is much higher than all existing words + if (this.length <= s) { + this._expand(s + 1); + this.words[s] |= q; + return this; + } + + // Add bit and propagate, if needed + var carry = q; + for (var i = s; carry !== 0 && i < this.length; i++) { + var w = this.words[i] | 0; + w += carry; + carry = w >>> 26; + w &= 0x3ffffff; + this.words[i] = w; + } + if (carry !== 0) { + this.words[i] = carry; + this.length++; + } + return this; + }; + + BN.prototype.isZero = function isZero () { + return this.length === 1 && this.words[0] === 0; + }; + + BN.prototype.cmpn = function cmpn (num) { + var negative = num < 0; + + if (this.negative !== 0 && !negative) return -1; + if (this.negative === 0 && negative) return 1; + + this._strip(); + + var res; + if (this.length > 1) { + res = 1; + } else { + if (negative) { + num = -num; + } + + assert(num <= 0x3ffffff, 'Number is too big'); + + var w = this.words[0] | 0; + res = w === num ? 0 : w < num ? -1 : 1; + } + if (this.negative !== 0) return -res | 0; + return res; + }; + + // Compare two numbers and return: + // 1 - if `this` > `num` + // 0 - if `this` == `num` + // -1 - if `this` < `num` + BN.prototype.cmp = function cmp (num) { + if (this.negative !== 0 && num.negative === 0) return -1; + if (this.negative === 0 && num.negative !== 0) return 1; + + var res = this.ucmp(num); + if (this.negative !== 0) return -res | 0; + return res; + }; + + // Unsigned comparison + BN.prototype.ucmp = function ucmp (num) { + // At this point both numbers have the same sign + if (this.length > num.length) return 1; + if (this.length < num.length) return -1; + + var res = 0; + for (var i = this.length - 1; i >= 0; i--) { + var a = this.words[i] | 0; + var b = num.words[i] | 0; + + if (a === b) continue; + if (a < b) { + res = -1; + } else if (a > b) { + res = 1; + } + break; + } + return res; + }; + + BN.prototype.gtn = function gtn (num) { + return this.cmpn(num) === 1; + }; + + BN.prototype.gt = function gt (num) { + return this.cmp(num) === 1; + }; + + BN.prototype.gten = function gten (num) { + return this.cmpn(num) >= 0; + }; + + BN.prototype.gte = function gte (num) { + return this.cmp(num) >= 0; + }; + + BN.prototype.ltn = function ltn (num) { + return this.cmpn(num) === -1; + }; + + BN.prototype.lt = function lt (num) { + return this.cmp(num) === -1; + }; + + BN.prototype.lten = function lten (num) { + return this.cmpn(num) <= 0; + }; + + BN.prototype.lte = function lte (num) { + return this.cmp(num) <= 0; + }; + + BN.prototype.eqn = function eqn (num) { + return this.cmpn(num) === 0; + }; + + BN.prototype.eq = function eq (num) { + return this.cmp(num) === 0; + }; + + // + // A reduce context, could be using montgomery or something better, depending + // on the `m` itself. + // + BN.red = function red (num) { + return new Red(num); + }; + + BN.prototype.toRed = function toRed (ctx) { + assert(!this.red, 'Already a number in reduction context'); + assert(this.negative === 0, 'red works only with positives'); + return ctx.convertTo(this)._forceRed(ctx); + }; + + BN.prototype.fromRed = function fromRed () { + assert(this.red, 'fromRed works only with numbers in reduction context'); + return this.red.convertFrom(this); + }; + + BN.prototype._forceRed = function _forceRed (ctx) { + this.red = ctx; + return this; + }; + + BN.prototype.forceRed = function forceRed (ctx) { + assert(!this.red, 'Already a number in reduction context'); + return this._forceRed(ctx); + }; + + BN.prototype.redAdd = function redAdd (num) { + assert(this.red, 'redAdd works only with red numbers'); + return this.red.add(this, num); + }; + + BN.prototype.redIAdd = function redIAdd (num) { + assert(this.red, 'redIAdd works only with red numbers'); + return this.red.iadd(this, num); + }; + + BN.prototype.redSub = function redSub (num) { + assert(this.red, 'redSub works only with red numbers'); + return this.red.sub(this, num); + }; + + BN.prototype.redISub = function redISub (num) { + assert(this.red, 'redISub works only with red numbers'); + return this.red.isub(this, num); + }; + + BN.prototype.redShl = function redShl (num) { + assert(this.red, 'redShl works only with red numbers'); + return this.red.shl(this, num); + }; + + BN.prototype.redMul = function redMul (num) { + assert(this.red, 'redMul works only with red numbers'); + this.red._verify2(this, num); + return this.red.mul(this, num); + }; + + BN.prototype.redIMul = function redIMul (num) { + assert(this.red, 'redMul works only with red numbers'); + this.red._verify2(this, num); + return this.red.imul(this, num); + }; + + BN.prototype.redSqr = function redSqr () { + assert(this.red, 'redSqr works only with red numbers'); + this.red._verify1(this); + return this.red.sqr(this); + }; + + BN.prototype.redISqr = function redISqr () { + assert(this.red, 'redISqr works only with red numbers'); + this.red._verify1(this); + return this.red.isqr(this); + }; + + // Square root over p + BN.prototype.redSqrt = function redSqrt () { + assert(this.red, 'redSqrt works only with red numbers'); + this.red._verify1(this); + return this.red.sqrt(this); + }; + + BN.prototype.redInvm = function redInvm () { + assert(this.red, 'redInvm works only with red numbers'); + this.red._verify1(this); + return this.red.invm(this); + }; + + // Return negative clone of `this` % `red modulo` + BN.prototype.redNeg = function redNeg () { + assert(this.red, 'redNeg works only with red numbers'); + this.red._verify1(this); + return this.red.neg(this); + }; + + BN.prototype.redPow = function redPow (num) { + assert(this.red && !num.red, 'redPow(normalNum)'); + this.red._verify1(this); + return this.red.pow(this, num); + }; + + // Prime numbers with efficient reduction + var primes = { + k256: null, + p224: null, + p192: null, + p25519: null + }; + + // Pseudo-Mersenne prime + function MPrime (name, p) { + // P = 2 ^ N - K + this.name = name; + this.p = new BN(p, 16); + this.n = this.p.bitLength(); + this.k = new BN(1).iushln(this.n).isub(this.p); + + this.tmp = this._tmp(); + } + + MPrime.prototype._tmp = function _tmp () { + var tmp = new BN(null); + tmp.words = new Array(Math.ceil(this.n / 13)); + return tmp; + }; + + MPrime.prototype.ireduce = function ireduce (num) { + // Assumes that `num` is less than `P^2` + // num = HI * (2 ^ N - K) + HI * K + LO = HI * K + LO (mod P) + var r = num; + var rlen; + + do { + this.split(r, this.tmp); + r = this.imulK(r); + r = r.iadd(this.tmp); + rlen = r.bitLength(); + } while (rlen > this.n); + + var cmp = rlen < this.n ? -1 : r.ucmp(this.p); + if (cmp === 0) { + r.words[0] = 0; + r.length = 1; + } else if (cmp > 0) { + r.isub(this.p); + } else { + if (r.strip !== undefined) { + // r is a BN v4 instance + r.strip(); + } else { + // r is a BN v5 instance + r._strip(); + } + } + + return r; + }; + + MPrime.prototype.split = function split (input, out) { + input.iushrn(this.n, 0, out); + }; + + MPrime.prototype.imulK = function imulK (num) { + return num.imul(this.k); + }; + + function K256 () { + MPrime.call( + this, + 'k256', + 'ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff fffffffe fffffc2f'); + } + inherits(K256, MPrime); + + K256.prototype.split = function split (input, output) { + // 256 = 9 * 26 + 22 + var mask = 0x3fffff; + + var outLen = Math.min(input.length, 9); + for (var i = 0; i < outLen; i++) { + output.words[i] = input.words[i]; + } + output.length = outLen; + + if (input.length <= 9) { + input.words[0] = 0; + input.length = 1; + return; + } + + // Shift by 9 limbs + var prev = input.words[9]; + output.words[output.length++] = prev & mask; + + for (i = 10; i < input.length; i++) { + var next = input.words[i] | 0; + input.words[i - 10] = ((next & mask) << 4) | (prev >>> 22); + prev = next; + } + prev >>>= 22; + input.words[i - 10] = prev; + if (prev === 0 && input.length > 10) { + input.length -= 10; + } else { + input.length -= 9; + } + }; + + K256.prototype.imulK = function imulK (num) { + // K = 0x1000003d1 = [ 0x40, 0x3d1 ] + num.words[num.length] = 0; + num.words[num.length + 1] = 0; + num.length += 2; + + // bounded at: 0x40 * 0x3ffffff + 0x3d0 = 0x100000390 + var lo = 0; + for (var i = 0; i < num.length; i++) { + var w = num.words[i] | 0; + lo += w * 0x3d1; + num.words[i] = lo & 0x3ffffff; + lo = w * 0x40 + ((lo / 0x4000000) | 0); + } + + // Fast length reduction + if (num.words[num.length - 1] === 0) { + num.length--; + if (num.words[num.length - 1] === 0) { + num.length--; + } + } + return num; + }; + + function P224 () { + MPrime.call( + this, + 'p224', + 'ffffffff ffffffff ffffffff ffffffff 00000000 00000000 00000001'); + } + inherits(P224, MPrime); + + function P192 () { + MPrime.call( + this, + 'p192', + 'ffffffff ffffffff ffffffff fffffffe ffffffff ffffffff'); + } + inherits(P192, MPrime); + + function P25519 () { + // 2 ^ 255 - 19 + MPrime.call( + this, + '25519', + '7fffffffffffffff ffffffffffffffff ffffffffffffffff ffffffffffffffed'); + } + inherits(P25519, MPrime); + + P25519.prototype.imulK = function imulK (num) { + // K = 0x13 + var carry = 0; + for (var i = 0; i < num.length; i++) { + var hi = (num.words[i] | 0) * 0x13 + carry; + var lo = hi & 0x3ffffff; + hi >>>= 26; + + num.words[i] = lo; + carry = hi; + } + if (carry !== 0) { + num.words[num.length++] = carry; + } + return num; + }; + + // Exported mostly for testing purposes, use plain name instead + BN._prime = function prime (name) { + // Cached version of prime + if (primes[name]) return primes[name]; + + var prime; + if (name === 'k256') { + prime = new K256(); + } else if (name === 'p224') { + prime = new P224(); + } else if (name === 'p192') { + prime = new P192(); + } else if (name === 'p25519') { + prime = new P25519(); + } else { + throw new Error('Unknown prime ' + name); + } + primes[name] = prime; + + return prime; + }; + + // + // Base reduction engine + // + function Red (m) { + if (typeof m === 'string') { + var prime = BN._prime(m); + this.m = prime.p; + this.prime = prime; + } else { + assert(m.gtn(1), 'modulus must be greater than 1'); + this.m = m; + this.prime = null; + } + } + + Red.prototype._verify1 = function _verify1 (a) { + assert(a.negative === 0, 'red works only with positives'); + assert(a.red, 'red works only with red numbers'); + }; + + Red.prototype._verify2 = function _verify2 (a, b) { + assert((a.negative | b.negative) === 0, 'red works only with positives'); + assert(a.red && a.red === b.red, + 'red works only with red numbers'); + }; + + Red.prototype.imod = function imod (a) { + if (this.prime) return this.prime.ireduce(a)._forceRed(this); + + move(a, a.umod(this.m)._forceRed(this)); + return a; + }; + + Red.prototype.neg = function neg (a) { + if (a.isZero()) { + return a.clone(); + } + + return this.m.sub(a)._forceRed(this); + }; + + Red.prototype.add = function add (a, b) { + this._verify2(a, b); + + var res = a.add(b); + if (res.cmp(this.m) >= 0) { + res.isub(this.m); + } + return res._forceRed(this); + }; + + Red.prototype.iadd = function iadd (a, b) { + this._verify2(a, b); + + var res = a.iadd(b); + if (res.cmp(this.m) >= 0) { + res.isub(this.m); + } + return res; + }; + + Red.prototype.sub = function sub (a, b) { + this._verify2(a, b); + + var res = a.sub(b); + if (res.cmpn(0) < 0) { + res.iadd(this.m); + } + return res._forceRed(this); + }; + + Red.prototype.isub = function isub (a, b) { + this._verify2(a, b); + + var res = a.isub(b); + if (res.cmpn(0) < 0) { + res.iadd(this.m); + } + return res; + }; + + Red.prototype.shl = function shl (a, num) { + this._verify1(a); + return this.imod(a.ushln(num)); + }; + + Red.prototype.imul = function imul (a, b) { + this._verify2(a, b); + return this.imod(a.imul(b)); + }; + + Red.prototype.mul = function mul (a, b) { + this._verify2(a, b); + return this.imod(a.mul(b)); + }; + + Red.prototype.isqr = function isqr (a) { + return this.imul(a, a.clone()); + }; + + Red.prototype.sqr = function sqr (a) { + return this.mul(a, a); + }; + + Red.prototype.sqrt = function sqrt (a) { + if (a.isZero()) return a.clone(); + + var mod3 = this.m.andln(3); + assert(mod3 % 2 === 1); + + // Fast case + if (mod3 === 3) { + var pow = this.m.add(new BN(1)).iushrn(2); + return this.pow(a, pow); + } + + // Tonelli-Shanks algorithm (Totally unoptimized and slow) + // + // Find Q and S, that Q * 2 ^ S = (P - 1) + var q = this.m.subn(1); + var s = 0; + while (!q.isZero() && q.andln(1) === 0) { + s++; + q.iushrn(1); + } + assert(!q.isZero()); + + var one = new BN(1).toRed(this); + var nOne = one.redNeg(); + + // Find quadratic non-residue + // NOTE: Max is such because of generalized Riemann hypothesis. + var lpow = this.m.subn(1).iushrn(1); + var z = this.m.bitLength(); + z = new BN(2 * z * z).toRed(this); + + while (this.pow(z, lpow).cmp(nOne) !== 0) { + z.redIAdd(nOne); + } + + var c = this.pow(z, q); + var r = this.pow(a, q.addn(1).iushrn(1)); + var t = this.pow(a, q); + var m = s; + while (t.cmp(one) !== 0) { + var tmp = t; + for (var i = 0; tmp.cmp(one) !== 0; i++) { + tmp = tmp.redSqr(); + } + assert(i < m); + var b = this.pow(c, new BN(1).iushln(m - i - 1)); + + r = r.redMul(b); + c = b.redSqr(); + t = t.redMul(c); + m = i; + } + + return r; + }; + + Red.prototype.invm = function invm (a) { + var inv = a._invmp(this.m); + if (inv.negative !== 0) { + inv.negative = 0; + return this.imod(inv).redNeg(); + } else { + return this.imod(inv); + } + }; + + Red.prototype.pow = function pow (a, num) { + if (num.isZero()) return new BN(1).toRed(this); + if (num.cmpn(1) === 0) return a.clone(); + + var windowSize = 4; + var wnd = new Array(1 << windowSize); + wnd[0] = new BN(1).toRed(this); + wnd[1] = a; + for (var i = 2; i < wnd.length; i++) { + wnd[i] = this.mul(wnd[i - 1], a); + } + + var res = wnd[0]; + var current = 0; + var currentLen = 0; + var start = num.bitLength() % 26; + if (start === 0) { + start = 26; + } + + for (i = num.length - 1; i >= 0; i--) { + var word = num.words[i]; + for (var j = start - 1; j >= 0; j--) { + var bit = (word >> j) & 1; + if (res !== wnd[0]) { + res = this.sqr(res); + } + + if (bit === 0 && current === 0) { + currentLen = 0; + continue; + } + + current <<= 1; + current |= bit; + currentLen++; + if (currentLen !== windowSize && (i !== 0 || j !== 0)) continue; + + res = this.mul(res, wnd[current]); + currentLen = 0; + current = 0; + } + start = 26; + } + + return res; + }; + + Red.prototype.convertTo = function convertTo (num) { + var r = num.umod(this.m); + + return r === num ? r.clone() : r; + }; + + Red.prototype.convertFrom = function convertFrom (num) { + var res = num.clone(); + res.red = null; + return res; + }; + + // + // Montgomery method engine + // + + BN.mont = function mont (num) { + return new Mont(num); + }; + + function Mont (m) { + Red.call(this, m); + + this.shift = this.m.bitLength(); + if (this.shift % 26 !== 0) { + this.shift += 26 - (this.shift % 26); + } + + this.r = new BN(1).iushln(this.shift); + this.r2 = this.imod(this.r.sqr()); + this.rinv = this.r._invmp(this.m); + + this.minv = this.rinv.mul(this.r).isubn(1).div(this.m); + this.minv = this.minv.umod(this.r); + this.minv = this.r.sub(this.minv); + } + inherits(Mont, Red); + + Mont.prototype.convertTo = function convertTo (num) { + return this.imod(num.ushln(this.shift)); + }; + + Mont.prototype.convertFrom = function convertFrom (num) { + var r = this.imod(num.mul(this.rinv)); + r.red = null; + return r; + }; + + Mont.prototype.imul = function imul (a, b) { + if (a.isZero() || b.isZero()) { + a.words[0] = 0; + a.length = 1; + return a; + } + + var t = a.imul(b); + var c = t.maskn(this.shift).mul(this.minv).imaskn(this.shift).mul(this.m); + var u = t.isub(c).iushrn(this.shift); + var res = u; + + if (u.cmp(this.m) >= 0) { + res = u.isub(this.m); + } else if (u.cmpn(0) < 0) { + res = u.iadd(this.m); + } + + return res._forceRed(this); + }; + + Mont.prototype.mul = function mul (a, b) { + if (a.isZero() || b.isZero()) return new BN(0)._forceRed(this); + + var t = a.mul(b); + var c = t.maskn(this.shift).mul(this.minv).imaskn(this.shift).mul(this.m); + var u = t.isub(c).iushrn(this.shift); + var res = u; + if (u.cmp(this.m) >= 0) { + res = u.isub(this.m); + } else if (u.cmpn(0) < 0) { + res = u.iadd(this.m); + } + + return res._forceRed(this); + }; + + Mont.prototype.invm = function invm (a) { + // (AR)^-1 * R^2 = (A^-1 * R^-1) * R^2 = A^-1 * R + var res = this.imod(a._invmp(this.m).mul(this.r2)); + return res._forceRed(this); + }; + })(module, commonjsGlobal); +} (bn)); + +var BN$1 = bn.exports; + +const version$b = "logger/5.7.0"; + +let _permanentCensorErrors = false; +let _censorErrors = false; +const LogLevels = { debug: 1, "default": 2, info: 2, warning: 3, error: 4, off: 5 }; +let _logLevel = LogLevels["default"]; +let _globalLogger = null; +function _checkNormalize() { + try { + const missing = []; + // Make sure all forms of normalization are supported + ["NFD", "NFC", "NFKD", "NFKC"].forEach((form) => { + try { + if ("test".normalize(form) !== "test") { + throw new Error("bad normalize"); + } + ; + } + catch (error) { + missing.push(form); + } + }); + if (missing.length) { + throw new Error("missing " + missing.join(", ")); + } + if (String.fromCharCode(0xe9).normalize("NFD") !== String.fromCharCode(0x65, 0x0301)) { + throw new Error("broken implementation"); + } + } + catch (error) { + return error.message; + } + return null; +} +const _normalizeError = _checkNormalize(); +var LogLevel; +(function (LogLevel) { + LogLevel["DEBUG"] = "DEBUG"; + LogLevel["INFO"] = "INFO"; + LogLevel["WARNING"] = "WARNING"; + LogLevel["ERROR"] = "ERROR"; + LogLevel["OFF"] = "OFF"; +})(LogLevel || (LogLevel = {})); +var ErrorCode; +(function (ErrorCode) { + /////////////////// + // Generic Errors + // Unknown Error + ErrorCode["UNKNOWN_ERROR"] = "UNKNOWN_ERROR"; + // Not Implemented + ErrorCode["NOT_IMPLEMENTED"] = "NOT_IMPLEMENTED"; + // Unsupported Operation + // - operation + ErrorCode["UNSUPPORTED_OPERATION"] = "UNSUPPORTED_OPERATION"; + // Network Error (i.e. Ethereum Network, such as an invalid chain ID) + // - event ("noNetwork" is not re-thrown in provider.ready; otherwise thrown) + ErrorCode["NETWORK_ERROR"] = "NETWORK_ERROR"; + // Some sort of bad response from the server + ErrorCode["SERVER_ERROR"] = "SERVER_ERROR"; + // Timeout + ErrorCode["TIMEOUT"] = "TIMEOUT"; + /////////////////// + // Operational Errors + // Buffer Overrun + ErrorCode["BUFFER_OVERRUN"] = "BUFFER_OVERRUN"; + // Numeric Fault + // - operation: the operation being executed + // - fault: the reason this faulted + ErrorCode["NUMERIC_FAULT"] = "NUMERIC_FAULT"; + /////////////////// + // Argument Errors + // Missing new operator to an object + // - name: The name of the class + ErrorCode["MISSING_NEW"] = "MISSING_NEW"; + // Invalid argument (e.g. value is incompatible with type) to a function: + // - argument: The argument name that was invalid + // - value: The value of the argument + ErrorCode["INVALID_ARGUMENT"] = "INVALID_ARGUMENT"; + // Missing argument to a function: + // - count: The number of arguments received + // - expectedCount: The number of arguments expected + ErrorCode["MISSING_ARGUMENT"] = "MISSING_ARGUMENT"; + // Too many arguments + // - count: The number of arguments received + // - expectedCount: The number of arguments expected + ErrorCode["UNEXPECTED_ARGUMENT"] = "UNEXPECTED_ARGUMENT"; + /////////////////// + // Blockchain Errors + // Call exception + // - transaction: the transaction + // - address?: the contract address + // - args?: The arguments passed into the function + // - method?: The Solidity method signature + // - errorSignature?: The EIP848 error signature + // - errorArgs?: The EIP848 error parameters + // - reason: The reason (only for EIP848 "Error(string)") + ErrorCode["CALL_EXCEPTION"] = "CALL_EXCEPTION"; + // Insufficient funds (< value + gasLimit * gasPrice) + // - transaction: the transaction attempted + ErrorCode["INSUFFICIENT_FUNDS"] = "INSUFFICIENT_FUNDS"; + // Nonce has already been used + // - transaction: the transaction attempted + ErrorCode["NONCE_EXPIRED"] = "NONCE_EXPIRED"; + // The replacement fee for the transaction is too low + // - transaction: the transaction attempted + ErrorCode["REPLACEMENT_UNDERPRICED"] = "REPLACEMENT_UNDERPRICED"; + // The gas limit could not be estimated + // - transaction: the transaction passed to estimateGas + ErrorCode["UNPREDICTABLE_GAS_LIMIT"] = "UNPREDICTABLE_GAS_LIMIT"; + // The transaction was replaced by one with a higher gas price + // - reason: "cancelled", "replaced" or "repriced" + // - cancelled: true if reason == "cancelled" or reason == "replaced") + // - hash: original transaction hash + // - replacement: the full TransactionsResponse for the replacement + // - receipt: the receipt of the replacement + ErrorCode["TRANSACTION_REPLACED"] = "TRANSACTION_REPLACED"; + /////////////////// + // Interaction Errors + // The user rejected the action, such as signing a message or sending + // a transaction + ErrorCode["ACTION_REJECTED"] = "ACTION_REJECTED"; +})(ErrorCode || (ErrorCode = {})); +const HEX = "0123456789abcdef"; +class Logger { + constructor(version) { + Object.defineProperty(this, "version", { + enumerable: true, + value: version, + writable: false + }); + } + _log(logLevel, args) { + const level = logLevel.toLowerCase(); + if (LogLevels[level] == null) { + this.throwArgumentError("invalid log level name", "logLevel", logLevel); + } + if (_logLevel > LogLevels[level]) { + return; + } + console.log.apply(console, args); + } + debug(...args) { + this._log(Logger.levels.DEBUG, args); + } + info(...args) { + this._log(Logger.levels.INFO, args); + } + warn(...args) { + this._log(Logger.levels.WARNING, args); + } + makeError(message, code, params) { + // Errors are being censored + if (_censorErrors) { + return this.makeError("censored error", code, {}); + } + if (!code) { + code = Logger.errors.UNKNOWN_ERROR; + } + if (!params) { + params = {}; + } + const messageDetails = []; + Object.keys(params).forEach((key) => { + const value = params[key]; + try { + if (value instanceof Uint8Array) { + let hex = ""; + for (let i = 0; i < value.length; i++) { + hex += HEX[value[i] >> 4]; + hex += HEX[value[i] & 0x0f]; + } + messageDetails.push(key + "=Uint8Array(0x" + hex + ")"); + } + else { + messageDetails.push(key + "=" + JSON.stringify(value)); + } + } + catch (error) { + messageDetails.push(key + "=" + JSON.stringify(params[key].toString())); + } + }); + messageDetails.push(`code=${code}`); + messageDetails.push(`version=${this.version}`); + const reason = message; + let url = ""; + switch (code) { + case ErrorCode.NUMERIC_FAULT: { + url = "NUMERIC_FAULT"; + const fault = message; + switch (fault) { + case "overflow": + case "underflow": + case "division-by-zero": + url += "-" + fault; + break; + case "negative-power": + case "negative-width": + url += "-unsupported"; + break; + case "unbound-bitwise-result": + url += "-unbound-result"; + break; + } + break; + } + case ErrorCode.CALL_EXCEPTION: + case ErrorCode.INSUFFICIENT_FUNDS: + case ErrorCode.MISSING_NEW: + case ErrorCode.NONCE_EXPIRED: + case ErrorCode.REPLACEMENT_UNDERPRICED: + case ErrorCode.TRANSACTION_REPLACED: + case ErrorCode.UNPREDICTABLE_GAS_LIMIT: + url = code; + break; + } + if (url) { + message += " [ See: https:/\/links.ethers.org/v5-errors-" + url + " ]"; + } + if (messageDetails.length) { + message += " (" + messageDetails.join(", ") + ")"; + } + // @TODO: Any?? + const error = new Error(message); + error.reason = reason; + error.code = code; + Object.keys(params).forEach(function (key) { + error[key] = params[key]; + }); + return error; + } + throwError(message, code, params) { + throw this.makeError(message, code, params); + } + throwArgumentError(message, name, value) { + return this.throwError(message, Logger.errors.INVALID_ARGUMENT, { + argument: name, + value: value + }); + } + assert(condition, message, code, params) { + if (!!condition) { + return; + } + this.throwError(message, code, params); + } + assertArgument(condition, message, name, value) { + if (!!condition) { + return; + } + this.throwArgumentError(message, name, value); + } + checkNormalize(message) { + if (_normalizeError) { + this.throwError("platform missing String.prototype.normalize", Logger.errors.UNSUPPORTED_OPERATION, { + operation: "String.prototype.normalize", form: _normalizeError + }); + } + } + checkSafeUint53(value, message) { + if (typeof (value) !== "number") { + return; + } + if (message == null) { + message = "value not safe"; + } + if (value < 0 || value >= 0x1fffffffffffff) { + this.throwError(message, Logger.errors.NUMERIC_FAULT, { + operation: "checkSafeInteger", + fault: "out-of-safe-range", + value: value + }); + } + if (value % 1) { + this.throwError(message, Logger.errors.NUMERIC_FAULT, { + operation: "checkSafeInteger", + fault: "non-integer", + value: value + }); + } + } + checkArgumentCount(count, expectedCount, message) { + if (message) { + message = ": " + message; + } + else { + message = ""; + } + if (count < expectedCount) { + this.throwError("missing argument" + message, Logger.errors.MISSING_ARGUMENT, { + count: count, + expectedCount: expectedCount + }); + } + if (count > expectedCount) { + this.throwError("too many arguments" + message, Logger.errors.UNEXPECTED_ARGUMENT, { + count: count, + expectedCount: expectedCount + }); + } + } + checkNew(target, kind) { + if (target === Object || target == null) { + this.throwError("missing new", Logger.errors.MISSING_NEW, { name: kind.name }); + } + } + checkAbstract(target, kind) { + if (target === kind) { + this.throwError("cannot instantiate abstract class " + JSON.stringify(kind.name) + " directly; use a sub-class", Logger.errors.UNSUPPORTED_OPERATION, { name: target.name, operation: "new" }); + } + else if (target === Object || target == null) { + this.throwError("missing new", Logger.errors.MISSING_NEW, { name: kind.name }); + } + } + static globalLogger() { + if (!_globalLogger) { + _globalLogger = new Logger(version$b); + } + return _globalLogger; + } + static setCensorship(censorship, permanent) { + if (!censorship && permanent) { + this.globalLogger().throwError("cannot permanently disable censorship", Logger.errors.UNSUPPORTED_OPERATION, { + operation: "setCensorship" + }); + } + if (_permanentCensorErrors) { + if (!censorship) { + return; + } + this.globalLogger().throwError("error censorship permanent", Logger.errors.UNSUPPORTED_OPERATION, { + operation: "setCensorship" + }); + } + _censorErrors = !!censorship; + _permanentCensorErrors = !!permanent; + } + static setLogLevel(logLevel) { + const level = LogLevels[logLevel.toLowerCase()]; + if (level == null) { + Logger.globalLogger().warn("invalid log level - " + logLevel); + return; + } + _logLevel = level; + } + static from(version) { + return new Logger(version); + } +} +Logger.errors = ErrorCode; +Logger.levels = LogLevel; + +const version$a = "bytes/5.7.0"; + +const logger$e = new Logger(version$a); +/////////////////////////////// +function isHexable(value) { + return !!(value.toHexString); +} +function addSlice(array) { + if (array.slice) { + return array; + } + array.slice = function () { + const args = Array.prototype.slice.call(arguments); + return addSlice(new Uint8Array(Array.prototype.slice.apply(array, args))); + }; + return array; +} +function isBytesLike(value) { + return ((isHexString(value) && !(value.length % 2)) || isBytes(value)); +} +function isInteger(value) { + return (typeof (value) === "number" && value == value && (value % 1) === 0); +} +function isBytes(value) { + if (value == null) { + return false; + } + if (value.constructor === Uint8Array) { + return true; + } + if (typeof (value) === "string") { + return false; + } + if (!isInteger(value.length) || value.length < 0) { + return false; + } + for (let i = 0; i < value.length; i++) { + const v = value[i]; + if (!isInteger(v) || v < 0 || v >= 256) { + return false; + } + } + return true; +} +function arrayify(value, options) { + if (!options) { + options = {}; + } + if (typeof (value) === "number") { + logger$e.checkSafeUint53(value, "invalid arrayify value"); + const result = []; + while (value) { + result.unshift(value & 0xff); + value = parseInt(String(value / 256)); + } + if (result.length === 0) { + result.push(0); + } + return addSlice(new Uint8Array(result)); + } + if (options.allowMissingPrefix && typeof (value) === "string" && value.substring(0, 2) !== "0x") { + value = "0x" + value; + } + if (isHexable(value)) { + value = value.toHexString(); + } + if (isHexString(value)) { + let hex = value.substring(2); + if (hex.length % 2) { + if (options.hexPad === "left") { + hex = "0" + hex; + } + else if (options.hexPad === "right") { + hex += "0"; + } + else { + logger$e.throwArgumentError("hex data is odd-length", "value", value); + } + } + const result = []; + for (let i = 0; i < hex.length; i += 2) { + result.push(parseInt(hex.substring(i, i + 2), 16)); + } + return addSlice(new Uint8Array(result)); + } + if (isBytes(value)) { + return addSlice(new Uint8Array(value)); + } + return logger$e.throwArgumentError("invalid arrayify value", "value", value); +} +function concat(items) { + const objects = items.map(item => arrayify(item)); + const length = objects.reduce((accum, item) => (accum + item.length), 0); + const result = new Uint8Array(length); + objects.reduce((offset, object) => { + result.set(object, offset); + return offset + object.length; + }, 0); + return addSlice(result); +} +function stripZeros(value) { + let result = arrayify(value); + if (result.length === 0) { + return result; + } + // Find the first non-zero entry + let start = 0; + while (start < result.length && result[start] === 0) { + start++; + } + // If we started with zeros, strip them + if (start) { + result = result.slice(start); + } + return result; +} +function isHexString(value, length) { + if (typeof (value) !== "string" || !value.match(/^0x[0-9A-Fa-f]*$/)) { + return false; + } + if (length && value.length !== 2 + 2 * length) { + return false; + } + return true; +} +const HexCharacters = "0123456789abcdef"; +function hexlify(value, options) { + if (!options) { + options = {}; + } + if (typeof (value) === "number") { + logger$e.checkSafeUint53(value, "invalid hexlify value"); + let hex = ""; + while (value) { + hex = HexCharacters[value & 0xf] + hex; + value = Math.floor(value / 16); + } + if (hex.length) { + if (hex.length % 2) { + hex = "0" + hex; + } + return "0x" + hex; + } + return "0x00"; + } + if (typeof (value) === "bigint") { + value = value.toString(16); + if (value.length % 2) { + return ("0x0" + value); + } + return "0x" + value; + } + if (options.allowMissingPrefix && typeof (value) === "string" && value.substring(0, 2) !== "0x") { + value = "0x" + value; + } + if (isHexable(value)) { + return value.toHexString(); + } + if (isHexString(value)) { + if (value.length % 2) { + if (options.hexPad === "left") { + value = "0x0" + value.substring(2); + } + else if (options.hexPad === "right") { + value += "0"; + } + else { + logger$e.throwArgumentError("hex data is odd-length", "value", value); + } + } + return value.toLowerCase(); + } + if (isBytes(value)) { + let result = "0x"; + for (let i = 0; i < value.length; i++) { + let v = value[i]; + result += HexCharacters[(v & 0xf0) >> 4] + HexCharacters[v & 0x0f]; + } + return result; + } + return logger$e.throwArgumentError("invalid hexlify value", "value", value); +} +/* +function unoddify(value: BytesLike | Hexable | number): BytesLike | Hexable | number { + if (typeof(value) === "string" && value.length % 2 && value.substring(0, 2) === "0x") { + return "0x0" + value.substring(2); + } + return value; +} +*/ +function hexDataLength(data) { + if (typeof (data) !== "string") { + data = hexlify(data); + } + else if (!isHexString(data) || (data.length % 2)) { + return null; + } + return (data.length - 2) / 2; +} +function hexDataSlice(data, offset, endOffset) { + if (typeof (data) !== "string") { + data = hexlify(data); + } + else if (!isHexString(data) || (data.length % 2)) { + logger$e.throwArgumentError("invalid hexData", "value", data); + } + offset = 2 + 2 * offset; + if (endOffset != null) { + return "0x" + data.substring(offset, 2 + 2 * endOffset); + } + return "0x" + data.substring(offset); +} +function hexConcat(items) { + let result = "0x"; + items.forEach((item) => { + result += hexlify(item).substring(2); + }); + return result; +} +function hexZeroPad(value, length) { + if (typeof (value) !== "string") { + value = hexlify(value); + } + else if (!isHexString(value)) { + logger$e.throwArgumentError("invalid hex string", "value", value); + } + if (value.length > 2 * length + 2) { + logger$e.throwArgumentError("value out of range", "value", arguments[1]); + } + while (value.length < 2 * length + 2) { + value = "0x0" + value.substring(2); + } + return value; +} + +const version$9 = "bignumber/5.7.0"; + +var BN = BN$1.BN; +const logger$d = new Logger(version$9); +const _constructorGuard$1 = {}; +const MAX_SAFE = 0x1fffffffffffff; +// Only warn about passing 10 into radix once +let _warnedToStringRadix = false; +class BigNumber { + constructor(constructorGuard, hex) { + if (constructorGuard !== _constructorGuard$1) { + logger$d.throwError("cannot call constructor directly; use BigNumber.from", Logger.errors.UNSUPPORTED_OPERATION, { + operation: "new (BigNumber)" + }); + } + this._hex = hex; + this._isBigNumber = true; + Object.freeze(this); + } + fromTwos(value) { + return toBigNumber(toBN(this).fromTwos(value)); + } + toTwos(value) { + return toBigNumber(toBN(this).toTwos(value)); + } + abs() { + if (this._hex[0] === "-") { + return BigNumber.from(this._hex.substring(1)); + } + return this; + } + add(other) { + return toBigNumber(toBN(this).add(toBN(other))); + } + sub(other) { + return toBigNumber(toBN(this).sub(toBN(other))); + } + div(other) { + const o = BigNumber.from(other); + if (o.isZero()) { + throwFault("division-by-zero", "div"); + } + return toBigNumber(toBN(this).div(toBN(other))); + } + mul(other) { + return toBigNumber(toBN(this).mul(toBN(other))); + } + mod(other) { + const value = toBN(other); + if (value.isNeg()) { + throwFault("division-by-zero", "mod"); + } + return toBigNumber(toBN(this).umod(value)); + } + pow(other) { + const value = toBN(other); + if (value.isNeg()) { + throwFault("negative-power", "pow"); + } + return toBigNumber(toBN(this).pow(value)); + } + and(other) { + const value = toBN(other); + if (this.isNegative() || value.isNeg()) { + throwFault("unbound-bitwise-result", "and"); + } + return toBigNumber(toBN(this).and(value)); + } + or(other) { + const value = toBN(other); + if (this.isNegative() || value.isNeg()) { + throwFault("unbound-bitwise-result", "or"); + } + return toBigNumber(toBN(this).or(value)); + } + xor(other) { + const value = toBN(other); + if (this.isNegative() || value.isNeg()) { + throwFault("unbound-bitwise-result", "xor"); + } + return toBigNumber(toBN(this).xor(value)); + } + mask(value) { + if (this.isNegative() || value < 0) { + throwFault("negative-width", "mask"); + } + return toBigNumber(toBN(this).maskn(value)); + } + shl(value) { + if (this.isNegative() || value < 0) { + throwFault("negative-width", "shl"); + } + return toBigNumber(toBN(this).shln(value)); + } + shr(value) { + if (this.isNegative() || value < 0) { + throwFault("negative-width", "shr"); + } + return toBigNumber(toBN(this).shrn(value)); + } + eq(other) { + return toBN(this).eq(toBN(other)); + } + lt(other) { + return toBN(this).lt(toBN(other)); + } + lte(other) { + return toBN(this).lte(toBN(other)); + } + gt(other) { + return toBN(this).gt(toBN(other)); + } + gte(other) { + return toBN(this).gte(toBN(other)); + } + isNegative() { + return (this._hex[0] === "-"); + } + isZero() { + return toBN(this).isZero(); + } + toNumber() { + try { + return toBN(this).toNumber(); + } + catch (error) { + throwFault("overflow", "toNumber", this.toString()); + } + return null; + } + toBigInt() { + try { + return BigInt(this.toString()); + } + catch (e) { } + return logger$d.throwError("this platform does not support BigInt", Logger.errors.UNSUPPORTED_OPERATION, { + value: this.toString() + }); + } + toString() { + // Lots of people expect this, which we do not support, so check (See: #889) + if (arguments.length > 0) { + if (arguments[0] === 10) { + if (!_warnedToStringRadix) { + _warnedToStringRadix = true; + logger$d.warn("BigNumber.toString does not accept any parameters; base-10 is assumed"); + } + } + else if (arguments[0] === 16) { + logger$d.throwError("BigNumber.toString does not accept any parameters; use bigNumber.toHexString()", Logger.errors.UNEXPECTED_ARGUMENT, {}); + } + else { + logger$d.throwError("BigNumber.toString does not accept parameters", Logger.errors.UNEXPECTED_ARGUMENT, {}); + } + } + return toBN(this).toString(10); + } + toHexString() { + return this._hex; + } + toJSON(key) { + return { type: "BigNumber", hex: this.toHexString() }; + } + static from(value) { + if (value instanceof BigNumber) { + return value; + } + if (typeof (value) === "string") { + if (value.match(/^-?0x[0-9a-f]+$/i)) { + return new BigNumber(_constructorGuard$1, toHex(value)); + } + if (value.match(/^-?[0-9]+$/)) { + return new BigNumber(_constructorGuard$1, toHex(new BN(value))); + } + return logger$d.throwArgumentError("invalid BigNumber string", "value", value); + } + if (typeof (value) === "number") { + if (value % 1) { + throwFault("underflow", "BigNumber.from", value); + } + if (value >= MAX_SAFE || value <= -MAX_SAFE) { + throwFault("overflow", "BigNumber.from", value); + } + return BigNumber.from(String(value)); + } + const anyValue = value; + if (typeof (anyValue) === "bigint") { + return BigNumber.from(anyValue.toString()); + } + if (isBytes(anyValue)) { + return BigNumber.from(hexlify(anyValue)); + } + if (anyValue) { + // Hexable interface (takes priority) + if (anyValue.toHexString) { + const hex = anyValue.toHexString(); + if (typeof (hex) === "string") { + return BigNumber.from(hex); + } + } + else { + // For now, handle legacy JSON-ified values (goes away in v6) + let hex = anyValue._hex; + // New-form JSON + if (hex == null && anyValue.type === "BigNumber") { + hex = anyValue.hex; + } + if (typeof (hex) === "string") { + if (isHexString(hex) || (hex[0] === "-" && isHexString(hex.substring(1)))) { + return BigNumber.from(hex); + } + } + } + } + return logger$d.throwArgumentError("invalid BigNumber value", "value", value); + } + static isBigNumber(value) { + return !!(value && value._isBigNumber); + } +} +// Normalize the hex string +function toHex(value) { + // For BN, call on the hex string + if (typeof (value) !== "string") { + return toHex(value.toString(16)); + } + // If negative, prepend the negative sign to the normalized positive value + if (value[0] === "-") { + // Strip off the negative sign + value = value.substring(1); + // Cannot have multiple negative signs (e.g. "--0x04") + if (value[0] === "-") { + logger$d.throwArgumentError("invalid hex", "value", value); + } + // Call toHex on the positive component + value = toHex(value); + // Do not allow "-0x00" + if (value === "0x00") { + return value; + } + // Negate the value + return "-" + value; + } + // Add a "0x" prefix if missing + if (value.substring(0, 2) !== "0x") { + value = "0x" + value; + } + // Normalize zero + if (value === "0x") { + return "0x00"; + } + // Make the string even length + if (value.length % 2) { + value = "0x0" + value.substring(2); + } + // Trim to smallest even-length string + while (value.length > 4 && value.substring(0, 4) === "0x00") { + value = "0x" + value.substring(4); + } + return value; +} +function toBigNumber(value) { + return BigNumber.from(toHex(value)); +} +function toBN(value) { + const hex = BigNumber.from(value).toHexString(); + if (hex[0] === "-") { + return (new BN("-" + hex.substring(3), 16)); + } + return new BN(hex.substring(2), 16); +} +function throwFault(fault, operation, value) { + const params = { fault: fault, operation: operation }; + if (value != null) { + params.value = value; + } + return logger$d.throwError(fault, Logger.errors.NUMERIC_FAULT, params); +} +// value should have no prefix +function _base36To16(value) { + return (new BN(value, 36)).toString(16); +} + +const version$8 = "properties/5.7.0"; + +var __awaiter$3 = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) { + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } + return new (P || (P = Promise))(function (resolve, reject) { + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } + step((generator = generator.apply(thisArg, _arguments || [])).next()); + }); +}; +const logger$c = new Logger(version$8); +function defineReadOnly(object, name, value) { + Object.defineProperty(object, name, { + enumerable: true, + value: value, + writable: false, + }); +} +// Crawl up the constructor chain to find a static method +function getStatic(ctor, key) { + for (let i = 0; i < 32; i++) { + if (ctor[key]) { + return ctor[key]; + } + if (!ctor.prototype || typeof (ctor.prototype) !== "object") { + break; + } + ctor = Object.getPrototypeOf(ctor.prototype).constructor; + } + return null; +} +function resolveProperties(object) { + return __awaiter$3(this, void 0, void 0, function* () { + const promises = Object.keys(object).map((key) => { + const value = object[key]; + return Promise.resolve(value).then((v) => ({ key: key, value: v })); + }); + const results = yield Promise.all(promises); + return results.reduce((accum, result) => { + accum[(result.key)] = result.value; + return accum; + }, {}); + }); +} +function shallowCopy(object) { + const result = {}; + for (const key in object) { + result[key] = object[key]; + } + return result; +} +const opaque = { bigint: true, boolean: true, "function": true, number: true, string: true }; +function _isFrozen(object) { + // Opaque objects are not mutable, so safe to copy by assignment + if (object === undefined || object === null || opaque[typeof (object)]) { + return true; + } + if (Array.isArray(object) || typeof (object) === "object") { + if (!Object.isFrozen(object)) { + return false; + } + const keys = Object.keys(object); + for (let i = 0; i < keys.length; i++) { + let value = null; + try { + value = object[keys[i]]; + } + catch (error) { + // If accessing a value triggers an error, it is a getter + // designed to do so (e.g. Result) and is therefore "frozen" + continue; + } + if (!_isFrozen(value)) { + return false; + } + } + return true; + } + return logger$c.throwArgumentError(`Cannot deepCopy ${typeof (object)}`, "object", object); +} +// Returns a new copy of object, such that no properties may be replaced. +// New properties may be added only to objects. +function _deepCopy(object) { + if (_isFrozen(object)) { + return object; + } + // Arrays are mutable, so we need to create a copy + if (Array.isArray(object)) { + return Object.freeze(object.map((item) => deepCopy(item))); + } + if (typeof (object) === "object") { + const result = {}; + for (const key in object) { + const value = object[key]; + if (value === undefined) { + continue; + } + defineReadOnly(result, key, deepCopy(value)); + } + return result; + } + return logger$c.throwArgumentError(`Cannot deepCopy ${typeof (object)}`, "object", object); +} +function deepCopy(object) { + return _deepCopy(object); +} +class Description { + constructor(info) { + for (const key in info) { + this[key] = deepCopy(info[key]); + } + } +} + +const version$7 = "abi/5.7.0"; + +const logger$b = new Logger(version$7); +const _constructorGuard = {}; +let ModifiersBytes = { calldata: true, memory: true, storage: true }; +let ModifiersNest = { calldata: true, memory: true }; +function checkModifier(type, name) { + if (type === "bytes" || type === "string") { + if (ModifiersBytes[name]) { + return true; + } + } + else if (type === "address") { + if (name === "payable") { + return true; + } + } + else if (type.indexOf("[") >= 0 || type === "tuple") { + if (ModifiersNest[name]) { + return true; + } + } + if (ModifiersBytes[name] || name === "payable") { + logger$b.throwArgumentError("invalid modifier", "name", name); + } + return false; +} +// @TODO: Make sure that children of an indexed tuple are marked with a null indexed +function parseParamType(param, allowIndexed) { + let originalParam = param; + function throwError(i) { + logger$b.throwArgumentError(`unexpected character at position ${i}`, "param", param); + } + param = param.replace(/\s/g, " "); + function newNode(parent) { + let node = { type: "", name: "", parent: parent, state: { allowType: true } }; + if (allowIndexed) { + node.indexed = false; + } + return node; + } + let parent = { type: "", name: "", state: { allowType: true } }; + let node = parent; + for (let i = 0; i < param.length; i++) { + let c = param[i]; + switch (c) { + case "(": + if (node.state.allowType && node.type === "") { + node.type = "tuple"; + } + else if (!node.state.allowParams) { + throwError(i); + } + node.state.allowType = false; + node.type = verifyType(node.type); + node.components = [newNode(node)]; + node = node.components[0]; + break; + case ")": + delete node.state; + if (node.name === "indexed") { + if (!allowIndexed) { + throwError(i); + } + node.indexed = true; + node.name = ""; + } + if (checkModifier(node.type, node.name)) { + node.name = ""; + } + node.type = verifyType(node.type); + let child = node; + node = node.parent; + if (!node) { + throwError(i); + } + delete child.parent; + node.state.allowParams = false; + node.state.allowName = true; + node.state.allowArray = true; + break; + case ",": + delete node.state; + if (node.name === "indexed") { + if (!allowIndexed) { + throwError(i); + } + node.indexed = true; + node.name = ""; + } + if (checkModifier(node.type, node.name)) { + node.name = ""; + } + node.type = verifyType(node.type); + let sibling = newNode(node.parent); + //{ type: "", name: "", parent: node.parent, state: { allowType: true } }; + node.parent.components.push(sibling); + delete node.parent; + node = sibling; + break; + // Hit a space... + case " ": + // If reading type, the type is done and may read a param or name + if (node.state.allowType) { + if (node.type !== "") { + node.type = verifyType(node.type); + delete node.state.allowType; + node.state.allowName = true; + node.state.allowParams = true; + } + } + // If reading name, the name is done + if (node.state.allowName) { + if (node.name !== "") { + if (node.name === "indexed") { + if (!allowIndexed) { + throwError(i); + } + if (node.indexed) { + throwError(i); + } + node.indexed = true; + node.name = ""; + } + else if (checkModifier(node.type, node.name)) { + node.name = ""; + } + else { + node.state.allowName = false; + } + } + } + break; + case "[": + if (!node.state.allowArray) { + throwError(i); + } + node.type += c; + node.state.allowArray = false; + node.state.allowName = false; + node.state.readArray = true; + break; + case "]": + if (!node.state.readArray) { + throwError(i); + } + node.type += c; + node.state.readArray = false; + node.state.allowArray = true; + node.state.allowName = true; + break; + default: + if (node.state.allowType) { + node.type += c; + node.state.allowParams = true; + node.state.allowArray = true; + } + else if (node.state.allowName) { + node.name += c; + delete node.state.allowArray; + } + else if (node.state.readArray) { + node.type += c; + } + else { + throwError(i); + } + } + } + if (node.parent) { + logger$b.throwArgumentError("unexpected eof", "param", param); + } + delete parent.state; + if (node.name === "indexed") { + if (!allowIndexed) { + throwError(originalParam.length - 7); + } + if (node.indexed) { + throwError(originalParam.length - 7); + } + node.indexed = true; + node.name = ""; + } + else if (checkModifier(node.type, node.name)) { + node.name = ""; + } + parent.type = verifyType(parent.type); + return parent; +} +function populate(object, params) { + for (let key in params) { + defineReadOnly(object, key, params[key]); + } +} +const FormatTypes = Object.freeze({ + // Bare formatting, as is needed for computing a sighash of an event or function + sighash: "sighash", + // Human-Readable with Minimal spacing and without names (compact human-readable) + minimal: "minimal", + // Human-Readable with nice spacing, including all names + full: "full", + // JSON-format a la Solidity + json: "json" +}); +const paramTypeArray = new RegExp(/^(.*)\[([0-9]*)\]$/); +class ParamType { + constructor(constructorGuard, params) { + if (constructorGuard !== _constructorGuard) { + logger$b.throwError("use fromString", Logger.errors.UNSUPPORTED_OPERATION, { + operation: "new ParamType()" + }); + } + populate(this, params); + let match = this.type.match(paramTypeArray); + if (match) { + populate(this, { + arrayLength: parseInt(match[2] || "-1"), + arrayChildren: ParamType.fromObject({ + type: match[1], + components: this.components + }), + baseType: "array" + }); + } + else { + populate(this, { + arrayLength: null, + arrayChildren: null, + baseType: ((this.components != null) ? "tuple" : this.type) + }); + } + this._isParamType = true; + Object.freeze(this); + } + // Format the parameter fragment + // - sighash: "(uint256,address)" + // - minimal: "tuple(uint256,address) indexed" + // - full: "tuple(uint256 foo, address bar) indexed baz" + format(format) { + if (!format) { + format = FormatTypes.sighash; + } + if (!FormatTypes[format]) { + logger$b.throwArgumentError("invalid format type", "format", format); + } + if (format === FormatTypes.json) { + let result = { + type: ((this.baseType === "tuple") ? "tuple" : this.type), + name: (this.name || undefined) + }; + if (typeof (this.indexed) === "boolean") { + result.indexed = this.indexed; + } + if (this.components) { + result.components = this.components.map((comp) => JSON.parse(comp.format(format))); + } + return JSON.stringify(result); + } + let result = ""; + // Array + if (this.baseType === "array") { + result += this.arrayChildren.format(format); + result += "[" + (this.arrayLength < 0 ? "" : String(this.arrayLength)) + "]"; + } + else { + if (this.baseType === "tuple") { + if (format !== FormatTypes.sighash) { + result += this.type; + } + result += "(" + this.components.map((comp) => comp.format(format)).join((format === FormatTypes.full) ? ", " : ",") + ")"; + } + else { + result += this.type; + } + } + if (format !== FormatTypes.sighash) { + if (this.indexed === true) { + result += " indexed"; + } + if (format === FormatTypes.full && this.name) { + result += " " + this.name; + } + } + return result; + } + static from(value, allowIndexed) { + if (typeof (value) === "string") { + return ParamType.fromString(value, allowIndexed); + } + return ParamType.fromObject(value); + } + static fromObject(value) { + if (ParamType.isParamType(value)) { + return value; + } + return new ParamType(_constructorGuard, { + name: (value.name || null), + type: verifyType(value.type), + indexed: ((value.indexed == null) ? null : !!value.indexed), + components: (value.components ? value.components.map(ParamType.fromObject) : null) + }); + } + static fromString(value, allowIndexed) { + function ParamTypify(node) { + return ParamType.fromObject({ + name: node.name, + type: node.type, + indexed: node.indexed, + components: node.components + }); + } + return ParamTypify(parseParamType(value, !!allowIndexed)); + } + static isParamType(value) { + return !!(value != null && value._isParamType); + } +} +function parseParams(value, allowIndex) { + return splitNesting(value).map((param) => ParamType.fromString(param, allowIndex)); +} +class Fragment { + constructor(constructorGuard, params) { + if (constructorGuard !== _constructorGuard) { + logger$b.throwError("use a static from method", Logger.errors.UNSUPPORTED_OPERATION, { + operation: "new Fragment()" + }); + } + populate(this, params); + this._isFragment = true; + Object.freeze(this); + } + static from(value) { + if (Fragment.isFragment(value)) { + return value; + } + if (typeof (value) === "string") { + return Fragment.fromString(value); + } + return Fragment.fromObject(value); + } + static fromObject(value) { + if (Fragment.isFragment(value)) { + return value; + } + switch (value.type) { + case "function": + return FunctionFragment.fromObject(value); + case "event": + return EventFragment.fromObject(value); + case "constructor": + return ConstructorFragment.fromObject(value); + case "error": + return ErrorFragment.fromObject(value); + case "fallback": + case "receive": + // @TODO: Something? Maybe return a FunctionFragment? A custom DefaultFunctionFragment? + return null; + } + return logger$b.throwArgumentError("invalid fragment object", "value", value); + } + static fromString(value) { + // Make sure the "returns" is surrounded by a space and all whitespace is exactly one space + value = value.replace(/\s/g, " "); + value = value.replace(/\(/g, " (").replace(/\)/g, ") ").replace(/\s+/g, " "); + value = value.trim(); + if (value.split(" ")[0] === "event") { + return EventFragment.fromString(value.substring(5).trim()); + } + else if (value.split(" ")[0] === "function") { + return FunctionFragment.fromString(value.substring(8).trim()); + } + else if (value.split("(")[0].trim() === "constructor") { + return ConstructorFragment.fromString(value.trim()); + } + else if (value.split(" ")[0] === "error") { + return ErrorFragment.fromString(value.substring(5).trim()); + } + return logger$b.throwArgumentError("unsupported fragment", "value", value); + } + static isFragment(value) { + return !!(value && value._isFragment); + } +} +class EventFragment extends Fragment { + format(format) { + if (!format) { + format = FormatTypes.sighash; + } + if (!FormatTypes[format]) { + logger$b.throwArgumentError("invalid format type", "format", format); + } + if (format === FormatTypes.json) { + return JSON.stringify({ + type: "event", + anonymous: this.anonymous, + name: this.name, + inputs: this.inputs.map((input) => JSON.parse(input.format(format))) + }); + } + let result = ""; + if (format !== FormatTypes.sighash) { + result += "event "; + } + result += this.name + "(" + this.inputs.map((input) => input.format(format)).join((format === FormatTypes.full) ? ", " : ",") + ") "; + if (format !== FormatTypes.sighash) { + if (this.anonymous) { + result += "anonymous "; + } + } + return result.trim(); + } + static from(value) { + if (typeof (value) === "string") { + return EventFragment.fromString(value); + } + return EventFragment.fromObject(value); + } + static fromObject(value) { + if (EventFragment.isEventFragment(value)) { + return value; + } + if (value.type !== "event") { + logger$b.throwArgumentError("invalid event object", "value", value); + } + const params = { + name: verifyIdentifier(value.name), + anonymous: value.anonymous, + inputs: (value.inputs ? value.inputs.map(ParamType.fromObject) : []), + type: "event" + }; + return new EventFragment(_constructorGuard, params); + } + static fromString(value) { + let match = value.match(regexParen); + if (!match) { + logger$b.throwArgumentError("invalid event string", "value", value); + } + let anonymous = false; + match[3].split(" ").forEach((modifier) => { + switch (modifier.trim()) { + case "anonymous": + anonymous = true; + break; + case "": + break; + default: + logger$b.warn("unknown modifier: " + modifier); + } + }); + return EventFragment.fromObject({ + name: match[1].trim(), + anonymous: anonymous, + inputs: parseParams(match[2], true), + type: "event" + }); + } + static isEventFragment(value) { + return (value && value._isFragment && value.type === "event"); + } +} +function parseGas(value, params) { + params.gas = null; + let comps = value.split("@"); + if (comps.length !== 1) { + if (comps.length > 2) { + logger$b.throwArgumentError("invalid human-readable ABI signature", "value", value); + } + if (!comps[1].match(/^[0-9]+$/)) { + logger$b.throwArgumentError("invalid human-readable ABI signature gas", "value", value); + } + params.gas = BigNumber.from(comps[1]); + return comps[0]; + } + return value; +} +function parseModifiers(value, params) { + params.constant = false; + params.payable = false; + params.stateMutability = "nonpayable"; + value.split(" ").forEach((modifier) => { + switch (modifier.trim()) { + case "constant": + params.constant = true; + break; + case "payable": + params.payable = true; + params.stateMutability = "payable"; + break; + case "nonpayable": + params.payable = false; + params.stateMutability = "nonpayable"; + break; + case "pure": + params.constant = true; + params.stateMutability = "pure"; + break; + case "view": + params.constant = true; + params.stateMutability = "view"; + break; + case "external": + case "public": + case "": + break; + default: + console.log("unknown modifier: " + modifier); + } + }); +} +function verifyState(value) { + let result = { + constant: false, + payable: true, + stateMutability: "payable" + }; + if (value.stateMutability != null) { + result.stateMutability = value.stateMutability; + // Set (and check things are consistent) the constant property + result.constant = (result.stateMutability === "view" || result.stateMutability === "pure"); + if (value.constant != null) { + if ((!!value.constant) !== result.constant) { + logger$b.throwArgumentError("cannot have constant function with mutability " + result.stateMutability, "value", value); + } + } + // Set (and check things are consistent) the payable property + result.payable = (result.stateMutability === "payable"); + if (value.payable != null) { + if ((!!value.payable) !== result.payable) { + logger$b.throwArgumentError("cannot have payable function with mutability " + result.stateMutability, "value", value); + } + } + } + else if (value.payable != null) { + result.payable = !!value.payable; + // If payable we can assume non-constant; otherwise we can't assume + if (value.constant == null && !result.payable && value.type !== "constructor") { + logger$b.throwArgumentError("unable to determine stateMutability", "value", value); + } + result.constant = !!value.constant; + if (result.constant) { + result.stateMutability = "view"; + } + else { + result.stateMutability = (result.payable ? "payable" : "nonpayable"); + } + if (result.payable && result.constant) { + logger$b.throwArgumentError("cannot have constant payable function", "value", value); + } + } + else if (value.constant != null) { + result.constant = !!value.constant; + result.payable = !result.constant; + result.stateMutability = (result.constant ? "view" : "payable"); + } + else if (value.type !== "constructor") { + logger$b.throwArgumentError("unable to determine stateMutability", "value", value); + } + return result; +} +class ConstructorFragment extends Fragment { + format(format) { + if (!format) { + format = FormatTypes.sighash; + } + if (!FormatTypes[format]) { + logger$b.throwArgumentError("invalid format type", "format", format); + } + if (format === FormatTypes.json) { + return JSON.stringify({ + type: "constructor", + stateMutability: ((this.stateMutability !== "nonpayable") ? this.stateMutability : undefined), + payable: this.payable, + gas: (this.gas ? this.gas.toNumber() : undefined), + inputs: this.inputs.map((input) => JSON.parse(input.format(format))) + }); + } + if (format === FormatTypes.sighash) { + logger$b.throwError("cannot format a constructor for sighash", Logger.errors.UNSUPPORTED_OPERATION, { + operation: "format(sighash)" + }); + } + let result = "constructor(" + this.inputs.map((input) => input.format(format)).join((format === FormatTypes.full) ? ", " : ",") + ") "; + if (this.stateMutability && this.stateMutability !== "nonpayable") { + result += this.stateMutability + " "; + } + return result.trim(); + } + static from(value) { + if (typeof (value) === "string") { + return ConstructorFragment.fromString(value); + } + return ConstructorFragment.fromObject(value); + } + static fromObject(value) { + if (ConstructorFragment.isConstructorFragment(value)) { + return value; + } + if (value.type !== "constructor") { + logger$b.throwArgumentError("invalid constructor object", "value", value); + } + let state = verifyState(value); + if (state.constant) { + logger$b.throwArgumentError("constructor cannot be constant", "value", value); + } + const params = { + name: null, + type: value.type, + inputs: (value.inputs ? value.inputs.map(ParamType.fromObject) : []), + payable: state.payable, + stateMutability: state.stateMutability, + gas: (value.gas ? BigNumber.from(value.gas) : null) + }; + return new ConstructorFragment(_constructorGuard, params); + } + static fromString(value) { + let params = { type: "constructor" }; + value = parseGas(value, params); + let parens = value.match(regexParen); + if (!parens || parens[1].trim() !== "constructor") { + logger$b.throwArgumentError("invalid constructor string", "value", value); + } + params.inputs = parseParams(parens[2].trim(), false); + parseModifiers(parens[3].trim(), params); + return ConstructorFragment.fromObject(params); + } + static isConstructorFragment(value) { + return (value && value._isFragment && value.type === "constructor"); + } +} +class FunctionFragment extends ConstructorFragment { + format(format) { + if (!format) { + format = FormatTypes.sighash; + } + if (!FormatTypes[format]) { + logger$b.throwArgumentError("invalid format type", "format", format); + } + if (format === FormatTypes.json) { + return JSON.stringify({ + type: "function", + name: this.name, + constant: this.constant, + stateMutability: ((this.stateMutability !== "nonpayable") ? this.stateMutability : undefined), + payable: this.payable, + gas: (this.gas ? this.gas.toNumber() : undefined), + inputs: this.inputs.map((input) => JSON.parse(input.format(format))), + outputs: this.outputs.map((output) => JSON.parse(output.format(format))), + }); + } + let result = ""; + if (format !== FormatTypes.sighash) { + result += "function "; + } + result += this.name + "(" + this.inputs.map((input) => input.format(format)).join((format === FormatTypes.full) ? ", " : ",") + ") "; + if (format !== FormatTypes.sighash) { + if (this.stateMutability) { + if (this.stateMutability !== "nonpayable") { + result += (this.stateMutability + " "); + } + } + else if (this.constant) { + result += "view "; + } + if (this.outputs && this.outputs.length) { + result += "returns (" + this.outputs.map((output) => output.format(format)).join(", ") + ") "; + } + if (this.gas != null) { + result += "@" + this.gas.toString() + " "; + } + } + return result.trim(); + } + static from(value) { + if (typeof (value) === "string") { + return FunctionFragment.fromString(value); + } + return FunctionFragment.fromObject(value); + } + static fromObject(value) { + if (FunctionFragment.isFunctionFragment(value)) { + return value; + } + if (value.type !== "function") { + logger$b.throwArgumentError("invalid function object", "value", value); + } + let state = verifyState(value); + const params = { + type: value.type, + name: verifyIdentifier(value.name), + constant: state.constant, + inputs: (value.inputs ? value.inputs.map(ParamType.fromObject) : []), + outputs: (value.outputs ? value.outputs.map(ParamType.fromObject) : []), + payable: state.payable, + stateMutability: state.stateMutability, + gas: (value.gas ? BigNumber.from(value.gas) : null) + }; + return new FunctionFragment(_constructorGuard, params); + } + static fromString(value) { + let params = { type: "function" }; + value = parseGas(value, params); + let comps = value.split(" returns "); + if (comps.length > 2) { + logger$b.throwArgumentError("invalid function string", "value", value); + } + let parens = comps[0].match(regexParen); + if (!parens) { + logger$b.throwArgumentError("invalid function signature", "value", value); + } + params.name = parens[1].trim(); + if (params.name) { + verifyIdentifier(params.name); + } + params.inputs = parseParams(parens[2], false); + parseModifiers(parens[3].trim(), params); + // We have outputs + if (comps.length > 1) { + let returns = comps[1].match(regexParen); + if (returns[1].trim() != "" || returns[3].trim() != "") { + logger$b.throwArgumentError("unexpected tokens", "value", value); + } + params.outputs = parseParams(returns[2], false); + } + else { + params.outputs = []; + } + return FunctionFragment.fromObject(params); + } + static isFunctionFragment(value) { + return (value && value._isFragment && value.type === "function"); + } +} +//export class StructFragment extends Fragment { +//} +function checkForbidden(fragment) { + const sig = fragment.format(); + if (sig === "Error(string)" || sig === "Panic(uint256)") { + logger$b.throwArgumentError(`cannot specify user defined ${sig} error`, "fragment", fragment); + } + return fragment; +} +class ErrorFragment extends Fragment { + format(format) { + if (!format) { + format = FormatTypes.sighash; + } + if (!FormatTypes[format]) { + logger$b.throwArgumentError("invalid format type", "format", format); + } + if (format === FormatTypes.json) { + return JSON.stringify({ + type: "error", + name: this.name, + inputs: this.inputs.map((input) => JSON.parse(input.format(format))), + }); + } + let result = ""; + if (format !== FormatTypes.sighash) { + result += "error "; + } + result += this.name + "(" + this.inputs.map((input) => input.format(format)).join((format === FormatTypes.full) ? ", " : ",") + ") "; + return result.trim(); + } + static from(value) { + if (typeof (value) === "string") { + return ErrorFragment.fromString(value); + } + return ErrorFragment.fromObject(value); + } + static fromObject(value) { + if (ErrorFragment.isErrorFragment(value)) { + return value; + } + if (value.type !== "error") { + logger$b.throwArgumentError("invalid error object", "value", value); + } + const params = { + type: value.type, + name: verifyIdentifier(value.name), + inputs: (value.inputs ? value.inputs.map(ParamType.fromObject) : []) + }; + return checkForbidden(new ErrorFragment(_constructorGuard, params)); + } + static fromString(value) { + let params = { type: "error" }; + let parens = value.match(regexParen); + if (!parens) { + logger$b.throwArgumentError("invalid error signature", "value", value); + } + params.name = parens[1].trim(); + if (params.name) { + verifyIdentifier(params.name); + } + params.inputs = parseParams(parens[2], false); + return checkForbidden(ErrorFragment.fromObject(params)); + } + static isErrorFragment(value) { + return (value && value._isFragment && value.type === "error"); + } +} +function verifyType(type) { + // These need to be transformed to their full description + if (type.match(/^uint($|[^1-9])/)) { + type = "uint256" + type.substring(4); + } + else if (type.match(/^int($|[^1-9])/)) { + type = "int256" + type.substring(3); + } + // @TODO: more verification + return type; +} +// See: https://github.com/ethereum/solidity/blob/1f8f1a3db93a548d0555e3e14cfc55a10e25b60e/docs/grammar/SolidityLexer.g4#L234 +const regexIdentifier = new RegExp("^[a-zA-Z$_][a-zA-Z0-9$_]*$"); +function verifyIdentifier(value) { + if (!value || !value.match(regexIdentifier)) { + logger$b.throwArgumentError(`invalid identifier "${value}"`, "value", value); + } + return value; +} +const regexParen = new RegExp("^([^)(]*)\\((.*)\\)([^)(]*)$"); +function splitNesting(value) { + value = value.trim(); + let result = []; + let accum = ""; + let depth = 0; + for (let offset = 0; offset < value.length; offset++) { + let c = value[offset]; + if (c === "," && depth === 0) { + result.push(accum); + accum = ""; + } + else { + accum += c; + if (c === "(") { + depth++; + } + else if (c === ")") { + depth--; + if (depth === -1) { + logger$b.throwArgumentError("unbalanced parenthesis", "value", value); + } + } + } + } + if (accum) { + result.push(accum); + } + return result; +} + +const logger$a = new Logger(version$7); +function checkResultErrors(result) { + // Find the first error (if any) + const errors = []; + const checkErrors = function (path, object) { + if (!Array.isArray(object)) { + return; + } + for (let key in object) { + const childPath = path.slice(); + childPath.push(key); + try { + checkErrors(childPath, object[key]); + } + catch (error) { + errors.push({ path: childPath, error: error }); + } + } + }; + checkErrors([], result); + return errors; +} +class Coder { + constructor(name, type, localName, dynamic) { + // @TODO: defineReadOnly these + this.name = name; + this.type = type; + this.localName = localName; + this.dynamic = dynamic; + } + _throwError(message, value) { + logger$a.throwArgumentError(message, this.localName, value); + } +} +class Writer { + constructor(wordSize) { + defineReadOnly(this, "wordSize", wordSize || 32); + this._data = []; + this._dataLength = 0; + this._padding = new Uint8Array(wordSize); + } + get data() { + return hexConcat(this._data); + } + get length() { return this._dataLength; } + _writeData(data) { + this._data.push(data); + this._dataLength += data.length; + return data.length; + } + appendWriter(writer) { + return this._writeData(concat(writer._data)); + } + // Arrayish items; padded on the right to wordSize + writeBytes(value) { + let bytes = arrayify(value); + const paddingOffset = bytes.length % this.wordSize; + if (paddingOffset) { + bytes = concat([bytes, this._padding.slice(paddingOffset)]); + } + return this._writeData(bytes); + } + _getValue(value) { + let bytes = arrayify(BigNumber.from(value)); + if (bytes.length > this.wordSize) { + logger$a.throwError("value out-of-bounds", Logger.errors.BUFFER_OVERRUN, { + length: this.wordSize, + offset: bytes.length + }); + } + if (bytes.length % this.wordSize) { + bytes = concat([this._padding.slice(bytes.length % this.wordSize), bytes]); + } + return bytes; + } + // BigNumberish items; padded on the left to wordSize + writeValue(value) { + return this._writeData(this._getValue(value)); + } + writeUpdatableValue() { + const offset = this._data.length; + this._data.push(this._padding); + this._dataLength += this.wordSize; + return (value) => { + this._data[offset] = this._getValue(value); + }; + } +} +class Reader { + constructor(data, wordSize, coerceFunc, allowLoose) { + defineReadOnly(this, "_data", arrayify(data)); + defineReadOnly(this, "wordSize", wordSize || 32); + defineReadOnly(this, "_coerceFunc", coerceFunc); + defineReadOnly(this, "allowLoose", allowLoose); + this._offset = 0; + } + get data() { return hexlify(this._data); } + get consumed() { return this._offset; } + // The default Coerce function + static coerce(name, value) { + let match = name.match("^u?int([0-9]+)$"); + if (match && parseInt(match[1]) <= 48) { + value = value.toNumber(); + } + return value; + } + coerce(name, value) { + if (this._coerceFunc) { + return this._coerceFunc(name, value); + } + return Reader.coerce(name, value); + } + _peekBytes(offset, length, loose) { + let alignedLength = Math.ceil(length / this.wordSize) * this.wordSize; + if (this._offset + alignedLength > this._data.length) { + if (this.allowLoose && loose && this._offset + length <= this._data.length) { + alignedLength = length; + } + else { + logger$a.throwError("data out-of-bounds", Logger.errors.BUFFER_OVERRUN, { + length: this._data.length, + offset: this._offset + alignedLength + }); + } + } + return this._data.slice(this._offset, this._offset + alignedLength); + } + subReader(offset) { + return new Reader(this._data.slice(this._offset + offset), this.wordSize, this._coerceFunc, this.allowLoose); + } + readBytes(length, loose) { + let bytes = this._peekBytes(0, length, !!loose); + this._offset += bytes.length; + // @TODO: Make sure the length..end bytes are all 0? + return bytes.slice(0, length); + } + readValue() { + return BigNumber.from(this.readBytes(this.wordSize)); + } +} + +var sha3$1 = {exports: {}}; + +/** + * [js-sha3]{@link https://github.com/emn178/js-sha3} + * + * @version 0.8.0 + * @author Chen, Yi-Cyuan [emn178@gmail.com] + * @copyright Chen, Yi-Cyuan 2015-2018 + * @license MIT + */ + +(function (module) { + /*jslint bitwise: true */ + (function () { + + var INPUT_ERROR = 'input is invalid type'; + var FINALIZE_ERROR = 'finalize already called'; + var WINDOW = typeof window === 'object'; + var root = WINDOW ? window : {}; + if (root.JS_SHA3_NO_WINDOW) { + WINDOW = false; + } + var WEB_WORKER = !WINDOW && typeof self === 'object'; + var NODE_JS = !root.JS_SHA3_NO_NODE_JS && typeof process === 'object' && process.versions && process.versions.node; + if (NODE_JS) { + root = commonjsGlobal; + } else if (WEB_WORKER) { + root = self; + } + var COMMON_JS = !root.JS_SHA3_NO_COMMON_JS && 'object' === 'object' && module.exports; + var ARRAY_BUFFER = !root.JS_SHA3_NO_ARRAY_BUFFER && typeof ArrayBuffer !== 'undefined'; + var HEX_CHARS = '0123456789abcdef'.split(''); + var SHAKE_PADDING = [31, 7936, 2031616, 520093696]; + var CSHAKE_PADDING = [4, 1024, 262144, 67108864]; + var KECCAK_PADDING = [1, 256, 65536, 16777216]; + var PADDING = [6, 1536, 393216, 100663296]; + var SHIFT = [0, 8, 16, 24]; + var RC = [1, 0, 32898, 0, 32906, 2147483648, 2147516416, 2147483648, 32907, 0, 2147483649, + 0, 2147516545, 2147483648, 32777, 2147483648, 138, 0, 136, 0, 2147516425, 0, + 2147483658, 0, 2147516555, 0, 139, 2147483648, 32905, 2147483648, 32771, + 2147483648, 32770, 2147483648, 128, 2147483648, 32778, 0, 2147483658, 2147483648, + 2147516545, 2147483648, 32896, 2147483648, 2147483649, 0, 2147516424, 2147483648]; + var BITS = [224, 256, 384, 512]; + var SHAKE_BITS = [128, 256]; + var OUTPUT_TYPES = ['hex', 'buffer', 'arrayBuffer', 'array', 'digest']; + var CSHAKE_BYTEPAD = { + '128': 168, + '256': 136 + }; + + if (root.JS_SHA3_NO_NODE_JS || !Array.isArray) { + Array.isArray = function (obj) { + return Object.prototype.toString.call(obj) === '[object Array]'; + }; + } + + if (ARRAY_BUFFER && (root.JS_SHA3_NO_ARRAY_BUFFER_IS_VIEW || !ArrayBuffer.isView)) { + ArrayBuffer.isView = function (obj) { + return typeof obj === 'object' && obj.buffer && obj.buffer.constructor === ArrayBuffer; + }; + } + + var createOutputMethod = function (bits, padding, outputType) { + return function (message) { + return new Keccak(bits, padding, bits).update(message)[outputType](); + }; + }; + + var createShakeOutputMethod = function (bits, padding, outputType) { + return function (message, outputBits) { + return new Keccak(bits, padding, outputBits).update(message)[outputType](); + }; + }; + + var createCshakeOutputMethod = function (bits, padding, outputType) { + return function (message, outputBits, n, s) { + return methods['cshake' + bits].update(message, outputBits, n, s)[outputType](); + }; + }; + + var createKmacOutputMethod = function (bits, padding, outputType) { + return function (key, message, outputBits, s) { + return methods['kmac' + bits].update(key, message, outputBits, s)[outputType](); + }; + }; + + var createOutputMethods = function (method, createMethod, bits, padding) { + for (var i = 0; i < OUTPUT_TYPES.length; ++i) { + var type = OUTPUT_TYPES[i]; + method[type] = createMethod(bits, padding, type); + } + return method; + }; + + var createMethod = function (bits, padding) { + var method = createOutputMethod(bits, padding, 'hex'); + method.create = function () { + return new Keccak(bits, padding, bits); + }; + method.update = function (message) { + return method.create().update(message); + }; + return createOutputMethods(method, createOutputMethod, bits, padding); + }; + + var createShakeMethod = function (bits, padding) { + var method = createShakeOutputMethod(bits, padding, 'hex'); + method.create = function (outputBits) { + return new Keccak(bits, padding, outputBits); + }; + method.update = function (message, outputBits) { + return method.create(outputBits).update(message); + }; + return createOutputMethods(method, createShakeOutputMethod, bits, padding); + }; + + var createCshakeMethod = function (bits, padding) { + var w = CSHAKE_BYTEPAD[bits]; + var method = createCshakeOutputMethod(bits, padding, 'hex'); + method.create = function (outputBits, n, s) { + if (!n && !s) { + return methods['shake' + bits].create(outputBits); + } else { + return new Keccak(bits, padding, outputBits).bytepad([n, s], w); + } + }; + method.update = function (message, outputBits, n, s) { + return method.create(outputBits, n, s).update(message); + }; + return createOutputMethods(method, createCshakeOutputMethod, bits, padding); + }; + + var createKmacMethod = function (bits, padding) { + var w = CSHAKE_BYTEPAD[bits]; + var method = createKmacOutputMethod(bits, padding, 'hex'); + method.create = function (key, outputBits, s) { + return new Kmac(bits, padding, outputBits).bytepad(['KMAC', s], w).bytepad([key], w); + }; + method.update = function (key, message, outputBits, s) { + return method.create(key, outputBits, s).update(message); + }; + return createOutputMethods(method, createKmacOutputMethod, bits, padding); + }; + + var algorithms = [ + { name: 'keccak', padding: KECCAK_PADDING, bits: BITS, createMethod: createMethod }, + { name: 'sha3', padding: PADDING, bits: BITS, createMethod: createMethod }, + { name: 'shake', padding: SHAKE_PADDING, bits: SHAKE_BITS, createMethod: createShakeMethod }, + { name: 'cshake', padding: CSHAKE_PADDING, bits: SHAKE_BITS, createMethod: createCshakeMethod }, + { name: 'kmac', padding: CSHAKE_PADDING, bits: SHAKE_BITS, createMethod: createKmacMethod } + ]; + + var methods = {}, methodNames = []; + + for (var i = 0; i < algorithms.length; ++i) { + var algorithm = algorithms[i]; + var bits = algorithm.bits; + for (var j = 0; j < bits.length; ++j) { + var methodName = algorithm.name + '_' + bits[j]; + methodNames.push(methodName); + methods[methodName] = algorithm.createMethod(bits[j], algorithm.padding); + if (algorithm.name !== 'sha3') { + var newMethodName = algorithm.name + bits[j]; + methodNames.push(newMethodName); + methods[newMethodName] = methods[methodName]; + } + } + } + + function Keccak(bits, padding, outputBits) { + this.blocks = []; + this.s = []; + this.padding = padding; + this.outputBits = outputBits; + this.reset = true; + this.finalized = false; + this.block = 0; + this.start = 0; + this.blockCount = (1600 - (bits << 1)) >> 5; + this.byteCount = this.blockCount << 2; + this.outputBlocks = outputBits >> 5; + this.extraBytes = (outputBits & 31) >> 3; + + for (var i = 0; i < 50; ++i) { + this.s[i] = 0; + } + } + + Keccak.prototype.update = function (message) { + if (this.finalized) { + throw new Error(FINALIZE_ERROR); + } + var notString, type = typeof message; + if (type !== 'string') { + if (type === 'object') { + if (message === null) { + throw new Error(INPUT_ERROR); + } else if (ARRAY_BUFFER && message.constructor === ArrayBuffer) { + message = new Uint8Array(message); + } else if (!Array.isArray(message)) { + if (!ARRAY_BUFFER || !ArrayBuffer.isView(message)) { + throw new Error(INPUT_ERROR); + } + } + } else { + throw new Error(INPUT_ERROR); + } + notString = true; + } + var blocks = this.blocks, byteCount = this.byteCount, length = message.length, + blockCount = this.blockCount, index = 0, s = this.s, i, code; + + while (index < length) { + if (this.reset) { + this.reset = false; + blocks[0] = this.block; + for (i = 1; i < blockCount + 1; ++i) { + blocks[i] = 0; + } + } + if (notString) { + for (i = this.start; index < length && i < byteCount; ++index) { + blocks[i >> 2] |= message[index] << SHIFT[i++ & 3]; + } + } else { + for (i = this.start; index < length && i < byteCount; ++index) { + code = message.charCodeAt(index); + if (code < 0x80) { + blocks[i >> 2] |= code << SHIFT[i++ & 3]; + } else if (code < 0x800) { + blocks[i >> 2] |= (0xc0 | (code >> 6)) << SHIFT[i++ & 3]; + blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3]; + } else if (code < 0xd800 || code >= 0xe000) { + blocks[i >> 2] |= (0xe0 | (code >> 12)) << SHIFT[i++ & 3]; + blocks[i >> 2] |= (0x80 | ((code >> 6) & 0x3f)) << SHIFT[i++ & 3]; + blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3]; + } else { + code = 0x10000 + (((code & 0x3ff) << 10) | (message.charCodeAt(++index) & 0x3ff)); + blocks[i >> 2] |= (0xf0 | (code >> 18)) << SHIFT[i++ & 3]; + blocks[i >> 2] |= (0x80 | ((code >> 12) & 0x3f)) << SHIFT[i++ & 3]; + blocks[i >> 2] |= (0x80 | ((code >> 6) & 0x3f)) << SHIFT[i++ & 3]; + blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3]; + } + } + } + this.lastByteIndex = i; + if (i >= byteCount) { + this.start = i - byteCount; + this.block = blocks[blockCount]; + for (i = 0; i < blockCount; ++i) { + s[i] ^= blocks[i]; + } + f(s); + this.reset = true; + } else { + this.start = i; + } + } + return this; + }; + + Keccak.prototype.encode = function (x, right) { + var o = x & 255, n = 1; + var bytes = [o]; + x = x >> 8; + o = x & 255; + while (o > 0) { + bytes.unshift(o); + x = x >> 8; + o = x & 255; + ++n; + } + if (right) { + bytes.push(n); + } else { + bytes.unshift(n); + } + this.update(bytes); + return bytes.length; + }; + + Keccak.prototype.encodeString = function (str) { + var notString, type = typeof str; + if (type !== 'string') { + if (type === 'object') { + if (str === null) { + throw new Error(INPUT_ERROR); + } else if (ARRAY_BUFFER && str.constructor === ArrayBuffer) { + str = new Uint8Array(str); + } else if (!Array.isArray(str)) { + if (!ARRAY_BUFFER || !ArrayBuffer.isView(str)) { + throw new Error(INPUT_ERROR); + } + } + } else { + throw new Error(INPUT_ERROR); + } + notString = true; + } + var bytes = 0, length = str.length; + if (notString) { + bytes = length; + } else { + for (var i = 0; i < str.length; ++i) { + var code = str.charCodeAt(i); + if (code < 0x80) { + bytes += 1; + } else if (code < 0x800) { + bytes += 2; + } else if (code < 0xd800 || code >= 0xe000) { + bytes += 3; + } else { + code = 0x10000 + (((code & 0x3ff) << 10) | (str.charCodeAt(++i) & 0x3ff)); + bytes += 4; + } + } + } + bytes += this.encode(bytes * 8); + this.update(str); + return bytes; + }; + + Keccak.prototype.bytepad = function (strs, w) { + var bytes = this.encode(w); + for (var i = 0; i < strs.length; ++i) { + bytes += this.encodeString(strs[i]); + } + var paddingBytes = w - bytes % w; + var zeros = []; + zeros.length = paddingBytes; + this.update(zeros); + return this; + }; + + Keccak.prototype.finalize = function () { + if (this.finalized) { + return; + } + this.finalized = true; + var blocks = this.blocks, i = this.lastByteIndex, blockCount = this.blockCount, s = this.s; + blocks[i >> 2] |= this.padding[i & 3]; + if (this.lastByteIndex === this.byteCount) { + blocks[0] = blocks[blockCount]; + for (i = 1; i < blockCount + 1; ++i) { + blocks[i] = 0; + } + } + blocks[blockCount - 1] |= 0x80000000; + for (i = 0; i < blockCount; ++i) { + s[i] ^= blocks[i]; + } + f(s); + }; + + Keccak.prototype.toString = Keccak.prototype.hex = function () { + this.finalize(); + + var blockCount = this.blockCount, s = this.s, outputBlocks = this.outputBlocks, + extraBytes = this.extraBytes, i = 0, j = 0; + var hex = '', block; + while (j < outputBlocks) { + for (i = 0; i < blockCount && j < outputBlocks; ++i, ++j) { + block = s[i]; + hex += HEX_CHARS[(block >> 4) & 0x0F] + HEX_CHARS[block & 0x0F] + + HEX_CHARS[(block >> 12) & 0x0F] + HEX_CHARS[(block >> 8) & 0x0F] + + HEX_CHARS[(block >> 20) & 0x0F] + HEX_CHARS[(block >> 16) & 0x0F] + + HEX_CHARS[(block >> 28) & 0x0F] + HEX_CHARS[(block >> 24) & 0x0F]; + } + if (j % blockCount === 0) { + f(s); + i = 0; + } + } + if (extraBytes) { + block = s[i]; + hex += HEX_CHARS[(block >> 4) & 0x0F] + HEX_CHARS[block & 0x0F]; + if (extraBytes > 1) { + hex += HEX_CHARS[(block >> 12) & 0x0F] + HEX_CHARS[(block >> 8) & 0x0F]; + } + if (extraBytes > 2) { + hex += HEX_CHARS[(block >> 20) & 0x0F] + HEX_CHARS[(block >> 16) & 0x0F]; + } + } + return hex; + }; + + Keccak.prototype.arrayBuffer = function () { + this.finalize(); + + var blockCount = this.blockCount, s = this.s, outputBlocks = this.outputBlocks, + extraBytes = this.extraBytes, i = 0, j = 0; + var bytes = this.outputBits >> 3; + var buffer; + if (extraBytes) { + buffer = new ArrayBuffer((outputBlocks + 1) << 2); + } else { + buffer = new ArrayBuffer(bytes); + } + var array = new Uint32Array(buffer); + while (j < outputBlocks) { + for (i = 0; i < blockCount && j < outputBlocks; ++i, ++j) { + array[j] = s[i]; + } + if (j % blockCount === 0) { + f(s); + } + } + if (extraBytes) { + array[i] = s[i]; + buffer = buffer.slice(0, bytes); + } + return buffer; + }; + + Keccak.prototype.buffer = Keccak.prototype.arrayBuffer; + + Keccak.prototype.digest = Keccak.prototype.array = function () { + this.finalize(); + + var blockCount = this.blockCount, s = this.s, outputBlocks = this.outputBlocks, + extraBytes = this.extraBytes, i = 0, j = 0; + var array = [], offset, block; + while (j < outputBlocks) { + for (i = 0; i < blockCount && j < outputBlocks; ++i, ++j) { + offset = j << 2; + block = s[i]; + array[offset] = block & 0xFF; + array[offset + 1] = (block >> 8) & 0xFF; + array[offset + 2] = (block >> 16) & 0xFF; + array[offset + 3] = (block >> 24) & 0xFF; + } + if (j % blockCount === 0) { + f(s); + } + } + if (extraBytes) { + offset = j << 2; + block = s[i]; + array[offset] = block & 0xFF; + if (extraBytes > 1) { + array[offset + 1] = (block >> 8) & 0xFF; + } + if (extraBytes > 2) { + array[offset + 2] = (block >> 16) & 0xFF; + } + } + return array; + }; + + function Kmac(bits, padding, outputBits) { + Keccak.call(this, bits, padding, outputBits); + } + + Kmac.prototype = new Keccak(); + + Kmac.prototype.finalize = function () { + this.encode(this.outputBits, true); + return Keccak.prototype.finalize.call(this); + }; + + var f = function (s) { + var h, l, n, c0, c1, c2, c3, c4, c5, c6, c7, c8, c9, + b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, b13, b14, b15, b16, b17, + b18, b19, b20, b21, b22, b23, b24, b25, b26, b27, b28, b29, b30, b31, b32, b33, + b34, b35, b36, b37, b38, b39, b40, b41, b42, b43, b44, b45, b46, b47, b48, b49; + for (n = 0; n < 48; n += 2) { + c0 = s[0] ^ s[10] ^ s[20] ^ s[30] ^ s[40]; + c1 = s[1] ^ s[11] ^ s[21] ^ s[31] ^ s[41]; + c2 = s[2] ^ s[12] ^ s[22] ^ s[32] ^ s[42]; + c3 = s[3] ^ s[13] ^ s[23] ^ s[33] ^ s[43]; + c4 = s[4] ^ s[14] ^ s[24] ^ s[34] ^ s[44]; + c5 = s[5] ^ s[15] ^ s[25] ^ s[35] ^ s[45]; + c6 = s[6] ^ s[16] ^ s[26] ^ s[36] ^ s[46]; + c7 = s[7] ^ s[17] ^ s[27] ^ s[37] ^ s[47]; + c8 = s[8] ^ s[18] ^ s[28] ^ s[38] ^ s[48]; + c9 = s[9] ^ s[19] ^ s[29] ^ s[39] ^ s[49]; + + h = c8 ^ ((c2 << 1) | (c3 >>> 31)); + l = c9 ^ ((c3 << 1) | (c2 >>> 31)); + s[0] ^= h; + s[1] ^= l; + s[10] ^= h; + s[11] ^= l; + s[20] ^= h; + s[21] ^= l; + s[30] ^= h; + s[31] ^= l; + s[40] ^= h; + s[41] ^= l; + h = c0 ^ ((c4 << 1) | (c5 >>> 31)); + l = c1 ^ ((c5 << 1) | (c4 >>> 31)); + s[2] ^= h; + s[3] ^= l; + s[12] ^= h; + s[13] ^= l; + s[22] ^= h; + s[23] ^= l; + s[32] ^= h; + s[33] ^= l; + s[42] ^= h; + s[43] ^= l; + h = c2 ^ ((c6 << 1) | (c7 >>> 31)); + l = c3 ^ ((c7 << 1) | (c6 >>> 31)); + s[4] ^= h; + s[5] ^= l; + s[14] ^= h; + s[15] ^= l; + s[24] ^= h; + s[25] ^= l; + s[34] ^= h; + s[35] ^= l; + s[44] ^= h; + s[45] ^= l; + h = c4 ^ ((c8 << 1) | (c9 >>> 31)); + l = c5 ^ ((c9 << 1) | (c8 >>> 31)); + s[6] ^= h; + s[7] ^= l; + s[16] ^= h; + s[17] ^= l; + s[26] ^= h; + s[27] ^= l; + s[36] ^= h; + s[37] ^= l; + s[46] ^= h; + s[47] ^= l; + h = c6 ^ ((c0 << 1) | (c1 >>> 31)); + l = c7 ^ ((c1 << 1) | (c0 >>> 31)); + s[8] ^= h; + s[9] ^= l; + s[18] ^= h; + s[19] ^= l; + s[28] ^= h; + s[29] ^= l; + s[38] ^= h; + s[39] ^= l; + s[48] ^= h; + s[49] ^= l; + + b0 = s[0]; + b1 = s[1]; + b32 = (s[11] << 4) | (s[10] >>> 28); + b33 = (s[10] << 4) | (s[11] >>> 28); + b14 = (s[20] << 3) | (s[21] >>> 29); + b15 = (s[21] << 3) | (s[20] >>> 29); + b46 = (s[31] << 9) | (s[30] >>> 23); + b47 = (s[30] << 9) | (s[31] >>> 23); + b28 = (s[40] << 18) | (s[41] >>> 14); + b29 = (s[41] << 18) | (s[40] >>> 14); + b20 = (s[2] << 1) | (s[3] >>> 31); + b21 = (s[3] << 1) | (s[2] >>> 31); + b2 = (s[13] << 12) | (s[12] >>> 20); + b3 = (s[12] << 12) | (s[13] >>> 20); + b34 = (s[22] << 10) | (s[23] >>> 22); + b35 = (s[23] << 10) | (s[22] >>> 22); + b16 = (s[33] << 13) | (s[32] >>> 19); + b17 = (s[32] << 13) | (s[33] >>> 19); + b48 = (s[42] << 2) | (s[43] >>> 30); + b49 = (s[43] << 2) | (s[42] >>> 30); + b40 = (s[5] << 30) | (s[4] >>> 2); + b41 = (s[4] << 30) | (s[5] >>> 2); + b22 = (s[14] << 6) | (s[15] >>> 26); + b23 = (s[15] << 6) | (s[14] >>> 26); + b4 = (s[25] << 11) | (s[24] >>> 21); + b5 = (s[24] << 11) | (s[25] >>> 21); + b36 = (s[34] << 15) | (s[35] >>> 17); + b37 = (s[35] << 15) | (s[34] >>> 17); + b18 = (s[45] << 29) | (s[44] >>> 3); + b19 = (s[44] << 29) | (s[45] >>> 3); + b10 = (s[6] << 28) | (s[7] >>> 4); + b11 = (s[7] << 28) | (s[6] >>> 4); + b42 = (s[17] << 23) | (s[16] >>> 9); + b43 = (s[16] << 23) | (s[17] >>> 9); + b24 = (s[26] << 25) | (s[27] >>> 7); + b25 = (s[27] << 25) | (s[26] >>> 7); + b6 = (s[36] << 21) | (s[37] >>> 11); + b7 = (s[37] << 21) | (s[36] >>> 11); + b38 = (s[47] << 24) | (s[46] >>> 8); + b39 = (s[46] << 24) | (s[47] >>> 8); + b30 = (s[8] << 27) | (s[9] >>> 5); + b31 = (s[9] << 27) | (s[8] >>> 5); + b12 = (s[18] << 20) | (s[19] >>> 12); + b13 = (s[19] << 20) | (s[18] >>> 12); + b44 = (s[29] << 7) | (s[28] >>> 25); + b45 = (s[28] << 7) | (s[29] >>> 25); + b26 = (s[38] << 8) | (s[39] >>> 24); + b27 = (s[39] << 8) | (s[38] >>> 24); + b8 = (s[48] << 14) | (s[49] >>> 18); + b9 = (s[49] << 14) | (s[48] >>> 18); + + s[0] = b0 ^ (~b2 & b4); + s[1] = b1 ^ (~b3 & b5); + s[10] = b10 ^ (~b12 & b14); + s[11] = b11 ^ (~b13 & b15); + s[20] = b20 ^ (~b22 & b24); + s[21] = b21 ^ (~b23 & b25); + s[30] = b30 ^ (~b32 & b34); + s[31] = b31 ^ (~b33 & b35); + s[40] = b40 ^ (~b42 & b44); + s[41] = b41 ^ (~b43 & b45); + s[2] = b2 ^ (~b4 & b6); + s[3] = b3 ^ (~b5 & b7); + s[12] = b12 ^ (~b14 & b16); + s[13] = b13 ^ (~b15 & b17); + s[22] = b22 ^ (~b24 & b26); + s[23] = b23 ^ (~b25 & b27); + s[32] = b32 ^ (~b34 & b36); + s[33] = b33 ^ (~b35 & b37); + s[42] = b42 ^ (~b44 & b46); + s[43] = b43 ^ (~b45 & b47); + s[4] = b4 ^ (~b6 & b8); + s[5] = b5 ^ (~b7 & b9); + s[14] = b14 ^ (~b16 & b18); + s[15] = b15 ^ (~b17 & b19); + s[24] = b24 ^ (~b26 & b28); + s[25] = b25 ^ (~b27 & b29); + s[34] = b34 ^ (~b36 & b38); + s[35] = b35 ^ (~b37 & b39); + s[44] = b44 ^ (~b46 & b48); + s[45] = b45 ^ (~b47 & b49); + s[6] = b6 ^ (~b8 & b0); + s[7] = b7 ^ (~b9 & b1); + s[16] = b16 ^ (~b18 & b10); + s[17] = b17 ^ (~b19 & b11); + s[26] = b26 ^ (~b28 & b20); + s[27] = b27 ^ (~b29 & b21); + s[36] = b36 ^ (~b38 & b30); + s[37] = b37 ^ (~b39 & b31); + s[46] = b46 ^ (~b48 & b40); + s[47] = b47 ^ (~b49 & b41); + s[8] = b8 ^ (~b0 & b2); + s[9] = b9 ^ (~b1 & b3); + s[18] = b18 ^ (~b10 & b12); + s[19] = b19 ^ (~b11 & b13); + s[28] = b28 ^ (~b20 & b22); + s[29] = b29 ^ (~b21 & b23); + s[38] = b38 ^ (~b30 & b32); + s[39] = b39 ^ (~b31 & b33); + s[48] = b48 ^ (~b40 & b42); + s[49] = b49 ^ (~b41 & b43); + + s[0] ^= RC[n]; + s[1] ^= RC[n + 1]; + } + }; + + if (COMMON_JS) { + module.exports = methods; + } else { + for (i = 0; i < methodNames.length; ++i) { + root[methodNames[i]] = methods[methodNames[i]]; + } + } + })(); +} (sha3$1)); + +var sha3 = sha3$1.exports; + +function keccak256(data) { + return '0x' + sha3.keccak_256(arrayify(data)); +} + +const version$6 = "rlp/5.7.0"; + +const logger$9 = new Logger(version$6); +function arrayifyInteger(value) { + const result = []; + while (value) { + result.unshift(value & 0xff); + value >>= 8; + } + return result; +} +function _encode(object) { + if (Array.isArray(object)) { + let payload = []; + object.forEach(function (child) { + payload = payload.concat(_encode(child)); + }); + if (payload.length <= 55) { + payload.unshift(0xc0 + payload.length); + return payload; + } + const length = arrayifyInteger(payload.length); + length.unshift(0xf7 + length.length); + return length.concat(payload); + } + if (!isBytesLike(object)) { + logger$9.throwArgumentError("RLP object must be BytesLike", "object", object); + } + const data = Array.prototype.slice.call(arrayify(object)); + if (data.length === 1 && data[0] <= 0x7f) { + return data; + } + else if (data.length <= 55) { + data.unshift(0x80 + data.length); + return data; + } + const length = arrayifyInteger(data.length); + length.unshift(0xb7 + length.length); + return length.concat(data); +} +function encode(object) { + return hexlify(_encode(object)); +} + +const version$5 = "address/5.7.0"; + +const logger$8 = new Logger(version$5); +function getChecksumAddress(address) { + if (!isHexString(address, 20)) { + logger$8.throwArgumentError("invalid address", "address", address); + } + address = address.toLowerCase(); + const chars = address.substring(2).split(""); + const expanded = new Uint8Array(40); + for (let i = 0; i < 40; i++) { + expanded[i] = chars[i].charCodeAt(0); + } + const hashed = arrayify(keccak256(expanded)); + for (let i = 0; i < 40; i += 2) { + if ((hashed[i >> 1] >> 4) >= 8) { + chars[i] = chars[i].toUpperCase(); + } + if ((hashed[i >> 1] & 0x0f) >= 8) { + chars[i + 1] = chars[i + 1].toUpperCase(); + } + } + return "0x" + chars.join(""); +} +// Shims for environments that are missing some required constants and functions +const MAX_SAFE_INTEGER = 0x1fffffffffffff; +function log10(x) { + if (Math.log10) { + return Math.log10(x); + } + return Math.log(x) / Math.LN10; +} +// See: https://en.wikipedia.org/wiki/International_Bank_Account_Number +// Create lookup table +const ibanLookup = {}; +for (let i = 0; i < 10; i++) { + ibanLookup[String(i)] = String(i); +} +for (let i = 0; i < 26; i++) { + ibanLookup[String.fromCharCode(65 + i)] = String(10 + i); +} +// How many decimal digits can we process? (for 64-bit float, this is 15) +const safeDigits = Math.floor(log10(MAX_SAFE_INTEGER)); +function ibanChecksum(address) { + address = address.toUpperCase(); + address = address.substring(4) + address.substring(0, 2) + "00"; + let expanded = address.split("").map((c) => { return ibanLookup[c]; }).join(""); + // Javascript can handle integers safely up to 15 (decimal) digits + while (expanded.length >= safeDigits) { + let block = expanded.substring(0, safeDigits); + expanded = parseInt(block, 10) % 97 + expanded.substring(block.length); + } + let checksum = String(98 - (parseInt(expanded, 10) % 97)); + while (checksum.length < 2) { + checksum = "0" + checksum; + } + return checksum; +} +function getAddress(address) { + let result = null; + if (typeof (address) !== "string") { + logger$8.throwArgumentError("invalid address", "address", address); + } + if (address.match(/^(0x)?[0-9a-fA-F]{40}$/)) { + // Missing the 0x prefix + if (address.substring(0, 2) !== "0x") { + address = "0x" + address; + } + result = getChecksumAddress(address); + // It is a checksummed address with a bad checksum + if (address.match(/([A-F].*[a-f])|([a-f].*[A-F])/) && result !== address) { + logger$8.throwArgumentError("bad address checksum", "address", address); + } + // Maybe ICAP? (we only support direct mode) + } + else if (address.match(/^XE[0-9]{2}[0-9A-Za-z]{30,31}$/)) { + // It is an ICAP address with a bad checksum + if (address.substring(2, 4) !== ibanChecksum(address)) { + logger$8.throwArgumentError("bad icap checksum", "address", address); + } + result = _base36To16(address.substring(4)); + while (result.length < 40) { + result = "0" + result; + } + result = getChecksumAddress("0x" + result); + } + else { + logger$8.throwArgumentError("invalid address", "address", address); + } + return result; +} +// http://ethereum.stackexchange.com/questions/760/how-is-the-address-of-an-ethereum-contract-computed +function getContractAddress(transaction) { + let from = null; + try { + from = getAddress(transaction.from); + } + catch (error) { + logger$8.throwArgumentError("missing from address", "transaction", transaction); + } + const nonce = stripZeros(arrayify(BigNumber.from(transaction.nonce).toHexString())); + return getAddress(hexDataSlice(keccak256(encode([from, nonce])), 12)); +} + +class AddressCoder extends Coder { + constructor(localName) { + super("address", "address", localName, false); + } + defaultValue() { + return "0x0000000000000000000000000000000000000000"; + } + encode(writer, value) { + try { + value = getAddress(value); + } + catch (error) { + this._throwError(error.message, value); + } + return writer.writeValue(value); + } + decode(reader) { + return getAddress(hexZeroPad(reader.readValue().toHexString(), 20)); + } +} + +// Clones the functionality of an existing Coder, but without a localName +class AnonymousCoder extends Coder { + constructor(coder) { + super(coder.name, coder.type, undefined, coder.dynamic); + this.coder = coder; + } + defaultValue() { + return this.coder.defaultValue(); + } + encode(writer, value) { + return this.coder.encode(writer, value); + } + decode(reader) { + return this.coder.decode(reader); + } +} + +const logger$7 = new Logger(version$7); +function pack(writer, coders, values) { + let arrayValues = null; + if (Array.isArray(values)) { + arrayValues = values; + } + else if (values && typeof (values) === "object") { + let unique = {}; + arrayValues = coders.map((coder) => { + const name = coder.localName; + if (!name) { + logger$7.throwError("cannot encode object for signature with missing names", Logger.errors.INVALID_ARGUMENT, { + argument: "values", + coder: coder, + value: values + }); + } + if (unique[name]) { + logger$7.throwError("cannot encode object for signature with duplicate names", Logger.errors.INVALID_ARGUMENT, { + argument: "values", + coder: coder, + value: values + }); + } + unique[name] = true; + return values[name]; + }); + } + else { + logger$7.throwArgumentError("invalid tuple value", "tuple", values); + } + if (coders.length !== arrayValues.length) { + logger$7.throwArgumentError("types/value length mismatch", "tuple", values); + } + let staticWriter = new Writer(writer.wordSize); + let dynamicWriter = new Writer(writer.wordSize); + let updateFuncs = []; + coders.forEach((coder, index) => { + let value = arrayValues[index]; + if (coder.dynamic) { + // Get current dynamic offset (for the future pointer) + let dynamicOffset = dynamicWriter.length; + // Encode the dynamic value into the dynamicWriter + coder.encode(dynamicWriter, value); + // Prepare to populate the correct offset once we are done + let updateFunc = staticWriter.writeUpdatableValue(); + updateFuncs.push((baseOffset) => { + updateFunc(baseOffset + dynamicOffset); + }); + } + else { + coder.encode(staticWriter, value); + } + }); + // Backfill all the dynamic offsets, now that we know the static length + updateFuncs.forEach((func) => { func(staticWriter.length); }); + let length = writer.appendWriter(staticWriter); + length += writer.appendWriter(dynamicWriter); + return length; +} +function unpack(reader, coders) { + let values = []; + // A reader anchored to this base + let baseReader = reader.subReader(0); + coders.forEach((coder) => { + let value = null; + if (coder.dynamic) { + let offset = reader.readValue(); + let offsetReader = baseReader.subReader(offset.toNumber()); + try { + value = coder.decode(offsetReader); + } + catch (error) { + // Cannot recover from this + if (error.code === Logger.errors.BUFFER_OVERRUN) { + throw error; + } + value = error; + value.baseType = coder.name; + value.name = coder.localName; + value.type = coder.type; + } + } + else { + try { + value = coder.decode(reader); + } + catch (error) { + // Cannot recover from this + if (error.code === Logger.errors.BUFFER_OVERRUN) { + throw error; + } + value = error; + value.baseType = coder.name; + value.name = coder.localName; + value.type = coder.type; + } + } + if (value != undefined) { + values.push(value); + } + }); + // We only output named properties for uniquely named coders + const uniqueNames = coders.reduce((accum, coder) => { + const name = coder.localName; + if (name) { + if (!accum[name]) { + accum[name] = 0; + } + accum[name]++; + } + return accum; + }, {}); + // Add any named parameters (i.e. tuples) + coders.forEach((coder, index) => { + let name = coder.localName; + if (!name || uniqueNames[name] !== 1) { + return; + } + if (name === "length") { + name = "_length"; + } + if (values[name] != null) { + return; + } + const value = values[index]; + if (value instanceof Error) { + Object.defineProperty(values, name, { + enumerable: true, + get: () => { throw value; } + }); + } + else { + values[name] = value; + } + }); + for (let i = 0; i < values.length; i++) { + const value = values[i]; + if (value instanceof Error) { + Object.defineProperty(values, i, { + enumerable: true, + get: () => { throw value; } + }); + } + } + return Object.freeze(values); +} +class ArrayCoder extends Coder { + constructor(coder, length, localName) { + const type = (coder.type + "[" + (length >= 0 ? length : "") + "]"); + const dynamic = (length === -1 || coder.dynamic); + super("array", type, localName, dynamic); + this.coder = coder; + this.length = length; + } + defaultValue() { + // Verifies the child coder is valid (even if the array is dynamic or 0-length) + const defaultChild = this.coder.defaultValue(); + const result = []; + for (let i = 0; i < this.length; i++) { + result.push(defaultChild); + } + return result; + } + encode(writer, value) { + if (!Array.isArray(value)) { + this._throwError("expected array value", value); + } + let count = this.length; + if (count === -1) { + count = value.length; + writer.writeValue(value.length); + } + logger$7.checkArgumentCount(value.length, count, "coder array" + (this.localName ? (" " + this.localName) : "")); + let coders = []; + for (let i = 0; i < value.length; i++) { + coders.push(this.coder); + } + return pack(writer, coders, value); + } + decode(reader) { + let count = this.length; + if (count === -1) { + count = reader.readValue().toNumber(); + // Check that there is *roughly* enough data to ensure + // stray random data is not being read as a length. Each + // slot requires at least 32 bytes for their value (or 32 + // bytes as a link to the data). This could use a much + // tighter bound, but we are erroring on the side of safety. + if (count * 32 > reader._data.length) { + logger$7.throwError("insufficient data length", Logger.errors.BUFFER_OVERRUN, { + length: reader._data.length, + count: count + }); + } + } + let coders = []; + for (let i = 0; i < count; i++) { + coders.push(new AnonymousCoder(this.coder)); + } + return reader.coerce(this.name, unpack(reader, coders)); + } +} + +class BooleanCoder extends Coder { + constructor(localName) { + super("bool", "bool", localName, false); + } + defaultValue() { + return false; + } + encode(writer, value) { + return writer.writeValue(value ? 1 : 0); + } + decode(reader) { + return reader.coerce(this.type, !reader.readValue().isZero()); + } +} + +class DynamicBytesCoder extends Coder { + constructor(type, localName) { + super(type, type, localName, true); + } + defaultValue() { + return "0x"; + } + encode(writer, value) { + value = arrayify(value); + let length = writer.writeValue(value.length); + length += writer.writeBytes(value); + return length; + } + decode(reader) { + return reader.readBytes(reader.readValue().toNumber(), true); + } +} +class BytesCoder extends DynamicBytesCoder { + constructor(localName) { + super("bytes", localName); + } + decode(reader) { + return reader.coerce(this.name, hexlify(super.decode(reader))); + } +} + +// @TODO: Merge this with bytes +class FixedBytesCoder extends Coder { + constructor(size, localName) { + let name = "bytes" + String(size); + super(name, name, localName, false); + this.size = size; + } + defaultValue() { + return ("0x0000000000000000000000000000000000000000000000000000000000000000").substring(0, 2 + this.size * 2); + } + encode(writer, value) { + let data = arrayify(value); + if (data.length !== this.size) { + this._throwError("incorrect data length", value); + } + return writer.writeBytes(data); + } + decode(reader) { + return reader.coerce(this.name, hexlify(reader.readBytes(this.size))); + } +} + +class NullCoder extends Coder { + constructor(localName) { + super("null", "", localName, false); + } + defaultValue() { + return null; + } + encode(writer, value) { + if (value != null) { + this._throwError("not null", value); + } + return writer.writeBytes([]); + } + decode(reader) { + reader.readBytes(0); + return reader.coerce(this.name, null); + } +} + +const NegativeOne = ( /*#__PURE__*/BigNumber.from(-1)); +const Zero = ( /*#__PURE__*/BigNumber.from(0)); +const One = ( /*#__PURE__*/BigNumber.from(1)); +const MaxUint256 = ( /*#__PURE__*/BigNumber.from("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")); + +class NumberCoder extends Coder { + constructor(size, signed, localName) { + const name = ((signed ? "int" : "uint") + (size * 8)); + super(name, name, localName, false); + this.size = size; + this.signed = signed; + } + defaultValue() { + return 0; + } + encode(writer, value) { + let v = BigNumber.from(value); + // Check bounds are safe for encoding + let maxUintValue = MaxUint256.mask(writer.wordSize * 8); + if (this.signed) { + let bounds = maxUintValue.mask(this.size * 8 - 1); + if (v.gt(bounds) || v.lt(bounds.add(One).mul(NegativeOne))) { + this._throwError("value out-of-bounds", value); + } + } + else if (v.lt(Zero) || v.gt(maxUintValue.mask(this.size * 8))) { + this._throwError("value out-of-bounds", value); + } + v = v.toTwos(this.size * 8).mask(this.size * 8); + if (this.signed) { + v = v.fromTwos(this.size * 8).toTwos(8 * writer.wordSize); + } + return writer.writeValue(v); + } + decode(reader) { + let value = reader.readValue().mask(this.size * 8); + if (this.signed) { + value = value.fromTwos(this.size * 8); + } + return reader.coerce(this.name, value); + } +} + +const version$4 = "strings/5.7.0"; + +const logger$6 = new Logger(version$4); +/////////////////////////////// +var UnicodeNormalizationForm; +(function (UnicodeNormalizationForm) { + UnicodeNormalizationForm["current"] = ""; + UnicodeNormalizationForm["NFC"] = "NFC"; + UnicodeNormalizationForm["NFD"] = "NFD"; + UnicodeNormalizationForm["NFKC"] = "NFKC"; + UnicodeNormalizationForm["NFKD"] = "NFKD"; +})(UnicodeNormalizationForm || (UnicodeNormalizationForm = {})); +var Utf8ErrorReason; +(function (Utf8ErrorReason) { + // A continuation byte was present where there was nothing to continue + // - offset = the index the codepoint began in + Utf8ErrorReason["UNEXPECTED_CONTINUE"] = "unexpected continuation byte"; + // An invalid (non-continuation) byte to start a UTF-8 codepoint was found + // - offset = the index the codepoint began in + Utf8ErrorReason["BAD_PREFIX"] = "bad codepoint prefix"; + // The string is too short to process the expected codepoint + // - offset = the index the codepoint began in + Utf8ErrorReason["OVERRUN"] = "string overrun"; + // A missing continuation byte was expected but not found + // - offset = the index the continuation byte was expected at + Utf8ErrorReason["MISSING_CONTINUE"] = "missing continuation byte"; + // The computed code point is outside the range for UTF-8 + // - offset = start of this codepoint + // - badCodepoint = the computed codepoint; outside the UTF-8 range + Utf8ErrorReason["OUT_OF_RANGE"] = "out of UTF-8 range"; + // UTF-8 strings may not contain UTF-16 surrogate pairs + // - offset = start of this codepoint + // - badCodepoint = the computed codepoint; inside the UTF-16 surrogate range + Utf8ErrorReason["UTF16_SURROGATE"] = "UTF-16 surrogate"; + // The string is an overlong representation + // - offset = start of this codepoint + // - badCodepoint = the computed codepoint; already bounds checked + Utf8ErrorReason["OVERLONG"] = "overlong representation"; +})(Utf8ErrorReason || (Utf8ErrorReason = {})); +function errorFunc(reason, offset, bytes, output, badCodepoint) { + return logger$6.throwArgumentError(`invalid codepoint at offset ${offset}; ${reason}`, "bytes", bytes); +} +function ignoreFunc(reason, offset, bytes, output, badCodepoint) { + // If there is an invalid prefix (including stray continuation), skip any additional continuation bytes + if (reason === Utf8ErrorReason.BAD_PREFIX || reason === Utf8ErrorReason.UNEXPECTED_CONTINUE) { + let i = 0; + for (let o = offset + 1; o < bytes.length; o++) { + if (bytes[o] >> 6 !== 0x02) { + break; + } + i++; + } + return i; + } + // This byte runs us past the end of the string, so just jump to the end + // (but the first byte was read already read and therefore skipped) + if (reason === Utf8ErrorReason.OVERRUN) { + return bytes.length - offset - 1; + } + // Nothing to skip + return 0; +} +function replaceFunc(reason, offset, bytes, output, badCodepoint) { + // Overlong representations are otherwise "valid" code points; just non-deistingtished + if (reason === Utf8ErrorReason.OVERLONG) { + output.push(badCodepoint); + return 0; + } + // Put the replacement character into the output + output.push(0xfffd); + // Otherwise, process as if ignoring errors + return ignoreFunc(reason, offset, bytes); +} +// Common error handing strategies +const Utf8ErrorFuncs = Object.freeze({ + error: errorFunc, + ignore: ignoreFunc, + replace: replaceFunc +}); +// http://stackoverflow.com/questions/13356493/decode-utf-8-with-javascript#13691499 +function getUtf8CodePoints(bytes, onError) { + if (onError == null) { + onError = Utf8ErrorFuncs.error; + } + bytes = arrayify(bytes); + const result = []; + let i = 0; + // Invalid bytes are ignored + while (i < bytes.length) { + const c = bytes[i++]; + // 0xxx xxxx + if (c >> 7 === 0) { + result.push(c); + continue; + } + // Multibyte; how many bytes left for this character? + let extraLength = null; + let overlongMask = null; + // 110x xxxx 10xx xxxx + if ((c & 0xe0) === 0xc0) { + extraLength = 1; + overlongMask = 0x7f; + // 1110 xxxx 10xx xxxx 10xx xxxx + } + else if ((c & 0xf0) === 0xe0) { + extraLength = 2; + overlongMask = 0x7ff; + // 1111 0xxx 10xx xxxx 10xx xxxx 10xx xxxx + } + else if ((c & 0xf8) === 0xf0) { + extraLength = 3; + overlongMask = 0xffff; + } + else { + if ((c & 0xc0) === 0x80) { + i += onError(Utf8ErrorReason.UNEXPECTED_CONTINUE, i - 1, bytes, result); + } + else { + i += onError(Utf8ErrorReason.BAD_PREFIX, i - 1, bytes, result); + } + continue; + } + // Do we have enough bytes in our data? + if (i - 1 + extraLength >= bytes.length) { + i += onError(Utf8ErrorReason.OVERRUN, i - 1, bytes, result); + continue; + } + // Remove the length prefix from the char + let res = c & ((1 << (8 - extraLength - 1)) - 1); + for (let j = 0; j < extraLength; j++) { + let nextChar = bytes[i]; + // Invalid continuation byte + if ((nextChar & 0xc0) != 0x80) { + i += onError(Utf8ErrorReason.MISSING_CONTINUE, i, bytes, result); + res = null; + break; + } + res = (res << 6) | (nextChar & 0x3f); + i++; + } + // See above loop for invalid continuation byte + if (res === null) { + continue; + } + // Maximum code point + if (res > 0x10ffff) { + i += onError(Utf8ErrorReason.OUT_OF_RANGE, i - 1 - extraLength, bytes, result, res); + continue; + } + // Reserved for UTF-16 surrogate halves + if (res >= 0xd800 && res <= 0xdfff) { + i += onError(Utf8ErrorReason.UTF16_SURROGATE, i - 1 - extraLength, bytes, result, res); + continue; + } + // Check for overlong sequences (more bytes than needed) + if (res <= overlongMask) { + i += onError(Utf8ErrorReason.OVERLONG, i - 1 - extraLength, bytes, result, res); + continue; + } + result.push(res); + } + return result; +} +// http://stackoverflow.com/questions/18729405/how-to-convert-utf8-string-to-byte-array +function toUtf8Bytes(str, form = UnicodeNormalizationForm.current) { + if (form != UnicodeNormalizationForm.current) { + logger$6.checkNormalize(); + str = str.normalize(form); + } + let result = []; + for (let i = 0; i < str.length; i++) { + const c = str.charCodeAt(i); + if (c < 0x80) { + result.push(c); + } + else if (c < 0x800) { + result.push((c >> 6) | 0xc0); + result.push((c & 0x3f) | 0x80); + } + else if ((c & 0xfc00) == 0xd800) { + i++; + const c2 = str.charCodeAt(i); + if (i >= str.length || (c2 & 0xfc00) !== 0xdc00) { + throw new Error("invalid utf-8 string"); + } + // Surrogate Pair + const pair = 0x10000 + ((c & 0x03ff) << 10) + (c2 & 0x03ff); + result.push((pair >> 18) | 0xf0); + result.push(((pair >> 12) & 0x3f) | 0x80); + result.push(((pair >> 6) & 0x3f) | 0x80); + result.push((pair & 0x3f) | 0x80); + } + else { + result.push((c >> 12) | 0xe0); + result.push(((c >> 6) & 0x3f) | 0x80); + result.push((c & 0x3f) | 0x80); + } + } + return arrayify(result); +} +function _toUtf8String(codePoints) { + return codePoints.map((codePoint) => { + if (codePoint <= 0xffff) { + return String.fromCharCode(codePoint); + } + codePoint -= 0x10000; + return String.fromCharCode((((codePoint >> 10) & 0x3ff) + 0xd800), ((codePoint & 0x3ff) + 0xdc00)); + }).join(""); +} +function toUtf8String(bytes, onError) { + return _toUtf8String(getUtf8CodePoints(bytes, onError)); +} + +class StringCoder extends DynamicBytesCoder { + constructor(localName) { + super("string", localName); + } + defaultValue() { + return ""; + } + encode(writer, value) { + return super.encode(writer, toUtf8Bytes(value)); + } + decode(reader) { + return toUtf8String(super.decode(reader)); + } +} + +class TupleCoder extends Coder { + constructor(coders, localName) { + let dynamic = false; + const types = []; + coders.forEach((coder) => { + if (coder.dynamic) { + dynamic = true; + } + types.push(coder.type); + }); + const type = ("tuple(" + types.join(",") + ")"); + super("tuple", type, localName, dynamic); + this.coders = coders; + } + defaultValue() { + const values = []; + this.coders.forEach((coder) => { + values.push(coder.defaultValue()); + }); + // We only output named properties for uniquely named coders + const uniqueNames = this.coders.reduce((accum, coder) => { + const name = coder.localName; + if (name) { + if (!accum[name]) { + accum[name] = 0; + } + accum[name]++; + } + return accum; + }, {}); + // Add named values + this.coders.forEach((coder, index) => { + let name = coder.localName; + if (!name || uniqueNames[name] !== 1) { + return; + } + if (name === "length") { + name = "_length"; + } + if (values[name] != null) { + return; + } + values[name] = values[index]; + }); + return Object.freeze(values); + } + encode(writer, value) { + return pack(writer, this.coders, value); + } + decode(reader) { + return reader.coerce(this.name, unpack(reader, this.coders)); + } +} + +const logger$5 = new Logger(version$7); +const paramTypeBytes = new RegExp(/^bytes([0-9]*)$/); +const paramTypeNumber = new RegExp(/^(u?int)([0-9]*)$/); +class AbiCoder { + constructor(coerceFunc) { + defineReadOnly(this, "coerceFunc", coerceFunc || null); + } + _getCoder(param) { + switch (param.baseType) { + case "address": + return new AddressCoder(param.name); + case "bool": + return new BooleanCoder(param.name); + case "string": + return new StringCoder(param.name); + case "bytes": + return new BytesCoder(param.name); + case "array": + return new ArrayCoder(this._getCoder(param.arrayChildren), param.arrayLength, param.name); + case "tuple": + return new TupleCoder((param.components || []).map((component) => { + return this._getCoder(component); + }), param.name); + case "": + return new NullCoder(param.name); + } + // u?int[0-9]* + let match = param.type.match(paramTypeNumber); + if (match) { + let size = parseInt(match[2] || "256"); + if (size === 0 || size > 256 || (size % 8) !== 0) { + logger$5.throwArgumentError("invalid " + match[1] + " bit length", "param", param); + } + return new NumberCoder(size / 8, (match[1] === "int"), param.name); + } + // bytes[0-9]+ + match = param.type.match(paramTypeBytes); + if (match) { + let size = parseInt(match[1]); + if (size === 0 || size > 32) { + logger$5.throwArgumentError("invalid bytes length", "param", param); + } + return new FixedBytesCoder(size, param.name); + } + return logger$5.throwArgumentError("invalid type", "type", param.type); + } + _getWordSize() { return 32; } + _getReader(data, allowLoose) { + return new Reader(data, this._getWordSize(), this.coerceFunc, allowLoose); + } + _getWriter() { + return new Writer(this._getWordSize()); + } + getDefaultValue(types) { + const coders = types.map((type) => this._getCoder(ParamType.from(type))); + const coder = new TupleCoder(coders, "_"); + return coder.defaultValue(); + } + encode(types, values) { + if (types.length !== values.length) { + logger$5.throwError("types/values length mismatch", Logger.errors.INVALID_ARGUMENT, { + count: { types: types.length, values: values.length }, + value: { types: types, values: values } + }); + } + const coders = types.map((type) => this._getCoder(ParamType.from(type))); + const coder = (new TupleCoder(coders, "_")); + const writer = this._getWriter(); + coder.encode(writer, values); + return writer.data; + } + decode(types, data, loose) { + const coders = types.map((type) => this._getCoder(ParamType.from(type))); + const coder = new TupleCoder(coders, "_"); + return coder.decode(this._getReader(arrayify(data), loose)); + } +} +const defaultAbiCoder = new AbiCoder(); + +function id(text) { + return keccak256(toUtf8Bytes(text)); +} + +const logger$4 = new Logger(version$7); +class LogDescription extends Description { +} +class TransactionDescription extends Description { +} +class ErrorDescription extends Description { +} +class Indexed extends Description { + static isIndexed(value) { + return !!(value && value._isIndexed); + } +} +const BuiltinErrors = { + "0x08c379a0": { signature: "Error(string)", name: "Error", inputs: ["string"], reason: true }, + "0x4e487b71": { signature: "Panic(uint256)", name: "Panic", inputs: ["uint256"] } +}; +function wrapAccessError(property, error) { + const wrap = new Error(`deferred error during ABI decoding triggered accessing ${property}`); + wrap.error = error; + return wrap; +} +/* +function checkNames(fragment: Fragment, type: "input" | "output", params: Array): void { + params.reduce((accum, param) => { + if (param.name) { + if (accum[param.name]) { + logger.throwArgumentError(`duplicate ${ type } parameter ${ JSON.stringify(param.name) } in ${ fragment.format("full") }`, "fragment", fragment); + } + accum[param.name] = true; + } + return accum; + }, <{ [ name: string ]: boolean }>{ }); +} +*/ +class Interface { + constructor(fragments) { + let abi = []; + if (typeof (fragments) === "string") { + abi = JSON.parse(fragments); + } + else { + abi = fragments; + } + defineReadOnly(this, "fragments", abi.map((fragment) => { + return Fragment.from(fragment); + }).filter((fragment) => (fragment != null))); + defineReadOnly(this, "_abiCoder", getStatic(new.target, "getAbiCoder")()); + defineReadOnly(this, "functions", {}); + defineReadOnly(this, "errors", {}); + defineReadOnly(this, "events", {}); + defineReadOnly(this, "structs", {}); + // Add all fragments by their signature + this.fragments.forEach((fragment) => { + let bucket = null; + switch (fragment.type) { + case "constructor": + if (this.deploy) { + logger$4.warn("duplicate definition - constructor"); + return; + } + //checkNames(fragment, "input", fragment.inputs); + defineReadOnly(this, "deploy", fragment); + return; + case "function": + //checkNames(fragment, "input", fragment.inputs); + //checkNames(fragment, "output", (fragment).outputs); + bucket = this.functions; + break; + case "event": + //checkNames(fragment, "input", fragment.inputs); + bucket = this.events; + break; + case "error": + bucket = this.errors; + break; + default: + return; + } + let signature = fragment.format(); + if (bucket[signature]) { + logger$4.warn("duplicate definition - " + signature); + return; + } + bucket[signature] = fragment; + }); + // If we do not have a constructor add a default + if (!this.deploy) { + defineReadOnly(this, "deploy", ConstructorFragment.from({ + payable: false, + type: "constructor" + })); + } + defineReadOnly(this, "_isInterface", true); + } + format(format) { + if (!format) { + format = FormatTypes.full; + } + if (format === FormatTypes.sighash) { + logger$4.throwArgumentError("interface does not support formatting sighash", "format", format); + } + const abi = this.fragments.map((fragment) => fragment.format(format)); + // We need to re-bundle the JSON fragments a bit + if (format === FormatTypes.json) { + return JSON.stringify(abi.map((j) => JSON.parse(j))); + } + return abi; + } + // Sub-classes can override these to handle other blockchains + static getAbiCoder() { + return defaultAbiCoder; + } + static getAddress(address) { + return getAddress(address); + } + static getSighash(fragment) { + return hexDataSlice(id(fragment.format()), 0, 4); + } + static getEventTopic(eventFragment) { + return id(eventFragment.format()); + } + // Find a function definition by any means necessary (unless it is ambiguous) + getFunction(nameOrSignatureOrSighash) { + if (isHexString(nameOrSignatureOrSighash)) { + for (const name in this.functions) { + if (nameOrSignatureOrSighash === this.getSighash(name)) { + return this.functions[name]; + } + } + logger$4.throwArgumentError("no matching function", "sighash", nameOrSignatureOrSighash); + } + // It is a bare name, look up the function (will return null if ambiguous) + if (nameOrSignatureOrSighash.indexOf("(") === -1) { + const name = nameOrSignatureOrSighash.trim(); + const matching = Object.keys(this.functions).filter((f) => (f.split("(" /* fix:) */)[0] === name)); + if (matching.length === 0) { + logger$4.throwArgumentError("no matching function", "name", name); + } + else if (matching.length > 1) { + logger$4.throwArgumentError("multiple matching functions", "name", name); + } + return this.functions[matching[0]]; + } + // Normalize the signature and lookup the function + const result = this.functions[FunctionFragment.fromString(nameOrSignatureOrSighash).format()]; + if (!result) { + logger$4.throwArgumentError("no matching function", "signature", nameOrSignatureOrSighash); + } + return result; + } + // Find an event definition by any means necessary (unless it is ambiguous) + getEvent(nameOrSignatureOrTopic) { + if (isHexString(nameOrSignatureOrTopic)) { + const topichash = nameOrSignatureOrTopic.toLowerCase(); + for (const name in this.events) { + if (topichash === this.getEventTopic(name)) { + return this.events[name]; + } + } + logger$4.throwArgumentError("no matching event", "topichash", topichash); + } + // It is a bare name, look up the function (will return null if ambiguous) + if (nameOrSignatureOrTopic.indexOf("(") === -1) { + const name = nameOrSignatureOrTopic.trim(); + const matching = Object.keys(this.events).filter((f) => (f.split("(" /* fix:) */)[0] === name)); + if (matching.length === 0) { + logger$4.throwArgumentError("no matching event", "name", name); + } + else if (matching.length > 1) { + logger$4.throwArgumentError("multiple matching events", "name", name); + } + return this.events[matching[0]]; + } + // Normalize the signature and lookup the function + const result = this.events[EventFragment.fromString(nameOrSignatureOrTopic).format()]; + if (!result) { + logger$4.throwArgumentError("no matching event", "signature", nameOrSignatureOrTopic); + } + return result; + } + // Find a function definition by any means necessary (unless it is ambiguous) + getError(nameOrSignatureOrSighash) { + if (isHexString(nameOrSignatureOrSighash)) { + const getSighash = getStatic(this.constructor, "getSighash"); + for (const name in this.errors) { + const error = this.errors[name]; + if (nameOrSignatureOrSighash === getSighash(error)) { + return this.errors[name]; + } + } + logger$4.throwArgumentError("no matching error", "sighash", nameOrSignatureOrSighash); + } + // It is a bare name, look up the function (will return null if ambiguous) + if (nameOrSignatureOrSighash.indexOf("(") === -1) { + const name = nameOrSignatureOrSighash.trim(); + const matching = Object.keys(this.errors).filter((f) => (f.split("(" /* fix:) */)[0] === name)); + if (matching.length === 0) { + logger$4.throwArgumentError("no matching error", "name", name); + } + else if (matching.length > 1) { + logger$4.throwArgumentError("multiple matching errors", "name", name); + } + return this.errors[matching[0]]; + } + // Normalize the signature and lookup the function + const result = this.errors[FunctionFragment.fromString(nameOrSignatureOrSighash).format()]; + if (!result) { + logger$4.throwArgumentError("no matching error", "signature", nameOrSignatureOrSighash); + } + return result; + } + // Get the sighash (the bytes4 selector) used by Solidity to identify a function + getSighash(fragment) { + if (typeof (fragment) === "string") { + try { + fragment = this.getFunction(fragment); + } + catch (error) { + try { + fragment = this.getError(fragment); + } + catch (_) { + throw error; + } + } + } + return getStatic(this.constructor, "getSighash")(fragment); + } + // Get the topic (the bytes32 hash) used by Solidity to identify an event + getEventTopic(eventFragment) { + if (typeof (eventFragment) === "string") { + eventFragment = this.getEvent(eventFragment); + } + return getStatic(this.constructor, "getEventTopic")(eventFragment); + } + _decodeParams(params, data) { + return this._abiCoder.decode(params, data); + } + _encodeParams(params, values) { + return this._abiCoder.encode(params, values); + } + encodeDeploy(values) { + return this._encodeParams(this.deploy.inputs, values || []); + } + decodeErrorResult(fragment, data) { + if (typeof (fragment) === "string") { + fragment = this.getError(fragment); + } + const bytes = arrayify(data); + if (hexlify(bytes.slice(0, 4)) !== this.getSighash(fragment)) { + logger$4.throwArgumentError(`data signature does not match error ${fragment.name}.`, "data", hexlify(bytes)); + } + return this._decodeParams(fragment.inputs, bytes.slice(4)); + } + encodeErrorResult(fragment, values) { + if (typeof (fragment) === "string") { + fragment = this.getError(fragment); + } + return hexlify(concat([ + this.getSighash(fragment), + this._encodeParams(fragment.inputs, values || []) + ])); + } + // Decode the data for a function call (e.g. tx.data) + decodeFunctionData(functionFragment, data) { + if (typeof (functionFragment) === "string") { + functionFragment = this.getFunction(functionFragment); + } + const bytes = arrayify(data); + if (hexlify(bytes.slice(0, 4)) !== this.getSighash(functionFragment)) { + logger$4.throwArgumentError(`data signature does not match function ${functionFragment.name}.`, "data", hexlify(bytes)); + } + return this._decodeParams(functionFragment.inputs, bytes.slice(4)); + } + // Encode the data for a function call (e.g. tx.data) + encodeFunctionData(functionFragment, values) { + if (typeof (functionFragment) === "string") { + functionFragment = this.getFunction(functionFragment); + } + return hexlify(concat([ + this.getSighash(functionFragment), + this._encodeParams(functionFragment.inputs, values || []) + ])); + } + // Decode the result from a function call (e.g. from eth_call) + decodeFunctionResult(functionFragment, data) { + if (typeof (functionFragment) === "string") { + functionFragment = this.getFunction(functionFragment); + } + let bytes = arrayify(data); + let reason = null; + let message = ""; + let errorArgs = null; + let errorName = null; + let errorSignature = null; + switch (bytes.length % this._abiCoder._getWordSize()) { + case 0: + try { + return this._abiCoder.decode(functionFragment.outputs, bytes); + } + catch (error) { } + break; + case 4: { + const selector = hexlify(bytes.slice(0, 4)); + const builtin = BuiltinErrors[selector]; + if (builtin) { + errorArgs = this._abiCoder.decode(builtin.inputs, bytes.slice(4)); + errorName = builtin.name; + errorSignature = builtin.signature; + if (builtin.reason) { + reason = errorArgs[0]; + } + if (errorName === "Error") { + message = `; VM Exception while processing transaction: reverted with reason string ${JSON.stringify(errorArgs[0])}`; + } + else if (errorName === "Panic") { + message = `; VM Exception while processing transaction: reverted with panic code ${errorArgs[0]}`; + } + } + else { + try { + const error = this.getError(selector); + errorArgs = this._abiCoder.decode(error.inputs, bytes.slice(4)); + errorName = error.name; + errorSignature = error.format(); + } + catch (error) { } + } + break; + } + } + return logger$4.throwError("call revert exception" + message, Logger.errors.CALL_EXCEPTION, { + method: functionFragment.format(), + data: hexlify(data), errorArgs, errorName, errorSignature, reason + }); + } + // Encode the result for a function call (e.g. for eth_call) + encodeFunctionResult(functionFragment, values) { + if (typeof (functionFragment) === "string") { + functionFragment = this.getFunction(functionFragment); + } + return hexlify(this._abiCoder.encode(functionFragment.outputs, values || [])); + } + // Create the filter for the event with search criteria (e.g. for eth_filterLog) + encodeFilterTopics(eventFragment, values) { + if (typeof (eventFragment) === "string") { + eventFragment = this.getEvent(eventFragment); + } + if (values.length > eventFragment.inputs.length) { + logger$4.throwError("too many arguments for " + eventFragment.format(), Logger.errors.UNEXPECTED_ARGUMENT, { + argument: "values", + value: values + }); + } + let topics = []; + if (!eventFragment.anonymous) { + topics.push(this.getEventTopic(eventFragment)); + } + const encodeTopic = (param, value) => { + if (param.type === "string") { + return id(value); + } + else if (param.type === "bytes") { + return keccak256(hexlify(value)); + } + if (param.type === "bool" && typeof (value) === "boolean") { + value = (value ? "0x01" : "0x00"); + } + if (param.type.match(/^u?int/)) { + value = BigNumber.from(value).toHexString(); + } + // Check addresses are valid + if (param.type === "address") { + this._abiCoder.encode(["address"], [value]); + } + return hexZeroPad(hexlify(value), 32); + }; + values.forEach((value, index) => { + let param = eventFragment.inputs[index]; + if (!param.indexed) { + if (value != null) { + logger$4.throwArgumentError("cannot filter non-indexed parameters; must be null", ("contract." + param.name), value); + } + return; + } + if (value == null) { + topics.push(null); + } + else if (param.baseType === "array" || param.baseType === "tuple") { + logger$4.throwArgumentError("filtering with tuples or arrays not supported", ("contract." + param.name), value); + } + else if (Array.isArray(value)) { + topics.push(value.map((value) => encodeTopic(param, value))); + } + else { + topics.push(encodeTopic(param, value)); + } + }); + // Trim off trailing nulls + while (topics.length && topics[topics.length - 1] === null) { + topics.pop(); + } + return topics; + } + encodeEventLog(eventFragment, values) { + if (typeof (eventFragment) === "string") { + eventFragment = this.getEvent(eventFragment); + } + const topics = []; + const dataTypes = []; + const dataValues = []; + if (!eventFragment.anonymous) { + topics.push(this.getEventTopic(eventFragment)); + } + if (values.length !== eventFragment.inputs.length) { + logger$4.throwArgumentError("event arguments/values mismatch", "values", values); + } + eventFragment.inputs.forEach((param, index) => { + const value = values[index]; + if (param.indexed) { + if (param.type === "string") { + topics.push(id(value)); + } + else if (param.type === "bytes") { + topics.push(keccak256(value)); + } + else if (param.baseType === "tuple" || param.baseType === "array") { + // @TODO + throw new Error("not implemented"); + } + else { + topics.push(this._abiCoder.encode([param.type], [value])); + } + } + else { + dataTypes.push(param); + dataValues.push(value); + } + }); + return { + data: this._abiCoder.encode(dataTypes, dataValues), + topics: topics + }; + } + // Decode a filter for the event and the search criteria + decodeEventLog(eventFragment, data, topics) { + if (typeof (eventFragment) === "string") { + eventFragment = this.getEvent(eventFragment); + } + if (topics != null && !eventFragment.anonymous) { + let topicHash = this.getEventTopic(eventFragment); + if (!isHexString(topics[0], 32) || topics[0].toLowerCase() !== topicHash) { + logger$4.throwError("fragment/topic mismatch", Logger.errors.INVALID_ARGUMENT, { argument: "topics[0]", expected: topicHash, value: topics[0] }); + } + topics = topics.slice(1); + } + let indexed = []; + let nonIndexed = []; + let dynamic = []; + eventFragment.inputs.forEach((param, index) => { + if (param.indexed) { + if (param.type === "string" || param.type === "bytes" || param.baseType === "tuple" || param.baseType === "array") { + indexed.push(ParamType.fromObject({ type: "bytes32", name: param.name })); + dynamic.push(true); + } + else { + indexed.push(param); + dynamic.push(false); + } + } + else { + nonIndexed.push(param); + dynamic.push(false); + } + }); + let resultIndexed = (topics != null) ? this._abiCoder.decode(indexed, concat(topics)) : null; + let resultNonIndexed = this._abiCoder.decode(nonIndexed, data, true); + let result = []; + let nonIndexedIndex = 0, indexedIndex = 0; + eventFragment.inputs.forEach((param, index) => { + if (param.indexed) { + if (resultIndexed == null) { + result[index] = new Indexed({ _isIndexed: true, hash: null }); + } + else if (dynamic[index]) { + result[index] = new Indexed({ _isIndexed: true, hash: resultIndexed[indexedIndex++] }); + } + else { + try { + result[index] = resultIndexed[indexedIndex++]; + } + catch (error) { + result[index] = error; + } + } + } + else { + try { + result[index] = resultNonIndexed[nonIndexedIndex++]; + } + catch (error) { + result[index] = error; + } + } + // Add the keyword argument if named and safe + if (param.name && result[param.name] == null) { + const value = result[index]; + // Make error named values throw on access + if (value instanceof Error) { + Object.defineProperty(result, param.name, { + enumerable: true, + get: () => { throw wrapAccessError(`property ${JSON.stringify(param.name)}`, value); } + }); + } + else { + result[param.name] = value; + } + } + }); + // Make all error indexed values throw on access + for (let i = 0; i < result.length; i++) { + const value = result[i]; + if (value instanceof Error) { + Object.defineProperty(result, i, { + enumerable: true, + get: () => { throw wrapAccessError(`index ${i}`, value); } + }); + } + } + return Object.freeze(result); + } + // Given a transaction, find the matching function fragment (if any) and + // determine all its properties and call parameters + parseTransaction(tx) { + let fragment = this.getFunction(tx.data.substring(0, 10).toLowerCase()); + if (!fragment) { + return null; + } + return new TransactionDescription({ + args: this._abiCoder.decode(fragment.inputs, "0x" + tx.data.substring(10)), + functionFragment: fragment, + name: fragment.name, + signature: fragment.format(), + sighash: this.getSighash(fragment), + value: BigNumber.from(tx.value || "0"), + }); + } + // @TODO + //parseCallResult(data: BytesLike): ?? + // Given an event log, find the matching event fragment (if any) and + // determine all its properties and values + parseLog(log) { + let fragment = this.getEvent(log.topics[0]); + if (!fragment || fragment.anonymous) { + return null; + } + // @TODO: If anonymous, and the only method, and the input count matches, should we parse? + // Probably not, because just because it is the only event in the ABI does + // not mean we have the full ABI; maybe just a fragment? + return new LogDescription({ + eventFragment: fragment, + name: fragment.name, + signature: fragment.format(), + topic: this.getEventTopic(fragment), + args: this.decodeEventLog(fragment, log.data, log.topics) + }); + } + parseError(data) { + const hexData = hexlify(data); + let fragment = this.getError(hexData.substring(0, 10).toLowerCase()); + if (!fragment) { + return null; + } + return new ErrorDescription({ + args: this._abiCoder.decode(fragment.inputs, "0x" + hexData.substring(10)), + errorFragment: fragment, + name: fragment.name, + signature: fragment.format(), + sighash: this.getSighash(fragment), + }); + } + /* + static from(value: Array | string | Interface) { + if (Interface.isInterface(value)) { + return value; + } + if (typeof(value) === "string") { + return new Interface(JSON.parse(value)); + } + return new Interface(value); + } + */ + static isInterface(value) { + return !!(value && value._isInterface); + } +} + +const version$3 = "abstract-provider/5.7.0"; + +var __awaiter$2 = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) { + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } + return new (P || (P = Promise))(function (resolve, reject) { + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } + step((generator = generator.apply(thisArg, _arguments || [])).next()); + }); +}; +const logger$3 = new Logger(version$3); +/////////////////////////////// +// Exported Abstracts +class Provider { + constructor() { + logger$3.checkAbstract(new.target, Provider); + defineReadOnly(this, "_isProvider", true); + } + getFeeData() { + return __awaiter$2(this, void 0, void 0, function* () { + const { block, gasPrice } = yield resolveProperties({ + block: this.getBlock("latest"), + gasPrice: this.getGasPrice().catch((error) => { + // @TODO: Why is this now failing on Calaveras? + //console.log(error); + return null; + }) + }); + let lastBaseFeePerGas = null, maxFeePerGas = null, maxPriorityFeePerGas = null; + if (block && block.baseFeePerGas) { + // We may want to compute this more accurately in the future, + // using the formula "check if the base fee is correct". + // See: https://eips.ethereum.org/EIPS/eip-1559 + lastBaseFeePerGas = block.baseFeePerGas; + maxPriorityFeePerGas = BigNumber.from("1500000000"); + maxFeePerGas = block.baseFeePerGas.mul(2).add(maxPriorityFeePerGas); + } + return { lastBaseFeePerGas, maxFeePerGas, maxPriorityFeePerGas, gasPrice }; + }); + } + // Alias for "on" + addListener(eventName, listener) { + return this.on(eventName, listener); + } + // Alias for "off" + removeListener(eventName, listener) { + return this.off(eventName, listener); + } + static isProvider(value) { + return !!(value && value._isProvider); + } +} + +const version$2 = "abstract-signer/5.7.0"; + +var __awaiter$1 = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) { + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } + return new (P || (P = Promise))(function (resolve, reject) { + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } + step((generator = generator.apply(thisArg, _arguments || [])).next()); + }); +}; +const logger$2 = new Logger(version$2); +const allowedTransactionKeys = [ + "accessList", "ccipReadEnabled", "chainId", "customData", "data", "from", "gasLimit", "gasPrice", "maxFeePerGas", "maxPriorityFeePerGas", "nonce", "to", "type", "value" +]; +const forwardErrors = [ + Logger.errors.INSUFFICIENT_FUNDS, + Logger.errors.NONCE_EXPIRED, + Logger.errors.REPLACEMENT_UNDERPRICED, +]; +class Signer { + /////////////////// + // Sub-classes MUST call super + constructor() { + logger$2.checkAbstract(new.target, Signer); + defineReadOnly(this, "_isSigner", true); + } + /////////////////// + // Sub-classes MAY override these + getBalance(blockTag) { + return __awaiter$1(this, void 0, void 0, function* () { + this._checkProvider("getBalance"); + return yield this.provider.getBalance(this.getAddress(), blockTag); + }); + } + getTransactionCount(blockTag) { + return __awaiter$1(this, void 0, void 0, function* () { + this._checkProvider("getTransactionCount"); + return yield this.provider.getTransactionCount(this.getAddress(), blockTag); + }); + } + // Populates "from" if unspecified, and estimates the gas for the transaction + estimateGas(transaction) { + return __awaiter$1(this, void 0, void 0, function* () { + this._checkProvider("estimateGas"); + const tx = yield resolveProperties(this.checkTransaction(transaction)); + return yield this.provider.estimateGas(tx); + }); + } + // Populates "from" if unspecified, and calls with the transaction + call(transaction, blockTag) { + return __awaiter$1(this, void 0, void 0, function* () { + this._checkProvider("call"); + const tx = yield resolveProperties(this.checkTransaction(transaction)); + return yield this.provider.call(tx, blockTag); + }); + } + // Populates all fields in a transaction, signs it and sends it to the network + sendTransaction(transaction) { + return __awaiter$1(this, void 0, void 0, function* () { + this._checkProvider("sendTransaction"); + const tx = yield this.populateTransaction(transaction); + const signedTx = yield this.signTransaction(tx); + return yield this.provider.sendTransaction(signedTx); + }); + } + getChainId() { + return __awaiter$1(this, void 0, void 0, function* () { + this._checkProvider("getChainId"); + const network = yield this.provider.getNetwork(); + return network.chainId; + }); + } + getGasPrice() { + return __awaiter$1(this, void 0, void 0, function* () { + this._checkProvider("getGasPrice"); + return yield this.provider.getGasPrice(); + }); + } + getFeeData() { + return __awaiter$1(this, void 0, void 0, function* () { + this._checkProvider("getFeeData"); + return yield this.provider.getFeeData(); + }); + } + resolveName(name) { + return __awaiter$1(this, void 0, void 0, function* () { + this._checkProvider("resolveName"); + return yield this.provider.resolveName(name); + }); + } + // Checks a transaction does not contain invalid keys and if + // no "from" is provided, populates it. + // - does NOT require a provider + // - adds "from" is not present + // - returns a COPY (safe to mutate the result) + // By default called from: (overriding these prevents it) + // - call + // - estimateGas + // - populateTransaction (and therefor sendTransaction) + checkTransaction(transaction) { + for (const key in transaction) { + if (allowedTransactionKeys.indexOf(key) === -1) { + logger$2.throwArgumentError("invalid transaction key: " + key, "transaction", transaction); + } + } + const tx = shallowCopy(transaction); + if (tx.from == null) { + tx.from = this.getAddress(); + } + else { + // Make sure any provided address matches this signer + tx.from = Promise.all([ + Promise.resolve(tx.from), + this.getAddress() + ]).then((result) => { + if (result[0].toLowerCase() !== result[1].toLowerCase()) { + logger$2.throwArgumentError("from address mismatch", "transaction", transaction); + } + return result[0]; + }); + } + return tx; + } + // Populates ALL keys for a transaction and checks that "from" matches + // this Signer. Should be used by sendTransaction but NOT by signTransaction. + // By default called from: (overriding these prevents it) + // - sendTransaction + // + // Notes: + // - We allow gasPrice for EIP-1559 as long as it matches maxFeePerGas + populateTransaction(transaction) { + return __awaiter$1(this, void 0, void 0, function* () { + const tx = yield resolveProperties(this.checkTransaction(transaction)); + if (tx.to != null) { + tx.to = Promise.resolve(tx.to).then((to) => __awaiter$1(this, void 0, void 0, function* () { + if (to == null) { + return null; + } + const address = yield this.resolveName(to); + if (address == null) { + logger$2.throwArgumentError("provided ENS name resolves to null", "tx.to", to); + } + return address; + })); + // Prevent this error from causing an UnhandledPromiseException + tx.to.catch((error) => { }); + } + // Do not allow mixing pre-eip-1559 and eip-1559 properties + const hasEip1559 = (tx.maxFeePerGas != null || tx.maxPriorityFeePerGas != null); + if (tx.gasPrice != null && (tx.type === 2 || hasEip1559)) { + logger$2.throwArgumentError("eip-1559 transaction do not support gasPrice", "transaction", transaction); + } + else if ((tx.type === 0 || tx.type === 1) && hasEip1559) { + logger$2.throwArgumentError("pre-eip-1559 transaction do not support maxFeePerGas/maxPriorityFeePerGas", "transaction", transaction); + } + if ((tx.type === 2 || tx.type == null) && (tx.maxFeePerGas != null && tx.maxPriorityFeePerGas != null)) { + // Fully-formed EIP-1559 transaction (skip getFeeData) + tx.type = 2; + } + else if (tx.type === 0 || tx.type === 1) { + // Explicit Legacy or EIP-2930 transaction + // Populate missing gasPrice + if (tx.gasPrice == null) { + tx.gasPrice = this.getGasPrice(); + } + } + else { + // We need to get fee data to determine things + const feeData = yield this.getFeeData(); + if (tx.type == null) { + // We need to auto-detect the intended type of this transaction... + if (feeData.maxFeePerGas != null && feeData.maxPriorityFeePerGas != null) { + // The network supports EIP-1559! + // Upgrade transaction from null to eip-1559 + tx.type = 2; + if (tx.gasPrice != null) { + // Using legacy gasPrice property on an eip-1559 network, + // so use gasPrice as both fee properties + const gasPrice = tx.gasPrice; + delete tx.gasPrice; + tx.maxFeePerGas = gasPrice; + tx.maxPriorityFeePerGas = gasPrice; + } + else { + // Populate missing fee data + if (tx.maxFeePerGas == null) { + tx.maxFeePerGas = feeData.maxFeePerGas; + } + if (tx.maxPriorityFeePerGas == null) { + tx.maxPriorityFeePerGas = feeData.maxPriorityFeePerGas; + } + } + } + else if (feeData.gasPrice != null) { + // Network doesn't support EIP-1559... + // ...but they are trying to use EIP-1559 properties + if (hasEip1559) { + logger$2.throwError("network does not support EIP-1559", Logger.errors.UNSUPPORTED_OPERATION, { + operation: "populateTransaction" + }); + } + // Populate missing fee data + if (tx.gasPrice == null) { + tx.gasPrice = feeData.gasPrice; + } + // Explicitly set untyped transaction to legacy + tx.type = 0; + } + else { + // getFeeData has failed us. + logger$2.throwError("failed to get consistent fee data", Logger.errors.UNSUPPORTED_OPERATION, { + operation: "signer.getFeeData" + }); + } + } + else if (tx.type === 2) { + // Explicitly using EIP-1559 + // Populate missing fee data + if (tx.maxFeePerGas == null) { + tx.maxFeePerGas = feeData.maxFeePerGas; + } + if (tx.maxPriorityFeePerGas == null) { + tx.maxPriorityFeePerGas = feeData.maxPriorityFeePerGas; + } + } + } + if (tx.nonce == null) { + tx.nonce = this.getTransactionCount("pending"); + } + if (tx.gasLimit == null) { + tx.gasLimit = this.estimateGas(tx).catch((error) => { + if (forwardErrors.indexOf(error.code) >= 0) { + throw error; + } + return logger$2.throwError("cannot estimate gas; transaction may fail or may require manual gas limit", Logger.errors.UNPREDICTABLE_GAS_LIMIT, { + error: error, + tx: tx + }); + }); + } + if (tx.chainId == null) { + tx.chainId = this.getChainId(); + } + else { + tx.chainId = Promise.all([ + Promise.resolve(tx.chainId), + this.getChainId() + ]).then((results) => { + if (results[1] !== 0 && results[0] !== results[1]) { + logger$2.throwArgumentError("chainId address mismatch", "transaction", transaction); + } + return results[0]; + }); + } + return yield resolveProperties(tx); + }); + } + /////////////////// + // Sub-classes SHOULD leave these alone + _checkProvider(operation) { + if (!this.provider) { + logger$2.throwError("missing provider", Logger.errors.UNSUPPORTED_OPERATION, { + operation: (operation || "_checkProvider") + }); + } + } + static isSigner(value) { + return !!(value && value._isSigner); + } +} +class VoidSigner extends Signer { + constructor(address, provider) { + super(); + defineReadOnly(this, "address", address); + defineReadOnly(this, "provider", provider || null); + } + getAddress() { + return Promise.resolve(this.address); + } + _fail(message, operation) { + return Promise.resolve().then(() => { + logger$2.throwError(message, Logger.errors.UNSUPPORTED_OPERATION, { operation: operation }); + }); + } + signMessage(message) { + return this._fail("VoidSigner cannot sign messages", "signMessage"); + } + signTransaction(transaction) { + return this._fail("VoidSigner cannot sign transactions", "signTransaction"); + } + _signTypedData(domain, types, value) { + return this._fail("VoidSigner cannot sign typed data", "signTypedData"); + } + connect(provider) { + return new VoidSigner(this.address, provider); + } +} + +const version$1 = "transactions/5.7.0"; + +const logger$1 = new Logger(version$1); +var TransactionTypes; +(function (TransactionTypes) { + TransactionTypes[TransactionTypes["legacy"] = 0] = "legacy"; + TransactionTypes[TransactionTypes["eip2930"] = 1] = "eip2930"; + TransactionTypes[TransactionTypes["eip1559"] = 2] = "eip1559"; +})(TransactionTypes || (TransactionTypes = {})); +function accessSetify(addr, storageKeys) { + return { + address: getAddress(addr), + storageKeys: (storageKeys || []).map((storageKey, index) => { + if (hexDataLength(storageKey) !== 32) { + logger$1.throwArgumentError("invalid access list storageKey", `accessList[${addr}:${index}]`, storageKey); + } + return storageKey.toLowerCase(); + }) + }; +} +function accessListify(value) { + if (Array.isArray(value)) { + return value.map((set, index) => { + if (Array.isArray(set)) { + if (set.length > 2) { + logger$1.throwArgumentError("access list expected to be [ address, storageKeys[] ]", `value[${index}]`, set); + } + return accessSetify(set[0], set[1]); + } + return accessSetify(set.address, set.storageKeys); + }); + } + const result = Object.keys(value).map((addr) => { + const storageKeys = value[addr].reduce((accum, storageKey) => { + accum[storageKey] = true; + return accum; + }, {}); + return accessSetify(addr, Object.keys(storageKeys).sort()); + }); + result.sort((a, b) => (a.address.localeCompare(b.address))); + return result; +} + +const version = "contracts/5.7.0"; + +var __awaiter = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) { + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } + return new (P || (P = Promise))(function (resolve, reject) { + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } + step((generator = generator.apply(thisArg, _arguments || [])).next()); + }); +}; +const logger = new Logger(version); +function resolveName(resolver, nameOrPromise) { + return __awaiter(this, void 0, void 0, function* () { + const name = yield nameOrPromise; + if (typeof (name) !== "string") { + logger.throwArgumentError("invalid address or ENS name", "name", name); + } + // If it is already an address, just use it (after adding checksum) + try { + return getAddress(name); + } + catch (error) { } + if (!resolver) { + logger.throwError("a provider or signer is needed to resolve ENS names", Logger.errors.UNSUPPORTED_OPERATION, { + operation: "resolveName" + }); + } + const address = yield resolver.resolveName(name); + if (address == null) { + logger.throwArgumentError("resolver or addr is not configured for ENS name", "name", name); + } + return address; + }); +} +// Recursively replaces ENS names with promises to resolve the name and resolves all properties +function resolveAddresses(resolver, value, paramType) { + return __awaiter(this, void 0, void 0, function* () { + if (Array.isArray(paramType)) { + return yield Promise.all(paramType.map((paramType, index) => { + return resolveAddresses(resolver, ((Array.isArray(value)) ? value[index] : value[paramType.name]), paramType); + })); + } + if (paramType.type === "address") { + return yield resolveName(resolver, value); + } + if (paramType.type === "tuple") { + return yield resolveAddresses(resolver, value, paramType.components); + } + if (paramType.baseType === "array") { + if (!Array.isArray(value)) { + return Promise.reject(logger.makeError("invalid value for array", Logger.errors.INVALID_ARGUMENT, { + argument: "value", + value + })); + } + return yield Promise.all(value.map((v) => resolveAddresses(resolver, v, paramType.arrayChildren))); + } + return value; + }); +} +function populateTransaction(contract, fragment, args) { + return __awaiter(this, void 0, void 0, function* () { + // If an extra argument is given, it is overrides + let overrides = {}; + if (args.length === fragment.inputs.length + 1 && typeof (args[args.length - 1]) === "object") { + overrides = shallowCopy(args.pop()); + } + // Make sure the parameter count matches + logger.checkArgumentCount(args.length, fragment.inputs.length, "passed to contract"); + // Populate "from" override (allow promises) + if (contract.signer) { + if (overrides.from) { + // Contracts with a Signer are from the Signer's frame-of-reference; + // but we allow overriding "from" if it matches the signer + overrides.from = resolveProperties({ + override: resolveName(contract.signer, overrides.from), + signer: contract.signer.getAddress() + }).then((check) => __awaiter(this, void 0, void 0, function* () { + if (getAddress(check.signer) !== check.override) { + logger.throwError("Contract with a Signer cannot override from", Logger.errors.UNSUPPORTED_OPERATION, { + operation: "overrides.from" + }); + } + return check.override; + })); + } + else { + overrides.from = contract.signer.getAddress(); + } + } + else if (overrides.from) { + overrides.from = resolveName(contract.provider, overrides.from); + //} else { + // Contracts without a signer can override "from", and if + // unspecified the zero address is used + //overrides.from = AddressZero; + } + // Wait for all dependencies to be resolved (prefer the signer over the provider) + const resolved = yield resolveProperties({ + args: resolveAddresses(contract.signer || contract.provider, args, fragment.inputs), + address: contract.resolvedAddress, + overrides: (resolveProperties(overrides) || {}) + }); + // The ABI coded transaction + const data = contract.interface.encodeFunctionData(fragment, resolved.args); + const tx = { + data: data, + to: resolved.address + }; + // Resolved Overrides + const ro = resolved.overrides; + // Populate simple overrides + if (ro.nonce != null) { + tx.nonce = BigNumber.from(ro.nonce).toNumber(); + } + if (ro.gasLimit != null) { + tx.gasLimit = BigNumber.from(ro.gasLimit); + } + if (ro.gasPrice != null) { + tx.gasPrice = BigNumber.from(ro.gasPrice); + } + if (ro.maxFeePerGas != null) { + tx.maxFeePerGas = BigNumber.from(ro.maxFeePerGas); + } + if (ro.maxPriorityFeePerGas != null) { + tx.maxPriorityFeePerGas = BigNumber.from(ro.maxPriorityFeePerGas); + } + if (ro.from != null) { + tx.from = ro.from; + } + if (ro.type != null) { + tx.type = ro.type; + } + if (ro.accessList != null) { + tx.accessList = accessListify(ro.accessList); + } + // If there was no "gasLimit" override, but the ABI specifies a default, use it + if (tx.gasLimit == null && fragment.gas != null) { + // Compute the intrinsic gas cost for this transaction + // @TODO: This is based on the yellow paper as of Petersburg; this is something + // we may wish to parameterize in v6 as part of the Network object. Since this + // is always a non-nil to address, we can ignore G_create, but may wish to add + // similar logic to the ContractFactory. + let intrinsic = 21000; + const bytes = arrayify(data); + for (let i = 0; i < bytes.length; i++) { + intrinsic += 4; + if (bytes[i]) { + intrinsic += 64; + } + } + tx.gasLimit = BigNumber.from(fragment.gas).add(intrinsic); + } + // Populate "value" override + if (ro.value) { + const roValue = BigNumber.from(ro.value); + if (!roValue.isZero() && !fragment.payable) { + logger.throwError("non-payable method cannot override value", Logger.errors.UNSUPPORTED_OPERATION, { + operation: "overrides.value", + value: overrides.value + }); + } + tx.value = roValue; + } + if (ro.customData) { + tx.customData = shallowCopy(ro.customData); + } + if (ro.ccipReadEnabled) { + tx.ccipReadEnabled = !!ro.ccipReadEnabled; + } + // Remove the overrides + delete overrides.nonce; + delete overrides.gasLimit; + delete overrides.gasPrice; + delete overrides.from; + delete overrides.value; + delete overrides.type; + delete overrides.accessList; + delete overrides.maxFeePerGas; + delete overrides.maxPriorityFeePerGas; + delete overrides.customData; + delete overrides.ccipReadEnabled; + // Make sure there are no stray overrides, which may indicate a + // typo or using an unsupported key. + const leftovers = Object.keys(overrides).filter((key) => (overrides[key] != null)); + if (leftovers.length) { + logger.throwError(`cannot override ${leftovers.map((l) => JSON.stringify(l)).join(",")}`, Logger.errors.UNSUPPORTED_OPERATION, { + operation: "overrides", + overrides: leftovers + }); + } + return tx; + }); +} +function buildPopulate(contract, fragment) { + return function (...args) { + return populateTransaction(contract, fragment, args); + }; +} +function buildEstimate(contract, fragment) { + const signerOrProvider = (contract.signer || contract.provider); + return function (...args) { + return __awaiter(this, void 0, void 0, function* () { + if (!signerOrProvider) { + logger.throwError("estimate require a provider or signer", Logger.errors.UNSUPPORTED_OPERATION, { + operation: "estimateGas" + }); + } + const tx = yield populateTransaction(contract, fragment, args); + return yield signerOrProvider.estimateGas(tx); + }); + }; +} +function addContractWait(contract, tx) { + const wait = tx.wait.bind(tx); + tx.wait = (confirmations) => { + return wait(confirmations).then((receipt) => { + receipt.events = receipt.logs.map((log) => { + let event = deepCopy(log); + let parsed = null; + try { + parsed = contract.interface.parseLog(log); + } + catch (e) { } + // Successfully parsed the event log; include it + if (parsed) { + event.args = parsed.args; + event.decode = (data, topics) => { + return contract.interface.decodeEventLog(parsed.eventFragment, data, topics); + }; + event.event = parsed.name; + event.eventSignature = parsed.signature; + } + // Useful operations + event.removeListener = () => { return contract.provider; }; + event.getBlock = () => { + return contract.provider.getBlock(receipt.blockHash); + }; + event.getTransaction = () => { + return contract.provider.getTransaction(receipt.transactionHash); + }; + event.getTransactionReceipt = () => { + return Promise.resolve(receipt); + }; + return event; + }); + return receipt; + }); + }; +} +function buildCall(contract, fragment, collapseSimple) { + const signerOrProvider = (contract.signer || contract.provider); + return function (...args) { + return __awaiter(this, void 0, void 0, function* () { + // Extract the "blockTag" override if present + let blockTag = undefined; + if (args.length === fragment.inputs.length + 1 && typeof (args[args.length - 1]) === "object") { + const overrides = shallowCopy(args.pop()); + if (overrides.blockTag != null) { + blockTag = yield overrides.blockTag; + } + delete overrides.blockTag; + args.push(overrides); + } + // If the contract was just deployed, wait until it is mined + if (contract.deployTransaction != null) { + yield contract._deployed(blockTag); + } + // Call a node and get the result + const tx = yield populateTransaction(contract, fragment, args); + const result = yield signerOrProvider.call(tx, blockTag); + try { + let value = contract.interface.decodeFunctionResult(fragment, result); + if (collapseSimple && fragment.outputs.length === 1) { + value = value[0]; + } + return value; + } + catch (error) { + if (error.code === Logger.errors.CALL_EXCEPTION) { + error.address = contract.address; + error.args = args; + error.transaction = tx; + } + throw error; + } + }); + }; +} +function buildSend(contract, fragment) { + return function (...args) { + return __awaiter(this, void 0, void 0, function* () { + if (!contract.signer) { + logger.throwError("sending a transaction requires a signer", Logger.errors.UNSUPPORTED_OPERATION, { + operation: "sendTransaction" + }); + } + // If the contract was just deployed, wait until it is mined + if (contract.deployTransaction != null) { + yield contract._deployed(); + } + const txRequest = yield populateTransaction(contract, fragment, args); + const tx = yield contract.signer.sendTransaction(txRequest); + // Tweak the tx.wait so the receipt has extra properties + addContractWait(contract, tx); + return tx; + }); + }; +} +function buildDefault(contract, fragment, collapseSimple) { + if (fragment.constant) { + return buildCall(contract, fragment, collapseSimple); + } + return buildSend(contract, fragment); +} +function getEventTag(filter) { + if (filter.address && (filter.topics == null || filter.topics.length === 0)) { + return "*"; + } + return (filter.address || "*") + "@" + (filter.topics ? filter.topics.map((topic) => { + if (Array.isArray(topic)) { + return topic.join("|"); + } + return topic; + }).join(":") : ""); +} +class RunningEvent { + constructor(tag, filter) { + defineReadOnly(this, "tag", tag); + defineReadOnly(this, "filter", filter); + this._listeners = []; + } + addListener(listener, once) { + this._listeners.push({ listener: listener, once: once }); + } + removeListener(listener) { + let done = false; + this._listeners = this._listeners.filter((item) => { + if (done || item.listener !== listener) { + return true; + } + done = true; + return false; + }); + } + removeAllListeners() { + this._listeners = []; + } + listeners() { + return this._listeners.map((i) => i.listener); + } + listenerCount() { + return this._listeners.length; + } + run(args) { + const listenerCount = this.listenerCount(); + this._listeners = this._listeners.filter((item) => { + const argsCopy = args.slice(); + // Call the callback in the next event loop + setTimeout(() => { + item.listener.apply(this, argsCopy); + }, 0); + // Reschedule it if it not "once" + return !(item.once); + }); + return listenerCount; + } + prepareEvent(event) { + } + // Returns the array that will be applied to an emit + getEmit(event) { + return [event]; + } +} +class ErrorRunningEvent extends RunningEvent { + constructor() { + super("error", null); + } +} +// @TODO Fragment should inherit Wildcard? and just override getEmit? +// or have a common abstract super class, with enough constructor +// options to configure both. +// A Fragment Event will populate all the properties that Wildcard +// will, and additionally dereference the arguments when emitting +class FragmentRunningEvent extends RunningEvent { + constructor(address, contractInterface, fragment, topics) { + const filter = { + address: address + }; + let topic = contractInterface.getEventTopic(fragment); + if (topics) { + if (topic !== topics[0]) { + logger.throwArgumentError("topic mismatch", "topics", topics); + } + filter.topics = topics.slice(); + } + else { + filter.topics = [topic]; + } + super(getEventTag(filter), filter); + defineReadOnly(this, "address", address); + defineReadOnly(this, "interface", contractInterface); + defineReadOnly(this, "fragment", fragment); + } + prepareEvent(event) { + super.prepareEvent(event); + event.event = this.fragment.name; + event.eventSignature = this.fragment.format(); + event.decode = (data, topics) => { + return this.interface.decodeEventLog(this.fragment, data, topics); + }; + try { + event.args = this.interface.decodeEventLog(this.fragment, event.data, event.topics); + } + catch (error) { + event.args = null; + event.decodeError = error; + } + } + getEmit(event) { + const errors = checkResultErrors(event.args); + if (errors.length) { + throw errors[0].error; + } + const args = (event.args || []).slice(); + args.push(event); + return args; + } +} +// A Wildcard Event will attempt to populate: +// - event The name of the event name +// - eventSignature The full signature of the event +// - decode A function to decode data and topics +// - args The decoded data and topics +class WildcardRunningEvent extends RunningEvent { + constructor(address, contractInterface) { + super("*", { address: address }); + defineReadOnly(this, "address", address); + defineReadOnly(this, "interface", contractInterface); + } + prepareEvent(event) { + super.prepareEvent(event); + try { + const parsed = this.interface.parseLog(event); + event.event = parsed.name; + event.eventSignature = parsed.signature; + event.decode = (data, topics) => { + return this.interface.decodeEventLog(parsed.eventFragment, data, topics); + }; + event.args = parsed.args; + } + catch (error) { + // No matching event + } + } +} +class BaseContract { + constructor(addressOrName, contractInterface, signerOrProvider) { + // @TODO: Maybe still check the addressOrName looks like a valid address or name? + //address = getAddress(address); + defineReadOnly(this, "interface", getStatic(new.target, "getInterface")(contractInterface)); + if (signerOrProvider == null) { + defineReadOnly(this, "provider", null); + defineReadOnly(this, "signer", null); + } + else if (Signer.isSigner(signerOrProvider)) { + defineReadOnly(this, "provider", signerOrProvider.provider || null); + defineReadOnly(this, "signer", signerOrProvider); + } + else if (Provider.isProvider(signerOrProvider)) { + defineReadOnly(this, "provider", signerOrProvider); + defineReadOnly(this, "signer", null); + } + else { + logger.throwArgumentError("invalid signer or provider", "signerOrProvider", signerOrProvider); + } + defineReadOnly(this, "callStatic", {}); + defineReadOnly(this, "estimateGas", {}); + defineReadOnly(this, "functions", {}); + defineReadOnly(this, "populateTransaction", {}); + defineReadOnly(this, "filters", {}); + { + const uniqueFilters = {}; + Object.keys(this.interface.events).forEach((eventSignature) => { + const event = this.interface.events[eventSignature]; + defineReadOnly(this.filters, eventSignature, (...args) => { + return { + address: this.address, + topics: this.interface.encodeFilterTopics(event, args) + }; + }); + if (!uniqueFilters[event.name]) { + uniqueFilters[event.name] = []; + } + uniqueFilters[event.name].push(eventSignature); + }); + Object.keys(uniqueFilters).forEach((name) => { + const filters = uniqueFilters[name]; + if (filters.length === 1) { + defineReadOnly(this.filters, name, this.filters[filters[0]]); + } + else { + logger.warn(`Duplicate definition of ${name} (${filters.join(", ")})`); + } + }); + } + defineReadOnly(this, "_runningEvents", {}); + defineReadOnly(this, "_wrappedEmits", {}); + if (addressOrName == null) { + logger.throwArgumentError("invalid contract address or ENS name", "addressOrName", addressOrName); + } + defineReadOnly(this, "address", addressOrName); + if (this.provider) { + defineReadOnly(this, "resolvedAddress", resolveName(this.provider, addressOrName)); + } + else { + try { + defineReadOnly(this, "resolvedAddress", Promise.resolve(getAddress(addressOrName))); + } + catch (error) { + // Without a provider, we cannot use ENS names + logger.throwError("provider is required to use ENS name as contract address", Logger.errors.UNSUPPORTED_OPERATION, { + operation: "new Contract" + }); + } + } + // Swallow bad ENS names to prevent Unhandled Exceptions + this.resolvedAddress.catch((e) => { }); + const uniqueNames = {}; + const uniqueSignatures = {}; + Object.keys(this.interface.functions).forEach((signature) => { + const fragment = this.interface.functions[signature]; + // Check that the signature is unique; if not the ABI generation has + // not been cleaned or may be incorrectly generated + if (uniqueSignatures[signature]) { + logger.warn(`Duplicate ABI entry for ${JSON.stringify(signature)}`); + return; + } + uniqueSignatures[signature] = true; + // Track unique names; we only expose bare named functions if they + // are ambiguous + { + const name = fragment.name; + if (!uniqueNames[`%${name}`]) { + uniqueNames[`%${name}`] = []; + } + uniqueNames[`%${name}`].push(signature); + } + if (this[signature] == null) { + defineReadOnly(this, signature, buildDefault(this, fragment, true)); + } + // We do not collapse simple calls on this bucket, which allows + // frameworks to safely use this without introspection as well as + // allows decoding error recovery. + if (this.functions[signature] == null) { + defineReadOnly(this.functions, signature, buildDefault(this, fragment, false)); + } + if (this.callStatic[signature] == null) { + defineReadOnly(this.callStatic, signature, buildCall(this, fragment, true)); + } + if (this.populateTransaction[signature] == null) { + defineReadOnly(this.populateTransaction, signature, buildPopulate(this, fragment)); + } + if (this.estimateGas[signature] == null) { + defineReadOnly(this.estimateGas, signature, buildEstimate(this, fragment)); + } + }); + Object.keys(uniqueNames).forEach((name) => { + // Ambiguous names to not get attached as bare names + const signatures = uniqueNames[name]; + if (signatures.length > 1) { + return; + } + // Strip off the leading "%" used for prototype protection + name = name.substring(1); + const signature = signatures[0]; + // If overwriting a member property that is null, swallow the error + try { + if (this[name] == null) { + defineReadOnly(this, name, this[signature]); + } + } + catch (e) { } + if (this.functions[name] == null) { + defineReadOnly(this.functions, name, this.functions[signature]); + } + if (this.callStatic[name] == null) { + defineReadOnly(this.callStatic, name, this.callStatic[signature]); + } + if (this.populateTransaction[name] == null) { + defineReadOnly(this.populateTransaction, name, this.populateTransaction[signature]); + } + if (this.estimateGas[name] == null) { + defineReadOnly(this.estimateGas, name, this.estimateGas[signature]); + } + }); + } + static getContractAddress(transaction) { + return getContractAddress(transaction); + } + static getInterface(contractInterface) { + if (Interface.isInterface(contractInterface)) { + return contractInterface; + } + return new Interface(contractInterface); + } + // @TODO: Allow timeout? + deployed() { + return this._deployed(); + } + _deployed(blockTag) { + if (!this._deployedPromise) { + // If we were just deployed, we know the transaction we should occur in + if (this.deployTransaction) { + this._deployedPromise = this.deployTransaction.wait().then(() => { + return this; + }); + } + else { + // @TODO: Once we allow a timeout to be passed in, we will wait + // up to that many blocks for getCode + // Otherwise, poll for our code to be deployed + this._deployedPromise = this.provider.getCode(this.address, blockTag).then((code) => { + if (code === "0x") { + logger.throwError("contract not deployed", Logger.errors.UNSUPPORTED_OPERATION, { + contractAddress: this.address, + operation: "getDeployed" + }); + } + return this; + }); + } + } + return this._deployedPromise; + } + // @TODO: + // estimateFallback(overrides?: TransactionRequest): Promise + // @TODO: + // estimateDeploy(bytecode: string, ...args): Promise + fallback(overrides) { + if (!this.signer) { + logger.throwError("sending a transactions require a signer", Logger.errors.UNSUPPORTED_OPERATION, { operation: "sendTransaction(fallback)" }); + } + const tx = shallowCopy(overrides || {}); + ["from", "to"].forEach(function (key) { + if (tx[key] == null) { + return; + } + logger.throwError("cannot override " + key, Logger.errors.UNSUPPORTED_OPERATION, { operation: key }); + }); + tx.to = this.resolvedAddress; + return this.deployed().then(() => { + return this.signer.sendTransaction(tx); + }); + } + // Reconnect to a different signer or provider + connect(signerOrProvider) { + if (typeof (signerOrProvider) === "string") { + signerOrProvider = new VoidSigner(signerOrProvider, this.provider); + } + const contract = new (this.constructor)(this.address, this.interface, signerOrProvider); + if (this.deployTransaction) { + defineReadOnly(contract, "deployTransaction", this.deployTransaction); + } + return contract; + } + // Re-attach to a different on-chain instance of this contract + attach(addressOrName) { + return new (this.constructor)(addressOrName, this.interface, this.signer || this.provider); + } + static isIndexed(value) { + return Indexed.isIndexed(value); + } + _normalizeRunningEvent(runningEvent) { + // Already have an instance of this event running; we can re-use it + if (this._runningEvents[runningEvent.tag]) { + return this._runningEvents[runningEvent.tag]; + } + return runningEvent; + } + _getRunningEvent(eventName) { + if (typeof (eventName) === "string") { + // Listen for "error" events (if your contract has an error event, include + // the full signature to bypass this special event keyword) + if (eventName === "error") { + return this._normalizeRunningEvent(new ErrorRunningEvent()); + } + // Listen for any event that is registered + if (eventName === "event") { + return this._normalizeRunningEvent(new RunningEvent("event", null)); + } + // Listen for any event + if (eventName === "*") { + return this._normalizeRunningEvent(new WildcardRunningEvent(this.address, this.interface)); + } + // Get the event Fragment (throws if ambiguous/unknown event) + const fragment = this.interface.getEvent(eventName); + return this._normalizeRunningEvent(new FragmentRunningEvent(this.address, this.interface, fragment)); + } + // We have topics to filter by... + if (eventName.topics && eventName.topics.length > 0) { + // Is it a known topichash? (throws if no matching topichash) + try { + const topic = eventName.topics[0]; + if (typeof (topic) !== "string") { + throw new Error("invalid topic"); // @TODO: May happen for anonymous events + } + const fragment = this.interface.getEvent(topic); + return this._normalizeRunningEvent(new FragmentRunningEvent(this.address, this.interface, fragment, eventName.topics)); + } + catch (error) { } + // Filter by the unknown topichash + const filter = { + address: this.address, + topics: eventName.topics + }; + return this._normalizeRunningEvent(new RunningEvent(getEventTag(filter), filter)); + } + return this._normalizeRunningEvent(new WildcardRunningEvent(this.address, this.interface)); + } + _checkRunningEvents(runningEvent) { + if (runningEvent.listenerCount() === 0) { + delete this._runningEvents[runningEvent.tag]; + // If we have a poller for this, remove it + const emit = this._wrappedEmits[runningEvent.tag]; + if (emit && runningEvent.filter) { + this.provider.off(runningEvent.filter, emit); + delete this._wrappedEmits[runningEvent.tag]; + } + } + } + // Subclasses can override this to gracefully recover + // from parse errors if they wish + _wrapEvent(runningEvent, log, listener) { + const event = deepCopy(log); + event.removeListener = () => { + if (!listener) { + return; + } + runningEvent.removeListener(listener); + this._checkRunningEvents(runningEvent); + }; + event.getBlock = () => { return this.provider.getBlock(log.blockHash); }; + event.getTransaction = () => { return this.provider.getTransaction(log.transactionHash); }; + event.getTransactionReceipt = () => { return this.provider.getTransactionReceipt(log.transactionHash); }; + // This may throw if the topics and data mismatch the signature + runningEvent.prepareEvent(event); + return event; + } + _addEventListener(runningEvent, listener, once) { + if (!this.provider) { + logger.throwError("events require a provider or a signer with a provider", Logger.errors.UNSUPPORTED_OPERATION, { operation: "once" }); + } + runningEvent.addListener(listener, once); + // Track this running event and its listeners (may already be there; but no hard in updating) + this._runningEvents[runningEvent.tag] = runningEvent; + // If we are not polling the provider, start polling + if (!this._wrappedEmits[runningEvent.tag]) { + const wrappedEmit = (log) => { + let event = this._wrapEvent(runningEvent, log, listener); + // Try to emit the result for the parameterized event... + if (event.decodeError == null) { + try { + const args = runningEvent.getEmit(event); + this.emit(runningEvent.filter, ...args); + } + catch (error) { + event.decodeError = error.error; + } + } + // Always emit "event" for fragment-base events + if (runningEvent.filter != null) { + this.emit("event", event); + } + // Emit "error" if there was an error + if (event.decodeError != null) { + this.emit("error", event.decodeError, event); + } + }; + this._wrappedEmits[runningEvent.tag] = wrappedEmit; + // Special events, like "error" do not have a filter + if (runningEvent.filter != null) { + this.provider.on(runningEvent.filter, wrappedEmit); + } + } + } + queryFilter(event, fromBlockOrBlockhash, toBlock) { + const runningEvent = this._getRunningEvent(event); + const filter = shallowCopy(runningEvent.filter); + if (typeof (fromBlockOrBlockhash) === "string" && isHexString(fromBlockOrBlockhash, 32)) { + if (toBlock != null) { + logger.throwArgumentError("cannot specify toBlock with blockhash", "toBlock", toBlock); + } + filter.blockHash = fromBlockOrBlockhash; + } + else { + filter.fromBlock = ((fromBlockOrBlockhash != null) ? fromBlockOrBlockhash : 0); + filter.toBlock = ((toBlock != null) ? toBlock : "latest"); + } + return this.provider.getLogs(filter).then((logs) => { + return logs.map((log) => this._wrapEvent(runningEvent, log, null)); + }); + } + on(event, listener) { + this._addEventListener(this._getRunningEvent(event), listener, false); + return this; + } + once(event, listener) { + this._addEventListener(this._getRunningEvent(event), listener, true); + return this; + } + emit(eventName, ...args) { + if (!this.provider) { + return false; + } + const runningEvent = this._getRunningEvent(eventName); + const result = (runningEvent.run(args) > 0); + // May have drained all the "once" events; check for living events + this._checkRunningEvents(runningEvent); + return result; + } + listenerCount(eventName) { + if (!this.provider) { + return 0; + } + if (eventName == null) { + return Object.keys(this._runningEvents).reduce((accum, key) => { + return accum + this._runningEvents[key].listenerCount(); + }, 0); + } + return this._getRunningEvent(eventName).listenerCount(); + } + listeners(eventName) { + if (!this.provider) { + return []; + } + if (eventName == null) { + const result = []; + for (let tag in this._runningEvents) { + this._runningEvents[tag].listeners().forEach((listener) => { + result.push(listener); + }); + } + return result; + } + return this._getRunningEvent(eventName).listeners(); + } + removeAllListeners(eventName) { + if (!this.provider) { + return this; + } + if (eventName == null) { + for (const tag in this._runningEvents) { + const runningEvent = this._runningEvents[tag]; + runningEvent.removeAllListeners(); + this._checkRunningEvents(runningEvent); + } + return this; + } + // Delete any listeners + const runningEvent = this._getRunningEvent(eventName); + runningEvent.removeAllListeners(); + this._checkRunningEvents(runningEvent); + return this; + } + off(eventName, listener) { + if (!this.provider) { + return this; + } + const runningEvent = this._getRunningEvent(eventName); + runningEvent.removeListener(listener); + this._checkRunningEvents(runningEvent); + return this; + } + removeListener(eventName, listener) { + return this.off(eventName, listener); + } +} +class Contract extends BaseContract { +} + +const getContractArguments = ({ contract, method, params })=>{ + let fragment = contract.interface.fragments.find((fragment) => { + return fragment.name == method + }); + + if(params instanceof Array) { + return params + } else if (params instanceof Object) { + return fragment.inputs.map((input) => { + return params[input.name] + }) + } +}; + +var estimateEVM = ({ provider, from, to, value, method, api, params }) => { + if(typeof api == "undefined"){ + return provider.estimateGas({ from, to, value }) + } else { + let contract = new Contract(to, api, provider); + let contractMethod = contract.estimateGas[method]; + let contractArguments = getContractArguments({ contract, method, params }); + if(contractArguments) { + return contractMethod(...contractArguments, { from, value }) + } else { + return contractMethod({ from, value }) + } + } +}; + +function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; } +let getCacheStore = () => { + if (getWindow()._cacheStore == undefined) { + resetCache(); + } + return getWindow()._cacheStore +}; + +let getPromiseStore = () => { + if (getWindow()._promiseStore == undefined) { + resetCache(); + } + return getWindow()._promiseStore +}; + +let resetCache = () => { + getWindow()._cacheStore = {}; + getWindow()._promiseStore = {}; + getWindow()._clientProviders = {}; +}; + +let set = function ({ key, value, expires }) { + getCacheStore()[key] = { + expiresAt: Date.now() + expires, + value, + }; +}; + +let get = function ({ key, expires }) { + let cachedEntry = getCacheStore()[key]; + if (_optionalChain([cachedEntry, 'optionalAccess', _ => _.expiresAt]) > Date.now()) { + return cachedEntry.value + } +}; + +let getPromise = function({ key }) { + return getPromiseStore()[key] +}; + +let setPromise = function({ key, promise }) { + getPromiseStore()[key] = promise; + return promise +}; + +let deletePromise = function({ key }) { + getPromiseStore()[key] = undefined; +}; + +let cache = function ({ call, key, expires = 0 }) { + return new Promise((resolve, reject)=>{ + let value; + key = JSON.stringify(key); + + // get existing promise (of a previous pending request asking for the exact same thing) + let existingPromise = getPromise({ key }); + if(existingPromise) { + return existingPromise + .then(resolve) + .catch(reject) + } + + setPromise({ key, promise: new Promise((resolveQueue, rejectQueue)=>{ + if (expires === 0) { + return call() + .then((value)=>{ + resolve(value); + resolveQueue(value); + }) + .catch((error)=>{ + reject(error); + rejectQueue(error); + }) + } + + // get cached value + value = get({ key, expires }); + if (value) { + resolve(value); + resolveQueue(value); + return value + } + + // set new cache value + call() + .then((value)=>{ + if (value) { + set({ key, value, expires }); + } + resolve(value); + resolveQueue(value); + }) + .catch((error)=>{ + reject(error); + rejectQueue(error); + }); + }) + }).then(()=>{ + deletePromise({ key }); + }).catch(()=>{ + deletePromise({ key }); + }); + }) +}; + +let estimate = async function ({ blockchain, from, to, value, method, api, params, cache: cache$1 }) { + if(!supported.includes(blockchain)) { throw 'Unknown blockchain: ' + blockchain } + if(typeof value == 'undefined') { value = '0'; } + + const provider = await getProvider(blockchain); + + return await cache({ + expires: cache$1 || 0, + key: [blockchain, from, to, value, method, params], + call: async()=>estimateEVM({ provider, from, to, value, method, api, params }) + }) +}; + +let accountInfo = async ({ address, api, method, params, provider, block }) => { + const info = await provider.getAccountInfo(new PublicKey(address)); + return api.decode(info.data) +}; + +let balance = ({ address, provider }) => { + return provider.getBalance(new PublicKey(address)) +}; + +var requestSolana = async ({ blockchain, address, api, method, params, block }) => { + const provider = await Solana.getProvider(blockchain); + + if(method == undefined || method === 'getAccountInfo') { + if(api == undefined) { + api = ACCOUNT_LAYOUT; + } + return accountInfo({ address, api, method, params, provider, block }) + } else if(method === 'getProgramAccounts') { + return provider.getProgramAccounts(new PublicKey(address), params).then((accounts)=>{ + if(api){ + return accounts.map((account)=>{ + account.data = api.decode(account.account.data); + return account + }) + } else { + return accounts + } + }) + } else if(method === 'getTokenAccountBalance') { + return provider.getTokenAccountBalance(new PublicKey(address)) + } else if (method === 'latestBlockNumber') { + return provider.getBlockHeight() + } else if (method === 'balance') { + return balance({ address, provider }) + } +}; + +var parseUrl = (url) => { + if (typeof url == 'object') { + return url + } + let deconstructed = url.match(/(?\w+):\/\/(?[\w\d]+)(\/(?[\w\d]+)*)?/); + + if(deconstructed.groups.part2 == undefined) { + if(deconstructed.groups.part1.match(/\d/)) { + return { + blockchain: deconstructed.groups.blockchain, + address: deconstructed.groups.part1 + } + } else { + return { + blockchain: deconstructed.groups.blockchain, + method: deconstructed.groups.part1 + } + } + } else { + return { + blockchain: deconstructed.groups.blockchain, + address: deconstructed.groups.part1, + method: deconstructed.groups.part2 + } + } +}; + +let request = async function (url, options) { + let { blockchain, address, method } = parseUrl(url); + let { api, params, cache: cache$1, block } = (typeof(url) == 'object' ? url : options) || {}; + + return await cache({ + expires: cache$1 || 0, + key: [blockchain, address, method, params, block], + call: async()=>{ + if(supported.evm.includes(blockchain)) ; else if(supported.solana.includes(blockchain)) { + + + return requestSolana({ blockchain, address, api, method, params, block }) + + + } else { + throw 'Unknown blockchain: ' + blockchain + } + } + }) +}; + +export { estimate, getProvider, request, resetCache, setProvider, setProviderEndpoints, simulate }; diff --git a/dist/umd/index.solana.js b/dist/umd/index.solana.js new file mode 100644 index 0000000..7ba0c35 --- /dev/null +++ b/dist/umd/index.solana.js @@ -0,0 +1,9152 @@ +(function (global, factory) { + typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@depay/solana-web3.js')) : + typeof define === 'function' && define.amd ? define(['exports', '@depay/solana-web3.js'], factory) : + (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.Web3Client = {}, global.SolanaWeb3js)); +})(this, (function (exports, solanaWeb3_js) { 'use strict'; + + class StaticJsonRpcSequentialProvider extends solanaWeb3_js.Connection { + + constructor(url, network, endpoints) { + super(url); + this._network = network; + this._endpoint = url; + this._endpoints = endpoints; + } + } + + let getWindow = () => { + if (typeof global == 'object') return global + return window + }; + + const ENDPOINTS = { + solana: ['https://solana-mainnet.phantom.app/YBPpkkN4g91xDiAnTE9r0RcMkjg0sKUIWvAfoFVJ', 'https://mainnet-beta.solflare.network', 'https://solana-mainnet.rpc.extrnode.com'] + }; + + const getProviders = ()=> { + if(getWindow()._clientProviders == undefined) { + getWindow()._clientProviders = {}; + } + return getWindow()._clientProviders + }; + + const setProvider$1 = (blockchain, provider)=> { + getProviders()[blockchain] = provider; + }; + + const setProviderEndpoints$1 = async (blockchain, endpoints)=> { + + let endpoint; + let window = getWindow(); + + if( + window.fetch == undefined || + (typeof process != 'undefined' && process['env'] && process['env']['NODE_ENV'] == 'test') || + (typeof window.cy != 'undefined') + ) { + endpoint = endpoints[0]; + } else { + + let responseTimes = await Promise.all(endpoints.map((endpoint)=>{ + return new Promise(async (resolve)=>{ + let timeout = 900; + let before = new Date().getTime(); + setTimeout(()=>resolve(timeout), timeout); + const response = await fetch(endpoint, { + method: 'POST', + headers: { + 'Accept': 'application/json', + 'Content-Type': 'application/json' + }, + body: JSON.stringify({ method: 'getIdentity', id: 1, jsonrpc: '2.0' }) + }); + if(!response.ok) { return resolve(999) } + let after = new Date().getTime(); + resolve(after-before); + }) + })); + + const fastestResponse = Math.min(...responseTimes); + const fastestIndex = responseTimes.indexOf(fastestResponse); + endpoint = endpoints[fastestIndex]; + } + + setProvider$1( + blockchain, + new StaticJsonRpcSequentialProvider(endpoint, blockchain, endpoints) + ); + }; + + const getProvider$1 = async (blockchain)=> { + + let providers = getProviders(); + if(providers && providers[blockchain]){ return providers[blockchain] } + + let window = getWindow(); + if(window._getProviderPromise && window._getProviderPromise[blockchain]) { return await window._getProviderPromise[blockchain] } + + if(!window._getProviderPromise){ window._getProviderPromise = {}; } + window._getProviderPromise[blockchain] = new Promise(async(resolve)=> { + await setProviderEndpoints$1(blockchain, ENDPOINTS[blockchain]); + resolve(getWindow()._clientProviders[blockchain]); + }); + + return await window._getProviderPromise[blockchain] + }; + + var Solana = { + getProvider: getProvider$1, + setProviderEndpoints: setProviderEndpoints$1, + setProvider: setProvider$1, + }; + + let supported = ['solana']; + supported.evm = []; + supported.solana = ['solana']; + + const getProvider = async (blockchain)=>{ + + if(supported.evm.includes(blockchain)) ; else if(supported.solana.includes(blockchain)) ; else { + throw 'Unknown blockchain: ' + blockchain + } + }; + + const setProvider = (blockchain, provider)=>{ + + if(supported.evm.includes(blockchain)) ; else if(supported.solana.includes(blockchain)) ; else { + throw 'Unknown blockchain: ' + blockchain + } + }; + + const setProviderEndpoints = (blockchain, endpoints)=>{ + + if(supported.evm.includes(blockchain)) ; else if(supported.solana.includes(blockchain)) ; else { + throw 'Unknown blockchain: ' + blockchain + } + }; + + function _optionalChain$1(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; } + let simulate = async function ({ blockchain, from, to, keys, api, params }) { + if(!supported.solana.includes(blockchain)) { throw `${blockchain} not supported for simulation!` } + + const data = solanaWeb3_js.Buffer.alloc(api.span); + api.encode(params, data); + + keys = keys.map((key)=>{ + return({...key, + pubkey: new solanaWeb3_js.PublicKey(key.pubkey) + }) + }); + + const instruction = new solanaWeb3_js.TransactionInstruction({ + programId: new solanaWeb3_js.PublicKey(to), + keys, + data + }); + + let transaction = new solanaWeb3_js.Transaction({ feePayer: new solanaWeb3_js.PublicKey(from) }); + transaction.add(instruction); + + let result; + try{ + const provider = await getProvider('solana'); + result = await provider.simulateTransaction(transaction); + } catch (error) { + console.log(error); + } + + return({ + logs: _optionalChain$1([result, 'optionalAccess', _ => _.value, 'optionalAccess', _2 => _2.logs]) + }) + }; + + var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {}; + + var bn = {exports: {}}; + + (function (module) { + (function (module, exports) { + + // Utils + function assert (val, msg) { + if (!val) throw new Error(msg || 'Assertion failed'); + } + + // Could use `inherits` module, but don't want to move from single file + // architecture yet. + function inherits (ctor, superCtor) { + ctor.super_ = superCtor; + var TempCtor = function () {}; + TempCtor.prototype = superCtor.prototype; + ctor.prototype = new TempCtor(); + ctor.prototype.constructor = ctor; + } + + // BN + + function BN (number, base, endian) { + if (BN.isBN(number)) { + return number; + } + + this.negative = 0; + this.words = null; + this.length = 0; + + // Reduction context + this.red = null; + + if (number !== null) { + if (base === 'le' || base === 'be') { + endian = base; + base = 10; + } + + this._init(number || 0, base || 10, endian || 'be'); + } + } + if (typeof module === 'object') { + module.exports = BN; + } else { + exports.BN = BN; + } + + BN.BN = BN; + BN.wordSize = 26; + + var Buffer; + try { + if (typeof window !== 'undefined' && typeof window.Buffer !== 'undefined') { + Buffer = window.Buffer; + } else { + Buffer = require('buffer').Buffer; + } + } catch (e) { + } + + BN.isBN = function isBN (num) { + if (num instanceof BN) { + return true; + } + + return num !== null && typeof num === 'object' && + num.constructor.wordSize === BN.wordSize && Array.isArray(num.words); + }; + + BN.max = function max (left, right) { + if (left.cmp(right) > 0) return left; + return right; + }; + + BN.min = function min (left, right) { + if (left.cmp(right) < 0) return left; + return right; + }; + + BN.prototype._init = function init (number, base, endian) { + if (typeof number === 'number') { + return this._initNumber(number, base, endian); + } + + if (typeof number === 'object') { + return this._initArray(number, base, endian); + } + + if (base === 'hex') { + base = 16; + } + assert(base === (base | 0) && base >= 2 && base <= 36); + + number = number.toString().replace(/\s+/g, ''); + var start = 0; + if (number[0] === '-') { + start++; + this.negative = 1; + } + + if (start < number.length) { + if (base === 16) { + this._parseHex(number, start, endian); + } else { + this._parseBase(number, base, start); + if (endian === 'le') { + this._initArray(this.toArray(), base, endian); + } + } + } + }; + + BN.prototype._initNumber = function _initNumber (number, base, endian) { + if (number < 0) { + this.negative = 1; + number = -number; + } + if (number < 0x4000000) { + this.words = [number & 0x3ffffff]; + this.length = 1; + } else if (number < 0x10000000000000) { + this.words = [ + number & 0x3ffffff, + (number / 0x4000000) & 0x3ffffff + ]; + this.length = 2; + } else { + assert(number < 0x20000000000000); // 2 ^ 53 (unsafe) + this.words = [ + number & 0x3ffffff, + (number / 0x4000000) & 0x3ffffff, + 1 + ]; + this.length = 3; + } + + if (endian !== 'le') return; + + // Reverse the bytes + this._initArray(this.toArray(), base, endian); + }; + + BN.prototype._initArray = function _initArray (number, base, endian) { + // Perhaps a Uint8Array + assert(typeof number.length === 'number'); + if (number.length <= 0) { + this.words = [0]; + this.length = 1; + return this; + } + + this.length = Math.ceil(number.length / 3); + this.words = new Array(this.length); + for (var i = 0; i < this.length; i++) { + this.words[i] = 0; + } + + var j, w; + var off = 0; + if (endian === 'be') { + for (i = number.length - 1, j = 0; i >= 0; i -= 3) { + w = number[i] | (number[i - 1] << 8) | (number[i - 2] << 16); + this.words[j] |= (w << off) & 0x3ffffff; + this.words[j + 1] = (w >>> (26 - off)) & 0x3ffffff; + off += 24; + if (off >= 26) { + off -= 26; + j++; + } + } + } else if (endian === 'le') { + for (i = 0, j = 0; i < number.length; i += 3) { + w = number[i] | (number[i + 1] << 8) | (number[i + 2] << 16); + this.words[j] |= (w << off) & 0x3ffffff; + this.words[j + 1] = (w >>> (26 - off)) & 0x3ffffff; + off += 24; + if (off >= 26) { + off -= 26; + j++; + } + } + } + return this._strip(); + }; + + function parseHex4Bits (string, index) { + var c = string.charCodeAt(index); + // '0' - '9' + if (c >= 48 && c <= 57) { + return c - 48; + // 'A' - 'F' + } else if (c >= 65 && c <= 70) { + return c - 55; + // 'a' - 'f' + } else if (c >= 97 && c <= 102) { + return c - 87; + } else { + assert(false, 'Invalid character in ' + string); + } + } + + function parseHexByte (string, lowerBound, index) { + var r = parseHex4Bits(string, index); + if (index - 1 >= lowerBound) { + r |= parseHex4Bits(string, index - 1) << 4; + } + return r; + } + + BN.prototype._parseHex = function _parseHex (number, start, endian) { + // Create possibly bigger array to ensure that it fits the number + this.length = Math.ceil((number.length - start) / 6); + this.words = new Array(this.length); + for (var i = 0; i < this.length; i++) { + this.words[i] = 0; + } + + // 24-bits chunks + var off = 0; + var j = 0; + + var w; + if (endian === 'be') { + for (i = number.length - 1; i >= start; i -= 2) { + w = parseHexByte(number, start, i) << off; + this.words[j] |= w & 0x3ffffff; + if (off >= 18) { + off -= 18; + j += 1; + this.words[j] |= w >>> 26; + } else { + off += 8; + } + } + } else { + var parseLength = number.length - start; + for (i = parseLength % 2 === 0 ? start + 1 : start; i < number.length; i += 2) { + w = parseHexByte(number, start, i) << off; + this.words[j] |= w & 0x3ffffff; + if (off >= 18) { + off -= 18; + j += 1; + this.words[j] |= w >>> 26; + } else { + off += 8; + } + } + } + + this._strip(); + }; + + function parseBase (str, start, end, mul) { + var r = 0; + var b = 0; + var len = Math.min(str.length, end); + for (var i = start; i < len; i++) { + var c = str.charCodeAt(i) - 48; + + r *= mul; + + // 'a' + if (c >= 49) { + b = c - 49 + 0xa; + + // 'A' + } else if (c >= 17) { + b = c - 17 + 0xa; + + // '0' - '9' + } else { + b = c; + } + assert(c >= 0 && b < mul, 'Invalid character'); + r += b; + } + return r; + } + + BN.prototype._parseBase = function _parseBase (number, base, start) { + // Initialize as zero + this.words = [0]; + this.length = 1; + + // Find length of limb in base + for (var limbLen = 0, limbPow = 1; limbPow <= 0x3ffffff; limbPow *= base) { + limbLen++; + } + limbLen--; + limbPow = (limbPow / base) | 0; + + var total = number.length - start; + var mod = total % limbLen; + var end = Math.min(total, total - mod) + start; + + var word = 0; + for (var i = start; i < end; i += limbLen) { + word = parseBase(number, i, i + limbLen, base); + + this.imuln(limbPow); + if (this.words[0] + word < 0x4000000) { + this.words[0] += word; + } else { + this._iaddn(word); + } + } + + if (mod !== 0) { + var pow = 1; + word = parseBase(number, i, number.length, base); + + for (i = 0; i < mod; i++) { + pow *= base; + } + + this.imuln(pow); + if (this.words[0] + word < 0x4000000) { + this.words[0] += word; + } else { + this._iaddn(word); + } + } + + this._strip(); + }; + + BN.prototype.copy = function copy (dest) { + dest.words = new Array(this.length); + for (var i = 0; i < this.length; i++) { + dest.words[i] = this.words[i]; + } + dest.length = this.length; + dest.negative = this.negative; + dest.red = this.red; + }; + + function move (dest, src) { + dest.words = src.words; + dest.length = src.length; + dest.negative = src.negative; + dest.red = src.red; + } + + BN.prototype._move = function _move (dest) { + move(dest, this); + }; + + BN.prototype.clone = function clone () { + var r = new BN(null); + this.copy(r); + return r; + }; + + BN.prototype._expand = function _expand (size) { + while (this.length < size) { + this.words[this.length++] = 0; + } + return this; + }; + + // Remove leading `0` from `this` + BN.prototype._strip = function strip () { + while (this.length > 1 && this.words[this.length - 1] === 0) { + this.length--; + } + return this._normSign(); + }; + + BN.prototype._normSign = function _normSign () { + // -0 = 0 + if (this.length === 1 && this.words[0] === 0) { + this.negative = 0; + } + return this; + }; + + // Check Symbol.for because not everywhere where Symbol defined + // See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol#Browser_compatibility + if (typeof Symbol !== 'undefined' && typeof Symbol.for === 'function') { + try { + BN.prototype[Symbol.for('nodejs.util.inspect.custom')] = inspect; + } catch (e) { + BN.prototype.inspect = inspect; + } + } else { + BN.prototype.inspect = inspect; + } + + function inspect () { + return (this.red ? ''; + } + + /* + + var zeros = []; + var groupSizes = []; + var groupBases = []; + + var s = ''; + var i = -1; + while (++i < BN.wordSize) { + zeros[i] = s; + s += '0'; + } + groupSizes[0] = 0; + groupSizes[1] = 0; + groupBases[0] = 0; + groupBases[1] = 0; + var base = 2 - 1; + while (++base < 36 + 1) { + var groupSize = 0; + var groupBase = 1; + while (groupBase < (1 << BN.wordSize) / base) { + groupBase *= base; + groupSize += 1; + } + groupSizes[base] = groupSize; + groupBases[base] = groupBase; + } + + */ + + var zeros = [ + '', + '0', + '00', + '000', + '0000', + '00000', + '000000', + '0000000', + '00000000', + '000000000', + '0000000000', + '00000000000', + '000000000000', + '0000000000000', + '00000000000000', + '000000000000000', + '0000000000000000', + '00000000000000000', + '000000000000000000', + '0000000000000000000', + '00000000000000000000', + '000000000000000000000', + '0000000000000000000000', + '00000000000000000000000', + '000000000000000000000000', + '0000000000000000000000000' + ]; + + var groupSizes = [ + 0, 0, + 25, 16, 12, 11, 10, 9, 8, + 8, 7, 7, 7, 7, 6, 6, + 6, 6, 6, 6, 6, 5, 5, + 5, 5, 5, 5, 5, 5, 5, + 5, 5, 5, 5, 5, 5, 5 + ]; + + var groupBases = [ + 0, 0, + 33554432, 43046721, 16777216, 48828125, 60466176, 40353607, 16777216, + 43046721, 10000000, 19487171, 35831808, 62748517, 7529536, 11390625, + 16777216, 24137569, 34012224, 47045881, 64000000, 4084101, 5153632, + 6436343, 7962624, 9765625, 11881376, 14348907, 17210368, 20511149, + 24300000, 28629151, 33554432, 39135393, 45435424, 52521875, 60466176 + ]; + + BN.prototype.toString = function toString (base, padding) { + base = base || 10; + padding = padding | 0 || 1; + + var out; + if (base === 16 || base === 'hex') { + out = ''; + var off = 0; + var carry = 0; + for (var i = 0; i < this.length; i++) { + var w = this.words[i]; + var word = (((w << off) | carry) & 0xffffff).toString(16); + carry = (w >>> (24 - off)) & 0xffffff; + off += 2; + if (off >= 26) { + off -= 26; + i--; + } + if (carry !== 0 || i !== this.length - 1) { + out = zeros[6 - word.length] + word + out; + } else { + out = word + out; + } + } + if (carry !== 0) { + out = carry.toString(16) + out; + } + while (out.length % padding !== 0) { + out = '0' + out; + } + if (this.negative !== 0) { + out = '-' + out; + } + return out; + } + + if (base === (base | 0) && base >= 2 && base <= 36) { + // var groupSize = Math.floor(BN.wordSize * Math.LN2 / Math.log(base)); + var groupSize = groupSizes[base]; + // var groupBase = Math.pow(base, groupSize); + var groupBase = groupBases[base]; + out = ''; + var c = this.clone(); + c.negative = 0; + while (!c.isZero()) { + var r = c.modrn(groupBase).toString(base); + c = c.idivn(groupBase); + + if (!c.isZero()) { + out = zeros[groupSize - r.length] + r + out; + } else { + out = r + out; + } + } + if (this.isZero()) { + out = '0' + out; + } + while (out.length % padding !== 0) { + out = '0' + out; + } + if (this.negative !== 0) { + out = '-' + out; + } + return out; + } + + assert(false, 'Base should be between 2 and 36'); + }; + + BN.prototype.toNumber = function toNumber () { + var ret = this.words[0]; + if (this.length === 2) { + ret += this.words[1] * 0x4000000; + } else if (this.length === 3 && this.words[2] === 0x01) { + // NOTE: at this stage it is known that the top bit is set + ret += 0x10000000000000 + (this.words[1] * 0x4000000); + } else if (this.length > 2) { + assert(false, 'Number can only safely store up to 53 bits'); + } + return (this.negative !== 0) ? -ret : ret; + }; + + BN.prototype.toJSON = function toJSON () { + return this.toString(16, 2); + }; + + if (Buffer) { + BN.prototype.toBuffer = function toBuffer (endian, length) { + return this.toArrayLike(Buffer, endian, length); + }; + } + + BN.prototype.toArray = function toArray (endian, length) { + return this.toArrayLike(Array, endian, length); + }; + + var allocate = function allocate (ArrayType, size) { + if (ArrayType.allocUnsafe) { + return ArrayType.allocUnsafe(size); + } + return new ArrayType(size); + }; + + BN.prototype.toArrayLike = function toArrayLike (ArrayType, endian, length) { + this._strip(); + + var byteLength = this.byteLength(); + var reqLength = length || Math.max(1, byteLength); + assert(byteLength <= reqLength, 'byte array longer than desired length'); + assert(reqLength > 0, 'Requested array length <= 0'); + + var res = allocate(ArrayType, reqLength); + var postfix = endian === 'le' ? 'LE' : 'BE'; + this['_toArrayLike' + postfix](res, byteLength); + return res; + }; + + BN.prototype._toArrayLikeLE = function _toArrayLikeLE (res, byteLength) { + var position = 0; + var carry = 0; + + for (var i = 0, shift = 0; i < this.length; i++) { + var word = (this.words[i] << shift) | carry; + + res[position++] = word & 0xff; + if (position < res.length) { + res[position++] = (word >> 8) & 0xff; + } + if (position < res.length) { + res[position++] = (word >> 16) & 0xff; + } + + if (shift === 6) { + if (position < res.length) { + res[position++] = (word >> 24) & 0xff; + } + carry = 0; + shift = 0; + } else { + carry = word >>> 24; + shift += 2; + } + } + + if (position < res.length) { + res[position++] = carry; + + while (position < res.length) { + res[position++] = 0; + } + } + }; + + BN.prototype._toArrayLikeBE = function _toArrayLikeBE (res, byteLength) { + var position = res.length - 1; + var carry = 0; + + for (var i = 0, shift = 0; i < this.length; i++) { + var word = (this.words[i] << shift) | carry; + + res[position--] = word & 0xff; + if (position >= 0) { + res[position--] = (word >> 8) & 0xff; + } + if (position >= 0) { + res[position--] = (word >> 16) & 0xff; + } + + if (shift === 6) { + if (position >= 0) { + res[position--] = (word >> 24) & 0xff; + } + carry = 0; + shift = 0; + } else { + carry = word >>> 24; + shift += 2; + } + } + + if (position >= 0) { + res[position--] = carry; + + while (position >= 0) { + res[position--] = 0; + } + } + }; + + if (Math.clz32) { + BN.prototype._countBits = function _countBits (w) { + return 32 - Math.clz32(w); + }; + } else { + BN.prototype._countBits = function _countBits (w) { + var t = w; + var r = 0; + if (t >= 0x1000) { + r += 13; + t >>>= 13; + } + if (t >= 0x40) { + r += 7; + t >>>= 7; + } + if (t >= 0x8) { + r += 4; + t >>>= 4; + } + if (t >= 0x02) { + r += 2; + t >>>= 2; + } + return r + t; + }; + } + + BN.prototype._zeroBits = function _zeroBits (w) { + // Short-cut + if (w === 0) return 26; + + var t = w; + var r = 0; + if ((t & 0x1fff) === 0) { + r += 13; + t >>>= 13; + } + if ((t & 0x7f) === 0) { + r += 7; + t >>>= 7; + } + if ((t & 0xf) === 0) { + r += 4; + t >>>= 4; + } + if ((t & 0x3) === 0) { + r += 2; + t >>>= 2; + } + if ((t & 0x1) === 0) { + r++; + } + return r; + }; + + // Return number of used bits in a BN + BN.prototype.bitLength = function bitLength () { + var w = this.words[this.length - 1]; + var hi = this._countBits(w); + return (this.length - 1) * 26 + hi; + }; + + function toBitArray (num) { + var w = new Array(num.bitLength()); + + for (var bit = 0; bit < w.length; bit++) { + var off = (bit / 26) | 0; + var wbit = bit % 26; + + w[bit] = (num.words[off] >>> wbit) & 0x01; + } + + return w; + } + + // Number of trailing zero bits + BN.prototype.zeroBits = function zeroBits () { + if (this.isZero()) return 0; + + var r = 0; + for (var i = 0; i < this.length; i++) { + var b = this._zeroBits(this.words[i]); + r += b; + if (b !== 26) break; + } + return r; + }; + + BN.prototype.byteLength = function byteLength () { + return Math.ceil(this.bitLength() / 8); + }; + + BN.prototype.toTwos = function toTwos (width) { + if (this.negative !== 0) { + return this.abs().inotn(width).iaddn(1); + } + return this.clone(); + }; + + BN.prototype.fromTwos = function fromTwos (width) { + if (this.testn(width - 1)) { + return this.notn(width).iaddn(1).ineg(); + } + return this.clone(); + }; + + BN.prototype.isNeg = function isNeg () { + return this.negative !== 0; + }; + + // Return negative clone of `this` + BN.prototype.neg = function neg () { + return this.clone().ineg(); + }; + + BN.prototype.ineg = function ineg () { + if (!this.isZero()) { + this.negative ^= 1; + } + + return this; + }; + + // Or `num` with `this` in-place + BN.prototype.iuor = function iuor (num) { + while (this.length < num.length) { + this.words[this.length++] = 0; + } + + for (var i = 0; i < num.length; i++) { + this.words[i] = this.words[i] | num.words[i]; + } + + return this._strip(); + }; + + BN.prototype.ior = function ior (num) { + assert((this.negative | num.negative) === 0); + return this.iuor(num); + }; + + // Or `num` with `this` + BN.prototype.or = function or (num) { + if (this.length > num.length) return this.clone().ior(num); + return num.clone().ior(this); + }; + + BN.prototype.uor = function uor (num) { + if (this.length > num.length) return this.clone().iuor(num); + return num.clone().iuor(this); + }; + + // And `num` with `this` in-place + BN.prototype.iuand = function iuand (num) { + // b = min-length(num, this) + var b; + if (this.length > num.length) { + b = num; + } else { + b = this; + } + + for (var i = 0; i < b.length; i++) { + this.words[i] = this.words[i] & num.words[i]; + } + + this.length = b.length; + + return this._strip(); + }; + + BN.prototype.iand = function iand (num) { + assert((this.negative | num.negative) === 0); + return this.iuand(num); + }; + + // And `num` with `this` + BN.prototype.and = function and (num) { + if (this.length > num.length) return this.clone().iand(num); + return num.clone().iand(this); + }; + + BN.prototype.uand = function uand (num) { + if (this.length > num.length) return this.clone().iuand(num); + return num.clone().iuand(this); + }; + + // Xor `num` with `this` in-place + BN.prototype.iuxor = function iuxor (num) { + // a.length > b.length + var a; + var b; + if (this.length > num.length) { + a = this; + b = num; + } else { + a = num; + b = this; + } + + for (var i = 0; i < b.length; i++) { + this.words[i] = a.words[i] ^ b.words[i]; + } + + if (this !== a) { + for (; i < a.length; i++) { + this.words[i] = a.words[i]; + } + } + + this.length = a.length; + + return this._strip(); + }; + + BN.prototype.ixor = function ixor (num) { + assert((this.negative | num.negative) === 0); + return this.iuxor(num); + }; + + // Xor `num` with `this` + BN.prototype.xor = function xor (num) { + if (this.length > num.length) return this.clone().ixor(num); + return num.clone().ixor(this); + }; + + BN.prototype.uxor = function uxor (num) { + if (this.length > num.length) return this.clone().iuxor(num); + return num.clone().iuxor(this); + }; + + // Not ``this`` with ``width`` bitwidth + BN.prototype.inotn = function inotn (width) { + assert(typeof width === 'number' && width >= 0); + + var bytesNeeded = Math.ceil(width / 26) | 0; + var bitsLeft = width % 26; + + // Extend the buffer with leading zeroes + this._expand(bytesNeeded); + + if (bitsLeft > 0) { + bytesNeeded--; + } + + // Handle complete words + for (var i = 0; i < bytesNeeded; i++) { + this.words[i] = ~this.words[i] & 0x3ffffff; + } + + // Handle the residue + if (bitsLeft > 0) { + this.words[i] = ~this.words[i] & (0x3ffffff >> (26 - bitsLeft)); + } + + // And remove leading zeroes + return this._strip(); + }; + + BN.prototype.notn = function notn (width) { + return this.clone().inotn(width); + }; + + // Set `bit` of `this` + BN.prototype.setn = function setn (bit, val) { + assert(typeof bit === 'number' && bit >= 0); + + var off = (bit / 26) | 0; + var wbit = bit % 26; + + this._expand(off + 1); + + if (val) { + this.words[off] = this.words[off] | (1 << wbit); + } else { + this.words[off] = this.words[off] & ~(1 << wbit); + } + + return this._strip(); + }; + + // Add `num` to `this` in-place + BN.prototype.iadd = function iadd (num) { + var r; + + // negative + positive + if (this.negative !== 0 && num.negative === 0) { + this.negative = 0; + r = this.isub(num); + this.negative ^= 1; + return this._normSign(); + + // positive + negative + } else if (this.negative === 0 && num.negative !== 0) { + num.negative = 0; + r = this.isub(num); + num.negative = 1; + return r._normSign(); + } + + // a.length > b.length + var a, b; + if (this.length > num.length) { + a = this; + b = num; + } else { + a = num; + b = this; + } + + var carry = 0; + for (var i = 0; i < b.length; i++) { + r = (a.words[i] | 0) + (b.words[i] | 0) + carry; + this.words[i] = r & 0x3ffffff; + carry = r >>> 26; + } + for (; carry !== 0 && i < a.length; i++) { + r = (a.words[i] | 0) + carry; + this.words[i] = r & 0x3ffffff; + carry = r >>> 26; + } + + this.length = a.length; + if (carry !== 0) { + this.words[this.length] = carry; + this.length++; + // Copy the rest of the words + } else if (a !== this) { + for (; i < a.length; i++) { + this.words[i] = a.words[i]; + } + } + + return this; + }; + + // Add `num` to `this` + BN.prototype.add = function add (num) { + var res; + if (num.negative !== 0 && this.negative === 0) { + num.negative = 0; + res = this.sub(num); + num.negative ^= 1; + return res; + } else if (num.negative === 0 && this.negative !== 0) { + this.negative = 0; + res = num.sub(this); + this.negative = 1; + return res; + } + + if (this.length > num.length) return this.clone().iadd(num); + + return num.clone().iadd(this); + }; + + // Subtract `num` from `this` in-place + BN.prototype.isub = function isub (num) { + // this - (-num) = this + num + if (num.negative !== 0) { + num.negative = 0; + var r = this.iadd(num); + num.negative = 1; + return r._normSign(); + + // -this - num = -(this + num) + } else if (this.negative !== 0) { + this.negative = 0; + this.iadd(num); + this.negative = 1; + return this._normSign(); + } + + // At this point both numbers are positive + var cmp = this.cmp(num); + + // Optimization - zeroify + if (cmp === 0) { + this.negative = 0; + this.length = 1; + this.words[0] = 0; + return this; + } + + // a > b + var a, b; + if (cmp > 0) { + a = this; + b = num; + } else { + a = num; + b = this; + } + + var carry = 0; + for (var i = 0; i < b.length; i++) { + r = (a.words[i] | 0) - (b.words[i] | 0) + carry; + carry = r >> 26; + this.words[i] = r & 0x3ffffff; + } + for (; carry !== 0 && i < a.length; i++) { + r = (a.words[i] | 0) + carry; + carry = r >> 26; + this.words[i] = r & 0x3ffffff; + } + + // Copy rest of the words + if (carry === 0 && i < a.length && a !== this) { + for (; i < a.length; i++) { + this.words[i] = a.words[i]; + } + } + + this.length = Math.max(this.length, i); + + if (a !== this) { + this.negative = 1; + } + + return this._strip(); + }; + + // Subtract `num` from `this` + BN.prototype.sub = function sub (num) { + return this.clone().isub(num); + }; + + function smallMulTo (self, num, out) { + out.negative = num.negative ^ self.negative; + var len = (self.length + num.length) | 0; + out.length = len; + len = (len - 1) | 0; + + // Peel one iteration (compiler can't do it, because of code complexity) + var a = self.words[0] | 0; + var b = num.words[0] | 0; + var r = a * b; + + var lo = r & 0x3ffffff; + var carry = (r / 0x4000000) | 0; + out.words[0] = lo; + + for (var k = 1; k < len; k++) { + // Sum all words with the same `i + j = k` and accumulate `ncarry`, + // note that ncarry could be >= 0x3ffffff + var ncarry = carry >>> 26; + var rword = carry & 0x3ffffff; + var maxJ = Math.min(k, num.length - 1); + for (var j = Math.max(0, k - self.length + 1); j <= maxJ; j++) { + var i = (k - j) | 0; + a = self.words[i] | 0; + b = num.words[j] | 0; + r = a * b + rword; + ncarry += (r / 0x4000000) | 0; + rword = r & 0x3ffffff; + } + out.words[k] = rword | 0; + carry = ncarry | 0; + } + if (carry !== 0) { + out.words[k] = carry | 0; + } else { + out.length--; + } + + return out._strip(); + } + + // TODO(indutny): it may be reasonable to omit it for users who don't need + // to work with 256-bit numbers, otherwise it gives 20% improvement for 256-bit + // multiplication (like elliptic secp256k1). + var comb10MulTo = function comb10MulTo (self, num, out) { + var a = self.words; + var b = num.words; + var o = out.words; + var c = 0; + var lo; + var mid; + var hi; + var a0 = a[0] | 0; + var al0 = a0 & 0x1fff; + var ah0 = a0 >>> 13; + var a1 = a[1] | 0; + var al1 = a1 & 0x1fff; + var ah1 = a1 >>> 13; + var a2 = a[2] | 0; + var al2 = a2 & 0x1fff; + var ah2 = a2 >>> 13; + var a3 = a[3] | 0; + var al3 = a3 & 0x1fff; + var ah3 = a3 >>> 13; + var a4 = a[4] | 0; + var al4 = a4 & 0x1fff; + var ah4 = a4 >>> 13; + var a5 = a[5] | 0; + var al5 = a5 & 0x1fff; + var ah5 = a5 >>> 13; + var a6 = a[6] | 0; + var al6 = a6 & 0x1fff; + var ah6 = a6 >>> 13; + var a7 = a[7] | 0; + var al7 = a7 & 0x1fff; + var ah7 = a7 >>> 13; + var a8 = a[8] | 0; + var al8 = a8 & 0x1fff; + var ah8 = a8 >>> 13; + var a9 = a[9] | 0; + var al9 = a9 & 0x1fff; + var ah9 = a9 >>> 13; + var b0 = b[0] | 0; + var bl0 = b0 & 0x1fff; + var bh0 = b0 >>> 13; + var b1 = b[1] | 0; + var bl1 = b1 & 0x1fff; + var bh1 = b1 >>> 13; + var b2 = b[2] | 0; + var bl2 = b2 & 0x1fff; + var bh2 = b2 >>> 13; + var b3 = b[3] | 0; + var bl3 = b3 & 0x1fff; + var bh3 = b3 >>> 13; + var b4 = b[4] | 0; + var bl4 = b4 & 0x1fff; + var bh4 = b4 >>> 13; + var b5 = b[5] | 0; + var bl5 = b5 & 0x1fff; + var bh5 = b5 >>> 13; + var b6 = b[6] | 0; + var bl6 = b6 & 0x1fff; + var bh6 = b6 >>> 13; + var b7 = b[7] | 0; + var bl7 = b7 & 0x1fff; + var bh7 = b7 >>> 13; + var b8 = b[8] | 0; + var bl8 = b8 & 0x1fff; + var bh8 = b8 >>> 13; + var b9 = b[9] | 0; + var bl9 = b9 & 0x1fff; + var bh9 = b9 >>> 13; + + out.negative = self.negative ^ num.negative; + out.length = 19; + /* k = 0 */ + lo = Math.imul(al0, bl0); + mid = Math.imul(al0, bh0); + mid = (mid + Math.imul(ah0, bl0)) | 0; + hi = Math.imul(ah0, bh0); + var w0 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; + c = (((hi + (mid >>> 13)) | 0) + (w0 >>> 26)) | 0; + w0 &= 0x3ffffff; + /* k = 1 */ + lo = Math.imul(al1, bl0); + mid = Math.imul(al1, bh0); + mid = (mid + Math.imul(ah1, bl0)) | 0; + hi = Math.imul(ah1, bh0); + lo = (lo + Math.imul(al0, bl1)) | 0; + mid = (mid + Math.imul(al0, bh1)) | 0; + mid = (mid + Math.imul(ah0, bl1)) | 0; + hi = (hi + Math.imul(ah0, bh1)) | 0; + var w1 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; + c = (((hi + (mid >>> 13)) | 0) + (w1 >>> 26)) | 0; + w1 &= 0x3ffffff; + /* k = 2 */ + lo = Math.imul(al2, bl0); + mid = Math.imul(al2, bh0); + mid = (mid + Math.imul(ah2, bl0)) | 0; + hi = Math.imul(ah2, bh0); + lo = (lo + Math.imul(al1, bl1)) | 0; + mid = (mid + Math.imul(al1, bh1)) | 0; + mid = (mid + Math.imul(ah1, bl1)) | 0; + hi = (hi + Math.imul(ah1, bh1)) | 0; + lo = (lo + Math.imul(al0, bl2)) | 0; + mid = (mid + Math.imul(al0, bh2)) | 0; + mid = (mid + Math.imul(ah0, bl2)) | 0; + hi = (hi + Math.imul(ah0, bh2)) | 0; + var w2 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; + c = (((hi + (mid >>> 13)) | 0) + (w2 >>> 26)) | 0; + w2 &= 0x3ffffff; + /* k = 3 */ + lo = Math.imul(al3, bl0); + mid = Math.imul(al3, bh0); + mid = (mid + Math.imul(ah3, bl0)) | 0; + hi = Math.imul(ah3, bh0); + lo = (lo + Math.imul(al2, bl1)) | 0; + mid = (mid + Math.imul(al2, bh1)) | 0; + mid = (mid + Math.imul(ah2, bl1)) | 0; + hi = (hi + Math.imul(ah2, bh1)) | 0; + lo = (lo + Math.imul(al1, bl2)) | 0; + mid = (mid + Math.imul(al1, bh2)) | 0; + mid = (mid + Math.imul(ah1, bl2)) | 0; + hi = (hi + Math.imul(ah1, bh2)) | 0; + lo = (lo + Math.imul(al0, bl3)) | 0; + mid = (mid + Math.imul(al0, bh3)) | 0; + mid = (mid + Math.imul(ah0, bl3)) | 0; + hi = (hi + Math.imul(ah0, bh3)) | 0; + var w3 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; + c = (((hi + (mid >>> 13)) | 0) + (w3 >>> 26)) | 0; + w3 &= 0x3ffffff; + /* k = 4 */ + lo = Math.imul(al4, bl0); + mid = Math.imul(al4, bh0); + mid = (mid + Math.imul(ah4, bl0)) | 0; + hi = Math.imul(ah4, bh0); + lo = (lo + Math.imul(al3, bl1)) | 0; + mid = (mid + Math.imul(al3, bh1)) | 0; + mid = (mid + Math.imul(ah3, bl1)) | 0; + hi = (hi + Math.imul(ah3, bh1)) | 0; + lo = (lo + Math.imul(al2, bl2)) | 0; + mid = (mid + Math.imul(al2, bh2)) | 0; + mid = (mid + Math.imul(ah2, bl2)) | 0; + hi = (hi + Math.imul(ah2, bh2)) | 0; + lo = (lo + Math.imul(al1, bl3)) | 0; + mid = (mid + Math.imul(al1, bh3)) | 0; + mid = (mid + Math.imul(ah1, bl3)) | 0; + hi = (hi + Math.imul(ah1, bh3)) | 0; + lo = (lo + Math.imul(al0, bl4)) | 0; + mid = (mid + Math.imul(al0, bh4)) | 0; + mid = (mid + Math.imul(ah0, bl4)) | 0; + hi = (hi + Math.imul(ah0, bh4)) | 0; + var w4 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; + c = (((hi + (mid >>> 13)) | 0) + (w4 >>> 26)) | 0; + w4 &= 0x3ffffff; + /* k = 5 */ + lo = Math.imul(al5, bl0); + mid = Math.imul(al5, bh0); + mid = (mid + Math.imul(ah5, bl0)) | 0; + hi = Math.imul(ah5, bh0); + lo = (lo + Math.imul(al4, bl1)) | 0; + mid = (mid + Math.imul(al4, bh1)) | 0; + mid = (mid + Math.imul(ah4, bl1)) | 0; + hi = (hi + Math.imul(ah4, bh1)) | 0; + lo = (lo + Math.imul(al3, bl2)) | 0; + mid = (mid + Math.imul(al3, bh2)) | 0; + mid = (mid + Math.imul(ah3, bl2)) | 0; + hi = (hi + Math.imul(ah3, bh2)) | 0; + lo = (lo + Math.imul(al2, bl3)) | 0; + mid = (mid + Math.imul(al2, bh3)) | 0; + mid = (mid + Math.imul(ah2, bl3)) | 0; + hi = (hi + Math.imul(ah2, bh3)) | 0; + lo = (lo + Math.imul(al1, bl4)) | 0; + mid = (mid + Math.imul(al1, bh4)) | 0; + mid = (mid + Math.imul(ah1, bl4)) | 0; + hi = (hi + Math.imul(ah1, bh4)) | 0; + lo = (lo + Math.imul(al0, bl5)) | 0; + mid = (mid + Math.imul(al0, bh5)) | 0; + mid = (mid + Math.imul(ah0, bl5)) | 0; + hi = (hi + Math.imul(ah0, bh5)) | 0; + var w5 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; + c = (((hi + (mid >>> 13)) | 0) + (w5 >>> 26)) | 0; + w5 &= 0x3ffffff; + /* k = 6 */ + lo = Math.imul(al6, bl0); + mid = Math.imul(al6, bh0); + mid = (mid + Math.imul(ah6, bl0)) | 0; + hi = Math.imul(ah6, bh0); + lo = (lo + Math.imul(al5, bl1)) | 0; + mid = (mid + Math.imul(al5, bh1)) | 0; + mid = (mid + Math.imul(ah5, bl1)) | 0; + hi = (hi + Math.imul(ah5, bh1)) | 0; + lo = (lo + Math.imul(al4, bl2)) | 0; + mid = (mid + Math.imul(al4, bh2)) | 0; + mid = (mid + Math.imul(ah4, bl2)) | 0; + hi = (hi + Math.imul(ah4, bh2)) | 0; + lo = (lo + Math.imul(al3, bl3)) | 0; + mid = (mid + Math.imul(al3, bh3)) | 0; + mid = (mid + Math.imul(ah3, bl3)) | 0; + hi = (hi + Math.imul(ah3, bh3)) | 0; + lo = (lo + Math.imul(al2, bl4)) | 0; + mid = (mid + Math.imul(al2, bh4)) | 0; + mid = (mid + Math.imul(ah2, bl4)) | 0; + hi = (hi + Math.imul(ah2, bh4)) | 0; + lo = (lo + Math.imul(al1, bl5)) | 0; + mid = (mid + Math.imul(al1, bh5)) | 0; + mid = (mid + Math.imul(ah1, bl5)) | 0; + hi = (hi + Math.imul(ah1, bh5)) | 0; + lo = (lo + Math.imul(al0, bl6)) | 0; + mid = (mid + Math.imul(al0, bh6)) | 0; + mid = (mid + Math.imul(ah0, bl6)) | 0; + hi = (hi + Math.imul(ah0, bh6)) | 0; + var w6 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; + c = (((hi + (mid >>> 13)) | 0) + (w6 >>> 26)) | 0; + w6 &= 0x3ffffff; + /* k = 7 */ + lo = Math.imul(al7, bl0); + mid = Math.imul(al7, bh0); + mid = (mid + Math.imul(ah7, bl0)) | 0; + hi = Math.imul(ah7, bh0); + lo = (lo + Math.imul(al6, bl1)) | 0; + mid = (mid + Math.imul(al6, bh1)) | 0; + mid = (mid + Math.imul(ah6, bl1)) | 0; + hi = (hi + Math.imul(ah6, bh1)) | 0; + lo = (lo + Math.imul(al5, bl2)) | 0; + mid = (mid + Math.imul(al5, bh2)) | 0; + mid = (mid + Math.imul(ah5, bl2)) | 0; + hi = (hi + Math.imul(ah5, bh2)) | 0; + lo = (lo + Math.imul(al4, bl3)) | 0; + mid = (mid + Math.imul(al4, bh3)) | 0; + mid = (mid + Math.imul(ah4, bl3)) | 0; + hi = (hi + Math.imul(ah4, bh3)) | 0; + lo = (lo + Math.imul(al3, bl4)) | 0; + mid = (mid + Math.imul(al3, bh4)) | 0; + mid = (mid + Math.imul(ah3, bl4)) | 0; + hi = (hi + Math.imul(ah3, bh4)) | 0; + lo = (lo + Math.imul(al2, bl5)) | 0; + mid = (mid + Math.imul(al2, bh5)) | 0; + mid = (mid + Math.imul(ah2, bl5)) | 0; + hi = (hi + Math.imul(ah2, bh5)) | 0; + lo = (lo + Math.imul(al1, bl6)) | 0; + mid = (mid + Math.imul(al1, bh6)) | 0; + mid = (mid + Math.imul(ah1, bl6)) | 0; + hi = (hi + Math.imul(ah1, bh6)) | 0; + lo = (lo + Math.imul(al0, bl7)) | 0; + mid = (mid + Math.imul(al0, bh7)) | 0; + mid = (mid + Math.imul(ah0, bl7)) | 0; + hi = (hi + Math.imul(ah0, bh7)) | 0; + var w7 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; + c = (((hi + (mid >>> 13)) | 0) + (w7 >>> 26)) | 0; + w7 &= 0x3ffffff; + /* k = 8 */ + lo = Math.imul(al8, bl0); + mid = Math.imul(al8, bh0); + mid = (mid + Math.imul(ah8, bl0)) | 0; + hi = Math.imul(ah8, bh0); + lo = (lo + Math.imul(al7, bl1)) | 0; + mid = (mid + Math.imul(al7, bh1)) | 0; + mid = (mid + Math.imul(ah7, bl1)) | 0; + hi = (hi + Math.imul(ah7, bh1)) | 0; + lo = (lo + Math.imul(al6, bl2)) | 0; + mid = (mid + Math.imul(al6, bh2)) | 0; + mid = (mid + Math.imul(ah6, bl2)) | 0; + hi = (hi + Math.imul(ah6, bh2)) | 0; + lo = (lo + Math.imul(al5, bl3)) | 0; + mid = (mid + Math.imul(al5, bh3)) | 0; + mid = (mid + Math.imul(ah5, bl3)) | 0; + hi = (hi + Math.imul(ah5, bh3)) | 0; + lo = (lo + Math.imul(al4, bl4)) | 0; + mid = (mid + Math.imul(al4, bh4)) | 0; + mid = (mid + Math.imul(ah4, bl4)) | 0; + hi = (hi + Math.imul(ah4, bh4)) | 0; + lo = (lo + Math.imul(al3, bl5)) | 0; + mid = (mid + Math.imul(al3, bh5)) | 0; + mid = (mid + Math.imul(ah3, bl5)) | 0; + hi = (hi + Math.imul(ah3, bh5)) | 0; + lo = (lo + Math.imul(al2, bl6)) | 0; + mid = (mid + Math.imul(al2, bh6)) | 0; + mid = (mid + Math.imul(ah2, bl6)) | 0; + hi = (hi + Math.imul(ah2, bh6)) | 0; + lo = (lo + Math.imul(al1, bl7)) | 0; + mid = (mid + Math.imul(al1, bh7)) | 0; + mid = (mid + Math.imul(ah1, bl7)) | 0; + hi = (hi + Math.imul(ah1, bh7)) | 0; + lo = (lo + Math.imul(al0, bl8)) | 0; + mid = (mid + Math.imul(al0, bh8)) | 0; + mid = (mid + Math.imul(ah0, bl8)) | 0; + hi = (hi + Math.imul(ah0, bh8)) | 0; + var w8 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; + c = (((hi + (mid >>> 13)) | 0) + (w8 >>> 26)) | 0; + w8 &= 0x3ffffff; + /* k = 9 */ + lo = Math.imul(al9, bl0); + mid = Math.imul(al9, bh0); + mid = (mid + Math.imul(ah9, bl0)) | 0; + hi = Math.imul(ah9, bh0); + lo = (lo + Math.imul(al8, bl1)) | 0; + mid = (mid + Math.imul(al8, bh1)) | 0; + mid = (mid + Math.imul(ah8, bl1)) | 0; + hi = (hi + Math.imul(ah8, bh1)) | 0; + lo = (lo + Math.imul(al7, bl2)) | 0; + mid = (mid + Math.imul(al7, bh2)) | 0; + mid = (mid + Math.imul(ah7, bl2)) | 0; + hi = (hi + Math.imul(ah7, bh2)) | 0; + lo = (lo + Math.imul(al6, bl3)) | 0; + mid = (mid + Math.imul(al6, bh3)) | 0; + mid = (mid + Math.imul(ah6, bl3)) | 0; + hi = (hi + Math.imul(ah6, bh3)) | 0; + lo = (lo + Math.imul(al5, bl4)) | 0; + mid = (mid + Math.imul(al5, bh4)) | 0; + mid = (mid + Math.imul(ah5, bl4)) | 0; + hi = (hi + Math.imul(ah5, bh4)) | 0; + lo = (lo + Math.imul(al4, bl5)) | 0; + mid = (mid + Math.imul(al4, bh5)) | 0; + mid = (mid + Math.imul(ah4, bl5)) | 0; + hi = (hi + Math.imul(ah4, bh5)) | 0; + lo = (lo + Math.imul(al3, bl6)) | 0; + mid = (mid + Math.imul(al3, bh6)) | 0; + mid = (mid + Math.imul(ah3, bl6)) | 0; + hi = (hi + Math.imul(ah3, bh6)) | 0; + lo = (lo + Math.imul(al2, bl7)) | 0; + mid = (mid + Math.imul(al2, bh7)) | 0; + mid = (mid + Math.imul(ah2, bl7)) | 0; + hi = (hi + Math.imul(ah2, bh7)) | 0; + lo = (lo + Math.imul(al1, bl8)) | 0; + mid = (mid + Math.imul(al1, bh8)) | 0; + mid = (mid + Math.imul(ah1, bl8)) | 0; + hi = (hi + Math.imul(ah1, bh8)) | 0; + lo = (lo + Math.imul(al0, bl9)) | 0; + mid = (mid + Math.imul(al0, bh9)) | 0; + mid = (mid + Math.imul(ah0, bl9)) | 0; + hi = (hi + Math.imul(ah0, bh9)) | 0; + var w9 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; + c = (((hi + (mid >>> 13)) | 0) + (w9 >>> 26)) | 0; + w9 &= 0x3ffffff; + /* k = 10 */ + lo = Math.imul(al9, bl1); + mid = Math.imul(al9, bh1); + mid = (mid + Math.imul(ah9, bl1)) | 0; + hi = Math.imul(ah9, bh1); + lo = (lo + Math.imul(al8, bl2)) | 0; + mid = (mid + Math.imul(al8, bh2)) | 0; + mid = (mid + Math.imul(ah8, bl2)) | 0; + hi = (hi + Math.imul(ah8, bh2)) | 0; + lo = (lo + Math.imul(al7, bl3)) | 0; + mid = (mid + Math.imul(al7, bh3)) | 0; + mid = (mid + Math.imul(ah7, bl3)) | 0; + hi = (hi + Math.imul(ah7, bh3)) | 0; + lo = (lo + Math.imul(al6, bl4)) | 0; + mid = (mid + Math.imul(al6, bh4)) | 0; + mid = (mid + Math.imul(ah6, bl4)) | 0; + hi = (hi + Math.imul(ah6, bh4)) | 0; + lo = (lo + Math.imul(al5, bl5)) | 0; + mid = (mid + Math.imul(al5, bh5)) | 0; + mid = (mid + Math.imul(ah5, bl5)) | 0; + hi = (hi + Math.imul(ah5, bh5)) | 0; + lo = (lo + Math.imul(al4, bl6)) | 0; + mid = (mid + Math.imul(al4, bh6)) | 0; + mid = (mid + Math.imul(ah4, bl6)) | 0; + hi = (hi + Math.imul(ah4, bh6)) | 0; + lo = (lo + Math.imul(al3, bl7)) | 0; + mid = (mid + Math.imul(al3, bh7)) | 0; + mid = (mid + Math.imul(ah3, bl7)) | 0; + hi = (hi + Math.imul(ah3, bh7)) | 0; + lo = (lo + Math.imul(al2, bl8)) | 0; + mid = (mid + Math.imul(al2, bh8)) | 0; + mid = (mid + Math.imul(ah2, bl8)) | 0; + hi = (hi + Math.imul(ah2, bh8)) | 0; + lo = (lo + Math.imul(al1, bl9)) | 0; + mid = (mid + Math.imul(al1, bh9)) | 0; + mid = (mid + Math.imul(ah1, bl9)) | 0; + hi = (hi + Math.imul(ah1, bh9)) | 0; + var w10 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; + c = (((hi + (mid >>> 13)) | 0) + (w10 >>> 26)) | 0; + w10 &= 0x3ffffff; + /* k = 11 */ + lo = Math.imul(al9, bl2); + mid = Math.imul(al9, bh2); + mid = (mid + Math.imul(ah9, bl2)) | 0; + hi = Math.imul(ah9, bh2); + lo = (lo + Math.imul(al8, bl3)) | 0; + mid = (mid + Math.imul(al8, bh3)) | 0; + mid = (mid + Math.imul(ah8, bl3)) | 0; + hi = (hi + Math.imul(ah8, bh3)) | 0; + lo = (lo + Math.imul(al7, bl4)) | 0; + mid = (mid + Math.imul(al7, bh4)) | 0; + mid = (mid + Math.imul(ah7, bl4)) | 0; + hi = (hi + Math.imul(ah7, bh4)) | 0; + lo = (lo + Math.imul(al6, bl5)) | 0; + mid = (mid + Math.imul(al6, bh5)) | 0; + mid = (mid + Math.imul(ah6, bl5)) | 0; + hi = (hi + Math.imul(ah6, bh5)) | 0; + lo = (lo + Math.imul(al5, bl6)) | 0; + mid = (mid + Math.imul(al5, bh6)) | 0; + mid = (mid + Math.imul(ah5, bl6)) | 0; + hi = (hi + Math.imul(ah5, bh6)) | 0; + lo = (lo + Math.imul(al4, bl7)) | 0; + mid = (mid + Math.imul(al4, bh7)) | 0; + mid = (mid + Math.imul(ah4, bl7)) | 0; + hi = (hi + Math.imul(ah4, bh7)) | 0; + lo = (lo + Math.imul(al3, bl8)) | 0; + mid = (mid + Math.imul(al3, bh8)) | 0; + mid = (mid + Math.imul(ah3, bl8)) | 0; + hi = (hi + Math.imul(ah3, bh8)) | 0; + lo = (lo + Math.imul(al2, bl9)) | 0; + mid = (mid + Math.imul(al2, bh9)) | 0; + mid = (mid + Math.imul(ah2, bl9)) | 0; + hi = (hi + Math.imul(ah2, bh9)) | 0; + var w11 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; + c = (((hi + (mid >>> 13)) | 0) + (w11 >>> 26)) | 0; + w11 &= 0x3ffffff; + /* k = 12 */ + lo = Math.imul(al9, bl3); + mid = Math.imul(al9, bh3); + mid = (mid + Math.imul(ah9, bl3)) | 0; + hi = Math.imul(ah9, bh3); + lo = (lo + Math.imul(al8, bl4)) | 0; + mid = (mid + Math.imul(al8, bh4)) | 0; + mid = (mid + Math.imul(ah8, bl4)) | 0; + hi = (hi + Math.imul(ah8, bh4)) | 0; + lo = (lo + Math.imul(al7, bl5)) | 0; + mid = (mid + Math.imul(al7, bh5)) | 0; + mid = (mid + Math.imul(ah7, bl5)) | 0; + hi = (hi + Math.imul(ah7, bh5)) | 0; + lo = (lo + Math.imul(al6, bl6)) | 0; + mid = (mid + Math.imul(al6, bh6)) | 0; + mid = (mid + Math.imul(ah6, bl6)) | 0; + hi = (hi + Math.imul(ah6, bh6)) | 0; + lo = (lo + Math.imul(al5, bl7)) | 0; + mid = (mid + Math.imul(al5, bh7)) | 0; + mid = (mid + Math.imul(ah5, bl7)) | 0; + hi = (hi + Math.imul(ah5, bh7)) | 0; + lo = (lo + Math.imul(al4, bl8)) | 0; + mid = (mid + Math.imul(al4, bh8)) | 0; + mid = (mid + Math.imul(ah4, bl8)) | 0; + hi = (hi + Math.imul(ah4, bh8)) | 0; + lo = (lo + Math.imul(al3, bl9)) | 0; + mid = (mid + Math.imul(al3, bh9)) | 0; + mid = (mid + Math.imul(ah3, bl9)) | 0; + hi = (hi + Math.imul(ah3, bh9)) | 0; + var w12 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; + c = (((hi + (mid >>> 13)) | 0) + (w12 >>> 26)) | 0; + w12 &= 0x3ffffff; + /* k = 13 */ + lo = Math.imul(al9, bl4); + mid = Math.imul(al9, bh4); + mid = (mid + Math.imul(ah9, bl4)) | 0; + hi = Math.imul(ah9, bh4); + lo = (lo + Math.imul(al8, bl5)) | 0; + mid = (mid + Math.imul(al8, bh5)) | 0; + mid = (mid + Math.imul(ah8, bl5)) | 0; + hi = (hi + Math.imul(ah8, bh5)) | 0; + lo = (lo + Math.imul(al7, bl6)) | 0; + mid = (mid + Math.imul(al7, bh6)) | 0; + mid = (mid + Math.imul(ah7, bl6)) | 0; + hi = (hi + Math.imul(ah7, bh6)) | 0; + lo = (lo + Math.imul(al6, bl7)) | 0; + mid = (mid + Math.imul(al6, bh7)) | 0; + mid = (mid + Math.imul(ah6, bl7)) | 0; + hi = (hi + Math.imul(ah6, bh7)) | 0; + lo = (lo + Math.imul(al5, bl8)) | 0; + mid = (mid + Math.imul(al5, bh8)) | 0; + mid = (mid + Math.imul(ah5, bl8)) | 0; + hi = (hi + Math.imul(ah5, bh8)) | 0; + lo = (lo + Math.imul(al4, bl9)) | 0; + mid = (mid + Math.imul(al4, bh9)) | 0; + mid = (mid + Math.imul(ah4, bl9)) | 0; + hi = (hi + Math.imul(ah4, bh9)) | 0; + var w13 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; + c = (((hi + (mid >>> 13)) | 0) + (w13 >>> 26)) | 0; + w13 &= 0x3ffffff; + /* k = 14 */ + lo = Math.imul(al9, bl5); + mid = Math.imul(al9, bh5); + mid = (mid + Math.imul(ah9, bl5)) | 0; + hi = Math.imul(ah9, bh5); + lo = (lo + Math.imul(al8, bl6)) | 0; + mid = (mid + Math.imul(al8, bh6)) | 0; + mid = (mid + Math.imul(ah8, bl6)) | 0; + hi = (hi + Math.imul(ah8, bh6)) | 0; + lo = (lo + Math.imul(al7, bl7)) | 0; + mid = (mid + Math.imul(al7, bh7)) | 0; + mid = (mid + Math.imul(ah7, bl7)) | 0; + hi = (hi + Math.imul(ah7, bh7)) | 0; + lo = (lo + Math.imul(al6, bl8)) | 0; + mid = (mid + Math.imul(al6, bh8)) | 0; + mid = (mid + Math.imul(ah6, bl8)) | 0; + hi = (hi + Math.imul(ah6, bh8)) | 0; + lo = (lo + Math.imul(al5, bl9)) | 0; + mid = (mid + Math.imul(al5, bh9)) | 0; + mid = (mid + Math.imul(ah5, bl9)) | 0; + hi = (hi + Math.imul(ah5, bh9)) | 0; + var w14 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; + c = (((hi + (mid >>> 13)) | 0) + (w14 >>> 26)) | 0; + w14 &= 0x3ffffff; + /* k = 15 */ + lo = Math.imul(al9, bl6); + mid = Math.imul(al9, bh6); + mid = (mid + Math.imul(ah9, bl6)) | 0; + hi = Math.imul(ah9, bh6); + lo = (lo + Math.imul(al8, bl7)) | 0; + mid = (mid + Math.imul(al8, bh7)) | 0; + mid = (mid + Math.imul(ah8, bl7)) | 0; + hi = (hi + Math.imul(ah8, bh7)) | 0; + lo = (lo + Math.imul(al7, bl8)) | 0; + mid = (mid + Math.imul(al7, bh8)) | 0; + mid = (mid + Math.imul(ah7, bl8)) | 0; + hi = (hi + Math.imul(ah7, bh8)) | 0; + lo = (lo + Math.imul(al6, bl9)) | 0; + mid = (mid + Math.imul(al6, bh9)) | 0; + mid = (mid + Math.imul(ah6, bl9)) | 0; + hi = (hi + Math.imul(ah6, bh9)) | 0; + var w15 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; + c = (((hi + (mid >>> 13)) | 0) + (w15 >>> 26)) | 0; + w15 &= 0x3ffffff; + /* k = 16 */ + lo = Math.imul(al9, bl7); + mid = Math.imul(al9, bh7); + mid = (mid + Math.imul(ah9, bl7)) | 0; + hi = Math.imul(ah9, bh7); + lo = (lo + Math.imul(al8, bl8)) | 0; + mid = (mid + Math.imul(al8, bh8)) | 0; + mid = (mid + Math.imul(ah8, bl8)) | 0; + hi = (hi + Math.imul(ah8, bh8)) | 0; + lo = (lo + Math.imul(al7, bl9)) | 0; + mid = (mid + Math.imul(al7, bh9)) | 0; + mid = (mid + Math.imul(ah7, bl9)) | 0; + hi = (hi + Math.imul(ah7, bh9)) | 0; + var w16 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; + c = (((hi + (mid >>> 13)) | 0) + (w16 >>> 26)) | 0; + w16 &= 0x3ffffff; + /* k = 17 */ + lo = Math.imul(al9, bl8); + mid = Math.imul(al9, bh8); + mid = (mid + Math.imul(ah9, bl8)) | 0; + hi = Math.imul(ah9, bh8); + lo = (lo + Math.imul(al8, bl9)) | 0; + mid = (mid + Math.imul(al8, bh9)) | 0; + mid = (mid + Math.imul(ah8, bl9)) | 0; + hi = (hi + Math.imul(ah8, bh9)) | 0; + var w17 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; + c = (((hi + (mid >>> 13)) | 0) + (w17 >>> 26)) | 0; + w17 &= 0x3ffffff; + /* k = 18 */ + lo = Math.imul(al9, bl9); + mid = Math.imul(al9, bh9); + mid = (mid + Math.imul(ah9, bl9)) | 0; + hi = Math.imul(ah9, bh9); + var w18 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; + c = (((hi + (mid >>> 13)) | 0) + (w18 >>> 26)) | 0; + w18 &= 0x3ffffff; + o[0] = w0; + o[1] = w1; + o[2] = w2; + o[3] = w3; + o[4] = w4; + o[5] = w5; + o[6] = w6; + o[7] = w7; + o[8] = w8; + o[9] = w9; + o[10] = w10; + o[11] = w11; + o[12] = w12; + o[13] = w13; + o[14] = w14; + o[15] = w15; + o[16] = w16; + o[17] = w17; + o[18] = w18; + if (c !== 0) { + o[19] = c; + out.length++; + } + return out; + }; + + // Polyfill comb + if (!Math.imul) { + comb10MulTo = smallMulTo; + } + + function bigMulTo (self, num, out) { + out.negative = num.negative ^ self.negative; + out.length = self.length + num.length; + + var carry = 0; + var hncarry = 0; + for (var k = 0; k < out.length - 1; k++) { + // Sum all words with the same `i + j = k` and accumulate `ncarry`, + // note that ncarry could be >= 0x3ffffff + var ncarry = hncarry; + hncarry = 0; + var rword = carry & 0x3ffffff; + var maxJ = Math.min(k, num.length - 1); + for (var j = Math.max(0, k - self.length + 1); j <= maxJ; j++) { + var i = k - j; + var a = self.words[i] | 0; + var b = num.words[j] | 0; + var r = a * b; + + var lo = r & 0x3ffffff; + ncarry = (ncarry + ((r / 0x4000000) | 0)) | 0; + lo = (lo + rword) | 0; + rword = lo & 0x3ffffff; + ncarry = (ncarry + (lo >>> 26)) | 0; + + hncarry += ncarry >>> 26; + ncarry &= 0x3ffffff; + } + out.words[k] = rword; + carry = ncarry; + ncarry = hncarry; + } + if (carry !== 0) { + out.words[k] = carry; + } else { + out.length--; + } + + return out._strip(); + } + + function jumboMulTo (self, num, out) { + // Temporary disable, see https://github.com/indutny/bn.js/issues/211 + // var fftm = new FFTM(); + // return fftm.mulp(self, num, out); + return bigMulTo(self, num, out); + } + + BN.prototype.mulTo = function mulTo (num, out) { + var res; + var len = this.length + num.length; + if (this.length === 10 && num.length === 10) { + res = comb10MulTo(this, num, out); + } else if (len < 63) { + res = smallMulTo(this, num, out); + } else if (len < 1024) { + res = bigMulTo(this, num, out); + } else { + res = jumboMulTo(this, num, out); + } + + return res; + }; + + // Multiply `this` by `num` + BN.prototype.mul = function mul (num) { + var out = new BN(null); + out.words = new Array(this.length + num.length); + return this.mulTo(num, out); + }; + + // Multiply employing FFT + BN.prototype.mulf = function mulf (num) { + var out = new BN(null); + out.words = new Array(this.length + num.length); + return jumboMulTo(this, num, out); + }; + + // In-place Multiplication + BN.prototype.imul = function imul (num) { + return this.clone().mulTo(num, this); + }; + + BN.prototype.imuln = function imuln (num) { + var isNegNum = num < 0; + if (isNegNum) num = -num; + + assert(typeof num === 'number'); + assert(num < 0x4000000); + + // Carry + var carry = 0; + for (var i = 0; i < this.length; i++) { + var w = (this.words[i] | 0) * num; + var lo = (w & 0x3ffffff) + (carry & 0x3ffffff); + carry >>= 26; + carry += (w / 0x4000000) | 0; + // NOTE: lo is 27bit maximum + carry += lo >>> 26; + this.words[i] = lo & 0x3ffffff; + } + + if (carry !== 0) { + this.words[i] = carry; + this.length++; + } + + return isNegNum ? this.ineg() : this; + }; + + BN.prototype.muln = function muln (num) { + return this.clone().imuln(num); + }; + + // `this` * `this` + BN.prototype.sqr = function sqr () { + return this.mul(this); + }; + + // `this` * `this` in-place + BN.prototype.isqr = function isqr () { + return this.imul(this.clone()); + }; + + // Math.pow(`this`, `num`) + BN.prototype.pow = function pow (num) { + var w = toBitArray(num); + if (w.length === 0) return new BN(1); + + // Skip leading zeroes + var res = this; + for (var i = 0; i < w.length; i++, res = res.sqr()) { + if (w[i] !== 0) break; + } + + if (++i < w.length) { + for (var q = res.sqr(); i < w.length; i++, q = q.sqr()) { + if (w[i] === 0) continue; + + res = res.mul(q); + } + } + + return res; + }; + + // Shift-left in-place + BN.prototype.iushln = function iushln (bits) { + assert(typeof bits === 'number' && bits >= 0); + var r = bits % 26; + var s = (bits - r) / 26; + var carryMask = (0x3ffffff >>> (26 - r)) << (26 - r); + var i; + + if (r !== 0) { + var carry = 0; + + for (i = 0; i < this.length; i++) { + var newCarry = this.words[i] & carryMask; + var c = ((this.words[i] | 0) - newCarry) << r; + this.words[i] = c | carry; + carry = newCarry >>> (26 - r); + } + + if (carry) { + this.words[i] = carry; + this.length++; + } + } + + if (s !== 0) { + for (i = this.length - 1; i >= 0; i--) { + this.words[i + s] = this.words[i]; + } + + for (i = 0; i < s; i++) { + this.words[i] = 0; + } + + this.length += s; + } + + return this._strip(); + }; + + BN.prototype.ishln = function ishln (bits) { + // TODO(indutny): implement me + assert(this.negative === 0); + return this.iushln(bits); + }; + + // Shift-right in-place + // NOTE: `hint` is a lowest bit before trailing zeroes + // NOTE: if `extended` is present - it will be filled with destroyed bits + BN.prototype.iushrn = function iushrn (bits, hint, extended) { + assert(typeof bits === 'number' && bits >= 0); + var h; + if (hint) { + h = (hint - (hint % 26)) / 26; + } else { + h = 0; + } + + var r = bits % 26; + var s = Math.min((bits - r) / 26, this.length); + var mask = 0x3ffffff ^ ((0x3ffffff >>> r) << r); + var maskedWords = extended; + + h -= s; + h = Math.max(0, h); + + // Extended mode, copy masked part + if (maskedWords) { + for (var i = 0; i < s; i++) { + maskedWords.words[i] = this.words[i]; + } + maskedWords.length = s; + } + + if (s === 0) ; else if (this.length > s) { + this.length -= s; + for (i = 0; i < this.length; i++) { + this.words[i] = this.words[i + s]; + } + } else { + this.words[0] = 0; + this.length = 1; + } + + var carry = 0; + for (i = this.length - 1; i >= 0 && (carry !== 0 || i >= h); i--) { + var word = this.words[i] | 0; + this.words[i] = (carry << (26 - r)) | (word >>> r); + carry = word & mask; + } + + // Push carried bits as a mask + if (maskedWords && carry !== 0) { + maskedWords.words[maskedWords.length++] = carry; + } + + if (this.length === 0) { + this.words[0] = 0; + this.length = 1; + } + + return this._strip(); + }; + + BN.prototype.ishrn = function ishrn (bits, hint, extended) { + // TODO(indutny): implement me + assert(this.negative === 0); + return this.iushrn(bits, hint, extended); + }; + + // Shift-left + BN.prototype.shln = function shln (bits) { + return this.clone().ishln(bits); + }; + + BN.prototype.ushln = function ushln (bits) { + return this.clone().iushln(bits); + }; + + // Shift-right + BN.prototype.shrn = function shrn (bits) { + return this.clone().ishrn(bits); + }; + + BN.prototype.ushrn = function ushrn (bits) { + return this.clone().iushrn(bits); + }; + + // Test if n bit is set + BN.prototype.testn = function testn (bit) { + assert(typeof bit === 'number' && bit >= 0); + var r = bit % 26; + var s = (bit - r) / 26; + var q = 1 << r; + + // Fast case: bit is much higher than all existing words + if (this.length <= s) return false; + + // Check bit and return + var w = this.words[s]; + + return !!(w & q); + }; + + // Return only lowers bits of number (in-place) + BN.prototype.imaskn = function imaskn (bits) { + assert(typeof bits === 'number' && bits >= 0); + var r = bits % 26; + var s = (bits - r) / 26; + + assert(this.negative === 0, 'imaskn works only with positive numbers'); + + if (this.length <= s) { + return this; + } + + if (r !== 0) { + s++; + } + this.length = Math.min(s, this.length); + + if (r !== 0) { + var mask = 0x3ffffff ^ ((0x3ffffff >>> r) << r); + this.words[this.length - 1] &= mask; + } + + return this._strip(); + }; + + // Return only lowers bits of number + BN.prototype.maskn = function maskn (bits) { + return this.clone().imaskn(bits); + }; + + // Add plain number `num` to `this` + BN.prototype.iaddn = function iaddn (num) { + assert(typeof num === 'number'); + assert(num < 0x4000000); + if (num < 0) return this.isubn(-num); + + // Possible sign change + if (this.negative !== 0) { + if (this.length === 1 && (this.words[0] | 0) <= num) { + this.words[0] = num - (this.words[0] | 0); + this.negative = 0; + return this; + } + + this.negative = 0; + this.isubn(num); + this.negative = 1; + return this; + } + + // Add without checks + return this._iaddn(num); + }; + + BN.prototype._iaddn = function _iaddn (num) { + this.words[0] += num; + + // Carry + for (var i = 0; i < this.length && this.words[i] >= 0x4000000; i++) { + this.words[i] -= 0x4000000; + if (i === this.length - 1) { + this.words[i + 1] = 1; + } else { + this.words[i + 1]++; + } + } + this.length = Math.max(this.length, i + 1); + + return this; + }; + + // Subtract plain number `num` from `this` + BN.prototype.isubn = function isubn (num) { + assert(typeof num === 'number'); + assert(num < 0x4000000); + if (num < 0) return this.iaddn(-num); + + if (this.negative !== 0) { + this.negative = 0; + this.iaddn(num); + this.negative = 1; + return this; + } + + this.words[0] -= num; + + if (this.length === 1 && this.words[0] < 0) { + this.words[0] = -this.words[0]; + this.negative = 1; + } else { + // Carry + for (var i = 0; i < this.length && this.words[i] < 0; i++) { + this.words[i] += 0x4000000; + this.words[i + 1] -= 1; + } + } + + return this._strip(); + }; + + BN.prototype.addn = function addn (num) { + return this.clone().iaddn(num); + }; + + BN.prototype.subn = function subn (num) { + return this.clone().isubn(num); + }; + + BN.prototype.iabs = function iabs () { + this.negative = 0; + + return this; + }; + + BN.prototype.abs = function abs () { + return this.clone().iabs(); + }; + + BN.prototype._ishlnsubmul = function _ishlnsubmul (num, mul, shift) { + var len = num.length + shift; + var i; + + this._expand(len); + + var w; + var carry = 0; + for (i = 0; i < num.length; i++) { + w = (this.words[i + shift] | 0) + carry; + var right = (num.words[i] | 0) * mul; + w -= right & 0x3ffffff; + carry = (w >> 26) - ((right / 0x4000000) | 0); + this.words[i + shift] = w & 0x3ffffff; + } + for (; i < this.length - shift; i++) { + w = (this.words[i + shift] | 0) + carry; + carry = w >> 26; + this.words[i + shift] = w & 0x3ffffff; + } + + if (carry === 0) return this._strip(); + + // Subtraction overflow + assert(carry === -1); + carry = 0; + for (i = 0; i < this.length; i++) { + w = -(this.words[i] | 0) + carry; + carry = w >> 26; + this.words[i] = w & 0x3ffffff; + } + this.negative = 1; + + return this._strip(); + }; + + BN.prototype._wordDiv = function _wordDiv (num, mode) { + var shift = this.length - num.length; + + var a = this.clone(); + var b = num; + + // Normalize + var bhi = b.words[b.length - 1] | 0; + var bhiBits = this._countBits(bhi); + shift = 26 - bhiBits; + if (shift !== 0) { + b = b.ushln(shift); + a.iushln(shift); + bhi = b.words[b.length - 1] | 0; + } + + // Initialize quotient + var m = a.length - b.length; + var q; + + if (mode !== 'mod') { + q = new BN(null); + q.length = m + 1; + q.words = new Array(q.length); + for (var i = 0; i < q.length; i++) { + q.words[i] = 0; + } + } + + var diff = a.clone()._ishlnsubmul(b, 1, m); + if (diff.negative === 0) { + a = diff; + if (q) { + q.words[m] = 1; + } + } + + for (var j = m - 1; j >= 0; j--) { + var qj = (a.words[b.length + j] | 0) * 0x4000000 + + (a.words[b.length + j - 1] | 0); + + // NOTE: (qj / bhi) is (0x3ffffff * 0x4000000 + 0x3ffffff) / 0x2000000 max + // (0x7ffffff) + qj = Math.min((qj / bhi) | 0, 0x3ffffff); + + a._ishlnsubmul(b, qj, j); + while (a.negative !== 0) { + qj--; + a.negative = 0; + a._ishlnsubmul(b, 1, j); + if (!a.isZero()) { + a.negative ^= 1; + } + } + if (q) { + q.words[j] = qj; + } + } + if (q) { + q._strip(); + } + a._strip(); + + // Denormalize + if (mode !== 'div' && shift !== 0) { + a.iushrn(shift); + } + + return { + div: q || null, + mod: a + }; + }; + + // NOTE: 1) `mode` can be set to `mod` to request mod only, + // to `div` to request div only, or be absent to + // request both div & mod + // 2) `positive` is true if unsigned mod is requested + BN.prototype.divmod = function divmod (num, mode, positive) { + assert(!num.isZero()); + + if (this.isZero()) { + return { + div: new BN(0), + mod: new BN(0) + }; + } + + var div, mod, res; + if (this.negative !== 0 && num.negative === 0) { + res = this.neg().divmod(num, mode); + + if (mode !== 'mod') { + div = res.div.neg(); + } + + if (mode !== 'div') { + mod = res.mod.neg(); + if (positive && mod.negative !== 0) { + mod.iadd(num); + } + } + + return { + div: div, + mod: mod + }; + } + + if (this.negative === 0 && num.negative !== 0) { + res = this.divmod(num.neg(), mode); + + if (mode !== 'mod') { + div = res.div.neg(); + } + + return { + div: div, + mod: res.mod + }; + } + + if ((this.negative & num.negative) !== 0) { + res = this.neg().divmod(num.neg(), mode); + + if (mode !== 'div') { + mod = res.mod.neg(); + if (positive && mod.negative !== 0) { + mod.isub(num); + } + } + + return { + div: res.div, + mod: mod + }; + } + + // Both numbers are positive at this point + + // Strip both numbers to approximate shift value + if (num.length > this.length || this.cmp(num) < 0) { + return { + div: new BN(0), + mod: this + }; + } + + // Very short reduction + if (num.length === 1) { + if (mode === 'div') { + return { + div: this.divn(num.words[0]), + mod: null + }; + } + + if (mode === 'mod') { + return { + div: null, + mod: new BN(this.modrn(num.words[0])) + }; + } + + return { + div: this.divn(num.words[0]), + mod: new BN(this.modrn(num.words[0])) + }; + } + + return this._wordDiv(num, mode); + }; + + // Find `this` / `num` + BN.prototype.div = function div (num) { + return this.divmod(num, 'div', false).div; + }; + + // Find `this` % `num` + BN.prototype.mod = function mod (num) { + return this.divmod(num, 'mod', false).mod; + }; + + BN.prototype.umod = function umod (num) { + return this.divmod(num, 'mod', true).mod; + }; + + // Find Round(`this` / `num`) + BN.prototype.divRound = function divRound (num) { + var dm = this.divmod(num); + + // Fast case - exact division + if (dm.mod.isZero()) return dm.div; + + var mod = dm.div.negative !== 0 ? dm.mod.isub(num) : dm.mod; + + var half = num.ushrn(1); + var r2 = num.andln(1); + var cmp = mod.cmp(half); + + // Round down + if (cmp < 0 || (r2 === 1 && cmp === 0)) return dm.div; + + // Round up + return dm.div.negative !== 0 ? dm.div.isubn(1) : dm.div.iaddn(1); + }; + + BN.prototype.modrn = function modrn (num) { + var isNegNum = num < 0; + if (isNegNum) num = -num; + + assert(num <= 0x3ffffff); + var p = (1 << 26) % num; + + var acc = 0; + for (var i = this.length - 1; i >= 0; i--) { + acc = (p * acc + (this.words[i] | 0)) % num; + } + + return isNegNum ? -acc : acc; + }; + + // WARNING: DEPRECATED + BN.prototype.modn = function modn (num) { + return this.modrn(num); + }; + + // In-place division by number + BN.prototype.idivn = function idivn (num) { + var isNegNum = num < 0; + if (isNegNum) num = -num; + + assert(num <= 0x3ffffff); + + var carry = 0; + for (var i = this.length - 1; i >= 0; i--) { + var w = (this.words[i] | 0) + carry * 0x4000000; + this.words[i] = (w / num) | 0; + carry = w % num; + } + + this._strip(); + return isNegNum ? this.ineg() : this; + }; + + BN.prototype.divn = function divn (num) { + return this.clone().idivn(num); + }; + + BN.prototype.egcd = function egcd (p) { + assert(p.negative === 0); + assert(!p.isZero()); + + var x = this; + var y = p.clone(); + + if (x.negative !== 0) { + x = x.umod(p); + } else { + x = x.clone(); + } + + // A * x + B * y = x + var A = new BN(1); + var B = new BN(0); + + // C * x + D * y = y + var C = new BN(0); + var D = new BN(1); + + var g = 0; + + while (x.isEven() && y.isEven()) { + x.iushrn(1); + y.iushrn(1); + ++g; + } + + var yp = y.clone(); + var xp = x.clone(); + + while (!x.isZero()) { + for (var i = 0, im = 1; (x.words[0] & im) === 0 && i < 26; ++i, im <<= 1); + if (i > 0) { + x.iushrn(i); + while (i-- > 0) { + if (A.isOdd() || B.isOdd()) { + A.iadd(yp); + B.isub(xp); + } + + A.iushrn(1); + B.iushrn(1); + } + } + + for (var j = 0, jm = 1; (y.words[0] & jm) === 0 && j < 26; ++j, jm <<= 1); + if (j > 0) { + y.iushrn(j); + while (j-- > 0) { + if (C.isOdd() || D.isOdd()) { + C.iadd(yp); + D.isub(xp); + } + + C.iushrn(1); + D.iushrn(1); + } + } + + if (x.cmp(y) >= 0) { + x.isub(y); + A.isub(C); + B.isub(D); + } else { + y.isub(x); + C.isub(A); + D.isub(B); + } + } + + return { + a: C, + b: D, + gcd: y.iushln(g) + }; + }; + + // This is reduced incarnation of the binary EEA + // above, designated to invert members of the + // _prime_ fields F(p) at a maximal speed + BN.prototype._invmp = function _invmp (p) { + assert(p.negative === 0); + assert(!p.isZero()); + + var a = this; + var b = p.clone(); + + if (a.negative !== 0) { + a = a.umod(p); + } else { + a = a.clone(); + } + + var x1 = new BN(1); + var x2 = new BN(0); + + var delta = b.clone(); + + while (a.cmpn(1) > 0 && b.cmpn(1) > 0) { + for (var i = 0, im = 1; (a.words[0] & im) === 0 && i < 26; ++i, im <<= 1); + if (i > 0) { + a.iushrn(i); + while (i-- > 0) { + if (x1.isOdd()) { + x1.iadd(delta); + } + + x1.iushrn(1); + } + } + + for (var j = 0, jm = 1; (b.words[0] & jm) === 0 && j < 26; ++j, jm <<= 1); + if (j > 0) { + b.iushrn(j); + while (j-- > 0) { + if (x2.isOdd()) { + x2.iadd(delta); + } + + x2.iushrn(1); + } + } + + if (a.cmp(b) >= 0) { + a.isub(b); + x1.isub(x2); + } else { + b.isub(a); + x2.isub(x1); + } + } + + var res; + if (a.cmpn(1) === 0) { + res = x1; + } else { + res = x2; + } + + if (res.cmpn(0) < 0) { + res.iadd(p); + } + + return res; + }; + + BN.prototype.gcd = function gcd (num) { + if (this.isZero()) return num.abs(); + if (num.isZero()) return this.abs(); + + var a = this.clone(); + var b = num.clone(); + a.negative = 0; + b.negative = 0; + + // Remove common factor of two + for (var shift = 0; a.isEven() && b.isEven(); shift++) { + a.iushrn(1); + b.iushrn(1); + } + + do { + while (a.isEven()) { + a.iushrn(1); + } + while (b.isEven()) { + b.iushrn(1); + } + + var r = a.cmp(b); + if (r < 0) { + // Swap `a` and `b` to make `a` always bigger than `b` + var t = a; + a = b; + b = t; + } else if (r === 0 || b.cmpn(1) === 0) { + break; + } + + a.isub(b); + } while (true); + + return b.iushln(shift); + }; + + // Invert number in the field F(num) + BN.prototype.invm = function invm (num) { + return this.egcd(num).a.umod(num); + }; + + BN.prototype.isEven = function isEven () { + return (this.words[0] & 1) === 0; + }; + + BN.prototype.isOdd = function isOdd () { + return (this.words[0] & 1) === 1; + }; + + // And first word and num + BN.prototype.andln = function andln (num) { + return this.words[0] & num; + }; + + // Increment at the bit position in-line + BN.prototype.bincn = function bincn (bit) { + assert(typeof bit === 'number'); + var r = bit % 26; + var s = (bit - r) / 26; + var q = 1 << r; + + // Fast case: bit is much higher than all existing words + if (this.length <= s) { + this._expand(s + 1); + this.words[s] |= q; + return this; + } + + // Add bit and propagate, if needed + var carry = q; + for (var i = s; carry !== 0 && i < this.length; i++) { + var w = this.words[i] | 0; + w += carry; + carry = w >>> 26; + w &= 0x3ffffff; + this.words[i] = w; + } + if (carry !== 0) { + this.words[i] = carry; + this.length++; + } + return this; + }; + + BN.prototype.isZero = function isZero () { + return this.length === 1 && this.words[0] === 0; + }; + + BN.prototype.cmpn = function cmpn (num) { + var negative = num < 0; + + if (this.negative !== 0 && !negative) return -1; + if (this.negative === 0 && negative) return 1; + + this._strip(); + + var res; + if (this.length > 1) { + res = 1; + } else { + if (negative) { + num = -num; + } + + assert(num <= 0x3ffffff, 'Number is too big'); + + var w = this.words[0] | 0; + res = w === num ? 0 : w < num ? -1 : 1; + } + if (this.negative !== 0) return -res | 0; + return res; + }; + + // Compare two numbers and return: + // 1 - if `this` > `num` + // 0 - if `this` == `num` + // -1 - if `this` < `num` + BN.prototype.cmp = function cmp (num) { + if (this.negative !== 0 && num.negative === 0) return -1; + if (this.negative === 0 && num.negative !== 0) return 1; + + var res = this.ucmp(num); + if (this.negative !== 0) return -res | 0; + return res; + }; + + // Unsigned comparison + BN.prototype.ucmp = function ucmp (num) { + // At this point both numbers have the same sign + if (this.length > num.length) return 1; + if (this.length < num.length) return -1; + + var res = 0; + for (var i = this.length - 1; i >= 0; i--) { + var a = this.words[i] | 0; + var b = num.words[i] | 0; + + if (a === b) continue; + if (a < b) { + res = -1; + } else if (a > b) { + res = 1; + } + break; + } + return res; + }; + + BN.prototype.gtn = function gtn (num) { + return this.cmpn(num) === 1; + }; + + BN.prototype.gt = function gt (num) { + return this.cmp(num) === 1; + }; + + BN.prototype.gten = function gten (num) { + return this.cmpn(num) >= 0; + }; + + BN.prototype.gte = function gte (num) { + return this.cmp(num) >= 0; + }; + + BN.prototype.ltn = function ltn (num) { + return this.cmpn(num) === -1; + }; + + BN.prototype.lt = function lt (num) { + return this.cmp(num) === -1; + }; + + BN.prototype.lten = function lten (num) { + return this.cmpn(num) <= 0; + }; + + BN.prototype.lte = function lte (num) { + return this.cmp(num) <= 0; + }; + + BN.prototype.eqn = function eqn (num) { + return this.cmpn(num) === 0; + }; + + BN.prototype.eq = function eq (num) { + return this.cmp(num) === 0; + }; + + // + // A reduce context, could be using montgomery or something better, depending + // on the `m` itself. + // + BN.red = function red (num) { + return new Red(num); + }; + + BN.prototype.toRed = function toRed (ctx) { + assert(!this.red, 'Already a number in reduction context'); + assert(this.negative === 0, 'red works only with positives'); + return ctx.convertTo(this)._forceRed(ctx); + }; + + BN.prototype.fromRed = function fromRed () { + assert(this.red, 'fromRed works only with numbers in reduction context'); + return this.red.convertFrom(this); + }; + + BN.prototype._forceRed = function _forceRed (ctx) { + this.red = ctx; + return this; + }; + + BN.prototype.forceRed = function forceRed (ctx) { + assert(!this.red, 'Already a number in reduction context'); + return this._forceRed(ctx); + }; + + BN.prototype.redAdd = function redAdd (num) { + assert(this.red, 'redAdd works only with red numbers'); + return this.red.add(this, num); + }; + + BN.prototype.redIAdd = function redIAdd (num) { + assert(this.red, 'redIAdd works only with red numbers'); + return this.red.iadd(this, num); + }; + + BN.prototype.redSub = function redSub (num) { + assert(this.red, 'redSub works only with red numbers'); + return this.red.sub(this, num); + }; + + BN.prototype.redISub = function redISub (num) { + assert(this.red, 'redISub works only with red numbers'); + return this.red.isub(this, num); + }; + + BN.prototype.redShl = function redShl (num) { + assert(this.red, 'redShl works only with red numbers'); + return this.red.shl(this, num); + }; + + BN.prototype.redMul = function redMul (num) { + assert(this.red, 'redMul works only with red numbers'); + this.red._verify2(this, num); + return this.red.mul(this, num); + }; + + BN.prototype.redIMul = function redIMul (num) { + assert(this.red, 'redMul works only with red numbers'); + this.red._verify2(this, num); + return this.red.imul(this, num); + }; + + BN.prototype.redSqr = function redSqr () { + assert(this.red, 'redSqr works only with red numbers'); + this.red._verify1(this); + return this.red.sqr(this); + }; + + BN.prototype.redISqr = function redISqr () { + assert(this.red, 'redISqr works only with red numbers'); + this.red._verify1(this); + return this.red.isqr(this); + }; + + // Square root over p + BN.prototype.redSqrt = function redSqrt () { + assert(this.red, 'redSqrt works only with red numbers'); + this.red._verify1(this); + return this.red.sqrt(this); + }; + + BN.prototype.redInvm = function redInvm () { + assert(this.red, 'redInvm works only with red numbers'); + this.red._verify1(this); + return this.red.invm(this); + }; + + // Return negative clone of `this` % `red modulo` + BN.prototype.redNeg = function redNeg () { + assert(this.red, 'redNeg works only with red numbers'); + this.red._verify1(this); + return this.red.neg(this); + }; + + BN.prototype.redPow = function redPow (num) { + assert(this.red && !num.red, 'redPow(normalNum)'); + this.red._verify1(this); + return this.red.pow(this, num); + }; + + // Prime numbers with efficient reduction + var primes = { + k256: null, + p224: null, + p192: null, + p25519: null + }; + + // Pseudo-Mersenne prime + function MPrime (name, p) { + // P = 2 ^ N - K + this.name = name; + this.p = new BN(p, 16); + this.n = this.p.bitLength(); + this.k = new BN(1).iushln(this.n).isub(this.p); + + this.tmp = this._tmp(); + } + + MPrime.prototype._tmp = function _tmp () { + var tmp = new BN(null); + tmp.words = new Array(Math.ceil(this.n / 13)); + return tmp; + }; + + MPrime.prototype.ireduce = function ireduce (num) { + // Assumes that `num` is less than `P^2` + // num = HI * (2 ^ N - K) + HI * K + LO = HI * K + LO (mod P) + var r = num; + var rlen; + + do { + this.split(r, this.tmp); + r = this.imulK(r); + r = r.iadd(this.tmp); + rlen = r.bitLength(); + } while (rlen > this.n); + + var cmp = rlen < this.n ? -1 : r.ucmp(this.p); + if (cmp === 0) { + r.words[0] = 0; + r.length = 1; + } else if (cmp > 0) { + r.isub(this.p); + } else { + if (r.strip !== undefined) { + // r is a BN v4 instance + r.strip(); + } else { + // r is a BN v5 instance + r._strip(); + } + } + + return r; + }; + + MPrime.prototype.split = function split (input, out) { + input.iushrn(this.n, 0, out); + }; + + MPrime.prototype.imulK = function imulK (num) { + return num.imul(this.k); + }; + + function K256 () { + MPrime.call( + this, + 'k256', + 'ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff fffffffe fffffc2f'); + } + inherits(K256, MPrime); + + K256.prototype.split = function split (input, output) { + // 256 = 9 * 26 + 22 + var mask = 0x3fffff; + + var outLen = Math.min(input.length, 9); + for (var i = 0; i < outLen; i++) { + output.words[i] = input.words[i]; + } + output.length = outLen; + + if (input.length <= 9) { + input.words[0] = 0; + input.length = 1; + return; + } + + // Shift by 9 limbs + var prev = input.words[9]; + output.words[output.length++] = prev & mask; + + for (i = 10; i < input.length; i++) { + var next = input.words[i] | 0; + input.words[i - 10] = ((next & mask) << 4) | (prev >>> 22); + prev = next; + } + prev >>>= 22; + input.words[i - 10] = prev; + if (prev === 0 && input.length > 10) { + input.length -= 10; + } else { + input.length -= 9; + } + }; + + K256.prototype.imulK = function imulK (num) { + // K = 0x1000003d1 = [ 0x40, 0x3d1 ] + num.words[num.length] = 0; + num.words[num.length + 1] = 0; + num.length += 2; + + // bounded at: 0x40 * 0x3ffffff + 0x3d0 = 0x100000390 + var lo = 0; + for (var i = 0; i < num.length; i++) { + var w = num.words[i] | 0; + lo += w * 0x3d1; + num.words[i] = lo & 0x3ffffff; + lo = w * 0x40 + ((lo / 0x4000000) | 0); + } + + // Fast length reduction + if (num.words[num.length - 1] === 0) { + num.length--; + if (num.words[num.length - 1] === 0) { + num.length--; + } + } + return num; + }; + + function P224 () { + MPrime.call( + this, + 'p224', + 'ffffffff ffffffff ffffffff ffffffff 00000000 00000000 00000001'); + } + inherits(P224, MPrime); + + function P192 () { + MPrime.call( + this, + 'p192', + 'ffffffff ffffffff ffffffff fffffffe ffffffff ffffffff'); + } + inherits(P192, MPrime); + + function P25519 () { + // 2 ^ 255 - 19 + MPrime.call( + this, + '25519', + '7fffffffffffffff ffffffffffffffff ffffffffffffffff ffffffffffffffed'); + } + inherits(P25519, MPrime); + + P25519.prototype.imulK = function imulK (num) { + // K = 0x13 + var carry = 0; + for (var i = 0; i < num.length; i++) { + var hi = (num.words[i] | 0) * 0x13 + carry; + var lo = hi & 0x3ffffff; + hi >>>= 26; + + num.words[i] = lo; + carry = hi; + } + if (carry !== 0) { + num.words[num.length++] = carry; + } + return num; + }; + + // Exported mostly for testing purposes, use plain name instead + BN._prime = function prime (name) { + // Cached version of prime + if (primes[name]) return primes[name]; + + var prime; + if (name === 'k256') { + prime = new K256(); + } else if (name === 'p224') { + prime = new P224(); + } else if (name === 'p192') { + prime = new P192(); + } else if (name === 'p25519') { + prime = new P25519(); + } else { + throw new Error('Unknown prime ' + name); + } + primes[name] = prime; + + return prime; + }; + + // + // Base reduction engine + // + function Red (m) { + if (typeof m === 'string') { + var prime = BN._prime(m); + this.m = prime.p; + this.prime = prime; + } else { + assert(m.gtn(1), 'modulus must be greater than 1'); + this.m = m; + this.prime = null; + } + } + + Red.prototype._verify1 = function _verify1 (a) { + assert(a.negative === 0, 'red works only with positives'); + assert(a.red, 'red works only with red numbers'); + }; + + Red.prototype._verify2 = function _verify2 (a, b) { + assert((a.negative | b.negative) === 0, 'red works only with positives'); + assert(a.red && a.red === b.red, + 'red works only with red numbers'); + }; + + Red.prototype.imod = function imod (a) { + if (this.prime) return this.prime.ireduce(a)._forceRed(this); + + move(a, a.umod(this.m)._forceRed(this)); + return a; + }; + + Red.prototype.neg = function neg (a) { + if (a.isZero()) { + return a.clone(); + } + + return this.m.sub(a)._forceRed(this); + }; + + Red.prototype.add = function add (a, b) { + this._verify2(a, b); + + var res = a.add(b); + if (res.cmp(this.m) >= 0) { + res.isub(this.m); + } + return res._forceRed(this); + }; + + Red.prototype.iadd = function iadd (a, b) { + this._verify2(a, b); + + var res = a.iadd(b); + if (res.cmp(this.m) >= 0) { + res.isub(this.m); + } + return res; + }; + + Red.prototype.sub = function sub (a, b) { + this._verify2(a, b); + + var res = a.sub(b); + if (res.cmpn(0) < 0) { + res.iadd(this.m); + } + return res._forceRed(this); + }; + + Red.prototype.isub = function isub (a, b) { + this._verify2(a, b); + + var res = a.isub(b); + if (res.cmpn(0) < 0) { + res.iadd(this.m); + } + return res; + }; + + Red.prototype.shl = function shl (a, num) { + this._verify1(a); + return this.imod(a.ushln(num)); + }; + + Red.prototype.imul = function imul (a, b) { + this._verify2(a, b); + return this.imod(a.imul(b)); + }; + + Red.prototype.mul = function mul (a, b) { + this._verify2(a, b); + return this.imod(a.mul(b)); + }; + + Red.prototype.isqr = function isqr (a) { + return this.imul(a, a.clone()); + }; + + Red.prototype.sqr = function sqr (a) { + return this.mul(a, a); + }; + + Red.prototype.sqrt = function sqrt (a) { + if (a.isZero()) return a.clone(); + + var mod3 = this.m.andln(3); + assert(mod3 % 2 === 1); + + // Fast case + if (mod3 === 3) { + var pow = this.m.add(new BN(1)).iushrn(2); + return this.pow(a, pow); + } + + // Tonelli-Shanks algorithm (Totally unoptimized and slow) + // + // Find Q and S, that Q * 2 ^ S = (P - 1) + var q = this.m.subn(1); + var s = 0; + while (!q.isZero() && q.andln(1) === 0) { + s++; + q.iushrn(1); + } + assert(!q.isZero()); + + var one = new BN(1).toRed(this); + var nOne = one.redNeg(); + + // Find quadratic non-residue + // NOTE: Max is such because of generalized Riemann hypothesis. + var lpow = this.m.subn(1).iushrn(1); + var z = this.m.bitLength(); + z = new BN(2 * z * z).toRed(this); + + while (this.pow(z, lpow).cmp(nOne) !== 0) { + z.redIAdd(nOne); + } + + var c = this.pow(z, q); + var r = this.pow(a, q.addn(1).iushrn(1)); + var t = this.pow(a, q); + var m = s; + while (t.cmp(one) !== 0) { + var tmp = t; + for (var i = 0; tmp.cmp(one) !== 0; i++) { + tmp = tmp.redSqr(); + } + assert(i < m); + var b = this.pow(c, new BN(1).iushln(m - i - 1)); + + r = r.redMul(b); + c = b.redSqr(); + t = t.redMul(c); + m = i; + } + + return r; + }; + + Red.prototype.invm = function invm (a) { + var inv = a._invmp(this.m); + if (inv.negative !== 0) { + inv.negative = 0; + return this.imod(inv).redNeg(); + } else { + return this.imod(inv); + } + }; + + Red.prototype.pow = function pow (a, num) { + if (num.isZero()) return new BN(1).toRed(this); + if (num.cmpn(1) === 0) return a.clone(); + + var windowSize = 4; + var wnd = new Array(1 << windowSize); + wnd[0] = new BN(1).toRed(this); + wnd[1] = a; + for (var i = 2; i < wnd.length; i++) { + wnd[i] = this.mul(wnd[i - 1], a); + } + + var res = wnd[0]; + var current = 0; + var currentLen = 0; + var start = num.bitLength() % 26; + if (start === 0) { + start = 26; + } + + for (i = num.length - 1; i >= 0; i--) { + var word = num.words[i]; + for (var j = start - 1; j >= 0; j--) { + var bit = (word >> j) & 1; + if (res !== wnd[0]) { + res = this.sqr(res); + } + + if (bit === 0 && current === 0) { + currentLen = 0; + continue; + } + + current <<= 1; + current |= bit; + currentLen++; + if (currentLen !== windowSize && (i !== 0 || j !== 0)) continue; + + res = this.mul(res, wnd[current]); + currentLen = 0; + current = 0; + } + start = 26; + } + + return res; + }; + + Red.prototype.convertTo = function convertTo (num) { + var r = num.umod(this.m); + + return r === num ? r.clone() : r; + }; + + Red.prototype.convertFrom = function convertFrom (num) { + var res = num.clone(); + res.red = null; + return res; + }; + + // + // Montgomery method engine + // + + BN.mont = function mont (num) { + return new Mont(num); + }; + + function Mont (m) { + Red.call(this, m); + + this.shift = this.m.bitLength(); + if (this.shift % 26 !== 0) { + this.shift += 26 - (this.shift % 26); + } + + this.r = new BN(1).iushln(this.shift); + this.r2 = this.imod(this.r.sqr()); + this.rinv = this.r._invmp(this.m); + + this.minv = this.rinv.mul(this.r).isubn(1).div(this.m); + this.minv = this.minv.umod(this.r); + this.minv = this.r.sub(this.minv); + } + inherits(Mont, Red); + + Mont.prototype.convertTo = function convertTo (num) { + return this.imod(num.ushln(this.shift)); + }; + + Mont.prototype.convertFrom = function convertFrom (num) { + var r = this.imod(num.mul(this.rinv)); + r.red = null; + return r; + }; + + Mont.prototype.imul = function imul (a, b) { + if (a.isZero() || b.isZero()) { + a.words[0] = 0; + a.length = 1; + return a; + } + + var t = a.imul(b); + var c = t.maskn(this.shift).mul(this.minv).imaskn(this.shift).mul(this.m); + var u = t.isub(c).iushrn(this.shift); + var res = u; + + if (u.cmp(this.m) >= 0) { + res = u.isub(this.m); + } else if (u.cmpn(0) < 0) { + res = u.iadd(this.m); + } + + return res._forceRed(this); + }; + + Mont.prototype.mul = function mul (a, b) { + if (a.isZero() || b.isZero()) return new BN(0)._forceRed(this); + + var t = a.mul(b); + var c = t.maskn(this.shift).mul(this.minv).imaskn(this.shift).mul(this.m); + var u = t.isub(c).iushrn(this.shift); + var res = u; + if (u.cmp(this.m) >= 0) { + res = u.isub(this.m); + } else if (u.cmpn(0) < 0) { + res = u.iadd(this.m); + } + + return res._forceRed(this); + }; + + Mont.prototype.invm = function invm (a) { + // (AR)^-1 * R^2 = (A^-1 * R^-1) * R^2 = A^-1 * R + var res = this.imod(a._invmp(this.m).mul(this.r2)); + return res._forceRed(this); + }; + })(module, commonjsGlobal); + } (bn)); + + var BN$1 = bn.exports; + + const version$b = "logger/5.7.0"; + + let _permanentCensorErrors = false; + let _censorErrors = false; + const LogLevels = { debug: 1, "default": 2, info: 2, warning: 3, error: 4, off: 5 }; + let _logLevel = LogLevels["default"]; + let _globalLogger = null; + function _checkNormalize() { + try { + const missing = []; + // Make sure all forms of normalization are supported + ["NFD", "NFC", "NFKD", "NFKC"].forEach((form) => { + try { + if ("test".normalize(form) !== "test") { + throw new Error("bad normalize"); + } + ; + } + catch (error) { + missing.push(form); + } + }); + if (missing.length) { + throw new Error("missing " + missing.join(", ")); + } + if (String.fromCharCode(0xe9).normalize("NFD") !== String.fromCharCode(0x65, 0x0301)) { + throw new Error("broken implementation"); + } + } + catch (error) { + return error.message; + } + return null; + } + const _normalizeError = _checkNormalize(); + var LogLevel; + (function (LogLevel) { + LogLevel["DEBUG"] = "DEBUG"; + LogLevel["INFO"] = "INFO"; + LogLevel["WARNING"] = "WARNING"; + LogLevel["ERROR"] = "ERROR"; + LogLevel["OFF"] = "OFF"; + })(LogLevel || (LogLevel = {})); + var ErrorCode; + (function (ErrorCode) { + /////////////////// + // Generic Errors + // Unknown Error + ErrorCode["UNKNOWN_ERROR"] = "UNKNOWN_ERROR"; + // Not Implemented + ErrorCode["NOT_IMPLEMENTED"] = "NOT_IMPLEMENTED"; + // Unsupported Operation + // - operation + ErrorCode["UNSUPPORTED_OPERATION"] = "UNSUPPORTED_OPERATION"; + // Network Error (i.e. Ethereum Network, such as an invalid chain ID) + // - event ("noNetwork" is not re-thrown in provider.ready; otherwise thrown) + ErrorCode["NETWORK_ERROR"] = "NETWORK_ERROR"; + // Some sort of bad response from the server + ErrorCode["SERVER_ERROR"] = "SERVER_ERROR"; + // Timeout + ErrorCode["TIMEOUT"] = "TIMEOUT"; + /////////////////// + // Operational Errors + // Buffer Overrun + ErrorCode["BUFFER_OVERRUN"] = "BUFFER_OVERRUN"; + // Numeric Fault + // - operation: the operation being executed + // - fault: the reason this faulted + ErrorCode["NUMERIC_FAULT"] = "NUMERIC_FAULT"; + /////////////////// + // Argument Errors + // Missing new operator to an object + // - name: The name of the class + ErrorCode["MISSING_NEW"] = "MISSING_NEW"; + // Invalid argument (e.g. value is incompatible with type) to a function: + // - argument: The argument name that was invalid + // - value: The value of the argument + ErrorCode["INVALID_ARGUMENT"] = "INVALID_ARGUMENT"; + // Missing argument to a function: + // - count: The number of arguments received + // - expectedCount: The number of arguments expected + ErrorCode["MISSING_ARGUMENT"] = "MISSING_ARGUMENT"; + // Too many arguments + // - count: The number of arguments received + // - expectedCount: The number of arguments expected + ErrorCode["UNEXPECTED_ARGUMENT"] = "UNEXPECTED_ARGUMENT"; + /////////////////// + // Blockchain Errors + // Call exception + // - transaction: the transaction + // - address?: the contract address + // - args?: The arguments passed into the function + // - method?: The Solidity method signature + // - errorSignature?: The EIP848 error signature + // - errorArgs?: The EIP848 error parameters + // - reason: The reason (only for EIP848 "Error(string)") + ErrorCode["CALL_EXCEPTION"] = "CALL_EXCEPTION"; + // Insufficient funds (< value + gasLimit * gasPrice) + // - transaction: the transaction attempted + ErrorCode["INSUFFICIENT_FUNDS"] = "INSUFFICIENT_FUNDS"; + // Nonce has already been used + // - transaction: the transaction attempted + ErrorCode["NONCE_EXPIRED"] = "NONCE_EXPIRED"; + // The replacement fee for the transaction is too low + // - transaction: the transaction attempted + ErrorCode["REPLACEMENT_UNDERPRICED"] = "REPLACEMENT_UNDERPRICED"; + // The gas limit could not be estimated + // - transaction: the transaction passed to estimateGas + ErrorCode["UNPREDICTABLE_GAS_LIMIT"] = "UNPREDICTABLE_GAS_LIMIT"; + // The transaction was replaced by one with a higher gas price + // - reason: "cancelled", "replaced" or "repriced" + // - cancelled: true if reason == "cancelled" or reason == "replaced") + // - hash: original transaction hash + // - replacement: the full TransactionsResponse for the replacement + // - receipt: the receipt of the replacement + ErrorCode["TRANSACTION_REPLACED"] = "TRANSACTION_REPLACED"; + /////////////////// + // Interaction Errors + // The user rejected the action, such as signing a message or sending + // a transaction + ErrorCode["ACTION_REJECTED"] = "ACTION_REJECTED"; + })(ErrorCode || (ErrorCode = {})); + const HEX = "0123456789abcdef"; + class Logger { + constructor(version) { + Object.defineProperty(this, "version", { + enumerable: true, + value: version, + writable: false + }); + } + _log(logLevel, args) { + const level = logLevel.toLowerCase(); + if (LogLevels[level] == null) { + this.throwArgumentError("invalid log level name", "logLevel", logLevel); + } + if (_logLevel > LogLevels[level]) { + return; + } + console.log.apply(console, args); + } + debug(...args) { + this._log(Logger.levels.DEBUG, args); + } + info(...args) { + this._log(Logger.levels.INFO, args); + } + warn(...args) { + this._log(Logger.levels.WARNING, args); + } + makeError(message, code, params) { + // Errors are being censored + if (_censorErrors) { + return this.makeError("censored error", code, {}); + } + if (!code) { + code = Logger.errors.UNKNOWN_ERROR; + } + if (!params) { + params = {}; + } + const messageDetails = []; + Object.keys(params).forEach((key) => { + const value = params[key]; + try { + if (value instanceof Uint8Array) { + let hex = ""; + for (let i = 0; i < value.length; i++) { + hex += HEX[value[i] >> 4]; + hex += HEX[value[i] & 0x0f]; + } + messageDetails.push(key + "=Uint8Array(0x" + hex + ")"); + } + else { + messageDetails.push(key + "=" + JSON.stringify(value)); + } + } + catch (error) { + messageDetails.push(key + "=" + JSON.stringify(params[key].toString())); + } + }); + messageDetails.push(`code=${code}`); + messageDetails.push(`version=${this.version}`); + const reason = message; + let url = ""; + switch (code) { + case ErrorCode.NUMERIC_FAULT: { + url = "NUMERIC_FAULT"; + const fault = message; + switch (fault) { + case "overflow": + case "underflow": + case "division-by-zero": + url += "-" + fault; + break; + case "negative-power": + case "negative-width": + url += "-unsupported"; + break; + case "unbound-bitwise-result": + url += "-unbound-result"; + break; + } + break; + } + case ErrorCode.CALL_EXCEPTION: + case ErrorCode.INSUFFICIENT_FUNDS: + case ErrorCode.MISSING_NEW: + case ErrorCode.NONCE_EXPIRED: + case ErrorCode.REPLACEMENT_UNDERPRICED: + case ErrorCode.TRANSACTION_REPLACED: + case ErrorCode.UNPREDICTABLE_GAS_LIMIT: + url = code; + break; + } + if (url) { + message += " [ See: https:/\/links.ethers.org/v5-errors-" + url + " ]"; + } + if (messageDetails.length) { + message += " (" + messageDetails.join(", ") + ")"; + } + // @TODO: Any?? + const error = new Error(message); + error.reason = reason; + error.code = code; + Object.keys(params).forEach(function (key) { + error[key] = params[key]; + }); + return error; + } + throwError(message, code, params) { + throw this.makeError(message, code, params); + } + throwArgumentError(message, name, value) { + return this.throwError(message, Logger.errors.INVALID_ARGUMENT, { + argument: name, + value: value + }); + } + assert(condition, message, code, params) { + if (!!condition) { + return; + } + this.throwError(message, code, params); + } + assertArgument(condition, message, name, value) { + if (!!condition) { + return; + } + this.throwArgumentError(message, name, value); + } + checkNormalize(message) { + if (_normalizeError) { + this.throwError("platform missing String.prototype.normalize", Logger.errors.UNSUPPORTED_OPERATION, { + operation: "String.prototype.normalize", form: _normalizeError + }); + } + } + checkSafeUint53(value, message) { + if (typeof (value) !== "number") { + return; + } + if (message == null) { + message = "value not safe"; + } + if (value < 0 || value >= 0x1fffffffffffff) { + this.throwError(message, Logger.errors.NUMERIC_FAULT, { + operation: "checkSafeInteger", + fault: "out-of-safe-range", + value: value + }); + } + if (value % 1) { + this.throwError(message, Logger.errors.NUMERIC_FAULT, { + operation: "checkSafeInteger", + fault: "non-integer", + value: value + }); + } + } + checkArgumentCount(count, expectedCount, message) { + if (message) { + message = ": " + message; + } + else { + message = ""; + } + if (count < expectedCount) { + this.throwError("missing argument" + message, Logger.errors.MISSING_ARGUMENT, { + count: count, + expectedCount: expectedCount + }); + } + if (count > expectedCount) { + this.throwError("too many arguments" + message, Logger.errors.UNEXPECTED_ARGUMENT, { + count: count, + expectedCount: expectedCount + }); + } + } + checkNew(target, kind) { + if (target === Object || target == null) { + this.throwError("missing new", Logger.errors.MISSING_NEW, { name: kind.name }); + } + } + checkAbstract(target, kind) { + if (target === kind) { + this.throwError("cannot instantiate abstract class " + JSON.stringify(kind.name) + " directly; use a sub-class", Logger.errors.UNSUPPORTED_OPERATION, { name: target.name, operation: "new" }); + } + else if (target === Object || target == null) { + this.throwError("missing new", Logger.errors.MISSING_NEW, { name: kind.name }); + } + } + static globalLogger() { + if (!_globalLogger) { + _globalLogger = new Logger(version$b); + } + return _globalLogger; + } + static setCensorship(censorship, permanent) { + if (!censorship && permanent) { + this.globalLogger().throwError("cannot permanently disable censorship", Logger.errors.UNSUPPORTED_OPERATION, { + operation: "setCensorship" + }); + } + if (_permanentCensorErrors) { + if (!censorship) { + return; + } + this.globalLogger().throwError("error censorship permanent", Logger.errors.UNSUPPORTED_OPERATION, { + operation: "setCensorship" + }); + } + _censorErrors = !!censorship; + _permanentCensorErrors = !!permanent; + } + static setLogLevel(logLevel) { + const level = LogLevels[logLevel.toLowerCase()]; + if (level == null) { + Logger.globalLogger().warn("invalid log level - " + logLevel); + return; + } + _logLevel = level; + } + static from(version) { + return new Logger(version); + } + } + Logger.errors = ErrorCode; + Logger.levels = LogLevel; + + const version$a = "bytes/5.7.0"; + + const logger$e = new Logger(version$a); + /////////////////////////////// + function isHexable(value) { + return !!(value.toHexString); + } + function addSlice(array) { + if (array.slice) { + return array; + } + array.slice = function () { + const args = Array.prototype.slice.call(arguments); + return addSlice(new Uint8Array(Array.prototype.slice.apply(array, args))); + }; + return array; + } + function isBytesLike(value) { + return ((isHexString(value) && !(value.length % 2)) || isBytes(value)); + } + function isInteger(value) { + return (typeof (value) === "number" && value == value && (value % 1) === 0); + } + function isBytes(value) { + if (value == null) { + return false; + } + if (value.constructor === Uint8Array) { + return true; + } + if (typeof (value) === "string") { + return false; + } + if (!isInteger(value.length) || value.length < 0) { + return false; + } + for (let i = 0; i < value.length; i++) { + const v = value[i]; + if (!isInteger(v) || v < 0 || v >= 256) { + return false; + } + } + return true; + } + function arrayify(value, options) { + if (!options) { + options = {}; + } + if (typeof (value) === "number") { + logger$e.checkSafeUint53(value, "invalid arrayify value"); + const result = []; + while (value) { + result.unshift(value & 0xff); + value = parseInt(String(value / 256)); + } + if (result.length === 0) { + result.push(0); + } + return addSlice(new Uint8Array(result)); + } + if (options.allowMissingPrefix && typeof (value) === "string" && value.substring(0, 2) !== "0x") { + value = "0x" + value; + } + if (isHexable(value)) { + value = value.toHexString(); + } + if (isHexString(value)) { + let hex = value.substring(2); + if (hex.length % 2) { + if (options.hexPad === "left") { + hex = "0" + hex; + } + else if (options.hexPad === "right") { + hex += "0"; + } + else { + logger$e.throwArgumentError("hex data is odd-length", "value", value); + } + } + const result = []; + for (let i = 0; i < hex.length; i += 2) { + result.push(parseInt(hex.substring(i, i + 2), 16)); + } + return addSlice(new Uint8Array(result)); + } + if (isBytes(value)) { + return addSlice(new Uint8Array(value)); + } + return logger$e.throwArgumentError("invalid arrayify value", "value", value); + } + function concat(items) { + const objects = items.map(item => arrayify(item)); + const length = objects.reduce((accum, item) => (accum + item.length), 0); + const result = new Uint8Array(length); + objects.reduce((offset, object) => { + result.set(object, offset); + return offset + object.length; + }, 0); + return addSlice(result); + } + function stripZeros(value) { + let result = arrayify(value); + if (result.length === 0) { + return result; + } + // Find the first non-zero entry + let start = 0; + while (start < result.length && result[start] === 0) { + start++; + } + // If we started with zeros, strip them + if (start) { + result = result.slice(start); + } + return result; + } + function isHexString(value, length) { + if (typeof (value) !== "string" || !value.match(/^0x[0-9A-Fa-f]*$/)) { + return false; + } + if (length && value.length !== 2 + 2 * length) { + return false; + } + return true; + } + const HexCharacters = "0123456789abcdef"; + function hexlify(value, options) { + if (!options) { + options = {}; + } + if (typeof (value) === "number") { + logger$e.checkSafeUint53(value, "invalid hexlify value"); + let hex = ""; + while (value) { + hex = HexCharacters[value & 0xf] + hex; + value = Math.floor(value / 16); + } + if (hex.length) { + if (hex.length % 2) { + hex = "0" + hex; + } + return "0x" + hex; + } + return "0x00"; + } + if (typeof (value) === "bigint") { + value = value.toString(16); + if (value.length % 2) { + return ("0x0" + value); + } + return "0x" + value; + } + if (options.allowMissingPrefix && typeof (value) === "string" && value.substring(0, 2) !== "0x") { + value = "0x" + value; + } + if (isHexable(value)) { + return value.toHexString(); + } + if (isHexString(value)) { + if (value.length % 2) { + if (options.hexPad === "left") { + value = "0x0" + value.substring(2); + } + else if (options.hexPad === "right") { + value += "0"; + } + else { + logger$e.throwArgumentError("hex data is odd-length", "value", value); + } + } + return value.toLowerCase(); + } + if (isBytes(value)) { + let result = "0x"; + for (let i = 0; i < value.length; i++) { + let v = value[i]; + result += HexCharacters[(v & 0xf0) >> 4] + HexCharacters[v & 0x0f]; + } + return result; + } + return logger$e.throwArgumentError("invalid hexlify value", "value", value); + } + /* + function unoddify(value: BytesLike | Hexable | number): BytesLike | Hexable | number { + if (typeof(value) === "string" && value.length % 2 && value.substring(0, 2) === "0x") { + return "0x0" + value.substring(2); + } + return value; + } + */ + function hexDataLength(data) { + if (typeof (data) !== "string") { + data = hexlify(data); + } + else if (!isHexString(data) || (data.length % 2)) { + return null; + } + return (data.length - 2) / 2; + } + function hexDataSlice(data, offset, endOffset) { + if (typeof (data) !== "string") { + data = hexlify(data); + } + else if (!isHexString(data) || (data.length % 2)) { + logger$e.throwArgumentError("invalid hexData", "value", data); + } + offset = 2 + 2 * offset; + if (endOffset != null) { + return "0x" + data.substring(offset, 2 + 2 * endOffset); + } + return "0x" + data.substring(offset); + } + function hexConcat(items) { + let result = "0x"; + items.forEach((item) => { + result += hexlify(item).substring(2); + }); + return result; + } + function hexZeroPad(value, length) { + if (typeof (value) !== "string") { + value = hexlify(value); + } + else if (!isHexString(value)) { + logger$e.throwArgumentError("invalid hex string", "value", value); + } + if (value.length > 2 * length + 2) { + logger$e.throwArgumentError("value out of range", "value", arguments[1]); + } + while (value.length < 2 * length + 2) { + value = "0x0" + value.substring(2); + } + return value; + } + + const version$9 = "bignumber/5.7.0"; + + var BN = BN$1.BN; + const logger$d = new Logger(version$9); + const _constructorGuard$1 = {}; + const MAX_SAFE = 0x1fffffffffffff; + // Only warn about passing 10 into radix once + let _warnedToStringRadix = false; + class BigNumber { + constructor(constructorGuard, hex) { + if (constructorGuard !== _constructorGuard$1) { + logger$d.throwError("cannot call constructor directly; use BigNumber.from", Logger.errors.UNSUPPORTED_OPERATION, { + operation: "new (BigNumber)" + }); + } + this._hex = hex; + this._isBigNumber = true; + Object.freeze(this); + } + fromTwos(value) { + return toBigNumber(toBN(this).fromTwos(value)); + } + toTwos(value) { + return toBigNumber(toBN(this).toTwos(value)); + } + abs() { + if (this._hex[0] === "-") { + return BigNumber.from(this._hex.substring(1)); + } + return this; + } + add(other) { + return toBigNumber(toBN(this).add(toBN(other))); + } + sub(other) { + return toBigNumber(toBN(this).sub(toBN(other))); + } + div(other) { + const o = BigNumber.from(other); + if (o.isZero()) { + throwFault("division-by-zero", "div"); + } + return toBigNumber(toBN(this).div(toBN(other))); + } + mul(other) { + return toBigNumber(toBN(this).mul(toBN(other))); + } + mod(other) { + const value = toBN(other); + if (value.isNeg()) { + throwFault("division-by-zero", "mod"); + } + return toBigNumber(toBN(this).umod(value)); + } + pow(other) { + const value = toBN(other); + if (value.isNeg()) { + throwFault("negative-power", "pow"); + } + return toBigNumber(toBN(this).pow(value)); + } + and(other) { + const value = toBN(other); + if (this.isNegative() || value.isNeg()) { + throwFault("unbound-bitwise-result", "and"); + } + return toBigNumber(toBN(this).and(value)); + } + or(other) { + const value = toBN(other); + if (this.isNegative() || value.isNeg()) { + throwFault("unbound-bitwise-result", "or"); + } + return toBigNumber(toBN(this).or(value)); + } + xor(other) { + const value = toBN(other); + if (this.isNegative() || value.isNeg()) { + throwFault("unbound-bitwise-result", "xor"); + } + return toBigNumber(toBN(this).xor(value)); + } + mask(value) { + if (this.isNegative() || value < 0) { + throwFault("negative-width", "mask"); + } + return toBigNumber(toBN(this).maskn(value)); + } + shl(value) { + if (this.isNegative() || value < 0) { + throwFault("negative-width", "shl"); + } + return toBigNumber(toBN(this).shln(value)); + } + shr(value) { + if (this.isNegative() || value < 0) { + throwFault("negative-width", "shr"); + } + return toBigNumber(toBN(this).shrn(value)); + } + eq(other) { + return toBN(this).eq(toBN(other)); + } + lt(other) { + return toBN(this).lt(toBN(other)); + } + lte(other) { + return toBN(this).lte(toBN(other)); + } + gt(other) { + return toBN(this).gt(toBN(other)); + } + gte(other) { + return toBN(this).gte(toBN(other)); + } + isNegative() { + return (this._hex[0] === "-"); + } + isZero() { + return toBN(this).isZero(); + } + toNumber() { + try { + return toBN(this).toNumber(); + } + catch (error) { + throwFault("overflow", "toNumber", this.toString()); + } + return null; + } + toBigInt() { + try { + return BigInt(this.toString()); + } + catch (e) { } + return logger$d.throwError("this platform does not support BigInt", Logger.errors.UNSUPPORTED_OPERATION, { + value: this.toString() + }); + } + toString() { + // Lots of people expect this, which we do not support, so check (See: #889) + if (arguments.length > 0) { + if (arguments[0] === 10) { + if (!_warnedToStringRadix) { + _warnedToStringRadix = true; + logger$d.warn("BigNumber.toString does not accept any parameters; base-10 is assumed"); + } + } + else if (arguments[0] === 16) { + logger$d.throwError("BigNumber.toString does not accept any parameters; use bigNumber.toHexString()", Logger.errors.UNEXPECTED_ARGUMENT, {}); + } + else { + logger$d.throwError("BigNumber.toString does not accept parameters", Logger.errors.UNEXPECTED_ARGUMENT, {}); + } + } + return toBN(this).toString(10); + } + toHexString() { + return this._hex; + } + toJSON(key) { + return { type: "BigNumber", hex: this.toHexString() }; + } + static from(value) { + if (value instanceof BigNumber) { + return value; + } + if (typeof (value) === "string") { + if (value.match(/^-?0x[0-9a-f]+$/i)) { + return new BigNumber(_constructorGuard$1, toHex(value)); + } + if (value.match(/^-?[0-9]+$/)) { + return new BigNumber(_constructorGuard$1, toHex(new BN(value))); + } + return logger$d.throwArgumentError("invalid BigNumber string", "value", value); + } + if (typeof (value) === "number") { + if (value % 1) { + throwFault("underflow", "BigNumber.from", value); + } + if (value >= MAX_SAFE || value <= -MAX_SAFE) { + throwFault("overflow", "BigNumber.from", value); + } + return BigNumber.from(String(value)); + } + const anyValue = value; + if (typeof (anyValue) === "bigint") { + return BigNumber.from(anyValue.toString()); + } + if (isBytes(anyValue)) { + return BigNumber.from(hexlify(anyValue)); + } + if (anyValue) { + // Hexable interface (takes priority) + if (anyValue.toHexString) { + const hex = anyValue.toHexString(); + if (typeof (hex) === "string") { + return BigNumber.from(hex); + } + } + else { + // For now, handle legacy JSON-ified values (goes away in v6) + let hex = anyValue._hex; + // New-form JSON + if (hex == null && anyValue.type === "BigNumber") { + hex = anyValue.hex; + } + if (typeof (hex) === "string") { + if (isHexString(hex) || (hex[0] === "-" && isHexString(hex.substring(1)))) { + return BigNumber.from(hex); + } + } + } + } + return logger$d.throwArgumentError("invalid BigNumber value", "value", value); + } + static isBigNumber(value) { + return !!(value && value._isBigNumber); + } + } + // Normalize the hex string + function toHex(value) { + // For BN, call on the hex string + if (typeof (value) !== "string") { + return toHex(value.toString(16)); + } + // If negative, prepend the negative sign to the normalized positive value + if (value[0] === "-") { + // Strip off the negative sign + value = value.substring(1); + // Cannot have multiple negative signs (e.g. "--0x04") + if (value[0] === "-") { + logger$d.throwArgumentError("invalid hex", "value", value); + } + // Call toHex on the positive component + value = toHex(value); + // Do not allow "-0x00" + if (value === "0x00") { + return value; + } + // Negate the value + return "-" + value; + } + // Add a "0x" prefix if missing + if (value.substring(0, 2) !== "0x") { + value = "0x" + value; + } + // Normalize zero + if (value === "0x") { + return "0x00"; + } + // Make the string even length + if (value.length % 2) { + value = "0x0" + value.substring(2); + } + // Trim to smallest even-length string + while (value.length > 4 && value.substring(0, 4) === "0x00") { + value = "0x" + value.substring(4); + } + return value; + } + function toBigNumber(value) { + return BigNumber.from(toHex(value)); + } + function toBN(value) { + const hex = BigNumber.from(value).toHexString(); + if (hex[0] === "-") { + return (new BN("-" + hex.substring(3), 16)); + } + return new BN(hex.substring(2), 16); + } + function throwFault(fault, operation, value) { + const params = { fault: fault, operation: operation }; + if (value != null) { + params.value = value; + } + return logger$d.throwError(fault, Logger.errors.NUMERIC_FAULT, params); + } + // value should have no prefix + function _base36To16(value) { + return (new BN(value, 36)).toString(16); + } + + const version$8 = "properties/5.7.0"; + + var __awaiter$3 = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) { + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } + return new (P || (P = Promise))(function (resolve, reject) { + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } + step((generator = generator.apply(thisArg, _arguments || [])).next()); + }); + }; + const logger$c = new Logger(version$8); + function defineReadOnly(object, name, value) { + Object.defineProperty(object, name, { + enumerable: true, + value: value, + writable: false, + }); + } + // Crawl up the constructor chain to find a static method + function getStatic(ctor, key) { + for (let i = 0; i < 32; i++) { + if (ctor[key]) { + return ctor[key]; + } + if (!ctor.prototype || typeof (ctor.prototype) !== "object") { + break; + } + ctor = Object.getPrototypeOf(ctor.prototype).constructor; + } + return null; + } + function resolveProperties(object) { + return __awaiter$3(this, void 0, void 0, function* () { + const promises = Object.keys(object).map((key) => { + const value = object[key]; + return Promise.resolve(value).then((v) => ({ key: key, value: v })); + }); + const results = yield Promise.all(promises); + return results.reduce((accum, result) => { + accum[(result.key)] = result.value; + return accum; + }, {}); + }); + } + function shallowCopy(object) { + const result = {}; + for (const key in object) { + result[key] = object[key]; + } + return result; + } + const opaque = { bigint: true, boolean: true, "function": true, number: true, string: true }; + function _isFrozen(object) { + // Opaque objects are not mutable, so safe to copy by assignment + if (object === undefined || object === null || opaque[typeof (object)]) { + return true; + } + if (Array.isArray(object) || typeof (object) === "object") { + if (!Object.isFrozen(object)) { + return false; + } + const keys = Object.keys(object); + for (let i = 0; i < keys.length; i++) { + let value = null; + try { + value = object[keys[i]]; + } + catch (error) { + // If accessing a value triggers an error, it is a getter + // designed to do so (e.g. Result) and is therefore "frozen" + continue; + } + if (!_isFrozen(value)) { + return false; + } + } + return true; + } + return logger$c.throwArgumentError(`Cannot deepCopy ${typeof (object)}`, "object", object); + } + // Returns a new copy of object, such that no properties may be replaced. + // New properties may be added only to objects. + function _deepCopy(object) { + if (_isFrozen(object)) { + return object; + } + // Arrays are mutable, so we need to create a copy + if (Array.isArray(object)) { + return Object.freeze(object.map((item) => deepCopy(item))); + } + if (typeof (object) === "object") { + const result = {}; + for (const key in object) { + const value = object[key]; + if (value === undefined) { + continue; + } + defineReadOnly(result, key, deepCopy(value)); + } + return result; + } + return logger$c.throwArgumentError(`Cannot deepCopy ${typeof (object)}`, "object", object); + } + function deepCopy(object) { + return _deepCopy(object); + } + class Description { + constructor(info) { + for (const key in info) { + this[key] = deepCopy(info[key]); + } + } + } + + const version$7 = "abi/5.7.0"; + + const logger$b = new Logger(version$7); + const _constructorGuard = {}; + let ModifiersBytes = { calldata: true, memory: true, storage: true }; + let ModifiersNest = { calldata: true, memory: true }; + function checkModifier(type, name) { + if (type === "bytes" || type === "string") { + if (ModifiersBytes[name]) { + return true; + } + } + else if (type === "address") { + if (name === "payable") { + return true; + } + } + else if (type.indexOf("[") >= 0 || type === "tuple") { + if (ModifiersNest[name]) { + return true; + } + } + if (ModifiersBytes[name] || name === "payable") { + logger$b.throwArgumentError("invalid modifier", "name", name); + } + return false; + } + // @TODO: Make sure that children of an indexed tuple are marked with a null indexed + function parseParamType(param, allowIndexed) { + let originalParam = param; + function throwError(i) { + logger$b.throwArgumentError(`unexpected character at position ${i}`, "param", param); + } + param = param.replace(/\s/g, " "); + function newNode(parent) { + let node = { type: "", name: "", parent: parent, state: { allowType: true } }; + if (allowIndexed) { + node.indexed = false; + } + return node; + } + let parent = { type: "", name: "", state: { allowType: true } }; + let node = parent; + for (let i = 0; i < param.length; i++) { + let c = param[i]; + switch (c) { + case "(": + if (node.state.allowType && node.type === "") { + node.type = "tuple"; + } + else if (!node.state.allowParams) { + throwError(i); + } + node.state.allowType = false; + node.type = verifyType(node.type); + node.components = [newNode(node)]; + node = node.components[0]; + break; + case ")": + delete node.state; + if (node.name === "indexed") { + if (!allowIndexed) { + throwError(i); + } + node.indexed = true; + node.name = ""; + } + if (checkModifier(node.type, node.name)) { + node.name = ""; + } + node.type = verifyType(node.type); + let child = node; + node = node.parent; + if (!node) { + throwError(i); + } + delete child.parent; + node.state.allowParams = false; + node.state.allowName = true; + node.state.allowArray = true; + break; + case ",": + delete node.state; + if (node.name === "indexed") { + if (!allowIndexed) { + throwError(i); + } + node.indexed = true; + node.name = ""; + } + if (checkModifier(node.type, node.name)) { + node.name = ""; + } + node.type = verifyType(node.type); + let sibling = newNode(node.parent); + //{ type: "", name: "", parent: node.parent, state: { allowType: true } }; + node.parent.components.push(sibling); + delete node.parent; + node = sibling; + break; + // Hit a space... + case " ": + // If reading type, the type is done and may read a param or name + if (node.state.allowType) { + if (node.type !== "") { + node.type = verifyType(node.type); + delete node.state.allowType; + node.state.allowName = true; + node.state.allowParams = true; + } + } + // If reading name, the name is done + if (node.state.allowName) { + if (node.name !== "") { + if (node.name === "indexed") { + if (!allowIndexed) { + throwError(i); + } + if (node.indexed) { + throwError(i); + } + node.indexed = true; + node.name = ""; + } + else if (checkModifier(node.type, node.name)) { + node.name = ""; + } + else { + node.state.allowName = false; + } + } + } + break; + case "[": + if (!node.state.allowArray) { + throwError(i); + } + node.type += c; + node.state.allowArray = false; + node.state.allowName = false; + node.state.readArray = true; + break; + case "]": + if (!node.state.readArray) { + throwError(i); + } + node.type += c; + node.state.readArray = false; + node.state.allowArray = true; + node.state.allowName = true; + break; + default: + if (node.state.allowType) { + node.type += c; + node.state.allowParams = true; + node.state.allowArray = true; + } + else if (node.state.allowName) { + node.name += c; + delete node.state.allowArray; + } + else if (node.state.readArray) { + node.type += c; + } + else { + throwError(i); + } + } + } + if (node.parent) { + logger$b.throwArgumentError("unexpected eof", "param", param); + } + delete parent.state; + if (node.name === "indexed") { + if (!allowIndexed) { + throwError(originalParam.length - 7); + } + if (node.indexed) { + throwError(originalParam.length - 7); + } + node.indexed = true; + node.name = ""; + } + else if (checkModifier(node.type, node.name)) { + node.name = ""; + } + parent.type = verifyType(parent.type); + return parent; + } + function populate(object, params) { + for (let key in params) { + defineReadOnly(object, key, params[key]); + } + } + const FormatTypes = Object.freeze({ + // Bare formatting, as is needed for computing a sighash of an event or function + sighash: "sighash", + // Human-Readable with Minimal spacing and without names (compact human-readable) + minimal: "minimal", + // Human-Readable with nice spacing, including all names + full: "full", + // JSON-format a la Solidity + json: "json" + }); + const paramTypeArray = new RegExp(/^(.*)\[([0-9]*)\]$/); + class ParamType { + constructor(constructorGuard, params) { + if (constructorGuard !== _constructorGuard) { + logger$b.throwError("use fromString", Logger.errors.UNSUPPORTED_OPERATION, { + operation: "new ParamType()" + }); + } + populate(this, params); + let match = this.type.match(paramTypeArray); + if (match) { + populate(this, { + arrayLength: parseInt(match[2] || "-1"), + arrayChildren: ParamType.fromObject({ + type: match[1], + components: this.components + }), + baseType: "array" + }); + } + else { + populate(this, { + arrayLength: null, + arrayChildren: null, + baseType: ((this.components != null) ? "tuple" : this.type) + }); + } + this._isParamType = true; + Object.freeze(this); + } + // Format the parameter fragment + // - sighash: "(uint256,address)" + // - minimal: "tuple(uint256,address) indexed" + // - full: "tuple(uint256 foo, address bar) indexed baz" + format(format) { + if (!format) { + format = FormatTypes.sighash; + } + if (!FormatTypes[format]) { + logger$b.throwArgumentError("invalid format type", "format", format); + } + if (format === FormatTypes.json) { + let result = { + type: ((this.baseType === "tuple") ? "tuple" : this.type), + name: (this.name || undefined) + }; + if (typeof (this.indexed) === "boolean") { + result.indexed = this.indexed; + } + if (this.components) { + result.components = this.components.map((comp) => JSON.parse(comp.format(format))); + } + return JSON.stringify(result); + } + let result = ""; + // Array + if (this.baseType === "array") { + result += this.arrayChildren.format(format); + result += "[" + (this.arrayLength < 0 ? "" : String(this.arrayLength)) + "]"; + } + else { + if (this.baseType === "tuple") { + if (format !== FormatTypes.sighash) { + result += this.type; + } + result += "(" + this.components.map((comp) => comp.format(format)).join((format === FormatTypes.full) ? ", " : ",") + ")"; + } + else { + result += this.type; + } + } + if (format !== FormatTypes.sighash) { + if (this.indexed === true) { + result += " indexed"; + } + if (format === FormatTypes.full && this.name) { + result += " " + this.name; + } + } + return result; + } + static from(value, allowIndexed) { + if (typeof (value) === "string") { + return ParamType.fromString(value, allowIndexed); + } + return ParamType.fromObject(value); + } + static fromObject(value) { + if (ParamType.isParamType(value)) { + return value; + } + return new ParamType(_constructorGuard, { + name: (value.name || null), + type: verifyType(value.type), + indexed: ((value.indexed == null) ? null : !!value.indexed), + components: (value.components ? value.components.map(ParamType.fromObject) : null) + }); + } + static fromString(value, allowIndexed) { + function ParamTypify(node) { + return ParamType.fromObject({ + name: node.name, + type: node.type, + indexed: node.indexed, + components: node.components + }); + } + return ParamTypify(parseParamType(value, !!allowIndexed)); + } + static isParamType(value) { + return !!(value != null && value._isParamType); + } + } + function parseParams(value, allowIndex) { + return splitNesting(value).map((param) => ParamType.fromString(param, allowIndex)); + } + class Fragment { + constructor(constructorGuard, params) { + if (constructorGuard !== _constructorGuard) { + logger$b.throwError("use a static from method", Logger.errors.UNSUPPORTED_OPERATION, { + operation: "new Fragment()" + }); + } + populate(this, params); + this._isFragment = true; + Object.freeze(this); + } + static from(value) { + if (Fragment.isFragment(value)) { + return value; + } + if (typeof (value) === "string") { + return Fragment.fromString(value); + } + return Fragment.fromObject(value); + } + static fromObject(value) { + if (Fragment.isFragment(value)) { + return value; + } + switch (value.type) { + case "function": + return FunctionFragment.fromObject(value); + case "event": + return EventFragment.fromObject(value); + case "constructor": + return ConstructorFragment.fromObject(value); + case "error": + return ErrorFragment.fromObject(value); + case "fallback": + case "receive": + // @TODO: Something? Maybe return a FunctionFragment? A custom DefaultFunctionFragment? + return null; + } + return logger$b.throwArgumentError("invalid fragment object", "value", value); + } + static fromString(value) { + // Make sure the "returns" is surrounded by a space and all whitespace is exactly one space + value = value.replace(/\s/g, " "); + value = value.replace(/\(/g, " (").replace(/\)/g, ") ").replace(/\s+/g, " "); + value = value.trim(); + if (value.split(" ")[0] === "event") { + return EventFragment.fromString(value.substring(5).trim()); + } + else if (value.split(" ")[0] === "function") { + return FunctionFragment.fromString(value.substring(8).trim()); + } + else if (value.split("(")[0].trim() === "constructor") { + return ConstructorFragment.fromString(value.trim()); + } + else if (value.split(" ")[0] === "error") { + return ErrorFragment.fromString(value.substring(5).trim()); + } + return logger$b.throwArgumentError("unsupported fragment", "value", value); + } + static isFragment(value) { + return !!(value && value._isFragment); + } + } + class EventFragment extends Fragment { + format(format) { + if (!format) { + format = FormatTypes.sighash; + } + if (!FormatTypes[format]) { + logger$b.throwArgumentError("invalid format type", "format", format); + } + if (format === FormatTypes.json) { + return JSON.stringify({ + type: "event", + anonymous: this.anonymous, + name: this.name, + inputs: this.inputs.map((input) => JSON.parse(input.format(format))) + }); + } + let result = ""; + if (format !== FormatTypes.sighash) { + result += "event "; + } + result += this.name + "(" + this.inputs.map((input) => input.format(format)).join((format === FormatTypes.full) ? ", " : ",") + ") "; + if (format !== FormatTypes.sighash) { + if (this.anonymous) { + result += "anonymous "; + } + } + return result.trim(); + } + static from(value) { + if (typeof (value) === "string") { + return EventFragment.fromString(value); + } + return EventFragment.fromObject(value); + } + static fromObject(value) { + if (EventFragment.isEventFragment(value)) { + return value; + } + if (value.type !== "event") { + logger$b.throwArgumentError("invalid event object", "value", value); + } + const params = { + name: verifyIdentifier(value.name), + anonymous: value.anonymous, + inputs: (value.inputs ? value.inputs.map(ParamType.fromObject) : []), + type: "event" + }; + return new EventFragment(_constructorGuard, params); + } + static fromString(value) { + let match = value.match(regexParen); + if (!match) { + logger$b.throwArgumentError("invalid event string", "value", value); + } + let anonymous = false; + match[3].split(" ").forEach((modifier) => { + switch (modifier.trim()) { + case "anonymous": + anonymous = true; + break; + case "": + break; + default: + logger$b.warn("unknown modifier: " + modifier); + } + }); + return EventFragment.fromObject({ + name: match[1].trim(), + anonymous: anonymous, + inputs: parseParams(match[2], true), + type: "event" + }); + } + static isEventFragment(value) { + return (value && value._isFragment && value.type === "event"); + } + } + function parseGas(value, params) { + params.gas = null; + let comps = value.split("@"); + if (comps.length !== 1) { + if (comps.length > 2) { + logger$b.throwArgumentError("invalid human-readable ABI signature", "value", value); + } + if (!comps[1].match(/^[0-9]+$/)) { + logger$b.throwArgumentError("invalid human-readable ABI signature gas", "value", value); + } + params.gas = BigNumber.from(comps[1]); + return comps[0]; + } + return value; + } + function parseModifiers(value, params) { + params.constant = false; + params.payable = false; + params.stateMutability = "nonpayable"; + value.split(" ").forEach((modifier) => { + switch (modifier.trim()) { + case "constant": + params.constant = true; + break; + case "payable": + params.payable = true; + params.stateMutability = "payable"; + break; + case "nonpayable": + params.payable = false; + params.stateMutability = "nonpayable"; + break; + case "pure": + params.constant = true; + params.stateMutability = "pure"; + break; + case "view": + params.constant = true; + params.stateMutability = "view"; + break; + case "external": + case "public": + case "": + break; + default: + console.log("unknown modifier: " + modifier); + } + }); + } + function verifyState(value) { + let result = { + constant: false, + payable: true, + stateMutability: "payable" + }; + if (value.stateMutability != null) { + result.stateMutability = value.stateMutability; + // Set (and check things are consistent) the constant property + result.constant = (result.stateMutability === "view" || result.stateMutability === "pure"); + if (value.constant != null) { + if ((!!value.constant) !== result.constant) { + logger$b.throwArgumentError("cannot have constant function with mutability " + result.stateMutability, "value", value); + } + } + // Set (and check things are consistent) the payable property + result.payable = (result.stateMutability === "payable"); + if (value.payable != null) { + if ((!!value.payable) !== result.payable) { + logger$b.throwArgumentError("cannot have payable function with mutability " + result.stateMutability, "value", value); + } + } + } + else if (value.payable != null) { + result.payable = !!value.payable; + // If payable we can assume non-constant; otherwise we can't assume + if (value.constant == null && !result.payable && value.type !== "constructor") { + logger$b.throwArgumentError("unable to determine stateMutability", "value", value); + } + result.constant = !!value.constant; + if (result.constant) { + result.stateMutability = "view"; + } + else { + result.stateMutability = (result.payable ? "payable" : "nonpayable"); + } + if (result.payable && result.constant) { + logger$b.throwArgumentError("cannot have constant payable function", "value", value); + } + } + else if (value.constant != null) { + result.constant = !!value.constant; + result.payable = !result.constant; + result.stateMutability = (result.constant ? "view" : "payable"); + } + else if (value.type !== "constructor") { + logger$b.throwArgumentError("unable to determine stateMutability", "value", value); + } + return result; + } + class ConstructorFragment extends Fragment { + format(format) { + if (!format) { + format = FormatTypes.sighash; + } + if (!FormatTypes[format]) { + logger$b.throwArgumentError("invalid format type", "format", format); + } + if (format === FormatTypes.json) { + return JSON.stringify({ + type: "constructor", + stateMutability: ((this.stateMutability !== "nonpayable") ? this.stateMutability : undefined), + payable: this.payable, + gas: (this.gas ? this.gas.toNumber() : undefined), + inputs: this.inputs.map((input) => JSON.parse(input.format(format))) + }); + } + if (format === FormatTypes.sighash) { + logger$b.throwError("cannot format a constructor for sighash", Logger.errors.UNSUPPORTED_OPERATION, { + operation: "format(sighash)" + }); + } + let result = "constructor(" + this.inputs.map((input) => input.format(format)).join((format === FormatTypes.full) ? ", " : ",") + ") "; + if (this.stateMutability && this.stateMutability !== "nonpayable") { + result += this.stateMutability + " "; + } + return result.trim(); + } + static from(value) { + if (typeof (value) === "string") { + return ConstructorFragment.fromString(value); + } + return ConstructorFragment.fromObject(value); + } + static fromObject(value) { + if (ConstructorFragment.isConstructorFragment(value)) { + return value; + } + if (value.type !== "constructor") { + logger$b.throwArgumentError("invalid constructor object", "value", value); + } + let state = verifyState(value); + if (state.constant) { + logger$b.throwArgumentError("constructor cannot be constant", "value", value); + } + const params = { + name: null, + type: value.type, + inputs: (value.inputs ? value.inputs.map(ParamType.fromObject) : []), + payable: state.payable, + stateMutability: state.stateMutability, + gas: (value.gas ? BigNumber.from(value.gas) : null) + }; + return new ConstructorFragment(_constructorGuard, params); + } + static fromString(value) { + let params = { type: "constructor" }; + value = parseGas(value, params); + let parens = value.match(regexParen); + if (!parens || parens[1].trim() !== "constructor") { + logger$b.throwArgumentError("invalid constructor string", "value", value); + } + params.inputs = parseParams(parens[2].trim(), false); + parseModifiers(parens[3].trim(), params); + return ConstructorFragment.fromObject(params); + } + static isConstructorFragment(value) { + return (value && value._isFragment && value.type === "constructor"); + } + } + class FunctionFragment extends ConstructorFragment { + format(format) { + if (!format) { + format = FormatTypes.sighash; + } + if (!FormatTypes[format]) { + logger$b.throwArgumentError("invalid format type", "format", format); + } + if (format === FormatTypes.json) { + return JSON.stringify({ + type: "function", + name: this.name, + constant: this.constant, + stateMutability: ((this.stateMutability !== "nonpayable") ? this.stateMutability : undefined), + payable: this.payable, + gas: (this.gas ? this.gas.toNumber() : undefined), + inputs: this.inputs.map((input) => JSON.parse(input.format(format))), + outputs: this.outputs.map((output) => JSON.parse(output.format(format))), + }); + } + let result = ""; + if (format !== FormatTypes.sighash) { + result += "function "; + } + result += this.name + "(" + this.inputs.map((input) => input.format(format)).join((format === FormatTypes.full) ? ", " : ",") + ") "; + if (format !== FormatTypes.sighash) { + if (this.stateMutability) { + if (this.stateMutability !== "nonpayable") { + result += (this.stateMutability + " "); + } + } + else if (this.constant) { + result += "view "; + } + if (this.outputs && this.outputs.length) { + result += "returns (" + this.outputs.map((output) => output.format(format)).join(", ") + ") "; + } + if (this.gas != null) { + result += "@" + this.gas.toString() + " "; + } + } + return result.trim(); + } + static from(value) { + if (typeof (value) === "string") { + return FunctionFragment.fromString(value); + } + return FunctionFragment.fromObject(value); + } + static fromObject(value) { + if (FunctionFragment.isFunctionFragment(value)) { + return value; + } + if (value.type !== "function") { + logger$b.throwArgumentError("invalid function object", "value", value); + } + let state = verifyState(value); + const params = { + type: value.type, + name: verifyIdentifier(value.name), + constant: state.constant, + inputs: (value.inputs ? value.inputs.map(ParamType.fromObject) : []), + outputs: (value.outputs ? value.outputs.map(ParamType.fromObject) : []), + payable: state.payable, + stateMutability: state.stateMutability, + gas: (value.gas ? BigNumber.from(value.gas) : null) + }; + return new FunctionFragment(_constructorGuard, params); + } + static fromString(value) { + let params = { type: "function" }; + value = parseGas(value, params); + let comps = value.split(" returns "); + if (comps.length > 2) { + logger$b.throwArgumentError("invalid function string", "value", value); + } + let parens = comps[0].match(regexParen); + if (!parens) { + logger$b.throwArgumentError("invalid function signature", "value", value); + } + params.name = parens[1].trim(); + if (params.name) { + verifyIdentifier(params.name); + } + params.inputs = parseParams(parens[2], false); + parseModifiers(parens[3].trim(), params); + // We have outputs + if (comps.length > 1) { + let returns = comps[1].match(regexParen); + if (returns[1].trim() != "" || returns[3].trim() != "") { + logger$b.throwArgumentError("unexpected tokens", "value", value); + } + params.outputs = parseParams(returns[2], false); + } + else { + params.outputs = []; + } + return FunctionFragment.fromObject(params); + } + static isFunctionFragment(value) { + return (value && value._isFragment && value.type === "function"); + } + } + //export class StructFragment extends Fragment { + //} + function checkForbidden(fragment) { + const sig = fragment.format(); + if (sig === "Error(string)" || sig === "Panic(uint256)") { + logger$b.throwArgumentError(`cannot specify user defined ${sig} error`, "fragment", fragment); + } + return fragment; + } + class ErrorFragment extends Fragment { + format(format) { + if (!format) { + format = FormatTypes.sighash; + } + if (!FormatTypes[format]) { + logger$b.throwArgumentError("invalid format type", "format", format); + } + if (format === FormatTypes.json) { + return JSON.stringify({ + type: "error", + name: this.name, + inputs: this.inputs.map((input) => JSON.parse(input.format(format))), + }); + } + let result = ""; + if (format !== FormatTypes.sighash) { + result += "error "; + } + result += this.name + "(" + this.inputs.map((input) => input.format(format)).join((format === FormatTypes.full) ? ", " : ",") + ") "; + return result.trim(); + } + static from(value) { + if (typeof (value) === "string") { + return ErrorFragment.fromString(value); + } + return ErrorFragment.fromObject(value); + } + static fromObject(value) { + if (ErrorFragment.isErrorFragment(value)) { + return value; + } + if (value.type !== "error") { + logger$b.throwArgumentError("invalid error object", "value", value); + } + const params = { + type: value.type, + name: verifyIdentifier(value.name), + inputs: (value.inputs ? value.inputs.map(ParamType.fromObject) : []) + }; + return checkForbidden(new ErrorFragment(_constructorGuard, params)); + } + static fromString(value) { + let params = { type: "error" }; + let parens = value.match(regexParen); + if (!parens) { + logger$b.throwArgumentError("invalid error signature", "value", value); + } + params.name = parens[1].trim(); + if (params.name) { + verifyIdentifier(params.name); + } + params.inputs = parseParams(parens[2], false); + return checkForbidden(ErrorFragment.fromObject(params)); + } + static isErrorFragment(value) { + return (value && value._isFragment && value.type === "error"); + } + } + function verifyType(type) { + // These need to be transformed to their full description + if (type.match(/^uint($|[^1-9])/)) { + type = "uint256" + type.substring(4); + } + else if (type.match(/^int($|[^1-9])/)) { + type = "int256" + type.substring(3); + } + // @TODO: more verification + return type; + } + // See: https://github.com/ethereum/solidity/blob/1f8f1a3db93a548d0555e3e14cfc55a10e25b60e/docs/grammar/SolidityLexer.g4#L234 + const regexIdentifier = new RegExp("^[a-zA-Z$_][a-zA-Z0-9$_]*$"); + function verifyIdentifier(value) { + if (!value || !value.match(regexIdentifier)) { + logger$b.throwArgumentError(`invalid identifier "${value}"`, "value", value); + } + return value; + } + const regexParen = new RegExp("^([^)(]*)\\((.*)\\)([^)(]*)$"); + function splitNesting(value) { + value = value.trim(); + let result = []; + let accum = ""; + let depth = 0; + for (let offset = 0; offset < value.length; offset++) { + let c = value[offset]; + if (c === "," && depth === 0) { + result.push(accum); + accum = ""; + } + else { + accum += c; + if (c === "(") { + depth++; + } + else if (c === ")") { + depth--; + if (depth === -1) { + logger$b.throwArgumentError("unbalanced parenthesis", "value", value); + } + } + } + } + if (accum) { + result.push(accum); + } + return result; + } + + const logger$a = new Logger(version$7); + function checkResultErrors(result) { + // Find the first error (if any) + const errors = []; + const checkErrors = function (path, object) { + if (!Array.isArray(object)) { + return; + } + for (let key in object) { + const childPath = path.slice(); + childPath.push(key); + try { + checkErrors(childPath, object[key]); + } + catch (error) { + errors.push({ path: childPath, error: error }); + } + } + }; + checkErrors([], result); + return errors; + } + class Coder { + constructor(name, type, localName, dynamic) { + // @TODO: defineReadOnly these + this.name = name; + this.type = type; + this.localName = localName; + this.dynamic = dynamic; + } + _throwError(message, value) { + logger$a.throwArgumentError(message, this.localName, value); + } + } + class Writer { + constructor(wordSize) { + defineReadOnly(this, "wordSize", wordSize || 32); + this._data = []; + this._dataLength = 0; + this._padding = new Uint8Array(wordSize); + } + get data() { + return hexConcat(this._data); + } + get length() { return this._dataLength; } + _writeData(data) { + this._data.push(data); + this._dataLength += data.length; + return data.length; + } + appendWriter(writer) { + return this._writeData(concat(writer._data)); + } + // Arrayish items; padded on the right to wordSize + writeBytes(value) { + let bytes = arrayify(value); + const paddingOffset = bytes.length % this.wordSize; + if (paddingOffset) { + bytes = concat([bytes, this._padding.slice(paddingOffset)]); + } + return this._writeData(bytes); + } + _getValue(value) { + let bytes = arrayify(BigNumber.from(value)); + if (bytes.length > this.wordSize) { + logger$a.throwError("value out-of-bounds", Logger.errors.BUFFER_OVERRUN, { + length: this.wordSize, + offset: bytes.length + }); + } + if (bytes.length % this.wordSize) { + bytes = concat([this._padding.slice(bytes.length % this.wordSize), bytes]); + } + return bytes; + } + // BigNumberish items; padded on the left to wordSize + writeValue(value) { + return this._writeData(this._getValue(value)); + } + writeUpdatableValue() { + const offset = this._data.length; + this._data.push(this._padding); + this._dataLength += this.wordSize; + return (value) => { + this._data[offset] = this._getValue(value); + }; + } + } + class Reader { + constructor(data, wordSize, coerceFunc, allowLoose) { + defineReadOnly(this, "_data", arrayify(data)); + defineReadOnly(this, "wordSize", wordSize || 32); + defineReadOnly(this, "_coerceFunc", coerceFunc); + defineReadOnly(this, "allowLoose", allowLoose); + this._offset = 0; + } + get data() { return hexlify(this._data); } + get consumed() { return this._offset; } + // The default Coerce function + static coerce(name, value) { + let match = name.match("^u?int([0-9]+)$"); + if (match && parseInt(match[1]) <= 48) { + value = value.toNumber(); + } + return value; + } + coerce(name, value) { + if (this._coerceFunc) { + return this._coerceFunc(name, value); + } + return Reader.coerce(name, value); + } + _peekBytes(offset, length, loose) { + let alignedLength = Math.ceil(length / this.wordSize) * this.wordSize; + if (this._offset + alignedLength > this._data.length) { + if (this.allowLoose && loose && this._offset + length <= this._data.length) { + alignedLength = length; + } + else { + logger$a.throwError("data out-of-bounds", Logger.errors.BUFFER_OVERRUN, { + length: this._data.length, + offset: this._offset + alignedLength + }); + } + } + return this._data.slice(this._offset, this._offset + alignedLength); + } + subReader(offset) { + return new Reader(this._data.slice(this._offset + offset), this.wordSize, this._coerceFunc, this.allowLoose); + } + readBytes(length, loose) { + let bytes = this._peekBytes(0, length, !!loose); + this._offset += bytes.length; + // @TODO: Make sure the length..end bytes are all 0? + return bytes.slice(0, length); + } + readValue() { + return BigNumber.from(this.readBytes(this.wordSize)); + } + } + + var sha3$1 = {exports: {}}; + + /** + * [js-sha3]{@link https://github.com/emn178/js-sha3} + * + * @version 0.8.0 + * @author Chen, Yi-Cyuan [emn178@gmail.com] + * @copyright Chen, Yi-Cyuan 2015-2018 + * @license MIT + */ + + (function (module) { + /*jslint bitwise: true */ + (function () { + + var INPUT_ERROR = 'input is invalid type'; + var FINALIZE_ERROR = 'finalize already called'; + var WINDOW = typeof window === 'object'; + var root = WINDOW ? window : {}; + if (root.JS_SHA3_NO_WINDOW) { + WINDOW = false; + } + var WEB_WORKER = !WINDOW && typeof self === 'object'; + var NODE_JS = !root.JS_SHA3_NO_NODE_JS && typeof process === 'object' && process.versions && process.versions.node; + if (NODE_JS) { + root = commonjsGlobal; + } else if (WEB_WORKER) { + root = self; + } + var COMMON_JS = !root.JS_SHA3_NO_COMMON_JS && 'object' === 'object' && module.exports; + var ARRAY_BUFFER = !root.JS_SHA3_NO_ARRAY_BUFFER && typeof ArrayBuffer !== 'undefined'; + var HEX_CHARS = '0123456789abcdef'.split(''); + var SHAKE_PADDING = [31, 7936, 2031616, 520093696]; + var CSHAKE_PADDING = [4, 1024, 262144, 67108864]; + var KECCAK_PADDING = [1, 256, 65536, 16777216]; + var PADDING = [6, 1536, 393216, 100663296]; + var SHIFT = [0, 8, 16, 24]; + var RC = [1, 0, 32898, 0, 32906, 2147483648, 2147516416, 2147483648, 32907, 0, 2147483649, + 0, 2147516545, 2147483648, 32777, 2147483648, 138, 0, 136, 0, 2147516425, 0, + 2147483658, 0, 2147516555, 0, 139, 2147483648, 32905, 2147483648, 32771, + 2147483648, 32770, 2147483648, 128, 2147483648, 32778, 0, 2147483658, 2147483648, + 2147516545, 2147483648, 32896, 2147483648, 2147483649, 0, 2147516424, 2147483648]; + var BITS = [224, 256, 384, 512]; + var SHAKE_BITS = [128, 256]; + var OUTPUT_TYPES = ['hex', 'buffer', 'arrayBuffer', 'array', 'digest']; + var CSHAKE_BYTEPAD = { + '128': 168, + '256': 136 + }; + + if (root.JS_SHA3_NO_NODE_JS || !Array.isArray) { + Array.isArray = function (obj) { + return Object.prototype.toString.call(obj) === '[object Array]'; + }; + } + + if (ARRAY_BUFFER && (root.JS_SHA3_NO_ARRAY_BUFFER_IS_VIEW || !ArrayBuffer.isView)) { + ArrayBuffer.isView = function (obj) { + return typeof obj === 'object' && obj.buffer && obj.buffer.constructor === ArrayBuffer; + }; + } + + var createOutputMethod = function (bits, padding, outputType) { + return function (message) { + return new Keccak(bits, padding, bits).update(message)[outputType](); + }; + }; + + var createShakeOutputMethod = function (bits, padding, outputType) { + return function (message, outputBits) { + return new Keccak(bits, padding, outputBits).update(message)[outputType](); + }; + }; + + var createCshakeOutputMethod = function (bits, padding, outputType) { + return function (message, outputBits, n, s) { + return methods['cshake' + bits].update(message, outputBits, n, s)[outputType](); + }; + }; + + var createKmacOutputMethod = function (bits, padding, outputType) { + return function (key, message, outputBits, s) { + return methods['kmac' + bits].update(key, message, outputBits, s)[outputType](); + }; + }; + + var createOutputMethods = function (method, createMethod, bits, padding) { + for (var i = 0; i < OUTPUT_TYPES.length; ++i) { + var type = OUTPUT_TYPES[i]; + method[type] = createMethod(bits, padding, type); + } + return method; + }; + + var createMethod = function (bits, padding) { + var method = createOutputMethod(bits, padding, 'hex'); + method.create = function () { + return new Keccak(bits, padding, bits); + }; + method.update = function (message) { + return method.create().update(message); + }; + return createOutputMethods(method, createOutputMethod, bits, padding); + }; + + var createShakeMethod = function (bits, padding) { + var method = createShakeOutputMethod(bits, padding, 'hex'); + method.create = function (outputBits) { + return new Keccak(bits, padding, outputBits); + }; + method.update = function (message, outputBits) { + return method.create(outputBits).update(message); + }; + return createOutputMethods(method, createShakeOutputMethod, bits, padding); + }; + + var createCshakeMethod = function (bits, padding) { + var w = CSHAKE_BYTEPAD[bits]; + var method = createCshakeOutputMethod(bits, padding, 'hex'); + method.create = function (outputBits, n, s) { + if (!n && !s) { + return methods['shake' + bits].create(outputBits); + } else { + return new Keccak(bits, padding, outputBits).bytepad([n, s], w); + } + }; + method.update = function (message, outputBits, n, s) { + return method.create(outputBits, n, s).update(message); + }; + return createOutputMethods(method, createCshakeOutputMethod, bits, padding); + }; + + var createKmacMethod = function (bits, padding) { + var w = CSHAKE_BYTEPAD[bits]; + var method = createKmacOutputMethod(bits, padding, 'hex'); + method.create = function (key, outputBits, s) { + return new Kmac(bits, padding, outputBits).bytepad(['KMAC', s], w).bytepad([key], w); + }; + method.update = function (key, message, outputBits, s) { + return method.create(key, outputBits, s).update(message); + }; + return createOutputMethods(method, createKmacOutputMethod, bits, padding); + }; + + var algorithms = [ + { name: 'keccak', padding: KECCAK_PADDING, bits: BITS, createMethod: createMethod }, + { name: 'sha3', padding: PADDING, bits: BITS, createMethod: createMethod }, + { name: 'shake', padding: SHAKE_PADDING, bits: SHAKE_BITS, createMethod: createShakeMethod }, + { name: 'cshake', padding: CSHAKE_PADDING, bits: SHAKE_BITS, createMethod: createCshakeMethod }, + { name: 'kmac', padding: CSHAKE_PADDING, bits: SHAKE_BITS, createMethod: createKmacMethod } + ]; + + var methods = {}, methodNames = []; + + for (var i = 0; i < algorithms.length; ++i) { + var algorithm = algorithms[i]; + var bits = algorithm.bits; + for (var j = 0; j < bits.length; ++j) { + var methodName = algorithm.name + '_' + bits[j]; + methodNames.push(methodName); + methods[methodName] = algorithm.createMethod(bits[j], algorithm.padding); + if (algorithm.name !== 'sha3') { + var newMethodName = algorithm.name + bits[j]; + methodNames.push(newMethodName); + methods[newMethodName] = methods[methodName]; + } + } + } + + function Keccak(bits, padding, outputBits) { + this.blocks = []; + this.s = []; + this.padding = padding; + this.outputBits = outputBits; + this.reset = true; + this.finalized = false; + this.block = 0; + this.start = 0; + this.blockCount = (1600 - (bits << 1)) >> 5; + this.byteCount = this.blockCount << 2; + this.outputBlocks = outputBits >> 5; + this.extraBytes = (outputBits & 31) >> 3; + + for (var i = 0; i < 50; ++i) { + this.s[i] = 0; + } + } + + Keccak.prototype.update = function (message) { + if (this.finalized) { + throw new Error(FINALIZE_ERROR); + } + var notString, type = typeof message; + if (type !== 'string') { + if (type === 'object') { + if (message === null) { + throw new Error(INPUT_ERROR); + } else if (ARRAY_BUFFER && message.constructor === ArrayBuffer) { + message = new Uint8Array(message); + } else if (!Array.isArray(message)) { + if (!ARRAY_BUFFER || !ArrayBuffer.isView(message)) { + throw new Error(INPUT_ERROR); + } + } + } else { + throw new Error(INPUT_ERROR); + } + notString = true; + } + var blocks = this.blocks, byteCount = this.byteCount, length = message.length, + blockCount = this.blockCount, index = 0, s = this.s, i, code; + + while (index < length) { + if (this.reset) { + this.reset = false; + blocks[0] = this.block; + for (i = 1; i < blockCount + 1; ++i) { + blocks[i] = 0; + } + } + if (notString) { + for (i = this.start; index < length && i < byteCount; ++index) { + blocks[i >> 2] |= message[index] << SHIFT[i++ & 3]; + } + } else { + for (i = this.start; index < length && i < byteCount; ++index) { + code = message.charCodeAt(index); + if (code < 0x80) { + blocks[i >> 2] |= code << SHIFT[i++ & 3]; + } else if (code < 0x800) { + blocks[i >> 2] |= (0xc0 | (code >> 6)) << SHIFT[i++ & 3]; + blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3]; + } else if (code < 0xd800 || code >= 0xe000) { + blocks[i >> 2] |= (0xe0 | (code >> 12)) << SHIFT[i++ & 3]; + blocks[i >> 2] |= (0x80 | ((code >> 6) & 0x3f)) << SHIFT[i++ & 3]; + blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3]; + } else { + code = 0x10000 + (((code & 0x3ff) << 10) | (message.charCodeAt(++index) & 0x3ff)); + blocks[i >> 2] |= (0xf0 | (code >> 18)) << SHIFT[i++ & 3]; + blocks[i >> 2] |= (0x80 | ((code >> 12) & 0x3f)) << SHIFT[i++ & 3]; + blocks[i >> 2] |= (0x80 | ((code >> 6) & 0x3f)) << SHIFT[i++ & 3]; + blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3]; + } + } + } + this.lastByteIndex = i; + if (i >= byteCount) { + this.start = i - byteCount; + this.block = blocks[blockCount]; + for (i = 0; i < blockCount; ++i) { + s[i] ^= blocks[i]; + } + f(s); + this.reset = true; + } else { + this.start = i; + } + } + return this; + }; + + Keccak.prototype.encode = function (x, right) { + var o = x & 255, n = 1; + var bytes = [o]; + x = x >> 8; + o = x & 255; + while (o > 0) { + bytes.unshift(o); + x = x >> 8; + o = x & 255; + ++n; + } + if (right) { + bytes.push(n); + } else { + bytes.unshift(n); + } + this.update(bytes); + return bytes.length; + }; + + Keccak.prototype.encodeString = function (str) { + var notString, type = typeof str; + if (type !== 'string') { + if (type === 'object') { + if (str === null) { + throw new Error(INPUT_ERROR); + } else if (ARRAY_BUFFER && str.constructor === ArrayBuffer) { + str = new Uint8Array(str); + } else if (!Array.isArray(str)) { + if (!ARRAY_BUFFER || !ArrayBuffer.isView(str)) { + throw new Error(INPUT_ERROR); + } + } + } else { + throw new Error(INPUT_ERROR); + } + notString = true; + } + var bytes = 0, length = str.length; + if (notString) { + bytes = length; + } else { + for (var i = 0; i < str.length; ++i) { + var code = str.charCodeAt(i); + if (code < 0x80) { + bytes += 1; + } else if (code < 0x800) { + bytes += 2; + } else if (code < 0xd800 || code >= 0xe000) { + bytes += 3; + } else { + code = 0x10000 + (((code & 0x3ff) << 10) | (str.charCodeAt(++i) & 0x3ff)); + bytes += 4; + } + } + } + bytes += this.encode(bytes * 8); + this.update(str); + return bytes; + }; + + Keccak.prototype.bytepad = function (strs, w) { + var bytes = this.encode(w); + for (var i = 0; i < strs.length; ++i) { + bytes += this.encodeString(strs[i]); + } + var paddingBytes = w - bytes % w; + var zeros = []; + zeros.length = paddingBytes; + this.update(zeros); + return this; + }; + + Keccak.prototype.finalize = function () { + if (this.finalized) { + return; + } + this.finalized = true; + var blocks = this.blocks, i = this.lastByteIndex, blockCount = this.blockCount, s = this.s; + blocks[i >> 2] |= this.padding[i & 3]; + if (this.lastByteIndex === this.byteCount) { + blocks[0] = blocks[blockCount]; + for (i = 1; i < blockCount + 1; ++i) { + blocks[i] = 0; + } + } + blocks[blockCount - 1] |= 0x80000000; + for (i = 0; i < blockCount; ++i) { + s[i] ^= blocks[i]; + } + f(s); + }; + + Keccak.prototype.toString = Keccak.prototype.hex = function () { + this.finalize(); + + var blockCount = this.blockCount, s = this.s, outputBlocks = this.outputBlocks, + extraBytes = this.extraBytes, i = 0, j = 0; + var hex = '', block; + while (j < outputBlocks) { + for (i = 0; i < blockCount && j < outputBlocks; ++i, ++j) { + block = s[i]; + hex += HEX_CHARS[(block >> 4) & 0x0F] + HEX_CHARS[block & 0x0F] + + HEX_CHARS[(block >> 12) & 0x0F] + HEX_CHARS[(block >> 8) & 0x0F] + + HEX_CHARS[(block >> 20) & 0x0F] + HEX_CHARS[(block >> 16) & 0x0F] + + HEX_CHARS[(block >> 28) & 0x0F] + HEX_CHARS[(block >> 24) & 0x0F]; + } + if (j % blockCount === 0) { + f(s); + i = 0; + } + } + if (extraBytes) { + block = s[i]; + hex += HEX_CHARS[(block >> 4) & 0x0F] + HEX_CHARS[block & 0x0F]; + if (extraBytes > 1) { + hex += HEX_CHARS[(block >> 12) & 0x0F] + HEX_CHARS[(block >> 8) & 0x0F]; + } + if (extraBytes > 2) { + hex += HEX_CHARS[(block >> 20) & 0x0F] + HEX_CHARS[(block >> 16) & 0x0F]; + } + } + return hex; + }; + + Keccak.prototype.arrayBuffer = function () { + this.finalize(); + + var blockCount = this.blockCount, s = this.s, outputBlocks = this.outputBlocks, + extraBytes = this.extraBytes, i = 0, j = 0; + var bytes = this.outputBits >> 3; + var buffer; + if (extraBytes) { + buffer = new ArrayBuffer((outputBlocks + 1) << 2); + } else { + buffer = new ArrayBuffer(bytes); + } + var array = new Uint32Array(buffer); + while (j < outputBlocks) { + for (i = 0; i < blockCount && j < outputBlocks; ++i, ++j) { + array[j] = s[i]; + } + if (j % blockCount === 0) { + f(s); + } + } + if (extraBytes) { + array[i] = s[i]; + buffer = buffer.slice(0, bytes); + } + return buffer; + }; + + Keccak.prototype.buffer = Keccak.prototype.arrayBuffer; + + Keccak.prototype.digest = Keccak.prototype.array = function () { + this.finalize(); + + var blockCount = this.blockCount, s = this.s, outputBlocks = this.outputBlocks, + extraBytes = this.extraBytes, i = 0, j = 0; + var array = [], offset, block; + while (j < outputBlocks) { + for (i = 0; i < blockCount && j < outputBlocks; ++i, ++j) { + offset = j << 2; + block = s[i]; + array[offset] = block & 0xFF; + array[offset + 1] = (block >> 8) & 0xFF; + array[offset + 2] = (block >> 16) & 0xFF; + array[offset + 3] = (block >> 24) & 0xFF; + } + if (j % blockCount === 0) { + f(s); + } + } + if (extraBytes) { + offset = j << 2; + block = s[i]; + array[offset] = block & 0xFF; + if (extraBytes > 1) { + array[offset + 1] = (block >> 8) & 0xFF; + } + if (extraBytes > 2) { + array[offset + 2] = (block >> 16) & 0xFF; + } + } + return array; + }; + + function Kmac(bits, padding, outputBits) { + Keccak.call(this, bits, padding, outputBits); + } + + Kmac.prototype = new Keccak(); + + Kmac.prototype.finalize = function () { + this.encode(this.outputBits, true); + return Keccak.prototype.finalize.call(this); + }; + + var f = function (s) { + var h, l, n, c0, c1, c2, c3, c4, c5, c6, c7, c8, c9, + b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, b13, b14, b15, b16, b17, + b18, b19, b20, b21, b22, b23, b24, b25, b26, b27, b28, b29, b30, b31, b32, b33, + b34, b35, b36, b37, b38, b39, b40, b41, b42, b43, b44, b45, b46, b47, b48, b49; + for (n = 0; n < 48; n += 2) { + c0 = s[0] ^ s[10] ^ s[20] ^ s[30] ^ s[40]; + c1 = s[1] ^ s[11] ^ s[21] ^ s[31] ^ s[41]; + c2 = s[2] ^ s[12] ^ s[22] ^ s[32] ^ s[42]; + c3 = s[3] ^ s[13] ^ s[23] ^ s[33] ^ s[43]; + c4 = s[4] ^ s[14] ^ s[24] ^ s[34] ^ s[44]; + c5 = s[5] ^ s[15] ^ s[25] ^ s[35] ^ s[45]; + c6 = s[6] ^ s[16] ^ s[26] ^ s[36] ^ s[46]; + c7 = s[7] ^ s[17] ^ s[27] ^ s[37] ^ s[47]; + c8 = s[8] ^ s[18] ^ s[28] ^ s[38] ^ s[48]; + c9 = s[9] ^ s[19] ^ s[29] ^ s[39] ^ s[49]; + + h = c8 ^ ((c2 << 1) | (c3 >>> 31)); + l = c9 ^ ((c3 << 1) | (c2 >>> 31)); + s[0] ^= h; + s[1] ^= l; + s[10] ^= h; + s[11] ^= l; + s[20] ^= h; + s[21] ^= l; + s[30] ^= h; + s[31] ^= l; + s[40] ^= h; + s[41] ^= l; + h = c0 ^ ((c4 << 1) | (c5 >>> 31)); + l = c1 ^ ((c5 << 1) | (c4 >>> 31)); + s[2] ^= h; + s[3] ^= l; + s[12] ^= h; + s[13] ^= l; + s[22] ^= h; + s[23] ^= l; + s[32] ^= h; + s[33] ^= l; + s[42] ^= h; + s[43] ^= l; + h = c2 ^ ((c6 << 1) | (c7 >>> 31)); + l = c3 ^ ((c7 << 1) | (c6 >>> 31)); + s[4] ^= h; + s[5] ^= l; + s[14] ^= h; + s[15] ^= l; + s[24] ^= h; + s[25] ^= l; + s[34] ^= h; + s[35] ^= l; + s[44] ^= h; + s[45] ^= l; + h = c4 ^ ((c8 << 1) | (c9 >>> 31)); + l = c5 ^ ((c9 << 1) | (c8 >>> 31)); + s[6] ^= h; + s[7] ^= l; + s[16] ^= h; + s[17] ^= l; + s[26] ^= h; + s[27] ^= l; + s[36] ^= h; + s[37] ^= l; + s[46] ^= h; + s[47] ^= l; + h = c6 ^ ((c0 << 1) | (c1 >>> 31)); + l = c7 ^ ((c1 << 1) | (c0 >>> 31)); + s[8] ^= h; + s[9] ^= l; + s[18] ^= h; + s[19] ^= l; + s[28] ^= h; + s[29] ^= l; + s[38] ^= h; + s[39] ^= l; + s[48] ^= h; + s[49] ^= l; + + b0 = s[0]; + b1 = s[1]; + b32 = (s[11] << 4) | (s[10] >>> 28); + b33 = (s[10] << 4) | (s[11] >>> 28); + b14 = (s[20] << 3) | (s[21] >>> 29); + b15 = (s[21] << 3) | (s[20] >>> 29); + b46 = (s[31] << 9) | (s[30] >>> 23); + b47 = (s[30] << 9) | (s[31] >>> 23); + b28 = (s[40] << 18) | (s[41] >>> 14); + b29 = (s[41] << 18) | (s[40] >>> 14); + b20 = (s[2] << 1) | (s[3] >>> 31); + b21 = (s[3] << 1) | (s[2] >>> 31); + b2 = (s[13] << 12) | (s[12] >>> 20); + b3 = (s[12] << 12) | (s[13] >>> 20); + b34 = (s[22] << 10) | (s[23] >>> 22); + b35 = (s[23] << 10) | (s[22] >>> 22); + b16 = (s[33] << 13) | (s[32] >>> 19); + b17 = (s[32] << 13) | (s[33] >>> 19); + b48 = (s[42] << 2) | (s[43] >>> 30); + b49 = (s[43] << 2) | (s[42] >>> 30); + b40 = (s[5] << 30) | (s[4] >>> 2); + b41 = (s[4] << 30) | (s[5] >>> 2); + b22 = (s[14] << 6) | (s[15] >>> 26); + b23 = (s[15] << 6) | (s[14] >>> 26); + b4 = (s[25] << 11) | (s[24] >>> 21); + b5 = (s[24] << 11) | (s[25] >>> 21); + b36 = (s[34] << 15) | (s[35] >>> 17); + b37 = (s[35] << 15) | (s[34] >>> 17); + b18 = (s[45] << 29) | (s[44] >>> 3); + b19 = (s[44] << 29) | (s[45] >>> 3); + b10 = (s[6] << 28) | (s[7] >>> 4); + b11 = (s[7] << 28) | (s[6] >>> 4); + b42 = (s[17] << 23) | (s[16] >>> 9); + b43 = (s[16] << 23) | (s[17] >>> 9); + b24 = (s[26] << 25) | (s[27] >>> 7); + b25 = (s[27] << 25) | (s[26] >>> 7); + b6 = (s[36] << 21) | (s[37] >>> 11); + b7 = (s[37] << 21) | (s[36] >>> 11); + b38 = (s[47] << 24) | (s[46] >>> 8); + b39 = (s[46] << 24) | (s[47] >>> 8); + b30 = (s[8] << 27) | (s[9] >>> 5); + b31 = (s[9] << 27) | (s[8] >>> 5); + b12 = (s[18] << 20) | (s[19] >>> 12); + b13 = (s[19] << 20) | (s[18] >>> 12); + b44 = (s[29] << 7) | (s[28] >>> 25); + b45 = (s[28] << 7) | (s[29] >>> 25); + b26 = (s[38] << 8) | (s[39] >>> 24); + b27 = (s[39] << 8) | (s[38] >>> 24); + b8 = (s[48] << 14) | (s[49] >>> 18); + b9 = (s[49] << 14) | (s[48] >>> 18); + + s[0] = b0 ^ (~b2 & b4); + s[1] = b1 ^ (~b3 & b5); + s[10] = b10 ^ (~b12 & b14); + s[11] = b11 ^ (~b13 & b15); + s[20] = b20 ^ (~b22 & b24); + s[21] = b21 ^ (~b23 & b25); + s[30] = b30 ^ (~b32 & b34); + s[31] = b31 ^ (~b33 & b35); + s[40] = b40 ^ (~b42 & b44); + s[41] = b41 ^ (~b43 & b45); + s[2] = b2 ^ (~b4 & b6); + s[3] = b3 ^ (~b5 & b7); + s[12] = b12 ^ (~b14 & b16); + s[13] = b13 ^ (~b15 & b17); + s[22] = b22 ^ (~b24 & b26); + s[23] = b23 ^ (~b25 & b27); + s[32] = b32 ^ (~b34 & b36); + s[33] = b33 ^ (~b35 & b37); + s[42] = b42 ^ (~b44 & b46); + s[43] = b43 ^ (~b45 & b47); + s[4] = b4 ^ (~b6 & b8); + s[5] = b5 ^ (~b7 & b9); + s[14] = b14 ^ (~b16 & b18); + s[15] = b15 ^ (~b17 & b19); + s[24] = b24 ^ (~b26 & b28); + s[25] = b25 ^ (~b27 & b29); + s[34] = b34 ^ (~b36 & b38); + s[35] = b35 ^ (~b37 & b39); + s[44] = b44 ^ (~b46 & b48); + s[45] = b45 ^ (~b47 & b49); + s[6] = b6 ^ (~b8 & b0); + s[7] = b7 ^ (~b9 & b1); + s[16] = b16 ^ (~b18 & b10); + s[17] = b17 ^ (~b19 & b11); + s[26] = b26 ^ (~b28 & b20); + s[27] = b27 ^ (~b29 & b21); + s[36] = b36 ^ (~b38 & b30); + s[37] = b37 ^ (~b39 & b31); + s[46] = b46 ^ (~b48 & b40); + s[47] = b47 ^ (~b49 & b41); + s[8] = b8 ^ (~b0 & b2); + s[9] = b9 ^ (~b1 & b3); + s[18] = b18 ^ (~b10 & b12); + s[19] = b19 ^ (~b11 & b13); + s[28] = b28 ^ (~b20 & b22); + s[29] = b29 ^ (~b21 & b23); + s[38] = b38 ^ (~b30 & b32); + s[39] = b39 ^ (~b31 & b33); + s[48] = b48 ^ (~b40 & b42); + s[49] = b49 ^ (~b41 & b43); + + s[0] ^= RC[n]; + s[1] ^= RC[n + 1]; + } + }; + + if (COMMON_JS) { + module.exports = methods; + } else { + for (i = 0; i < methodNames.length; ++i) { + root[methodNames[i]] = methods[methodNames[i]]; + } + } + })(); + } (sha3$1)); + + var sha3 = sha3$1.exports; + + function keccak256(data) { + return '0x' + sha3.keccak_256(arrayify(data)); + } + + const version$6 = "rlp/5.7.0"; + + const logger$9 = new Logger(version$6); + function arrayifyInteger(value) { + const result = []; + while (value) { + result.unshift(value & 0xff); + value >>= 8; + } + return result; + } + function _encode(object) { + if (Array.isArray(object)) { + let payload = []; + object.forEach(function (child) { + payload = payload.concat(_encode(child)); + }); + if (payload.length <= 55) { + payload.unshift(0xc0 + payload.length); + return payload; + } + const length = arrayifyInteger(payload.length); + length.unshift(0xf7 + length.length); + return length.concat(payload); + } + if (!isBytesLike(object)) { + logger$9.throwArgumentError("RLP object must be BytesLike", "object", object); + } + const data = Array.prototype.slice.call(arrayify(object)); + if (data.length === 1 && data[0] <= 0x7f) { + return data; + } + else if (data.length <= 55) { + data.unshift(0x80 + data.length); + return data; + } + const length = arrayifyInteger(data.length); + length.unshift(0xb7 + length.length); + return length.concat(data); + } + function encode(object) { + return hexlify(_encode(object)); + } + + const version$5 = "address/5.7.0"; + + const logger$8 = new Logger(version$5); + function getChecksumAddress(address) { + if (!isHexString(address, 20)) { + logger$8.throwArgumentError("invalid address", "address", address); + } + address = address.toLowerCase(); + const chars = address.substring(2).split(""); + const expanded = new Uint8Array(40); + for (let i = 0; i < 40; i++) { + expanded[i] = chars[i].charCodeAt(0); + } + const hashed = arrayify(keccak256(expanded)); + for (let i = 0; i < 40; i += 2) { + if ((hashed[i >> 1] >> 4) >= 8) { + chars[i] = chars[i].toUpperCase(); + } + if ((hashed[i >> 1] & 0x0f) >= 8) { + chars[i + 1] = chars[i + 1].toUpperCase(); + } + } + return "0x" + chars.join(""); + } + // Shims for environments that are missing some required constants and functions + const MAX_SAFE_INTEGER = 0x1fffffffffffff; + function log10(x) { + if (Math.log10) { + return Math.log10(x); + } + return Math.log(x) / Math.LN10; + } + // See: https://en.wikipedia.org/wiki/International_Bank_Account_Number + // Create lookup table + const ibanLookup = {}; + for (let i = 0; i < 10; i++) { + ibanLookup[String(i)] = String(i); + } + for (let i = 0; i < 26; i++) { + ibanLookup[String.fromCharCode(65 + i)] = String(10 + i); + } + // How many decimal digits can we process? (for 64-bit float, this is 15) + const safeDigits = Math.floor(log10(MAX_SAFE_INTEGER)); + function ibanChecksum(address) { + address = address.toUpperCase(); + address = address.substring(4) + address.substring(0, 2) + "00"; + let expanded = address.split("").map((c) => { return ibanLookup[c]; }).join(""); + // Javascript can handle integers safely up to 15 (decimal) digits + while (expanded.length >= safeDigits) { + let block = expanded.substring(0, safeDigits); + expanded = parseInt(block, 10) % 97 + expanded.substring(block.length); + } + let checksum = String(98 - (parseInt(expanded, 10) % 97)); + while (checksum.length < 2) { + checksum = "0" + checksum; + } + return checksum; + } + function getAddress(address) { + let result = null; + if (typeof (address) !== "string") { + logger$8.throwArgumentError("invalid address", "address", address); + } + if (address.match(/^(0x)?[0-9a-fA-F]{40}$/)) { + // Missing the 0x prefix + if (address.substring(0, 2) !== "0x") { + address = "0x" + address; + } + result = getChecksumAddress(address); + // It is a checksummed address with a bad checksum + if (address.match(/([A-F].*[a-f])|([a-f].*[A-F])/) && result !== address) { + logger$8.throwArgumentError("bad address checksum", "address", address); + } + // Maybe ICAP? (we only support direct mode) + } + else if (address.match(/^XE[0-9]{2}[0-9A-Za-z]{30,31}$/)) { + // It is an ICAP address with a bad checksum + if (address.substring(2, 4) !== ibanChecksum(address)) { + logger$8.throwArgumentError("bad icap checksum", "address", address); + } + result = _base36To16(address.substring(4)); + while (result.length < 40) { + result = "0" + result; + } + result = getChecksumAddress("0x" + result); + } + else { + logger$8.throwArgumentError("invalid address", "address", address); + } + return result; + } + // http://ethereum.stackexchange.com/questions/760/how-is-the-address-of-an-ethereum-contract-computed + function getContractAddress(transaction) { + let from = null; + try { + from = getAddress(transaction.from); + } + catch (error) { + logger$8.throwArgumentError("missing from address", "transaction", transaction); + } + const nonce = stripZeros(arrayify(BigNumber.from(transaction.nonce).toHexString())); + return getAddress(hexDataSlice(keccak256(encode([from, nonce])), 12)); + } + + class AddressCoder extends Coder { + constructor(localName) { + super("address", "address", localName, false); + } + defaultValue() { + return "0x0000000000000000000000000000000000000000"; + } + encode(writer, value) { + try { + value = getAddress(value); + } + catch (error) { + this._throwError(error.message, value); + } + return writer.writeValue(value); + } + decode(reader) { + return getAddress(hexZeroPad(reader.readValue().toHexString(), 20)); + } + } + + // Clones the functionality of an existing Coder, but without a localName + class AnonymousCoder extends Coder { + constructor(coder) { + super(coder.name, coder.type, undefined, coder.dynamic); + this.coder = coder; + } + defaultValue() { + return this.coder.defaultValue(); + } + encode(writer, value) { + return this.coder.encode(writer, value); + } + decode(reader) { + return this.coder.decode(reader); + } + } + + const logger$7 = new Logger(version$7); + function pack(writer, coders, values) { + let arrayValues = null; + if (Array.isArray(values)) { + arrayValues = values; + } + else if (values && typeof (values) === "object") { + let unique = {}; + arrayValues = coders.map((coder) => { + const name = coder.localName; + if (!name) { + logger$7.throwError("cannot encode object for signature with missing names", Logger.errors.INVALID_ARGUMENT, { + argument: "values", + coder: coder, + value: values + }); + } + if (unique[name]) { + logger$7.throwError("cannot encode object for signature with duplicate names", Logger.errors.INVALID_ARGUMENT, { + argument: "values", + coder: coder, + value: values + }); + } + unique[name] = true; + return values[name]; + }); + } + else { + logger$7.throwArgumentError("invalid tuple value", "tuple", values); + } + if (coders.length !== arrayValues.length) { + logger$7.throwArgumentError("types/value length mismatch", "tuple", values); + } + let staticWriter = new Writer(writer.wordSize); + let dynamicWriter = new Writer(writer.wordSize); + let updateFuncs = []; + coders.forEach((coder, index) => { + let value = arrayValues[index]; + if (coder.dynamic) { + // Get current dynamic offset (for the future pointer) + let dynamicOffset = dynamicWriter.length; + // Encode the dynamic value into the dynamicWriter + coder.encode(dynamicWriter, value); + // Prepare to populate the correct offset once we are done + let updateFunc = staticWriter.writeUpdatableValue(); + updateFuncs.push((baseOffset) => { + updateFunc(baseOffset + dynamicOffset); + }); + } + else { + coder.encode(staticWriter, value); + } + }); + // Backfill all the dynamic offsets, now that we know the static length + updateFuncs.forEach((func) => { func(staticWriter.length); }); + let length = writer.appendWriter(staticWriter); + length += writer.appendWriter(dynamicWriter); + return length; + } + function unpack(reader, coders) { + let values = []; + // A reader anchored to this base + let baseReader = reader.subReader(0); + coders.forEach((coder) => { + let value = null; + if (coder.dynamic) { + let offset = reader.readValue(); + let offsetReader = baseReader.subReader(offset.toNumber()); + try { + value = coder.decode(offsetReader); + } + catch (error) { + // Cannot recover from this + if (error.code === Logger.errors.BUFFER_OVERRUN) { + throw error; + } + value = error; + value.baseType = coder.name; + value.name = coder.localName; + value.type = coder.type; + } + } + else { + try { + value = coder.decode(reader); + } + catch (error) { + // Cannot recover from this + if (error.code === Logger.errors.BUFFER_OVERRUN) { + throw error; + } + value = error; + value.baseType = coder.name; + value.name = coder.localName; + value.type = coder.type; + } + } + if (value != undefined) { + values.push(value); + } + }); + // We only output named properties for uniquely named coders + const uniqueNames = coders.reduce((accum, coder) => { + const name = coder.localName; + if (name) { + if (!accum[name]) { + accum[name] = 0; + } + accum[name]++; + } + return accum; + }, {}); + // Add any named parameters (i.e. tuples) + coders.forEach((coder, index) => { + let name = coder.localName; + if (!name || uniqueNames[name] !== 1) { + return; + } + if (name === "length") { + name = "_length"; + } + if (values[name] != null) { + return; + } + const value = values[index]; + if (value instanceof Error) { + Object.defineProperty(values, name, { + enumerable: true, + get: () => { throw value; } + }); + } + else { + values[name] = value; + } + }); + for (let i = 0; i < values.length; i++) { + const value = values[i]; + if (value instanceof Error) { + Object.defineProperty(values, i, { + enumerable: true, + get: () => { throw value; } + }); + } + } + return Object.freeze(values); + } + class ArrayCoder extends Coder { + constructor(coder, length, localName) { + const type = (coder.type + "[" + (length >= 0 ? length : "") + "]"); + const dynamic = (length === -1 || coder.dynamic); + super("array", type, localName, dynamic); + this.coder = coder; + this.length = length; + } + defaultValue() { + // Verifies the child coder is valid (even if the array is dynamic or 0-length) + const defaultChild = this.coder.defaultValue(); + const result = []; + for (let i = 0; i < this.length; i++) { + result.push(defaultChild); + } + return result; + } + encode(writer, value) { + if (!Array.isArray(value)) { + this._throwError("expected array value", value); + } + let count = this.length; + if (count === -1) { + count = value.length; + writer.writeValue(value.length); + } + logger$7.checkArgumentCount(value.length, count, "coder array" + (this.localName ? (" " + this.localName) : "")); + let coders = []; + for (let i = 0; i < value.length; i++) { + coders.push(this.coder); + } + return pack(writer, coders, value); + } + decode(reader) { + let count = this.length; + if (count === -1) { + count = reader.readValue().toNumber(); + // Check that there is *roughly* enough data to ensure + // stray random data is not being read as a length. Each + // slot requires at least 32 bytes for their value (or 32 + // bytes as a link to the data). This could use a much + // tighter bound, but we are erroring on the side of safety. + if (count * 32 > reader._data.length) { + logger$7.throwError("insufficient data length", Logger.errors.BUFFER_OVERRUN, { + length: reader._data.length, + count: count + }); + } + } + let coders = []; + for (let i = 0; i < count; i++) { + coders.push(new AnonymousCoder(this.coder)); + } + return reader.coerce(this.name, unpack(reader, coders)); + } + } + + class BooleanCoder extends Coder { + constructor(localName) { + super("bool", "bool", localName, false); + } + defaultValue() { + return false; + } + encode(writer, value) { + return writer.writeValue(value ? 1 : 0); + } + decode(reader) { + return reader.coerce(this.type, !reader.readValue().isZero()); + } + } + + class DynamicBytesCoder extends Coder { + constructor(type, localName) { + super(type, type, localName, true); + } + defaultValue() { + return "0x"; + } + encode(writer, value) { + value = arrayify(value); + let length = writer.writeValue(value.length); + length += writer.writeBytes(value); + return length; + } + decode(reader) { + return reader.readBytes(reader.readValue().toNumber(), true); + } + } + class BytesCoder extends DynamicBytesCoder { + constructor(localName) { + super("bytes", localName); + } + decode(reader) { + return reader.coerce(this.name, hexlify(super.decode(reader))); + } + } + + // @TODO: Merge this with bytes + class FixedBytesCoder extends Coder { + constructor(size, localName) { + let name = "bytes" + String(size); + super(name, name, localName, false); + this.size = size; + } + defaultValue() { + return ("0x0000000000000000000000000000000000000000000000000000000000000000").substring(0, 2 + this.size * 2); + } + encode(writer, value) { + let data = arrayify(value); + if (data.length !== this.size) { + this._throwError("incorrect data length", value); + } + return writer.writeBytes(data); + } + decode(reader) { + return reader.coerce(this.name, hexlify(reader.readBytes(this.size))); + } + } + + class NullCoder extends Coder { + constructor(localName) { + super("null", "", localName, false); + } + defaultValue() { + return null; + } + encode(writer, value) { + if (value != null) { + this._throwError("not null", value); + } + return writer.writeBytes([]); + } + decode(reader) { + reader.readBytes(0); + return reader.coerce(this.name, null); + } + } + + const NegativeOne = ( /*#__PURE__*/BigNumber.from(-1)); + const Zero = ( /*#__PURE__*/BigNumber.from(0)); + const One = ( /*#__PURE__*/BigNumber.from(1)); + const MaxUint256 = ( /*#__PURE__*/BigNumber.from("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")); + + class NumberCoder extends Coder { + constructor(size, signed, localName) { + const name = ((signed ? "int" : "uint") + (size * 8)); + super(name, name, localName, false); + this.size = size; + this.signed = signed; + } + defaultValue() { + return 0; + } + encode(writer, value) { + let v = BigNumber.from(value); + // Check bounds are safe for encoding + let maxUintValue = MaxUint256.mask(writer.wordSize * 8); + if (this.signed) { + let bounds = maxUintValue.mask(this.size * 8 - 1); + if (v.gt(bounds) || v.lt(bounds.add(One).mul(NegativeOne))) { + this._throwError("value out-of-bounds", value); + } + } + else if (v.lt(Zero) || v.gt(maxUintValue.mask(this.size * 8))) { + this._throwError("value out-of-bounds", value); + } + v = v.toTwos(this.size * 8).mask(this.size * 8); + if (this.signed) { + v = v.fromTwos(this.size * 8).toTwos(8 * writer.wordSize); + } + return writer.writeValue(v); + } + decode(reader) { + let value = reader.readValue().mask(this.size * 8); + if (this.signed) { + value = value.fromTwos(this.size * 8); + } + return reader.coerce(this.name, value); + } + } + + const version$4 = "strings/5.7.0"; + + const logger$6 = new Logger(version$4); + /////////////////////////////// + var UnicodeNormalizationForm; + (function (UnicodeNormalizationForm) { + UnicodeNormalizationForm["current"] = ""; + UnicodeNormalizationForm["NFC"] = "NFC"; + UnicodeNormalizationForm["NFD"] = "NFD"; + UnicodeNormalizationForm["NFKC"] = "NFKC"; + UnicodeNormalizationForm["NFKD"] = "NFKD"; + })(UnicodeNormalizationForm || (UnicodeNormalizationForm = {})); + var Utf8ErrorReason; + (function (Utf8ErrorReason) { + // A continuation byte was present where there was nothing to continue + // - offset = the index the codepoint began in + Utf8ErrorReason["UNEXPECTED_CONTINUE"] = "unexpected continuation byte"; + // An invalid (non-continuation) byte to start a UTF-8 codepoint was found + // - offset = the index the codepoint began in + Utf8ErrorReason["BAD_PREFIX"] = "bad codepoint prefix"; + // The string is too short to process the expected codepoint + // - offset = the index the codepoint began in + Utf8ErrorReason["OVERRUN"] = "string overrun"; + // A missing continuation byte was expected but not found + // - offset = the index the continuation byte was expected at + Utf8ErrorReason["MISSING_CONTINUE"] = "missing continuation byte"; + // The computed code point is outside the range for UTF-8 + // - offset = start of this codepoint + // - badCodepoint = the computed codepoint; outside the UTF-8 range + Utf8ErrorReason["OUT_OF_RANGE"] = "out of UTF-8 range"; + // UTF-8 strings may not contain UTF-16 surrogate pairs + // - offset = start of this codepoint + // - badCodepoint = the computed codepoint; inside the UTF-16 surrogate range + Utf8ErrorReason["UTF16_SURROGATE"] = "UTF-16 surrogate"; + // The string is an overlong representation + // - offset = start of this codepoint + // - badCodepoint = the computed codepoint; already bounds checked + Utf8ErrorReason["OVERLONG"] = "overlong representation"; + })(Utf8ErrorReason || (Utf8ErrorReason = {})); + function errorFunc(reason, offset, bytes, output, badCodepoint) { + return logger$6.throwArgumentError(`invalid codepoint at offset ${offset}; ${reason}`, "bytes", bytes); + } + function ignoreFunc(reason, offset, bytes, output, badCodepoint) { + // If there is an invalid prefix (including stray continuation), skip any additional continuation bytes + if (reason === Utf8ErrorReason.BAD_PREFIX || reason === Utf8ErrorReason.UNEXPECTED_CONTINUE) { + let i = 0; + for (let o = offset + 1; o < bytes.length; o++) { + if (bytes[o] >> 6 !== 0x02) { + break; + } + i++; + } + return i; + } + // This byte runs us past the end of the string, so just jump to the end + // (but the first byte was read already read and therefore skipped) + if (reason === Utf8ErrorReason.OVERRUN) { + return bytes.length - offset - 1; + } + // Nothing to skip + return 0; + } + function replaceFunc(reason, offset, bytes, output, badCodepoint) { + // Overlong representations are otherwise "valid" code points; just non-deistingtished + if (reason === Utf8ErrorReason.OVERLONG) { + output.push(badCodepoint); + return 0; + } + // Put the replacement character into the output + output.push(0xfffd); + // Otherwise, process as if ignoring errors + return ignoreFunc(reason, offset, bytes); + } + // Common error handing strategies + const Utf8ErrorFuncs = Object.freeze({ + error: errorFunc, + ignore: ignoreFunc, + replace: replaceFunc + }); + // http://stackoverflow.com/questions/13356493/decode-utf-8-with-javascript#13691499 + function getUtf8CodePoints(bytes, onError) { + if (onError == null) { + onError = Utf8ErrorFuncs.error; + } + bytes = arrayify(bytes); + const result = []; + let i = 0; + // Invalid bytes are ignored + while (i < bytes.length) { + const c = bytes[i++]; + // 0xxx xxxx + if (c >> 7 === 0) { + result.push(c); + continue; + } + // Multibyte; how many bytes left for this character? + let extraLength = null; + let overlongMask = null; + // 110x xxxx 10xx xxxx + if ((c & 0xe0) === 0xc0) { + extraLength = 1; + overlongMask = 0x7f; + // 1110 xxxx 10xx xxxx 10xx xxxx + } + else if ((c & 0xf0) === 0xe0) { + extraLength = 2; + overlongMask = 0x7ff; + // 1111 0xxx 10xx xxxx 10xx xxxx 10xx xxxx + } + else if ((c & 0xf8) === 0xf0) { + extraLength = 3; + overlongMask = 0xffff; + } + else { + if ((c & 0xc0) === 0x80) { + i += onError(Utf8ErrorReason.UNEXPECTED_CONTINUE, i - 1, bytes, result); + } + else { + i += onError(Utf8ErrorReason.BAD_PREFIX, i - 1, bytes, result); + } + continue; + } + // Do we have enough bytes in our data? + if (i - 1 + extraLength >= bytes.length) { + i += onError(Utf8ErrorReason.OVERRUN, i - 1, bytes, result); + continue; + } + // Remove the length prefix from the char + let res = c & ((1 << (8 - extraLength - 1)) - 1); + for (let j = 0; j < extraLength; j++) { + let nextChar = bytes[i]; + // Invalid continuation byte + if ((nextChar & 0xc0) != 0x80) { + i += onError(Utf8ErrorReason.MISSING_CONTINUE, i, bytes, result); + res = null; + break; + } + res = (res << 6) | (nextChar & 0x3f); + i++; + } + // See above loop for invalid continuation byte + if (res === null) { + continue; + } + // Maximum code point + if (res > 0x10ffff) { + i += onError(Utf8ErrorReason.OUT_OF_RANGE, i - 1 - extraLength, bytes, result, res); + continue; + } + // Reserved for UTF-16 surrogate halves + if (res >= 0xd800 && res <= 0xdfff) { + i += onError(Utf8ErrorReason.UTF16_SURROGATE, i - 1 - extraLength, bytes, result, res); + continue; + } + // Check for overlong sequences (more bytes than needed) + if (res <= overlongMask) { + i += onError(Utf8ErrorReason.OVERLONG, i - 1 - extraLength, bytes, result, res); + continue; + } + result.push(res); + } + return result; + } + // http://stackoverflow.com/questions/18729405/how-to-convert-utf8-string-to-byte-array + function toUtf8Bytes(str, form = UnicodeNormalizationForm.current) { + if (form != UnicodeNormalizationForm.current) { + logger$6.checkNormalize(); + str = str.normalize(form); + } + let result = []; + for (let i = 0; i < str.length; i++) { + const c = str.charCodeAt(i); + if (c < 0x80) { + result.push(c); + } + else if (c < 0x800) { + result.push((c >> 6) | 0xc0); + result.push((c & 0x3f) | 0x80); + } + else if ((c & 0xfc00) == 0xd800) { + i++; + const c2 = str.charCodeAt(i); + if (i >= str.length || (c2 & 0xfc00) !== 0xdc00) { + throw new Error("invalid utf-8 string"); + } + // Surrogate Pair + const pair = 0x10000 + ((c & 0x03ff) << 10) + (c2 & 0x03ff); + result.push((pair >> 18) | 0xf0); + result.push(((pair >> 12) & 0x3f) | 0x80); + result.push(((pair >> 6) & 0x3f) | 0x80); + result.push((pair & 0x3f) | 0x80); + } + else { + result.push((c >> 12) | 0xe0); + result.push(((c >> 6) & 0x3f) | 0x80); + result.push((c & 0x3f) | 0x80); + } + } + return arrayify(result); + } + function _toUtf8String(codePoints) { + return codePoints.map((codePoint) => { + if (codePoint <= 0xffff) { + return String.fromCharCode(codePoint); + } + codePoint -= 0x10000; + return String.fromCharCode((((codePoint >> 10) & 0x3ff) + 0xd800), ((codePoint & 0x3ff) + 0xdc00)); + }).join(""); + } + function toUtf8String(bytes, onError) { + return _toUtf8String(getUtf8CodePoints(bytes, onError)); + } + + class StringCoder extends DynamicBytesCoder { + constructor(localName) { + super("string", localName); + } + defaultValue() { + return ""; + } + encode(writer, value) { + return super.encode(writer, toUtf8Bytes(value)); + } + decode(reader) { + return toUtf8String(super.decode(reader)); + } + } + + class TupleCoder extends Coder { + constructor(coders, localName) { + let dynamic = false; + const types = []; + coders.forEach((coder) => { + if (coder.dynamic) { + dynamic = true; + } + types.push(coder.type); + }); + const type = ("tuple(" + types.join(",") + ")"); + super("tuple", type, localName, dynamic); + this.coders = coders; + } + defaultValue() { + const values = []; + this.coders.forEach((coder) => { + values.push(coder.defaultValue()); + }); + // We only output named properties for uniquely named coders + const uniqueNames = this.coders.reduce((accum, coder) => { + const name = coder.localName; + if (name) { + if (!accum[name]) { + accum[name] = 0; + } + accum[name]++; + } + return accum; + }, {}); + // Add named values + this.coders.forEach((coder, index) => { + let name = coder.localName; + if (!name || uniqueNames[name] !== 1) { + return; + } + if (name === "length") { + name = "_length"; + } + if (values[name] != null) { + return; + } + values[name] = values[index]; + }); + return Object.freeze(values); + } + encode(writer, value) { + return pack(writer, this.coders, value); + } + decode(reader) { + return reader.coerce(this.name, unpack(reader, this.coders)); + } + } + + const logger$5 = new Logger(version$7); + const paramTypeBytes = new RegExp(/^bytes([0-9]*)$/); + const paramTypeNumber = new RegExp(/^(u?int)([0-9]*)$/); + class AbiCoder { + constructor(coerceFunc) { + defineReadOnly(this, "coerceFunc", coerceFunc || null); + } + _getCoder(param) { + switch (param.baseType) { + case "address": + return new AddressCoder(param.name); + case "bool": + return new BooleanCoder(param.name); + case "string": + return new StringCoder(param.name); + case "bytes": + return new BytesCoder(param.name); + case "array": + return new ArrayCoder(this._getCoder(param.arrayChildren), param.arrayLength, param.name); + case "tuple": + return new TupleCoder((param.components || []).map((component) => { + return this._getCoder(component); + }), param.name); + case "": + return new NullCoder(param.name); + } + // u?int[0-9]* + let match = param.type.match(paramTypeNumber); + if (match) { + let size = parseInt(match[2] || "256"); + if (size === 0 || size > 256 || (size % 8) !== 0) { + logger$5.throwArgumentError("invalid " + match[1] + " bit length", "param", param); + } + return new NumberCoder(size / 8, (match[1] === "int"), param.name); + } + // bytes[0-9]+ + match = param.type.match(paramTypeBytes); + if (match) { + let size = parseInt(match[1]); + if (size === 0 || size > 32) { + logger$5.throwArgumentError("invalid bytes length", "param", param); + } + return new FixedBytesCoder(size, param.name); + } + return logger$5.throwArgumentError("invalid type", "type", param.type); + } + _getWordSize() { return 32; } + _getReader(data, allowLoose) { + return new Reader(data, this._getWordSize(), this.coerceFunc, allowLoose); + } + _getWriter() { + return new Writer(this._getWordSize()); + } + getDefaultValue(types) { + const coders = types.map((type) => this._getCoder(ParamType.from(type))); + const coder = new TupleCoder(coders, "_"); + return coder.defaultValue(); + } + encode(types, values) { + if (types.length !== values.length) { + logger$5.throwError("types/values length mismatch", Logger.errors.INVALID_ARGUMENT, { + count: { types: types.length, values: values.length }, + value: { types: types, values: values } + }); + } + const coders = types.map((type) => this._getCoder(ParamType.from(type))); + const coder = (new TupleCoder(coders, "_")); + const writer = this._getWriter(); + coder.encode(writer, values); + return writer.data; + } + decode(types, data, loose) { + const coders = types.map((type) => this._getCoder(ParamType.from(type))); + const coder = new TupleCoder(coders, "_"); + return coder.decode(this._getReader(arrayify(data), loose)); + } + } + const defaultAbiCoder = new AbiCoder(); + + function id(text) { + return keccak256(toUtf8Bytes(text)); + } + + const logger$4 = new Logger(version$7); + class LogDescription extends Description { + } + class TransactionDescription extends Description { + } + class ErrorDescription extends Description { + } + class Indexed extends Description { + static isIndexed(value) { + return !!(value && value._isIndexed); + } + } + const BuiltinErrors = { + "0x08c379a0": { signature: "Error(string)", name: "Error", inputs: ["string"], reason: true }, + "0x4e487b71": { signature: "Panic(uint256)", name: "Panic", inputs: ["uint256"] } + }; + function wrapAccessError(property, error) { + const wrap = new Error(`deferred error during ABI decoding triggered accessing ${property}`); + wrap.error = error; + return wrap; + } + /* + function checkNames(fragment: Fragment, type: "input" | "output", params: Array): void { + params.reduce((accum, param) => { + if (param.name) { + if (accum[param.name]) { + logger.throwArgumentError(`duplicate ${ type } parameter ${ JSON.stringify(param.name) } in ${ fragment.format("full") }`, "fragment", fragment); + } + accum[param.name] = true; + } + return accum; + }, <{ [ name: string ]: boolean }>{ }); + } + */ + class Interface { + constructor(fragments) { + let abi = []; + if (typeof (fragments) === "string") { + abi = JSON.parse(fragments); + } + else { + abi = fragments; + } + defineReadOnly(this, "fragments", abi.map((fragment) => { + return Fragment.from(fragment); + }).filter((fragment) => (fragment != null))); + defineReadOnly(this, "_abiCoder", getStatic(new.target, "getAbiCoder")()); + defineReadOnly(this, "functions", {}); + defineReadOnly(this, "errors", {}); + defineReadOnly(this, "events", {}); + defineReadOnly(this, "structs", {}); + // Add all fragments by their signature + this.fragments.forEach((fragment) => { + let bucket = null; + switch (fragment.type) { + case "constructor": + if (this.deploy) { + logger$4.warn("duplicate definition - constructor"); + return; + } + //checkNames(fragment, "input", fragment.inputs); + defineReadOnly(this, "deploy", fragment); + return; + case "function": + //checkNames(fragment, "input", fragment.inputs); + //checkNames(fragment, "output", (fragment).outputs); + bucket = this.functions; + break; + case "event": + //checkNames(fragment, "input", fragment.inputs); + bucket = this.events; + break; + case "error": + bucket = this.errors; + break; + default: + return; + } + let signature = fragment.format(); + if (bucket[signature]) { + logger$4.warn("duplicate definition - " + signature); + return; + } + bucket[signature] = fragment; + }); + // If we do not have a constructor add a default + if (!this.deploy) { + defineReadOnly(this, "deploy", ConstructorFragment.from({ + payable: false, + type: "constructor" + })); + } + defineReadOnly(this, "_isInterface", true); + } + format(format) { + if (!format) { + format = FormatTypes.full; + } + if (format === FormatTypes.sighash) { + logger$4.throwArgumentError("interface does not support formatting sighash", "format", format); + } + const abi = this.fragments.map((fragment) => fragment.format(format)); + // We need to re-bundle the JSON fragments a bit + if (format === FormatTypes.json) { + return JSON.stringify(abi.map((j) => JSON.parse(j))); + } + return abi; + } + // Sub-classes can override these to handle other blockchains + static getAbiCoder() { + return defaultAbiCoder; + } + static getAddress(address) { + return getAddress(address); + } + static getSighash(fragment) { + return hexDataSlice(id(fragment.format()), 0, 4); + } + static getEventTopic(eventFragment) { + return id(eventFragment.format()); + } + // Find a function definition by any means necessary (unless it is ambiguous) + getFunction(nameOrSignatureOrSighash) { + if (isHexString(nameOrSignatureOrSighash)) { + for (const name in this.functions) { + if (nameOrSignatureOrSighash === this.getSighash(name)) { + return this.functions[name]; + } + } + logger$4.throwArgumentError("no matching function", "sighash", nameOrSignatureOrSighash); + } + // It is a bare name, look up the function (will return null if ambiguous) + if (nameOrSignatureOrSighash.indexOf("(") === -1) { + const name = nameOrSignatureOrSighash.trim(); + const matching = Object.keys(this.functions).filter((f) => (f.split("(" /* fix:) */)[0] === name)); + if (matching.length === 0) { + logger$4.throwArgumentError("no matching function", "name", name); + } + else if (matching.length > 1) { + logger$4.throwArgumentError("multiple matching functions", "name", name); + } + return this.functions[matching[0]]; + } + // Normalize the signature and lookup the function + const result = this.functions[FunctionFragment.fromString(nameOrSignatureOrSighash).format()]; + if (!result) { + logger$4.throwArgumentError("no matching function", "signature", nameOrSignatureOrSighash); + } + return result; + } + // Find an event definition by any means necessary (unless it is ambiguous) + getEvent(nameOrSignatureOrTopic) { + if (isHexString(nameOrSignatureOrTopic)) { + const topichash = nameOrSignatureOrTopic.toLowerCase(); + for (const name in this.events) { + if (topichash === this.getEventTopic(name)) { + return this.events[name]; + } + } + logger$4.throwArgumentError("no matching event", "topichash", topichash); + } + // It is a bare name, look up the function (will return null if ambiguous) + if (nameOrSignatureOrTopic.indexOf("(") === -1) { + const name = nameOrSignatureOrTopic.trim(); + const matching = Object.keys(this.events).filter((f) => (f.split("(" /* fix:) */)[0] === name)); + if (matching.length === 0) { + logger$4.throwArgumentError("no matching event", "name", name); + } + else if (matching.length > 1) { + logger$4.throwArgumentError("multiple matching events", "name", name); + } + return this.events[matching[0]]; + } + // Normalize the signature and lookup the function + const result = this.events[EventFragment.fromString(nameOrSignatureOrTopic).format()]; + if (!result) { + logger$4.throwArgumentError("no matching event", "signature", nameOrSignatureOrTopic); + } + return result; + } + // Find a function definition by any means necessary (unless it is ambiguous) + getError(nameOrSignatureOrSighash) { + if (isHexString(nameOrSignatureOrSighash)) { + const getSighash = getStatic(this.constructor, "getSighash"); + for (const name in this.errors) { + const error = this.errors[name]; + if (nameOrSignatureOrSighash === getSighash(error)) { + return this.errors[name]; + } + } + logger$4.throwArgumentError("no matching error", "sighash", nameOrSignatureOrSighash); + } + // It is a bare name, look up the function (will return null if ambiguous) + if (nameOrSignatureOrSighash.indexOf("(") === -1) { + const name = nameOrSignatureOrSighash.trim(); + const matching = Object.keys(this.errors).filter((f) => (f.split("(" /* fix:) */)[0] === name)); + if (matching.length === 0) { + logger$4.throwArgumentError("no matching error", "name", name); + } + else if (matching.length > 1) { + logger$4.throwArgumentError("multiple matching errors", "name", name); + } + return this.errors[matching[0]]; + } + // Normalize the signature and lookup the function + const result = this.errors[FunctionFragment.fromString(nameOrSignatureOrSighash).format()]; + if (!result) { + logger$4.throwArgumentError("no matching error", "signature", nameOrSignatureOrSighash); + } + return result; + } + // Get the sighash (the bytes4 selector) used by Solidity to identify a function + getSighash(fragment) { + if (typeof (fragment) === "string") { + try { + fragment = this.getFunction(fragment); + } + catch (error) { + try { + fragment = this.getError(fragment); + } + catch (_) { + throw error; + } + } + } + return getStatic(this.constructor, "getSighash")(fragment); + } + // Get the topic (the bytes32 hash) used by Solidity to identify an event + getEventTopic(eventFragment) { + if (typeof (eventFragment) === "string") { + eventFragment = this.getEvent(eventFragment); + } + return getStatic(this.constructor, "getEventTopic")(eventFragment); + } + _decodeParams(params, data) { + return this._abiCoder.decode(params, data); + } + _encodeParams(params, values) { + return this._abiCoder.encode(params, values); + } + encodeDeploy(values) { + return this._encodeParams(this.deploy.inputs, values || []); + } + decodeErrorResult(fragment, data) { + if (typeof (fragment) === "string") { + fragment = this.getError(fragment); + } + const bytes = arrayify(data); + if (hexlify(bytes.slice(0, 4)) !== this.getSighash(fragment)) { + logger$4.throwArgumentError(`data signature does not match error ${fragment.name}.`, "data", hexlify(bytes)); + } + return this._decodeParams(fragment.inputs, bytes.slice(4)); + } + encodeErrorResult(fragment, values) { + if (typeof (fragment) === "string") { + fragment = this.getError(fragment); + } + return hexlify(concat([ + this.getSighash(fragment), + this._encodeParams(fragment.inputs, values || []) + ])); + } + // Decode the data for a function call (e.g. tx.data) + decodeFunctionData(functionFragment, data) { + if (typeof (functionFragment) === "string") { + functionFragment = this.getFunction(functionFragment); + } + const bytes = arrayify(data); + if (hexlify(bytes.slice(0, 4)) !== this.getSighash(functionFragment)) { + logger$4.throwArgumentError(`data signature does not match function ${functionFragment.name}.`, "data", hexlify(bytes)); + } + return this._decodeParams(functionFragment.inputs, bytes.slice(4)); + } + // Encode the data for a function call (e.g. tx.data) + encodeFunctionData(functionFragment, values) { + if (typeof (functionFragment) === "string") { + functionFragment = this.getFunction(functionFragment); + } + return hexlify(concat([ + this.getSighash(functionFragment), + this._encodeParams(functionFragment.inputs, values || []) + ])); + } + // Decode the result from a function call (e.g. from eth_call) + decodeFunctionResult(functionFragment, data) { + if (typeof (functionFragment) === "string") { + functionFragment = this.getFunction(functionFragment); + } + let bytes = arrayify(data); + let reason = null; + let message = ""; + let errorArgs = null; + let errorName = null; + let errorSignature = null; + switch (bytes.length % this._abiCoder._getWordSize()) { + case 0: + try { + return this._abiCoder.decode(functionFragment.outputs, bytes); + } + catch (error) { } + break; + case 4: { + const selector = hexlify(bytes.slice(0, 4)); + const builtin = BuiltinErrors[selector]; + if (builtin) { + errorArgs = this._abiCoder.decode(builtin.inputs, bytes.slice(4)); + errorName = builtin.name; + errorSignature = builtin.signature; + if (builtin.reason) { + reason = errorArgs[0]; + } + if (errorName === "Error") { + message = `; VM Exception while processing transaction: reverted with reason string ${JSON.stringify(errorArgs[0])}`; + } + else if (errorName === "Panic") { + message = `; VM Exception while processing transaction: reverted with panic code ${errorArgs[0]}`; + } + } + else { + try { + const error = this.getError(selector); + errorArgs = this._abiCoder.decode(error.inputs, bytes.slice(4)); + errorName = error.name; + errorSignature = error.format(); + } + catch (error) { } + } + break; + } + } + return logger$4.throwError("call revert exception" + message, Logger.errors.CALL_EXCEPTION, { + method: functionFragment.format(), + data: hexlify(data), errorArgs, errorName, errorSignature, reason + }); + } + // Encode the result for a function call (e.g. for eth_call) + encodeFunctionResult(functionFragment, values) { + if (typeof (functionFragment) === "string") { + functionFragment = this.getFunction(functionFragment); + } + return hexlify(this._abiCoder.encode(functionFragment.outputs, values || [])); + } + // Create the filter for the event with search criteria (e.g. for eth_filterLog) + encodeFilterTopics(eventFragment, values) { + if (typeof (eventFragment) === "string") { + eventFragment = this.getEvent(eventFragment); + } + if (values.length > eventFragment.inputs.length) { + logger$4.throwError("too many arguments for " + eventFragment.format(), Logger.errors.UNEXPECTED_ARGUMENT, { + argument: "values", + value: values + }); + } + let topics = []; + if (!eventFragment.anonymous) { + topics.push(this.getEventTopic(eventFragment)); + } + const encodeTopic = (param, value) => { + if (param.type === "string") { + return id(value); + } + else if (param.type === "bytes") { + return keccak256(hexlify(value)); + } + if (param.type === "bool" && typeof (value) === "boolean") { + value = (value ? "0x01" : "0x00"); + } + if (param.type.match(/^u?int/)) { + value = BigNumber.from(value).toHexString(); + } + // Check addresses are valid + if (param.type === "address") { + this._abiCoder.encode(["address"], [value]); + } + return hexZeroPad(hexlify(value), 32); + }; + values.forEach((value, index) => { + let param = eventFragment.inputs[index]; + if (!param.indexed) { + if (value != null) { + logger$4.throwArgumentError("cannot filter non-indexed parameters; must be null", ("contract." + param.name), value); + } + return; + } + if (value == null) { + topics.push(null); + } + else if (param.baseType === "array" || param.baseType === "tuple") { + logger$4.throwArgumentError("filtering with tuples or arrays not supported", ("contract." + param.name), value); + } + else if (Array.isArray(value)) { + topics.push(value.map((value) => encodeTopic(param, value))); + } + else { + topics.push(encodeTopic(param, value)); + } + }); + // Trim off trailing nulls + while (topics.length && topics[topics.length - 1] === null) { + topics.pop(); + } + return topics; + } + encodeEventLog(eventFragment, values) { + if (typeof (eventFragment) === "string") { + eventFragment = this.getEvent(eventFragment); + } + const topics = []; + const dataTypes = []; + const dataValues = []; + if (!eventFragment.anonymous) { + topics.push(this.getEventTopic(eventFragment)); + } + if (values.length !== eventFragment.inputs.length) { + logger$4.throwArgumentError("event arguments/values mismatch", "values", values); + } + eventFragment.inputs.forEach((param, index) => { + const value = values[index]; + if (param.indexed) { + if (param.type === "string") { + topics.push(id(value)); + } + else if (param.type === "bytes") { + topics.push(keccak256(value)); + } + else if (param.baseType === "tuple" || param.baseType === "array") { + // @TODO + throw new Error("not implemented"); + } + else { + topics.push(this._abiCoder.encode([param.type], [value])); + } + } + else { + dataTypes.push(param); + dataValues.push(value); + } + }); + return { + data: this._abiCoder.encode(dataTypes, dataValues), + topics: topics + }; + } + // Decode a filter for the event and the search criteria + decodeEventLog(eventFragment, data, topics) { + if (typeof (eventFragment) === "string") { + eventFragment = this.getEvent(eventFragment); + } + if (topics != null && !eventFragment.anonymous) { + let topicHash = this.getEventTopic(eventFragment); + if (!isHexString(topics[0], 32) || topics[0].toLowerCase() !== topicHash) { + logger$4.throwError("fragment/topic mismatch", Logger.errors.INVALID_ARGUMENT, { argument: "topics[0]", expected: topicHash, value: topics[0] }); + } + topics = topics.slice(1); + } + let indexed = []; + let nonIndexed = []; + let dynamic = []; + eventFragment.inputs.forEach((param, index) => { + if (param.indexed) { + if (param.type === "string" || param.type === "bytes" || param.baseType === "tuple" || param.baseType === "array") { + indexed.push(ParamType.fromObject({ type: "bytes32", name: param.name })); + dynamic.push(true); + } + else { + indexed.push(param); + dynamic.push(false); + } + } + else { + nonIndexed.push(param); + dynamic.push(false); + } + }); + let resultIndexed = (topics != null) ? this._abiCoder.decode(indexed, concat(topics)) : null; + let resultNonIndexed = this._abiCoder.decode(nonIndexed, data, true); + let result = []; + let nonIndexedIndex = 0, indexedIndex = 0; + eventFragment.inputs.forEach((param, index) => { + if (param.indexed) { + if (resultIndexed == null) { + result[index] = new Indexed({ _isIndexed: true, hash: null }); + } + else if (dynamic[index]) { + result[index] = new Indexed({ _isIndexed: true, hash: resultIndexed[indexedIndex++] }); + } + else { + try { + result[index] = resultIndexed[indexedIndex++]; + } + catch (error) { + result[index] = error; + } + } + } + else { + try { + result[index] = resultNonIndexed[nonIndexedIndex++]; + } + catch (error) { + result[index] = error; + } + } + // Add the keyword argument if named and safe + if (param.name && result[param.name] == null) { + const value = result[index]; + // Make error named values throw on access + if (value instanceof Error) { + Object.defineProperty(result, param.name, { + enumerable: true, + get: () => { throw wrapAccessError(`property ${JSON.stringify(param.name)}`, value); } + }); + } + else { + result[param.name] = value; + } + } + }); + // Make all error indexed values throw on access + for (let i = 0; i < result.length; i++) { + const value = result[i]; + if (value instanceof Error) { + Object.defineProperty(result, i, { + enumerable: true, + get: () => { throw wrapAccessError(`index ${i}`, value); } + }); + } + } + return Object.freeze(result); + } + // Given a transaction, find the matching function fragment (if any) and + // determine all its properties and call parameters + parseTransaction(tx) { + let fragment = this.getFunction(tx.data.substring(0, 10).toLowerCase()); + if (!fragment) { + return null; + } + return new TransactionDescription({ + args: this._abiCoder.decode(fragment.inputs, "0x" + tx.data.substring(10)), + functionFragment: fragment, + name: fragment.name, + signature: fragment.format(), + sighash: this.getSighash(fragment), + value: BigNumber.from(tx.value || "0"), + }); + } + // @TODO + //parseCallResult(data: BytesLike): ?? + // Given an event log, find the matching event fragment (if any) and + // determine all its properties and values + parseLog(log) { + let fragment = this.getEvent(log.topics[0]); + if (!fragment || fragment.anonymous) { + return null; + } + // @TODO: If anonymous, and the only method, and the input count matches, should we parse? + // Probably not, because just because it is the only event in the ABI does + // not mean we have the full ABI; maybe just a fragment? + return new LogDescription({ + eventFragment: fragment, + name: fragment.name, + signature: fragment.format(), + topic: this.getEventTopic(fragment), + args: this.decodeEventLog(fragment, log.data, log.topics) + }); + } + parseError(data) { + const hexData = hexlify(data); + let fragment = this.getError(hexData.substring(0, 10).toLowerCase()); + if (!fragment) { + return null; + } + return new ErrorDescription({ + args: this._abiCoder.decode(fragment.inputs, "0x" + hexData.substring(10)), + errorFragment: fragment, + name: fragment.name, + signature: fragment.format(), + sighash: this.getSighash(fragment), + }); + } + /* + static from(value: Array | string | Interface) { + if (Interface.isInterface(value)) { + return value; + } + if (typeof(value) === "string") { + return new Interface(JSON.parse(value)); + } + return new Interface(value); + } + */ + static isInterface(value) { + return !!(value && value._isInterface); + } + } + + const version$3 = "abstract-provider/5.7.0"; + + var __awaiter$2 = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) { + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } + return new (P || (P = Promise))(function (resolve, reject) { + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } + step((generator = generator.apply(thisArg, _arguments || [])).next()); + }); + }; + const logger$3 = new Logger(version$3); + /////////////////////////////// + // Exported Abstracts + class Provider { + constructor() { + logger$3.checkAbstract(new.target, Provider); + defineReadOnly(this, "_isProvider", true); + } + getFeeData() { + return __awaiter$2(this, void 0, void 0, function* () { + const { block, gasPrice } = yield resolveProperties({ + block: this.getBlock("latest"), + gasPrice: this.getGasPrice().catch((error) => { + // @TODO: Why is this now failing on Calaveras? + //console.log(error); + return null; + }) + }); + let lastBaseFeePerGas = null, maxFeePerGas = null, maxPriorityFeePerGas = null; + if (block && block.baseFeePerGas) { + // We may want to compute this more accurately in the future, + // using the formula "check if the base fee is correct". + // See: https://eips.ethereum.org/EIPS/eip-1559 + lastBaseFeePerGas = block.baseFeePerGas; + maxPriorityFeePerGas = BigNumber.from("1500000000"); + maxFeePerGas = block.baseFeePerGas.mul(2).add(maxPriorityFeePerGas); + } + return { lastBaseFeePerGas, maxFeePerGas, maxPriorityFeePerGas, gasPrice }; + }); + } + // Alias for "on" + addListener(eventName, listener) { + return this.on(eventName, listener); + } + // Alias for "off" + removeListener(eventName, listener) { + return this.off(eventName, listener); + } + static isProvider(value) { + return !!(value && value._isProvider); + } + } + + const version$2 = "abstract-signer/5.7.0"; + + var __awaiter$1 = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) { + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } + return new (P || (P = Promise))(function (resolve, reject) { + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } + step((generator = generator.apply(thisArg, _arguments || [])).next()); + }); + }; + const logger$2 = new Logger(version$2); + const allowedTransactionKeys = [ + "accessList", "ccipReadEnabled", "chainId", "customData", "data", "from", "gasLimit", "gasPrice", "maxFeePerGas", "maxPriorityFeePerGas", "nonce", "to", "type", "value" + ]; + const forwardErrors = [ + Logger.errors.INSUFFICIENT_FUNDS, + Logger.errors.NONCE_EXPIRED, + Logger.errors.REPLACEMENT_UNDERPRICED, + ]; + class Signer { + /////////////////// + // Sub-classes MUST call super + constructor() { + logger$2.checkAbstract(new.target, Signer); + defineReadOnly(this, "_isSigner", true); + } + /////////////////// + // Sub-classes MAY override these + getBalance(blockTag) { + return __awaiter$1(this, void 0, void 0, function* () { + this._checkProvider("getBalance"); + return yield this.provider.getBalance(this.getAddress(), blockTag); + }); + } + getTransactionCount(blockTag) { + return __awaiter$1(this, void 0, void 0, function* () { + this._checkProvider("getTransactionCount"); + return yield this.provider.getTransactionCount(this.getAddress(), blockTag); + }); + } + // Populates "from" if unspecified, and estimates the gas for the transaction + estimateGas(transaction) { + return __awaiter$1(this, void 0, void 0, function* () { + this._checkProvider("estimateGas"); + const tx = yield resolveProperties(this.checkTransaction(transaction)); + return yield this.provider.estimateGas(tx); + }); + } + // Populates "from" if unspecified, and calls with the transaction + call(transaction, blockTag) { + return __awaiter$1(this, void 0, void 0, function* () { + this._checkProvider("call"); + const tx = yield resolveProperties(this.checkTransaction(transaction)); + return yield this.provider.call(tx, blockTag); + }); + } + // Populates all fields in a transaction, signs it and sends it to the network + sendTransaction(transaction) { + return __awaiter$1(this, void 0, void 0, function* () { + this._checkProvider("sendTransaction"); + const tx = yield this.populateTransaction(transaction); + const signedTx = yield this.signTransaction(tx); + return yield this.provider.sendTransaction(signedTx); + }); + } + getChainId() { + return __awaiter$1(this, void 0, void 0, function* () { + this._checkProvider("getChainId"); + const network = yield this.provider.getNetwork(); + return network.chainId; + }); + } + getGasPrice() { + return __awaiter$1(this, void 0, void 0, function* () { + this._checkProvider("getGasPrice"); + return yield this.provider.getGasPrice(); + }); + } + getFeeData() { + return __awaiter$1(this, void 0, void 0, function* () { + this._checkProvider("getFeeData"); + return yield this.provider.getFeeData(); + }); + } + resolveName(name) { + return __awaiter$1(this, void 0, void 0, function* () { + this._checkProvider("resolveName"); + return yield this.provider.resolveName(name); + }); + } + // Checks a transaction does not contain invalid keys and if + // no "from" is provided, populates it. + // - does NOT require a provider + // - adds "from" is not present + // - returns a COPY (safe to mutate the result) + // By default called from: (overriding these prevents it) + // - call + // - estimateGas + // - populateTransaction (and therefor sendTransaction) + checkTransaction(transaction) { + for (const key in transaction) { + if (allowedTransactionKeys.indexOf(key) === -1) { + logger$2.throwArgumentError("invalid transaction key: " + key, "transaction", transaction); + } + } + const tx = shallowCopy(transaction); + if (tx.from == null) { + tx.from = this.getAddress(); + } + else { + // Make sure any provided address matches this signer + tx.from = Promise.all([ + Promise.resolve(tx.from), + this.getAddress() + ]).then((result) => { + if (result[0].toLowerCase() !== result[1].toLowerCase()) { + logger$2.throwArgumentError("from address mismatch", "transaction", transaction); + } + return result[0]; + }); + } + return tx; + } + // Populates ALL keys for a transaction and checks that "from" matches + // this Signer. Should be used by sendTransaction but NOT by signTransaction. + // By default called from: (overriding these prevents it) + // - sendTransaction + // + // Notes: + // - We allow gasPrice for EIP-1559 as long as it matches maxFeePerGas + populateTransaction(transaction) { + return __awaiter$1(this, void 0, void 0, function* () { + const tx = yield resolveProperties(this.checkTransaction(transaction)); + if (tx.to != null) { + tx.to = Promise.resolve(tx.to).then((to) => __awaiter$1(this, void 0, void 0, function* () { + if (to == null) { + return null; + } + const address = yield this.resolveName(to); + if (address == null) { + logger$2.throwArgumentError("provided ENS name resolves to null", "tx.to", to); + } + return address; + })); + // Prevent this error from causing an UnhandledPromiseException + tx.to.catch((error) => { }); + } + // Do not allow mixing pre-eip-1559 and eip-1559 properties + const hasEip1559 = (tx.maxFeePerGas != null || tx.maxPriorityFeePerGas != null); + if (tx.gasPrice != null && (tx.type === 2 || hasEip1559)) { + logger$2.throwArgumentError("eip-1559 transaction do not support gasPrice", "transaction", transaction); + } + else if ((tx.type === 0 || tx.type === 1) && hasEip1559) { + logger$2.throwArgumentError("pre-eip-1559 transaction do not support maxFeePerGas/maxPriorityFeePerGas", "transaction", transaction); + } + if ((tx.type === 2 || tx.type == null) && (tx.maxFeePerGas != null && tx.maxPriorityFeePerGas != null)) { + // Fully-formed EIP-1559 transaction (skip getFeeData) + tx.type = 2; + } + else if (tx.type === 0 || tx.type === 1) { + // Explicit Legacy or EIP-2930 transaction + // Populate missing gasPrice + if (tx.gasPrice == null) { + tx.gasPrice = this.getGasPrice(); + } + } + else { + // We need to get fee data to determine things + const feeData = yield this.getFeeData(); + if (tx.type == null) { + // We need to auto-detect the intended type of this transaction... + if (feeData.maxFeePerGas != null && feeData.maxPriorityFeePerGas != null) { + // The network supports EIP-1559! + // Upgrade transaction from null to eip-1559 + tx.type = 2; + if (tx.gasPrice != null) { + // Using legacy gasPrice property on an eip-1559 network, + // so use gasPrice as both fee properties + const gasPrice = tx.gasPrice; + delete tx.gasPrice; + tx.maxFeePerGas = gasPrice; + tx.maxPriorityFeePerGas = gasPrice; + } + else { + // Populate missing fee data + if (tx.maxFeePerGas == null) { + tx.maxFeePerGas = feeData.maxFeePerGas; + } + if (tx.maxPriorityFeePerGas == null) { + tx.maxPriorityFeePerGas = feeData.maxPriorityFeePerGas; + } + } + } + else if (feeData.gasPrice != null) { + // Network doesn't support EIP-1559... + // ...but they are trying to use EIP-1559 properties + if (hasEip1559) { + logger$2.throwError("network does not support EIP-1559", Logger.errors.UNSUPPORTED_OPERATION, { + operation: "populateTransaction" + }); + } + // Populate missing fee data + if (tx.gasPrice == null) { + tx.gasPrice = feeData.gasPrice; + } + // Explicitly set untyped transaction to legacy + tx.type = 0; + } + else { + // getFeeData has failed us. + logger$2.throwError("failed to get consistent fee data", Logger.errors.UNSUPPORTED_OPERATION, { + operation: "signer.getFeeData" + }); + } + } + else if (tx.type === 2) { + // Explicitly using EIP-1559 + // Populate missing fee data + if (tx.maxFeePerGas == null) { + tx.maxFeePerGas = feeData.maxFeePerGas; + } + if (tx.maxPriorityFeePerGas == null) { + tx.maxPriorityFeePerGas = feeData.maxPriorityFeePerGas; + } + } + } + if (tx.nonce == null) { + tx.nonce = this.getTransactionCount("pending"); + } + if (tx.gasLimit == null) { + tx.gasLimit = this.estimateGas(tx).catch((error) => { + if (forwardErrors.indexOf(error.code) >= 0) { + throw error; + } + return logger$2.throwError("cannot estimate gas; transaction may fail or may require manual gas limit", Logger.errors.UNPREDICTABLE_GAS_LIMIT, { + error: error, + tx: tx + }); + }); + } + if (tx.chainId == null) { + tx.chainId = this.getChainId(); + } + else { + tx.chainId = Promise.all([ + Promise.resolve(tx.chainId), + this.getChainId() + ]).then((results) => { + if (results[1] !== 0 && results[0] !== results[1]) { + logger$2.throwArgumentError("chainId address mismatch", "transaction", transaction); + } + return results[0]; + }); + } + return yield resolveProperties(tx); + }); + } + /////////////////// + // Sub-classes SHOULD leave these alone + _checkProvider(operation) { + if (!this.provider) { + logger$2.throwError("missing provider", Logger.errors.UNSUPPORTED_OPERATION, { + operation: (operation || "_checkProvider") + }); + } + } + static isSigner(value) { + return !!(value && value._isSigner); + } + } + class VoidSigner extends Signer { + constructor(address, provider) { + super(); + defineReadOnly(this, "address", address); + defineReadOnly(this, "provider", provider || null); + } + getAddress() { + return Promise.resolve(this.address); + } + _fail(message, operation) { + return Promise.resolve().then(() => { + logger$2.throwError(message, Logger.errors.UNSUPPORTED_OPERATION, { operation: operation }); + }); + } + signMessage(message) { + return this._fail("VoidSigner cannot sign messages", "signMessage"); + } + signTransaction(transaction) { + return this._fail("VoidSigner cannot sign transactions", "signTransaction"); + } + _signTypedData(domain, types, value) { + return this._fail("VoidSigner cannot sign typed data", "signTypedData"); + } + connect(provider) { + return new VoidSigner(this.address, provider); + } + } + + const version$1 = "transactions/5.7.0"; + + const logger$1 = new Logger(version$1); + var TransactionTypes; + (function (TransactionTypes) { + TransactionTypes[TransactionTypes["legacy"] = 0] = "legacy"; + TransactionTypes[TransactionTypes["eip2930"] = 1] = "eip2930"; + TransactionTypes[TransactionTypes["eip1559"] = 2] = "eip1559"; + })(TransactionTypes || (TransactionTypes = {})); + function accessSetify(addr, storageKeys) { + return { + address: getAddress(addr), + storageKeys: (storageKeys || []).map((storageKey, index) => { + if (hexDataLength(storageKey) !== 32) { + logger$1.throwArgumentError("invalid access list storageKey", `accessList[${addr}:${index}]`, storageKey); + } + return storageKey.toLowerCase(); + }) + }; + } + function accessListify(value) { + if (Array.isArray(value)) { + return value.map((set, index) => { + if (Array.isArray(set)) { + if (set.length > 2) { + logger$1.throwArgumentError("access list expected to be [ address, storageKeys[] ]", `value[${index}]`, set); + } + return accessSetify(set[0], set[1]); + } + return accessSetify(set.address, set.storageKeys); + }); + } + const result = Object.keys(value).map((addr) => { + const storageKeys = value[addr].reduce((accum, storageKey) => { + accum[storageKey] = true; + return accum; + }, {}); + return accessSetify(addr, Object.keys(storageKeys).sort()); + }); + result.sort((a, b) => (a.address.localeCompare(b.address))); + return result; + } + + const version = "contracts/5.7.0"; + + var __awaiter = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) { + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } + return new (P || (P = Promise))(function (resolve, reject) { + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } + step((generator = generator.apply(thisArg, _arguments || [])).next()); + }); + }; + const logger = new Logger(version); + function resolveName(resolver, nameOrPromise) { + return __awaiter(this, void 0, void 0, function* () { + const name = yield nameOrPromise; + if (typeof (name) !== "string") { + logger.throwArgumentError("invalid address or ENS name", "name", name); + } + // If it is already an address, just use it (after adding checksum) + try { + return getAddress(name); + } + catch (error) { } + if (!resolver) { + logger.throwError("a provider or signer is needed to resolve ENS names", Logger.errors.UNSUPPORTED_OPERATION, { + operation: "resolveName" + }); + } + const address = yield resolver.resolveName(name); + if (address == null) { + logger.throwArgumentError("resolver or addr is not configured for ENS name", "name", name); + } + return address; + }); + } + // Recursively replaces ENS names with promises to resolve the name and resolves all properties + function resolveAddresses(resolver, value, paramType) { + return __awaiter(this, void 0, void 0, function* () { + if (Array.isArray(paramType)) { + return yield Promise.all(paramType.map((paramType, index) => { + return resolveAddresses(resolver, ((Array.isArray(value)) ? value[index] : value[paramType.name]), paramType); + })); + } + if (paramType.type === "address") { + return yield resolveName(resolver, value); + } + if (paramType.type === "tuple") { + return yield resolveAddresses(resolver, value, paramType.components); + } + if (paramType.baseType === "array") { + if (!Array.isArray(value)) { + return Promise.reject(logger.makeError("invalid value for array", Logger.errors.INVALID_ARGUMENT, { + argument: "value", + value + })); + } + return yield Promise.all(value.map((v) => resolveAddresses(resolver, v, paramType.arrayChildren))); + } + return value; + }); + } + function populateTransaction(contract, fragment, args) { + return __awaiter(this, void 0, void 0, function* () { + // If an extra argument is given, it is overrides + let overrides = {}; + if (args.length === fragment.inputs.length + 1 && typeof (args[args.length - 1]) === "object") { + overrides = shallowCopy(args.pop()); + } + // Make sure the parameter count matches + logger.checkArgumentCount(args.length, fragment.inputs.length, "passed to contract"); + // Populate "from" override (allow promises) + if (contract.signer) { + if (overrides.from) { + // Contracts with a Signer are from the Signer's frame-of-reference; + // but we allow overriding "from" if it matches the signer + overrides.from = resolveProperties({ + override: resolveName(contract.signer, overrides.from), + signer: contract.signer.getAddress() + }).then((check) => __awaiter(this, void 0, void 0, function* () { + if (getAddress(check.signer) !== check.override) { + logger.throwError("Contract with a Signer cannot override from", Logger.errors.UNSUPPORTED_OPERATION, { + operation: "overrides.from" + }); + } + return check.override; + })); + } + else { + overrides.from = contract.signer.getAddress(); + } + } + else if (overrides.from) { + overrides.from = resolveName(contract.provider, overrides.from); + //} else { + // Contracts without a signer can override "from", and if + // unspecified the zero address is used + //overrides.from = AddressZero; + } + // Wait for all dependencies to be resolved (prefer the signer over the provider) + const resolved = yield resolveProperties({ + args: resolveAddresses(contract.signer || contract.provider, args, fragment.inputs), + address: contract.resolvedAddress, + overrides: (resolveProperties(overrides) || {}) + }); + // The ABI coded transaction + const data = contract.interface.encodeFunctionData(fragment, resolved.args); + const tx = { + data: data, + to: resolved.address + }; + // Resolved Overrides + const ro = resolved.overrides; + // Populate simple overrides + if (ro.nonce != null) { + tx.nonce = BigNumber.from(ro.nonce).toNumber(); + } + if (ro.gasLimit != null) { + tx.gasLimit = BigNumber.from(ro.gasLimit); + } + if (ro.gasPrice != null) { + tx.gasPrice = BigNumber.from(ro.gasPrice); + } + if (ro.maxFeePerGas != null) { + tx.maxFeePerGas = BigNumber.from(ro.maxFeePerGas); + } + if (ro.maxPriorityFeePerGas != null) { + tx.maxPriorityFeePerGas = BigNumber.from(ro.maxPriorityFeePerGas); + } + if (ro.from != null) { + tx.from = ro.from; + } + if (ro.type != null) { + tx.type = ro.type; + } + if (ro.accessList != null) { + tx.accessList = accessListify(ro.accessList); + } + // If there was no "gasLimit" override, but the ABI specifies a default, use it + if (tx.gasLimit == null && fragment.gas != null) { + // Compute the intrinsic gas cost for this transaction + // @TODO: This is based on the yellow paper as of Petersburg; this is something + // we may wish to parameterize in v6 as part of the Network object. Since this + // is always a non-nil to address, we can ignore G_create, but may wish to add + // similar logic to the ContractFactory. + let intrinsic = 21000; + const bytes = arrayify(data); + for (let i = 0; i < bytes.length; i++) { + intrinsic += 4; + if (bytes[i]) { + intrinsic += 64; + } + } + tx.gasLimit = BigNumber.from(fragment.gas).add(intrinsic); + } + // Populate "value" override + if (ro.value) { + const roValue = BigNumber.from(ro.value); + if (!roValue.isZero() && !fragment.payable) { + logger.throwError("non-payable method cannot override value", Logger.errors.UNSUPPORTED_OPERATION, { + operation: "overrides.value", + value: overrides.value + }); + } + tx.value = roValue; + } + if (ro.customData) { + tx.customData = shallowCopy(ro.customData); + } + if (ro.ccipReadEnabled) { + tx.ccipReadEnabled = !!ro.ccipReadEnabled; + } + // Remove the overrides + delete overrides.nonce; + delete overrides.gasLimit; + delete overrides.gasPrice; + delete overrides.from; + delete overrides.value; + delete overrides.type; + delete overrides.accessList; + delete overrides.maxFeePerGas; + delete overrides.maxPriorityFeePerGas; + delete overrides.customData; + delete overrides.ccipReadEnabled; + // Make sure there are no stray overrides, which may indicate a + // typo or using an unsupported key. + const leftovers = Object.keys(overrides).filter((key) => (overrides[key] != null)); + if (leftovers.length) { + logger.throwError(`cannot override ${leftovers.map((l) => JSON.stringify(l)).join(",")}`, Logger.errors.UNSUPPORTED_OPERATION, { + operation: "overrides", + overrides: leftovers + }); + } + return tx; + }); + } + function buildPopulate(contract, fragment) { + return function (...args) { + return populateTransaction(contract, fragment, args); + }; + } + function buildEstimate(contract, fragment) { + const signerOrProvider = (contract.signer || contract.provider); + return function (...args) { + return __awaiter(this, void 0, void 0, function* () { + if (!signerOrProvider) { + logger.throwError("estimate require a provider or signer", Logger.errors.UNSUPPORTED_OPERATION, { + operation: "estimateGas" + }); + } + const tx = yield populateTransaction(contract, fragment, args); + return yield signerOrProvider.estimateGas(tx); + }); + }; + } + function addContractWait(contract, tx) { + const wait = tx.wait.bind(tx); + tx.wait = (confirmations) => { + return wait(confirmations).then((receipt) => { + receipt.events = receipt.logs.map((log) => { + let event = deepCopy(log); + let parsed = null; + try { + parsed = contract.interface.parseLog(log); + } + catch (e) { } + // Successfully parsed the event log; include it + if (parsed) { + event.args = parsed.args; + event.decode = (data, topics) => { + return contract.interface.decodeEventLog(parsed.eventFragment, data, topics); + }; + event.event = parsed.name; + event.eventSignature = parsed.signature; + } + // Useful operations + event.removeListener = () => { return contract.provider; }; + event.getBlock = () => { + return contract.provider.getBlock(receipt.blockHash); + }; + event.getTransaction = () => { + return contract.provider.getTransaction(receipt.transactionHash); + }; + event.getTransactionReceipt = () => { + return Promise.resolve(receipt); + }; + return event; + }); + return receipt; + }); + }; + } + function buildCall(contract, fragment, collapseSimple) { + const signerOrProvider = (contract.signer || contract.provider); + return function (...args) { + return __awaiter(this, void 0, void 0, function* () { + // Extract the "blockTag" override if present + let blockTag = undefined; + if (args.length === fragment.inputs.length + 1 && typeof (args[args.length - 1]) === "object") { + const overrides = shallowCopy(args.pop()); + if (overrides.blockTag != null) { + blockTag = yield overrides.blockTag; + } + delete overrides.blockTag; + args.push(overrides); + } + // If the contract was just deployed, wait until it is mined + if (contract.deployTransaction != null) { + yield contract._deployed(blockTag); + } + // Call a node and get the result + const tx = yield populateTransaction(contract, fragment, args); + const result = yield signerOrProvider.call(tx, blockTag); + try { + let value = contract.interface.decodeFunctionResult(fragment, result); + if (collapseSimple && fragment.outputs.length === 1) { + value = value[0]; + } + return value; + } + catch (error) { + if (error.code === Logger.errors.CALL_EXCEPTION) { + error.address = contract.address; + error.args = args; + error.transaction = tx; + } + throw error; + } + }); + }; + } + function buildSend(contract, fragment) { + return function (...args) { + return __awaiter(this, void 0, void 0, function* () { + if (!contract.signer) { + logger.throwError("sending a transaction requires a signer", Logger.errors.UNSUPPORTED_OPERATION, { + operation: "sendTransaction" + }); + } + // If the contract was just deployed, wait until it is mined + if (contract.deployTransaction != null) { + yield contract._deployed(); + } + const txRequest = yield populateTransaction(contract, fragment, args); + const tx = yield contract.signer.sendTransaction(txRequest); + // Tweak the tx.wait so the receipt has extra properties + addContractWait(contract, tx); + return tx; + }); + }; + } + function buildDefault(contract, fragment, collapseSimple) { + if (fragment.constant) { + return buildCall(contract, fragment, collapseSimple); + } + return buildSend(contract, fragment); + } + function getEventTag(filter) { + if (filter.address && (filter.topics == null || filter.topics.length === 0)) { + return "*"; + } + return (filter.address || "*") + "@" + (filter.topics ? filter.topics.map((topic) => { + if (Array.isArray(topic)) { + return topic.join("|"); + } + return topic; + }).join(":") : ""); + } + class RunningEvent { + constructor(tag, filter) { + defineReadOnly(this, "tag", tag); + defineReadOnly(this, "filter", filter); + this._listeners = []; + } + addListener(listener, once) { + this._listeners.push({ listener: listener, once: once }); + } + removeListener(listener) { + let done = false; + this._listeners = this._listeners.filter((item) => { + if (done || item.listener !== listener) { + return true; + } + done = true; + return false; + }); + } + removeAllListeners() { + this._listeners = []; + } + listeners() { + return this._listeners.map((i) => i.listener); + } + listenerCount() { + return this._listeners.length; + } + run(args) { + const listenerCount = this.listenerCount(); + this._listeners = this._listeners.filter((item) => { + const argsCopy = args.slice(); + // Call the callback in the next event loop + setTimeout(() => { + item.listener.apply(this, argsCopy); + }, 0); + // Reschedule it if it not "once" + return !(item.once); + }); + return listenerCount; + } + prepareEvent(event) { + } + // Returns the array that will be applied to an emit + getEmit(event) { + return [event]; + } + } + class ErrorRunningEvent extends RunningEvent { + constructor() { + super("error", null); + } + } + // @TODO Fragment should inherit Wildcard? and just override getEmit? + // or have a common abstract super class, with enough constructor + // options to configure both. + // A Fragment Event will populate all the properties that Wildcard + // will, and additionally dereference the arguments when emitting + class FragmentRunningEvent extends RunningEvent { + constructor(address, contractInterface, fragment, topics) { + const filter = { + address: address + }; + let topic = contractInterface.getEventTopic(fragment); + if (topics) { + if (topic !== topics[0]) { + logger.throwArgumentError("topic mismatch", "topics", topics); + } + filter.topics = topics.slice(); + } + else { + filter.topics = [topic]; + } + super(getEventTag(filter), filter); + defineReadOnly(this, "address", address); + defineReadOnly(this, "interface", contractInterface); + defineReadOnly(this, "fragment", fragment); + } + prepareEvent(event) { + super.prepareEvent(event); + event.event = this.fragment.name; + event.eventSignature = this.fragment.format(); + event.decode = (data, topics) => { + return this.interface.decodeEventLog(this.fragment, data, topics); + }; + try { + event.args = this.interface.decodeEventLog(this.fragment, event.data, event.topics); + } + catch (error) { + event.args = null; + event.decodeError = error; + } + } + getEmit(event) { + const errors = checkResultErrors(event.args); + if (errors.length) { + throw errors[0].error; + } + const args = (event.args || []).slice(); + args.push(event); + return args; + } + } + // A Wildcard Event will attempt to populate: + // - event The name of the event name + // - eventSignature The full signature of the event + // - decode A function to decode data and topics + // - args The decoded data and topics + class WildcardRunningEvent extends RunningEvent { + constructor(address, contractInterface) { + super("*", { address: address }); + defineReadOnly(this, "address", address); + defineReadOnly(this, "interface", contractInterface); + } + prepareEvent(event) { + super.prepareEvent(event); + try { + const parsed = this.interface.parseLog(event); + event.event = parsed.name; + event.eventSignature = parsed.signature; + event.decode = (data, topics) => { + return this.interface.decodeEventLog(parsed.eventFragment, data, topics); + }; + event.args = parsed.args; + } + catch (error) { + // No matching event + } + } + } + class BaseContract { + constructor(addressOrName, contractInterface, signerOrProvider) { + // @TODO: Maybe still check the addressOrName looks like a valid address or name? + //address = getAddress(address); + defineReadOnly(this, "interface", getStatic(new.target, "getInterface")(contractInterface)); + if (signerOrProvider == null) { + defineReadOnly(this, "provider", null); + defineReadOnly(this, "signer", null); + } + else if (Signer.isSigner(signerOrProvider)) { + defineReadOnly(this, "provider", signerOrProvider.provider || null); + defineReadOnly(this, "signer", signerOrProvider); + } + else if (Provider.isProvider(signerOrProvider)) { + defineReadOnly(this, "provider", signerOrProvider); + defineReadOnly(this, "signer", null); + } + else { + logger.throwArgumentError("invalid signer or provider", "signerOrProvider", signerOrProvider); + } + defineReadOnly(this, "callStatic", {}); + defineReadOnly(this, "estimateGas", {}); + defineReadOnly(this, "functions", {}); + defineReadOnly(this, "populateTransaction", {}); + defineReadOnly(this, "filters", {}); + { + const uniqueFilters = {}; + Object.keys(this.interface.events).forEach((eventSignature) => { + const event = this.interface.events[eventSignature]; + defineReadOnly(this.filters, eventSignature, (...args) => { + return { + address: this.address, + topics: this.interface.encodeFilterTopics(event, args) + }; + }); + if (!uniqueFilters[event.name]) { + uniqueFilters[event.name] = []; + } + uniqueFilters[event.name].push(eventSignature); + }); + Object.keys(uniqueFilters).forEach((name) => { + const filters = uniqueFilters[name]; + if (filters.length === 1) { + defineReadOnly(this.filters, name, this.filters[filters[0]]); + } + else { + logger.warn(`Duplicate definition of ${name} (${filters.join(", ")})`); + } + }); + } + defineReadOnly(this, "_runningEvents", {}); + defineReadOnly(this, "_wrappedEmits", {}); + if (addressOrName == null) { + logger.throwArgumentError("invalid contract address or ENS name", "addressOrName", addressOrName); + } + defineReadOnly(this, "address", addressOrName); + if (this.provider) { + defineReadOnly(this, "resolvedAddress", resolveName(this.provider, addressOrName)); + } + else { + try { + defineReadOnly(this, "resolvedAddress", Promise.resolve(getAddress(addressOrName))); + } + catch (error) { + // Without a provider, we cannot use ENS names + logger.throwError("provider is required to use ENS name as contract address", Logger.errors.UNSUPPORTED_OPERATION, { + operation: "new Contract" + }); + } + } + // Swallow bad ENS names to prevent Unhandled Exceptions + this.resolvedAddress.catch((e) => { }); + const uniqueNames = {}; + const uniqueSignatures = {}; + Object.keys(this.interface.functions).forEach((signature) => { + const fragment = this.interface.functions[signature]; + // Check that the signature is unique; if not the ABI generation has + // not been cleaned or may be incorrectly generated + if (uniqueSignatures[signature]) { + logger.warn(`Duplicate ABI entry for ${JSON.stringify(signature)}`); + return; + } + uniqueSignatures[signature] = true; + // Track unique names; we only expose bare named functions if they + // are ambiguous + { + const name = fragment.name; + if (!uniqueNames[`%${name}`]) { + uniqueNames[`%${name}`] = []; + } + uniqueNames[`%${name}`].push(signature); + } + if (this[signature] == null) { + defineReadOnly(this, signature, buildDefault(this, fragment, true)); + } + // We do not collapse simple calls on this bucket, which allows + // frameworks to safely use this without introspection as well as + // allows decoding error recovery. + if (this.functions[signature] == null) { + defineReadOnly(this.functions, signature, buildDefault(this, fragment, false)); + } + if (this.callStatic[signature] == null) { + defineReadOnly(this.callStatic, signature, buildCall(this, fragment, true)); + } + if (this.populateTransaction[signature] == null) { + defineReadOnly(this.populateTransaction, signature, buildPopulate(this, fragment)); + } + if (this.estimateGas[signature] == null) { + defineReadOnly(this.estimateGas, signature, buildEstimate(this, fragment)); + } + }); + Object.keys(uniqueNames).forEach((name) => { + // Ambiguous names to not get attached as bare names + const signatures = uniqueNames[name]; + if (signatures.length > 1) { + return; + } + // Strip off the leading "%" used for prototype protection + name = name.substring(1); + const signature = signatures[0]; + // If overwriting a member property that is null, swallow the error + try { + if (this[name] == null) { + defineReadOnly(this, name, this[signature]); + } + } + catch (e) { } + if (this.functions[name] == null) { + defineReadOnly(this.functions, name, this.functions[signature]); + } + if (this.callStatic[name] == null) { + defineReadOnly(this.callStatic, name, this.callStatic[signature]); + } + if (this.populateTransaction[name] == null) { + defineReadOnly(this.populateTransaction, name, this.populateTransaction[signature]); + } + if (this.estimateGas[name] == null) { + defineReadOnly(this.estimateGas, name, this.estimateGas[signature]); + } + }); + } + static getContractAddress(transaction) { + return getContractAddress(transaction); + } + static getInterface(contractInterface) { + if (Interface.isInterface(contractInterface)) { + return contractInterface; + } + return new Interface(contractInterface); + } + // @TODO: Allow timeout? + deployed() { + return this._deployed(); + } + _deployed(blockTag) { + if (!this._deployedPromise) { + // If we were just deployed, we know the transaction we should occur in + if (this.deployTransaction) { + this._deployedPromise = this.deployTransaction.wait().then(() => { + return this; + }); + } + else { + // @TODO: Once we allow a timeout to be passed in, we will wait + // up to that many blocks for getCode + // Otherwise, poll for our code to be deployed + this._deployedPromise = this.provider.getCode(this.address, blockTag).then((code) => { + if (code === "0x") { + logger.throwError("contract not deployed", Logger.errors.UNSUPPORTED_OPERATION, { + contractAddress: this.address, + operation: "getDeployed" + }); + } + return this; + }); + } + } + return this._deployedPromise; + } + // @TODO: + // estimateFallback(overrides?: TransactionRequest): Promise + // @TODO: + // estimateDeploy(bytecode: string, ...args): Promise + fallback(overrides) { + if (!this.signer) { + logger.throwError("sending a transactions require a signer", Logger.errors.UNSUPPORTED_OPERATION, { operation: "sendTransaction(fallback)" }); + } + const tx = shallowCopy(overrides || {}); + ["from", "to"].forEach(function (key) { + if (tx[key] == null) { + return; + } + logger.throwError("cannot override " + key, Logger.errors.UNSUPPORTED_OPERATION, { operation: key }); + }); + tx.to = this.resolvedAddress; + return this.deployed().then(() => { + return this.signer.sendTransaction(tx); + }); + } + // Reconnect to a different signer or provider + connect(signerOrProvider) { + if (typeof (signerOrProvider) === "string") { + signerOrProvider = new VoidSigner(signerOrProvider, this.provider); + } + const contract = new (this.constructor)(this.address, this.interface, signerOrProvider); + if (this.deployTransaction) { + defineReadOnly(contract, "deployTransaction", this.deployTransaction); + } + return contract; + } + // Re-attach to a different on-chain instance of this contract + attach(addressOrName) { + return new (this.constructor)(addressOrName, this.interface, this.signer || this.provider); + } + static isIndexed(value) { + return Indexed.isIndexed(value); + } + _normalizeRunningEvent(runningEvent) { + // Already have an instance of this event running; we can re-use it + if (this._runningEvents[runningEvent.tag]) { + return this._runningEvents[runningEvent.tag]; + } + return runningEvent; + } + _getRunningEvent(eventName) { + if (typeof (eventName) === "string") { + // Listen for "error" events (if your contract has an error event, include + // the full signature to bypass this special event keyword) + if (eventName === "error") { + return this._normalizeRunningEvent(new ErrorRunningEvent()); + } + // Listen for any event that is registered + if (eventName === "event") { + return this._normalizeRunningEvent(new RunningEvent("event", null)); + } + // Listen for any event + if (eventName === "*") { + return this._normalizeRunningEvent(new WildcardRunningEvent(this.address, this.interface)); + } + // Get the event Fragment (throws if ambiguous/unknown event) + const fragment = this.interface.getEvent(eventName); + return this._normalizeRunningEvent(new FragmentRunningEvent(this.address, this.interface, fragment)); + } + // We have topics to filter by... + if (eventName.topics && eventName.topics.length > 0) { + // Is it a known topichash? (throws if no matching topichash) + try { + const topic = eventName.topics[0]; + if (typeof (topic) !== "string") { + throw new Error("invalid topic"); // @TODO: May happen for anonymous events + } + const fragment = this.interface.getEvent(topic); + return this._normalizeRunningEvent(new FragmentRunningEvent(this.address, this.interface, fragment, eventName.topics)); + } + catch (error) { } + // Filter by the unknown topichash + const filter = { + address: this.address, + topics: eventName.topics + }; + return this._normalizeRunningEvent(new RunningEvent(getEventTag(filter), filter)); + } + return this._normalizeRunningEvent(new WildcardRunningEvent(this.address, this.interface)); + } + _checkRunningEvents(runningEvent) { + if (runningEvent.listenerCount() === 0) { + delete this._runningEvents[runningEvent.tag]; + // If we have a poller for this, remove it + const emit = this._wrappedEmits[runningEvent.tag]; + if (emit && runningEvent.filter) { + this.provider.off(runningEvent.filter, emit); + delete this._wrappedEmits[runningEvent.tag]; + } + } + } + // Subclasses can override this to gracefully recover + // from parse errors if they wish + _wrapEvent(runningEvent, log, listener) { + const event = deepCopy(log); + event.removeListener = () => { + if (!listener) { + return; + } + runningEvent.removeListener(listener); + this._checkRunningEvents(runningEvent); + }; + event.getBlock = () => { return this.provider.getBlock(log.blockHash); }; + event.getTransaction = () => { return this.provider.getTransaction(log.transactionHash); }; + event.getTransactionReceipt = () => { return this.provider.getTransactionReceipt(log.transactionHash); }; + // This may throw if the topics and data mismatch the signature + runningEvent.prepareEvent(event); + return event; + } + _addEventListener(runningEvent, listener, once) { + if (!this.provider) { + logger.throwError("events require a provider or a signer with a provider", Logger.errors.UNSUPPORTED_OPERATION, { operation: "once" }); + } + runningEvent.addListener(listener, once); + // Track this running event and its listeners (may already be there; but no hard in updating) + this._runningEvents[runningEvent.tag] = runningEvent; + // If we are not polling the provider, start polling + if (!this._wrappedEmits[runningEvent.tag]) { + const wrappedEmit = (log) => { + let event = this._wrapEvent(runningEvent, log, listener); + // Try to emit the result for the parameterized event... + if (event.decodeError == null) { + try { + const args = runningEvent.getEmit(event); + this.emit(runningEvent.filter, ...args); + } + catch (error) { + event.decodeError = error.error; + } + } + // Always emit "event" for fragment-base events + if (runningEvent.filter != null) { + this.emit("event", event); + } + // Emit "error" if there was an error + if (event.decodeError != null) { + this.emit("error", event.decodeError, event); + } + }; + this._wrappedEmits[runningEvent.tag] = wrappedEmit; + // Special events, like "error" do not have a filter + if (runningEvent.filter != null) { + this.provider.on(runningEvent.filter, wrappedEmit); + } + } + } + queryFilter(event, fromBlockOrBlockhash, toBlock) { + const runningEvent = this._getRunningEvent(event); + const filter = shallowCopy(runningEvent.filter); + if (typeof (fromBlockOrBlockhash) === "string" && isHexString(fromBlockOrBlockhash, 32)) { + if (toBlock != null) { + logger.throwArgumentError("cannot specify toBlock with blockhash", "toBlock", toBlock); + } + filter.blockHash = fromBlockOrBlockhash; + } + else { + filter.fromBlock = ((fromBlockOrBlockhash != null) ? fromBlockOrBlockhash : 0); + filter.toBlock = ((toBlock != null) ? toBlock : "latest"); + } + return this.provider.getLogs(filter).then((logs) => { + return logs.map((log) => this._wrapEvent(runningEvent, log, null)); + }); + } + on(event, listener) { + this._addEventListener(this._getRunningEvent(event), listener, false); + return this; + } + once(event, listener) { + this._addEventListener(this._getRunningEvent(event), listener, true); + return this; + } + emit(eventName, ...args) { + if (!this.provider) { + return false; + } + const runningEvent = this._getRunningEvent(eventName); + const result = (runningEvent.run(args) > 0); + // May have drained all the "once" events; check for living events + this._checkRunningEvents(runningEvent); + return result; + } + listenerCount(eventName) { + if (!this.provider) { + return 0; + } + if (eventName == null) { + return Object.keys(this._runningEvents).reduce((accum, key) => { + return accum + this._runningEvents[key].listenerCount(); + }, 0); + } + return this._getRunningEvent(eventName).listenerCount(); + } + listeners(eventName) { + if (!this.provider) { + return []; + } + if (eventName == null) { + const result = []; + for (let tag in this._runningEvents) { + this._runningEvents[tag].listeners().forEach((listener) => { + result.push(listener); + }); + } + return result; + } + return this._getRunningEvent(eventName).listeners(); + } + removeAllListeners(eventName) { + if (!this.provider) { + return this; + } + if (eventName == null) { + for (const tag in this._runningEvents) { + const runningEvent = this._runningEvents[tag]; + runningEvent.removeAllListeners(); + this._checkRunningEvents(runningEvent); + } + return this; + } + // Delete any listeners + const runningEvent = this._getRunningEvent(eventName); + runningEvent.removeAllListeners(); + this._checkRunningEvents(runningEvent); + return this; + } + off(eventName, listener) { + if (!this.provider) { + return this; + } + const runningEvent = this._getRunningEvent(eventName); + runningEvent.removeListener(listener); + this._checkRunningEvents(runningEvent); + return this; + } + removeListener(eventName, listener) { + return this.off(eventName, listener); + } + } + class Contract extends BaseContract { + } + + const getContractArguments = ({ contract, method, params })=>{ + let fragment = contract.interface.fragments.find((fragment) => { + return fragment.name == method + }); + + if(params instanceof Array) { + return params + } else if (params instanceof Object) { + return fragment.inputs.map((input) => { + return params[input.name] + }) + } + }; + + var estimateEVM = ({ provider, from, to, value, method, api, params }) => { + if(typeof api == "undefined"){ + return provider.estimateGas({ from, to, value }) + } else { + let contract = new Contract(to, api, provider); + let contractMethod = contract.estimateGas[method]; + let contractArguments = getContractArguments({ contract, method, params }); + if(contractArguments) { + return contractMethod(...contractArguments, { from, value }) + } else { + return contractMethod({ from, value }) + } + } + }; + + function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; } + let getCacheStore = () => { + if (getWindow()._cacheStore == undefined) { + resetCache(); + } + return getWindow()._cacheStore + }; + + let getPromiseStore = () => { + if (getWindow()._promiseStore == undefined) { + resetCache(); + } + return getWindow()._promiseStore + }; + + let resetCache = () => { + getWindow()._cacheStore = {}; + getWindow()._promiseStore = {}; + getWindow()._clientProviders = {}; + }; + + let set = function ({ key, value, expires }) { + getCacheStore()[key] = { + expiresAt: Date.now() + expires, + value, + }; + }; + + let get = function ({ key, expires }) { + let cachedEntry = getCacheStore()[key]; + if (_optionalChain([cachedEntry, 'optionalAccess', _ => _.expiresAt]) > Date.now()) { + return cachedEntry.value + } + }; + + let getPromise = function({ key }) { + return getPromiseStore()[key] + }; + + let setPromise = function({ key, promise }) { + getPromiseStore()[key] = promise; + return promise + }; + + let deletePromise = function({ key }) { + getPromiseStore()[key] = undefined; + }; + + let cache = function ({ call, key, expires = 0 }) { + return new Promise((resolve, reject)=>{ + let value; + key = JSON.stringify(key); + + // get existing promise (of a previous pending request asking for the exact same thing) + let existingPromise = getPromise({ key }); + if(existingPromise) { + return existingPromise + .then(resolve) + .catch(reject) + } + + setPromise({ key, promise: new Promise((resolveQueue, rejectQueue)=>{ + if (expires === 0) { + return call() + .then((value)=>{ + resolve(value); + resolveQueue(value); + }) + .catch((error)=>{ + reject(error); + rejectQueue(error); + }) + } + + // get cached value + value = get({ key, expires }); + if (value) { + resolve(value); + resolveQueue(value); + return value + } + + // set new cache value + call() + .then((value)=>{ + if (value) { + set({ key, value, expires }); + } + resolve(value); + resolveQueue(value); + }) + .catch((error)=>{ + reject(error); + rejectQueue(error); + }); + }) + }).then(()=>{ + deletePromise({ key }); + }).catch(()=>{ + deletePromise({ key }); + }); + }) + }; + + let estimate = async function ({ blockchain, from, to, value, method, api, params, cache: cache$1 }) { + if(!supported.includes(blockchain)) { throw 'Unknown blockchain: ' + blockchain } + if(typeof value == 'undefined') { value = '0'; } + + const provider = await getProvider(blockchain); + + return await cache({ + expires: cache$1 || 0, + key: [blockchain, from, to, value, method, params], + call: async()=>estimateEVM({ provider, from, to, value, method, api, params }) + }) + }; + + let accountInfo = async ({ address, api, method, params, provider, block }) => { + const info = await provider.getAccountInfo(new solanaWeb3_js.PublicKey(address)); + return api.decode(info.data) + }; + + let balance = ({ address, provider }) => { + return provider.getBalance(new solanaWeb3_js.PublicKey(address)) + }; + + var requestSolana = async ({ blockchain, address, api, method, params, block }) => { + const provider = await Solana.getProvider(blockchain); + + if(method == undefined || method === 'getAccountInfo') { + if(api == undefined) { + api = solanaWeb3_js.ACCOUNT_LAYOUT; + } + return accountInfo({ address, api, method, params, provider, block }) + } else if(method === 'getProgramAccounts') { + return provider.getProgramAccounts(new solanaWeb3_js.PublicKey(address), params).then((accounts)=>{ + if(api){ + return accounts.map((account)=>{ + account.data = api.decode(account.account.data); + return account + }) + } else { + return accounts + } + }) + } else if(method === 'getTokenAccountBalance') { + return provider.getTokenAccountBalance(new solanaWeb3_js.PublicKey(address)) + } else if (method === 'latestBlockNumber') { + return provider.getBlockHeight() + } else if (method === 'balance') { + return balance({ address, provider }) + } + }; + + var parseUrl = (url) => { + if (typeof url == 'object') { + return url + } + let deconstructed = url.match(/(?\w+):\/\/(?[\w\d]+)(\/(?[\w\d]+)*)?/); + + if(deconstructed.groups.part2 == undefined) { + if(deconstructed.groups.part1.match(/\d/)) { + return { + blockchain: deconstructed.groups.blockchain, + address: deconstructed.groups.part1 + } + } else { + return { + blockchain: deconstructed.groups.blockchain, + method: deconstructed.groups.part1 + } + } + } else { + return { + blockchain: deconstructed.groups.blockchain, + address: deconstructed.groups.part1, + method: deconstructed.groups.part2 + } + } + }; + + let request = async function (url, options) { + let { blockchain, address, method } = parseUrl(url); + let { api, params, cache: cache$1, block } = (typeof(url) == 'object' ? url : options) || {}; + + return await cache({ + expires: cache$1 || 0, + key: [blockchain, address, method, params, block], + call: async()=>{ + if(supported.evm.includes(blockchain)) ; else if(supported.solana.includes(blockchain)) { + + + return requestSolana({ blockchain, address, api, method, params, block }) + + + } else { + throw 'Unknown blockchain: ' + blockchain + } + } + }) + }; + + exports.estimate = estimate; + exports.getProvider = getProvider; + exports.request = request; + exports.resetCache = resetCache; + exports.setProvider = setProvider; + exports.setProviderEndpoints = setProviderEndpoints; + exports.simulate = simulate; + + Object.defineProperty(exports, '__esModule', { value: true }); + +})); diff --git a/package.evm.json b/package.evm.json index 094d9f8..0e76df7 100644 --- a/package.evm.json +++ b/package.evm.json @@ -1,7 +1,7 @@ { "name": "@depay/web3-client-evm", "moduleName": "Web3Client", - "version": "10.6.0", + "version": "10.6.1", "description": "A web3 client to fetch blockchain data just like you are used to with HTTP clients.", "main": "dist/umd/index.evm.js", "module": "dist/esm/index.evm.js", diff --git a/package.json b/package.json index d81fbc8..02ab27a 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@depay/web3-client", "moduleName": "Web3Client", - "version": "10.6.0", + "version": "10.6.1", "description": "A web3 client to fetch blockchain data just like you are used to with HTTP clients.", "main": "dist/umd/index.js", "module": "dist/esm/index.js", @@ -10,7 +10,7 @@ "dist" ], "scripts": { - "build": "rm -rf dist && rollup -c rollup.module.config.js && rollup -c rollup.module.evm.config.js", + "build": "rm -rf dist && rollup -c rollup.module.config.js && rollup -c rollup.module.evm.config.js && rollup -c rollup.module.solana.config.js", "build:evm": "rollup -c rollup.module.evm.config.js", "lint": "eslint \"src/**/*.{js,jsx,ts,tsx}\"", "lint:fix": "eslint \"src/**/*.{js,jsx,ts,tsx}\" --fix", diff --git a/package.solana.json b/package.solana.json index 2ee4d56..6622e06 100644 --- a/package.solana.json +++ b/package.solana.json @@ -1,7 +1,7 @@ { "name": "@depay/web3-client-solana", "moduleName": "Web3Client", - "version": "10.6.0", + "version": "10.6.1", "description": "A web3 client to fetch blockchain data just like you are used to with HTTP clients.", "main": "dist/umd/index.solana.js", "module": "dist/esm/index.solana.js", diff --git a/rollup.module.solana.config.js b/rollup.module.solana.config.js new file mode 100644 index 0000000..564aae8 --- /dev/null +++ b/rollup.module.solana.config.js @@ -0,0 +1,47 @@ +import commonjs from '@rollup/plugin-commonjs' +import globals from './rollup.globals.js' +import jscc from 'rollup-plugin-jscc' +import json from '@rollup/plugin-json' +import pkg from './package.solana.json' +import replace from '@rollup/plugin-replace' +import resolve from '@rollup/plugin-node-resolve' +import sucrase from '@rollup/plugin-sucrase' +import { nodeResolve } from '@rollup/plugin-node-resolve' + +export default { + input: 'src/index.js', + output: [ + { + format: 'es', + globals: globals, + file: 'dist/esm/index.solana.js' + }, + { + format: 'umd', + name: pkg.moduleName, + globals: globals, + file: 'dist/umd/index.solana.js' + } + ], + external: [ + ...Object.keys(pkg.dependencies || {}), + ...Object.keys(pkg.peerDependencies || {}), + ], + plugins: [ + jscc({ values: { _SOLANA: 1 }}), + json(), + sucrase({ + exclude: ['node_modules/**'], + transforms: ['jsx'] + }), + resolve(), + nodeResolve(), + commonjs({ + include: 'node_modules/**' + }), + replace({ + 'process.env.NODE_ENV': JSON.stringify( 'production' ), + preventAssignment: true + }) + ] +}