@lightprotocol/compressed-token 0.15.3 → 0.16.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,7 +3,7 @@
3
3
  var stateless_js = require('@lightprotocol/stateless.js');
4
4
  var web3_js = require('@solana/web3.js');
5
5
  var anchor = require('@coral-xyz/anchor');
6
- var require$$0 = require('buffer');
6
+ var splToken = require('@solana/spl-token');
7
7
 
8
8
  const IDL = {
9
9
  version: '1.2.0',
@@ -1162,7 +1162,8 @@ const IDL = {
1162
1162
  name: 'inputCompressedAccountsWithMerkleContext',
1163
1163
  type: {
1164
1164
  vec: {
1165
- defined: 'PackedCompressedAccountWithMerkleContext',
1165
+ defined:
1166
+ 'PackedCompressedAccountWithMerkleContext',
1166
1167
  },
1167
1168
  },
1168
1169
  },
@@ -1170,7 +1171,8 @@ const IDL = {
1170
1171
  name: 'outputCompressedAccounts',
1171
1172
  type: {
1172
1173
  vec: {
1173
- defined: 'OutputCompressedAccountWithPackedContext',
1174
+ defined:
1175
+ 'OutputCompressedAccountWithPackedContext',
1174
1176
  },
1175
1177
  },
1176
1178
  },
@@ -1226,7 +1228,8 @@ const IDL = {
1226
1228
  name: 'inputCompressedAccountsWithMerkleContext',
1227
1229
  type: {
1228
1230
  vec: {
1229
- defined: 'PackedCompressedAccountWithMerkleContext',
1231
+ defined:
1232
+ 'PackedCompressedAccountWithMerkleContext',
1230
1233
  },
1231
1234
  },
1232
1235
  },
@@ -1234,7 +1237,8 @@ const IDL = {
1234
1237
  name: 'outputCompressedAccounts',
1235
1238
  type: {
1236
1239
  vec: {
1237
- defined: 'OutputCompressedAccountWithPackedContext',
1240
+ defined:
1241
+ 'OutputCompressedAccountWithPackedContext',
1238
1242
  },
1239
1243
  },
1240
1244
  },
@@ -1479,7 +1483,8 @@ const IDL = {
1479
1483
  name: 'outputCompressedAccounts',
1480
1484
  type: {
1481
1485
  vec: {
1482
- defined: 'OutputCompressedAccountWithPackedContext',
1486
+ defined:
1487
+ 'OutputCompressedAccountWithPackedContext',
1483
1488
  },
1484
1489
  },
1485
1490
  },
@@ -1682,20 +1687,37 @@ const IDL = {
1682
1687
  * Packs Compressed Token Accounts.
1683
1688
  */
1684
1689
  function packCompressedTokenAccounts(params) {
1685
- const { inputCompressedTokenAccounts, outputStateTrees, remainingAccounts = [], rootIndices, tokenTransferOutputs, } = params;
1690
+ const {
1691
+ inputCompressedTokenAccounts,
1692
+ outputStateTrees,
1693
+ remainingAccounts = [],
1694
+ rootIndices,
1695
+ tokenTransferOutputs,
1696
+ } = params;
1686
1697
  const _remainingAccounts = remainingAccounts.slice();
1687
1698
  let delegateIndex = null;
1688
- if (inputCompressedTokenAccounts.length > 0 &&
1689
- inputCompressedTokenAccounts[0].parsed.delegate) {
1690
- delegateIndex = stateless_js.getIndexOrAdd(_remainingAccounts, inputCompressedTokenAccounts[0].parsed.delegate);
1699
+ if (
1700
+ inputCompressedTokenAccounts.length > 0 &&
1701
+ inputCompressedTokenAccounts[0].parsed.delegate
1702
+ ) {
1703
+ delegateIndex = stateless_js.getIndexOrAdd(
1704
+ _remainingAccounts,
1705
+ inputCompressedTokenAccounts[0].parsed.delegate,
1706
+ );
1691
1707
  }
1692
1708
  /// TODO: move pubkeyArray to remainingAccounts
1693
1709
  /// Currently just packs 'delegate' to pubkeyArray
1694
1710
  const packedInputTokenData = [];
1695
1711
  /// pack inputs
1696
1712
  inputCompressedTokenAccounts.forEach((account, index) => {
1697
- const merkleTreePubkeyIndex = stateless_js.getIndexOrAdd(_remainingAccounts, account.compressedAccount.merkleTree);
1698
- const nullifierQueuePubkeyIndex = stateless_js.getIndexOrAdd(_remainingAccounts, account.compressedAccount.nullifierQueue);
1713
+ const merkleTreePubkeyIndex = stateless_js.getIndexOrAdd(
1714
+ _remainingAccounts,
1715
+ account.compressedAccount.merkleTree,
1716
+ );
1717
+ const nullifierQueuePubkeyIndex = stateless_js.getIndexOrAdd(
1718
+ _remainingAccounts,
1719
+ account.compressedAccount.nullifierQueue,
1720
+ );
1699
1721
  packedInputTokenData.push({
1700
1722
  amount: account.parsed.amount,
1701
1723
  delegateIndex,
@@ -1713,15 +1735,27 @@ function packCompressedTokenAccounts(params) {
1713
1735
  });
1714
1736
  });
1715
1737
  /// pack output state trees
1716
- const paddedOutputStateMerkleTrees = stateless_js.padOutputStateMerkleTrees(outputStateTrees, tokenTransferOutputs.length, inputCompressedTokenAccounts.map(acc => acc.compressedAccount));
1738
+ const paddedOutputStateMerkleTrees = stateless_js.padOutputStateMerkleTrees(
1739
+ outputStateTrees,
1740
+ tokenTransferOutputs.length,
1741
+ inputCompressedTokenAccounts.map(acc => acc.compressedAccount),
1742
+ );
1717
1743
  const packedOutputTokenData = [];
1718
1744
  paddedOutputStateMerkleTrees.forEach((account, index) => {
1719
1745
  var _a;
1720
- const merkleTreeIndex = stateless_js.getIndexOrAdd(_remainingAccounts, account);
1746
+ const merkleTreeIndex = stateless_js.getIndexOrAdd(
1747
+ _remainingAccounts,
1748
+ account,
1749
+ );
1721
1750
  packedOutputTokenData.push({
1722
1751
  owner: tokenTransferOutputs[index].owner,
1723
1752
  amount: tokenTransferOutputs[index].amount,
1724
- lamports: ((_a = tokenTransferOutputs[index].lamports) === null || _a === void 0 ? void 0 : _a.eq(stateless_js.bn(0)))
1753
+ lamports: (
1754
+ (_a = tokenTransferOutputs[index].lamports) === null ||
1755
+ _a === void 0
1756
+ ? void 0
1757
+ : _a.eq(stateless_js.bn(0))
1758
+ )
1725
1759
  ? null
1726
1760
  : tokenTransferOutputs[index].lamports,
1727
1761
  merkleTreeIndex,
@@ -1729,7 +1763,7 @@ function packCompressedTokenAccounts(params) {
1729
1763
  });
1730
1764
  });
1731
1765
  // to meta
1732
- const remainingAccountMetas = _remainingAccounts.map((account) => ({
1766
+ const remainingAccountMetas = _remainingAccounts.map(account => ({
1733
1767
  pubkey: account,
1734
1768
  isWritable: true,
1735
1769
  isSigner: false,
@@ -1745,2854 +1779,19 @@ const POOL_SEED = Buffer.from('pool');
1745
1779
  const CPI_AUTHORITY_SEED = Buffer.from('cpi_authority');
1746
1780
  const SPL_TOKEN_MINT_RENT_EXEMPT_BALANCE = 1461600;
1747
1781
 
1748
- /** Address of the SPL Token program */
1749
- const TOKEN_PROGRAM_ID = new web3_js.PublicKey('TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA');
1750
- /** Address of the SPL Token 2022 program */
1751
- new web3_js.PublicKey('TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb');
1752
- /** Address of the SPL Associated Token Account program */
1753
- const ASSOCIATED_TOKEN_PROGRAM_ID = new web3_js.PublicKey('ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL');
1754
- /** Address of the special mint for wrapped native SOL in spl-token */
1755
- new web3_js.PublicKey('So11111111111111111111111111111111111111112');
1756
- /** Address of the special mint for wrapped native SOL in spl-token-2022 */
1757
- new web3_js.PublicKey('9pan9bMn5HatX4EJdBwg9VgCa7Uz5HL8N1m5D3NdXejP');
1758
-
1759
- var Layout$1 = {};
1760
-
1761
- /* The MIT License (MIT)
1762
- *
1763
- * Copyright 2015-2018 Peter A. Bigot
1764
- *
1765
- * Permission is hereby granted, free of charge, to any person obtaining a copy
1766
- * of this software and associated documentation files (the "Software"), to deal
1767
- * in the Software without restriction, including without limitation the rights
1768
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
1769
- * copies of the Software, and to permit persons to whom the Software is
1770
- * furnished to do so, subject to the following conditions:
1771
- *
1772
- * The above copyright notice and this permission notice shall be included in
1773
- * all copies or substantial portions of the Software.
1774
- *
1775
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1776
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1777
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
1778
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1779
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
1780
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
1781
- * THE SOFTWARE.
1782
- */
1783
- Object.defineProperty(Layout$1, "__esModule", { value: true });
1784
- Layout$1.s16 = Layout$1.s8 = Layout$1.nu64be = Layout$1.u48be = Layout$1.u40be = Layout$1.u32be = Layout$1.u24be = Layout$1.u16be = Layout$1.nu64 = Layout$1.u48 = Layout$1.u40 = u32 = Layout$1.u32 = Layout$1.u24 = Layout$1.u16 = u8 = Layout$1.u8 = Layout$1.offset = Layout$1.greedy = Layout$1.Constant = Layout$1.UTF8 = Layout$1.CString = Layout$1.Blob = Layout$1.Boolean = Layout$1.BitField = Layout$1.BitStructure = Layout$1.VariantLayout = Layout$1.Union = Layout$1.UnionLayoutDiscriminator = Layout$1.UnionDiscriminator = Layout$1.Structure = Layout$1.Sequence = Layout$1.DoubleBE = Layout$1.Double = Layout$1.FloatBE = Layout$1.Float = Layout$1.NearInt64BE = Layout$1.NearInt64 = Layout$1.NearUInt64BE = Layout$1.NearUInt64 = Layout$1.IntBE = Layout$1.Int = Layout$1.UIntBE = Layout$1.UInt = Layout$1.OffsetLayout = Layout$1.GreedyCount = Layout$1.ExternalLayout = Layout$1.bindConstructorLayout = Layout$1.nameWithProperty = Layout_2 = Layout$1.Layout = Layout$1.uint8ArrayToBuffer = Layout$1.checkUint8Array = void 0;
1785
- Layout$1.constant = Layout$1.utf8 = Layout$1.cstr = blob = Layout$1.blob = Layout$1.unionLayoutDiscriminator = Layout$1.union = Layout$1.seq = Layout$1.bits = struct = Layout$1.struct = Layout$1.f64be = Layout$1.f64 = Layout$1.f32be = Layout$1.f32 = Layout$1.ns64be = Layout$1.s48be = Layout$1.s40be = Layout$1.s32be = Layout$1.s24be = Layout$1.s16be = Layout$1.ns64 = Layout$1.s48 = Layout$1.s40 = Layout$1.s32 = Layout$1.s24 = void 0;
1786
- const buffer_1 = require$$0;
1787
- /* Check if a value is a Uint8Array.
1788
- *
1789
- * @ignore */
1790
- function checkUint8Array(b) {
1791
- if (!(b instanceof Uint8Array)) {
1792
- throw new TypeError('b must be a Uint8Array');
1793
- }
1794
- }
1795
- Layout$1.checkUint8Array = checkUint8Array;
1796
- /* Create a Buffer instance from a Uint8Array.
1797
- *
1798
- * @ignore */
1799
- function uint8ArrayToBuffer(b) {
1800
- checkUint8Array(b);
1801
- return buffer_1.Buffer.from(b.buffer, b.byteOffset, b.length);
1802
- }
1803
- Layout$1.uint8ArrayToBuffer = uint8ArrayToBuffer;
1804
- /**
1805
- * Base class for layout objects.
1806
- *
1807
- * **NOTE** This is an abstract base class; you can create instances
1808
- * if it amuses you, but they won't support the {@link
1809
- * Layout#encode|encode} or {@link Layout#decode|decode} functions.
1810
- *
1811
- * @param {Number} span - Initializer for {@link Layout#span|span}. The
1812
- * parameter must be an integer; a negative value signifies that the
1813
- * span is {@link Layout#getSpan|value-specific}.
1814
- *
1815
- * @param {string} [property] - Initializer for {@link
1816
- * Layout#property|property}.
1817
- *
1818
- * @abstract
1819
- */
1820
- class Layout {
1821
- constructor(span, property) {
1822
- if (!Number.isInteger(span)) {
1823
- throw new TypeError('span must be an integer');
1824
- }
1825
- /** The span of the layout in bytes.
1826
- *
1827
- * Positive values are generally expected.
1828
- *
1829
- * Zero will only appear in {@link Constant}s and in {@link
1830
- * Sequence}s where the {@link Sequence#count|count} is zero.
1831
- *
1832
- * A negative value indicates that the span is value-specific, and
1833
- * must be obtained using {@link Layout#getSpan|getSpan}. */
1834
- this.span = span;
1835
- /** The property name used when this layout is represented in an
1836
- * Object.
1837
- *
1838
- * Used only for layouts that {@link Layout#decode|decode} to Object
1839
- * instances. If left undefined the span of the unnamed layout will
1840
- * be treated as padding: it will not be mutated by {@link
1841
- * Layout#encode|encode} nor represented as a property in the
1842
- * decoded Object. */
1843
- this.property = property;
1844
- }
1845
- /** Function to create an Object into which decoded properties will
1846
- * be written.
1847
- *
1848
- * Used only for layouts that {@link Layout#decode|decode} to Object
1849
- * instances, which means:
1850
- * * {@link Structure}
1851
- * * {@link Union}
1852
- * * {@link VariantLayout}
1853
- * * {@link BitStructure}
1854
- *
1855
- * If left undefined the JavaScript representation of these layouts
1856
- * will be Object instances.
1857
- *
1858
- * See {@link bindConstructorLayout}.
1859
- */
1860
- makeDestinationObject() {
1861
- return {};
1862
- }
1863
- /**
1864
- * Calculate the span of a specific instance of a layout.
1865
- *
1866
- * @param {Uint8Array} b - the buffer that contains an encoded instance.
1867
- *
1868
- * @param {Number} [offset] - the offset at which the encoded instance
1869
- * starts. If absent a zero offset is inferred.
1870
- *
1871
- * @return {Number} - the number of bytes covered by the layout
1872
- * instance. If this method is not overridden in a subclass the
1873
- * definition-time constant {@link Layout#span|span} will be
1874
- * returned.
1875
- *
1876
- * @throws {RangeError} - if the length of the value cannot be
1877
- * determined.
1878
- */
1879
- getSpan(b, offset) {
1880
- if (0 > this.span) {
1881
- throw new RangeError('indeterminate span');
1882
- }
1883
- return this.span;
1884
- }
1885
- /**
1886
- * Replicate the layout using a new property.
1887
- *
1888
- * This function must be used to get a structurally-equivalent layout
1889
- * with a different name since all {@link Layout} instances are
1890
- * immutable.
1891
- *
1892
- * **NOTE** This is a shallow copy. All fields except {@link
1893
- * Layout#property|property} are strictly equal to the origin layout.
1894
- *
1895
- * @param {String} property - the value for {@link
1896
- * Layout#property|property} in the replica.
1897
- *
1898
- * @returns {Layout} - the copy with {@link Layout#property|property}
1899
- * set to `property`.
1900
- */
1901
- replicate(property) {
1902
- const rv = Object.create(this.constructor.prototype);
1903
- Object.assign(rv, this);
1904
- rv.property = property;
1905
- return rv;
1906
- }
1907
- /**
1908
- * Create an object from layout properties and an array of values.
1909
- *
1910
- * **NOTE** This function returns `undefined` if invoked on a layout
1911
- * that does not return its value as an Object. Objects are
1912
- * returned for things that are a {@link Structure}, which includes
1913
- * {@link VariantLayout|variant layouts} if they are structures, and
1914
- * excludes {@link Union}s. If you want this feature for a union
1915
- * you must use {@link Union.getVariant|getVariant} to select the
1916
- * desired layout.
1917
- *
1918
- * @param {Array} values - an array of values that correspond to the
1919
- * default order for properties. As with {@link Layout#decode|decode}
1920
- * layout elements that have no property name are skipped when
1921
- * iterating over the array values. Only the top-level properties are
1922
- * assigned; arguments are not assigned to properties of contained
1923
- * layouts. Any unused values are ignored.
1924
- *
1925
- * @return {(Object|undefined)}
1926
- */
1927
- fromArray(values) {
1928
- return undefined;
1929
- }
1930
- }
1931
- var Layout_2 = Layout$1.Layout = Layout;
1932
- /* Provide text that carries a name (such as for a function that will
1933
- * be throwing an error) annotated with the property of a given layout
1934
- * (such as one for which the value was unacceptable).
1935
- *
1936
- * @ignore */
1937
- function nameWithProperty(name, lo) {
1938
- if (lo.property) {
1939
- return name + '[' + lo.property + ']';
1940
- }
1941
- return name;
1942
- }
1943
- Layout$1.nameWithProperty = nameWithProperty;
1944
- /**
1945
- * Augment a class so that instances can be encoded/decoded using a
1946
- * given layout.
1947
- *
1948
- * Calling this function couples `Class` with `layout` in several ways:
1949
- *
1950
- * * `Class.layout_` becomes a static member property equal to `layout`;
1951
- * * `layout.boundConstructor_` becomes a static member property equal
1952
- * to `Class`;
1953
- * * The {@link Layout#makeDestinationObject|makeDestinationObject()}
1954
- * property of `layout` is set to a function that returns a `new
1955
- * Class()`;
1956
- * * `Class.decode(b, offset)` becomes a static member function that
1957
- * delegates to {@link Layout#decode|layout.decode}. The
1958
- * synthesized function may be captured and extended.
1959
- * * `Class.prototype.encode(b, offset)` provides an instance member
1960
- * function that delegates to {@link Layout#encode|layout.encode}
1961
- * with `src` set to `this`. The synthesized function may be
1962
- * captured and extended, but when the extension is invoked `this`
1963
- * must be explicitly bound to the instance.
1964
- *
1965
- * @param {class} Class - a JavaScript class with a nullary
1966
- * constructor.
1967
- *
1968
- * @param {Layout} layout - the {@link Layout} instance used to encode
1969
- * instances of `Class`.
1970
- */
1971
- // `Class` must be a constructor Function, but the assignment of a `layout_` property to it makes it difficult to type
1972
- // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
1973
- function bindConstructorLayout(Class, layout) {
1974
- if ('function' !== typeof Class) {
1975
- throw new TypeError('Class must be constructor');
1976
- }
1977
- if (Object.prototype.hasOwnProperty.call(Class, 'layout_')) {
1978
- throw new Error('Class is already bound to a layout');
1979
- }
1980
- if (!(layout && (layout instanceof Layout))) {
1981
- throw new TypeError('layout must be a Layout');
1982
- }
1983
- if (Object.prototype.hasOwnProperty.call(layout, 'boundConstructor_')) {
1984
- throw new Error('layout is already bound to a constructor');
1985
- }
1986
- Class.layout_ = layout;
1987
- layout.boundConstructor_ = Class;
1988
- layout.makeDestinationObject = (() => new Class());
1989
- Object.defineProperty(Class.prototype, 'encode', {
1990
- value(b, offset) {
1991
- return layout.encode(this, b, offset);
1992
- },
1993
- writable: true,
1994
- });
1995
- Object.defineProperty(Class, 'decode', {
1996
- value(b, offset) {
1997
- return layout.decode(b, offset);
1998
- },
1999
- writable: true,
2000
- });
2001
- }
2002
- Layout$1.bindConstructorLayout = bindConstructorLayout;
2003
- /**
2004
- * An object that behaves like a layout but does not consume space
2005
- * within its containing layout.
2006
- *
2007
- * This is primarily used to obtain metadata about a member, such as a
2008
- * {@link OffsetLayout} that can provide data about a {@link
2009
- * Layout#getSpan|value-specific span}.
2010
- *
2011
- * **NOTE** This is an abstract base class; you can create instances
2012
- * if it amuses you, but they won't support {@link
2013
- * ExternalLayout#isCount|isCount} or other {@link Layout} functions.
2014
- *
2015
- * @param {Number} span - initializer for {@link Layout#span|span}.
2016
- * The parameter can range from 1 through 6.
2017
- *
2018
- * @param {string} [property] - initializer for {@link
2019
- * Layout#property|property}.
2020
- *
2021
- * @abstract
2022
- * @augments {Layout}
2023
- */
2024
- class ExternalLayout extends Layout {
2025
- /**
2026
- * Return `true` iff the external layout decodes to an unsigned
2027
- * integer layout.
2028
- *
2029
- * In that case it can be used as the source of {@link
2030
- * Sequence#count|Sequence counts}, {@link Blob#length|Blob lengths},
2031
- * or as {@link UnionLayoutDiscriminator#layout|external union
2032
- * discriminators}.
2033
- *
2034
- * @abstract
2035
- */
2036
- isCount() {
2037
- throw new Error('ExternalLayout is abstract');
2038
- }
2039
- }
2040
- Layout$1.ExternalLayout = ExternalLayout;
2041
- /**
2042
- * An {@link ExternalLayout} that determines its {@link
2043
- * Layout#decode|value} based on offset into and length of the buffer
2044
- * on which it is invoked.
2045
- *
2046
- * *Factory*: {@link module:Layout.greedy|greedy}
2047
- *
2048
- * @param {Number} [elementSpan] - initializer for {@link
2049
- * GreedyCount#elementSpan|elementSpan}.
2050
- *
2051
- * @param {string} [property] - initializer for {@link
2052
- * Layout#property|property}.
2053
- *
2054
- * @augments {ExternalLayout}
2055
- */
2056
- class GreedyCount extends ExternalLayout {
2057
- constructor(elementSpan = 1, property) {
2058
- if ((!Number.isInteger(elementSpan)) || (0 >= elementSpan)) {
2059
- throw new TypeError('elementSpan must be a (positive) integer');
2060
- }
2061
- super(-1, property);
2062
- /** The layout for individual elements of the sequence. The value
2063
- * must be a positive integer. If not provided, the value will be
2064
- * 1. */
2065
- this.elementSpan = elementSpan;
2066
- }
2067
- /** @override */
2068
- isCount() {
2069
- return true;
2070
- }
2071
- /** @override */
2072
- decode(b, offset = 0) {
2073
- checkUint8Array(b);
2074
- const rem = b.length - offset;
2075
- return Math.floor(rem / this.elementSpan);
2076
- }
2077
- /** @override */
2078
- encode(src, b, offset) {
2079
- return 0;
2080
- }
2081
- }
2082
- Layout$1.GreedyCount = GreedyCount;
2083
- /**
2084
- * An {@link ExternalLayout} that supports accessing a {@link Layout}
2085
- * at a fixed offset from the start of another Layout. The offset may
2086
- * be before, within, or after the base layout.
2087
- *
2088
- * *Factory*: {@link module:Layout.offset|offset}
2089
- *
2090
- * @param {Layout} layout - initializer for {@link
2091
- * OffsetLayout#layout|layout}, modulo `property`.
2092
- *
2093
- * @param {Number} [offset] - Initializes {@link
2094
- * OffsetLayout#offset|offset}. Defaults to zero.
2095
- *
2096
- * @param {string} [property] - Optional new property name for a
2097
- * {@link Layout#replicate| replica} of `layout` to be used as {@link
2098
- * OffsetLayout#layout|layout}. If not provided the `layout` is used
2099
- * unchanged.
2100
- *
2101
- * @augments {Layout}
2102
- */
2103
- class OffsetLayout extends ExternalLayout {
2104
- constructor(layout, offset = 0, property) {
2105
- if (!(layout instanceof Layout)) {
2106
- throw new TypeError('layout must be a Layout');
2107
- }
2108
- if (!Number.isInteger(offset)) {
2109
- throw new TypeError('offset must be integer or undefined');
2110
- }
2111
- super(layout.span, property || layout.property);
2112
- /** The subordinated layout. */
2113
- this.layout = layout;
2114
- /** The location of {@link OffsetLayout#layout} relative to the
2115
- * start of another layout.
2116
- *
2117
- * The value may be positive or negative, but an error will thrown
2118
- * if at the point of use it goes outside the span of the Uint8Array
2119
- * being accessed. */
2120
- this.offset = offset;
2121
- }
2122
- /** @override */
2123
- isCount() {
2124
- return ((this.layout instanceof UInt)
2125
- || (this.layout instanceof UIntBE));
2126
- }
2127
- /** @override */
2128
- decode(b, offset = 0) {
2129
- return this.layout.decode(b, offset + this.offset);
2130
- }
2131
- /** @override */
2132
- encode(src, b, offset = 0) {
2133
- return this.layout.encode(src, b, offset + this.offset);
2134
- }
2135
- }
2136
- Layout$1.OffsetLayout = OffsetLayout;
2137
- /**
2138
- * Represent an unsigned integer in little-endian format.
2139
- *
2140
- * *Factory*: {@link module:Layout.u8|u8}, {@link
2141
- * module:Layout.u16|u16}, {@link module:Layout.u24|u24}, {@link
2142
- * module:Layout.u32|u32}, {@link module:Layout.u40|u40}, {@link
2143
- * module:Layout.u48|u48}
2144
- *
2145
- * @param {Number} span - initializer for {@link Layout#span|span}.
2146
- * The parameter can range from 1 through 6.
2147
- *
2148
- * @param {string} [property] - initializer for {@link
2149
- * Layout#property|property}.
2150
- *
2151
- * @augments {Layout}
2152
- */
2153
- class UInt extends Layout {
2154
- constructor(span, property) {
2155
- super(span, property);
2156
- if (6 < this.span) {
2157
- throw new RangeError('span must not exceed 6 bytes');
2158
- }
2159
- }
2160
- /** @override */
2161
- decode(b, offset = 0) {
2162
- return uint8ArrayToBuffer(b).readUIntLE(offset, this.span);
2163
- }
2164
- /** @override */
2165
- encode(src, b, offset = 0) {
2166
- uint8ArrayToBuffer(b).writeUIntLE(src, offset, this.span);
2167
- return this.span;
2168
- }
2169
- }
2170
- Layout$1.UInt = UInt;
2171
- /**
2172
- * Represent an unsigned integer in big-endian format.
2173
- *
2174
- * *Factory*: {@link module:Layout.u8be|u8be}, {@link
2175
- * module:Layout.u16be|u16be}, {@link module:Layout.u24be|u24be},
2176
- * {@link module:Layout.u32be|u32be}, {@link
2177
- * module:Layout.u40be|u40be}, {@link module:Layout.u48be|u48be}
2178
- *
2179
- * @param {Number} span - initializer for {@link Layout#span|span}.
2180
- * The parameter can range from 1 through 6.
2181
- *
2182
- * @param {string} [property] - initializer for {@link
2183
- * Layout#property|property}.
2184
- *
2185
- * @augments {Layout}
2186
- */
2187
- class UIntBE extends Layout {
2188
- constructor(span, property) {
2189
- super(span, property);
2190
- if (6 < this.span) {
2191
- throw new RangeError('span must not exceed 6 bytes');
2192
- }
2193
- }
2194
- /** @override */
2195
- decode(b, offset = 0) {
2196
- return uint8ArrayToBuffer(b).readUIntBE(offset, this.span);
2197
- }
2198
- /** @override */
2199
- encode(src, b, offset = 0) {
2200
- uint8ArrayToBuffer(b).writeUIntBE(src, offset, this.span);
2201
- return this.span;
2202
- }
2203
- }
2204
- Layout$1.UIntBE = UIntBE;
2205
- /**
2206
- * Represent a signed integer in little-endian format.
2207
- *
2208
- * *Factory*: {@link module:Layout.s8|s8}, {@link
2209
- * module:Layout.s16|s16}, {@link module:Layout.s24|s24}, {@link
2210
- * module:Layout.s32|s32}, {@link module:Layout.s40|s40}, {@link
2211
- * module:Layout.s48|s48}
2212
- *
2213
- * @param {Number} span - initializer for {@link Layout#span|span}.
2214
- * The parameter can range from 1 through 6.
2215
- *
2216
- * @param {string} [property] - initializer for {@link
2217
- * Layout#property|property}.
2218
- *
2219
- * @augments {Layout}
2220
- */
2221
- class Int extends Layout {
2222
- constructor(span, property) {
2223
- super(span, property);
2224
- if (6 < this.span) {
2225
- throw new RangeError('span must not exceed 6 bytes');
2226
- }
2227
- }
2228
- /** @override */
2229
- decode(b, offset = 0) {
2230
- return uint8ArrayToBuffer(b).readIntLE(offset, this.span);
2231
- }
2232
- /** @override */
2233
- encode(src, b, offset = 0) {
2234
- uint8ArrayToBuffer(b).writeIntLE(src, offset, this.span);
2235
- return this.span;
2236
- }
2237
- }
2238
- Layout$1.Int = Int;
2239
- /**
2240
- * Represent a signed integer in big-endian format.
2241
- *
2242
- * *Factory*: {@link module:Layout.s8be|s8be}, {@link
2243
- * module:Layout.s16be|s16be}, {@link module:Layout.s24be|s24be},
2244
- * {@link module:Layout.s32be|s32be}, {@link
2245
- * module:Layout.s40be|s40be}, {@link module:Layout.s48be|s48be}
2246
- *
2247
- * @param {Number} span - initializer for {@link Layout#span|span}.
2248
- * The parameter can range from 1 through 6.
2249
- *
2250
- * @param {string} [property] - initializer for {@link
2251
- * Layout#property|property}.
2252
- *
2253
- * @augments {Layout}
2254
- */
2255
- class IntBE extends Layout {
2256
- constructor(span, property) {
2257
- super(span, property);
2258
- if (6 < this.span) {
2259
- throw new RangeError('span must not exceed 6 bytes');
2260
- }
2261
- }
2262
- /** @override */
2263
- decode(b, offset = 0) {
2264
- return uint8ArrayToBuffer(b).readIntBE(offset, this.span);
2265
- }
2266
- /** @override */
2267
- encode(src, b, offset = 0) {
2268
- uint8ArrayToBuffer(b).writeIntBE(src, offset, this.span);
2269
- return this.span;
2270
- }
2271
- }
2272
- Layout$1.IntBE = IntBE;
2273
- const V2E32 = Math.pow(2, 32);
2274
- /* True modulus high and low 32-bit words, where low word is always
2275
- * non-negative. */
2276
- function divmodInt64(src) {
2277
- const hi32 = Math.floor(src / V2E32);
2278
- const lo32 = src - (hi32 * V2E32);
2279
- return { hi32, lo32 };
2280
- }
2281
- /* Reconstruct Number from quotient and non-negative remainder */
2282
- function roundedInt64(hi32, lo32) {
2283
- return hi32 * V2E32 + lo32;
2284
- }
2285
- /**
2286
- * Represent an unsigned 64-bit integer in little-endian format when
2287
- * encoded and as a near integral JavaScript Number when decoded.
2288
- *
2289
- * *Factory*: {@link module:Layout.nu64|nu64}
2290
- *
2291
- * **NOTE** Values with magnitude greater than 2^52 may not decode to
2292
- * the exact value of the encoded representation.
2293
- *
2294
- * @augments {Layout}
2295
- */
2296
- class NearUInt64 extends Layout {
2297
- constructor(property) {
2298
- super(8, property);
2299
- }
2300
- /** @override */
2301
- decode(b, offset = 0) {
2302
- const buffer = uint8ArrayToBuffer(b);
2303
- const lo32 = buffer.readUInt32LE(offset);
2304
- const hi32 = buffer.readUInt32LE(offset + 4);
2305
- return roundedInt64(hi32, lo32);
2306
- }
2307
- /** @override */
2308
- encode(src, b, offset = 0) {
2309
- const split = divmodInt64(src);
2310
- const buffer = uint8ArrayToBuffer(b);
2311
- buffer.writeUInt32LE(split.lo32, offset);
2312
- buffer.writeUInt32LE(split.hi32, offset + 4);
2313
- return 8;
2314
- }
2315
- }
2316
- Layout$1.NearUInt64 = NearUInt64;
2317
- /**
2318
- * Represent an unsigned 64-bit integer in big-endian format when
2319
- * encoded and as a near integral JavaScript Number when decoded.
2320
- *
2321
- * *Factory*: {@link module:Layout.nu64be|nu64be}
2322
- *
2323
- * **NOTE** Values with magnitude greater than 2^52 may not decode to
2324
- * the exact value of the encoded representation.
2325
- *
2326
- * @augments {Layout}
2327
- */
2328
- class NearUInt64BE extends Layout {
2329
- constructor(property) {
2330
- super(8, property);
2331
- }
2332
- /** @override */
2333
- decode(b, offset = 0) {
2334
- const buffer = uint8ArrayToBuffer(b);
2335
- const hi32 = buffer.readUInt32BE(offset);
2336
- const lo32 = buffer.readUInt32BE(offset + 4);
2337
- return roundedInt64(hi32, lo32);
2338
- }
2339
- /** @override */
2340
- encode(src, b, offset = 0) {
2341
- const split = divmodInt64(src);
2342
- const buffer = uint8ArrayToBuffer(b);
2343
- buffer.writeUInt32BE(split.hi32, offset);
2344
- buffer.writeUInt32BE(split.lo32, offset + 4);
2345
- return 8;
2346
- }
2347
- }
2348
- Layout$1.NearUInt64BE = NearUInt64BE;
2349
- /**
2350
- * Represent a signed 64-bit integer in little-endian format when
2351
- * encoded and as a near integral JavaScript Number when decoded.
2352
- *
2353
- * *Factory*: {@link module:Layout.ns64|ns64}
2354
- *
2355
- * **NOTE** Values with magnitude greater than 2^52 may not decode to
2356
- * the exact value of the encoded representation.
2357
- *
2358
- * @augments {Layout}
2359
- */
2360
- class NearInt64 extends Layout {
2361
- constructor(property) {
2362
- super(8, property);
2363
- }
2364
- /** @override */
2365
- decode(b, offset = 0) {
2366
- const buffer = uint8ArrayToBuffer(b);
2367
- const lo32 = buffer.readUInt32LE(offset);
2368
- const hi32 = buffer.readInt32LE(offset + 4);
2369
- return roundedInt64(hi32, lo32);
2370
- }
2371
- /** @override */
2372
- encode(src, b, offset = 0) {
2373
- const split = divmodInt64(src);
2374
- const buffer = uint8ArrayToBuffer(b);
2375
- buffer.writeUInt32LE(split.lo32, offset);
2376
- buffer.writeInt32LE(split.hi32, offset + 4);
2377
- return 8;
2378
- }
2379
- }
2380
- Layout$1.NearInt64 = NearInt64;
2381
- /**
2382
- * Represent a signed 64-bit integer in big-endian format when
2383
- * encoded and as a near integral JavaScript Number when decoded.
2384
- *
2385
- * *Factory*: {@link module:Layout.ns64be|ns64be}
2386
- *
2387
- * **NOTE** Values with magnitude greater than 2^52 may not decode to
2388
- * the exact value of the encoded representation.
2389
- *
2390
- * @augments {Layout}
2391
- */
2392
- class NearInt64BE extends Layout {
2393
- constructor(property) {
2394
- super(8, property);
2395
- }
2396
- /** @override */
2397
- decode(b, offset = 0) {
2398
- const buffer = uint8ArrayToBuffer(b);
2399
- const hi32 = buffer.readInt32BE(offset);
2400
- const lo32 = buffer.readUInt32BE(offset + 4);
2401
- return roundedInt64(hi32, lo32);
2402
- }
2403
- /** @override */
2404
- encode(src, b, offset = 0) {
2405
- const split = divmodInt64(src);
2406
- const buffer = uint8ArrayToBuffer(b);
2407
- buffer.writeInt32BE(split.hi32, offset);
2408
- buffer.writeUInt32BE(split.lo32, offset + 4);
2409
- return 8;
2410
- }
2411
- }
2412
- Layout$1.NearInt64BE = NearInt64BE;
2413
- /**
2414
- * Represent a 32-bit floating point number in little-endian format.
2415
- *
2416
- * *Factory*: {@link module:Layout.f32|f32}
2417
- *
2418
- * @param {string} [property] - initializer for {@link
2419
- * Layout#property|property}.
2420
- *
2421
- * @augments {Layout}
2422
- */
2423
- class Float extends Layout {
2424
- constructor(property) {
2425
- super(4, property);
2426
- }
2427
- /** @override */
2428
- decode(b, offset = 0) {
2429
- return uint8ArrayToBuffer(b).readFloatLE(offset);
2430
- }
2431
- /** @override */
2432
- encode(src, b, offset = 0) {
2433
- uint8ArrayToBuffer(b).writeFloatLE(src, offset);
2434
- return 4;
2435
- }
2436
- }
2437
- Layout$1.Float = Float;
2438
- /**
2439
- * Represent a 32-bit floating point number in big-endian format.
2440
- *
2441
- * *Factory*: {@link module:Layout.f32be|f32be}
2442
- *
2443
- * @param {string} [property] - initializer for {@link
2444
- * Layout#property|property}.
2445
- *
2446
- * @augments {Layout}
2447
- */
2448
- class FloatBE extends Layout {
2449
- constructor(property) {
2450
- super(4, property);
2451
- }
2452
- /** @override */
2453
- decode(b, offset = 0) {
2454
- return uint8ArrayToBuffer(b).readFloatBE(offset);
2455
- }
2456
- /** @override */
2457
- encode(src, b, offset = 0) {
2458
- uint8ArrayToBuffer(b).writeFloatBE(src, offset);
2459
- return 4;
2460
- }
2461
- }
2462
- Layout$1.FloatBE = FloatBE;
2463
- /**
2464
- * Represent a 64-bit floating point number in little-endian format.
2465
- *
2466
- * *Factory*: {@link module:Layout.f64|f64}
2467
- *
2468
- * @param {string} [property] - initializer for {@link
2469
- * Layout#property|property}.
2470
- *
2471
- * @augments {Layout}
2472
- */
2473
- class Double extends Layout {
2474
- constructor(property) {
2475
- super(8, property);
2476
- }
2477
- /** @override */
2478
- decode(b, offset = 0) {
2479
- return uint8ArrayToBuffer(b).readDoubleLE(offset);
2480
- }
2481
- /** @override */
2482
- encode(src, b, offset = 0) {
2483
- uint8ArrayToBuffer(b).writeDoubleLE(src, offset);
2484
- return 8;
2485
- }
2486
- }
2487
- Layout$1.Double = Double;
2488
- /**
2489
- * Represent a 64-bit floating point number in big-endian format.
2490
- *
2491
- * *Factory*: {@link module:Layout.f64be|f64be}
2492
- *
2493
- * @param {string} [property] - initializer for {@link
2494
- * Layout#property|property}.
2495
- *
2496
- * @augments {Layout}
2497
- */
2498
- class DoubleBE extends Layout {
2499
- constructor(property) {
2500
- super(8, property);
2501
- }
2502
- /** @override */
2503
- decode(b, offset = 0) {
2504
- return uint8ArrayToBuffer(b).readDoubleBE(offset);
2505
- }
2506
- /** @override */
2507
- encode(src, b, offset = 0) {
2508
- uint8ArrayToBuffer(b).writeDoubleBE(src, offset);
2509
- return 8;
2510
- }
2511
- }
2512
- Layout$1.DoubleBE = DoubleBE;
2513
- /**
2514
- * Represent a contiguous sequence of a specific layout as an Array.
2515
- *
2516
- * *Factory*: {@link module:Layout.seq|seq}
2517
- *
2518
- * @param {Layout} elementLayout - initializer for {@link
2519
- * Sequence#elementLayout|elementLayout}.
2520
- *
2521
- * @param {(Number|ExternalLayout)} count - initializer for {@link
2522
- * Sequence#count|count}. The parameter must be either a positive
2523
- * integer or an instance of {@link ExternalLayout}.
2524
- *
2525
- * @param {string} [property] - initializer for {@link
2526
- * Layout#property|property}.
2527
- *
2528
- * @augments {Layout}
2529
- */
2530
- class Sequence extends Layout {
2531
- constructor(elementLayout, count, property) {
2532
- if (!(elementLayout instanceof Layout)) {
2533
- throw new TypeError('elementLayout must be a Layout');
2534
- }
2535
- if (!(((count instanceof ExternalLayout) && count.isCount())
2536
- || (Number.isInteger(count) && (0 <= count)))) {
2537
- throw new TypeError('count must be non-negative integer '
2538
- + 'or an unsigned integer ExternalLayout');
2539
- }
2540
- let span = -1;
2541
- if ((!(count instanceof ExternalLayout))
2542
- && (0 < elementLayout.span)) {
2543
- span = count * elementLayout.span;
2544
- }
2545
- super(span, property);
2546
- /** The layout for individual elements of the sequence. */
2547
- this.elementLayout = elementLayout;
2548
- /** The number of elements in the sequence.
2549
- *
2550
- * This will be either a non-negative integer or an instance of
2551
- * {@link ExternalLayout} for which {@link
2552
- * ExternalLayout#isCount|isCount()} is `true`. */
2553
- this.count = count;
2554
- }
2555
- /** @override */
2556
- getSpan(b, offset = 0) {
2557
- if (0 <= this.span) {
2558
- return this.span;
2559
- }
2560
- let span = 0;
2561
- let count = this.count;
2562
- if (count instanceof ExternalLayout) {
2563
- count = count.decode(b, offset);
2564
- }
2565
- if (0 < this.elementLayout.span) {
2566
- span = count * this.elementLayout.span;
2567
- }
2568
- else {
2569
- let idx = 0;
2570
- while (idx < count) {
2571
- span += this.elementLayout.getSpan(b, offset + span);
2572
- ++idx;
2573
- }
2574
- }
2575
- return span;
2576
- }
2577
- /** @override */
2578
- decode(b, offset = 0) {
2579
- const rv = [];
2580
- let i = 0;
2581
- let count = this.count;
2582
- if (count instanceof ExternalLayout) {
2583
- count = count.decode(b, offset);
2584
- }
2585
- while (i < count) {
2586
- rv.push(this.elementLayout.decode(b, offset));
2587
- offset += this.elementLayout.getSpan(b, offset);
2588
- i += 1;
2589
- }
2590
- return rv;
2591
- }
2592
- /** Implement {@link Layout#encode|encode} for {@link Sequence}.
2593
- *
2594
- * **NOTE** If `src` is shorter than {@link Sequence#count|count} then
2595
- * the unused space in the buffer is left unchanged. If `src` is
2596
- * longer than {@link Sequence#count|count} the unneeded elements are
2597
- * ignored.
2598
- *
2599
- * **NOTE** If {@link Layout#count|count} is an instance of {@link
2600
- * ExternalLayout} then the length of `src` will be encoded as the
2601
- * count after `src` is encoded. */
2602
- encode(src, b, offset = 0) {
2603
- const elo = this.elementLayout;
2604
- const span = src.reduce((span, v) => {
2605
- return span + elo.encode(v, b, offset + span);
2606
- }, 0);
2607
- if (this.count instanceof ExternalLayout) {
2608
- this.count.encode(src.length, b, offset);
2609
- }
2610
- return span;
2611
- }
2612
- }
2613
- Layout$1.Sequence = Sequence;
2614
- /**
2615
- * Represent a contiguous sequence of arbitrary layout elements as an
2616
- * Object.
2617
- *
2618
- * *Factory*: {@link module:Layout.struct|struct}
2619
- *
2620
- * **NOTE** The {@link Layout#span|span} of the structure is variable
2621
- * if any layout in {@link Structure#fields|fields} has a variable
2622
- * span. When {@link Layout#encode|encoding} we must have a value for
2623
- * all variable-length fields, or we wouldn't be able to figure out
2624
- * how much space to use for storage. We can only identify the value
2625
- * for a field when it has a {@link Layout#property|property}. As
2626
- * such, although a structure may contain both unnamed fields and
2627
- * variable-length fields, it cannot contain an unnamed
2628
- * variable-length field.
2629
- *
2630
- * @param {Layout[]} fields - initializer for {@link
2631
- * Structure#fields|fields}. An error is raised if this contains a
2632
- * variable-length field for which a {@link Layout#property|property}
2633
- * is not defined.
2634
- *
2635
- * @param {string} [property] - initializer for {@link
2636
- * Layout#property|property}.
2637
- *
2638
- * @param {Boolean} [decodePrefixes] - initializer for {@link
2639
- * Structure#decodePrefixes|property}.
2640
- *
2641
- * @throws {Error} - if `fields` contains an unnamed variable-length
2642
- * layout.
2643
- *
2644
- * @augments {Layout}
2645
- */
2646
- class Structure extends Layout {
2647
- constructor(fields, property, decodePrefixes) {
2648
- if (!(Array.isArray(fields)
2649
- && fields.reduce((acc, v) => acc && (v instanceof Layout), true))) {
2650
- throw new TypeError('fields must be array of Layout instances');
2651
- }
2652
- if (('boolean' === typeof property)
2653
- && (undefined === decodePrefixes)) {
2654
- decodePrefixes = property;
2655
- property = undefined;
2656
- }
2657
- /* Verify absence of unnamed variable-length fields. */
2658
- for (const fd of fields) {
2659
- if ((0 > fd.span)
2660
- && (undefined === fd.property)) {
2661
- throw new Error('fields cannot contain unnamed variable-length layout');
2662
- }
2663
- }
2664
- let span = -1;
2665
- try {
2666
- span = fields.reduce((span, fd) => span + fd.getSpan(), 0);
2667
- }
2668
- catch (e) {
2669
- // ignore error
2670
- }
2671
- super(span, property);
2672
- /** The sequence of {@link Layout} values that comprise the
2673
- * structure.
2674
- *
2675
- * The individual elements need not be the same type, and may be
2676
- * either scalar or aggregate layouts. If a member layout leaves
2677
- * its {@link Layout#property|property} undefined the
2678
- * corresponding region of the buffer associated with the element
2679
- * will not be mutated.
2680
- *
2681
- * @type {Layout[]} */
2682
- this.fields = fields;
2683
- /** Control behavior of {@link Layout#decode|decode()} given short
2684
- * buffers.
2685
- *
2686
- * In some situations a structure many be extended with additional
2687
- * fields over time, with older installations providing only a
2688
- * prefix of the full structure. If this property is `true`
2689
- * decoding will accept those buffers and leave subsequent fields
2690
- * undefined, as long as the buffer ends at a field boundary.
2691
- * Defaults to `false`. */
2692
- this.decodePrefixes = !!decodePrefixes;
2693
- }
2694
- /** @override */
2695
- getSpan(b, offset = 0) {
2696
- if (0 <= this.span) {
2697
- return this.span;
2698
- }
2699
- let span = 0;
2700
- try {
2701
- span = this.fields.reduce((span, fd) => {
2702
- const fsp = fd.getSpan(b, offset);
2703
- offset += fsp;
2704
- return span + fsp;
2705
- }, 0);
2706
- }
2707
- catch (e) {
2708
- throw new RangeError('indeterminate span');
2709
- }
2710
- return span;
2711
- }
2712
- /** @override */
2713
- decode(b, offset = 0) {
2714
- checkUint8Array(b);
2715
- const dest = this.makeDestinationObject();
2716
- for (const fd of this.fields) {
2717
- if (undefined !== fd.property) {
2718
- dest[fd.property] = fd.decode(b, offset);
2719
- }
2720
- offset += fd.getSpan(b, offset);
2721
- if (this.decodePrefixes
2722
- && (b.length === offset)) {
2723
- break;
2724
- }
2725
- }
2726
- return dest;
2727
- }
2728
- /** Implement {@link Layout#encode|encode} for {@link Structure}.
2729
- *
2730
- * If `src` is missing a property for a member with a defined {@link
2731
- * Layout#property|property} the corresponding region of the buffer is
2732
- * left unmodified. */
2733
- encode(src, b, offset = 0) {
2734
- const firstOffset = offset;
2735
- let lastOffset = 0;
2736
- let lastWrote = 0;
2737
- for (const fd of this.fields) {
2738
- let span = fd.span;
2739
- lastWrote = (0 < span) ? span : 0;
2740
- if (undefined !== fd.property) {
2741
- const fv = src[fd.property];
2742
- if (undefined !== fv) {
2743
- lastWrote = fd.encode(fv, b, offset);
2744
- if (0 > span) {
2745
- /* Read the as-encoded span, which is not necessarily the
2746
- * same as what we wrote. */
2747
- span = fd.getSpan(b, offset);
2748
- }
2749
- }
2750
- }
2751
- lastOffset = offset;
2752
- offset += span;
2753
- }
2754
- /* Use (lastOffset + lastWrote) instead of offset because the last
2755
- * item may have had a dynamic length and we don't want to include
2756
- * the padding between it and the end of the space reserved for
2757
- * it. */
2758
- return (lastOffset + lastWrote) - firstOffset;
2759
- }
2760
- /** @override */
2761
- fromArray(values) {
2762
- const dest = this.makeDestinationObject();
2763
- for (const fd of this.fields) {
2764
- if ((undefined !== fd.property)
2765
- && (0 < values.length)) {
2766
- dest[fd.property] = values.shift();
2767
- }
2768
- }
2769
- return dest;
2770
- }
2771
- /**
2772
- * Get access to the layout of a given property.
2773
- *
2774
- * @param {String} property - the structure member of interest.
2775
- *
2776
- * @return {Layout} - the layout associated with `property`, or
2777
- * undefined if there is no such property.
2778
- */
2779
- layoutFor(property) {
2780
- if ('string' !== typeof property) {
2781
- throw new TypeError('property must be string');
2782
- }
2783
- for (const fd of this.fields) {
2784
- if (fd.property === property) {
2785
- return fd;
2786
- }
2787
- }
2788
- return undefined;
2789
- }
2790
- /**
2791
- * Get the offset of a structure member.
2792
- *
2793
- * @param {String} property - the structure member of interest.
2794
- *
2795
- * @return {Number} - the offset in bytes to the start of `property`
2796
- * within the structure, or undefined if `property` is not a field
2797
- * within the structure. If the property is a member but follows a
2798
- * variable-length structure member a negative number will be
2799
- * returned.
2800
- */
2801
- offsetOf(property) {
2802
- if ('string' !== typeof property) {
2803
- throw new TypeError('property must be string');
2804
- }
2805
- let offset = 0;
2806
- for (const fd of this.fields) {
2807
- if (fd.property === property) {
2808
- return offset;
2809
- }
2810
- if (0 > fd.span) {
2811
- offset = -1;
2812
- }
2813
- else if (0 <= offset) {
2814
- offset += fd.span;
2815
- }
2816
- }
2817
- return undefined;
2818
- }
2819
- }
2820
- Layout$1.Structure = Structure;
2821
- /**
2822
- * An object that can provide a {@link
2823
- * Union#discriminator|discriminator} API for {@link Union}.
2824
- *
2825
- * **NOTE** This is an abstract base class; you can create instances
2826
- * if it amuses you, but they won't support the {@link
2827
- * UnionDiscriminator#encode|encode} or {@link
2828
- * UnionDiscriminator#decode|decode} functions.
2829
- *
2830
- * @param {string} [property] - Default for {@link
2831
- * UnionDiscriminator#property|property}.
2832
- *
2833
- * @abstract
2834
- */
2835
- class UnionDiscriminator {
2836
- constructor(property) {
2837
- /** The {@link Layout#property|property} to be used when the
2838
- * discriminator is referenced in isolation (generally when {@link
2839
- * Union#decode|Union decode} cannot delegate to a specific
2840
- * variant). */
2841
- this.property = property;
2842
- }
2843
- /** Analog to {@link Layout#decode|Layout decode} for union discriminators.
2844
- *
2845
- * The implementation of this method need not reference the buffer if
2846
- * variant information is available through other means. */
2847
- decode(b, offset) {
2848
- throw new Error('UnionDiscriminator is abstract');
2849
- }
2850
- /** Analog to {@link Layout#decode|Layout encode} for union discriminators.
2851
- *
2852
- * The implementation of this method need not store the value if
2853
- * variant information is maintained through other means. */
2854
- encode(src, b, offset) {
2855
- throw new Error('UnionDiscriminator is abstract');
2856
- }
2857
- }
2858
- Layout$1.UnionDiscriminator = UnionDiscriminator;
2859
- /**
2860
- * An object that can provide a {@link
2861
- * UnionDiscriminator|discriminator API} for {@link Union} using an
2862
- * unsigned integral {@link Layout} instance located either inside or
2863
- * outside the union.
2864
- *
2865
- * @param {ExternalLayout} layout - initializes {@link
2866
- * UnionLayoutDiscriminator#layout|layout}. Must satisfy {@link
2867
- * ExternalLayout#isCount|isCount()}.
2868
- *
2869
- * @param {string} [property] - Default for {@link
2870
- * UnionDiscriminator#property|property}, superseding the property
2871
- * from `layout`, but defaulting to `variant` if neither `property`
2872
- * nor layout provide a property name.
2873
- *
2874
- * @augments {UnionDiscriminator}
2875
- */
2876
- class UnionLayoutDiscriminator extends UnionDiscriminator {
2877
- constructor(layout, property) {
2878
- if (!((layout instanceof ExternalLayout)
2879
- && layout.isCount())) {
2880
- throw new TypeError('layout must be an unsigned integer ExternalLayout');
2881
- }
2882
- super(property || layout.property || 'variant');
2883
- /** The {@link ExternalLayout} used to access the discriminator
2884
- * value. */
2885
- this.layout = layout;
2886
- }
2887
- /** Delegate decoding to {@link UnionLayoutDiscriminator#layout|layout}. */
2888
- decode(b, offset) {
2889
- return this.layout.decode(b, offset);
2890
- }
2891
- /** Delegate encoding to {@link UnionLayoutDiscriminator#layout|layout}. */
2892
- encode(src, b, offset) {
2893
- return this.layout.encode(src, b, offset);
2894
- }
2895
- }
2896
- Layout$1.UnionLayoutDiscriminator = UnionLayoutDiscriminator;
2897
- /**
2898
- * Represent any number of span-compatible layouts.
2899
- *
2900
- * *Factory*: {@link module:Layout.union|union}
2901
- *
2902
- * If the union has a {@link Union#defaultLayout|default layout} that
2903
- * layout must have a non-negative {@link Layout#span|span}. The span
2904
- * of a fixed-span union includes its {@link
2905
- * Union#discriminator|discriminator} if the variant is a {@link
2906
- * Union#usesPrefixDiscriminator|prefix of the union}, plus the span
2907
- * of its {@link Union#defaultLayout|default layout}.
2908
- *
2909
- * If the union does not have a default layout then the encoded span
2910
- * of the union depends on the encoded span of its variant (which may
2911
- * be fixed or variable).
2912
- *
2913
- * {@link VariantLayout#layout|Variant layout}s are added through
2914
- * {@link Union#addVariant|addVariant}. If the union has a default
2915
- * layout, the span of the {@link VariantLayout#layout|layout
2916
- * contained by the variant} must not exceed the span of the {@link
2917
- * Union#defaultLayout|default layout} (minus the span of a {@link
2918
- * Union#usesPrefixDiscriminator|prefix disriminator}, if used). The
2919
- * span of the variant will equal the span of the union itself.
2920
- *
2921
- * The variant for a buffer can only be identified from the {@link
2922
- * Union#discriminator|discriminator} {@link
2923
- * UnionDiscriminator#property|property} (in the case of the {@link
2924
- * Union#defaultLayout|default layout}), or by using {@link
2925
- * Union#getVariant|getVariant} and examining the resulting {@link
2926
- * VariantLayout} instance.
2927
- *
2928
- * A variant compatible with a JavaScript object can be identified
2929
- * using {@link Union#getSourceVariant|getSourceVariant}.
2930
- *
2931
- * @param {(UnionDiscriminator|ExternalLayout|Layout)} discr - How to
2932
- * identify the layout used to interpret the union contents. The
2933
- * parameter must be an instance of {@link UnionDiscriminator}, an
2934
- * {@link ExternalLayout} that satisfies {@link
2935
- * ExternalLayout#isCount|isCount()}, or {@link UInt} (or {@link
2936
- * UIntBE}). When a non-external layout element is passed the layout
2937
- * appears at the start of the union. In all cases the (synthesized)
2938
- * {@link UnionDiscriminator} instance is recorded as {@link
2939
- * Union#discriminator|discriminator}.
2940
- *
2941
- * @param {(Layout|null)} defaultLayout - initializer for {@link
2942
- * Union#defaultLayout|defaultLayout}. If absent defaults to `null`.
2943
- * If `null` there is no default layout: the union has data-dependent
2944
- * length and attempts to decode or encode unrecognized variants will
2945
- * throw an exception. A {@link Layout} instance must have a
2946
- * non-negative {@link Layout#span|span}, and if it lacks a {@link
2947
- * Layout#property|property} the {@link
2948
- * Union#defaultLayout|defaultLayout} will be a {@link
2949
- * Layout#replicate|replica} with property `content`.
2950
- *
2951
- * @param {string} [property] - initializer for {@link
2952
- * Layout#property|property}.
2953
- *
2954
- * @augments {Layout}
2955
- */
2956
- class Union extends Layout {
2957
- constructor(discr, defaultLayout, property) {
2958
- let discriminator;
2959
- if ((discr instanceof UInt)
2960
- || (discr instanceof UIntBE)) {
2961
- discriminator = new UnionLayoutDiscriminator(new OffsetLayout(discr));
2962
- }
2963
- else if ((discr instanceof ExternalLayout)
2964
- && discr.isCount()) {
2965
- discriminator = new UnionLayoutDiscriminator(discr);
2966
- }
2967
- else if (!(discr instanceof UnionDiscriminator)) {
2968
- throw new TypeError('discr must be a UnionDiscriminator '
2969
- + 'or an unsigned integer layout');
2970
- }
2971
- else {
2972
- discriminator = discr;
2973
- }
2974
- if (undefined === defaultLayout) {
2975
- defaultLayout = null;
2976
- }
2977
- if (!((null === defaultLayout)
2978
- || (defaultLayout instanceof Layout))) {
2979
- throw new TypeError('defaultLayout must be null or a Layout');
2980
- }
2981
- if (null !== defaultLayout) {
2982
- if (0 > defaultLayout.span) {
2983
- throw new Error('defaultLayout must have constant span');
2984
- }
2985
- if (undefined === defaultLayout.property) {
2986
- defaultLayout = defaultLayout.replicate('content');
2987
- }
2988
- }
2989
- /* The union span can be estimated only if there's a default
2990
- * layout. The union spans its default layout, plus any prefix
2991
- * variant layout. By construction both layouts, if present, have
2992
- * non-negative span. */
2993
- let span = -1;
2994
- if (defaultLayout) {
2995
- span = defaultLayout.span;
2996
- if ((0 <= span) && ((discr instanceof UInt)
2997
- || (discr instanceof UIntBE))) {
2998
- span += discriminator.layout.span;
2999
- }
3000
- }
3001
- super(span, property);
3002
- /** The interface for the discriminator value in isolation.
3003
- *
3004
- * This a {@link UnionDiscriminator} either passed to the
3005
- * constructor or synthesized from the `discr` constructor
3006
- * argument. {@link
3007
- * Union#usesPrefixDiscriminator|usesPrefixDiscriminator} will be
3008
- * `true` iff the `discr` parameter was a non-offset {@link
3009
- * Layout} instance. */
3010
- this.discriminator = discriminator;
3011
- /** `true` if the {@link Union#discriminator|discriminator} is the
3012
- * first field in the union.
3013
- *
3014
- * If `false` the discriminator is obtained from somewhere
3015
- * else. */
3016
- this.usesPrefixDiscriminator = (discr instanceof UInt)
3017
- || (discr instanceof UIntBE);
3018
- /** The layout for non-discriminator content when the value of the
3019
- * discriminator is not recognized.
3020
- *
3021
- * This is the value passed to the constructor. It is
3022
- * structurally equivalent to the second component of {@link
3023
- * Union#layout|layout} but may have a different property
3024
- * name. */
3025
- this.defaultLayout = defaultLayout;
3026
- /** A registry of allowed variants.
3027
- *
3028
- * The keys are unsigned integers which should be compatible with
3029
- * {@link Union.discriminator|discriminator}. The property value
3030
- * is the corresponding {@link VariantLayout} instances assigned
3031
- * to this union by {@link Union#addVariant|addVariant}.
3032
- *
3033
- * **NOTE** The registry remains mutable so that variants can be
3034
- * {@link Union#addVariant|added} at any time. Users should not
3035
- * manipulate the content of this property. */
3036
- this.registry = {};
3037
- /* Private variable used when invoking getSourceVariant */
3038
- let boundGetSourceVariant = this.defaultGetSourceVariant.bind(this);
3039
- /** Function to infer the variant selected by a source object.
3040
- *
3041
- * Defaults to {@link
3042
- * Union#defaultGetSourceVariant|defaultGetSourceVariant} but may
3043
- * be overridden using {@link
3044
- * Union#configGetSourceVariant|configGetSourceVariant}.
3045
- *
3046
- * @param {Object} src - as with {@link
3047
- * Union#defaultGetSourceVariant|defaultGetSourceVariant}.
3048
- *
3049
- * @returns {(undefined|VariantLayout)} The default variant
3050
- * (`undefined`) or first registered variant that uses a property
3051
- * available in `src`. */
3052
- this.getSourceVariant = function (src) {
3053
- return boundGetSourceVariant(src);
3054
- };
3055
- /** Function to override the implementation of {@link
3056
- * Union#getSourceVariant|getSourceVariant}.
3057
- *
3058
- * Use this if the desired variant cannot be identified using the
3059
- * algorithm of {@link
3060
- * Union#defaultGetSourceVariant|defaultGetSourceVariant}.
3061
- *
3062
- * **NOTE** The provided function will be invoked bound to this
3063
- * Union instance, providing local access to {@link
3064
- * Union#registry|registry}.
3065
- *
3066
- * @param {Function} gsv - a function that follows the API of
3067
- * {@link Union#defaultGetSourceVariant|defaultGetSourceVariant}. */
3068
- this.configGetSourceVariant = function (gsv) {
3069
- boundGetSourceVariant = gsv.bind(this);
3070
- };
3071
- }
3072
- /** @override */
3073
- getSpan(b, offset = 0) {
3074
- if (0 <= this.span) {
3075
- return this.span;
3076
- }
3077
- /* Default layouts always have non-negative span, so we don't have
3078
- * one and we have to recognize the variant which will in turn
3079
- * determine the span. */
3080
- const vlo = this.getVariant(b, offset);
3081
- if (!vlo) {
3082
- throw new Error('unable to determine span for unrecognized variant');
3083
- }
3084
- return vlo.getSpan(b, offset);
3085
- }
3086
- /**
3087
- * Method to infer a registered Union variant compatible with `src`.
3088
- *
3089
- * The first satisfied rule in the following sequence defines the
3090
- * return value:
3091
- * * If `src` has properties matching the Union discriminator and
3092
- * the default layout, `undefined` is returned regardless of the
3093
- * value of the discriminator property (this ensures the default
3094
- * layout will be used);
3095
- * * If `src` has a property matching the Union discriminator, the
3096
- * value of the discriminator identifies a registered variant, and
3097
- * either (a) the variant has no layout, or (b) `src` has the
3098
- * variant's property, then the variant is returned (because the
3099
- * source satisfies the constraints of the variant it identifies);
3100
- * * If `src` does not have a property matching the Union
3101
- * discriminator, but does have a property matching a registered
3102
- * variant, then the variant is returned (because the source
3103
- * matches a variant without an explicit conflict);
3104
- * * An error is thrown (because we either can't identify a variant,
3105
- * or we were explicitly told the variant but can't satisfy it).
3106
- *
3107
- * @param {Object} src - an object presumed to be compatible with
3108
- * the content of the Union.
3109
- *
3110
- * @return {(undefined|VariantLayout)} - as described above.
3111
- *
3112
- * @throws {Error} - if `src` cannot be associated with a default or
3113
- * registered variant.
3114
- */
3115
- defaultGetSourceVariant(src) {
3116
- if (Object.prototype.hasOwnProperty.call(src, this.discriminator.property)) {
3117
- if (this.defaultLayout && this.defaultLayout.property
3118
- && Object.prototype.hasOwnProperty.call(src, this.defaultLayout.property)) {
3119
- return undefined;
3120
- }
3121
- const vlo = this.registry[src[this.discriminator.property]];
3122
- if (vlo
3123
- && ((!vlo.layout)
3124
- || (vlo.property && Object.prototype.hasOwnProperty.call(src, vlo.property)))) {
3125
- return vlo;
3126
- }
3127
- }
3128
- else {
3129
- for (const tag in this.registry) {
3130
- const vlo = this.registry[tag];
3131
- if (vlo.property && Object.prototype.hasOwnProperty.call(src, vlo.property)) {
3132
- return vlo;
3133
- }
3134
- }
3135
- }
3136
- throw new Error('unable to infer src variant');
3137
- }
3138
- /** Implement {@link Layout#decode|decode} for {@link Union}.
3139
- *
3140
- * If the variant is {@link Union#addVariant|registered} the return
3141
- * value is an instance of that variant, with no explicit
3142
- * discriminator. Otherwise the {@link Union#defaultLayout|default
3143
- * layout} is used to decode the content. */
3144
- decode(b, offset = 0) {
3145
- let dest;
3146
- const dlo = this.discriminator;
3147
- const discr = dlo.decode(b, offset);
3148
- const clo = this.registry[discr];
3149
- if (undefined === clo) {
3150
- const defaultLayout = this.defaultLayout;
3151
- let contentOffset = 0;
3152
- if (this.usesPrefixDiscriminator) {
3153
- contentOffset = dlo.layout.span;
3154
- }
3155
- dest = this.makeDestinationObject();
3156
- dest[dlo.property] = discr;
3157
- // defaultLayout.property can be undefined, but this is allowed by buffer-layout
3158
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
3159
- dest[defaultLayout.property] = defaultLayout.decode(b, offset + contentOffset);
3160
- }
3161
- else {
3162
- dest = clo.decode(b, offset);
3163
- }
3164
- return dest;
3165
- }
3166
- /** Implement {@link Layout#encode|encode} for {@link Union}.
3167
- *
3168
- * This API assumes the `src` object is consistent with the union's
3169
- * {@link Union#defaultLayout|default layout}. To encode variants
3170
- * use the appropriate variant-specific {@link VariantLayout#encode}
3171
- * method. */
3172
- encode(src, b, offset = 0) {
3173
- const vlo = this.getSourceVariant(src);
3174
- if (undefined === vlo) {
3175
- const dlo = this.discriminator;
3176
- // this.defaultLayout is not undefined when vlo is undefined
3177
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
3178
- const clo = this.defaultLayout;
3179
- let contentOffset = 0;
3180
- if (this.usesPrefixDiscriminator) {
3181
- contentOffset = dlo.layout.span;
3182
- }
3183
- dlo.encode(src[dlo.property], b, offset);
3184
- // clo.property is not undefined when vlo is undefined
3185
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
3186
- return contentOffset + clo.encode(src[clo.property], b, offset + contentOffset);
3187
- }
3188
- return vlo.encode(src, b, offset);
3189
- }
3190
- /** Register a new variant structure within a union. The newly
3191
- * created variant is returned.
3192
- *
3193
- * @param {Number} variant - initializer for {@link
3194
- * VariantLayout#variant|variant}.
3195
- *
3196
- * @param {Layout} layout - initializer for {@link
3197
- * VariantLayout#layout|layout}.
3198
- *
3199
- * @param {String} property - initializer for {@link
3200
- * Layout#property|property}.
3201
- *
3202
- * @return {VariantLayout} */
3203
- addVariant(variant, layout, property) {
3204
- const rv = new VariantLayout(this, variant, layout, property);
3205
- this.registry[variant] = rv;
3206
- return rv;
3207
- }
3208
- /**
3209
- * Get the layout associated with a registered variant.
3210
- *
3211
- * If `vb` does not produce a registered variant the function returns
3212
- * `undefined`.
3213
- *
3214
- * @param {(Number|Uint8Array)} vb - either the variant number, or a
3215
- * buffer from which the discriminator is to be read.
3216
- *
3217
- * @param {Number} offset - offset into `vb` for the start of the
3218
- * union. Used only when `vb` is an instance of {Uint8Array}.
3219
- *
3220
- * @return {({VariantLayout}|undefined)}
3221
- */
3222
- getVariant(vb, offset = 0) {
3223
- let variant;
3224
- if (vb instanceof Uint8Array) {
3225
- variant = this.discriminator.decode(vb, offset);
3226
- }
3227
- else {
3228
- variant = vb;
3229
- }
3230
- return this.registry[variant];
3231
- }
3232
- }
3233
- Layout$1.Union = Union;
3234
- /**
3235
- * Represent a specific variant within a containing union.
3236
- *
3237
- * **NOTE** The {@link Layout#span|span} of the variant may include
3238
- * the span of the {@link Union#discriminator|discriminator} used to
3239
- * identify it, but values read and written using the variant strictly
3240
- * conform to the content of {@link VariantLayout#layout|layout}.
3241
- *
3242
- * **NOTE** User code should not invoke this constructor directly. Use
3243
- * the union {@link Union#addVariant|addVariant} helper method.
3244
- *
3245
- * @param {Union} union - initializer for {@link
3246
- * VariantLayout#union|union}.
3247
- *
3248
- * @param {Number} variant - initializer for {@link
3249
- * VariantLayout#variant|variant}.
3250
- *
3251
- * @param {Layout} [layout] - initializer for {@link
3252
- * VariantLayout#layout|layout}. If absent the variant carries no
3253
- * data.
3254
- *
3255
- * @param {String} [property] - initializer for {@link
3256
- * Layout#property|property}. Unlike many other layouts, variant
3257
- * layouts normally include a property name so they can be identified
3258
- * within their containing {@link Union}. The property identifier may
3259
- * be absent only if `layout` is is absent.
3260
- *
3261
- * @augments {Layout}
3262
- */
3263
- class VariantLayout extends Layout {
3264
- constructor(union, variant, layout, property) {
3265
- if (!(union instanceof Union)) {
3266
- throw new TypeError('union must be a Union');
3267
- }
3268
- if ((!Number.isInteger(variant)) || (0 > variant)) {
3269
- throw new TypeError('variant must be a (non-negative) integer');
3270
- }
3271
- if (('string' === typeof layout)
3272
- && (undefined === property)) {
3273
- property = layout;
3274
- layout = null;
3275
- }
3276
- if (layout) {
3277
- if (!(layout instanceof Layout)) {
3278
- throw new TypeError('layout must be a Layout');
3279
- }
3280
- if ((null !== union.defaultLayout)
3281
- && (0 <= layout.span)
3282
- && (layout.span > union.defaultLayout.span)) {
3283
- throw new Error('variant span exceeds span of containing union');
3284
- }
3285
- if ('string' !== typeof property) {
3286
- throw new TypeError('variant must have a String property');
3287
- }
3288
- }
3289
- let span = union.span;
3290
- if (0 > union.span) {
3291
- span = layout ? layout.span : 0;
3292
- if ((0 <= span) && union.usesPrefixDiscriminator) {
3293
- span += union.discriminator.layout.span;
3294
- }
3295
- }
3296
- super(span, property);
3297
- /** The {@link Union} to which this variant belongs. */
3298
- this.union = union;
3299
- /** The unsigned integral value identifying this variant within
3300
- * the {@link Union#discriminator|discriminator} of the containing
3301
- * union. */
3302
- this.variant = variant;
3303
- /** The {@link Layout} to be used when reading/writing the
3304
- * non-discriminator part of the {@link
3305
- * VariantLayout#union|union}. If `null` the variant carries no
3306
- * data. */
3307
- this.layout = layout || null;
3308
- }
3309
- /** @override */
3310
- getSpan(b, offset = 0) {
3311
- if (0 <= this.span) {
3312
- /* Will be equal to the containing union span if that is not
3313
- * variable. */
3314
- return this.span;
3315
- }
3316
- let contentOffset = 0;
3317
- if (this.union.usesPrefixDiscriminator) {
3318
- contentOffset = this.union.discriminator.layout.span;
3319
- }
3320
- /* Span is defined solely by the variant (and prefix discriminator) */
3321
- let span = 0;
3322
- if (this.layout) {
3323
- span = this.layout.getSpan(b, offset + contentOffset);
3324
- }
3325
- return contentOffset + span;
3326
- }
3327
- /** @override */
3328
- decode(b, offset = 0) {
3329
- const dest = this.makeDestinationObject();
3330
- if (this !== this.union.getVariant(b, offset)) {
3331
- throw new Error('variant mismatch');
3332
- }
3333
- let contentOffset = 0;
3334
- if (this.union.usesPrefixDiscriminator) {
3335
- contentOffset = this.union.discriminator.layout.span;
3336
- }
3337
- if (this.layout) {
3338
- dest[this.property] = this.layout.decode(b, offset + contentOffset);
3339
- }
3340
- else if (this.property) {
3341
- dest[this.property] = true;
3342
- }
3343
- else if (this.union.usesPrefixDiscriminator) {
3344
- dest[this.union.discriminator.property] = this.variant;
3345
- }
3346
- return dest;
3347
- }
3348
- /** @override */
3349
- encode(src, b, offset = 0) {
3350
- let contentOffset = 0;
3351
- if (this.union.usesPrefixDiscriminator) {
3352
- contentOffset = this.union.discriminator.layout.span;
3353
- }
3354
- if (this.layout
3355
- && (!Object.prototype.hasOwnProperty.call(src, this.property))) {
3356
- throw new TypeError('variant lacks property ' + this.property);
3357
- }
3358
- this.union.discriminator.encode(this.variant, b, offset);
3359
- let span = contentOffset;
3360
- if (this.layout) {
3361
- this.layout.encode(src[this.property], b, offset + contentOffset);
3362
- span += this.layout.getSpan(b, offset + contentOffset);
3363
- if ((0 <= this.union.span)
3364
- && (span > this.union.span)) {
3365
- throw new Error('encoded variant overruns containing union');
3366
- }
3367
- }
3368
- return span;
3369
- }
3370
- /** Delegate {@link Layout#fromArray|fromArray} to {@link
3371
- * VariantLayout#layout|layout}. */
3372
- fromArray(values) {
3373
- if (this.layout) {
3374
- return this.layout.fromArray(values);
3375
- }
3376
- return undefined;
3377
- }
3378
- }
3379
- Layout$1.VariantLayout = VariantLayout;
3380
- /** JavaScript chose to define bitwise operations as operating on
3381
- * signed 32-bit values in 2's complement form, meaning any integer
3382
- * with bit 31 set is going to look negative. For right shifts that's
3383
- * not a problem, because `>>>` is a logical shift, but for every
3384
- * other bitwise operator we have to compensate for possible negative
3385
- * results. */
3386
- function fixBitwiseResult(v) {
3387
- if (0 > v) {
3388
- v += 0x100000000;
3389
- }
3390
- return v;
3391
- }
3392
- /**
3393
- * Contain a sequence of bit fields as an unsigned integer.
3394
- *
3395
- * *Factory*: {@link module:Layout.bits|bits}
3396
- *
3397
- * This is a container element; within it there are {@link BitField}
3398
- * instances that provide the extracted properties. The container
3399
- * simply defines the aggregate representation and its bit ordering.
3400
- * The representation is an object containing properties with numeric
3401
- * or {@link Boolean} values.
3402
- *
3403
- * {@link BitField}s are added with the {@link
3404
- * BitStructure#addField|addField} and {@link
3405
- * BitStructure#addBoolean|addBoolean} methods.
3406
-
3407
- * @param {Layout} word - initializer for {@link
3408
- * BitStructure#word|word}. The parameter must be an instance of
3409
- * {@link UInt} (or {@link UIntBE}) that is no more than 4 bytes wide.
3410
- *
3411
- * @param {bool} [msb] - `true` if the bit numbering starts at the
3412
- * most significant bit of the containing word; `false` (default) if
3413
- * it starts at the least significant bit of the containing word. If
3414
- * the parameter at this position is a string and `property` is
3415
- * `undefined` the value of this argument will instead be used as the
3416
- * value of `property`.
3417
- *
3418
- * @param {string} [property] - initializer for {@link
3419
- * Layout#property|property}.
3420
- *
3421
- * @augments {Layout}
3422
- */
3423
- class BitStructure extends Layout {
3424
- constructor(word, msb, property) {
3425
- if (!((word instanceof UInt)
3426
- || (word instanceof UIntBE))) {
3427
- throw new TypeError('word must be a UInt or UIntBE layout');
3428
- }
3429
- if (('string' === typeof msb)
3430
- && (undefined === property)) {
3431
- property = msb;
3432
- msb = false;
3433
- }
3434
- if (4 < word.span) {
3435
- throw new RangeError('word cannot exceed 32 bits');
3436
- }
3437
- super(word.span, property);
3438
- /** The layout used for the packed value. {@link BitField}
3439
- * instances are packed sequentially depending on {@link
3440
- * BitStructure#msb|msb}. */
3441
- this.word = word;
3442
- /** Whether the bit sequences are packed starting at the most
3443
- * significant bit growing down (`true`), or the least significant
3444
- * bit growing up (`false`).
3445
- *
3446
- * **NOTE** Regardless of this value, the least significant bit of
3447
- * any {@link BitField} value is the least significant bit of the
3448
- * corresponding section of the packed value. */
3449
- this.msb = !!msb;
3450
- /** The sequence of {@link BitField} layouts that comprise the
3451
- * packed structure.
3452
- *
3453
- * **NOTE** The array remains mutable to allow fields to be {@link
3454
- * BitStructure#addField|added} after construction. Users should
3455
- * not manipulate the content of this property.*/
3456
- this.fields = [];
3457
- /* Storage for the value. Capture a variable instead of using an
3458
- * instance property because we don't want anything to change the
3459
- * value without going through the mutator. */
3460
- let value = 0;
3461
- this._packedSetValue = function (v) {
3462
- value = fixBitwiseResult(v);
3463
- return this;
3464
- };
3465
- this._packedGetValue = function () {
3466
- return value;
3467
- };
3468
- }
3469
- /** @override */
3470
- decode(b, offset = 0) {
3471
- const dest = this.makeDestinationObject();
3472
- const value = this.word.decode(b, offset);
3473
- this._packedSetValue(value);
3474
- for (const fd of this.fields) {
3475
- if (undefined !== fd.property) {
3476
- dest[fd.property] = fd.decode(b);
3477
- }
3478
- }
3479
- return dest;
3480
- }
3481
- /** Implement {@link Layout#encode|encode} for {@link BitStructure}.
3482
- *
3483
- * If `src` is missing a property for a member with a defined {@link
3484
- * Layout#property|property} the corresponding region of the packed
3485
- * value is left unmodified. Unused bits are also left unmodified. */
3486
- encode(src, b, offset = 0) {
3487
- const value = this.word.decode(b, offset);
3488
- this._packedSetValue(value);
3489
- for (const fd of this.fields) {
3490
- if (undefined !== fd.property) {
3491
- const fv = src[fd.property];
3492
- if (undefined !== fv) {
3493
- fd.encode(fv);
3494
- }
3495
- }
3496
- }
3497
- return this.word.encode(this._packedGetValue(), b, offset);
3498
- }
3499
- /** Register a new bitfield with a containing bit structure. The
3500
- * resulting bitfield is returned.
3501
- *
3502
- * @param {Number} bits - initializer for {@link BitField#bits|bits}.
3503
- *
3504
- * @param {string} property - initializer for {@link
3505
- * Layout#property|property}.
3506
- *
3507
- * @return {BitField} */
3508
- addField(bits, property) {
3509
- const bf = new BitField(this, bits, property);
3510
- this.fields.push(bf);
3511
- return bf;
3512
- }
3513
- /** As with {@link BitStructure#addField|addField} for single-bit
3514
- * fields with `boolean` value representation.
3515
- *
3516
- * @param {string} property - initializer for {@link
3517
- * Layout#property|property}.
3518
- *
3519
- * @return {Boolean} */
3520
- // `Boolean` conflicts with the native primitive type
3521
- // eslint-disable-next-line @typescript-eslint/ban-types
3522
- addBoolean(property) {
3523
- // This is my Boolean, not the Javascript one.
3524
- const bf = new Boolean(this, property);
3525
- this.fields.push(bf);
3526
- return bf;
3527
- }
3528
- /**
3529
- * Get access to the bit field for a given property.
3530
- *
3531
- * @param {String} property - the bit field of interest.
3532
- *
3533
- * @return {BitField} - the field associated with `property`, or
3534
- * undefined if there is no such property.
3535
- */
3536
- fieldFor(property) {
3537
- if ('string' !== typeof property) {
3538
- throw new TypeError('property must be string');
3539
- }
3540
- for (const fd of this.fields) {
3541
- if (fd.property === property) {
3542
- return fd;
3543
- }
3544
- }
3545
- return undefined;
3546
- }
3547
- }
3548
- Layout$1.BitStructure = BitStructure;
3549
- /**
3550
- * Represent a sequence of bits within a {@link BitStructure}.
3551
- *
3552
- * All bit field values are represented as unsigned integers.
3553
- *
3554
- * **NOTE** User code should not invoke this constructor directly.
3555
- * Use the container {@link BitStructure#addField|addField} helper
3556
- * method.
3557
- *
3558
- * **NOTE** BitField instances are not instances of {@link Layout}
3559
- * since {@link Layout#span|span} measures 8-bit units.
3560
- *
3561
- * @param {BitStructure} container - initializer for {@link
3562
- * BitField#container|container}.
3563
- *
3564
- * @param {Number} bits - initializer for {@link BitField#bits|bits}.
3565
- *
3566
- * @param {string} [property] - initializer for {@link
3567
- * Layout#property|property}.
3568
- */
3569
- class BitField {
3570
- constructor(container, bits, property) {
3571
- if (!(container instanceof BitStructure)) {
3572
- throw new TypeError('container must be a BitStructure');
3573
- }
3574
- if ((!Number.isInteger(bits)) || (0 >= bits)) {
3575
- throw new TypeError('bits must be positive integer');
3576
- }
3577
- const totalBits = 8 * container.span;
3578
- const usedBits = container.fields.reduce((sum, fd) => sum + fd.bits, 0);
3579
- if ((bits + usedBits) > totalBits) {
3580
- throw new Error('bits too long for span remainder ('
3581
- + (totalBits - usedBits) + ' of '
3582
- + totalBits + ' remain)');
3583
- }
3584
- /** The {@link BitStructure} instance to which this bit field
3585
- * belongs. */
3586
- this.container = container;
3587
- /** The span of this value in bits. */
3588
- this.bits = bits;
3589
- /** A mask of {@link BitField#bits|bits} bits isolating value bits
3590
- * that fit within the field.
3591
- *
3592
- * That is, it masks a value that has not yet been shifted into
3593
- * position within its containing packed integer. */
3594
- this.valueMask = (1 << bits) - 1;
3595
- if (32 === bits) { // shifted value out of range
3596
- this.valueMask = 0xFFFFFFFF;
3597
- }
3598
- /** The offset of the value within the containing packed unsigned
3599
- * integer. The least significant bit of the packed value is at
3600
- * offset zero, regardless of bit ordering used. */
3601
- this.start = usedBits;
3602
- if (this.container.msb) {
3603
- this.start = totalBits - usedBits - bits;
3604
- }
3605
- /** A mask of {@link BitField#bits|bits} isolating the field value
3606
- * within the containing packed unsigned integer. */
3607
- this.wordMask = fixBitwiseResult(this.valueMask << this.start);
3608
- /** The property name used when this bitfield is represented in an
3609
- * Object.
3610
- *
3611
- * Intended to be functionally equivalent to {@link
3612
- * Layout#property}.
3613
- *
3614
- * If left undefined the corresponding span of bits will be
3615
- * treated as padding: it will not be mutated by {@link
3616
- * Layout#encode|encode} nor represented as a property in the
3617
- * decoded Object. */
3618
- this.property = property;
3619
- }
3620
- /** Store a value into the corresponding subsequence of the containing
3621
- * bit field. */
3622
- decode(b, offset) {
3623
- const word = this.container._packedGetValue();
3624
- const wordValue = fixBitwiseResult(word & this.wordMask);
3625
- const value = wordValue >>> this.start;
3626
- return value;
3627
- }
3628
- /** Store a value into the corresponding subsequence of the containing
3629
- * bit field.
3630
- *
3631
- * **NOTE** This is not a specialization of {@link
3632
- * Layout#encode|Layout.encode} and there is no return value. */
3633
- encode(value) {
3634
- if ('number' !== typeof value
3635
- || !Number.isInteger(value)
3636
- || (value !== fixBitwiseResult(value & this.valueMask))) {
3637
- throw new TypeError(nameWithProperty('BitField.encode', this)
3638
- + ' value must be integer not exceeding ' + this.valueMask);
3639
- }
3640
- const word = this.container._packedGetValue();
3641
- const wordValue = fixBitwiseResult(value << this.start);
3642
- this.container._packedSetValue(fixBitwiseResult(word & ~this.wordMask)
3643
- | wordValue);
3644
- }
3645
- }
3646
- Layout$1.BitField = BitField;
3647
- /**
3648
- * Represent a single bit within a {@link BitStructure} as a
3649
- * JavaScript boolean.
3650
- *
3651
- * **NOTE** User code should not invoke this constructor directly.
3652
- * Use the container {@link BitStructure#addBoolean|addBoolean} helper
3653
- * method.
3654
- *
3655
- * @param {BitStructure} container - initializer for {@link
3656
- * BitField#container|container}.
3657
- *
3658
- * @param {string} [property] - initializer for {@link
3659
- * Layout#property|property}.
3660
- *
3661
- * @augments {BitField}
3662
- */
3663
- /* eslint-disable no-extend-native */
3664
- class Boolean extends BitField {
3665
- constructor(container, property) {
3666
- super(container, 1, property);
3667
- }
3668
- /** Override {@link BitField#decode|decode} for {@link Boolean|Boolean}.
3669
- *
3670
- * @returns {boolean} */
3671
- decode(b, offset) {
3672
- return !!super.decode(b, offset);
3673
- }
3674
- /** @override */
3675
- encode(value) {
3676
- if ('boolean' === typeof value) {
3677
- // BitField requires integer values
3678
- value = +value;
3679
- }
3680
- super.encode(value);
3681
- }
3682
- }
3683
- Layout$1.Boolean = Boolean;
3684
- /* eslint-enable no-extend-native */
3685
- /**
3686
- * Contain a fixed-length block of arbitrary data, represented as a
3687
- * Uint8Array.
3688
- *
3689
- * *Factory*: {@link module:Layout.blob|blob}
3690
- *
3691
- * @param {(Number|ExternalLayout)} length - initializes {@link
3692
- * Blob#length|length}.
3693
- *
3694
- * @param {String} [property] - initializer for {@link
3695
- * Layout#property|property}.
3696
- *
3697
- * @augments {Layout}
3698
- */
3699
- class Blob extends Layout {
3700
- constructor(length, property) {
3701
- if (!(((length instanceof ExternalLayout) && length.isCount())
3702
- || (Number.isInteger(length) && (0 <= length)))) {
3703
- throw new TypeError('length must be positive integer '
3704
- + 'or an unsigned integer ExternalLayout');
3705
- }
3706
- let span = -1;
3707
- if (!(length instanceof ExternalLayout)) {
3708
- span = length;
3709
- }
3710
- super(span, property);
3711
- /** The number of bytes in the blob.
3712
- *
3713
- * This may be a non-negative integer, or an instance of {@link
3714
- * ExternalLayout} that satisfies {@link
3715
- * ExternalLayout#isCount|isCount()}. */
3716
- this.length = length;
3717
- }
3718
- /** @override */
3719
- getSpan(b, offset) {
3720
- let span = this.span;
3721
- if (0 > span) {
3722
- span = this.length.decode(b, offset);
3723
- }
3724
- return span;
3725
- }
3726
- /** @override */
3727
- decode(b, offset = 0) {
3728
- let span = this.span;
3729
- if (0 > span) {
3730
- span = this.length.decode(b, offset);
3731
- }
3732
- return uint8ArrayToBuffer(b).slice(offset, offset + span);
3733
- }
3734
- /** Implement {@link Layout#encode|encode} for {@link Blob}.
3735
- *
3736
- * **NOTE** If {@link Layout#count|count} is an instance of {@link
3737
- * ExternalLayout} then the length of `src` will be encoded as the
3738
- * count after `src` is encoded. */
3739
- encode(src, b, offset) {
3740
- let span = this.length;
3741
- if (this.length instanceof ExternalLayout) {
3742
- span = src.length;
3743
- }
3744
- if (!(src instanceof Uint8Array && span === src.length)) {
3745
- throw new TypeError(nameWithProperty('Blob.encode', this)
3746
- + ' requires (length ' + span + ') Uint8Array as src');
3747
- }
3748
- if ((offset + span) > b.length) {
3749
- throw new RangeError('encoding overruns Uint8Array');
3750
- }
3751
- const srcBuffer = uint8ArrayToBuffer(src);
3752
- uint8ArrayToBuffer(b).write(srcBuffer.toString('hex'), offset, span, 'hex');
3753
- if (this.length instanceof ExternalLayout) {
3754
- this.length.encode(span, b, offset);
3755
- }
3756
- return span;
3757
- }
3758
- }
3759
- Layout$1.Blob = Blob;
3760
- /**
3761
- * Contain a `NUL`-terminated UTF8 string.
3762
- *
3763
- * *Factory*: {@link module:Layout.cstr|cstr}
3764
- *
3765
- * **NOTE** Any UTF8 string that incorporates a zero-valued byte will
3766
- * not be correctly decoded by this layout.
3767
- *
3768
- * @param {String} [property] - initializer for {@link
3769
- * Layout#property|property}.
3770
- *
3771
- * @augments {Layout}
3772
- */
3773
- class CString extends Layout {
3774
- constructor(property) {
3775
- super(-1, property);
3776
- }
3777
- /** @override */
3778
- getSpan(b, offset = 0) {
3779
- checkUint8Array(b);
3780
- let idx = offset;
3781
- while ((idx < b.length) && (0 !== b[idx])) {
3782
- idx += 1;
3783
- }
3784
- return 1 + idx - offset;
3785
- }
3786
- /** @override */
3787
- decode(b, offset = 0) {
3788
- const span = this.getSpan(b, offset);
3789
- return uint8ArrayToBuffer(b).slice(offset, offset + span - 1).toString('utf-8');
3790
- }
3791
- /** @override */
3792
- encode(src, b, offset = 0) {
3793
- /* Must force this to a string, lest it be a number and the
3794
- * "utf8-encoding" below actually allocate a buffer of length
3795
- * src */
3796
- if ('string' !== typeof src) {
3797
- src = String(src);
3798
- }
3799
- const srcb = buffer_1.Buffer.from(src, 'utf8');
3800
- const span = srcb.length;
3801
- if ((offset + span) > b.length) {
3802
- throw new RangeError('encoding overruns Buffer');
3803
- }
3804
- const buffer = uint8ArrayToBuffer(b);
3805
- srcb.copy(buffer, offset);
3806
- buffer[offset + span] = 0;
3807
- return span + 1;
3808
- }
3809
- }
3810
- Layout$1.CString = CString;
3811
- /**
3812
- * Contain a UTF8 string with implicit length.
3813
- *
3814
- * *Factory*: {@link module:Layout.utf8|utf8}
3815
- *
3816
- * **NOTE** Because the length is implicit in the size of the buffer
3817
- * this layout should be used only in isolation, or in a situation
3818
- * where the length can be expressed by operating on a slice of the
3819
- * containing buffer.
3820
- *
3821
- * @param {Number} [maxSpan] - the maximum length allowed for encoded
3822
- * string content. If not provided there is no bound on the allowed
3823
- * content.
3824
- *
3825
- * @param {String} [property] - initializer for {@link
3826
- * Layout#property|property}.
3827
- *
3828
- * @augments {Layout}
3829
- */
3830
- class UTF8 extends Layout {
3831
- constructor(maxSpan, property) {
3832
- if (('string' === typeof maxSpan) && (undefined === property)) {
3833
- property = maxSpan;
3834
- maxSpan = undefined;
3835
- }
3836
- if (undefined === maxSpan) {
3837
- maxSpan = -1;
3838
- }
3839
- else if (!Number.isInteger(maxSpan)) {
3840
- throw new TypeError('maxSpan must be an integer');
3841
- }
3842
- super(-1, property);
3843
- /** The maximum span of the layout in bytes.
3844
- *
3845
- * Positive values are generally expected. Zero is abnormal.
3846
- * Attempts to encode or decode a value that exceeds this length
3847
- * will throw a `RangeError`.
3848
- *
3849
- * A negative value indicates that there is no bound on the length
3850
- * of the content. */
3851
- this.maxSpan = maxSpan;
3852
- }
3853
- /** @override */
3854
- getSpan(b, offset = 0) {
3855
- checkUint8Array(b);
3856
- return b.length - offset;
3857
- }
3858
- /** @override */
3859
- decode(b, offset = 0) {
3860
- const span = this.getSpan(b, offset);
3861
- if ((0 <= this.maxSpan)
3862
- && (this.maxSpan < span)) {
3863
- throw new RangeError('text length exceeds maxSpan');
3864
- }
3865
- return uint8ArrayToBuffer(b).slice(offset, offset + span).toString('utf-8');
3866
- }
3867
- /** @override */
3868
- encode(src, b, offset = 0) {
3869
- /* Must force this to a string, lest it be a number and the
3870
- * "utf8-encoding" below actually allocate a buffer of length
3871
- * src */
3872
- if ('string' !== typeof src) {
3873
- src = String(src);
3874
- }
3875
- const srcb = buffer_1.Buffer.from(src, 'utf8');
3876
- const span = srcb.length;
3877
- if ((0 <= this.maxSpan)
3878
- && (this.maxSpan < span)) {
3879
- throw new RangeError('text length exceeds maxSpan');
3880
- }
3881
- if ((offset + span) > b.length) {
3882
- throw new RangeError('encoding overruns Buffer');
3883
- }
3884
- srcb.copy(uint8ArrayToBuffer(b), offset);
3885
- return span;
3886
- }
3887
- }
3888
- Layout$1.UTF8 = UTF8;
3889
- /**
3890
- * Contain a constant value.
3891
- *
3892
- * This layout may be used in cases where a JavaScript value can be
3893
- * inferred without an expression in the binary encoding. An example
3894
- * would be a {@link VariantLayout|variant layout} where the content
3895
- * is implied by the union {@link Union#discriminator|discriminator}.
3896
- *
3897
- * @param {Object|Number|String} value - initializer for {@link
3898
- * Constant#value|value}. If the value is an object (or array) and
3899
- * the application intends the object to remain unchanged regardless
3900
- * of what is done to values decoded by this layout, the value should
3901
- * be frozen prior passing it to this constructor.
3902
- *
3903
- * @param {String} [property] - initializer for {@link
3904
- * Layout#property|property}.
3905
- *
3906
- * @augments {Layout}
3907
- */
3908
- class Constant extends Layout {
3909
- constructor(value, property) {
3910
- super(0, property);
3911
- /** The value produced by this constant when the layout is {@link
3912
- * Constant#decode|decoded}.
3913
- *
3914
- * Any JavaScript value including `null` and `undefined` is
3915
- * permitted.
3916
- *
3917
- * **WARNING** If `value` passed in the constructor was not
3918
- * frozen, it is possible for users of decoded values to change
3919
- * the content of the value. */
3920
- this.value = value;
3921
- }
3922
- /** @override */
3923
- decode(b, offset) {
3924
- return this.value;
3925
- }
3926
- /** @override */
3927
- encode(src, b, offset) {
3928
- /* Constants take no space */
3929
- return 0;
3930
- }
3931
- }
3932
- Layout$1.Constant = Constant;
3933
- /** Factory for {@link GreedyCount}. */
3934
- Layout$1.greedy = ((elementSpan, property) => new GreedyCount(elementSpan, property));
3935
- /** Factory for {@link OffsetLayout}. */
3936
- Layout$1.offset = ((layout, offset, property) => new OffsetLayout(layout, offset, property));
3937
- /** Factory for {@link UInt|unsigned int layouts} spanning one
3938
- * byte. */
3939
- var u8 = Layout$1.u8 = ((property) => new UInt(1, property));
3940
- /** Factory for {@link UInt|little-endian unsigned int layouts}
3941
- * spanning two bytes. */
3942
- Layout$1.u16 = ((property) => new UInt(2, property));
3943
- /** Factory for {@link UInt|little-endian unsigned int layouts}
3944
- * spanning three bytes. */
3945
- Layout$1.u24 = ((property) => new UInt(3, property));
3946
- /** Factory for {@link UInt|little-endian unsigned int layouts}
3947
- * spanning four bytes. */
3948
- var u32 = Layout$1.u32 = ((property) => new UInt(4, property));
3949
- /** Factory for {@link UInt|little-endian unsigned int layouts}
3950
- * spanning five bytes. */
3951
- Layout$1.u40 = ((property) => new UInt(5, property));
3952
- /** Factory for {@link UInt|little-endian unsigned int layouts}
3953
- * spanning six bytes. */
3954
- Layout$1.u48 = ((property) => new UInt(6, property));
3955
- /** Factory for {@link NearUInt64|little-endian unsigned int
3956
- * layouts} interpreted as Numbers. */
3957
- Layout$1.nu64 = ((property) => new NearUInt64(property));
3958
- /** Factory for {@link UInt|big-endian unsigned int layouts}
3959
- * spanning two bytes. */
3960
- Layout$1.u16be = ((property) => new UIntBE(2, property));
3961
- /** Factory for {@link UInt|big-endian unsigned int layouts}
3962
- * spanning three bytes. */
3963
- Layout$1.u24be = ((property) => new UIntBE(3, property));
3964
- /** Factory for {@link UInt|big-endian unsigned int layouts}
3965
- * spanning four bytes. */
3966
- Layout$1.u32be = ((property) => new UIntBE(4, property));
3967
- /** Factory for {@link UInt|big-endian unsigned int layouts}
3968
- * spanning five bytes. */
3969
- Layout$1.u40be = ((property) => new UIntBE(5, property));
3970
- /** Factory for {@link UInt|big-endian unsigned int layouts}
3971
- * spanning six bytes. */
3972
- Layout$1.u48be = ((property) => new UIntBE(6, property));
3973
- /** Factory for {@link NearUInt64BE|big-endian unsigned int
3974
- * layouts} interpreted as Numbers. */
3975
- Layout$1.nu64be = ((property) => new NearUInt64BE(property));
3976
- /** Factory for {@link Int|signed int layouts} spanning one
3977
- * byte. */
3978
- Layout$1.s8 = ((property) => new Int(1, property));
3979
- /** Factory for {@link Int|little-endian signed int layouts}
3980
- * spanning two bytes. */
3981
- Layout$1.s16 = ((property) => new Int(2, property));
3982
- /** Factory for {@link Int|little-endian signed int layouts}
3983
- * spanning three bytes. */
3984
- Layout$1.s24 = ((property) => new Int(3, property));
3985
- /** Factory for {@link Int|little-endian signed int layouts}
3986
- * spanning four bytes. */
3987
- Layout$1.s32 = ((property) => new Int(4, property));
3988
- /** Factory for {@link Int|little-endian signed int layouts}
3989
- * spanning five bytes. */
3990
- Layout$1.s40 = ((property) => new Int(5, property));
3991
- /** Factory for {@link Int|little-endian signed int layouts}
3992
- * spanning six bytes. */
3993
- Layout$1.s48 = ((property) => new Int(6, property));
3994
- /** Factory for {@link NearInt64|little-endian signed int layouts}
3995
- * interpreted as Numbers. */
3996
- Layout$1.ns64 = ((property) => new NearInt64(property));
3997
- /** Factory for {@link Int|big-endian signed int layouts}
3998
- * spanning two bytes. */
3999
- Layout$1.s16be = ((property) => new IntBE(2, property));
4000
- /** Factory for {@link Int|big-endian signed int layouts}
4001
- * spanning three bytes. */
4002
- Layout$1.s24be = ((property) => new IntBE(3, property));
4003
- /** Factory for {@link Int|big-endian signed int layouts}
4004
- * spanning four bytes. */
4005
- Layout$1.s32be = ((property) => new IntBE(4, property));
4006
- /** Factory for {@link Int|big-endian signed int layouts}
4007
- * spanning five bytes. */
4008
- Layout$1.s40be = ((property) => new IntBE(5, property));
4009
- /** Factory for {@link Int|big-endian signed int layouts}
4010
- * spanning six bytes. */
4011
- Layout$1.s48be = ((property) => new IntBE(6, property));
4012
- /** Factory for {@link NearInt64BE|big-endian signed int layouts}
4013
- * interpreted as Numbers. */
4014
- Layout$1.ns64be = ((property) => new NearInt64BE(property));
4015
- /** Factory for {@link Float|little-endian 32-bit floating point} values. */
4016
- Layout$1.f32 = ((property) => new Float(property));
4017
- /** Factory for {@link FloatBE|big-endian 32-bit floating point} values. */
4018
- Layout$1.f32be = ((property) => new FloatBE(property));
4019
- /** Factory for {@link Double|little-endian 64-bit floating point} values. */
4020
- Layout$1.f64 = ((property) => new Double(property));
4021
- /** Factory for {@link DoubleBE|big-endian 64-bit floating point} values. */
4022
- Layout$1.f64be = ((property) => new DoubleBE(property));
4023
- /** Factory for {@link Structure} values. */
4024
- var struct = Layout$1.struct = ((fields, property, decodePrefixes) => new Structure(fields, property, decodePrefixes));
4025
- /** Factory for {@link BitStructure} values. */
4026
- Layout$1.bits = ((word, msb, property) => new BitStructure(word, msb, property));
4027
- /** Factory for {@link Sequence} values. */
4028
- Layout$1.seq = ((elementLayout, count, property) => new Sequence(elementLayout, count, property));
4029
- /** Factory for {@link Union} values. */
4030
- Layout$1.union = ((discr, defaultLayout, property) => new Union(discr, defaultLayout, property));
4031
- /** Factory for {@link UnionLayoutDiscriminator} values. */
4032
- Layout$1.unionLayoutDiscriminator = ((layout, property) => new UnionLayoutDiscriminator(layout, property));
4033
- /** Factory for {@link Blob} values. */
4034
- var blob = Layout$1.blob = ((length, property) => new Blob(length, property));
4035
- /** Factory for {@link CString} values. */
4036
- Layout$1.cstr = ((property) => new CString(property));
4037
- /** Factory for {@link UTF8} values. */
4038
- Layout$1.utf8 = ((maxSpan, property) => new UTF8(maxSpan, property));
4039
- /** Factory for {@link Constant} values. */
4040
- Layout$1.constant = ((value, property) => new Constant(value, property));
4041
-
4042
- const encodeDecode = (layout) => {
4043
- const decode = layout.decode.bind(layout);
4044
- const encode = layout.encode.bind(layout);
4045
- return { decode, encode };
4046
- };
4047
-
4048
- var browser = {};
4049
-
4050
- Object.defineProperty(browser, "__esModule", { value: true });
4051
- /**
4052
- * Convert a little-endian buffer into a BigInt.
4053
- * @param buf The little-endian buffer to convert
4054
- * @returns A BigInt with the little-endian representation of buf.
4055
- */
4056
- function toBigIntLE(buf) {
4057
- {
4058
- const reversed = Buffer.from(buf);
4059
- reversed.reverse();
4060
- const hex = reversed.toString('hex');
4061
- if (hex.length === 0) {
4062
- return BigInt(0);
4063
- }
4064
- return BigInt(`0x${hex}`);
4065
- }
4066
- }
4067
- var toBigIntLE_1 = browser.toBigIntLE = toBigIntLE;
4068
- /**
4069
- * Convert a big-endian buffer into a BigInt
4070
- * @param buf The big-endian buffer to convert.
4071
- * @returns A BigInt with the big-endian representation of buf.
4072
- */
4073
- function toBigIntBE(buf) {
4074
- {
4075
- const hex = buf.toString('hex');
4076
- if (hex.length === 0) {
4077
- return BigInt(0);
4078
- }
4079
- return BigInt(`0x${hex}`);
4080
- }
4081
- }
4082
- browser.toBigIntBE = toBigIntBE;
4083
- /**
4084
- * Convert a BigInt to a little-endian buffer.
4085
- * @param num The BigInt to convert.
4086
- * @param width The number of bytes that the resulting buffer should be.
4087
- * @returns A little-endian buffer representation of num.
4088
- */
4089
- function toBufferLE(num, width) {
4090
- {
4091
- const hex = num.toString(16);
4092
- const buffer = Buffer.from(hex.padStart(width * 2, '0').slice(0, width * 2), 'hex');
4093
- buffer.reverse();
4094
- return buffer;
4095
- }
4096
- }
4097
- var toBufferLE_1 = browser.toBufferLE = toBufferLE;
4098
- /**
4099
- * Convert a BigInt to a big-endian buffer.
4100
- * @param num The BigInt to convert.
4101
- * @param width The number of bytes that the resulting buffer should be.
4102
- * @returns A big-endian buffer representation of num.
4103
- */
4104
- function toBufferBE(num, width) {
4105
- {
4106
- const hex = num.toString(16);
4107
- return Buffer.from(hex.padStart(width * 2, '0').slice(0, width * 2), 'hex');
4108
- }
4109
- }
4110
- browser.toBufferBE = toBufferBE;
4111
-
4112
- const bigInt = (length) => (property) => {
4113
- const layout = blob(length, property);
4114
- const { encode, decode } = encodeDecode(layout);
4115
- const bigIntLayout = layout;
4116
- bigIntLayout.decode = (buffer, offset) => {
4117
- const src = decode(buffer, offset);
4118
- return toBigIntLE_1(Buffer.from(src));
4119
- };
4120
- bigIntLayout.encode = (bigInt, buffer, offset) => {
4121
- const src = toBufferLE_1(bigInt, length);
4122
- return encode(src, buffer, offset);
4123
- };
4124
- return bigIntLayout;
4125
- };
4126
- const u64 = bigInt(8);
4127
-
4128
- const bool = (property) => {
4129
- const layout = u8(property);
4130
- const { encode, decode } = encodeDecode(layout);
4131
- const boolLayout = layout;
4132
- boolLayout.decode = (buffer, offset) => {
4133
- const src = decode(buffer, offset);
4134
- return !!src;
4135
- };
4136
- boolLayout.encode = (bool, buffer, offset) => {
4137
- const src = Number(bool);
4138
- return encode(src, buffer, offset);
4139
- };
4140
- return boolLayout;
4141
- };
4142
-
4143
- const publicKey = (property) => {
4144
- const layout = blob(32, property);
4145
- const { encode, decode } = encodeDecode(layout);
4146
- const publicKeyLayout = layout;
4147
- publicKeyLayout.decode = (buffer, offset) => {
4148
- const src = decode(buffer, offset);
4149
- return new web3_js.PublicKey(src);
4150
- };
4151
- publicKeyLayout.encode = (publicKey, buffer, offset) => {
4152
- const src = publicKey.toBuffer();
4153
- return encode(src, buffer, offset);
4154
- };
4155
- return publicKeyLayout;
4156
- };
4157
-
4158
- /** Base class for errors */
4159
- class TokenError extends Error {
4160
- constructor(message) {
4161
- super(message);
4162
- }
4163
- }
4164
- /** Thrown if an account is not found at the expected address */
4165
- class TokenAccountNotFoundError extends TokenError {
4166
- constructor() {
4167
- super(...arguments);
4168
- this.name = 'TokenAccountNotFoundError';
4169
- }
4170
- }
4171
- /** Thrown if a program state account is not a valid Account */
4172
- class TokenInvalidAccountError extends TokenError {
4173
- constructor() {
4174
- super(...arguments);
4175
- this.name = 'TokenInvalidAccountError';
4176
- }
4177
- }
4178
- /** Thrown if a program state account is not owned by the expected token program */
4179
- class TokenInvalidAccountOwnerError extends TokenError {
4180
- constructor() {
4181
- super(...arguments);
4182
- this.name = 'TokenInvalidAccountOwnerError';
4183
- }
4184
- }
4185
- /** Thrown if the byte length of an program state account doesn't match the expected size */
4186
- class TokenInvalidAccountSizeError extends TokenError {
4187
- constructor() {
4188
- super(...arguments);
4189
- this.name = 'TokenInvalidAccountSizeError';
4190
- }
4191
- }
4192
- /** Thrown if the mint of a token account doesn't match the expected mint */
4193
- class TokenInvalidMintError extends TokenError {
4194
- constructor() {
4195
- super(...arguments);
4196
- this.name = 'TokenInvalidMintError';
4197
- }
4198
- }
4199
- /** Thrown if the owner of a token account doesn't match the expected owner */
4200
- class TokenInvalidOwnerError extends TokenError {
4201
- constructor() {
4202
- super(...arguments);
4203
- this.name = 'TokenInvalidOwnerError';
4204
- }
4205
- }
4206
- /** Thrown if the owner of a token account is a PDA (Program Derived Address) */
4207
- class TokenOwnerOffCurveError extends TokenError {
4208
- constructor() {
4209
- super(...arguments);
4210
- this.name = 'TokenOwnerOffCurveError';
4211
- }
4212
- }
4213
-
4214
- /** Instructions defined by the program */
4215
- var TokenInstruction;
4216
- (function (TokenInstruction) {
4217
- TokenInstruction[TokenInstruction["InitializeMint"] = 0] = "InitializeMint";
4218
- TokenInstruction[TokenInstruction["InitializeAccount"] = 1] = "InitializeAccount";
4219
- TokenInstruction[TokenInstruction["InitializeMultisig"] = 2] = "InitializeMultisig";
4220
- TokenInstruction[TokenInstruction["Transfer"] = 3] = "Transfer";
4221
- TokenInstruction[TokenInstruction["Approve"] = 4] = "Approve";
4222
- TokenInstruction[TokenInstruction["Revoke"] = 5] = "Revoke";
4223
- TokenInstruction[TokenInstruction["SetAuthority"] = 6] = "SetAuthority";
4224
- TokenInstruction[TokenInstruction["MintTo"] = 7] = "MintTo";
4225
- TokenInstruction[TokenInstruction["Burn"] = 8] = "Burn";
4226
- TokenInstruction[TokenInstruction["CloseAccount"] = 9] = "CloseAccount";
4227
- TokenInstruction[TokenInstruction["FreezeAccount"] = 10] = "FreezeAccount";
4228
- TokenInstruction[TokenInstruction["ThawAccount"] = 11] = "ThawAccount";
4229
- TokenInstruction[TokenInstruction["TransferChecked"] = 12] = "TransferChecked";
4230
- TokenInstruction[TokenInstruction["ApproveChecked"] = 13] = "ApproveChecked";
4231
- TokenInstruction[TokenInstruction["MintToChecked"] = 14] = "MintToChecked";
4232
- TokenInstruction[TokenInstruction["BurnChecked"] = 15] = "BurnChecked";
4233
- TokenInstruction[TokenInstruction["InitializeAccount2"] = 16] = "InitializeAccount2";
4234
- TokenInstruction[TokenInstruction["SyncNative"] = 17] = "SyncNative";
4235
- TokenInstruction[TokenInstruction["InitializeAccount3"] = 18] = "InitializeAccount3";
4236
- TokenInstruction[TokenInstruction["InitializeMultisig2"] = 19] = "InitializeMultisig2";
4237
- TokenInstruction[TokenInstruction["InitializeMint2"] = 20] = "InitializeMint2";
4238
- TokenInstruction[TokenInstruction["GetAccountDataSize"] = 21] = "GetAccountDataSize";
4239
- TokenInstruction[TokenInstruction["InitializeImmutableOwner"] = 22] = "InitializeImmutableOwner";
4240
- TokenInstruction[TokenInstruction["AmountToUiAmount"] = 23] = "AmountToUiAmount";
4241
- TokenInstruction[TokenInstruction["UiAmountToAmount"] = 24] = "UiAmountToAmount";
4242
- TokenInstruction[TokenInstruction["InitializeMintCloseAuthority"] = 25] = "InitializeMintCloseAuthority";
4243
- TokenInstruction[TokenInstruction["TransferFeeExtension"] = 26] = "TransferFeeExtension";
4244
- TokenInstruction[TokenInstruction["ConfidentialTransferExtension"] = 27] = "ConfidentialTransferExtension";
4245
- TokenInstruction[TokenInstruction["DefaultAccountStateExtension"] = 28] = "DefaultAccountStateExtension";
4246
- TokenInstruction[TokenInstruction["Reallocate"] = 29] = "Reallocate";
4247
- TokenInstruction[TokenInstruction["MemoTransferExtension"] = 30] = "MemoTransferExtension";
4248
- TokenInstruction[TokenInstruction["CreateNativeMint"] = 31] = "CreateNativeMint";
4249
- TokenInstruction[TokenInstruction["InitializeNonTransferableMint"] = 32] = "InitializeNonTransferableMint";
4250
- TokenInstruction[TokenInstruction["InterestBearingMintExtension"] = 33] = "InterestBearingMintExtension";
4251
- TokenInstruction[TokenInstruction["CpiGuardExtension"] = 34] = "CpiGuardExtension";
4252
- TokenInstruction[TokenInstruction["InitializePermanentDelegate"] = 35] = "InitializePermanentDelegate";
4253
- TokenInstruction[TokenInstruction["TransferHookExtension"] = 36] = "TransferHookExtension";
4254
- // ConfidentialTransferFeeExtension = 37,
4255
- // WithdrawalExcessLamports = 38,
4256
- TokenInstruction[TokenInstruction["MetadataPointerExtension"] = 39] = "MetadataPointerExtension";
4257
- TokenInstruction[TokenInstruction["GroupPointerExtension"] = 40] = "GroupPointerExtension";
4258
- TokenInstruction[TokenInstruction["GroupMemberPointerExtension"] = 41] = "GroupMemberPointerExtension";
4259
- })(TokenInstruction || (TokenInstruction = {}));
4260
-
4261
- /** @internal */
4262
- function addSigners(keys, ownerOrAuthority, multiSigners) {
4263
- if (multiSigners.length) {
4264
- keys.push({ pubkey: ownerOrAuthority, isSigner: false, isWritable: false });
4265
- for (const signer of multiSigners) {
4266
- keys.push({
4267
- pubkey: signer instanceof web3_js.PublicKey ? signer : signer.publicKey,
4268
- isSigner: true,
4269
- isWritable: false,
4270
- });
4271
- }
4272
- }
4273
- else {
4274
- keys.push({ pubkey: ownerOrAuthority, isSigner: true, isWritable: false });
4275
- }
4276
- return keys;
4277
- }
4278
-
4279
- var AccountType;
4280
- (function (AccountType) {
4281
- AccountType[AccountType["Uninitialized"] = 0] = "Uninitialized";
4282
- AccountType[AccountType["Mint"] = 1] = "Mint";
4283
- AccountType[AccountType["Account"] = 2] = "Account";
4284
- })(AccountType || (AccountType = {}));
4285
- const ACCOUNT_TYPE_SIZE = 1;
4286
-
4287
- /** Buffer layout for de/serializing a multisig */
4288
- const MultisigLayout = struct([
4289
- u8('m'),
4290
- u8('n'),
4291
- bool('isInitialized'),
4292
- publicKey('signer1'),
4293
- publicKey('signer2'),
4294
- publicKey('signer3'),
4295
- publicKey('signer4'),
4296
- publicKey('signer5'),
4297
- publicKey('signer6'),
4298
- publicKey('signer7'),
4299
- publicKey('signer8'),
4300
- publicKey('signer9'),
4301
- publicKey('signer10'),
4302
- publicKey('signer11'),
4303
- ]);
4304
- /** Byte length of a multisig */
4305
- const MULTISIG_SIZE = MultisigLayout.span;
4306
-
4307
- /** Token account state as stored by the program */
4308
- var AccountState;
4309
- (function (AccountState) {
4310
- AccountState[AccountState["Uninitialized"] = 0] = "Uninitialized";
4311
- AccountState[AccountState["Initialized"] = 1] = "Initialized";
4312
- AccountState[AccountState["Frozen"] = 2] = "Frozen";
4313
- })(AccountState || (AccountState = {}));
4314
- /** Buffer layout for de/serializing a token account */
4315
- const AccountLayout = struct([
4316
- publicKey('mint'),
4317
- publicKey('owner'),
4318
- u64('amount'),
4319
- u32('delegateOption'),
4320
- publicKey('delegate'),
4321
- u8('state'),
4322
- u32('isNativeOption'),
4323
- u64('isNative'),
4324
- u64('delegatedAmount'),
4325
- u32('closeAuthorityOption'),
4326
- publicKey('closeAuthority'),
4327
- ]);
4328
- /** Byte length of a token account */
4329
- const ACCOUNT_SIZE = AccountLayout.span;
4330
- /**
4331
- * Retrieve information about a token account
4332
- *
4333
- * @param connection Connection to use
4334
- * @param address Token account
4335
- * @param commitment Desired level of commitment for querying the state
4336
- * @param programId SPL Token program account
4337
- *
4338
- * @return Token account information
4339
- */
4340
- async function getAccount(connection, address, commitment, programId = TOKEN_PROGRAM_ID) {
4341
- const info = await connection.getAccountInfo(address, commitment);
4342
- return unpackAccount(address, info, programId);
4343
- }
4344
- /**
4345
- * Unpack a token account
4346
- *
4347
- * @param address Token account
4348
- * @param info Token account data
4349
- * @param programId SPL Token program account
4350
- *
4351
- * @return Unpacked token account
4352
- */
4353
- function unpackAccount(address, info, programId = TOKEN_PROGRAM_ID) {
4354
- if (!info)
4355
- throw new TokenAccountNotFoundError();
4356
- if (!info.owner.equals(programId))
4357
- throw new TokenInvalidAccountOwnerError();
4358
- if (info.data.length < ACCOUNT_SIZE)
4359
- throw new TokenInvalidAccountSizeError();
4360
- const rawAccount = AccountLayout.decode(info.data.slice(0, ACCOUNT_SIZE));
4361
- let tlvData = Buffer.alloc(0);
4362
- if (info.data.length > ACCOUNT_SIZE) {
4363
- if (info.data.length === MULTISIG_SIZE)
4364
- throw new TokenInvalidAccountSizeError();
4365
- if (info.data[ACCOUNT_SIZE] != AccountType.Account)
4366
- throw new TokenInvalidAccountError();
4367
- tlvData = info.data.slice(ACCOUNT_SIZE + ACCOUNT_TYPE_SIZE);
4368
- }
4369
- return {
4370
- address,
4371
- mint: rawAccount.mint,
4372
- owner: rawAccount.owner,
4373
- amount: rawAccount.amount,
4374
- delegate: rawAccount.delegateOption ? rawAccount.delegate : null,
4375
- delegatedAmount: rawAccount.delegatedAmount,
4376
- isInitialized: rawAccount.state !== AccountState.Uninitialized,
4377
- isFrozen: rawAccount.state === AccountState.Frozen,
4378
- isNative: !!rawAccount.isNativeOption,
4379
- rentExemptReserve: rawAccount.isNativeOption ? rawAccount.isNative : null,
4380
- closeAuthority: rawAccount.closeAuthorityOption ? rawAccount.closeAuthority : null,
4381
- tlvData,
4382
- };
4383
- }
4384
-
4385
- /** Buffer layout for de/serializing a mint */
4386
- const MintLayout = struct([
4387
- u32('mintAuthorityOption'),
4388
- publicKey('mintAuthority'),
4389
- u64('supply'),
4390
- u8('decimals'),
4391
- bool('isInitialized'),
4392
- u32('freezeAuthorityOption'),
4393
- publicKey('freezeAuthority'),
4394
- ]);
4395
- /** Byte length of a mint */
4396
- const MINT_SIZE = MintLayout.span;
4397
- /**
4398
- * Get the address of the associated token account for a given mint and owner
4399
- *
4400
- * @param mint Token mint account
4401
- * @param owner Owner of the new account
4402
- * @param allowOwnerOffCurve Allow the owner account to be a PDA (Program Derived Address)
4403
- * @param programId SPL Token program account
4404
- * @param associatedTokenProgramId SPL Associated Token program account
4405
- *
4406
- * @return Address of the associated token account
4407
- */
4408
- function getAssociatedTokenAddressSync(mint, owner, allowOwnerOffCurve = false, programId = TOKEN_PROGRAM_ID, associatedTokenProgramId = ASSOCIATED_TOKEN_PROGRAM_ID) {
4409
- if (!allowOwnerOffCurve && !web3_js.PublicKey.isOnCurve(owner.toBuffer()))
4410
- throw new TokenOwnerOffCurveError();
4411
- const [address] = web3_js.PublicKey.findProgramAddressSync([owner.toBuffer(), programId.toBuffer(), mint.toBuffer()], associatedTokenProgramId);
4412
- return address;
4413
- }
4414
-
4415
- class COptionPublicKeyLayout extends Layout_2 {
4416
- constructor(property) {
4417
- super(-1, property);
4418
- this.publicKeyLayout = publicKey();
4419
- }
4420
- decode(buffer, offset = 0) {
4421
- const option = buffer[offset];
4422
- if (option === 0) {
4423
- return null;
4424
- }
4425
- return this.publicKeyLayout.decode(buffer, offset + 1);
4426
- }
4427
- encode(src, buffer, offset = 0) {
4428
- if (src === null) {
4429
- buffer[offset] = 0;
4430
- return 1;
4431
- }
4432
- else {
4433
- buffer[offset] = 1;
4434
- this.publicKeyLayout.encode(src, buffer, offset + 1);
4435
- return 33;
4436
- }
4437
- }
4438
- getSpan(buffer, offset = 0) {
4439
- if (buffer) {
4440
- const option = buffer[offset];
4441
- return option === 0 ? 1 : 1 + this.publicKeyLayout.span;
4442
- }
4443
- return 1 + this.publicKeyLayout.span;
4444
- }
4445
- }
4446
-
4447
- /**
4448
- * Construct a CreateAssociatedTokenAccount instruction
4449
- *
4450
- * @param payer Payer of the initialization fees
4451
- * @param associatedToken New associated token account
4452
- * @param owner Owner of the new account
4453
- * @param mint Token mint account
4454
- * @param programId SPL Token program account
4455
- * @param associatedTokenProgramId SPL Associated Token program account
4456
- *
4457
- * @return Instruction to add to a transaction
4458
- */
4459
- function createAssociatedTokenAccountInstruction(payer, associatedToken, owner, mint, programId = TOKEN_PROGRAM_ID, associatedTokenProgramId = ASSOCIATED_TOKEN_PROGRAM_ID) {
4460
- return buildAssociatedTokenAccountInstruction(payer, associatedToken, owner, mint, Buffer.alloc(0), programId, associatedTokenProgramId);
4461
- }
4462
- function buildAssociatedTokenAccountInstruction(payer, associatedToken, owner, mint, instructionData, programId = TOKEN_PROGRAM_ID, associatedTokenProgramId = ASSOCIATED_TOKEN_PROGRAM_ID) {
4463
- const keys = [
4464
- { pubkey: payer, isSigner: true, isWritable: true },
4465
- { pubkey: associatedToken, isSigner: false, isWritable: true },
4466
- { pubkey: owner, isSigner: false, isWritable: false },
4467
- { pubkey: mint, isSigner: false, isWritable: false },
4468
- { pubkey: web3_js.SystemProgram.programId, isSigner: false, isWritable: false },
4469
- { pubkey: programId, isSigner: false, isWritable: false },
4470
- ];
4471
- return new web3_js.TransactionInstruction({
4472
- keys,
4473
- programId: associatedTokenProgramId,
4474
- data: instructionData,
4475
- });
4476
- }
4477
-
4478
- /** TODO: docs */
4479
- const initializeMint2InstructionData = struct([
4480
- u8('instruction'),
4481
- u8('decimals'),
4482
- publicKey('mintAuthority'),
4483
- new COptionPublicKeyLayout('freezeAuthority'),
4484
- ]);
4485
- /**
4486
- * Construct an InitializeMint2 instruction
4487
- *
4488
- * @param mint Token mint account
4489
- * @param decimals Number of decimals in token account amounts
4490
- * @param mintAuthority Minting authority
4491
- * @param freezeAuthority Optional authority that can freeze token accounts
4492
- * @param programId SPL Token program account
4493
- *
4494
- * @return Instruction to add to a transaction
4495
- */
4496
- function createInitializeMint2Instruction(mint, decimals, mintAuthority, freezeAuthority, programId = TOKEN_PROGRAM_ID) {
4497
- const keys = [{ pubkey: mint, isSigner: false, isWritable: true }];
4498
- const data = Buffer.alloc(initializeMint2InstructionData.span);
4499
- initializeMint2InstructionData.encode({
4500
- instruction: TokenInstruction.InitializeMint2,
4501
- decimals,
4502
- mintAuthority,
4503
- freezeAuthority,
4504
- }, data);
4505
- return new web3_js.TransactionInstruction({ keys, programId, data });
4506
- }
4507
-
4508
- /**
4509
- * Retrieve the associated token account, or create it if it doesn't exist
4510
- *
4511
- * @param connection Connection to use
4512
- * @param payer Payer of the transaction and initialization fees
4513
- * @param mint Mint associated with the account to set or verify
4514
- * @param owner Owner of the account to set or verify
4515
- * @param allowOwnerOffCurve Allow the owner account to be a PDA (Program Derived Address)
4516
- * @param commitment Desired level of commitment for querying the state
4517
- * @param confirmOptions Options for confirming the transaction
4518
- * @param programId SPL Token program account
4519
- * @param associatedTokenProgramId SPL Associated Token program account
4520
- *
4521
- * @return Address of the new associated token account
4522
- */
4523
- async function getOrCreateAssociatedTokenAccount(connection, payer, mint, owner, allowOwnerOffCurve = false, commitment, confirmOptions, programId = TOKEN_PROGRAM_ID, associatedTokenProgramId = ASSOCIATED_TOKEN_PROGRAM_ID) {
4524
- const associatedToken = getAssociatedTokenAddressSync(mint, owner, allowOwnerOffCurve, programId, associatedTokenProgramId);
4525
- // This is the optimal logic, considering TX fee, client-side computation, RPC roundtrips and guaranteed idempotent.
4526
- // Sadly we can't do this atomically.
4527
- let account;
4528
- try {
4529
- account = await getAccount(connection, associatedToken, commitment, programId);
4530
- }
4531
- catch (error) {
4532
- // TokenAccountNotFoundError can be possible if the associated address has already received some lamports,
4533
- // becoming a system account. Assuming program derived addressing is safe, this is the only case for the
4534
- // TokenInvalidAccountOwnerError in this code path.
4535
- if (error instanceof TokenAccountNotFoundError || error instanceof TokenInvalidAccountOwnerError) {
4536
- // As this isn't atomic, it's possible others can create associated accounts meanwhile.
4537
- try {
4538
- const transaction = new web3_js.Transaction().add(createAssociatedTokenAccountInstruction(payer.publicKey, associatedToken, owner, mint, programId, associatedTokenProgramId));
4539
- await web3_js.sendAndConfirmTransaction(connection, transaction, [payer], confirmOptions);
4540
- }
4541
- catch (error) {
4542
- // Ignore all errors; for now there is no API-compatible way to selectively ignore the expected
4543
- // instruction error if the associated account exists already.
4544
- }
4545
- // Now this should always succeed
4546
- account = await getAccount(connection, associatedToken, commitment, programId);
4547
- }
4548
- else {
4549
- throw error;
4550
- }
4551
- }
4552
- if (!account.mint.equals(mint))
4553
- throw new TokenInvalidMintError();
4554
- if (!account.owner.equals(owner))
4555
- throw new TokenInvalidOwnerError();
4556
- return account;
4557
- }
4558
-
4559
- /** TODO: docs */
4560
- const mintToInstructionData = struct([u8('instruction'), u64('amount')]);
4561
- /**
4562
- * Construct a MintTo instruction
4563
- *
4564
- * @param mint Public key of the mint
4565
- * @param destination Address of the token account to mint to
4566
- * @param authority The mint authority
4567
- * @param amount Amount to mint
4568
- * @param multiSigners Signing accounts if `authority` is a multisig
4569
- * @param programId SPL Token program account
4570
- *
4571
- * @return Instruction to add to a transaction
4572
- */
4573
- function createMintToInstruction(mint, destination, authority, amount, multiSigners = [], programId = TOKEN_PROGRAM_ID) {
4574
- const keys = addSigners([
4575
- { pubkey: mint, isSigner: false, isWritable: true },
4576
- { pubkey: destination, isSigner: false, isWritable: true },
4577
- ], authority, multiSigners);
4578
- const data = Buffer.alloc(mintToInstructionData.span);
4579
- mintToInstructionData.encode({
4580
- instruction: TokenInstruction.MintTo,
4581
- amount: BigInt(amount),
4582
- }, data);
4583
- return new web3_js.TransactionInstruction({ keys, programId, data });
4584
- }
4585
-
4586
1782
  /**
4587
1783
  * Sum up the token amounts of the compressed token accounts
4588
1784
  */
4589
- const sumUpTokenAmount = (accounts) => {
4590
- return accounts.reduce((acc, account) => acc.add(account.parsed.amount), stateless_js.bn(0));
1785
+ const sumUpTokenAmount = accounts => {
1786
+ return accounts.reduce(
1787
+ (acc, account) => acc.add(account.parsed.amount),
1788
+ stateless_js.bn(0),
1789
+ );
4591
1790
  };
4592
1791
  /**
4593
1792
  * Validate that all the compressed token accounts are owned by the same owner.
4594
1793
  */
4595
- const validateSameTokenOwner = (accounts) => {
1794
+ const validateSameTokenOwner = accounts => {
4596
1795
  const owner = accounts[0].parsed.owner;
4597
1796
  accounts.forEach(acc => {
4598
1797
  if (!acc.parsed.owner.equals(owner)) {
@@ -4603,7 +1802,7 @@ const validateSameTokenOwner = (accounts) => {
4603
1802
  /**
4604
1803
  * Parse compressed token accounts to get the mint, current owner and delegate.
4605
1804
  */
4606
- const parseTokenData = (compressedTokenAccounts) => {
1805
+ const parseTokenData = compressedTokenAccounts => {
4607
1806
  const mint = compressedTokenAccounts[0].parsed.mint;
4608
1807
  const currentOwner = compressedTokenAccounts[0].parsed.owner;
4609
1808
  const delegate = compressedTokenAccounts[0].parsed.delegate;
@@ -4617,13 +1816,22 @@ const parseTokenData = (compressedTokenAccounts) => {
4617
1816
  * @returns Output token data for the transfer
4618
1817
  * instruction
4619
1818
  */
4620
- function createTransferOutputState(inputCompressedTokenAccounts, toAddress, amount) {
1819
+ function createTransferOutputState(
1820
+ inputCompressedTokenAccounts,
1821
+ toAddress,
1822
+ amount,
1823
+ ) {
4621
1824
  amount = stateless_js.bn(amount);
4622
1825
  const inputAmount = sumUpTokenAmount(inputCompressedTokenAccounts);
4623
- const inputLamports = stateless_js.sumUpLamports(inputCompressedTokenAccounts.map(acc => acc.compressedAccount));
1826
+ const inputLamports = stateless_js.sumUpLamports(
1827
+ inputCompressedTokenAccounts.map(acc => acc.compressedAccount),
1828
+ );
4624
1829
  const changeAmount = inputAmount.sub(amount);
4625
1830
  stateless_js.validateSufficientBalance(changeAmount);
4626
- if (changeAmount.eq(stateless_js.bn(0)) && inputLamports.eq(stateless_js.bn(0))) {
1831
+ if (
1832
+ changeAmount.eq(stateless_js.bn(0)) &&
1833
+ inputLamports.eq(stateless_js.bn(0))
1834
+ ) {
4627
1835
  return [
4628
1836
  {
4629
1837
  owner: toAddress,
@@ -4634,7 +1842,9 @@ function createTransferOutputState(inputCompressedTokenAccounts, toAddress, amou
4634
1842
  ];
4635
1843
  }
4636
1844
  /// validates token program
4637
- stateless_js.validateSameOwner(inputCompressedTokenAccounts.map(acc => acc.compressedAccount));
1845
+ stateless_js.validateSameOwner(
1846
+ inputCompressedTokenAccounts.map(acc => acc.compressedAccount),
1847
+ );
4638
1848
  validateSameTokenOwner(inputCompressedTokenAccounts);
4639
1849
  const outputCompressedAccounts = [
4640
1850
  {
@@ -4661,15 +1871,22 @@ function createTransferOutputState(inputCompressedTokenAccounts, toAddress, amou
4661
1871
  */
4662
1872
  function createDecompressOutputState(inputCompressedTokenAccounts, amount) {
4663
1873
  amount = stateless_js.bn(amount);
4664
- const inputLamports = stateless_js.sumUpLamports(inputCompressedTokenAccounts.map(acc => acc.compressedAccount));
1874
+ const inputLamports = stateless_js.sumUpLamports(
1875
+ inputCompressedTokenAccounts.map(acc => acc.compressedAccount),
1876
+ );
4665
1877
  const inputAmount = sumUpTokenAmount(inputCompressedTokenAccounts);
4666
1878
  const changeAmount = inputAmount.sub(amount);
4667
1879
  stateless_js.validateSufficientBalance(changeAmount);
4668
1880
  /// lamports gets decompressed
4669
- if (changeAmount.eq(stateless_js.bn(0)) && inputLamports.eq(stateless_js.bn(0))) {
1881
+ if (
1882
+ changeAmount.eq(stateless_js.bn(0)) &&
1883
+ inputLamports.eq(stateless_js.bn(0))
1884
+ ) {
4670
1885
  return [];
4671
1886
  }
4672
- stateless_js.validateSameOwner(inputCompressedTokenAccounts.map(acc => acc.compressedAccount));
1887
+ stateless_js.validateSameOwner(
1888
+ inputCompressedTokenAccounts.map(acc => acc.compressedAccount),
1889
+ );
4673
1890
  validateSameTokenOwner(inputCompressedTokenAccounts);
4674
1891
  const tokenTransferOutputs = [
4675
1892
  {
@@ -4685,7 +1902,7 @@ class CompressedTokenProgram {
4685
1902
  /**
4686
1903
  * @internal
4687
1904
  */
4688
- constructor() { }
1905
+ constructor() {}
4689
1906
  /**
4690
1907
  * Set a custom programId via PublicKey or base58 encoded string.
4691
1908
  * This method is not required for regular usage.
@@ -4717,21 +1934,38 @@ class CompressedTokenProgram {
4717
1934
  /// program only for serde and building instructions, not for
4718
1935
  /// interacting with the network.
4719
1936
  const mockKeypair = web3_js.Keypair.generate();
4720
- const mockConnection = new web3_js.Connection('http://127.0.0.1:8899', 'confirmed');
4721
- const mockProvider = new anchor.AnchorProvider(mockConnection, stateless_js.useWallet(mockKeypair), stateless_js.confirmConfig);
1937
+ const mockConnection = new web3_js.Connection(
1938
+ 'http://127.0.0.1:8899',
1939
+ 'confirmed',
1940
+ );
1941
+ const mockProvider = new anchor.AnchorProvider(
1942
+ mockConnection,
1943
+ stateless_js.useWallet(mockKeypair),
1944
+ stateless_js.confirmConfig,
1945
+ );
4722
1946
  anchor.setProvider(mockProvider);
4723
- this._program = new anchor.Program(IDL, this.programId, mockProvider);
1947
+ this._program = new anchor.Program(
1948
+ IDL,
1949
+ this.programId,
1950
+ mockProvider,
1951
+ );
4724
1952
  }
4725
1953
  }
4726
1954
  /** @internal */
4727
1955
  static deriveTokenPoolPda(mint) {
4728
1956
  const seeds = [POOL_SEED, mint.toBuffer()];
4729
- const [address, _] = web3_js.PublicKey.findProgramAddressSync(seeds, this.programId);
1957
+ const [address, _] = web3_js.PublicKey.findProgramAddressSync(
1958
+ seeds,
1959
+ this.programId,
1960
+ );
4730
1961
  return address;
4731
1962
  }
4732
1963
  /** @internal */
4733
1964
  static get deriveCpiAuthorityPda() {
4734
- const [address, _] = web3_js.PublicKey.findProgramAddressSync([CPI_AUTHORITY_SEED], this.programId);
1965
+ const [address, _] = web3_js.PublicKey.findProgramAddressSync(
1966
+ [CPI_AUTHORITY_SEED],
1967
+ this.programId,
1968
+ );
4735
1969
  return address;
4736
1970
  }
4737
1971
  /**
@@ -4740,14 +1974,22 @@ class CompressedTokenProgram {
4740
1974
  static async createMint(params) {
4741
1975
  const { mint, authority, feePayer, rentExemptBalance } = params;
4742
1976
  /// Create and initialize SPL Mint account
4743
- const createMintAccountInstruction = web3_js.SystemProgram.createAccount({
4744
- fromPubkey: feePayer,
4745
- lamports: rentExemptBalance,
4746
- newAccountPubkey: mint,
4747
- programId: TOKEN_PROGRAM_ID,
4748
- space: MINT_SIZE,
4749
- });
4750
- const initializeMintInstruction = createInitializeMint2Instruction(mint, params.decimals, authority, params.freezeAuthority, TOKEN_PROGRAM_ID);
1977
+ const createMintAccountInstruction =
1978
+ web3_js.SystemProgram.createAccount({
1979
+ fromPubkey: feePayer,
1980
+ lamports: rentExemptBalance,
1981
+ newAccountPubkey: mint,
1982
+ programId: splToken.TOKEN_PROGRAM_ID,
1983
+ space: splToken.MINT_SIZE,
1984
+ });
1985
+ const initializeMintInstruction =
1986
+ splToken.createInitializeMint2Instruction(
1987
+ mint,
1988
+ params.decimals,
1989
+ authority,
1990
+ params.freezeAuthority,
1991
+ splToken.TOKEN_PROGRAM_ID,
1992
+ );
4751
1993
  const ix = await this.createTokenPool({
4752
1994
  feePayer,
4753
1995
  mint,
@@ -4764,13 +2006,13 @@ class CompressedTokenProgram {
4764
2006
  const ix = await this.program.methods
4765
2007
  .createTokenPool()
4766
2008
  .accounts({
4767
- mint,
4768
- feePayer,
4769
- tokenPoolPda,
4770
- systemProgram: web3_js.SystemProgram.programId,
4771
- tokenProgram: TOKEN_PROGRAM_ID,
4772
- cpiAuthorityPda: this.deriveCpiAuthorityPda,
4773
- })
2009
+ mint,
2010
+ feePayer,
2011
+ tokenPoolPda,
2012
+ systemProgram: web3_js.SystemProgram.programId,
2013
+ tokenProgram: splToken.TOKEN_PROGRAM_ID,
2014
+ cpiAuthorityPda: this.deriveCpiAuthorityPda,
2015
+ })
4774
2016
  .instruction();
4775
2017
  return ix;
4776
2018
  }
@@ -4779,31 +2021,41 @@ class CompressedTokenProgram {
4779
2021
  */
4780
2022
  static async mintTo(params) {
4781
2023
  const systemKeys = stateless_js.defaultStaticAccountsStruct();
4782
- const { mint, feePayer, authority, merkleTree, toPubkey, amount } = params;
2024
+ const { mint, feePayer, authority, merkleTree, toPubkey, amount } =
2025
+ params;
4783
2026
  const tokenPoolPda = this.deriveTokenPoolPda(mint);
4784
- const amounts = stateless_js.toArray(amount).map(amount => stateless_js.bn(amount));
2027
+ const amounts = stateless_js
2028
+ .toArray(amount)
2029
+ .map(amount => stateless_js.bn(amount));
4785
2030
  const toPubkeys = stateless_js.toArray(toPubkey);
4786
2031
  if (amounts.length !== toPubkeys.length) {
4787
- throw new Error('Amount and toPubkey arrays must have the same length');
2032
+ throw new Error(
2033
+ 'Amount and toPubkey arrays must have the same length',
2034
+ );
4788
2035
  }
4789
2036
  const instruction = await this.program.methods
4790
2037
  .mintTo(toPubkeys, amounts, null)
4791
2038
  .accounts({
4792
- feePayer,
4793
- authority,
4794
- cpiAuthorityPda: this.deriveCpiAuthorityPda,
4795
- mint,
4796
- tokenPoolPda,
4797
- tokenProgram: TOKEN_PROGRAM_ID,
4798
- lightSystemProgram: stateless_js.LightSystemProgram.programId,
4799
- registeredProgramPda: systemKeys.registeredProgramPda,
4800
- noopProgram: systemKeys.noopProgram,
4801
- accountCompressionAuthority: systemKeys.accountCompressionAuthority,
4802
- accountCompressionProgram: systemKeys.accountCompressionProgram,
4803
- merkleTree: merkleTree !== null && merkleTree !== void 0 ? merkleTree : stateless_js.defaultTestStateTreeAccounts().merkleTree,
4804
- selfProgram: this.programId,
4805
- solPoolPda: null,
4806
- })
2039
+ feePayer,
2040
+ authority,
2041
+ cpiAuthorityPda: this.deriveCpiAuthorityPda,
2042
+ mint,
2043
+ tokenPoolPda,
2044
+ tokenProgram: splToken.TOKEN_PROGRAM_ID,
2045
+ lightSystemProgram: stateless_js.LightSystemProgram.programId,
2046
+ registeredProgramPda: systemKeys.registeredProgramPda,
2047
+ noopProgram: systemKeys.noopProgram,
2048
+ accountCompressionAuthority:
2049
+ systemKeys.accountCompressionAuthority,
2050
+ accountCompressionProgram: systemKeys.accountCompressionProgram,
2051
+ merkleTree:
2052
+ merkleTree !== null && merkleTree !== void 0
2053
+ ? merkleTree
2054
+ : stateless_js.defaultTestStateTreeAccounts()
2055
+ .merkleTree,
2056
+ selfProgram: this.programId,
2057
+ solPoolPda: null,
2058
+ })
4807
2059
  .instruction();
4808
2060
  return instruction;
4809
2061
  }
@@ -4812,10 +2064,22 @@ class CompressedTokenProgram {
4812
2064
  * Mint tokens from registed SPL mint account to a compressed account
4813
2065
  */
4814
2066
  static async approveAndMintTo(params) {
4815
- const { mint, feePayer, authorityTokenAccount, authority, merkleTree, toPubkey, } = params;
2067
+ const {
2068
+ mint,
2069
+ feePayer,
2070
+ authorityTokenAccount,
2071
+ authority,
2072
+ merkleTree,
2073
+ toPubkey,
2074
+ } = params;
4816
2075
  const amount = BigInt(params.amount.toString());
4817
2076
  /// 1. Mint to existing ATA of mintAuthority.
4818
- const splMintToInstruction = createMintToInstruction(mint, authorityTokenAccount, authority, amount);
2077
+ const splMintToInstruction = splToken.createMintToInstruction(
2078
+ mint,
2079
+ authorityTokenAccount,
2080
+ authority,
2081
+ amount,
2082
+ );
4819
2083
  /// 2. Compress from mint authority ATA to recipient compressed account
4820
2084
  const compressInstruction = await this.compress({
4821
2085
  payer: feePayer,
@@ -4832,15 +2096,33 @@ class CompressedTokenProgram {
4832
2096
  * Construct transfer instruction for compressed tokens
4833
2097
  */
4834
2098
  static async transfer(params) {
4835
- const { payer, inputCompressedTokenAccounts, recentInputStateRootIndices, recentValidityProof, amount, outputStateTrees, toAddress, } = params;
4836
- const tokenTransferOutputs = createTransferOutputState(inputCompressedTokenAccounts, toAddress, amount);
4837
- const { inputTokenDataWithContext, packedOutputTokenData, remainingAccountMetas, } = packCompressedTokenAccounts({
2099
+ const {
2100
+ payer,
2101
+ inputCompressedTokenAccounts,
2102
+ recentInputStateRootIndices,
2103
+ recentValidityProof,
2104
+ amount,
2105
+ outputStateTrees,
2106
+ toAddress,
2107
+ } = params;
2108
+ const tokenTransferOutputs = createTransferOutputState(
2109
+ inputCompressedTokenAccounts,
2110
+ toAddress,
2111
+ amount,
2112
+ );
2113
+ const {
2114
+ inputTokenDataWithContext,
2115
+ packedOutputTokenData,
2116
+ remainingAccountMetas,
2117
+ } = packCompressedTokenAccounts({
4838
2118
  inputCompressedTokenAccounts,
4839
2119
  outputStateTrees,
4840
2120
  rootIndices: recentInputStateRootIndices,
4841
2121
  tokenTransferOutputs,
4842
2122
  });
4843
- const { mint, currentOwner } = parseTokenData(inputCompressedTokenAccounts);
2123
+ const { mint, currentOwner } = parseTokenData(
2124
+ inputCompressedTokenAccounts,
2125
+ );
4844
2126
  const data = {
4845
2127
  proof: recentValidityProof,
4846
2128
  mint,
@@ -4852,24 +2134,32 @@ class CompressedTokenProgram {
4852
2134
  cpiContext: null,
4853
2135
  lamportsChangeAccountMerkleTreeIndex: null,
4854
2136
  };
4855
- const encodedData = this.program.coder.types.encode('CompressedTokenInstructionDataTransfer', data);
4856
- const { accountCompressionAuthority, noopProgram, registeredProgramPda, accountCompressionProgram, } = stateless_js.defaultStaticAccountsStruct();
2137
+ const encodedData = this.program.coder.types.encode(
2138
+ 'CompressedTokenInstructionDataTransfer',
2139
+ data,
2140
+ );
2141
+ const {
2142
+ accountCompressionAuthority,
2143
+ noopProgram,
2144
+ registeredProgramPda,
2145
+ accountCompressionProgram,
2146
+ } = stateless_js.defaultStaticAccountsStruct();
4857
2147
  const instruction = await this.program.methods
4858
2148
  .transfer(encodedData)
4859
2149
  .accounts({
4860
- feePayer: payer,
4861
- authority: currentOwner,
4862
- cpiAuthorityPda: this.deriveCpiAuthorityPda,
4863
- lightSystemProgram: stateless_js.LightSystemProgram.programId,
4864
- registeredProgramPda: registeredProgramPda,
4865
- noopProgram: noopProgram,
4866
- accountCompressionAuthority: accountCompressionAuthority,
4867
- accountCompressionProgram: accountCompressionProgram,
4868
- selfProgram: this.programId,
4869
- tokenPoolPda: null,
4870
- compressOrDecompressTokenAccount: null,
4871
- tokenProgram: null,
4872
- })
2150
+ feePayer: payer,
2151
+ authority: currentOwner,
2152
+ cpiAuthorityPda: this.deriveCpiAuthorityPda,
2153
+ lightSystemProgram: stateless_js.LightSystemProgram.programId,
2154
+ registeredProgramPda: registeredProgramPda,
2155
+ noopProgram: noopProgram,
2156
+ accountCompressionAuthority: accountCompressionAuthority,
2157
+ accountCompressionProgram: accountCompressionProgram,
2158
+ selfProgram: this.programId,
2159
+ tokenPoolPda: null,
2160
+ compressOrDecompressTokenAccount: null,
2161
+ tokenProgram: null,
2162
+ })
4873
2163
  .remainingAccounts(remainingAccountMetas)
4874
2164
  .instruction();
4875
2165
  return instruction;
@@ -4878,12 +2168,14 @@ class CompressedTokenProgram {
4878
2168
  * Create lookup table instructions for the token program's default accounts.
4879
2169
  */
4880
2170
  static async createTokenProgramLookupTable(params) {
4881
- const { authority, mints, recentSlot, payer, remainingAccounts } = params;
4882
- const [createInstruction, lookupTableAddress] = web3_js.AddressLookupTableProgram.createLookupTable({
4883
- authority,
4884
- payer: authority,
4885
- recentSlot,
4886
- });
2171
+ const { authority, mints, recentSlot, payer, remainingAccounts } =
2172
+ params;
2173
+ const [createInstruction, lookupTableAddress] =
2174
+ web3_js.AddressLookupTableProgram.createLookupTable({
2175
+ authority,
2176
+ payer: authority,
2177
+ recentSlot,
2178
+ });
4887
2179
  let optionalMintKeys = [];
4888
2180
  if (mints) {
4889
2181
  optionalMintKeys = [
@@ -4891,28 +2183,35 @@ class CompressedTokenProgram {
4891
2183
  ...mints.map(mint => this.deriveTokenPoolPda(mint)),
4892
2184
  ];
4893
2185
  }
4894
- const extendInstruction = web3_js.AddressLookupTableProgram.extendLookupTable({
4895
- payer,
4896
- authority,
4897
- lookupTable: lookupTableAddress,
4898
- addresses: [
4899
- this.deriveCpiAuthorityPda,
4900
- stateless_js.LightSystemProgram.programId,
4901
- stateless_js.defaultStaticAccountsStruct().registeredProgramPda,
4902
- stateless_js.defaultStaticAccountsStruct().noopProgram,
4903
- stateless_js.defaultStaticAccountsStruct().accountCompressionAuthority,
4904
- stateless_js.defaultStaticAccountsStruct().accountCompressionProgram,
4905
- stateless_js.defaultTestStateTreeAccounts().merkleTree,
4906
- stateless_js.defaultTestStateTreeAccounts().nullifierQueue,
4907
- stateless_js.defaultTestStateTreeAccounts().addressTree,
4908
- stateless_js.defaultTestStateTreeAccounts().addressQueue,
4909
- this.programId,
4910
- TOKEN_PROGRAM_ID,
2186
+ const extendInstruction =
2187
+ web3_js.AddressLookupTableProgram.extendLookupTable({
2188
+ payer,
4911
2189
  authority,
4912
- ...optionalMintKeys,
4913
- ...(remainingAccounts !== null && remainingAccounts !== void 0 ? remainingAccounts : []),
4914
- ],
4915
- });
2190
+ lookupTable: lookupTableAddress,
2191
+ addresses: [
2192
+ this.deriveCpiAuthorityPda,
2193
+ stateless_js.LightSystemProgram.programId,
2194
+ stateless_js.defaultStaticAccountsStruct()
2195
+ .registeredProgramPda,
2196
+ stateless_js.defaultStaticAccountsStruct().noopProgram,
2197
+ stateless_js.defaultStaticAccountsStruct()
2198
+ .accountCompressionAuthority,
2199
+ stateless_js.defaultStaticAccountsStruct()
2200
+ .accountCompressionProgram,
2201
+ stateless_js.defaultTestStateTreeAccounts().merkleTree,
2202
+ stateless_js.defaultTestStateTreeAccounts().nullifierQueue,
2203
+ stateless_js.defaultTestStateTreeAccounts().addressTree,
2204
+ stateless_js.defaultTestStateTreeAccounts().addressQueue,
2205
+ this.programId,
2206
+ splToken.TOKEN_PROGRAM_ID,
2207
+ authority,
2208
+ ...optionalMintKeys,
2209
+ ...(remainingAccounts !== null &&
2210
+ remainingAccounts !== void 0
2211
+ ? remainingAccounts
2212
+ : []),
2213
+ ],
2214
+ });
4916
2215
  return {
4917
2216
  instructions: [createInstruction, extendInstruction],
4918
2217
  address: lookupTableAddress,
@@ -4923,14 +2222,19 @@ class CompressedTokenProgram {
4923
2222
  * @returns compressInstruction
4924
2223
  */
4925
2224
  static async compress(params) {
4926
- const { payer, owner, source, toAddress, mint, outputStateTree } = params;
2225
+ const { payer, owner, source, toAddress, mint, outputStateTree } =
2226
+ params;
4927
2227
  if (Array.isArray(params.amount) !== Array.isArray(params.toAddress)) {
4928
- throw new Error('Both amount and toAddress must be arrays or both must be single values');
2228
+ throw new Error(
2229
+ 'Both amount and toAddress must be arrays or both must be single values',
2230
+ );
4929
2231
  }
4930
2232
  let tokenTransferOutputs;
4931
2233
  if (Array.isArray(params.amount) && Array.isArray(params.toAddress)) {
4932
2234
  if (params.amount.length !== params.toAddress.length) {
4933
- throw new Error('Amount and toAddress arrays must have the same length');
2235
+ throw new Error(
2236
+ 'Amount and toAddress arrays must have the same length',
2237
+ );
4934
2238
  }
4935
2239
  tokenTransferOutputs = params.amount.map((amt, index) => {
4936
2240
  const amount = stateless_js.bn(amt);
@@ -4941,8 +2245,7 @@ class CompressedTokenProgram {
4941
2245
  tlv: null,
4942
2246
  };
4943
2247
  });
4944
- }
4945
- else {
2248
+ } else {
4946
2249
  tokenTransferOutputs = [
4947
2250
  {
4948
2251
  owner: toAddress,
@@ -4952,7 +2255,11 @@ class CompressedTokenProgram {
4952
2255
  },
4953
2256
  ];
4954
2257
  }
4955
- const { inputTokenDataWithContext, packedOutputTokenData, remainingAccountMetas, } = packCompressedTokenAccounts({
2258
+ const {
2259
+ inputTokenDataWithContext,
2260
+ packedOutputTokenData,
2261
+ remainingAccountMetas,
2262
+ } = packCompressedTokenAccounts({
4956
2263
  inputCompressedTokenAccounts: [],
4957
2264
  outputStateTrees: outputStateTree,
4958
2265
  rootIndices: [],
@@ -4966,30 +2273,40 @@ class CompressedTokenProgram {
4966
2273
  outputCompressedAccounts: packedOutputTokenData,
4967
2274
  compressOrDecompressAmount: Array.isArray(params.amount)
4968
2275
  ? params.amount
4969
- .map(amt => new anchor.BN(amt))
4970
- .reduce((sum, amt) => sum.add(amt), new anchor.BN(0))
2276
+ .map(amt => new anchor.BN(amt))
2277
+ .reduce((sum, amt) => sum.add(amt), new anchor.BN(0))
4971
2278
  : new anchor.BN(params.amount),
4972
2279
  isCompress: true,
4973
2280
  cpiContext: null,
4974
2281
  lamportsChangeAccountMerkleTreeIndex: null,
4975
2282
  };
4976
- const encodedData = this.program.coder.types.encode('CompressedTokenInstructionDataTransfer', data);
2283
+ const encodedData = this.program.coder.types.encode(
2284
+ 'CompressedTokenInstructionDataTransfer',
2285
+ data,
2286
+ );
4977
2287
  const instruction = await this.program.methods
4978
2288
  .transfer(encodedData)
4979
2289
  .accounts({
4980
- feePayer: payer,
4981
- authority: owner,
4982
- cpiAuthorityPda: this.deriveCpiAuthorityPda,
4983
- lightSystemProgram: stateless_js.LightSystemProgram.programId,
4984
- registeredProgramPda: stateless_js.defaultStaticAccountsStruct().registeredProgramPda,
4985
- noopProgram: stateless_js.defaultStaticAccountsStruct().noopProgram,
4986
- accountCompressionAuthority: stateless_js.defaultStaticAccountsStruct().accountCompressionAuthority,
4987
- accountCompressionProgram: stateless_js.defaultStaticAccountsStruct().accountCompressionProgram,
4988
- selfProgram: this.programId,
4989
- tokenPoolPda: this.deriveTokenPoolPda(mint),
4990
- compressOrDecompressTokenAccount: source, // token
4991
- tokenProgram: TOKEN_PROGRAM_ID,
4992
- })
2290
+ feePayer: payer,
2291
+ authority: owner,
2292
+ cpiAuthorityPda: this.deriveCpiAuthorityPda,
2293
+ lightSystemProgram: stateless_js.LightSystemProgram.programId,
2294
+ registeredProgramPda:
2295
+ stateless_js.defaultStaticAccountsStruct()
2296
+ .registeredProgramPda,
2297
+ noopProgram:
2298
+ stateless_js.defaultStaticAccountsStruct().noopProgram,
2299
+ accountCompressionAuthority:
2300
+ stateless_js.defaultStaticAccountsStruct()
2301
+ .accountCompressionAuthority,
2302
+ accountCompressionProgram:
2303
+ stateless_js.defaultStaticAccountsStruct()
2304
+ .accountCompressionProgram,
2305
+ selfProgram: this.programId,
2306
+ tokenPoolPda: this.deriveTokenPoolPda(mint),
2307
+ compressOrDecompressTokenAccount: source, // token
2308
+ tokenProgram: splToken.TOKEN_PROGRAM_ID,
2309
+ })
4993
2310
  .remainingAccounts(remainingAccountMetas)
4994
2311
  .instruction();
4995
2312
  return instruction;
@@ -4998,17 +2315,33 @@ class CompressedTokenProgram {
4998
2315
  * Construct decompress instruction
4999
2316
  */
5000
2317
  static async decompress(params) {
5001
- const { payer, inputCompressedTokenAccounts, toAddress, outputStateTree, recentValidityProof, recentInputStateRootIndices, } = params;
2318
+ const {
2319
+ payer,
2320
+ inputCompressedTokenAccounts,
2321
+ toAddress,
2322
+ outputStateTree,
2323
+ recentValidityProof,
2324
+ recentInputStateRootIndices,
2325
+ } = params;
5002
2326
  const amount = stateless_js.bn(params.amount);
5003
- const tokenTransferOutputs = createDecompressOutputState(inputCompressedTokenAccounts, amount);
2327
+ const tokenTransferOutputs = createDecompressOutputState(
2328
+ inputCompressedTokenAccounts,
2329
+ amount,
2330
+ );
5004
2331
  /// Pack
5005
- const { inputTokenDataWithContext, packedOutputTokenData, remainingAccountMetas, } = packCompressedTokenAccounts({
2332
+ const {
2333
+ inputTokenDataWithContext,
2334
+ packedOutputTokenData,
2335
+ remainingAccountMetas,
2336
+ } = packCompressedTokenAccounts({
5006
2337
  inputCompressedTokenAccounts,
5007
2338
  outputStateTrees: outputStateTree,
5008
2339
  rootIndices: recentInputStateRootIndices,
5009
2340
  tokenTransferOutputs: tokenTransferOutputs,
5010
2341
  });
5011
- const { mint, currentOwner } = parseTokenData(inputCompressedTokenAccounts);
2342
+ const { mint, currentOwner } = parseTokenData(
2343
+ inputCompressedTokenAccounts,
2344
+ );
5012
2345
  const data = {
5013
2346
  proof: recentValidityProof,
5014
2347
  mint,
@@ -5020,30 +2353,45 @@ class CompressedTokenProgram {
5020
2353
  cpiContext: null,
5021
2354
  lamportsChangeAccountMerkleTreeIndex: null,
5022
2355
  };
5023
- const encodedData = this.program.coder.types.encode('CompressedTokenInstructionDataTransfer', data);
5024
- const { accountCompressionAuthority, noopProgram, registeredProgramPda, accountCompressionProgram, } = stateless_js.defaultStaticAccountsStruct();
2356
+ const encodedData = this.program.coder.types.encode(
2357
+ 'CompressedTokenInstructionDataTransfer',
2358
+ data,
2359
+ );
2360
+ const {
2361
+ accountCompressionAuthority,
2362
+ noopProgram,
2363
+ registeredProgramPda,
2364
+ accountCompressionProgram,
2365
+ } = stateless_js.defaultStaticAccountsStruct();
5025
2366
  const instruction = await this.program.methods
5026
2367
  .transfer(encodedData)
5027
2368
  .accounts({
5028
- feePayer: payer,
5029
- authority: currentOwner,
5030
- cpiAuthorityPda: this.deriveCpiAuthorityPda,
5031
- lightSystemProgram: stateless_js.LightSystemProgram.programId,
5032
- registeredProgramPda: registeredProgramPda,
5033
- noopProgram: noopProgram,
5034
- accountCompressionAuthority: accountCompressionAuthority,
5035
- accountCompressionProgram: accountCompressionProgram,
5036
- selfProgram: this.programId,
5037
- tokenPoolPda: this.deriveTokenPoolPda(mint),
5038
- compressOrDecompressTokenAccount: toAddress,
5039
- tokenProgram: TOKEN_PROGRAM_ID,
5040
- })
2369
+ feePayer: payer,
2370
+ authority: currentOwner,
2371
+ cpiAuthorityPda: this.deriveCpiAuthorityPda,
2372
+ lightSystemProgram: stateless_js.LightSystemProgram.programId,
2373
+ registeredProgramPda: registeredProgramPda,
2374
+ noopProgram: noopProgram,
2375
+ accountCompressionAuthority: accountCompressionAuthority,
2376
+ accountCompressionProgram: accountCompressionProgram,
2377
+ selfProgram: this.programId,
2378
+ tokenPoolPda: this.deriveTokenPoolPda(mint),
2379
+ compressOrDecompressTokenAccount: toAddress,
2380
+ tokenProgram: splToken.TOKEN_PROGRAM_ID,
2381
+ })
5041
2382
  .remainingAccounts(remainingAccountMetas)
5042
2383
  .instruction();
5043
2384
  return instruction;
5044
2385
  }
5045
2386
  static async mergeTokenAccounts(params) {
5046
- const { payer, owner, inputCompressedTokenAccounts, outputStateTree, recentValidityProof, recentInputStateRootIndices, } = params;
2387
+ const {
2388
+ payer,
2389
+ owner,
2390
+ inputCompressedTokenAccounts,
2391
+ outputStateTree,
2392
+ recentValidityProof,
2393
+ recentInputStateRootIndices,
2394
+ } = params;
5047
2395
  if (inputCompressedTokenAccounts.length > 3) {
5048
2396
  throw new Error('Cannot merge more than 3 token accounts at once');
5049
2397
  }
@@ -5051,7 +2399,10 @@ class CompressedTokenProgram {
5051
2399
  payer,
5052
2400
  inputCompressedTokenAccounts,
5053
2401
  toAddress: owner,
5054
- amount: inputCompressedTokenAccounts.reduce((sum, account) => sum.add(account.parsed.amount), new anchor.BN(0)),
2402
+ amount: inputCompressedTokenAccounts.reduce(
2403
+ (sum, account) => sum.add(account.parsed.amount),
2404
+ new anchor.BN(0),
2405
+ ),
5055
2406
  outputStateTrees: outputStateTree,
5056
2407
  recentInputStateRootIndices,
5057
2408
  recentValidityProof,
@@ -5059,7 +2410,14 @@ class CompressedTokenProgram {
5059
2410
  return [ix];
5060
2411
  }
5061
2412
  static async compressSplTokenAccount(params) {
5062
- const { feePayer, authority, tokenAccount, mint, remainingAmount, outputStateTree, } = params;
2413
+ const {
2414
+ feePayer,
2415
+ authority,
2416
+ tokenAccount,
2417
+ mint,
2418
+ remainingAmount,
2419
+ outputStateTree,
2420
+ } = params;
5063
2421
  const remainingAccountMetas = [
5064
2422
  {
5065
2423
  pubkey: outputStateTree,
@@ -5068,22 +2426,35 @@ class CompressedTokenProgram {
5068
2426
  },
5069
2427
  ];
5070
2428
  const instruction = await this.program.methods
5071
- .compressSplTokenAccount(authority, remainingAmount !== null && remainingAmount !== void 0 ? remainingAmount : null, null)
2429
+ .compressSplTokenAccount(
2430
+ authority,
2431
+ remainingAmount !== null && remainingAmount !== void 0
2432
+ ? remainingAmount
2433
+ : null,
2434
+ null,
2435
+ )
5072
2436
  .accounts({
5073
- feePayer,
5074
- authority,
5075
- cpiAuthorityPda: this.deriveCpiAuthorityPda,
5076
- lightSystemProgram: stateless_js.LightSystemProgram.programId,
5077
- registeredProgramPda: stateless_js.defaultStaticAccountsStruct().registeredProgramPda,
5078
- noopProgram: stateless_js.defaultStaticAccountsStruct().noopProgram,
5079
- accountCompressionAuthority: stateless_js.defaultStaticAccountsStruct().accountCompressionAuthority,
5080
- accountCompressionProgram: stateless_js.defaultStaticAccountsStruct().accountCompressionProgram,
5081
- selfProgram: this.programId,
5082
- tokenPoolPda: this.deriveTokenPoolPda(mint),
5083
- compressOrDecompressTokenAccount: tokenAccount,
5084
- tokenProgram: TOKEN_PROGRAM_ID,
5085
- systemProgram: web3_js.SystemProgram.programId,
5086
- })
2437
+ feePayer,
2438
+ authority,
2439
+ cpiAuthorityPda: this.deriveCpiAuthorityPda,
2440
+ lightSystemProgram: stateless_js.LightSystemProgram.programId,
2441
+ registeredProgramPda:
2442
+ stateless_js.defaultStaticAccountsStruct()
2443
+ .registeredProgramPda,
2444
+ noopProgram:
2445
+ stateless_js.defaultStaticAccountsStruct().noopProgram,
2446
+ accountCompressionAuthority:
2447
+ stateless_js.defaultStaticAccountsStruct()
2448
+ .accountCompressionAuthority,
2449
+ accountCompressionProgram:
2450
+ stateless_js.defaultStaticAccountsStruct()
2451
+ .accountCompressionProgram,
2452
+ selfProgram: this.programId,
2453
+ tokenPoolPda: this.deriveTokenPoolPda(mint),
2454
+ compressOrDecompressTokenAccount: tokenAccount,
2455
+ tokenProgram: splToken.TOKEN_PROGRAM_ID,
2456
+ systemProgram: web3_js.SystemProgram.programId,
2457
+ })
5087
2458
  .remainingAccounts(remainingAccountMetas)
5088
2459
  .instruction();
5089
2460
  return instruction;
@@ -5092,7 +2463,9 @@ class CompressedTokenProgram {
5092
2463
  /**
5093
2464
  * Public key that identifies the CompressedPda program
5094
2465
  */
5095
- CompressedTokenProgram.programId = new web3_js.PublicKey('cTokenmWW8bLPjZEBAUgYy3zKxQZW6VKi7bqNFEVv3m');
2466
+ CompressedTokenProgram.programId = new web3_js.PublicKey(
2467
+ 'cTokenmWW8bLPjZEBAUgYy3zKxQZW6VKi7bqNFEVv3m',
2468
+ );
5096
2469
  CompressedTokenProgram._program = null;
5097
2470
 
5098
2471
  /**
@@ -5110,8 +2483,23 @@ CompressedTokenProgram._program = null;
5110
2483
  *
5111
2484
  * @return Signature of the confirmed transaction
5112
2485
  */
5113
- async function approveAndMintTo(rpc, payer, mint, destination, authority, amount, merkleTree, confirmOptions) {
5114
- const authorityTokenAccount = await getOrCreateAssociatedTokenAccount(rpc, payer, mint, authority.publicKey);
2486
+ async function approveAndMintTo(
2487
+ rpc,
2488
+ payer,
2489
+ mint,
2490
+ destination,
2491
+ authority,
2492
+ amount,
2493
+ merkleTree,
2494
+ confirmOptions,
2495
+ ) {
2496
+ const authorityTokenAccount =
2497
+ await splToken.getOrCreateAssociatedTokenAccount(
2498
+ rpc,
2499
+ payer,
2500
+ mint,
2501
+ authority.publicKey,
2502
+ );
5115
2503
  const ixs = await CompressedTokenProgram.approveAndMintTo({
5116
2504
  feePayer: payer.publicKey,
5117
2505
  mint,
@@ -5123,10 +2511,17 @@ async function approveAndMintTo(rpc, payer, mint, destination, authority, amount
5123
2511
  });
5124
2512
  const { blockhash } = await rpc.getLatestBlockhash();
5125
2513
  const additionalSigners = stateless_js.dedupeSigner(payer, [authority]);
5126
- const tx = stateless_js.buildAndSignTx([
5127
- web3_js.ComputeBudgetProgram.setComputeUnitLimit({ units: 1000000 }),
5128
- ...ixs,
5129
- ], payer, blockhash, additionalSigners);
2514
+ const tx = stateless_js.buildAndSignTx(
2515
+ [
2516
+ web3_js.ComputeBudgetProgram.setComputeUnitLimit({
2517
+ units: 1000000,
2518
+ }),
2519
+ ...ixs,
2520
+ ],
2521
+ payer,
2522
+ blockhash,
2523
+ additionalSigners,
2524
+ );
5130
2525
  const txId = await stateless_js.sendAndConfirmTx(rpc, tx, confirmOptions);
5131
2526
  return txId;
5132
2527
  }
@@ -5149,7 +2544,17 @@ async function approveAndMintTo(rpc, payer, mint, destination, authority, amount
5149
2544
  *
5150
2545
  * @return Signature of the confirmed transaction
5151
2546
  */
5152
- async function compress(rpc, payer, mint, amount, owner, sourceTokenAccount, toAddress, merkleTree, confirmOptions) {
2547
+ async function compress(
2548
+ rpc,
2549
+ payer,
2550
+ mint,
2551
+ amount,
2552
+ owner,
2553
+ sourceTokenAccount,
2554
+ toAddress,
2555
+ merkleTree,
2556
+ confirmOptions,
2557
+ ) {
5153
2558
  const compressIx = await CompressedTokenProgram.compress({
5154
2559
  payer: payer.publicKey,
5155
2560
  owner: owner.publicKey,
@@ -5161,13 +2566,23 @@ async function compress(rpc, payer, mint, amount, owner, sourceTokenAccount, toA
5161
2566
  });
5162
2567
  const blockhashCtx = await rpc.getLatestBlockhash();
5163
2568
  const additionalSigners = stateless_js.dedupeSigner(payer, [owner]);
5164
- const signedTx = stateless_js.buildAndSignTx([
5165
- web3_js.ComputeBudgetProgram.setComputeUnitLimit({
5166
- units: 1000000,
5167
- }),
5168
- compressIx,
5169
- ], payer, blockhashCtx.blockhash, additionalSigners);
5170
- const txId = await stateless_js.sendAndConfirmTx(rpc, signedTx, confirmOptions, blockhashCtx);
2569
+ const signedTx = stateless_js.buildAndSignTx(
2570
+ [
2571
+ web3_js.ComputeBudgetProgram.setComputeUnitLimit({
2572
+ units: 1000000,
2573
+ }),
2574
+ compressIx,
2575
+ ],
2576
+ payer,
2577
+ blockhashCtx.blockhash,
2578
+ additionalSigners,
2579
+ );
2580
+ const txId = await stateless_js.sendAndConfirmTx(
2581
+ rpc,
2582
+ signedTx,
2583
+ confirmOptions,
2584
+ blockhashCtx,
2585
+ );
5171
2586
  return txId;
5172
2587
  }
5173
2588
 
@@ -5188,15 +2603,33 @@ async function compress(rpc, payer, mint, amount, owner, sourceTokenAccount, toA
5188
2603
  *
5189
2604
  * @return Signature of the confirmed transaction
5190
2605
  */
5191
- async function transfer(rpc, payer, mint, amount, owner, toAddress,
5192
- /// TODO: allow multiple
5193
- merkleTree, confirmOptions) {
2606
+ async function transfer(
2607
+ rpc,
2608
+ payer,
2609
+ mint,
2610
+ amount,
2611
+ owner,
2612
+ toAddress,
2613
+ /// TODO: allow multiple
2614
+ merkleTree,
2615
+ confirmOptions,
2616
+ ) {
5194
2617
  amount = stateless_js.bn(amount);
5195
- const compressedTokenAccounts = await rpc.getCompressedTokenAccountsByOwner(owner.publicKey, {
5196
- mint,
5197
- });
5198
- const [inputAccounts] = selectMinCompressedTokenAccountsForTransfer(compressedTokenAccounts.items, amount);
5199
- const proof = await rpc.getValidityProof(inputAccounts.map(account => stateless_js.bn(account.compressedAccount.hash)));
2618
+ const compressedTokenAccounts = await rpc.getCompressedTokenAccountsByOwner(
2619
+ owner.publicKey,
2620
+ {
2621
+ mint,
2622
+ },
2623
+ );
2624
+ const [inputAccounts] = selectMinCompressedTokenAccountsForTransfer(
2625
+ compressedTokenAccounts.items,
2626
+ amount,
2627
+ );
2628
+ const proof = await rpc.getValidityProof(
2629
+ inputAccounts.map(account =>
2630
+ stateless_js.bn(account.compressedAccount.hash),
2631
+ ),
2632
+ );
5200
2633
  const ix = await CompressedTokenProgram.transfer({
5201
2634
  payer: payer.publicKey,
5202
2635
  inputCompressedTokenAccounts: inputAccounts,
@@ -5208,8 +2641,22 @@ merkleTree, confirmOptions) {
5208
2641
  });
5209
2642
  const { blockhash } = await rpc.getLatestBlockhash();
5210
2643
  const additionalSigners = stateless_js.dedupeSigner(payer, [owner]);
5211
- const signedTx = stateless_js.buildAndSignTx([web3_js.ComputeBudgetProgram.setComputeUnitLimit({ units: 1000000 }), ix], payer, blockhash, additionalSigners);
5212
- const txId = await stateless_js.sendAndConfirmTx(rpc, signedTx, confirmOptions);
2644
+ const signedTx = stateless_js.buildAndSignTx(
2645
+ [
2646
+ web3_js.ComputeBudgetProgram.setComputeUnitLimit({
2647
+ units: 1000000,
2648
+ }),
2649
+ ix,
2650
+ ],
2651
+ payer,
2652
+ blockhash,
2653
+ additionalSigners,
2654
+ );
2655
+ const txId = await stateless_js.sendAndConfirmTx(
2656
+ rpc,
2657
+ signedTx,
2658
+ confirmOptions,
2659
+ );
5213
2660
  return txId;
5214
2661
  }
5215
2662
  /**
@@ -5225,14 +2672,17 @@ function selectMinCompressedTokenAccountsForTransfer(accounts, transferAmount) {
5225
2672
  const selectedAccounts = [];
5226
2673
  accounts.sort((a, b) => b.parsed.amount.cmp(a.parsed.amount));
5227
2674
  for (const account of accounts) {
5228
- if (accumulatedAmount.gte(stateless_js.bn(transferAmount)))
5229
- break;
2675
+ if (accumulatedAmount.gte(stateless_js.bn(transferAmount))) break;
5230
2676
  accumulatedAmount = accumulatedAmount.add(account.parsed.amount);
5231
- accumulatedLamports = accumulatedLamports.add(account.compressedAccount.lamports);
2677
+ accumulatedLamports = accumulatedLamports.add(
2678
+ account.compressedAccount.lamports,
2679
+ );
5232
2680
  selectedAccounts.push(account);
5233
2681
  }
5234
2682
  if (accumulatedAmount.lt(stateless_js.bn(transferAmount))) {
5235
- throw new Error(`Not enough balance for transfer. Required: ${transferAmount.toString()}, available: ${accumulatedAmount.toString()}`);
2683
+ throw new Error(
2684
+ `Not enough balance for transfer. Required: ${transferAmount.toString()}, available: ${accumulatedAmount.toString()}`,
2685
+ );
5236
2686
  }
5237
2687
  return [
5238
2688
  selectedAccounts,
@@ -5259,16 +2709,34 @@ function selectMinCompressedTokenAccountsForTransfer(accounts, transferAmount) {
5259
2709
  *
5260
2710
  * @return Signature of the confirmed transaction
5261
2711
  */
5262
- async function decompress(rpc, payer, mint, amount, owner, toAddress,
5263
- /// TODO: allow multiple
5264
- merkleTree, confirmOptions) {
2712
+ async function decompress(
2713
+ rpc,
2714
+ payer,
2715
+ mint,
2716
+ amount,
2717
+ owner,
2718
+ toAddress,
2719
+ /// TODO: allow multiple
2720
+ merkleTree,
2721
+ confirmOptions,
2722
+ ) {
5265
2723
  amount = stateless_js.bn(amount);
5266
- const compressedTokenAccounts = await rpc.getCompressedTokenAccountsByOwner(owner.publicKey, {
5267
- mint,
5268
- });
2724
+ const compressedTokenAccounts = await rpc.getCompressedTokenAccountsByOwner(
2725
+ owner.publicKey,
2726
+ {
2727
+ mint,
2728
+ },
2729
+ );
5269
2730
  /// TODO: consider using a different selection algorithm
5270
- const [inputAccounts] = selectMinCompressedTokenAccountsForTransfer(compressedTokenAccounts.items, amount);
5271
- const proof = await rpc.getValidityProof(inputAccounts.map(account => stateless_js.bn(account.compressedAccount.hash)));
2731
+ const [inputAccounts] = selectMinCompressedTokenAccountsForTransfer(
2732
+ compressedTokenAccounts.items,
2733
+ amount,
2734
+ );
2735
+ const proof = await rpc.getValidityProof(
2736
+ inputAccounts.map(account =>
2737
+ stateless_js.bn(account.compressedAccount.hash),
2738
+ ),
2739
+ );
5272
2740
  const ix = await CompressedTokenProgram.decompress({
5273
2741
  payer: payer.publicKey,
5274
2742
  inputCompressedTokenAccounts: inputAccounts,
@@ -5280,8 +2748,22 @@ merkleTree, confirmOptions) {
5280
2748
  });
5281
2749
  const { blockhash } = await rpc.getLatestBlockhash();
5282
2750
  const additionalSigners = stateless_js.dedupeSigner(payer, [owner]);
5283
- const signedTx = stateless_js.buildAndSignTx([web3_js.ComputeBudgetProgram.setComputeUnitLimit({ units: 1000000 }), ix], payer, blockhash, additionalSigners);
5284
- const txId = await stateless_js.sendAndConfirmTx(rpc, signedTx, confirmOptions);
2751
+ const signedTx = stateless_js.buildAndSignTx(
2752
+ [
2753
+ web3_js.ComputeBudgetProgram.setComputeUnitLimit({
2754
+ units: 1000000,
2755
+ }),
2756
+ ix,
2757
+ ],
2758
+ payer,
2759
+ blockhash,
2760
+ additionalSigners,
2761
+ );
2762
+ const txId = await stateless_js.sendAndConfirmTx(
2763
+ rpc,
2764
+ signedTx,
2765
+ confirmOptions,
2766
+ );
5285
2767
  return txId;
5286
2768
  }
5287
2769
 
@@ -5297,8 +2779,17 @@ merkleTree, confirmOptions) {
5297
2779
  *
5298
2780
  * @return Address of the new mint and the transaction signature
5299
2781
  */
5300
- async function createMint(rpc, payer, mintAuthority, decimals, keypair = web3_js.Keypair.generate(), confirmOptions) {
5301
- const rentExemptBalance = await rpc.getMinimumBalanceForRentExemption(MINT_SIZE);
2782
+ async function createMint(
2783
+ rpc,
2784
+ payer,
2785
+ mintAuthority,
2786
+ decimals,
2787
+ keypair = web3_js.Keypair.generate(),
2788
+ confirmOptions,
2789
+ ) {
2790
+ const rentExemptBalance = await rpc.getMinimumBalanceForRentExemption(
2791
+ splToken.MINT_SIZE,
2792
+ );
5302
2793
  const ixs = await CompressedTokenProgram.createMint({
5303
2794
  feePayer: payer.publicKey,
5304
2795
  mint: keypair.publicKey,
@@ -5309,7 +2800,12 @@ async function createMint(rpc, payer, mintAuthority, decimals, keypair = web3_js
5309
2800
  });
5310
2801
  const { blockhash } = await rpc.getLatestBlockhash();
5311
2802
  const additionalSigners = stateless_js.dedupeSigner(payer, [keypair]);
5312
- const tx = stateless_js.buildAndSignTx(ixs, payer, blockhash, additionalSigners);
2803
+ const tx = stateless_js.buildAndSignTx(
2804
+ ixs,
2805
+ payer,
2806
+ blockhash,
2807
+ additionalSigners,
2808
+ );
5313
2809
  const txId = await stateless_js.sendAndConfirmTx(rpc, tx, confirmOptions);
5314
2810
  return { mint: keypair.publicKey, transactionSignature: txId };
5315
2811
  }
@@ -5331,7 +2827,16 @@ async function createMint(rpc, payer, mintAuthority, decimals, keypair = web3_js
5331
2827
  *
5332
2828
  * @return Signature of the confirmed transaction
5333
2829
  */
5334
- async function mintTo(rpc, payer, mint, destination, authority, amount, merkleTree, confirmOptions) {
2830
+ async function mintTo(
2831
+ rpc,
2832
+ payer,
2833
+ mint,
2834
+ destination,
2835
+ authority,
2836
+ amount,
2837
+ merkleTree,
2838
+ confirmOptions,
2839
+ ) {
5335
2840
  const additionalSigners = stateless_js.dedupeSigner(payer, [authority]);
5336
2841
  const ix = await CompressedTokenProgram.mintTo({
5337
2842
  feePayer: payer.publicKey,
@@ -5342,7 +2847,17 @@ async function mintTo(rpc, payer, mint, destination, authority, amount, merkleTr
5342
2847
  merkleTree,
5343
2848
  });
5344
2849
  const { blockhash } = await rpc.getLatestBlockhash();
5345
- const tx = stateless_js.buildAndSignTx([web3_js.ComputeBudgetProgram.setComputeUnitLimit({ units: 1000000 }), ix], payer, blockhash, additionalSigners);
2850
+ const tx = stateless_js.buildAndSignTx(
2851
+ [
2852
+ web3_js.ComputeBudgetProgram.setComputeUnitLimit({
2853
+ units: 1000000,
2854
+ }),
2855
+ ix,
2856
+ ],
2857
+ payer,
2858
+ blockhash,
2859
+ additionalSigners,
2860
+ );
5346
2861
  const txId = await stateless_js.sendAndConfirmTx(rpc, tx, confirmOptions);
5347
2862
  return txId;
5348
2863
  }
@@ -5360,35 +2875,67 @@ async function mintTo(rpc, payer, mint, destination, authority, amount, merkleTr
5360
2875
  *
5361
2876
  * @return Array of transaction signatures
5362
2877
  */
5363
- async function mergeTokenAccounts(rpc, payer, mint, owner, merkleTree, confirmOptions) {
5364
- const compressedTokenAccounts = await rpc.getCompressedTokenAccountsByOwner(owner.publicKey, { mint });
2878
+ async function mergeTokenAccounts(
2879
+ rpc,
2880
+ payer,
2881
+ mint,
2882
+ owner,
2883
+ merkleTree,
2884
+ confirmOptions,
2885
+ ) {
2886
+ const compressedTokenAccounts = await rpc.getCompressedTokenAccountsByOwner(
2887
+ owner.publicKey,
2888
+ { mint },
2889
+ );
5365
2890
  if (compressedTokenAccounts.items.length === 0) {
5366
- throw new Error(`No compressed token accounts found for mint ${mint.toBase58()}`);
2891
+ throw new Error(
2892
+ `No compressed token accounts found for mint ${mint.toBase58()}`,
2893
+ );
5367
2894
  }
5368
2895
  if (compressedTokenAccounts.items.length >= 6) {
5369
- throw new Error(`Too many compressed token accounts used for mint ${mint.toBase58()}`);
2896
+ throw new Error(
2897
+ `Too many compressed token accounts used for mint ${mint.toBase58()}`,
2898
+ );
5370
2899
  }
5371
2900
  const instructions = [
5372
2901
  web3_js.ComputeBudgetProgram.setComputeUnitLimit({ units: 1000000 }),
5373
2902
  ];
5374
- for (let i = 0; i < compressedTokenAccounts.items.slice(0, 6).length; i += 3) {
2903
+ for (
2904
+ let i = 0;
2905
+ i < compressedTokenAccounts.items.slice(0, 6).length;
2906
+ i += 3
2907
+ ) {
5375
2908
  const batch = compressedTokenAccounts.items.slice(i, i + 3);
5376
- const proof = await rpc.getValidityProof(batch.map(account => stateless_js.bn(account.compressedAccount.hash)));
5377
- const batchInstructions = await CompressedTokenProgram.mergeTokenAccounts({
5378
- payer: payer.publicKey,
5379
- owner: owner.publicKey,
5380
- mint,
5381
- inputCompressedTokenAccounts: batch,
5382
- outputStateTree: merkleTree,
5383
- recentValidityProof: proof.compressedProof,
5384
- recentInputStateRootIndices: proof.rootIndices,
5385
- });
2909
+ const proof = await rpc.getValidityProof(
2910
+ batch.map(account =>
2911
+ stateless_js.bn(account.compressedAccount.hash),
2912
+ ),
2913
+ );
2914
+ const batchInstructions =
2915
+ await CompressedTokenProgram.mergeTokenAccounts({
2916
+ payer: payer.publicKey,
2917
+ owner: owner.publicKey,
2918
+ mint,
2919
+ inputCompressedTokenAccounts: batch,
2920
+ outputStateTree: merkleTree,
2921
+ recentValidityProof: proof.compressedProof,
2922
+ recentInputStateRootIndices: proof.rootIndices,
2923
+ });
5386
2924
  instructions.push(...batchInstructions);
5387
2925
  }
5388
2926
  const { blockhash } = await rpc.getLatestBlockhash();
5389
2927
  const additionalSigners = stateless_js.dedupeSigner(payer, [owner]);
5390
- const signedTx = stateless_js.buildAndSignTx(instructions, payer, blockhash, additionalSigners);
5391
- const txId = await stateless_js.sendAndConfirmTx(rpc, signedTx, confirmOptions);
2928
+ const signedTx = stateless_js.buildAndSignTx(
2929
+ instructions,
2930
+ payer,
2931
+ blockhash,
2932
+ additionalSigners,
2933
+ );
2934
+ const txId = await stateless_js.sendAndConfirmTx(
2935
+ rpc,
2936
+ signedTx,
2937
+ confirmOptions,
2938
+ );
5392
2939
  return txId;
5393
2940
  }
5394
2941
 
@@ -5427,23 +2974,50 @@ async function createTokenPool(rpc, payer, mintAddress, confirmOptions) {
5427
2974
  *
5428
2975
  * @return Transaction signatures and the address of the created lookup table
5429
2976
  */
5430
- async function createTokenProgramLookupTable(rpc, payer, authority, mints, additionalAccounts) {
2977
+ async function createTokenProgramLookupTable(
2978
+ rpc,
2979
+ payer,
2980
+ authority,
2981
+ mints,
2982
+ additionalAccounts,
2983
+ ) {
5431
2984
  const recentSlot = await rpc.getSlot('finalized');
5432
- const { instructions, address } = await CompressedTokenProgram.createTokenProgramLookupTable({
5433
- payer: payer.publicKey,
5434
- authority: authority.publicKey,
5435
- mints,
5436
- remainingAccounts: additionalAccounts,
5437
- recentSlot,
5438
- });
2985
+ const { instructions, address } =
2986
+ await CompressedTokenProgram.createTokenProgramLookupTable({
2987
+ payer: payer.publicKey,
2988
+ authority: authority.publicKey,
2989
+ mints,
2990
+ remainingAccounts: additionalAccounts,
2991
+ recentSlot,
2992
+ });
5439
2993
  const additionalSigners = stateless_js.dedupeSigner(payer, [authority]);
5440
2994
  const blockhashCtx = await rpc.getLatestBlockhash();
5441
- const signedTx = stateless_js.buildAndSignTx([instructions[0]], payer, blockhashCtx.blockhash, additionalSigners);
2995
+ const signedTx = stateless_js.buildAndSignTx(
2996
+ [instructions[0]],
2997
+ payer,
2998
+ blockhashCtx.blockhash,
2999
+ additionalSigners,
3000
+ );
5442
3001
  /// Must wait for the first instruction to be finalized.
5443
- const txId = await stateless_js.sendAndConfirmTx(rpc, signedTx, { commitment: 'finalized' }, blockhashCtx);
3002
+ const txId = await stateless_js.sendAndConfirmTx(
3003
+ rpc,
3004
+ signedTx,
3005
+ { commitment: 'finalized' },
3006
+ blockhashCtx,
3007
+ );
5444
3008
  const blockhashCtx2 = await rpc.getLatestBlockhash();
5445
- const signedTx2 = stateless_js.buildAndSignTx([instructions[1]], payer, blockhashCtx2.blockhash, additionalSigners);
5446
- const txId2 = await stateless_js.sendAndConfirmTx(rpc, signedTx2, { commitment: 'finalized' }, blockhashCtx2);
3009
+ const signedTx2 = stateless_js.buildAndSignTx(
3010
+ [instructions[1]],
3011
+ payer,
3012
+ blockhashCtx2.blockhash,
3013
+ additionalSigners,
3014
+ );
3015
+ const txId2 = await stateless_js.sendAndConfirmTx(
3016
+ rpc,
3017
+ signedTx2,
3018
+ { commitment: 'finalized' },
3019
+ blockhashCtx2,
3020
+ );
5447
3021
  return { txIds: [txId, txId2], address };
5448
3022
  }
5449
3023
 
@@ -5461,7 +3035,16 @@ async function createTokenProgramLookupTable(rpc, payer, authority, mints, addit
5461
3035
  *
5462
3036
  * @return Signature of the confirmed transaction
5463
3037
  */
5464
- async function compressSplTokenAccount(rpc, payer, mint, owner, tokenAccount, outputStateTree, remainingAmount, confirmOptions) {
3038
+ async function compressSplTokenAccount(
3039
+ rpc,
3040
+ payer,
3041
+ mint,
3042
+ owner,
3043
+ tokenAccount,
3044
+ outputStateTree,
3045
+ remainingAmount,
3046
+ confirmOptions,
3047
+ ) {
5465
3048
  const compressIx = await CompressedTokenProgram.compressSplTokenAccount({
5466
3049
  feePayer: payer.publicKey,
5467
3050
  authority: owner.publicKey,
@@ -5472,13 +3055,23 @@ async function compressSplTokenAccount(rpc, payer, mint, owner, tokenAccount, ou
5472
3055
  });
5473
3056
  const blockhashCtx = await rpc.getLatestBlockhash();
5474
3057
  const additionalSigners = stateless_js.dedupeSigner(payer, [owner]);
5475
- const signedTx = stateless_js.buildAndSignTx([
5476
- web3_js.ComputeBudgetProgram.setComputeUnitLimit({
5477
- units: 1000000,
5478
- }),
5479
- compressIx,
5480
- ], payer, blockhashCtx.blockhash, additionalSigners);
5481
- const txId = await stateless_js.sendAndConfirmTx(rpc, signedTx, confirmOptions, blockhashCtx);
3058
+ const signedTx = stateless_js.buildAndSignTx(
3059
+ [
3060
+ web3_js.ComputeBudgetProgram.setComputeUnitLimit({
3061
+ units: 1000000,
3062
+ }),
3063
+ compressIx,
3064
+ ],
3065
+ payer,
3066
+ blockhashCtx.blockhash,
3067
+ additionalSigners,
3068
+ );
3069
+ const txId = await stateless_js.sendAndConfirmTx(
3070
+ rpc,
3071
+ signedTx,
3072
+ confirmOptions,
3073
+ blockhashCtx,
3074
+ );
5482
3075
  return txId;
5483
3076
  }
5484
3077
 
@@ -5500,7 +3093,8 @@ exports.mergeTokenAccounts = mergeTokenAccounts;
5500
3093
  exports.mintTo = mintTo;
5501
3094
  exports.packCompressedTokenAccounts = packCompressedTokenAccounts;
5502
3095
  exports.parseTokenData = parseTokenData;
5503
- exports.selectMinCompressedTokenAccountsForTransfer = selectMinCompressedTokenAccountsForTransfer;
3096
+ exports.selectMinCompressedTokenAccountsForTransfer =
3097
+ selectMinCompressedTokenAccountsForTransfer;
5504
3098
  exports.sumUpTokenAmount = sumUpTokenAmount;
5505
3099
  exports.transfer = transfer;
5506
3100
  exports.validateSameTokenOwner = validateSameTokenOwner;