@lightprotocol/compressed-token 0.15.4 → 0.17.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.
- package/dist/cjs/browser/index.cjs +273 -4975
- package/dist/cjs/browser/index.cjs.map +1 -1
- package/dist/cjs/node/index.cjs +330 -3009
- package/dist/cjs/node/index.cjs.map +1 -1
- package/dist/es/browser/index.js +271 -4974
- package/dist/es/browser/index.js.map +1 -1
- package/dist/types/index.d.ts +174 -20
- package/package.json +6 -6
package/dist/cjs/node/index.cjs
CHANGED
|
@@ -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
|
|
6
|
+
var splToken = require('@solana/spl-token');
|
|
7
7
|
|
|
8
8
|
const IDL = {
|
|
9
9
|
version: '1.2.0',
|
|
@@ -89,9 +89,6 @@ const IDL = {
|
|
|
89
89
|
name: 'tokenPoolPda',
|
|
90
90
|
isMut: true,
|
|
91
91
|
isSigner: false,
|
|
92
|
-
docs: [
|
|
93
|
-
'account to a token account of a different mint will fail',
|
|
94
|
-
],
|
|
95
92
|
},
|
|
96
93
|
{
|
|
97
94
|
name: 'tokenProgram',
|
|
@@ -169,6 +166,11 @@ const IDL = {
|
|
|
169
166
|
},
|
|
170
167
|
{
|
|
171
168
|
name: 'compressSplTokenAccount',
|
|
169
|
+
docs: [
|
|
170
|
+
'Compresses the balance of an spl token account sub an optional remaining',
|
|
171
|
+
'amount. This instruction does not close the spl token account. To close',
|
|
172
|
+
'the account bundle a close spl account instruction in your transaction.',
|
|
173
|
+
],
|
|
172
174
|
accounts: [
|
|
173
175
|
{
|
|
174
176
|
name: 'feePayer',
|
|
@@ -1656,23 +1658,141 @@ const IDL = {
|
|
|
1656
1658
|
errors: [
|
|
1657
1659
|
{
|
|
1658
1660
|
code: 6000,
|
|
1659
|
-
name: '
|
|
1660
|
-
msg: '
|
|
1661
|
+
name: 'PublicKeyAmountMissmatch',
|
|
1662
|
+
msg: 'public keys and amounts must be of same length',
|
|
1661
1663
|
},
|
|
1662
1664
|
{
|
|
1663
1665
|
code: 6001,
|
|
1664
|
-
name: '
|
|
1665
|
-
msg: '
|
|
1666
|
+
name: 'ComputeInputSumFailed',
|
|
1667
|
+
msg: 'ComputeInputSumFailed',
|
|
1666
1668
|
},
|
|
1667
1669
|
{
|
|
1668
1670
|
code: 6002,
|
|
1669
|
-
name: '
|
|
1670
|
-
msg: '
|
|
1671
|
+
name: 'ComputeOutputSumFailed',
|
|
1672
|
+
msg: 'ComputeOutputSumFailed',
|
|
1671
1673
|
},
|
|
1672
1674
|
{
|
|
1673
1675
|
code: 6003,
|
|
1674
|
-
name: '
|
|
1675
|
-
msg: '
|
|
1676
|
+
name: 'ComputeCompressSumFailed',
|
|
1677
|
+
msg: 'ComputeCompressSumFailed',
|
|
1678
|
+
},
|
|
1679
|
+
{
|
|
1680
|
+
code: 6004,
|
|
1681
|
+
name: 'ComputeDecompressSumFailed',
|
|
1682
|
+
msg: 'ComputeDecompressSumFailed',
|
|
1683
|
+
},
|
|
1684
|
+
{
|
|
1685
|
+
code: 6005,
|
|
1686
|
+
name: 'SumCheckFailed',
|
|
1687
|
+
msg: 'SumCheckFailed',
|
|
1688
|
+
},
|
|
1689
|
+
{
|
|
1690
|
+
code: 6006,
|
|
1691
|
+
name: 'DecompressRecipientUndefinedForDecompress',
|
|
1692
|
+
msg: 'DecompressRecipientUndefinedForDecompress',
|
|
1693
|
+
},
|
|
1694
|
+
{
|
|
1695
|
+
code: 6007,
|
|
1696
|
+
name: 'CompressedPdaUndefinedForDecompress',
|
|
1697
|
+
msg: 'CompressedPdaUndefinedForDecompress',
|
|
1698
|
+
},
|
|
1699
|
+
{
|
|
1700
|
+
code: 6008,
|
|
1701
|
+
name: 'DeCompressAmountUndefinedForDecompress',
|
|
1702
|
+
msg: 'DeCompressAmountUndefinedForDecompress',
|
|
1703
|
+
},
|
|
1704
|
+
{
|
|
1705
|
+
code: 6009,
|
|
1706
|
+
name: 'CompressedPdaUndefinedForCompress',
|
|
1707
|
+
msg: 'CompressedPdaUndefinedForCompress',
|
|
1708
|
+
},
|
|
1709
|
+
{
|
|
1710
|
+
code: 6010,
|
|
1711
|
+
name: 'DeCompressAmountUndefinedForCompress',
|
|
1712
|
+
msg: 'DeCompressAmountUndefinedForCompress',
|
|
1713
|
+
},
|
|
1714
|
+
{
|
|
1715
|
+
code: 6011,
|
|
1716
|
+
name: 'DelegateSignerCheckFailed',
|
|
1717
|
+
msg: 'DelegateSignerCheckFailed',
|
|
1718
|
+
},
|
|
1719
|
+
{
|
|
1720
|
+
code: 6012,
|
|
1721
|
+
name: 'MintTooLarge',
|
|
1722
|
+
msg: 'Minted amount greater than u64::MAX',
|
|
1723
|
+
},
|
|
1724
|
+
{
|
|
1725
|
+
code: 6013,
|
|
1726
|
+
name: 'SplTokenSupplyMismatch',
|
|
1727
|
+
msg: 'SplTokenSupplyMismatch',
|
|
1728
|
+
},
|
|
1729
|
+
{
|
|
1730
|
+
code: 6014,
|
|
1731
|
+
name: 'HeapMemoryCheckFailed',
|
|
1732
|
+
msg: 'HeapMemoryCheckFailed',
|
|
1733
|
+
},
|
|
1734
|
+
{
|
|
1735
|
+
code: 6015,
|
|
1736
|
+
name: 'InstructionNotCallable',
|
|
1737
|
+
msg: 'The instruction is not callable',
|
|
1738
|
+
},
|
|
1739
|
+
{
|
|
1740
|
+
code: 6016,
|
|
1741
|
+
name: 'ArithmeticUnderflow',
|
|
1742
|
+
msg: 'ArithmeticUnderflow',
|
|
1743
|
+
},
|
|
1744
|
+
{
|
|
1745
|
+
code: 6017,
|
|
1746
|
+
name: 'HashToFieldError',
|
|
1747
|
+
msg: 'HashToFieldError',
|
|
1748
|
+
},
|
|
1749
|
+
{
|
|
1750
|
+
code: 6018,
|
|
1751
|
+
name: 'InvalidAuthorityMint',
|
|
1752
|
+
msg: 'Expected the authority to be also a mint authority',
|
|
1753
|
+
},
|
|
1754
|
+
{
|
|
1755
|
+
code: 6019,
|
|
1756
|
+
name: 'InvalidFreezeAuthority',
|
|
1757
|
+
msg: 'Provided authority is not the freeze authority',
|
|
1758
|
+
},
|
|
1759
|
+
{
|
|
1760
|
+
code: 6020,
|
|
1761
|
+
name: 'InvalidDelegateIndex',
|
|
1762
|
+
},
|
|
1763
|
+
{
|
|
1764
|
+
code: 6021,
|
|
1765
|
+
name: 'TokenPoolPdaUndefined',
|
|
1766
|
+
},
|
|
1767
|
+
{
|
|
1768
|
+
code: 6022,
|
|
1769
|
+
name: 'IsTokenPoolPda',
|
|
1770
|
+
msg: 'Compress or decompress recipient is the same account as the token pool pda.',
|
|
1771
|
+
},
|
|
1772
|
+
{
|
|
1773
|
+
code: 6023,
|
|
1774
|
+
name: 'InvalidTokenPoolPda',
|
|
1775
|
+
},
|
|
1776
|
+
{
|
|
1777
|
+
code: 6024,
|
|
1778
|
+
name: 'NoInputTokenAccountsProvided',
|
|
1779
|
+
},
|
|
1780
|
+
{
|
|
1781
|
+
code: 6025,
|
|
1782
|
+
name: 'NoInputsProvided',
|
|
1783
|
+
},
|
|
1784
|
+
{
|
|
1785
|
+
code: 6026,
|
|
1786
|
+
name: 'MintHasNoFreezeAuthority',
|
|
1787
|
+
},
|
|
1788
|
+
{
|
|
1789
|
+
code: 6027,
|
|
1790
|
+
name: 'MintWithInvalidExtension',
|
|
1791
|
+
},
|
|
1792
|
+
{
|
|
1793
|
+
code: 6028,
|
|
1794
|
+
name: 'InsufficientTokenAccountBalance',
|
|
1795
|
+
msg: 'The token account balance is less than the remaining amount.',
|
|
1676
1796
|
},
|
|
1677
1797
|
],
|
|
1678
1798
|
};
|
|
@@ -1745,3012 +1865,176 @@ const POOL_SEED = Buffer.from('pool');
|
|
|
1745
1865
|
const CPI_AUTHORITY_SEED = Buffer.from('cpi_authority');
|
|
1746
1866
|
const SPL_TOKEN_MINT_RENT_EXEMPT_BALANCE = 1461600;
|
|
1747
1867
|
|
|
1748
|
-
/**
|
|
1749
|
-
|
|
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.
|
|
1868
|
+
/**
|
|
1869
|
+
* Sum up the token amounts of the compressed token accounts
|
|
1782
1870
|
*/
|
|
1783
|
-
|
|
1784
|
-
|
|
1785
|
-
|
|
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;
|
|
1871
|
+
const sumUpTokenAmount = (accounts) => {
|
|
1872
|
+
return accounts.reduce((acc, account) => acc.add(account.parsed.amount), stateless_js.bn(0));
|
|
1873
|
+
};
|
|
1804
1874
|
/**
|
|
1805
|
-
*
|
|
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
|
|
1875
|
+
* Validate that all the compressed token accounts are owned by the same owner.
|
|
1819
1876
|
*/
|
|
1820
|
-
|
|
1821
|
-
|
|
1822
|
-
|
|
1823
|
-
|
|
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');
|
|
1877
|
+
const validateSameTokenOwner = (accounts) => {
|
|
1878
|
+
const owner = accounts[0].parsed.owner;
|
|
1879
|
+
accounts.forEach(acc => {
|
|
1880
|
+
if (!acc.parsed.owner.equals(owner)) {
|
|
1881
|
+
throw new Error('Token accounts must be owned by the same owner');
|
|
1882
1882
|
}
|
|
1883
|
-
|
|
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;
|
|
1883
|
+
});
|
|
1884
|
+
};
|
|
1944
1885
|
/**
|
|
1945
|
-
*
|
|
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`.
|
|
1886
|
+
* Parse compressed token accounts to get the mint, current owner and delegate.
|
|
1970
1887
|
*/
|
|
1971
|
-
|
|
1972
|
-
|
|
1973
|
-
|
|
1974
|
-
|
|
1975
|
-
|
|
1976
|
-
|
|
1977
|
-
|
|
1978
|
-
|
|
1979
|
-
|
|
1980
|
-
|
|
1981
|
-
|
|
1982
|
-
|
|
1983
|
-
|
|
1984
|
-
|
|
1888
|
+
const parseTokenData = (compressedTokenAccounts) => {
|
|
1889
|
+
const mint = compressedTokenAccounts[0].parsed.mint;
|
|
1890
|
+
const currentOwner = compressedTokenAccounts[0].parsed.owner;
|
|
1891
|
+
const delegate = compressedTokenAccounts[0].parsed.delegate;
|
|
1892
|
+
return { mint, currentOwner, delegate };
|
|
1893
|
+
};
|
|
1894
|
+
/**
|
|
1895
|
+
* Create the output state for a transfer transaction.
|
|
1896
|
+
* @param inputCompressedTokenAccounts Input state
|
|
1897
|
+
* @param toAddress Recipient address
|
|
1898
|
+
* @param amount Amount of tokens to transfer
|
|
1899
|
+
* @returns Output token data for the transfer
|
|
1900
|
+
* instruction
|
|
1901
|
+
*/
|
|
1902
|
+
function createTransferOutputState(inputCompressedTokenAccounts, toAddress, amount) {
|
|
1903
|
+
amount = stateless_js.bn(amount);
|
|
1904
|
+
const inputAmount = sumUpTokenAmount(inputCompressedTokenAccounts);
|
|
1905
|
+
const inputLamports = stateless_js.sumUpLamports(inputCompressedTokenAccounts.map(acc => acc.compressedAccount));
|
|
1906
|
+
const changeAmount = inputAmount.sub(amount);
|
|
1907
|
+
stateless_js.validateSufficientBalance(changeAmount);
|
|
1908
|
+
if (changeAmount.eq(stateless_js.bn(0)) && inputLamports.eq(stateless_js.bn(0))) {
|
|
1909
|
+
return [
|
|
1910
|
+
{
|
|
1911
|
+
owner: toAddress,
|
|
1912
|
+
amount,
|
|
1913
|
+
lamports: inputLamports,
|
|
1914
|
+
tlv: null,
|
|
1915
|
+
},
|
|
1916
|
+
];
|
|
1985
1917
|
}
|
|
1986
|
-
|
|
1987
|
-
|
|
1988
|
-
|
|
1989
|
-
|
|
1990
|
-
|
|
1991
|
-
|
|
1918
|
+
/// validates token program
|
|
1919
|
+
stateless_js.validateSameOwner(inputCompressedTokenAccounts.map(acc => acc.compressedAccount));
|
|
1920
|
+
validateSameTokenOwner(inputCompressedTokenAccounts);
|
|
1921
|
+
const outputCompressedAccounts = [
|
|
1922
|
+
{
|
|
1923
|
+
owner: inputCompressedTokenAccounts[0].parsed.owner,
|
|
1924
|
+
amount: changeAmount,
|
|
1925
|
+
lamports: inputLamports,
|
|
1926
|
+
tlv: null,
|
|
1992
1927
|
},
|
|
1993
|
-
|
|
1994
|
-
|
|
1995
|
-
|
|
1996
|
-
|
|
1997
|
-
|
|
1928
|
+
{
|
|
1929
|
+
owner: toAddress,
|
|
1930
|
+
amount,
|
|
1931
|
+
lamports: stateless_js.bn(0),
|
|
1932
|
+
tlv: null,
|
|
1998
1933
|
},
|
|
1999
|
-
|
|
2000
|
-
|
|
1934
|
+
];
|
|
1935
|
+
return outputCompressedAccounts;
|
|
2001
1936
|
}
|
|
2002
|
-
Layout$1.bindConstructorLayout = bindConstructorLayout;
|
|
2003
1937
|
/**
|
|
2004
|
-
*
|
|
2005
|
-
*
|
|
2006
|
-
*
|
|
2007
|
-
*
|
|
2008
|
-
*
|
|
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}
|
|
1938
|
+
* Create the output state for a compress transaction.
|
|
1939
|
+
* @param inputCompressedTokenAccounts Input state
|
|
1940
|
+
* @param amount Amount of tokens to compress
|
|
1941
|
+
* @returns Output token data for the compress
|
|
1942
|
+
* instruction
|
|
2023
1943
|
*/
|
|
2024
|
-
|
|
1944
|
+
function createDecompressOutputState(inputCompressedTokenAccounts, amount) {
|
|
1945
|
+
amount = stateless_js.bn(amount);
|
|
1946
|
+
const inputLamports = stateless_js.sumUpLamports(inputCompressedTokenAccounts.map(acc => acc.compressedAccount));
|
|
1947
|
+
const inputAmount = sumUpTokenAmount(inputCompressedTokenAccounts);
|
|
1948
|
+
const changeAmount = inputAmount.sub(amount);
|
|
1949
|
+
stateless_js.validateSufficientBalance(changeAmount);
|
|
1950
|
+
/// lamports gets decompressed
|
|
1951
|
+
if (changeAmount.eq(stateless_js.bn(0)) && inputLamports.eq(stateless_js.bn(0))) {
|
|
1952
|
+
return [];
|
|
1953
|
+
}
|
|
1954
|
+
stateless_js.validateSameOwner(inputCompressedTokenAccounts.map(acc => acc.compressedAccount));
|
|
1955
|
+
validateSameTokenOwner(inputCompressedTokenAccounts);
|
|
1956
|
+
const tokenTransferOutputs = [
|
|
1957
|
+
{
|
|
1958
|
+
owner: inputCompressedTokenAccounts[0].parsed.owner,
|
|
1959
|
+
amount: changeAmount,
|
|
1960
|
+
lamports: inputLamports,
|
|
1961
|
+
tlv: null,
|
|
1962
|
+
},
|
|
1963
|
+
];
|
|
1964
|
+
return tokenTransferOutputs;
|
|
1965
|
+
}
|
|
1966
|
+
class CompressedTokenProgram {
|
|
2025
1967
|
/**
|
|
2026
|
-
*
|
|
2027
|
-
|
|
2028
|
-
|
|
2029
|
-
|
|
2030
|
-
*
|
|
2031
|
-
*
|
|
2032
|
-
* discriminators}.
|
|
1968
|
+
* @internal
|
|
1969
|
+
*/
|
|
1970
|
+
constructor() { }
|
|
1971
|
+
/**
|
|
1972
|
+
* Set a custom programId via PublicKey or base58 encoded string.
|
|
1973
|
+
* This method is not required for regular usage.
|
|
2033
1974
|
*
|
|
2034
|
-
*
|
|
1975
|
+
* Use this only if you know what you are doing.
|
|
2035
1976
|
*/
|
|
2036
|
-
|
|
2037
|
-
|
|
1977
|
+
static setProgramId(programId) {
|
|
1978
|
+
this.programId =
|
|
1979
|
+
typeof programId === 'string'
|
|
1980
|
+
? new web3_js.PublicKey(programId)
|
|
1981
|
+
: programId;
|
|
1982
|
+
// Reset program when programId changes
|
|
1983
|
+
this._program = null;
|
|
2038
1984
|
}
|
|
2039
|
-
|
|
2040
|
-
|
|
2041
|
-
|
|
2042
|
-
|
|
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');
|
|
1985
|
+
/** @internal */
|
|
1986
|
+
static get program() {
|
|
1987
|
+
if (!this._program) {
|
|
1988
|
+
this.initializeProgram();
|
|
2060
1989
|
}
|
|
2061
|
-
|
|
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;
|
|
1990
|
+
return this._program;
|
|
2066
1991
|
}
|
|
2067
|
-
/**
|
|
2068
|
-
|
|
2069
|
-
|
|
1992
|
+
/**
|
|
1993
|
+
* @internal
|
|
1994
|
+
* Initializes the program statically if not already initialized.
|
|
1995
|
+
*/
|
|
1996
|
+
static initializeProgram() {
|
|
1997
|
+
if (!this._program) {
|
|
1998
|
+
/// Note: We can use a mock connection because we're using the
|
|
1999
|
+
/// program only for serde and building instructions, not for
|
|
2000
|
+
/// interacting with the network.
|
|
2001
|
+
const mockKeypair = web3_js.Keypair.generate();
|
|
2002
|
+
const mockConnection = new web3_js.Connection('http://127.0.0.1:8899', 'confirmed');
|
|
2003
|
+
const mockProvider = new anchor.AnchorProvider(mockConnection, stateless_js.useWallet(mockKeypair), stateless_js.confirmConfig);
|
|
2004
|
+
anchor.setProvider(mockProvider);
|
|
2005
|
+
this._program = new anchor.Program(IDL, this.programId, mockProvider);
|
|
2006
|
+
}
|
|
2070
2007
|
}
|
|
2071
|
-
/** @
|
|
2072
|
-
|
|
2073
|
-
|
|
2074
|
-
const
|
|
2075
|
-
return
|
|
2008
|
+
/** @internal */
|
|
2009
|
+
static deriveTokenPoolPda(mint) {
|
|
2010
|
+
const seeds = [POOL_SEED, mint.toBuffer()];
|
|
2011
|
+
const [address, _] = web3_js.PublicKey.findProgramAddressSync(seeds, this.programId);
|
|
2012
|
+
return address;
|
|
2076
2013
|
}
|
|
2077
|
-
/** @
|
|
2078
|
-
|
|
2079
|
-
|
|
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
|
-
/**
|
|
4587
|
-
* Sum up the token amounts of the compressed token accounts
|
|
4588
|
-
*/
|
|
4589
|
-
const sumUpTokenAmount = (accounts) => {
|
|
4590
|
-
return accounts.reduce((acc, account) => acc.add(account.parsed.amount), stateless_js.bn(0));
|
|
4591
|
-
};
|
|
4592
|
-
/**
|
|
4593
|
-
* Validate that all the compressed token accounts are owned by the same owner.
|
|
4594
|
-
*/
|
|
4595
|
-
const validateSameTokenOwner = (accounts) => {
|
|
4596
|
-
const owner = accounts[0].parsed.owner;
|
|
4597
|
-
accounts.forEach(acc => {
|
|
4598
|
-
if (!acc.parsed.owner.equals(owner)) {
|
|
4599
|
-
throw new Error('Token accounts must be owned by the same owner');
|
|
4600
|
-
}
|
|
4601
|
-
});
|
|
4602
|
-
};
|
|
4603
|
-
/**
|
|
4604
|
-
* Parse compressed token accounts to get the mint, current owner and delegate.
|
|
4605
|
-
*/
|
|
4606
|
-
const parseTokenData = (compressedTokenAccounts) => {
|
|
4607
|
-
const mint = compressedTokenAccounts[0].parsed.mint;
|
|
4608
|
-
const currentOwner = compressedTokenAccounts[0].parsed.owner;
|
|
4609
|
-
const delegate = compressedTokenAccounts[0].parsed.delegate;
|
|
4610
|
-
return { mint, currentOwner, delegate };
|
|
4611
|
-
};
|
|
4612
|
-
/**
|
|
4613
|
-
* Create the output state for a transfer transaction.
|
|
4614
|
-
* @param inputCompressedTokenAccounts Input state
|
|
4615
|
-
* @param toAddress Recipient address
|
|
4616
|
-
* @param amount Amount of tokens to transfer
|
|
4617
|
-
* @returns Output token data for the transfer
|
|
4618
|
-
* instruction
|
|
4619
|
-
*/
|
|
4620
|
-
function createTransferOutputState(inputCompressedTokenAccounts, toAddress, amount) {
|
|
4621
|
-
amount = stateless_js.bn(amount);
|
|
4622
|
-
const inputAmount = sumUpTokenAmount(inputCompressedTokenAccounts);
|
|
4623
|
-
const inputLamports = stateless_js.sumUpLamports(inputCompressedTokenAccounts.map(acc => acc.compressedAccount));
|
|
4624
|
-
const changeAmount = inputAmount.sub(amount);
|
|
4625
|
-
stateless_js.validateSufficientBalance(changeAmount);
|
|
4626
|
-
if (changeAmount.eq(stateless_js.bn(0)) && inputLamports.eq(stateless_js.bn(0))) {
|
|
4627
|
-
return [
|
|
4628
|
-
{
|
|
4629
|
-
owner: toAddress,
|
|
4630
|
-
amount,
|
|
4631
|
-
lamports: inputLamports,
|
|
4632
|
-
tlv: null,
|
|
4633
|
-
},
|
|
4634
|
-
];
|
|
4635
|
-
}
|
|
4636
|
-
/// validates token program
|
|
4637
|
-
stateless_js.validateSameOwner(inputCompressedTokenAccounts.map(acc => acc.compressedAccount));
|
|
4638
|
-
validateSameTokenOwner(inputCompressedTokenAccounts);
|
|
4639
|
-
const outputCompressedAccounts = [
|
|
4640
|
-
{
|
|
4641
|
-
owner: inputCompressedTokenAccounts[0].parsed.owner,
|
|
4642
|
-
amount: changeAmount,
|
|
4643
|
-
lamports: inputLamports,
|
|
4644
|
-
tlv: null,
|
|
4645
|
-
},
|
|
4646
|
-
{
|
|
4647
|
-
owner: toAddress,
|
|
4648
|
-
amount,
|
|
4649
|
-
lamports: stateless_js.bn(0),
|
|
4650
|
-
tlv: null,
|
|
4651
|
-
},
|
|
4652
|
-
];
|
|
4653
|
-
return outputCompressedAccounts;
|
|
4654
|
-
}
|
|
4655
|
-
/**
|
|
4656
|
-
* Create the output state for a compress transaction.
|
|
4657
|
-
* @param inputCompressedTokenAccounts Input state
|
|
4658
|
-
* @param amount Amount of tokens to compress
|
|
4659
|
-
* @returns Output token data for the compress
|
|
4660
|
-
* instruction
|
|
4661
|
-
*/
|
|
4662
|
-
function createDecompressOutputState(inputCompressedTokenAccounts, amount) {
|
|
4663
|
-
amount = stateless_js.bn(amount);
|
|
4664
|
-
const inputLamports = stateless_js.sumUpLamports(inputCompressedTokenAccounts.map(acc => acc.compressedAccount));
|
|
4665
|
-
const inputAmount = sumUpTokenAmount(inputCompressedTokenAccounts);
|
|
4666
|
-
const changeAmount = inputAmount.sub(amount);
|
|
4667
|
-
stateless_js.validateSufficientBalance(changeAmount);
|
|
4668
|
-
/// lamports gets decompressed
|
|
4669
|
-
if (changeAmount.eq(stateless_js.bn(0)) && inputLamports.eq(stateless_js.bn(0))) {
|
|
4670
|
-
return [];
|
|
4671
|
-
}
|
|
4672
|
-
stateless_js.validateSameOwner(inputCompressedTokenAccounts.map(acc => acc.compressedAccount));
|
|
4673
|
-
validateSameTokenOwner(inputCompressedTokenAccounts);
|
|
4674
|
-
const tokenTransferOutputs = [
|
|
4675
|
-
{
|
|
4676
|
-
owner: inputCompressedTokenAccounts[0].parsed.owner,
|
|
4677
|
-
amount: changeAmount,
|
|
4678
|
-
lamports: inputLamports,
|
|
4679
|
-
tlv: null,
|
|
4680
|
-
},
|
|
4681
|
-
];
|
|
4682
|
-
return tokenTransferOutputs;
|
|
4683
|
-
}
|
|
4684
|
-
class CompressedTokenProgram {
|
|
4685
|
-
/**
|
|
4686
|
-
* @internal
|
|
4687
|
-
*/
|
|
4688
|
-
constructor() { }
|
|
4689
|
-
/**
|
|
4690
|
-
* Set a custom programId via PublicKey or base58 encoded string.
|
|
4691
|
-
* This method is not required for regular usage.
|
|
4692
|
-
*
|
|
4693
|
-
* Use this only if you know what you are doing.
|
|
4694
|
-
*/
|
|
4695
|
-
static setProgramId(programId) {
|
|
4696
|
-
this.programId =
|
|
4697
|
-
typeof programId === 'string'
|
|
4698
|
-
? new web3_js.PublicKey(programId)
|
|
4699
|
-
: programId;
|
|
4700
|
-
// Reset program when programId changes
|
|
4701
|
-
this._program = null;
|
|
4702
|
-
}
|
|
4703
|
-
/** @internal */
|
|
4704
|
-
static get program() {
|
|
4705
|
-
if (!this._program) {
|
|
4706
|
-
this.initializeProgram();
|
|
4707
|
-
}
|
|
4708
|
-
return this._program;
|
|
4709
|
-
}
|
|
4710
|
-
/**
|
|
4711
|
-
* @internal
|
|
4712
|
-
* Initializes the program statically if not already initialized.
|
|
4713
|
-
*/
|
|
4714
|
-
static initializeProgram() {
|
|
4715
|
-
if (!this._program) {
|
|
4716
|
-
/// Note: We can use a mock connection because we're using the
|
|
4717
|
-
/// program only for serde and building instructions, not for
|
|
4718
|
-
/// interacting with the network.
|
|
4719
|
-
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);
|
|
4722
|
-
anchor.setProvider(mockProvider);
|
|
4723
|
-
this._program = new anchor.Program(IDL, this.programId, mockProvider);
|
|
4724
|
-
}
|
|
4725
|
-
}
|
|
4726
|
-
/** @internal */
|
|
4727
|
-
static deriveTokenPoolPda(mint) {
|
|
4728
|
-
const seeds = [POOL_SEED, mint.toBuffer()];
|
|
4729
|
-
const [address, _] = web3_js.PublicKey.findProgramAddressSync(seeds, this.programId);
|
|
4730
|
-
return address;
|
|
4731
|
-
}
|
|
4732
|
-
/** @internal */
|
|
4733
|
-
static get deriveCpiAuthorityPda() {
|
|
4734
|
-
const [address, _] = web3_js.PublicKey.findProgramAddressSync([CPI_AUTHORITY_SEED], this.programId);
|
|
4735
|
-
return address;
|
|
2014
|
+
/** @internal */
|
|
2015
|
+
static get deriveCpiAuthorityPda() {
|
|
2016
|
+
const [address, _] = web3_js.PublicKey.findProgramAddressSync([CPI_AUTHORITY_SEED], this.programId);
|
|
2017
|
+
return address;
|
|
4736
2018
|
}
|
|
4737
2019
|
/**
|
|
4738
2020
|
* Construct createMint instruction for compressed tokens
|
|
4739
2021
|
*/
|
|
4740
2022
|
static async createMint(params) {
|
|
4741
|
-
const { mint, authority, feePayer, rentExemptBalance } = params;
|
|
2023
|
+
const { mint, authority, feePayer, rentExemptBalance, tokenProgramId, freezeAuthority, mintSize, } = params;
|
|
2024
|
+
const tokenProgram = tokenProgramId !== null && tokenProgramId !== void 0 ? tokenProgramId : splToken.TOKEN_PROGRAM_ID;
|
|
4742
2025
|
/// Create and initialize SPL Mint account
|
|
4743
2026
|
const createMintAccountInstruction = web3_js.SystemProgram.createAccount({
|
|
4744
2027
|
fromPubkey: feePayer,
|
|
4745
2028
|
lamports: rentExemptBalance,
|
|
4746
2029
|
newAccountPubkey: mint,
|
|
4747
|
-
programId:
|
|
4748
|
-
space: MINT_SIZE,
|
|
2030
|
+
programId: tokenProgram,
|
|
2031
|
+
space: mintSize !== null && mintSize !== void 0 ? mintSize : splToken.MINT_SIZE,
|
|
4749
2032
|
});
|
|
4750
|
-
const initializeMintInstruction = createInitializeMint2Instruction(mint, params.decimals, authority,
|
|
2033
|
+
const initializeMintInstruction = splToken.createInitializeMint2Instruction(mint, params.decimals, authority, freezeAuthority, tokenProgram);
|
|
4751
2034
|
const ix = await this.createTokenPool({
|
|
4752
2035
|
feePayer,
|
|
4753
2036
|
mint,
|
|
2037
|
+
tokenProgramId: tokenProgram,
|
|
4754
2038
|
});
|
|
4755
2039
|
return [createMintAccountInstruction, initializeMintInstruction, ix];
|
|
4756
2040
|
}
|
|
@@ -4759,7 +2043,8 @@ class CompressedTokenProgram {
|
|
|
4759
2043
|
* For new mints, use `CompressedTokenProgram.createMint`.
|
|
4760
2044
|
*/
|
|
4761
2045
|
static async createTokenPool(params) {
|
|
4762
|
-
const { mint, feePayer } = params;
|
|
2046
|
+
const { mint, feePayer, tokenProgramId } = params;
|
|
2047
|
+
const tokenProgram = tokenProgramId !== null && tokenProgramId !== void 0 ? tokenProgramId : splToken.TOKEN_PROGRAM_ID;
|
|
4763
2048
|
const tokenPoolPda = this.deriveTokenPoolPda(mint);
|
|
4764
2049
|
const ix = await this.program.methods
|
|
4765
2050
|
.createTokenPool()
|
|
@@ -4768,7 +2053,7 @@ class CompressedTokenProgram {
|
|
|
4768
2053
|
feePayer,
|
|
4769
2054
|
tokenPoolPda,
|
|
4770
2055
|
systemProgram: web3_js.SystemProgram.programId,
|
|
4771
|
-
tokenProgram
|
|
2056
|
+
tokenProgram,
|
|
4772
2057
|
cpiAuthorityPda: this.deriveCpiAuthorityPda,
|
|
4773
2058
|
})
|
|
4774
2059
|
.instruction();
|
|
@@ -4779,7 +2064,8 @@ class CompressedTokenProgram {
|
|
|
4779
2064
|
*/
|
|
4780
2065
|
static async mintTo(params) {
|
|
4781
2066
|
const systemKeys = stateless_js.defaultStaticAccountsStruct();
|
|
4782
|
-
const { mint, feePayer, authority, merkleTree, toPubkey, amount } = params;
|
|
2067
|
+
const { mint, feePayer, authority, merkleTree, toPubkey, amount, tokenProgramId, } = params;
|
|
2068
|
+
const tokenProgram = tokenProgramId !== null && tokenProgramId !== void 0 ? tokenProgramId : splToken.TOKEN_PROGRAM_ID;
|
|
4783
2069
|
const tokenPoolPda = this.deriveTokenPoolPda(mint);
|
|
4784
2070
|
const amounts = stateless_js.toArray(amount).map(amount => stateless_js.bn(amount));
|
|
4785
2071
|
const toPubkeys = stateless_js.toArray(toPubkey);
|
|
@@ -4794,7 +2080,7 @@ class CompressedTokenProgram {
|
|
|
4794
2080
|
cpiAuthorityPda: this.deriveCpiAuthorityPda,
|
|
4795
2081
|
mint,
|
|
4796
2082
|
tokenPoolPda,
|
|
4797
|
-
tokenProgram
|
|
2083
|
+
tokenProgram,
|
|
4798
2084
|
lightSystemProgram: stateless_js.LightSystemProgram.programId,
|
|
4799
2085
|
registeredProgramPda: systemKeys.registeredProgramPda,
|
|
4800
2086
|
noopProgram: systemKeys.noopProgram,
|
|
@@ -4807,15 +2093,14 @@ class CompressedTokenProgram {
|
|
|
4807
2093
|
.instruction();
|
|
4808
2094
|
return instruction;
|
|
4809
2095
|
}
|
|
4810
|
-
/// TODO: add compressBatch functionality for batch minting
|
|
4811
2096
|
/**
|
|
4812
|
-
* Mint tokens from
|
|
2097
|
+
* Mint tokens from registered SPL mint account to a compressed account
|
|
4813
2098
|
*/
|
|
4814
2099
|
static async approveAndMintTo(params) {
|
|
4815
|
-
const { mint, feePayer, authorityTokenAccount, authority, merkleTree, toPubkey, } = params;
|
|
2100
|
+
const { mint, feePayer, authorityTokenAccount, authority, merkleTree, toPubkey, tokenProgramId, } = params;
|
|
4816
2101
|
const amount = BigInt(params.amount.toString());
|
|
4817
2102
|
/// 1. Mint to existing ATA of mintAuthority.
|
|
4818
|
-
const splMintToInstruction = createMintToInstruction(mint, authorityTokenAccount, authority, amount);
|
|
2103
|
+
const splMintToInstruction = splToken.createMintToInstruction(mint, authorityTokenAccount, authority, amount, [], tokenProgramId);
|
|
4819
2104
|
/// 2. Compress from mint authority ATA to recipient compressed account
|
|
4820
2105
|
const compressInstruction = await this.compress({
|
|
4821
2106
|
payer: feePayer,
|
|
@@ -4825,6 +2110,7 @@ class CompressedTokenProgram {
|
|
|
4825
2110
|
mint,
|
|
4826
2111
|
amount: params.amount,
|
|
4827
2112
|
outputStateTree: merkleTree,
|
|
2113
|
+
tokenProgramId,
|
|
4828
2114
|
});
|
|
4829
2115
|
return [splMintToInstruction, compressInstruction];
|
|
4830
2116
|
}
|
|
@@ -4907,7 +2193,8 @@ class CompressedTokenProgram {
|
|
|
4907
2193
|
stateless_js.defaultTestStateTreeAccounts().addressTree,
|
|
4908
2194
|
stateless_js.defaultTestStateTreeAccounts().addressQueue,
|
|
4909
2195
|
this.programId,
|
|
4910
|
-
TOKEN_PROGRAM_ID,
|
|
2196
|
+
splToken.TOKEN_PROGRAM_ID,
|
|
2197
|
+
splToken.TOKEN_2022_PROGRAM_ID,
|
|
4911
2198
|
authority,
|
|
4912
2199
|
...optionalMintKeys,
|
|
4913
2200
|
...(remainingAccounts !== null && remainingAccounts !== void 0 ? remainingAccounts : []),
|
|
@@ -4923,7 +2210,7 @@ class CompressedTokenProgram {
|
|
|
4923
2210
|
* @returns compressInstruction
|
|
4924
2211
|
*/
|
|
4925
2212
|
static async compress(params) {
|
|
4926
|
-
const { payer, owner, source, toAddress, mint, outputStateTree } = params;
|
|
2213
|
+
const { payer, owner, source, toAddress, mint, outputStateTree, tokenProgramId, } = params;
|
|
4927
2214
|
if (Array.isArray(params.amount) !== Array.isArray(params.toAddress)) {
|
|
4928
2215
|
throw new Error('Both amount and toAddress must be arrays or both must be single values');
|
|
4929
2216
|
}
|
|
@@ -4974,6 +2261,7 @@ class CompressedTokenProgram {
|
|
|
4974
2261
|
lamportsChangeAccountMerkleTreeIndex: null,
|
|
4975
2262
|
};
|
|
4976
2263
|
const encodedData = this.program.coder.types.encode('CompressedTokenInstructionDataTransfer', data);
|
|
2264
|
+
const tokenProgram = tokenProgramId !== null && tokenProgramId !== void 0 ? tokenProgramId : splToken.TOKEN_PROGRAM_ID;
|
|
4977
2265
|
const instruction = await this.program.methods
|
|
4978
2266
|
.transfer(encodedData)
|
|
4979
2267
|
.accounts({
|
|
@@ -4988,7 +2276,7 @@ class CompressedTokenProgram {
|
|
|
4988
2276
|
selfProgram: this.programId,
|
|
4989
2277
|
tokenPoolPda: this.deriveTokenPoolPda(mint),
|
|
4990
2278
|
compressOrDecompressTokenAccount: source, // token
|
|
4991
|
-
tokenProgram
|
|
2279
|
+
tokenProgram,
|
|
4992
2280
|
})
|
|
4993
2281
|
.remainingAccounts(remainingAccountMetas)
|
|
4994
2282
|
.instruction();
|
|
@@ -4998,7 +2286,7 @@ class CompressedTokenProgram {
|
|
|
4998
2286
|
* Construct decompress instruction
|
|
4999
2287
|
*/
|
|
5000
2288
|
static async decompress(params) {
|
|
5001
|
-
const { payer, inputCompressedTokenAccounts, toAddress, outputStateTree, recentValidityProof, recentInputStateRootIndices, } = params;
|
|
2289
|
+
const { payer, inputCompressedTokenAccounts, toAddress, outputStateTree, recentValidityProof, recentInputStateRootIndices, tokenProgramId, } = params;
|
|
5002
2290
|
const amount = stateless_js.bn(params.amount);
|
|
5003
2291
|
const tokenTransferOutputs = createDecompressOutputState(inputCompressedTokenAccounts, amount);
|
|
5004
2292
|
/// Pack
|
|
@@ -5022,6 +2310,7 @@ class CompressedTokenProgram {
|
|
|
5022
2310
|
};
|
|
5023
2311
|
const encodedData = this.program.coder.types.encode('CompressedTokenInstructionDataTransfer', data);
|
|
5024
2312
|
const { accountCompressionAuthority, noopProgram, registeredProgramPda, accountCompressionProgram, } = stateless_js.defaultStaticAccountsStruct();
|
|
2313
|
+
const tokenProgram = tokenProgramId !== null && tokenProgramId !== void 0 ? tokenProgramId : splToken.TOKEN_PROGRAM_ID;
|
|
5025
2314
|
const instruction = await this.program.methods
|
|
5026
2315
|
.transfer(encodedData)
|
|
5027
2316
|
.accounts({
|
|
@@ -5036,7 +2325,7 @@ class CompressedTokenProgram {
|
|
|
5036
2325
|
selfProgram: this.programId,
|
|
5037
2326
|
tokenPoolPda: this.deriveTokenPoolPda(mint),
|
|
5038
2327
|
compressOrDecompressTokenAccount: toAddress,
|
|
5039
|
-
tokenProgram
|
|
2328
|
+
tokenProgram,
|
|
5040
2329
|
})
|
|
5041
2330
|
.remainingAccounts(remainingAccountMetas)
|
|
5042
2331
|
.instruction();
|
|
@@ -5059,7 +2348,8 @@ class CompressedTokenProgram {
|
|
|
5059
2348
|
return [ix];
|
|
5060
2349
|
}
|
|
5061
2350
|
static async compressSplTokenAccount(params) {
|
|
5062
|
-
const { feePayer, authority, tokenAccount, mint, remainingAmount, outputStateTree, } = params;
|
|
2351
|
+
const { feePayer, authority, tokenAccount, mint, remainingAmount, outputStateTree, tokenProgramId, } = params;
|
|
2352
|
+
const tokenProgram = tokenProgramId !== null && tokenProgramId !== void 0 ? tokenProgramId : splToken.TOKEN_PROGRAM_ID;
|
|
5063
2353
|
const remainingAccountMetas = [
|
|
5064
2354
|
{
|
|
5065
2355
|
pubkey: outputStateTree,
|
|
@@ -5081,13 +2371,17 @@ class CompressedTokenProgram {
|
|
|
5081
2371
|
selfProgram: this.programId,
|
|
5082
2372
|
tokenPoolPda: this.deriveTokenPoolPda(mint),
|
|
5083
2373
|
compressOrDecompressTokenAccount: tokenAccount,
|
|
5084
|
-
tokenProgram
|
|
2374
|
+
tokenProgram,
|
|
5085
2375
|
systemProgram: web3_js.SystemProgram.programId,
|
|
5086
2376
|
})
|
|
5087
2377
|
.remainingAccounts(remainingAccountMetas)
|
|
5088
2378
|
.instruction();
|
|
5089
2379
|
return instruction;
|
|
5090
2380
|
}
|
|
2381
|
+
static async get_mint_program_id(mint, connection) {
|
|
2382
|
+
var _a;
|
|
2383
|
+
return (_a = (await connection.getAccountInfo(mint))) === null || _a === void 0 ? void 0 : _a.owner;
|
|
2384
|
+
}
|
|
5091
2385
|
}
|
|
5092
2386
|
/**
|
|
5093
2387
|
* Public key that identifies the CompressedPda program
|
|
@@ -5110,8 +2404,11 @@ CompressedTokenProgram._program = null;
|
|
|
5110
2404
|
*
|
|
5111
2405
|
* @return Signature of the confirmed transaction
|
|
5112
2406
|
*/
|
|
5113
|
-
async function approveAndMintTo(rpc, payer, mint, destination, authority, amount, merkleTree, confirmOptions) {
|
|
5114
|
-
|
|
2407
|
+
async function approveAndMintTo(rpc, payer, mint, destination, authority, amount, merkleTree, confirmOptions, tokenProgramId) {
|
|
2408
|
+
tokenProgramId = tokenProgramId
|
|
2409
|
+
? tokenProgramId
|
|
2410
|
+
: await CompressedTokenProgram.get_mint_program_id(mint, rpc);
|
|
2411
|
+
const authorityTokenAccount = await splToken.getOrCreateAssociatedTokenAccount(rpc, payer, mint, authority.publicKey, undefined, undefined, confirmOptions, tokenProgramId);
|
|
5115
2412
|
const ixs = await CompressedTokenProgram.approveAndMintTo({
|
|
5116
2413
|
feePayer: payer.publicKey,
|
|
5117
2414
|
mint,
|
|
@@ -5120,6 +2417,7 @@ async function approveAndMintTo(rpc, payer, mint, destination, authority, amount
|
|
|
5120
2417
|
amount,
|
|
5121
2418
|
toPubkey: destination,
|
|
5122
2419
|
merkleTree,
|
|
2420
|
+
tokenProgramId,
|
|
5123
2421
|
});
|
|
5124
2422
|
const { blockhash } = await rpc.getLatestBlockhash();
|
|
5125
2423
|
const additionalSigners = stateless_js.dedupeSigner(payer, [authority]);
|
|
@@ -5149,7 +2447,10 @@ async function approveAndMintTo(rpc, payer, mint, destination, authority, amount
|
|
|
5149
2447
|
*
|
|
5150
2448
|
* @return Signature of the confirmed transaction
|
|
5151
2449
|
*/
|
|
5152
|
-
async function compress(rpc, payer, mint, amount, owner, sourceTokenAccount, toAddress, merkleTree, confirmOptions) {
|
|
2450
|
+
async function compress(rpc, payer, mint, amount, owner, sourceTokenAccount, toAddress, merkleTree, confirmOptions, tokenProgramId) {
|
|
2451
|
+
tokenProgramId = tokenProgramId
|
|
2452
|
+
? tokenProgramId
|
|
2453
|
+
: await CompressedTokenProgram.get_mint_program_id(mint, rpc);
|
|
5153
2454
|
const compressIx = await CompressedTokenProgram.compress({
|
|
5154
2455
|
payer: payer.publicKey,
|
|
5155
2456
|
owner: owner.publicKey,
|
|
@@ -5158,6 +2459,7 @@ async function compress(rpc, payer, mint, amount, owner, sourceTokenAccount, toA
|
|
|
5158
2459
|
amount,
|
|
5159
2460
|
mint,
|
|
5160
2461
|
outputStateTree: merkleTree,
|
|
2462
|
+
tokenProgramId,
|
|
5161
2463
|
});
|
|
5162
2464
|
const blockhashCtx = await rpc.getLatestBlockhash();
|
|
5163
2465
|
const additionalSigners = stateless_js.dedupeSigner(payer, [owner]);
|
|
@@ -5261,7 +2563,10 @@ function selectMinCompressedTokenAccountsForTransfer(accounts, transferAmount) {
|
|
|
5261
2563
|
*/
|
|
5262
2564
|
async function decompress(rpc, payer, mint, amount, owner, toAddress,
|
|
5263
2565
|
/// TODO: allow multiple
|
|
5264
|
-
merkleTree, confirmOptions) {
|
|
2566
|
+
merkleTree, confirmOptions, tokenProgramId) {
|
|
2567
|
+
tokenProgramId = tokenProgramId
|
|
2568
|
+
? tokenProgramId
|
|
2569
|
+
: await CompressedTokenProgram.get_mint_program_id(mint, rpc);
|
|
5265
2570
|
amount = stateless_js.bn(amount);
|
|
5266
2571
|
const compressedTokenAccounts = await rpc.getCompressedTokenAccountsByOwner(owner.publicKey, {
|
|
5267
2572
|
mint,
|
|
@@ -5277,6 +2582,7 @@ merkleTree, confirmOptions) {
|
|
|
5277
2582
|
outputStateTree: merkleTree,
|
|
5278
2583
|
recentInputStateRootIndices: proof.rootIndices,
|
|
5279
2584
|
recentValidityProof: proof.compressedProof,
|
|
2585
|
+
tokenProgramId,
|
|
5280
2586
|
});
|
|
5281
2587
|
const { blockhash } = await rpc.getLatestBlockhash();
|
|
5282
2588
|
const additionalSigners = stateless_js.dedupeSigner(payer, [owner]);
|
|
@@ -5294,11 +2600,13 @@ merkleTree, confirmOptions) {
|
|
|
5294
2600
|
* @param decimals Location of the decimal place
|
|
5295
2601
|
* @param keypair Optional keypair, defaulting to a new random one
|
|
5296
2602
|
* @param confirmOptions Options for confirming the transaction
|
|
2603
|
+
* @param isToken22 Whether to create a Token 2022 mint. Defaults to false.
|
|
5297
2604
|
*
|
|
5298
2605
|
* @return Address of the new mint and the transaction signature
|
|
5299
2606
|
*/
|
|
5300
|
-
async function createMint(rpc, payer, mintAuthority, decimals, keypair = web3_js.Keypair.generate(), confirmOptions) {
|
|
5301
|
-
const rentExemptBalance = await rpc.getMinimumBalanceForRentExemption(MINT_SIZE);
|
|
2607
|
+
async function createMint(rpc, payer, mintAuthority, decimals, keypair = web3_js.Keypair.generate(), confirmOptions, isToken22 = false) {
|
|
2608
|
+
const rentExemptBalance = await rpc.getMinimumBalanceForRentExemption(splToken.MINT_SIZE);
|
|
2609
|
+
const tokenProgramId = isToken22 ? splToken.TOKEN_2022_PROGRAM_ID : splToken.TOKEN_PROGRAM_ID;
|
|
5302
2610
|
const ixs = await CompressedTokenProgram.createMint({
|
|
5303
2611
|
feePayer: payer.publicKey,
|
|
5304
2612
|
mint: keypair.publicKey,
|
|
@@ -5306,6 +2614,7 @@ async function createMint(rpc, payer, mintAuthority, decimals, keypair = web3_js
|
|
|
5306
2614
|
authority: mintAuthority,
|
|
5307
2615
|
freezeAuthority: null, // TODO: add feature
|
|
5308
2616
|
rentExemptBalance,
|
|
2617
|
+
tokenProgramId,
|
|
5309
2618
|
});
|
|
5310
2619
|
const { blockhash } = await rpc.getLatestBlockhash();
|
|
5311
2620
|
const additionalSigners = stateless_js.dedupeSigner(payer, [keypair]);
|
|
@@ -5331,7 +2640,10 @@ async function createMint(rpc, payer, mintAuthority, decimals, keypair = web3_js
|
|
|
5331
2640
|
*
|
|
5332
2641
|
* @return Signature of the confirmed transaction
|
|
5333
2642
|
*/
|
|
5334
|
-
async function mintTo(rpc, payer, mint, destination, authority, amount, merkleTree, confirmOptions) {
|
|
2643
|
+
async function mintTo(rpc, payer, mint, destination, authority, amount, merkleTree, confirmOptions, tokenProgramId) {
|
|
2644
|
+
tokenProgramId = tokenProgramId
|
|
2645
|
+
? tokenProgramId
|
|
2646
|
+
: await CompressedTokenProgram.get_mint_program_id(mint, rpc);
|
|
5335
2647
|
const additionalSigners = stateless_js.dedupeSigner(payer, [authority]);
|
|
5336
2648
|
const ix = await CompressedTokenProgram.mintTo({
|
|
5337
2649
|
feePayer: payer.publicKey,
|
|
@@ -5340,6 +2652,7 @@ async function mintTo(rpc, payer, mint, destination, authority, amount, merkleTr
|
|
|
5340
2652
|
amount: amount,
|
|
5341
2653
|
toPubkey: destination,
|
|
5342
2654
|
merkleTree,
|
|
2655
|
+
tokenProgramId,
|
|
5343
2656
|
});
|
|
5344
2657
|
const { blockhash } = await rpc.getLatestBlockhash();
|
|
5345
2658
|
const tx = stateless_js.buildAndSignTx([web3_js.ComputeBudgetProgram.setComputeUnitLimit({ units: 1000000 }), ix], payer, blockhash, additionalSigners);
|
|
@@ -5403,10 +2716,14 @@ async function mergeTokenAccounts(rpc, payer, mint, owner, merkleTree, confirmOp
|
|
|
5403
2716
|
*
|
|
5404
2717
|
* @return transaction signature
|
|
5405
2718
|
*/
|
|
5406
|
-
async function createTokenPool(rpc, payer,
|
|
2719
|
+
async function createTokenPool(rpc, payer, mint, confirmOptions, tokenProgramId) {
|
|
2720
|
+
tokenProgramId = tokenProgramId
|
|
2721
|
+
? tokenProgramId
|
|
2722
|
+
: await CompressedTokenProgram.get_mint_program_id(mint, rpc);
|
|
5407
2723
|
const ix = await CompressedTokenProgram.createTokenPool({
|
|
5408
2724
|
feePayer: payer.publicKey,
|
|
5409
|
-
mint
|
|
2725
|
+
mint,
|
|
2726
|
+
tokenProgramId,
|
|
5410
2727
|
});
|
|
5411
2728
|
const { blockhash } = await rpc.getLatestBlockhash();
|
|
5412
2729
|
const tx = stateless_js.buildAndSignTx([ix], payer, blockhash);
|
|
@@ -5461,7 +2778,10 @@ async function createTokenProgramLookupTable(rpc, payer, authority, mints, addit
|
|
|
5461
2778
|
*
|
|
5462
2779
|
* @return Signature of the confirmed transaction
|
|
5463
2780
|
*/
|
|
5464
|
-
async function compressSplTokenAccount(rpc, payer, mint, owner, tokenAccount, outputStateTree, remainingAmount, confirmOptions) {
|
|
2781
|
+
async function compressSplTokenAccount(rpc, payer, mint, owner, tokenAccount, outputStateTree, remainingAmount, confirmOptions, tokenProgramId) {
|
|
2782
|
+
tokenProgramId = tokenProgramId
|
|
2783
|
+
? tokenProgramId
|
|
2784
|
+
: await CompressedTokenProgram.get_mint_program_id(mint, rpc);
|
|
5465
2785
|
const compressIx = await CompressedTokenProgram.compressSplTokenAccount({
|
|
5466
2786
|
feePayer: payer.publicKey,
|
|
5467
2787
|
authority: owner.publicKey,
|
|
@@ -5469,6 +2789,7 @@ async function compressSplTokenAccount(rpc, payer, mint, owner, tokenAccount, ou
|
|
|
5469
2789
|
mint,
|
|
5470
2790
|
remainingAmount,
|
|
5471
2791
|
outputStateTree,
|
|
2792
|
+
tokenProgramId,
|
|
5472
2793
|
});
|
|
5473
2794
|
const blockhashCtx = await rpc.getLatestBlockhash();
|
|
5474
2795
|
const additionalSigners = stateless_js.dedupeSigner(payer, [owner]);
|