From d7a0e5b1ef309247896d8965072b583bbebbe862 Mon Sep 17 00:00:00 2001 From: Sebastian Pape Date: Sat, 29 Jul 2023 16:49:08 +0200 Subject: [PATCH] v1.25.0: export bs58 and deserialize --- README.md | 4 +- dist/esm/index.js | 337 +++++++++++++++++++++++++++++++++++++++++----- dist/umd/index.js | 336 ++++++++++++++++++++++++++++++++++++++++----- package.json | 5 +- src/index.js | 4 + 5 files changed, 617 insertions(+), 69 deletions(-) diff --git a/README.md b/README.md index 6fa6234..ab08c5e 100644 --- a/README.md +++ b/README.md @@ -47,7 +47,9 @@ import { BN, Keypair, transact, - Web3MobileWallet + Web3MobileWallet, + bs58, + deserialize } from "@depay/solana-web3.js" ``` diff --git a/dist/esm/index.js b/dist/esm/index.js index 10e65a1..2132991 100644 --- a/dist/esm/index.js +++ b/dist/esm/index.js @@ -8119,7 +8119,7 @@ var require$$1$1 = /*@__PURE__*/getAugmentedNamespace(_polyfillNode_buffer); var BN$1 = bn$1.exports; -var safeBuffer = {exports: {}}; +var safeBuffer$1 = {exports: {}}; /*! safe-buffer. MIT License. Feross Aboukhadijeh */ @@ -8193,7 +8193,7 @@ var safeBuffer = {exports: {}}; return buffer.SlowBuffer(size); }; -})(safeBuffer, safeBuffer.exports); +})(safeBuffer$1, safeBuffer$1.exports); // Copyright (c) 2018 base-x contributors // Copyright (c) 2014-2018 The Bitcoin Core developers (base58.cpp) @@ -8202,9 +8202,9 @@ var safeBuffer = {exports: {}}; // @ts-ignore -var _Buffer = safeBuffer.exports.Buffer; +var _Buffer$1 = safeBuffer$1.exports.Buffer; -function base$1(ALPHABET) { +function base$2(ALPHABET) { if (ALPHABET.length >= 255) { throw new TypeError('Alphabet too long'); } @@ -8234,10 +8234,10 @@ function base$1(ALPHABET) { function encode(source) { if (Array.isArray(source) || source instanceof Uint8Array) { - source = _Buffer.from(source); + source = _Buffer$1.from(source); } - if (!_Buffer.isBuffer(source)) { + if (!_Buffer$1.isBuffer(source)) { throw new TypeError('Expected Buffer'); } @@ -8302,7 +8302,7 @@ function base$1(ALPHABET) { } if (source.length === 0) { - return _Buffer.alloc(0); + return _Buffer$1.alloc(0); } var psz = 0; // Skip and count leading '1's. @@ -8351,7 +8351,7 @@ function base$1(ALPHABET) { it4++; } - var vch = _Buffer.allocUnsafe(zeroes + (size - it4)); + var vch = _Buffer$1.allocUnsafe(zeroes + (size - it4)); vch.fill(0x00, 0, zeroes); var j = zeroes; @@ -8380,12 +8380,12 @@ function base$1(ALPHABET) { }; } -var src$1 = base$1; +var src$2 = base$2; -var basex$1 = src$1; -var ALPHABET$1 = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'; -var bs58$2 = basex$1(ALPHABET$1); -var bs58$3 = bs58$2; +var basex$2 = src$2; +var ALPHABET$2 = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'; +var bs58$4 = basex$2(ALPHABET$2); +var bs58$5 = bs58$4; var Chi = function Chi(a, b, c) { return a & b ^ ~a & c; @@ -8564,6 +8564,273 @@ wrapConstructor(function () { var lib$1 = {}; +var safeBuffer = {exports: {}}; + +/*! safe-buffer. MIT License. Feross Aboukhadijeh */ + +(function (module, exports) { + /* eslint-disable node/no-deprecated-api */ + var buffer = require$$1$1; + var Buffer = buffer.Buffer; // alternative to using Object.keys for old browsers + + function copyProps(src, dst) { + for (var key in src) { + dst[key] = src[key]; + } + } + + if (Buffer.from && Buffer.alloc && Buffer.allocUnsafe && Buffer.allocUnsafeSlow) { + module.exports = buffer; + } else { + // Copy properties from require('buffer') + copyProps(buffer, exports); + exports.Buffer = SafeBuffer; + } + + function SafeBuffer(arg, encodingOrOffset, length) { + return Buffer(arg, encodingOrOffset, length); + } + + SafeBuffer.prototype = Object.create(Buffer.prototype); // Copy static methods from Buffer + + copyProps(Buffer, SafeBuffer); + + SafeBuffer.from = function (arg, encodingOrOffset, length) { + if (typeof arg === 'number') { + throw new TypeError('Argument must not be a number'); + } + + return Buffer(arg, encodingOrOffset, length); + }; + + SafeBuffer.alloc = function (size, fill, encoding) { + if (typeof size !== 'number') { + throw new TypeError('Argument must be a number'); + } + + var buf = Buffer(size); + + if (fill !== undefined) { + if (typeof encoding === 'string') { + buf.fill(fill, encoding); + } else { + buf.fill(fill); + } + } else { + buf.fill(0); + } + + return buf; + }; + + SafeBuffer.allocUnsafe = function (size) { + if (typeof size !== 'number') { + throw new TypeError('Argument must be a number'); + } + + return Buffer(size); + }; + + SafeBuffer.allocUnsafeSlow = function (size) { + if (typeof size !== 'number') { + throw new TypeError('Argument must be a number'); + } + + return buffer.SlowBuffer(size); + }; +})(safeBuffer, safeBuffer.exports); + +// Copyright (c) 2018 base-x contributors +// Copyright (c) 2014-2018 The Bitcoin Core developers (base58.cpp) +// Distributed under the MIT software license, see the accompanying +// file LICENSE or http://www.opensource.org/licenses/mit-license.php. +// @ts-ignore + + +var _Buffer = safeBuffer.exports.Buffer; + +function base$1(ALPHABET) { + if (ALPHABET.length >= 255) { + throw new TypeError('Alphabet too long'); + } + + var BASE_MAP = new Uint8Array(256); + + for (var j = 0; j < BASE_MAP.length; j++) { + BASE_MAP[j] = 255; + } + + for (var i = 0; i < ALPHABET.length; i++) { + var x = ALPHABET.charAt(i); + var xc = x.charCodeAt(0); + + if (BASE_MAP[xc] !== 255) { + throw new TypeError(x + ' is ambiguous'); + } + + BASE_MAP[xc] = i; + } + + var BASE = ALPHABET.length; + var LEADER = ALPHABET.charAt(0); + var FACTOR = Math.log(BASE) / Math.log(256); // log(BASE) / log(256), rounded up + + var iFACTOR = Math.log(256) / Math.log(BASE); // log(256) / log(BASE), rounded up + + function encode(source) { + if (Array.isArray(source) || source instanceof Uint8Array) { + source = _Buffer.from(source); + } + + if (!_Buffer.isBuffer(source)) { + throw new TypeError('Expected Buffer'); + } + + if (source.length === 0) { + return ''; + } // Skip & count leading zeroes. + + + var zeroes = 0; + var length = 0; + var pbegin = 0; + var pend = source.length; + + while (pbegin !== pend && source[pbegin] === 0) { + pbegin++; + zeroes++; + } // Allocate enough space in big-endian base58 representation. + + + var size = (pend - pbegin) * iFACTOR + 1 >>> 0; + var b58 = new Uint8Array(size); // Process the bytes. + + while (pbegin !== pend) { + var carry = source[pbegin]; // Apply "b58 = b58 * 256 + ch". + + var i = 0; + + for (var it1 = size - 1; (carry !== 0 || i < length) && it1 !== -1; it1--, i++) { + carry += 256 * b58[it1] >>> 0; + b58[it1] = carry % BASE >>> 0; + carry = carry / BASE >>> 0; + } + + if (carry !== 0) { + throw new Error('Non-zero carry'); + } + + length = i; + pbegin++; + } // Skip leading zeroes in base58 result. + + + var it2 = size - length; + + while (it2 !== size && b58[it2] === 0) { + it2++; + } // Translate the result into a string. + + + var str = LEADER.repeat(zeroes); + + for (; it2 < size; ++it2) { + str += ALPHABET.charAt(b58[it2]); + } + + return str; + } + + function decodeUnsafe(source) { + if (typeof source !== 'string') { + throw new TypeError('Expected String'); + } + + if (source.length === 0) { + return _Buffer.alloc(0); + } + + var psz = 0; // Skip and count leading '1's. + + var zeroes = 0; + var length = 0; + + while (source[psz] === LEADER) { + zeroes++; + psz++; + } // Allocate enough space in big-endian base256 representation. + + + var size = (source.length - psz) * FACTOR + 1 >>> 0; // log(58) / log(256), rounded up. + + var b256 = new Uint8Array(size); // Process the characters. + + while (source[psz]) { + // Decode character + var carry = BASE_MAP[source.charCodeAt(psz)]; // Invalid character + + if (carry === 255) { + return; + } + + var i = 0; + + for (var it3 = size - 1; (carry !== 0 || i < length) && it3 !== -1; it3--, i++) { + carry += BASE * b256[it3] >>> 0; + b256[it3] = carry % 256 >>> 0; + carry = carry / 256 >>> 0; + } + + if (carry !== 0) { + throw new Error('Non-zero carry'); + } + + length = i; + psz++; + } // Skip leading zeroes in b256. + + + var it4 = size - length; + + while (it4 !== size && b256[it4] === 0) { + it4++; + } + + var vch = _Buffer.allocUnsafe(zeroes + (size - it4)); + + vch.fill(0x00, 0, zeroes); + var j = zeroes; + + while (it4 !== size) { + vch[j++] = b256[it4++]; + } + + return vch; + } + + function decode(string) { + var buffer = decodeUnsafe(string); + + if (buffer) { + return buffer; + } + + throw new Error('Non-base' + BASE + ' character'); + } + + return { + encode: encode, + decodeUnsafe: decodeUnsafe, + decode: decode + }; +} + +var src$1 = base$1; + +var basex$1 = src$1; +var ALPHABET$1 = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'; +var bs58$3 = basex$1(ALPHABET$1); + function inRange(a, min, max) { return min <= a && a <= max; } @@ -9220,7 +9487,7 @@ var deserializeUnchecked_1 = lib$1.deserializeUnchecked = deserialize_1 = lib$1. var bn_js_1 = __importDefault(bn$1.exports); -var bs58_1 = __importDefault(bs58$2); // TODO: Make sure this polyfill not included when not required +var bs58_1 = __importDefault(bs58$3); // TODO: Make sure this polyfill not included when not required var encoding = __importStar(require$$2); @@ -18967,7 +19234,7 @@ var PublicKey = /*#__PURE__*/function (_Struct2, _Symbol$toStringTag2) { } else { if (typeof value === 'string') { // assume base 58 encoding by default - var decoded = bs58$3.decode(value); + var decoded = bs58$5.decode(value); if (decoded.length != PUBLIC_KEY_LENGTH) { throw new Error("Invalid public key input"); @@ -19011,7 +19278,7 @@ var PublicKey = /*#__PURE__*/function (_Struct2, _Symbol$toStringTag2) { }, { key: "toBase58", value: function toBase58() { - return bs58$3.encode(this.toBytes()); + return bs58$5.encode(this.toBytes()); } }, { key: "toJSON", @@ -19896,7 +20163,7 @@ var Message = /*#__PURE__*/function () { return { programIdIndex: ix.programIdIndex, accountKeyIndexes: ix.accounts, - data: bs58$3.decode(ix.data) + data: bs58$5.decode(ix.data) }; }); } @@ -19958,7 +20225,7 @@ var Message = /*#__PURE__*/function () { var instructions = this.instructions.map(function (instruction) { var accounts = instruction.accounts, programIdIndex = instruction.programIdIndex; - var data = Array.from(bs58$3.decode(instruction.data)); + var data = Array.from(bs58$5.decode(instruction.data)); var keyIndicesCount = []; encodeLength(keyIndicesCount, accounts.length); var dataCount = []; @@ -19991,7 +20258,7 @@ var Message = /*#__PURE__*/function () { keys: this.accountKeys.map(function (key) { return toBuffer(key.toBytes()); }), - recentBlockhash: bs58$3.decode(this.recentBlockhash) + recentBlockhash: bs58$5.decode(this.recentBlockhash) }; var signData = Buffer$1.alloc(2048); var length = signDataLayout.encode(transaction, signData); @@ -20017,7 +20284,7 @@ var Message = /*#__PURE__*/function () { return { programIdIndex: ix.programIdIndex, accounts: ix.accountKeyIndexes, - data: bs58$3.encode(ix.data) + data: bs58$5.encode(ix.data) }; }); return new Message({ @@ -20064,7 +20331,7 @@ var Message = /*#__PURE__*/function () { byteArray = byteArray.slice(_accountCount); var dataLength = decodeLength(byteArray); var dataSlice = byteArray.slice(0, dataLength); - var data = bs58$3.encode(Buffer$1.from(dataSlice)); + var data = bs58$5.encode(Buffer$1.from(dataSlice)); byteArray = byteArray.slice(dataLength); instructions.push({ programIdIndex: programIdIndex, @@ -20079,7 +20346,7 @@ var Message = /*#__PURE__*/function () { numReadonlySignedAccounts: numReadonlySignedAccounts, numReadonlyUnsignedAccounts: numReadonlyUnsignedAccounts }, - recentBlockhash: bs58$3.encode(Buffer$1.from(recentBlockhash)), + recentBlockhash: bs58$5.encode(Buffer$1.from(recentBlockhash)), accountKeys: accountKeys, instructions: instructions }; @@ -20275,7 +20542,7 @@ var MessageV0 = /*#__PURE__*/function () { staticAccountKeys: this.staticAccountKeys.map(function (key) { return key.toBytes(); }), - recentBlockhash: bs58$3.decode(this.recentBlockhash), + recentBlockhash: bs58$5.decode(this.recentBlockhash), instructionsLength: new Uint8Array(encodedInstructionsLength), serializedInstructions: serializedInstructions, addressTableLookupsLength: new Uint8Array(encodedAddressTableLookupsLength), @@ -20427,7 +20694,7 @@ var MessageV0 = /*#__PURE__*/function () { staticAccountKeys.push(new PublicKey(byteArray.splice(0, PUBLIC_KEY_LENGTH))); } - var recentBlockhash = bs58$3.encode(byteArray.splice(0, PUBLIC_KEY_LENGTH)); + var recentBlockhash = bs58$5.encode(byteArray.splice(0, PUBLIC_KEY_LENGTH)); var instructionCount = decodeLength(byteArray); var compiledInstructions = []; @@ -20899,7 +21166,7 @@ var Transaction = /*#__PURE__*/function () { accounts: instruction.keys.map(function (meta) { return accountKeys.indexOf(meta.pubkey.toString()); }), - data: bs58$3.encode(data) + data: bs58$5.encode(data) }; }); compiledInstructions.forEach(function (instruction) { @@ -21312,7 +21579,7 @@ var Transaction = /*#__PURE__*/function () { var _signature2 = byteArray.slice(0, SIGNATURE_LENGTH_IN_BYTES); byteArray = byteArray.slice(SIGNATURE_LENGTH_IN_BYTES); - signatures.push(bs58$3.encode(Buffer$1.from(_signature2))); + signatures.push(bs58$5.encode(Buffer$1.from(_signature2))); } return Transaction.populate(Message.from(byteArray), signatures); @@ -21334,7 +21601,7 @@ var Transaction = /*#__PURE__*/function () { signatures.forEach(function (signature, index) { var sigPubkeyPair = { - signature: signature == bs58$3.encode(DEFAULT_SIGNATURE) ? null : bs58$3.decode(signature), + signature: signature == bs58$5.encode(DEFAULT_SIGNATURE) ? null : bs58$5.decode(signature), publicKey: message.accountKeys[index] }; transaction.signatures.push(sigPubkeyPair); @@ -21353,7 +21620,7 @@ var Transaction = /*#__PURE__*/function () { transaction.instructions.push(new TransactionInstruction({ keys: keys, programId: message.accountKeys[instruction.programIdIndex], - data: bs58$3.decode(instruction.data) + data: bs58$5.decode(instruction.data) })); }); transaction._message = message; @@ -23510,7 +23777,7 @@ function versionedMessageFromResponse(version, response) { return { programIdIndex: ix.programIdIndex, accountKeyIndexes: ix.accounts, - data: bs58$3.decode(ix.data) + data: bs58$5.decode(ix.data) }; }), addressTableLookups: response.addressTableLookups @@ -25796,7 +26063,7 @@ var Connection = /*#__PURE__*/function () { case 8: _context31.prev = 8; - decodedSignature = bs58$3.decode(rawSignature); + decodedSignature = bs58$5.decode(rawSignature); _context31.next = 15; break; @@ -34502,14 +34769,14 @@ var src = base; var basex = src; var ALPHABET = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'; -var bs58$1 = basex(ALPHABET); +var bs58$2 = basex(ALPHABET); Object.defineProperty(cjs$1, '__esModule', { value: true }); var web3_js = require$$1; var mobileWalletAdapterProtocol = cjs; -var bs58 = bs58$1; +var bs58$1 = bs58$2; function _interopDefaultLegacy(e) { return e && _typeof$1(e) === 'object' && 'default' in e ? e : { @@ -34517,7 +34784,7 @@ function _interopDefaultLegacy(e) { }; } -var bs58__default = /*#__PURE__*/_interopDefaultLegacy(bs58); +var bs58__default = /*#__PURE__*/_interopDefaultLegacy(bs58$1); /****************************************************************************** Copyright (c) Microsoft Corporation. @@ -42107,10 +42374,12 @@ var Web3MobileWallet = cjs$1.Web3MobileWallet; var transact = cjs$1.transact; var Buffer = require$$1$1.Buffer; var BN = bn$1.exports; +var bs58 = bs58$2; var ACCOUNT_LAYOUT = lib.struct([lib.publicKey('mint'), lib.publicKey('owner'), lib.u64('amount'), lib.u32('delegateOption'), lib.publicKey('delegate'), lib.u8('state'), lib.u32('isNativeOption'), lib.u64('isNative'), lib.u64('delegatedAmount'), lib.u32('closeAuthorityOption'), lib.publicKey('closeAuthority')]); var array$1 = lib.array; var bool$1 = lib.bool; +var deserialize$1 = lib.deserialize; var i128 = lib.i128; var i16 = lib.i16; var i32 = lib.i32; @@ -42130,4 +42399,4 @@ var u64$3 = lib.u64; var u8$1 = lib.u8; var vec = lib.vec; var vecU8 = lib.vecU8; -export { ACCOUNT_LAYOUT, AddressLookupTableProgram, BN, Buffer, Connection, Keypair, LAMPORTS_PER_SOL, PublicKey, SystemProgram, Transaction, TransactionInstruction, TransactionMessage, VersionedTransaction, Web3MobileWallet, array$1 as array, blob, bool$1 as bool, i128, i16, i32, i64, i8, map, nu64, offset, option, publicKey$1 as publicKey, rustEnum, seq, str, struct$1 as struct, tagged, transact, u128, u16$1 as u16, u32$2 as u32, u64$3 as u64, u8$1 as u8, vec, vecU8 }; +export { ACCOUNT_LAYOUT, AddressLookupTableProgram, BN, Buffer, Connection, Keypair, LAMPORTS_PER_SOL, PublicKey, SystemProgram, Transaction, TransactionInstruction, TransactionMessage, VersionedTransaction, Web3MobileWallet, array$1 as array, blob, bool$1 as bool, bs58, deserialize$1 as deserialize, i128, i16, i32, i64, i8, map, nu64, offset, option, publicKey$1 as publicKey, rustEnum, seq, str, struct$1 as struct, tagged, transact, u128, u16$1 as u16, u32$2 as u32, u64$3 as u64, u8$1 as u8, vec, vecU8 }; diff --git a/dist/umd/index.js b/dist/umd/index.js index abf3c6d..39b351f 100644 --- a/dist/umd/index.js +++ b/dist/umd/index.js @@ -8125,7 +8125,7 @@ if(_global$1.fetch == undefined) { throw('Please polyfill .fetch | See: https:// var BN$1 = bn$1.exports; - var safeBuffer = {exports: {}}; + var safeBuffer$1 = {exports: {}}; /*! safe-buffer. MIT License. Feross Aboukhadijeh */ @@ -8199,7 +8199,7 @@ if(_global$1.fetch == undefined) { throw('Please polyfill .fetch | See: https:// return buffer.SlowBuffer(size); }; - })(safeBuffer, safeBuffer.exports); + })(safeBuffer$1, safeBuffer$1.exports); // Copyright (c) 2018 base-x contributors // Copyright (c) 2014-2018 The Bitcoin Core developers (base58.cpp) @@ -8208,9 +8208,9 @@ if(_global$1.fetch == undefined) { throw('Please polyfill .fetch | See: https:// // @ts-ignore - var _Buffer = safeBuffer.exports.Buffer; + var _Buffer$1 = safeBuffer$1.exports.Buffer; - function base$1(ALPHABET) { + function base$2(ALPHABET) { if (ALPHABET.length >= 255) { throw new TypeError('Alphabet too long'); } @@ -8240,10 +8240,10 @@ if(_global$1.fetch == undefined) { throw('Please polyfill .fetch | See: https:// function encode(source) { if (Array.isArray(source) || source instanceof Uint8Array) { - source = _Buffer.from(source); + source = _Buffer$1.from(source); } - if (!_Buffer.isBuffer(source)) { + if (!_Buffer$1.isBuffer(source)) { throw new TypeError('Expected Buffer'); } @@ -8308,7 +8308,7 @@ if(_global$1.fetch == undefined) { throw('Please polyfill .fetch | See: https:// } if (source.length === 0) { - return _Buffer.alloc(0); + return _Buffer$1.alloc(0); } var psz = 0; // Skip and count leading '1's. @@ -8357,7 +8357,7 @@ if(_global$1.fetch == undefined) { throw('Please polyfill .fetch | See: https:// it4++; } - var vch = _Buffer.allocUnsafe(zeroes + (size - it4)); + var vch = _Buffer$1.allocUnsafe(zeroes + (size - it4)); vch.fill(0x00, 0, zeroes); var j = zeroes; @@ -8386,12 +8386,12 @@ if(_global$1.fetch == undefined) { throw('Please polyfill .fetch | See: https:// }; } - var src$1 = base$1; + var src$2 = base$2; - var basex$1 = src$1; - var ALPHABET$1 = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'; - var bs58$2 = basex$1(ALPHABET$1); - var bs58$3 = bs58$2; + var basex$2 = src$2; + var ALPHABET$2 = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'; + var bs58$4 = basex$2(ALPHABET$2); + var bs58$5 = bs58$4; var Chi = function Chi(a, b, c) { return a & b ^ ~a & c; @@ -8570,6 +8570,273 @@ if(_global$1.fetch == undefined) { throw('Please polyfill .fetch | See: https:// var lib$1 = {}; + var safeBuffer = {exports: {}}; + + /*! safe-buffer. MIT License. Feross Aboukhadijeh */ + + (function (module, exports) { + /* eslint-disable node/no-deprecated-api */ + var buffer = require$$1$1; + var Buffer = buffer.Buffer; // alternative to using Object.keys for old browsers + + function copyProps(src, dst) { + for (var key in src) { + dst[key] = src[key]; + } + } + + if (Buffer.from && Buffer.alloc && Buffer.allocUnsafe && Buffer.allocUnsafeSlow) { + module.exports = buffer; + } else { + // Copy properties from require('buffer') + copyProps(buffer, exports); + exports.Buffer = SafeBuffer; + } + + function SafeBuffer(arg, encodingOrOffset, length) { + return Buffer(arg, encodingOrOffset, length); + } + + SafeBuffer.prototype = Object.create(Buffer.prototype); // Copy static methods from Buffer + + copyProps(Buffer, SafeBuffer); + + SafeBuffer.from = function (arg, encodingOrOffset, length) { + if (typeof arg === 'number') { + throw new TypeError('Argument must not be a number'); + } + + return Buffer(arg, encodingOrOffset, length); + }; + + SafeBuffer.alloc = function (size, fill, encoding) { + if (typeof size !== 'number') { + throw new TypeError('Argument must be a number'); + } + + var buf = Buffer(size); + + if (fill !== undefined) { + if (typeof encoding === 'string') { + buf.fill(fill, encoding); + } else { + buf.fill(fill); + } + } else { + buf.fill(0); + } + + return buf; + }; + + SafeBuffer.allocUnsafe = function (size) { + if (typeof size !== 'number') { + throw new TypeError('Argument must be a number'); + } + + return Buffer(size); + }; + + SafeBuffer.allocUnsafeSlow = function (size) { + if (typeof size !== 'number') { + throw new TypeError('Argument must be a number'); + } + + return buffer.SlowBuffer(size); + }; + })(safeBuffer, safeBuffer.exports); + + // Copyright (c) 2018 base-x contributors + // Copyright (c) 2014-2018 The Bitcoin Core developers (base58.cpp) + // Distributed under the MIT software license, see the accompanying + // file LICENSE or http://www.opensource.org/licenses/mit-license.php. + // @ts-ignore + + + var _Buffer = safeBuffer.exports.Buffer; + + function base$1(ALPHABET) { + if (ALPHABET.length >= 255) { + throw new TypeError('Alphabet too long'); + } + + var BASE_MAP = new Uint8Array(256); + + for (var j = 0; j < BASE_MAP.length; j++) { + BASE_MAP[j] = 255; + } + + for (var i = 0; i < ALPHABET.length; i++) { + var x = ALPHABET.charAt(i); + var xc = x.charCodeAt(0); + + if (BASE_MAP[xc] !== 255) { + throw new TypeError(x + ' is ambiguous'); + } + + BASE_MAP[xc] = i; + } + + var BASE = ALPHABET.length; + var LEADER = ALPHABET.charAt(0); + var FACTOR = Math.log(BASE) / Math.log(256); // log(BASE) / log(256), rounded up + + var iFACTOR = Math.log(256) / Math.log(BASE); // log(256) / log(BASE), rounded up + + function encode(source) { + if (Array.isArray(source) || source instanceof Uint8Array) { + source = _Buffer.from(source); + } + + if (!_Buffer.isBuffer(source)) { + throw new TypeError('Expected Buffer'); + } + + if (source.length === 0) { + return ''; + } // Skip & count leading zeroes. + + + var zeroes = 0; + var length = 0; + var pbegin = 0; + var pend = source.length; + + while (pbegin !== pend && source[pbegin] === 0) { + pbegin++; + zeroes++; + } // Allocate enough space in big-endian base58 representation. + + + var size = (pend - pbegin) * iFACTOR + 1 >>> 0; + var b58 = new Uint8Array(size); // Process the bytes. + + while (pbegin !== pend) { + var carry = source[pbegin]; // Apply "b58 = b58 * 256 + ch". + + var i = 0; + + for (var it1 = size - 1; (carry !== 0 || i < length) && it1 !== -1; it1--, i++) { + carry += 256 * b58[it1] >>> 0; + b58[it1] = carry % BASE >>> 0; + carry = carry / BASE >>> 0; + } + + if (carry !== 0) { + throw new Error('Non-zero carry'); + } + + length = i; + pbegin++; + } // Skip leading zeroes in base58 result. + + + var it2 = size - length; + + while (it2 !== size && b58[it2] === 0) { + it2++; + } // Translate the result into a string. + + + var str = LEADER.repeat(zeroes); + + for (; it2 < size; ++it2) { + str += ALPHABET.charAt(b58[it2]); + } + + return str; + } + + function decodeUnsafe(source) { + if (typeof source !== 'string') { + throw new TypeError('Expected String'); + } + + if (source.length === 0) { + return _Buffer.alloc(0); + } + + var psz = 0; // Skip and count leading '1's. + + var zeroes = 0; + var length = 0; + + while (source[psz] === LEADER) { + zeroes++; + psz++; + } // Allocate enough space in big-endian base256 representation. + + + var size = (source.length - psz) * FACTOR + 1 >>> 0; // log(58) / log(256), rounded up. + + var b256 = new Uint8Array(size); // Process the characters. + + while (source[psz]) { + // Decode character + var carry = BASE_MAP[source.charCodeAt(psz)]; // Invalid character + + if (carry === 255) { + return; + } + + var i = 0; + + for (var it3 = size - 1; (carry !== 0 || i < length) && it3 !== -1; it3--, i++) { + carry += BASE * b256[it3] >>> 0; + b256[it3] = carry % 256 >>> 0; + carry = carry / 256 >>> 0; + } + + if (carry !== 0) { + throw new Error('Non-zero carry'); + } + + length = i; + psz++; + } // Skip leading zeroes in b256. + + + var it4 = size - length; + + while (it4 !== size && b256[it4] === 0) { + it4++; + } + + var vch = _Buffer.allocUnsafe(zeroes + (size - it4)); + + vch.fill(0x00, 0, zeroes); + var j = zeroes; + + while (it4 !== size) { + vch[j++] = b256[it4++]; + } + + return vch; + } + + function decode(string) { + var buffer = decodeUnsafe(string); + + if (buffer) { + return buffer; + } + + throw new Error('Non-base' + BASE + ' character'); + } + + return { + encode: encode, + decodeUnsafe: decodeUnsafe, + decode: decode + }; + } + + var src$1 = base$1; + + var basex$1 = src$1; + var ALPHABET$1 = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'; + var bs58$3 = basex$1(ALPHABET$1); + function inRange(a, min, max) { return min <= a && a <= max; } @@ -9226,7 +9493,7 @@ if(_global$1.fetch == undefined) { throw('Please polyfill .fetch | See: https:// var bn_js_1 = __importDefault(bn$1.exports); - var bs58_1 = __importDefault(bs58$2); // TODO: Make sure this polyfill not included when not required + var bs58_1 = __importDefault(bs58$3); // TODO: Make sure this polyfill not included when not required var encoding = __importStar(require$$2); @@ -18973,7 +19240,7 @@ if(_global$1.fetch == undefined) { throw('Please polyfill .fetch | See: https:// } else { if (typeof value === 'string') { // assume base 58 encoding by default - var decoded = bs58$3.decode(value); + var decoded = bs58$5.decode(value); if (decoded.length != PUBLIC_KEY_LENGTH) { throw new Error("Invalid public key input"); @@ -19017,7 +19284,7 @@ if(_global$1.fetch == undefined) { throw('Please polyfill .fetch | See: https:// }, { key: "toBase58", value: function toBase58() { - return bs58$3.encode(this.toBytes()); + return bs58$5.encode(this.toBytes()); } }, { key: "toJSON", @@ -19902,7 +20169,7 @@ if(_global$1.fetch == undefined) { throw('Please polyfill .fetch | See: https:// return { programIdIndex: ix.programIdIndex, accountKeyIndexes: ix.accounts, - data: bs58$3.decode(ix.data) + data: bs58$5.decode(ix.data) }; }); } @@ -19964,7 +20231,7 @@ if(_global$1.fetch == undefined) { throw('Please polyfill .fetch | See: https:// var instructions = this.instructions.map(function (instruction) { var accounts = instruction.accounts, programIdIndex = instruction.programIdIndex; - var data = Array.from(bs58$3.decode(instruction.data)); + var data = Array.from(bs58$5.decode(instruction.data)); var keyIndicesCount = []; encodeLength(keyIndicesCount, accounts.length); var dataCount = []; @@ -19997,7 +20264,7 @@ if(_global$1.fetch == undefined) { throw('Please polyfill .fetch | See: https:// keys: this.accountKeys.map(function (key) { return toBuffer(key.toBytes()); }), - recentBlockhash: bs58$3.decode(this.recentBlockhash) + recentBlockhash: bs58$5.decode(this.recentBlockhash) }; var signData = Buffer$1.alloc(2048); var length = signDataLayout.encode(transaction, signData); @@ -20023,7 +20290,7 @@ if(_global$1.fetch == undefined) { throw('Please polyfill .fetch | See: https:// return { programIdIndex: ix.programIdIndex, accounts: ix.accountKeyIndexes, - data: bs58$3.encode(ix.data) + data: bs58$5.encode(ix.data) }; }); return new Message({ @@ -20070,7 +20337,7 @@ if(_global$1.fetch == undefined) { throw('Please polyfill .fetch | See: https:// byteArray = byteArray.slice(_accountCount); var dataLength = decodeLength(byteArray); var dataSlice = byteArray.slice(0, dataLength); - var data = bs58$3.encode(Buffer$1.from(dataSlice)); + var data = bs58$5.encode(Buffer$1.from(dataSlice)); byteArray = byteArray.slice(dataLength); instructions.push({ programIdIndex: programIdIndex, @@ -20085,7 +20352,7 @@ if(_global$1.fetch == undefined) { throw('Please polyfill .fetch | See: https:// numReadonlySignedAccounts: numReadonlySignedAccounts, numReadonlyUnsignedAccounts: numReadonlyUnsignedAccounts }, - recentBlockhash: bs58$3.encode(Buffer$1.from(recentBlockhash)), + recentBlockhash: bs58$5.encode(Buffer$1.from(recentBlockhash)), accountKeys: accountKeys, instructions: instructions }; @@ -20281,7 +20548,7 @@ if(_global$1.fetch == undefined) { throw('Please polyfill .fetch | See: https:// staticAccountKeys: this.staticAccountKeys.map(function (key) { return key.toBytes(); }), - recentBlockhash: bs58$3.decode(this.recentBlockhash), + recentBlockhash: bs58$5.decode(this.recentBlockhash), instructionsLength: new Uint8Array(encodedInstructionsLength), serializedInstructions: serializedInstructions, addressTableLookupsLength: new Uint8Array(encodedAddressTableLookupsLength), @@ -20433,7 +20700,7 @@ if(_global$1.fetch == undefined) { throw('Please polyfill .fetch | See: https:// staticAccountKeys.push(new PublicKey(byteArray.splice(0, PUBLIC_KEY_LENGTH))); } - var recentBlockhash = bs58$3.encode(byteArray.splice(0, PUBLIC_KEY_LENGTH)); + var recentBlockhash = bs58$5.encode(byteArray.splice(0, PUBLIC_KEY_LENGTH)); var instructionCount = decodeLength(byteArray); var compiledInstructions = []; @@ -20905,7 +21172,7 @@ if(_global$1.fetch == undefined) { throw('Please polyfill .fetch | See: https:// accounts: instruction.keys.map(function (meta) { return accountKeys.indexOf(meta.pubkey.toString()); }), - data: bs58$3.encode(data) + data: bs58$5.encode(data) }; }); compiledInstructions.forEach(function (instruction) { @@ -21318,7 +21585,7 @@ if(_global$1.fetch == undefined) { throw('Please polyfill .fetch | See: https:// var _signature2 = byteArray.slice(0, SIGNATURE_LENGTH_IN_BYTES); byteArray = byteArray.slice(SIGNATURE_LENGTH_IN_BYTES); - signatures.push(bs58$3.encode(Buffer$1.from(_signature2))); + signatures.push(bs58$5.encode(Buffer$1.from(_signature2))); } return Transaction.populate(Message.from(byteArray), signatures); @@ -21340,7 +21607,7 @@ if(_global$1.fetch == undefined) { throw('Please polyfill .fetch | See: https:// signatures.forEach(function (signature, index) { var sigPubkeyPair = { - signature: signature == bs58$3.encode(DEFAULT_SIGNATURE) ? null : bs58$3.decode(signature), + signature: signature == bs58$5.encode(DEFAULT_SIGNATURE) ? null : bs58$5.decode(signature), publicKey: message.accountKeys[index] }; transaction.signatures.push(sigPubkeyPair); @@ -21359,7 +21626,7 @@ if(_global$1.fetch == undefined) { throw('Please polyfill .fetch | See: https:// transaction.instructions.push(new TransactionInstruction({ keys: keys, programId: message.accountKeys[instruction.programIdIndex], - data: bs58$3.decode(instruction.data) + data: bs58$5.decode(instruction.data) })); }); transaction._message = message; @@ -23516,7 +23783,7 @@ if(_global$1.fetch == undefined) { throw('Please polyfill .fetch | See: https:// return { programIdIndex: ix.programIdIndex, accountKeyIndexes: ix.accounts, - data: bs58$3.decode(ix.data) + data: bs58$5.decode(ix.data) }; }), addressTableLookups: response.addressTableLookups @@ -25802,7 +26069,7 @@ if(_global$1.fetch == undefined) { throw('Please polyfill .fetch | See: https:// case 8: _context31.prev = 8; - decodedSignature = bs58$3.decode(rawSignature); + decodedSignature = bs58$5.decode(rawSignature); _context31.next = 15; break; @@ -34508,14 +34775,14 @@ if(_global$1.fetch == undefined) { throw('Please polyfill .fetch | See: https:// var basex = src; var ALPHABET = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'; - var bs58$1 = basex(ALPHABET); + var bs58$2 = basex(ALPHABET); Object.defineProperty(cjs$1, '__esModule', { value: true }); var web3_js = require$$1; var mobileWalletAdapterProtocol = cjs; - var bs58 = bs58$1; + var bs58$1 = bs58$2; function _interopDefaultLegacy$1(e) { return e && _typeof$1(e) === 'object' && 'default' in e ? e : { @@ -34523,7 +34790,7 @@ if(_global$1.fetch == undefined) { throw('Please polyfill .fetch | See: https:// }; } - var bs58__default = /*#__PURE__*/_interopDefaultLegacy$1(bs58); + var bs58__default = /*#__PURE__*/_interopDefaultLegacy$1(bs58$1); /****************************************************************************** Copyright (c) Microsoft Corporation. @@ -42113,6 +42380,7 @@ if(_global$1.fetch == undefined) { throw('Please polyfill .fetch | See: https:// var transact = cjs$1.transact; var Buffer = require$$1$1.Buffer; var BN = bn$1.exports; + var bs58 = bs58$2; var ACCOUNT_LAYOUT = lib.struct([lib.publicKey('mint'), lib.publicKey('owner'), lib.u64('amount'), lib.u32('delegateOption'), lib.publicKey('delegate'), lib.u8('state'), lib.u32('isNativeOption'), lib.u64('isNative'), lib.u64('delegatedAmount'), lib.u32('closeAuthorityOption'), lib.publicKey('closeAuthority')]); exports.ACCOUNT_LAYOUT = ACCOUNT_LAYOUT; @@ -42132,6 +42400,8 @@ if(_global$1.fetch == undefined) { throw('Please polyfill .fetch | See: https:// exports.array = lib.array; exports.blob = blob; exports.bool = lib.bool; + exports.bs58 = bs58; + exports.deserialize = lib.deserialize; exports.i128 = lib.i128; exports.i16 = lib.i16; exports.i32 = lib.i32; diff --git a/package.json b/package.json index f9e688c..b7dcad7 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@depay/solana-web3.js", "moduleName": "SolanaWeb3js", - "version": "1.24.0", + "version": "1.25.0", "description": "Solana Web3.js browser pre-built usable in rollup bundles.", "main": "dist/umd/index.js", "module": "dist/esm/index.js", @@ -51,5 +51,8 @@ "secp256k1": "^4.0.2", "superstruct": "^0.14.2", "tweetnacl": "^1.0.0" + }, + "dependencies": { + "bs58": "^5.0.0" } } diff --git a/src/index.js b/src/index.js index 01f7abf..ab2019e 100644 --- a/src/index.js +++ b/src/index.js @@ -23,6 +23,7 @@ const transact = require("@solana-mobile/mobile-wallet-adapter-protocol-web3js") const Buffer = require('buffer/').Buffer const BN = require('bn.js') +const bs58 = require('bs58') import { u8, @@ -46,6 +47,7 @@ import { rustEnum, array, map, + deserialize, } from "@project-serum/borsh" const ACCOUNT_LAYOUT = struct([ @@ -103,4 +105,6 @@ export { Keypair, transact, Web3MobileWallet, + bs58, + deserialize, }