@lightprotocol/compressed-token 0.14.0 → 0.14.1
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 +3120 -2100
- package/dist/cjs/browser/index.cjs.map +1 -1
- package/dist/cjs/node/index.cjs +1303 -515
- package/dist/cjs/node/index.cjs.map +1 -1
- package/dist/es/browser/index.js +3047 -2062
- package/dist/es/browser/index.js.map +1 -1
- package/dist/types/index.d.ts +361 -109
- package/package.json +5 -5
package/dist/cjs/node/index.cjs
CHANGED
|
@@ -1062,7 +1062,8 @@ const IDL = {
|
|
|
1062
1062
|
name: 'inputCompressedAccountsWithMerkleContext',
|
|
1063
1063
|
type: {
|
|
1064
1064
|
vec: {
|
|
1065
|
-
defined:
|
|
1065
|
+
defined:
|
|
1066
|
+
'PackedCompressedAccountWithMerkleContext',
|
|
1066
1067
|
},
|
|
1067
1068
|
},
|
|
1068
1069
|
},
|
|
@@ -1070,7 +1071,8 @@ const IDL = {
|
|
|
1070
1071
|
name: 'outputCompressedAccounts',
|
|
1071
1072
|
type: {
|
|
1072
1073
|
vec: {
|
|
1073
|
-
defined:
|
|
1074
|
+
defined:
|
|
1075
|
+
'OutputCompressedAccountWithPackedContext',
|
|
1074
1076
|
},
|
|
1075
1077
|
},
|
|
1076
1078
|
},
|
|
@@ -1126,7 +1128,8 @@ const IDL = {
|
|
|
1126
1128
|
name: 'inputCompressedAccountsWithMerkleContext',
|
|
1127
1129
|
type: {
|
|
1128
1130
|
vec: {
|
|
1129
|
-
defined:
|
|
1131
|
+
defined:
|
|
1132
|
+
'PackedCompressedAccountWithMerkleContext',
|
|
1130
1133
|
},
|
|
1131
1134
|
},
|
|
1132
1135
|
},
|
|
@@ -1134,7 +1137,8 @@ const IDL = {
|
|
|
1134
1137
|
name: 'outputCompressedAccounts',
|
|
1135
1138
|
type: {
|
|
1136
1139
|
vec: {
|
|
1137
|
-
defined:
|
|
1140
|
+
defined:
|
|
1141
|
+
'OutputCompressedAccountWithPackedContext',
|
|
1138
1142
|
},
|
|
1139
1143
|
},
|
|
1140
1144
|
},
|
|
@@ -1379,7 +1383,8 @@ const IDL = {
|
|
|
1379
1383
|
name: 'outputCompressedAccounts',
|
|
1380
1384
|
type: {
|
|
1381
1385
|
vec: {
|
|
1382
|
-
defined:
|
|
1386
|
+
defined:
|
|
1387
|
+
'OutputCompressedAccountWithPackedContext',
|
|
1383
1388
|
},
|
|
1384
1389
|
},
|
|
1385
1390
|
},
|
|
@@ -1556,23 +1561,128 @@ const IDL = {
|
|
|
1556
1561
|
errors: [
|
|
1557
1562
|
{
|
|
1558
1563
|
code: 6000,
|
|
1559
|
-
name: '
|
|
1560
|
-
msg: '
|
|
1564
|
+
name: 'PublicKeyAmountMissmatch',
|
|
1565
|
+
msg: 'public keys and amounts must be of same length',
|
|
1561
1566
|
},
|
|
1562
1567
|
{
|
|
1563
1568
|
code: 6001,
|
|
1564
|
-
name: '
|
|
1565
|
-
msg: '
|
|
1569
|
+
name: 'ComputeInputSumFailed',
|
|
1570
|
+
msg: 'ComputeInputSumFailed',
|
|
1566
1571
|
},
|
|
1567
1572
|
{
|
|
1568
1573
|
code: 6002,
|
|
1569
|
-
name: '
|
|
1570
|
-
msg: '
|
|
1574
|
+
name: 'ComputeOutputSumFailed',
|
|
1575
|
+
msg: 'ComputeOutputSumFailed',
|
|
1571
1576
|
},
|
|
1572
1577
|
{
|
|
1573
1578
|
code: 6003,
|
|
1574
|
-
name: '
|
|
1575
|
-
msg: '
|
|
1579
|
+
name: 'ComputeCompressSumFailed',
|
|
1580
|
+
msg: 'ComputeCompressSumFailed',
|
|
1581
|
+
},
|
|
1582
|
+
{
|
|
1583
|
+
code: 6004,
|
|
1584
|
+
name: 'ComputeDecompressSumFailed',
|
|
1585
|
+
msg: 'ComputeDecompressSumFailed',
|
|
1586
|
+
},
|
|
1587
|
+
{
|
|
1588
|
+
code: 6005,
|
|
1589
|
+
name: 'SumCheckFailed',
|
|
1590
|
+
msg: 'SumCheckFailed',
|
|
1591
|
+
},
|
|
1592
|
+
{
|
|
1593
|
+
code: 6006,
|
|
1594
|
+
name: 'DecompressRecipientUndefinedForDecompress',
|
|
1595
|
+
msg: 'DecompressRecipientUndefinedForDecompress',
|
|
1596
|
+
},
|
|
1597
|
+
{
|
|
1598
|
+
code: 6007,
|
|
1599
|
+
name: 'CompressedPdaUndefinedForDecompress',
|
|
1600
|
+
msg: 'CompressedPdaUndefinedForDecompress',
|
|
1601
|
+
},
|
|
1602
|
+
{
|
|
1603
|
+
code: 6008,
|
|
1604
|
+
name: 'DeCompressAmountUndefinedForDecompress',
|
|
1605
|
+
msg: 'DeCompressAmountUndefinedForDecompress',
|
|
1606
|
+
},
|
|
1607
|
+
{
|
|
1608
|
+
code: 6009,
|
|
1609
|
+
name: 'CompressedPdaUndefinedForCompress',
|
|
1610
|
+
msg: 'CompressedPdaUndefinedForCompress',
|
|
1611
|
+
},
|
|
1612
|
+
{
|
|
1613
|
+
code: 6010,
|
|
1614
|
+
name: 'DeCompressAmountUndefinedForCompress',
|
|
1615
|
+
msg: 'DeCompressAmountUndefinedForCompress',
|
|
1616
|
+
},
|
|
1617
|
+
{
|
|
1618
|
+
code: 6011,
|
|
1619
|
+
name: 'DelegateSignerCheckFailed',
|
|
1620
|
+
msg: 'DelegateSignerCheckFailed',
|
|
1621
|
+
},
|
|
1622
|
+
{
|
|
1623
|
+
code: 6012,
|
|
1624
|
+
name: 'MintTooLarge',
|
|
1625
|
+
msg: 'Minted amount greater than u64::MAX',
|
|
1626
|
+
},
|
|
1627
|
+
{
|
|
1628
|
+
code: 6013,
|
|
1629
|
+
name: 'SplTokenSupplyMismatch',
|
|
1630
|
+
msg: 'SplTokenSupplyMismatch',
|
|
1631
|
+
},
|
|
1632
|
+
{
|
|
1633
|
+
code: 6014,
|
|
1634
|
+
name: 'HeapMemoryCheckFailed',
|
|
1635
|
+
msg: 'HeapMemoryCheckFailed',
|
|
1636
|
+
},
|
|
1637
|
+
{
|
|
1638
|
+
code: 6015,
|
|
1639
|
+
name: 'InstructionNotCallable',
|
|
1640
|
+
msg: 'The instruction is not callable',
|
|
1641
|
+
},
|
|
1642
|
+
{
|
|
1643
|
+
code: 6016,
|
|
1644
|
+
name: 'ArithmeticUnderflow',
|
|
1645
|
+
msg: 'ArithmeticUnderflow',
|
|
1646
|
+
},
|
|
1647
|
+
{
|
|
1648
|
+
code: 6017,
|
|
1649
|
+
name: 'HashToFieldError',
|
|
1650
|
+
msg: 'HashToFieldError',
|
|
1651
|
+
},
|
|
1652
|
+
{
|
|
1653
|
+
code: 6018,
|
|
1654
|
+
name: 'InvalidAuthorityMint',
|
|
1655
|
+
msg: 'Expected the authority to be also a mint authority',
|
|
1656
|
+
},
|
|
1657
|
+
{
|
|
1658
|
+
code: 6019,
|
|
1659
|
+
name: 'InvalidFreezeAuthority',
|
|
1660
|
+
msg: 'Provided authority is not the freeze authority',
|
|
1661
|
+
},
|
|
1662
|
+
{
|
|
1663
|
+
code: 6020,
|
|
1664
|
+
name: 'InvalidDelegateIndex',
|
|
1665
|
+
},
|
|
1666
|
+
{
|
|
1667
|
+
code: 6021,
|
|
1668
|
+
name: 'TokenPoolPdaUndefined',
|
|
1669
|
+
},
|
|
1670
|
+
{
|
|
1671
|
+
code: 6022,
|
|
1672
|
+
name: 'IsTokenPoolPda',
|
|
1673
|
+
msg: 'Compress or decompress recipient is the same account as the token pool pda.',
|
|
1674
|
+
},
|
|
1675
|
+
{
|
|
1676
|
+
code: 6023,
|
|
1677
|
+
name: 'InvalidTokenPoolPda',
|
|
1678
|
+
},
|
|
1679
|
+
{
|
|
1680
|
+
code: 6024,
|
|
1681
|
+
name: 'NoInputTokenAccountsProvided',
|
|
1682
|
+
},
|
|
1683
|
+
{
|
|
1684
|
+
code: 6025,
|
|
1685
|
+
name: 'NoInputsProvided',
|
|
1576
1686
|
},
|
|
1577
1687
|
],
|
|
1578
1688
|
};
|
|
@@ -1582,20 +1692,37 @@ const IDL = {
|
|
|
1582
1692
|
* Packs Compressed Token Accounts.
|
|
1583
1693
|
*/
|
|
1584
1694
|
function packCompressedTokenAccounts(params) {
|
|
1585
|
-
const {
|
|
1695
|
+
const {
|
|
1696
|
+
inputCompressedTokenAccounts,
|
|
1697
|
+
outputStateTrees,
|
|
1698
|
+
remainingAccounts = [],
|
|
1699
|
+
rootIndices,
|
|
1700
|
+
tokenTransferOutputs,
|
|
1701
|
+
} = params;
|
|
1586
1702
|
const _remainingAccounts = remainingAccounts.slice();
|
|
1587
1703
|
let delegateIndex = null;
|
|
1588
|
-
if (
|
|
1589
|
-
inputCompressedTokenAccounts
|
|
1590
|
-
|
|
1704
|
+
if (
|
|
1705
|
+
inputCompressedTokenAccounts.length > 0 &&
|
|
1706
|
+
inputCompressedTokenAccounts[0].parsed.delegate
|
|
1707
|
+
) {
|
|
1708
|
+
delegateIndex = stateless_js.getIndexOrAdd(
|
|
1709
|
+
_remainingAccounts,
|
|
1710
|
+
inputCompressedTokenAccounts[0].parsed.delegate,
|
|
1711
|
+
);
|
|
1591
1712
|
}
|
|
1592
1713
|
/// TODO: move pubkeyArray to remainingAccounts
|
|
1593
1714
|
/// Currently just packs 'delegate' to pubkeyArray
|
|
1594
1715
|
const packedInputTokenData = [];
|
|
1595
1716
|
/// pack inputs
|
|
1596
1717
|
inputCompressedTokenAccounts.forEach((account, index) => {
|
|
1597
|
-
const merkleTreePubkeyIndex = stateless_js.getIndexOrAdd(
|
|
1598
|
-
|
|
1718
|
+
const merkleTreePubkeyIndex = stateless_js.getIndexOrAdd(
|
|
1719
|
+
_remainingAccounts,
|
|
1720
|
+
account.compressedAccount.merkleTree,
|
|
1721
|
+
);
|
|
1722
|
+
const nullifierQueuePubkeyIndex = stateless_js.getIndexOrAdd(
|
|
1723
|
+
_remainingAccounts,
|
|
1724
|
+
account.compressedAccount.nullifierQueue,
|
|
1725
|
+
);
|
|
1599
1726
|
packedInputTokenData.push({
|
|
1600
1727
|
amount: account.parsed.amount,
|
|
1601
1728
|
delegateIndex,
|
|
@@ -1613,15 +1740,27 @@ function packCompressedTokenAccounts(params) {
|
|
|
1613
1740
|
});
|
|
1614
1741
|
});
|
|
1615
1742
|
/// pack output state trees
|
|
1616
|
-
const paddedOutputStateMerkleTrees = stateless_js.padOutputStateMerkleTrees(
|
|
1743
|
+
const paddedOutputStateMerkleTrees = stateless_js.padOutputStateMerkleTrees(
|
|
1744
|
+
outputStateTrees,
|
|
1745
|
+
tokenTransferOutputs.length,
|
|
1746
|
+
inputCompressedTokenAccounts.map(acc => acc.compressedAccount),
|
|
1747
|
+
);
|
|
1617
1748
|
const packedOutputTokenData = [];
|
|
1618
1749
|
paddedOutputStateMerkleTrees.forEach((account, index) => {
|
|
1619
1750
|
var _a;
|
|
1620
|
-
const merkleTreeIndex = stateless_js.getIndexOrAdd(
|
|
1751
|
+
const merkleTreeIndex = stateless_js.getIndexOrAdd(
|
|
1752
|
+
_remainingAccounts,
|
|
1753
|
+
account,
|
|
1754
|
+
);
|
|
1621
1755
|
packedOutputTokenData.push({
|
|
1622
1756
|
owner: tokenTransferOutputs[index].owner,
|
|
1623
1757
|
amount: tokenTransferOutputs[index].amount,
|
|
1624
|
-
lamports: (
|
|
1758
|
+
lamports: (
|
|
1759
|
+
(_a = tokenTransferOutputs[index].lamports) === null ||
|
|
1760
|
+
_a === void 0
|
|
1761
|
+
? void 0
|
|
1762
|
+
: _a.eq(stateless_js.bn(0))
|
|
1763
|
+
)
|
|
1625
1764
|
? null
|
|
1626
1765
|
: tokenTransferOutputs[index].lamports,
|
|
1627
1766
|
merkleTreeIndex,
|
|
@@ -1629,7 +1768,7 @@ function packCompressedTokenAccounts(params) {
|
|
|
1629
1768
|
});
|
|
1630
1769
|
});
|
|
1631
1770
|
// to meta
|
|
1632
|
-
const remainingAccountMetas = _remainingAccounts.map(
|
|
1771
|
+
const remainingAccountMetas = _remainingAccounts.map(account => ({
|
|
1633
1772
|
pubkey: account,
|
|
1634
1773
|
isWritable: true,
|
|
1635
1774
|
isSigner: false,
|
|
@@ -1646,11 +1785,15 @@ const CPI_AUTHORITY_SEED = Buffer.from('cpi_authority');
|
|
|
1646
1785
|
const SPL_TOKEN_MINT_RENT_EXEMPT_BALANCE = 1461600;
|
|
1647
1786
|
|
|
1648
1787
|
/** Address of the SPL Token program */
|
|
1649
|
-
const TOKEN_PROGRAM_ID = new web3_js.PublicKey(
|
|
1788
|
+
const TOKEN_PROGRAM_ID = new web3_js.PublicKey(
|
|
1789
|
+
'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA',
|
|
1790
|
+
);
|
|
1650
1791
|
/** Address of the SPL Token 2022 program */
|
|
1651
1792
|
new web3_js.PublicKey('TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb');
|
|
1652
1793
|
/** Address of the SPL Associated Token Account program */
|
|
1653
|
-
const ASSOCIATED_TOKEN_PROGRAM_ID = new web3_js.PublicKey(
|
|
1794
|
+
const ASSOCIATED_TOKEN_PROGRAM_ID = new web3_js.PublicKey(
|
|
1795
|
+
'ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL',
|
|
1796
|
+
);
|
|
1654
1797
|
/** Address of the special mint for wrapped native SOL in spl-token */
|
|
1655
1798
|
new web3_js.PublicKey('So11111111111111111111111111111111111111112');
|
|
1656
1799
|
/** Address of the special mint for wrapped native SOL in spl-token-2022 */
|
|
@@ -1680,9 +1823,88 @@ var Layout$1 = {};
|
|
|
1680
1823
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
1681
1824
|
* THE SOFTWARE.
|
|
1682
1825
|
*/
|
|
1683
|
-
Object.defineProperty(Layout$1,
|
|
1684
|
-
Layout$1.s16 =
|
|
1685
|
-
Layout$1.
|
|
1826
|
+
Object.defineProperty(Layout$1, '__esModule', { value: true });
|
|
1827
|
+
Layout$1.s16 =
|
|
1828
|
+
Layout$1.s8 =
|
|
1829
|
+
Layout$1.nu64be =
|
|
1830
|
+
Layout$1.u48be =
|
|
1831
|
+
Layout$1.u40be =
|
|
1832
|
+
Layout$1.u32be =
|
|
1833
|
+
Layout$1.u24be =
|
|
1834
|
+
Layout$1.u16be =
|
|
1835
|
+
Layout$1.nu64 =
|
|
1836
|
+
Layout$1.u48 =
|
|
1837
|
+
Layout$1.u40 =
|
|
1838
|
+
u32 =
|
|
1839
|
+
Layout$1.u32 =
|
|
1840
|
+
Layout$1.u24 =
|
|
1841
|
+
Layout$1.u16 =
|
|
1842
|
+
u8 =
|
|
1843
|
+
Layout$1.u8 =
|
|
1844
|
+
Layout$1.offset =
|
|
1845
|
+
Layout$1.greedy =
|
|
1846
|
+
Layout$1.Constant =
|
|
1847
|
+
Layout$1.UTF8 =
|
|
1848
|
+
Layout$1.CString =
|
|
1849
|
+
Layout$1.Blob =
|
|
1850
|
+
Layout$1.Boolean =
|
|
1851
|
+
Layout$1.BitField =
|
|
1852
|
+
Layout$1.BitStructure =
|
|
1853
|
+
Layout$1.VariantLayout =
|
|
1854
|
+
Layout$1.Union =
|
|
1855
|
+
Layout$1.UnionLayoutDiscriminator =
|
|
1856
|
+
Layout$1.UnionDiscriminator =
|
|
1857
|
+
Layout$1.Structure =
|
|
1858
|
+
Layout$1.Sequence =
|
|
1859
|
+
Layout$1.DoubleBE =
|
|
1860
|
+
Layout$1.Double =
|
|
1861
|
+
Layout$1.FloatBE =
|
|
1862
|
+
Layout$1.Float =
|
|
1863
|
+
Layout$1.NearInt64BE =
|
|
1864
|
+
Layout$1.NearInt64 =
|
|
1865
|
+
Layout$1.NearUInt64BE =
|
|
1866
|
+
Layout$1.NearUInt64 =
|
|
1867
|
+
Layout$1.IntBE =
|
|
1868
|
+
Layout$1.Int =
|
|
1869
|
+
Layout$1.UIntBE =
|
|
1870
|
+
Layout$1.UInt =
|
|
1871
|
+
Layout$1.OffsetLayout =
|
|
1872
|
+
Layout$1.GreedyCount =
|
|
1873
|
+
Layout$1.ExternalLayout =
|
|
1874
|
+
Layout$1.bindConstructorLayout =
|
|
1875
|
+
Layout$1.nameWithProperty =
|
|
1876
|
+
Layout_2 =
|
|
1877
|
+
Layout$1.Layout =
|
|
1878
|
+
Layout$1.uint8ArrayToBuffer =
|
|
1879
|
+
Layout$1.checkUint8Array =
|
|
1880
|
+
void 0;
|
|
1881
|
+
Layout$1.constant =
|
|
1882
|
+
Layout$1.utf8 =
|
|
1883
|
+
Layout$1.cstr =
|
|
1884
|
+
blob =
|
|
1885
|
+
Layout$1.blob =
|
|
1886
|
+
Layout$1.unionLayoutDiscriminator =
|
|
1887
|
+
Layout$1.union =
|
|
1888
|
+
Layout$1.seq =
|
|
1889
|
+
Layout$1.bits =
|
|
1890
|
+
struct =
|
|
1891
|
+
Layout$1.struct =
|
|
1892
|
+
Layout$1.f64be =
|
|
1893
|
+
Layout$1.f64 =
|
|
1894
|
+
Layout$1.f32be =
|
|
1895
|
+
Layout$1.f32 =
|
|
1896
|
+
Layout$1.ns64be =
|
|
1897
|
+
Layout$1.s48be =
|
|
1898
|
+
Layout$1.s40be =
|
|
1899
|
+
Layout$1.s32be =
|
|
1900
|
+
Layout$1.s24be =
|
|
1901
|
+
Layout$1.s16be =
|
|
1902
|
+
Layout$1.ns64 =
|
|
1903
|
+
Layout$1.s48 =
|
|
1904
|
+
Layout$1.s40 =
|
|
1905
|
+
Layout$1.s32 =
|
|
1906
|
+
Layout$1.s24 =
|
|
1907
|
+
void 0;
|
|
1686
1908
|
const buffer_1 = require$$0;
|
|
1687
1909
|
/* Check if a value is a Uint8Array.
|
|
1688
1910
|
*
|
|
@@ -1828,7 +2050,7 @@ class Layout {
|
|
|
1828
2050
|
return undefined;
|
|
1829
2051
|
}
|
|
1830
2052
|
}
|
|
1831
|
-
var Layout_2 = Layout$1.Layout = Layout;
|
|
2053
|
+
var Layout_2 = (Layout$1.Layout = Layout);
|
|
1832
2054
|
/* Provide text that carries a name (such as for a function that will
|
|
1833
2055
|
* be throwing an error) annotated with the property of a given layout
|
|
1834
2056
|
* (such as one for which the value was unacceptable).
|
|
@@ -1877,7 +2099,7 @@ function bindConstructorLayout(Class, layout) {
|
|
|
1877
2099
|
if (Object.prototype.hasOwnProperty.call(Class, 'layout_')) {
|
|
1878
2100
|
throw new Error('Class is already bound to a layout');
|
|
1879
2101
|
}
|
|
1880
|
-
if (!(layout &&
|
|
2102
|
+
if (!(layout && layout instanceof Layout)) {
|
|
1881
2103
|
throw new TypeError('layout must be a Layout');
|
|
1882
2104
|
}
|
|
1883
2105
|
if (Object.prototype.hasOwnProperty.call(layout, 'boundConstructor_')) {
|
|
@@ -1885,7 +2107,7 @@ function bindConstructorLayout(Class, layout) {
|
|
|
1885
2107
|
}
|
|
1886
2108
|
Class.layout_ = layout;
|
|
1887
2109
|
layout.boundConstructor_ = Class;
|
|
1888
|
-
layout.makeDestinationObject = (
|
|
2110
|
+
layout.makeDestinationObject = () => new Class();
|
|
1889
2111
|
Object.defineProperty(Class.prototype, 'encode', {
|
|
1890
2112
|
value(b, offset) {
|
|
1891
2113
|
return layout.encode(this, b, offset);
|
|
@@ -1955,7 +2177,7 @@ Layout$1.ExternalLayout = ExternalLayout;
|
|
|
1955
2177
|
*/
|
|
1956
2178
|
class GreedyCount extends ExternalLayout {
|
|
1957
2179
|
constructor(elementSpan = 1, property) {
|
|
1958
|
-
if (
|
|
2180
|
+
if (!Number.isInteger(elementSpan) || 0 >= elementSpan) {
|
|
1959
2181
|
throw new TypeError('elementSpan must be a (positive) integer');
|
|
1960
2182
|
}
|
|
1961
2183
|
super(-1, property);
|
|
@@ -2021,8 +2243,7 @@ class OffsetLayout extends ExternalLayout {
|
|
|
2021
2243
|
}
|
|
2022
2244
|
/** @override */
|
|
2023
2245
|
isCount() {
|
|
2024
|
-
return
|
|
2025
|
-
|| (this.layout instanceof UIntBE));
|
|
2246
|
+
return this.layout instanceof UInt || this.layout instanceof UIntBE;
|
|
2026
2247
|
}
|
|
2027
2248
|
/** @override */
|
|
2028
2249
|
decode(b, offset = 0) {
|
|
@@ -2175,7 +2396,7 @@ const V2E32 = Math.pow(2, 32);
|
|
|
2175
2396
|
* non-negative. */
|
|
2176
2397
|
function divmodInt64(src) {
|
|
2177
2398
|
const hi32 = Math.floor(src / V2E32);
|
|
2178
|
-
const lo32 = src -
|
|
2399
|
+
const lo32 = src - hi32 * V2E32;
|
|
2179
2400
|
return { hi32, lo32 };
|
|
2180
2401
|
}
|
|
2181
2402
|
/* Reconstruct Number from quotient and non-negative remainder */
|
|
@@ -2432,14 +2653,19 @@ class Sequence extends Layout {
|
|
|
2432
2653
|
if (!(elementLayout instanceof Layout)) {
|
|
2433
2654
|
throw new TypeError('elementLayout must be a Layout');
|
|
2434
2655
|
}
|
|
2435
|
-
if (
|
|
2436
|
-
|
|
2437
|
-
|
|
2438
|
-
|
|
2656
|
+
if (
|
|
2657
|
+
!(
|
|
2658
|
+
(count instanceof ExternalLayout && count.isCount()) ||
|
|
2659
|
+
(Number.isInteger(count) && 0 <= count)
|
|
2660
|
+
)
|
|
2661
|
+
) {
|
|
2662
|
+
throw new TypeError(
|
|
2663
|
+
'count must be non-negative integer ' +
|
|
2664
|
+
'or an unsigned integer ExternalLayout',
|
|
2665
|
+
);
|
|
2439
2666
|
}
|
|
2440
2667
|
let span = -1;
|
|
2441
|
-
if (
|
|
2442
|
-
&& (0 < elementLayout.span)) {
|
|
2668
|
+
if (!(count instanceof ExternalLayout) && 0 < elementLayout.span) {
|
|
2443
2669
|
span = count * elementLayout.span;
|
|
2444
2670
|
}
|
|
2445
2671
|
super(span, property);
|
|
@@ -2464,8 +2690,7 @@ class Sequence extends Layout {
|
|
|
2464
2690
|
}
|
|
2465
2691
|
if (0 < this.elementLayout.span) {
|
|
2466
2692
|
span = count * this.elementLayout.span;
|
|
2467
|
-
}
|
|
2468
|
-
else {
|
|
2693
|
+
} else {
|
|
2469
2694
|
let idx = 0;
|
|
2470
2695
|
while (idx < count) {
|
|
2471
2696
|
span += this.elementLayout.getSpan(b, offset + span);
|
|
@@ -2545,27 +2770,30 @@ Layout$1.Sequence = Sequence;
|
|
|
2545
2770
|
*/
|
|
2546
2771
|
class Structure extends Layout {
|
|
2547
2772
|
constructor(fields, property, decodePrefixes) {
|
|
2548
|
-
if (
|
|
2549
|
-
|
|
2773
|
+
if (
|
|
2774
|
+
!(
|
|
2775
|
+
Array.isArray(fields) &&
|
|
2776
|
+
fields.reduce((acc, v) => acc && v instanceof Layout, true)
|
|
2777
|
+
)
|
|
2778
|
+
) {
|
|
2550
2779
|
throw new TypeError('fields must be array of Layout instances');
|
|
2551
2780
|
}
|
|
2552
|
-
if (
|
|
2553
|
-
&& (undefined === decodePrefixes)) {
|
|
2781
|
+
if ('boolean' === typeof property && undefined === decodePrefixes) {
|
|
2554
2782
|
decodePrefixes = property;
|
|
2555
2783
|
property = undefined;
|
|
2556
2784
|
}
|
|
2557
2785
|
/* Verify absence of unnamed variable-length fields. */
|
|
2558
2786
|
for (const fd of fields) {
|
|
2559
|
-
if (
|
|
2560
|
-
|
|
2561
|
-
|
|
2787
|
+
if (0 > fd.span && undefined === fd.property) {
|
|
2788
|
+
throw new Error(
|
|
2789
|
+
'fields cannot contain unnamed variable-length layout',
|
|
2790
|
+
);
|
|
2562
2791
|
}
|
|
2563
2792
|
}
|
|
2564
2793
|
let span = -1;
|
|
2565
2794
|
try {
|
|
2566
2795
|
span = fields.reduce((span, fd) => span + fd.getSpan(), 0);
|
|
2567
|
-
}
|
|
2568
|
-
catch (e) {
|
|
2796
|
+
} catch (e) {
|
|
2569
2797
|
// ignore error
|
|
2570
2798
|
}
|
|
2571
2799
|
super(span, property);
|
|
@@ -2603,8 +2831,7 @@ class Structure extends Layout {
|
|
|
2603
2831
|
offset += fsp;
|
|
2604
2832
|
return span + fsp;
|
|
2605
2833
|
}, 0);
|
|
2606
|
-
}
|
|
2607
|
-
catch (e) {
|
|
2834
|
+
} catch (e) {
|
|
2608
2835
|
throw new RangeError('indeterminate span');
|
|
2609
2836
|
}
|
|
2610
2837
|
return span;
|
|
@@ -2618,8 +2845,7 @@ class Structure extends Layout {
|
|
|
2618
2845
|
dest[fd.property] = fd.decode(b, offset);
|
|
2619
2846
|
}
|
|
2620
2847
|
offset += fd.getSpan(b, offset);
|
|
2621
|
-
if (this.decodePrefixes
|
|
2622
|
-
&& (b.length === offset)) {
|
|
2848
|
+
if (this.decodePrefixes && b.length === offset) {
|
|
2623
2849
|
break;
|
|
2624
2850
|
}
|
|
2625
2851
|
}
|
|
@@ -2636,7 +2862,7 @@ class Structure extends Layout {
|
|
|
2636
2862
|
let lastWrote = 0;
|
|
2637
2863
|
for (const fd of this.fields) {
|
|
2638
2864
|
let span = fd.span;
|
|
2639
|
-
lastWrote =
|
|
2865
|
+
lastWrote = 0 < span ? span : 0;
|
|
2640
2866
|
if (undefined !== fd.property) {
|
|
2641
2867
|
const fv = src[fd.property];
|
|
2642
2868
|
if (undefined !== fv) {
|
|
@@ -2655,14 +2881,13 @@ class Structure extends Layout {
|
|
|
2655
2881
|
* item may have had a dynamic length and we don't want to include
|
|
2656
2882
|
* the padding between it and the end of the space reserved for
|
|
2657
2883
|
* it. */
|
|
2658
|
-
return
|
|
2884
|
+
return lastOffset + lastWrote - firstOffset;
|
|
2659
2885
|
}
|
|
2660
2886
|
/** @override */
|
|
2661
2887
|
fromArray(values) {
|
|
2662
2888
|
const dest = this.makeDestinationObject();
|
|
2663
2889
|
for (const fd of this.fields) {
|
|
2664
|
-
if (
|
|
2665
|
-
&& (0 < values.length)) {
|
|
2890
|
+
if (undefined !== fd.property && 0 < values.length) {
|
|
2666
2891
|
dest[fd.property] = values.shift();
|
|
2667
2892
|
}
|
|
2668
2893
|
}
|
|
@@ -2709,8 +2934,7 @@ class Structure extends Layout {
|
|
|
2709
2934
|
}
|
|
2710
2935
|
if (0 > fd.span) {
|
|
2711
2936
|
offset = -1;
|
|
2712
|
-
}
|
|
2713
|
-
else if (0 <= offset) {
|
|
2937
|
+
} else if (0 <= offset) {
|
|
2714
2938
|
offset += fd.span;
|
|
2715
2939
|
}
|
|
2716
2940
|
}
|
|
@@ -2775,9 +2999,10 @@ Layout$1.UnionDiscriminator = UnionDiscriminator;
|
|
|
2775
2999
|
*/
|
|
2776
3000
|
class UnionLayoutDiscriminator extends UnionDiscriminator {
|
|
2777
3001
|
constructor(layout, property) {
|
|
2778
|
-
if (!(
|
|
2779
|
-
|
|
2780
|
-
|
|
3002
|
+
if (!(layout instanceof ExternalLayout && layout.isCount())) {
|
|
3003
|
+
throw new TypeError(
|
|
3004
|
+
'layout must be an unsigned integer ExternalLayout',
|
|
3005
|
+
);
|
|
2781
3006
|
}
|
|
2782
3007
|
super(property || layout.property || 'variant');
|
|
2783
3008
|
/** The {@link ExternalLayout} used to access the discriminator
|
|
@@ -2856,26 +3081,24 @@ Layout$1.UnionLayoutDiscriminator = UnionLayoutDiscriminator;
|
|
|
2856
3081
|
class Union extends Layout {
|
|
2857
3082
|
constructor(discr, defaultLayout, property) {
|
|
2858
3083
|
let discriminator;
|
|
2859
|
-
if (
|
|
2860
|
-
|
|
2861
|
-
|
|
2862
|
-
|
|
2863
|
-
else if (
|
|
2864
|
-
&& discr.isCount()) {
|
|
3084
|
+
if (discr instanceof UInt || discr instanceof UIntBE) {
|
|
3085
|
+
discriminator = new UnionLayoutDiscriminator(
|
|
3086
|
+
new OffsetLayout(discr),
|
|
3087
|
+
);
|
|
3088
|
+
} else if (discr instanceof ExternalLayout && discr.isCount()) {
|
|
2865
3089
|
discriminator = new UnionLayoutDiscriminator(discr);
|
|
2866
|
-
}
|
|
2867
|
-
|
|
2868
|
-
|
|
2869
|
-
|
|
2870
|
-
|
|
2871
|
-
else {
|
|
3090
|
+
} else if (!(discr instanceof UnionDiscriminator)) {
|
|
3091
|
+
throw new TypeError(
|
|
3092
|
+
'discr must be a UnionDiscriminator ' +
|
|
3093
|
+
'or an unsigned integer layout',
|
|
3094
|
+
);
|
|
3095
|
+
} else {
|
|
2872
3096
|
discriminator = discr;
|
|
2873
3097
|
}
|
|
2874
3098
|
if (undefined === defaultLayout) {
|
|
2875
3099
|
defaultLayout = null;
|
|
2876
3100
|
}
|
|
2877
|
-
if (!(
|
|
2878
|
-
|| (defaultLayout instanceof Layout))) {
|
|
3101
|
+
if (!(null === defaultLayout || defaultLayout instanceof Layout)) {
|
|
2879
3102
|
throw new TypeError('defaultLayout must be null or a Layout');
|
|
2880
3103
|
}
|
|
2881
3104
|
if (null !== defaultLayout) {
|
|
@@ -2893,8 +3116,10 @@ class Union extends Layout {
|
|
|
2893
3116
|
let span = -1;
|
|
2894
3117
|
if (defaultLayout) {
|
|
2895
3118
|
span = defaultLayout.span;
|
|
2896
|
-
if (
|
|
2897
|
-
|
|
3119
|
+
if (
|
|
3120
|
+
0 <= span &&
|
|
3121
|
+
(discr instanceof UInt || discr instanceof UIntBE)
|
|
3122
|
+
) {
|
|
2898
3123
|
span += discriminator.layout.span;
|
|
2899
3124
|
}
|
|
2900
3125
|
}
|
|
@@ -2913,8 +3138,8 @@ class Union extends Layout {
|
|
|
2913
3138
|
*
|
|
2914
3139
|
* If `false` the discriminator is obtained from somewhere
|
|
2915
3140
|
* else. */
|
|
2916
|
-
this.usesPrefixDiscriminator =
|
|
2917
|
-
||
|
|
3141
|
+
this.usesPrefixDiscriminator =
|
|
3142
|
+
discr instanceof UInt || discr instanceof UIntBE;
|
|
2918
3143
|
/** The layout for non-discriminator content when the value of the
|
|
2919
3144
|
* discriminator is not recognized.
|
|
2920
3145
|
*
|
|
@@ -2979,7 +3204,9 @@ class Union extends Layout {
|
|
|
2979
3204
|
* determine the span. */
|
|
2980
3205
|
const vlo = this.getVariant(b, offset);
|
|
2981
3206
|
if (!vlo) {
|
|
2982
|
-
throw new Error(
|
|
3207
|
+
throw new Error(
|
|
3208
|
+
'unable to determine span for unrecognized variant',
|
|
3209
|
+
);
|
|
2983
3210
|
}
|
|
2984
3211
|
return vlo.getSpan(b, offset);
|
|
2985
3212
|
}
|
|
@@ -3013,22 +3240,41 @@ class Union extends Layout {
|
|
|
3013
3240
|
* registered variant.
|
|
3014
3241
|
*/
|
|
3015
3242
|
defaultGetSourceVariant(src) {
|
|
3016
|
-
if (
|
|
3017
|
-
|
|
3018
|
-
|
|
3243
|
+
if (
|
|
3244
|
+
Object.prototype.hasOwnProperty.call(
|
|
3245
|
+
src,
|
|
3246
|
+
this.discriminator.property,
|
|
3247
|
+
)
|
|
3248
|
+
) {
|
|
3249
|
+
if (
|
|
3250
|
+
this.defaultLayout &&
|
|
3251
|
+
this.defaultLayout.property &&
|
|
3252
|
+
Object.prototype.hasOwnProperty.call(
|
|
3253
|
+
src,
|
|
3254
|
+
this.defaultLayout.property,
|
|
3255
|
+
)
|
|
3256
|
+
) {
|
|
3019
3257
|
return undefined;
|
|
3020
3258
|
}
|
|
3021
3259
|
const vlo = this.registry[src[this.discriminator.property]];
|
|
3022
|
-
if (
|
|
3023
|
-
&&
|
|
3024
|
-
|
|
3260
|
+
if (
|
|
3261
|
+
vlo &&
|
|
3262
|
+
(!vlo.layout ||
|
|
3263
|
+
(vlo.property &&
|
|
3264
|
+
Object.prototype.hasOwnProperty.call(
|
|
3265
|
+
src,
|
|
3266
|
+
vlo.property,
|
|
3267
|
+
)))
|
|
3268
|
+
) {
|
|
3025
3269
|
return vlo;
|
|
3026
3270
|
}
|
|
3027
|
-
}
|
|
3028
|
-
else {
|
|
3271
|
+
} else {
|
|
3029
3272
|
for (const tag in this.registry) {
|
|
3030
3273
|
const vlo = this.registry[tag];
|
|
3031
|
-
if (
|
|
3274
|
+
if (
|
|
3275
|
+
vlo.property &&
|
|
3276
|
+
Object.prototype.hasOwnProperty.call(src, vlo.property)
|
|
3277
|
+
) {
|
|
3032
3278
|
return vlo;
|
|
3033
3279
|
}
|
|
3034
3280
|
}
|
|
@@ -3056,9 +3302,11 @@ class Union extends Layout {
|
|
|
3056
3302
|
dest[dlo.property] = discr;
|
|
3057
3303
|
// defaultLayout.property can be undefined, but this is allowed by buffer-layout
|
|
3058
3304
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
3059
|
-
dest[defaultLayout.property] = defaultLayout.decode(
|
|
3060
|
-
|
|
3061
|
-
|
|
3305
|
+
dest[defaultLayout.property] = defaultLayout.decode(
|
|
3306
|
+
b,
|
|
3307
|
+
offset + contentOffset,
|
|
3308
|
+
);
|
|
3309
|
+
} else {
|
|
3062
3310
|
dest = clo.decode(b, offset);
|
|
3063
3311
|
}
|
|
3064
3312
|
return dest;
|
|
@@ -3083,7 +3331,10 @@ class Union extends Layout {
|
|
|
3083
3331
|
dlo.encode(src[dlo.property], b, offset);
|
|
3084
3332
|
// clo.property is not undefined when vlo is undefined
|
|
3085
3333
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
3086
|
-
return
|
|
3334
|
+
return (
|
|
3335
|
+
contentOffset +
|
|
3336
|
+
clo.encode(src[clo.property], b, offset + contentOffset)
|
|
3337
|
+
);
|
|
3087
3338
|
}
|
|
3088
3339
|
return vlo.encode(src, b, offset);
|
|
3089
3340
|
}
|
|
@@ -3123,8 +3374,7 @@ class Union extends Layout {
|
|
|
3123
3374
|
let variant;
|
|
3124
3375
|
if (vb instanceof Uint8Array) {
|
|
3125
3376
|
variant = this.discriminator.decode(vb, offset);
|
|
3126
|
-
}
|
|
3127
|
-
else {
|
|
3377
|
+
} else {
|
|
3128
3378
|
variant = vb;
|
|
3129
3379
|
}
|
|
3130
3380
|
return this.registry[variant];
|
|
@@ -3165,11 +3415,10 @@ class VariantLayout extends Layout {
|
|
|
3165
3415
|
if (!(union instanceof Union)) {
|
|
3166
3416
|
throw new TypeError('union must be a Union');
|
|
3167
3417
|
}
|
|
3168
|
-
if (
|
|
3418
|
+
if (!Number.isInteger(variant) || 0 > variant) {
|
|
3169
3419
|
throw new TypeError('variant must be a (non-negative) integer');
|
|
3170
3420
|
}
|
|
3171
|
-
if (
|
|
3172
|
-
&& (undefined === property)) {
|
|
3421
|
+
if ('string' === typeof layout && undefined === property) {
|
|
3173
3422
|
property = layout;
|
|
3174
3423
|
layout = null;
|
|
3175
3424
|
}
|
|
@@ -3177,10 +3426,14 @@ class VariantLayout extends Layout {
|
|
|
3177
3426
|
if (!(layout instanceof Layout)) {
|
|
3178
3427
|
throw new TypeError('layout must be a Layout');
|
|
3179
3428
|
}
|
|
3180
|
-
if (
|
|
3181
|
-
|
|
3182
|
-
|
|
3183
|
-
|
|
3429
|
+
if (
|
|
3430
|
+
null !== union.defaultLayout &&
|
|
3431
|
+
0 <= layout.span &&
|
|
3432
|
+
layout.span > union.defaultLayout.span
|
|
3433
|
+
) {
|
|
3434
|
+
throw new Error(
|
|
3435
|
+
'variant span exceeds span of containing union',
|
|
3436
|
+
);
|
|
3184
3437
|
}
|
|
3185
3438
|
if ('string' !== typeof property) {
|
|
3186
3439
|
throw new TypeError('variant must have a String property');
|
|
@@ -3189,7 +3442,7 @@ class VariantLayout extends Layout {
|
|
|
3189
3442
|
let span = union.span;
|
|
3190
3443
|
if (0 > union.span) {
|
|
3191
3444
|
span = layout ? layout.span : 0;
|
|
3192
|
-
if (
|
|
3445
|
+
if (0 <= span && union.usesPrefixDiscriminator) {
|
|
3193
3446
|
span += union.discriminator.layout.span;
|
|
3194
3447
|
}
|
|
3195
3448
|
}
|
|
@@ -3236,11 +3489,9 @@ class VariantLayout extends Layout {
|
|
|
3236
3489
|
}
|
|
3237
3490
|
if (this.layout) {
|
|
3238
3491
|
dest[this.property] = this.layout.decode(b, offset + contentOffset);
|
|
3239
|
-
}
|
|
3240
|
-
else if (this.property) {
|
|
3492
|
+
} else if (this.property) {
|
|
3241
3493
|
dest[this.property] = true;
|
|
3242
|
-
}
|
|
3243
|
-
else if (this.union.usesPrefixDiscriminator) {
|
|
3494
|
+
} else if (this.union.usesPrefixDiscriminator) {
|
|
3244
3495
|
dest[this.union.discriminator.property] = this.variant;
|
|
3245
3496
|
}
|
|
3246
3497
|
return dest;
|
|
@@ -3251,8 +3502,10 @@ class VariantLayout extends Layout {
|
|
|
3251
3502
|
if (this.union.usesPrefixDiscriminator) {
|
|
3252
3503
|
contentOffset = this.union.discriminator.layout.span;
|
|
3253
3504
|
}
|
|
3254
|
-
if (
|
|
3255
|
-
|
|
3505
|
+
if (
|
|
3506
|
+
this.layout &&
|
|
3507
|
+
!Object.prototype.hasOwnProperty.call(src, this.property)
|
|
3508
|
+
) {
|
|
3256
3509
|
throw new TypeError('variant lacks property ' + this.property);
|
|
3257
3510
|
}
|
|
3258
3511
|
this.union.discriminator.encode(this.variant, b, offset);
|
|
@@ -3260,8 +3513,7 @@ class VariantLayout extends Layout {
|
|
|
3260
3513
|
if (this.layout) {
|
|
3261
3514
|
this.layout.encode(src[this.property], b, offset + contentOffset);
|
|
3262
3515
|
span += this.layout.getSpan(b, offset + contentOffset);
|
|
3263
|
-
if (
|
|
3264
|
-
&& (span > this.union.span)) {
|
|
3516
|
+
if (0 <= this.union.span && span > this.union.span) {
|
|
3265
3517
|
throw new Error('encoded variant overruns containing union');
|
|
3266
3518
|
}
|
|
3267
3519
|
}
|
|
@@ -3322,12 +3574,10 @@ function fixBitwiseResult(v) {
|
|
|
3322
3574
|
*/
|
|
3323
3575
|
class BitStructure extends Layout {
|
|
3324
3576
|
constructor(word, msb, property) {
|
|
3325
|
-
if (!(
|
|
3326
|
-
|| (word instanceof UIntBE))) {
|
|
3577
|
+
if (!(word instanceof UInt || word instanceof UIntBE)) {
|
|
3327
3578
|
throw new TypeError('word must be a UInt or UIntBE layout');
|
|
3328
3579
|
}
|
|
3329
|
-
if (
|
|
3330
|
-
&& (undefined === property)) {
|
|
3580
|
+
if ('string' === typeof msb && undefined === property) {
|
|
3331
3581
|
property = msb;
|
|
3332
3582
|
msb = false;
|
|
3333
3583
|
}
|
|
@@ -3471,15 +3721,19 @@ class BitField {
|
|
|
3471
3721
|
if (!(container instanceof BitStructure)) {
|
|
3472
3722
|
throw new TypeError('container must be a BitStructure');
|
|
3473
3723
|
}
|
|
3474
|
-
if (
|
|
3724
|
+
if (!Number.isInteger(bits) || 0 >= bits) {
|
|
3475
3725
|
throw new TypeError('bits must be positive integer');
|
|
3476
3726
|
}
|
|
3477
3727
|
const totalBits = 8 * container.span;
|
|
3478
3728
|
const usedBits = container.fields.reduce((sum, fd) => sum + fd.bits, 0);
|
|
3479
|
-
if (
|
|
3480
|
-
throw new Error(
|
|
3481
|
-
|
|
3482
|
-
|
|
3729
|
+
if (bits + usedBits > totalBits) {
|
|
3730
|
+
throw new Error(
|
|
3731
|
+
'bits too long for span remainder (' +
|
|
3732
|
+
(totalBits - usedBits) +
|
|
3733
|
+
' of ' +
|
|
3734
|
+
totalBits +
|
|
3735
|
+
' remain)',
|
|
3736
|
+
);
|
|
3483
3737
|
}
|
|
3484
3738
|
/** The {@link BitStructure} instance to which this bit field
|
|
3485
3739
|
* belongs. */
|
|
@@ -3492,8 +3746,9 @@ class BitField {
|
|
|
3492
3746
|
* That is, it masks a value that has not yet been shifted into
|
|
3493
3747
|
* position within its containing packed integer. */
|
|
3494
3748
|
this.valueMask = (1 << bits) - 1;
|
|
3495
|
-
if (32 === bits) {
|
|
3496
|
-
|
|
3749
|
+
if (32 === bits) {
|
|
3750
|
+
// shifted value out of range
|
|
3751
|
+
this.valueMask = 0xffffffff;
|
|
3497
3752
|
}
|
|
3498
3753
|
/** The offset of the value within the containing packed unsigned
|
|
3499
3754
|
* integer. The least significant bit of the packed value is at
|
|
@@ -3531,16 +3786,22 @@ class BitField {
|
|
|
3531
3786
|
* **NOTE** This is not a specialization of {@link
|
|
3532
3787
|
* Layout#encode|Layout.encode} and there is no return value. */
|
|
3533
3788
|
encode(value) {
|
|
3534
|
-
if (
|
|
3535
|
-
|
|
3536
|
-
|
|
3537
|
-
|
|
3538
|
-
|
|
3789
|
+
if (
|
|
3790
|
+
'number' !== typeof value ||
|
|
3791
|
+
!Number.isInteger(value) ||
|
|
3792
|
+
value !== fixBitwiseResult(value & this.valueMask)
|
|
3793
|
+
) {
|
|
3794
|
+
throw new TypeError(
|
|
3795
|
+
nameWithProperty('BitField.encode', this) +
|
|
3796
|
+
' value must be integer not exceeding ' +
|
|
3797
|
+
this.valueMask,
|
|
3798
|
+
);
|
|
3539
3799
|
}
|
|
3540
3800
|
const word = this.container._packedGetValue();
|
|
3541
3801
|
const wordValue = fixBitwiseResult(value << this.start);
|
|
3542
|
-
this.container._packedSetValue(
|
|
3543
|
-
| wordValue
|
|
3802
|
+
this.container._packedSetValue(
|
|
3803
|
+
fixBitwiseResult(word & ~this.wordMask) | wordValue,
|
|
3804
|
+
);
|
|
3544
3805
|
}
|
|
3545
3806
|
}
|
|
3546
3807
|
Layout$1.BitField = BitField;
|
|
@@ -3598,10 +3859,16 @@ Layout$1.Boolean = Boolean;
|
|
|
3598
3859
|
*/
|
|
3599
3860
|
class Blob extends Layout {
|
|
3600
3861
|
constructor(length, property) {
|
|
3601
|
-
if (
|
|
3602
|
-
|
|
3603
|
-
|
|
3604
|
-
|
|
3862
|
+
if (
|
|
3863
|
+
!(
|
|
3864
|
+
(length instanceof ExternalLayout && length.isCount()) ||
|
|
3865
|
+
(Number.isInteger(length) && 0 <= length)
|
|
3866
|
+
)
|
|
3867
|
+
) {
|
|
3868
|
+
throw new TypeError(
|
|
3869
|
+
'length must be positive integer ' +
|
|
3870
|
+
'or an unsigned integer ExternalLayout',
|
|
3871
|
+
);
|
|
3605
3872
|
}
|
|
3606
3873
|
let span = -1;
|
|
3607
3874
|
if (!(length instanceof ExternalLayout)) {
|
|
@@ -3642,14 +3909,23 @@ class Blob extends Layout {
|
|
|
3642
3909
|
span = src.length;
|
|
3643
3910
|
}
|
|
3644
3911
|
if (!(src instanceof Uint8Array && span === src.length)) {
|
|
3645
|
-
throw new TypeError(
|
|
3646
|
-
|
|
3912
|
+
throw new TypeError(
|
|
3913
|
+
nameWithProperty('Blob.encode', this) +
|
|
3914
|
+
' requires (length ' +
|
|
3915
|
+
span +
|
|
3916
|
+
') Uint8Array as src',
|
|
3917
|
+
);
|
|
3647
3918
|
}
|
|
3648
|
-
if (
|
|
3919
|
+
if (offset + span > b.length) {
|
|
3649
3920
|
throw new RangeError('encoding overruns Uint8Array');
|
|
3650
3921
|
}
|
|
3651
3922
|
const srcBuffer = uint8ArrayToBuffer(src);
|
|
3652
|
-
uint8ArrayToBuffer(b).write(
|
|
3923
|
+
uint8ArrayToBuffer(b).write(
|
|
3924
|
+
srcBuffer.toString('hex'),
|
|
3925
|
+
offset,
|
|
3926
|
+
span,
|
|
3927
|
+
'hex',
|
|
3928
|
+
);
|
|
3653
3929
|
if (this.length instanceof ExternalLayout) {
|
|
3654
3930
|
this.length.encode(span, b, offset);
|
|
3655
3931
|
}
|
|
@@ -3678,7 +3954,7 @@ class CString extends Layout {
|
|
|
3678
3954
|
getSpan(b, offset = 0) {
|
|
3679
3955
|
checkUint8Array(b);
|
|
3680
3956
|
let idx = offset;
|
|
3681
|
-
while (
|
|
3957
|
+
while (idx < b.length && 0 !== b[idx]) {
|
|
3682
3958
|
idx += 1;
|
|
3683
3959
|
}
|
|
3684
3960
|
return 1 + idx - offset;
|
|
@@ -3686,7 +3962,9 @@ class CString extends Layout {
|
|
|
3686
3962
|
/** @override */
|
|
3687
3963
|
decode(b, offset = 0) {
|
|
3688
3964
|
const span = this.getSpan(b, offset);
|
|
3689
|
-
return uint8ArrayToBuffer(b)
|
|
3965
|
+
return uint8ArrayToBuffer(b)
|
|
3966
|
+
.slice(offset, offset + span - 1)
|
|
3967
|
+
.toString('utf-8');
|
|
3690
3968
|
}
|
|
3691
3969
|
/** @override */
|
|
3692
3970
|
encode(src, b, offset = 0) {
|
|
@@ -3698,7 +3976,7 @@ class CString extends Layout {
|
|
|
3698
3976
|
}
|
|
3699
3977
|
const srcb = buffer_1.Buffer.from(src, 'utf8');
|
|
3700
3978
|
const span = srcb.length;
|
|
3701
|
-
if (
|
|
3979
|
+
if (offset + span > b.length) {
|
|
3702
3980
|
throw new RangeError('encoding overruns Buffer');
|
|
3703
3981
|
}
|
|
3704
3982
|
const buffer = uint8ArrayToBuffer(b);
|
|
@@ -3729,14 +4007,13 @@ Layout$1.CString = CString;
|
|
|
3729
4007
|
*/
|
|
3730
4008
|
class UTF8 extends Layout {
|
|
3731
4009
|
constructor(maxSpan, property) {
|
|
3732
|
-
if (
|
|
4010
|
+
if ('string' === typeof maxSpan && undefined === property) {
|
|
3733
4011
|
property = maxSpan;
|
|
3734
4012
|
maxSpan = undefined;
|
|
3735
4013
|
}
|
|
3736
4014
|
if (undefined === maxSpan) {
|
|
3737
4015
|
maxSpan = -1;
|
|
3738
|
-
}
|
|
3739
|
-
else if (!Number.isInteger(maxSpan)) {
|
|
4016
|
+
} else if (!Number.isInteger(maxSpan)) {
|
|
3740
4017
|
throw new TypeError('maxSpan must be an integer');
|
|
3741
4018
|
}
|
|
3742
4019
|
super(-1, property);
|
|
@@ -3758,11 +4035,12 @@ class UTF8 extends Layout {
|
|
|
3758
4035
|
/** @override */
|
|
3759
4036
|
decode(b, offset = 0) {
|
|
3760
4037
|
const span = this.getSpan(b, offset);
|
|
3761
|
-
if (
|
|
3762
|
-
&& (this.maxSpan < span)) {
|
|
4038
|
+
if (0 <= this.maxSpan && this.maxSpan < span) {
|
|
3763
4039
|
throw new RangeError('text length exceeds maxSpan');
|
|
3764
4040
|
}
|
|
3765
|
-
return uint8ArrayToBuffer(b)
|
|
4041
|
+
return uint8ArrayToBuffer(b)
|
|
4042
|
+
.slice(offset, offset + span)
|
|
4043
|
+
.toString('utf-8');
|
|
3766
4044
|
}
|
|
3767
4045
|
/** @override */
|
|
3768
4046
|
encode(src, b, offset = 0) {
|
|
@@ -3774,11 +4052,10 @@ class UTF8 extends Layout {
|
|
|
3774
4052
|
}
|
|
3775
4053
|
const srcb = buffer_1.Buffer.from(src, 'utf8');
|
|
3776
4054
|
const span = srcb.length;
|
|
3777
|
-
if (
|
|
3778
|
-
&& (this.maxSpan < span)) {
|
|
4055
|
+
if (0 <= this.maxSpan && this.maxSpan < span) {
|
|
3779
4056
|
throw new RangeError('text length exceeds maxSpan');
|
|
3780
4057
|
}
|
|
3781
|
-
if (
|
|
4058
|
+
if (offset + span > b.length) {
|
|
3782
4059
|
throw new RangeError('encoding overruns Buffer');
|
|
3783
4060
|
}
|
|
3784
4061
|
srcb.copy(uint8ArrayToBuffer(b), offset);
|
|
@@ -3831,115 +4108,121 @@ class Constant extends Layout {
|
|
|
3831
4108
|
}
|
|
3832
4109
|
Layout$1.Constant = Constant;
|
|
3833
4110
|
/** Factory for {@link GreedyCount}. */
|
|
3834
|
-
Layout$1.greedy = (
|
|
4111
|
+
Layout$1.greedy = (elementSpan, property) =>
|
|
4112
|
+
new GreedyCount(elementSpan, property);
|
|
3835
4113
|
/** Factory for {@link OffsetLayout}. */
|
|
3836
|
-
Layout$1.offset = (
|
|
4114
|
+
Layout$1.offset = (layout, offset, property) =>
|
|
4115
|
+
new OffsetLayout(layout, offset, property);
|
|
3837
4116
|
/** Factory for {@link UInt|unsigned int layouts} spanning one
|
|
3838
4117
|
* byte. */
|
|
3839
|
-
var u8 = Layout$1.u8 =
|
|
4118
|
+
var u8 = (Layout$1.u8 = property => new UInt(1, property));
|
|
3840
4119
|
/** Factory for {@link UInt|little-endian unsigned int layouts}
|
|
3841
4120
|
* spanning two bytes. */
|
|
3842
|
-
Layout$1.u16 =
|
|
4121
|
+
Layout$1.u16 = property => new UInt(2, property);
|
|
3843
4122
|
/** Factory for {@link UInt|little-endian unsigned int layouts}
|
|
3844
4123
|
* spanning three bytes. */
|
|
3845
|
-
Layout$1.u24 =
|
|
4124
|
+
Layout$1.u24 = property => new UInt(3, property);
|
|
3846
4125
|
/** Factory for {@link UInt|little-endian unsigned int layouts}
|
|
3847
4126
|
* spanning four bytes. */
|
|
3848
|
-
var u32 = Layout$1.u32 =
|
|
4127
|
+
var u32 = (Layout$1.u32 = property => new UInt(4, property));
|
|
3849
4128
|
/** Factory for {@link UInt|little-endian unsigned int layouts}
|
|
3850
4129
|
* spanning five bytes. */
|
|
3851
|
-
Layout$1.u40 =
|
|
4130
|
+
Layout$1.u40 = property => new UInt(5, property);
|
|
3852
4131
|
/** Factory for {@link UInt|little-endian unsigned int layouts}
|
|
3853
4132
|
* spanning six bytes. */
|
|
3854
|
-
Layout$1.u48 =
|
|
4133
|
+
Layout$1.u48 = property => new UInt(6, property);
|
|
3855
4134
|
/** Factory for {@link NearUInt64|little-endian unsigned int
|
|
3856
4135
|
* layouts} interpreted as Numbers. */
|
|
3857
|
-
Layout$1.nu64 =
|
|
4136
|
+
Layout$1.nu64 = property => new NearUInt64(property);
|
|
3858
4137
|
/** Factory for {@link UInt|big-endian unsigned int layouts}
|
|
3859
4138
|
* spanning two bytes. */
|
|
3860
|
-
Layout$1.u16be =
|
|
4139
|
+
Layout$1.u16be = property => new UIntBE(2, property);
|
|
3861
4140
|
/** Factory for {@link UInt|big-endian unsigned int layouts}
|
|
3862
4141
|
* spanning three bytes. */
|
|
3863
|
-
Layout$1.u24be =
|
|
4142
|
+
Layout$1.u24be = property => new UIntBE(3, property);
|
|
3864
4143
|
/** Factory for {@link UInt|big-endian unsigned int layouts}
|
|
3865
4144
|
* spanning four bytes. */
|
|
3866
|
-
Layout$1.u32be =
|
|
4145
|
+
Layout$1.u32be = property => new UIntBE(4, property);
|
|
3867
4146
|
/** Factory for {@link UInt|big-endian unsigned int layouts}
|
|
3868
4147
|
* spanning five bytes. */
|
|
3869
|
-
Layout$1.u40be =
|
|
4148
|
+
Layout$1.u40be = property => new UIntBE(5, property);
|
|
3870
4149
|
/** Factory for {@link UInt|big-endian unsigned int layouts}
|
|
3871
4150
|
* spanning six bytes. */
|
|
3872
|
-
Layout$1.u48be =
|
|
4151
|
+
Layout$1.u48be = property => new UIntBE(6, property);
|
|
3873
4152
|
/** Factory for {@link NearUInt64BE|big-endian unsigned int
|
|
3874
4153
|
* layouts} interpreted as Numbers. */
|
|
3875
|
-
Layout$1.nu64be =
|
|
4154
|
+
Layout$1.nu64be = property => new NearUInt64BE(property);
|
|
3876
4155
|
/** Factory for {@link Int|signed int layouts} spanning one
|
|
3877
4156
|
* byte. */
|
|
3878
|
-
Layout$1.s8 =
|
|
4157
|
+
Layout$1.s8 = property => new Int(1, property);
|
|
3879
4158
|
/** Factory for {@link Int|little-endian signed int layouts}
|
|
3880
4159
|
* spanning two bytes. */
|
|
3881
|
-
Layout$1.s16 =
|
|
4160
|
+
Layout$1.s16 = property => new Int(2, property);
|
|
3882
4161
|
/** Factory for {@link Int|little-endian signed int layouts}
|
|
3883
4162
|
* spanning three bytes. */
|
|
3884
|
-
Layout$1.s24 =
|
|
4163
|
+
Layout$1.s24 = property => new Int(3, property);
|
|
3885
4164
|
/** Factory for {@link Int|little-endian signed int layouts}
|
|
3886
4165
|
* spanning four bytes. */
|
|
3887
|
-
Layout$1.s32 =
|
|
4166
|
+
Layout$1.s32 = property => new Int(4, property);
|
|
3888
4167
|
/** Factory for {@link Int|little-endian signed int layouts}
|
|
3889
4168
|
* spanning five bytes. */
|
|
3890
|
-
Layout$1.s40 =
|
|
4169
|
+
Layout$1.s40 = property => new Int(5, property);
|
|
3891
4170
|
/** Factory for {@link Int|little-endian signed int layouts}
|
|
3892
4171
|
* spanning six bytes. */
|
|
3893
|
-
Layout$1.s48 =
|
|
4172
|
+
Layout$1.s48 = property => new Int(6, property);
|
|
3894
4173
|
/** Factory for {@link NearInt64|little-endian signed int layouts}
|
|
3895
4174
|
* interpreted as Numbers. */
|
|
3896
|
-
Layout$1.ns64 =
|
|
4175
|
+
Layout$1.ns64 = property => new NearInt64(property);
|
|
3897
4176
|
/** Factory for {@link Int|big-endian signed int layouts}
|
|
3898
4177
|
* spanning two bytes. */
|
|
3899
|
-
Layout$1.s16be =
|
|
4178
|
+
Layout$1.s16be = property => new IntBE(2, property);
|
|
3900
4179
|
/** Factory for {@link Int|big-endian signed int layouts}
|
|
3901
4180
|
* spanning three bytes. */
|
|
3902
|
-
Layout$1.s24be =
|
|
4181
|
+
Layout$1.s24be = property => new IntBE(3, property);
|
|
3903
4182
|
/** Factory for {@link Int|big-endian signed int layouts}
|
|
3904
4183
|
* spanning four bytes. */
|
|
3905
|
-
Layout$1.s32be =
|
|
4184
|
+
Layout$1.s32be = property => new IntBE(4, property);
|
|
3906
4185
|
/** Factory for {@link Int|big-endian signed int layouts}
|
|
3907
4186
|
* spanning five bytes. */
|
|
3908
|
-
Layout$1.s40be =
|
|
4187
|
+
Layout$1.s40be = property => new IntBE(5, property);
|
|
3909
4188
|
/** Factory for {@link Int|big-endian signed int layouts}
|
|
3910
4189
|
* spanning six bytes. */
|
|
3911
|
-
Layout$1.s48be =
|
|
4190
|
+
Layout$1.s48be = property => new IntBE(6, property);
|
|
3912
4191
|
/** Factory for {@link NearInt64BE|big-endian signed int layouts}
|
|
3913
4192
|
* interpreted as Numbers. */
|
|
3914
|
-
Layout$1.ns64be =
|
|
4193
|
+
Layout$1.ns64be = property => new NearInt64BE(property);
|
|
3915
4194
|
/** Factory for {@link Float|little-endian 32-bit floating point} values. */
|
|
3916
|
-
Layout$1.f32 =
|
|
4195
|
+
Layout$1.f32 = property => new Float(property);
|
|
3917
4196
|
/** Factory for {@link FloatBE|big-endian 32-bit floating point} values. */
|
|
3918
|
-
Layout$1.f32be =
|
|
4197
|
+
Layout$1.f32be = property => new FloatBE(property);
|
|
3919
4198
|
/** Factory for {@link Double|little-endian 64-bit floating point} values. */
|
|
3920
|
-
Layout$1.f64 =
|
|
4199
|
+
Layout$1.f64 = property => new Double(property);
|
|
3921
4200
|
/** Factory for {@link DoubleBE|big-endian 64-bit floating point} values. */
|
|
3922
|
-
Layout$1.f64be =
|
|
4201
|
+
Layout$1.f64be = property => new DoubleBE(property);
|
|
3923
4202
|
/** Factory for {@link Structure} values. */
|
|
3924
|
-
var struct = Layout$1.struct = (
|
|
4203
|
+
var struct = (Layout$1.struct = (fields, property, decodePrefixes) =>
|
|
4204
|
+
new Structure(fields, property, decodePrefixes));
|
|
3925
4205
|
/** Factory for {@link BitStructure} values. */
|
|
3926
|
-
Layout$1.bits = (
|
|
4206
|
+
Layout$1.bits = (word, msb, property) => new BitStructure(word, msb, property);
|
|
3927
4207
|
/** Factory for {@link Sequence} values. */
|
|
3928
|
-
Layout$1.seq = (
|
|
4208
|
+
Layout$1.seq = (elementLayout, count, property) =>
|
|
4209
|
+
new Sequence(elementLayout, count, property);
|
|
3929
4210
|
/** Factory for {@link Union} values. */
|
|
3930
|
-
Layout$1.union = (
|
|
4211
|
+
Layout$1.union = (discr, defaultLayout, property) =>
|
|
4212
|
+
new Union(discr, defaultLayout, property);
|
|
3931
4213
|
/** Factory for {@link UnionLayoutDiscriminator} values. */
|
|
3932
|
-
Layout$1.unionLayoutDiscriminator = (
|
|
4214
|
+
Layout$1.unionLayoutDiscriminator = (layout, property) =>
|
|
4215
|
+
new UnionLayoutDiscriminator(layout, property);
|
|
3933
4216
|
/** Factory for {@link Blob} values. */
|
|
3934
|
-
var blob = Layout$1.blob = (
|
|
4217
|
+
var blob = (Layout$1.blob = (length, property) => new Blob(length, property));
|
|
3935
4218
|
/** Factory for {@link CString} values. */
|
|
3936
|
-
Layout$1.cstr =
|
|
4219
|
+
Layout$1.cstr = property => new CString(property);
|
|
3937
4220
|
/** Factory for {@link UTF8} values. */
|
|
3938
|
-
Layout$1.utf8 = (
|
|
4221
|
+
Layout$1.utf8 = (maxSpan, property) => new UTF8(maxSpan, property);
|
|
3939
4222
|
/** Factory for {@link Constant} values. */
|
|
3940
|
-
Layout$1.constant = (
|
|
4223
|
+
Layout$1.constant = (value, property) => new Constant(value, property);
|
|
3941
4224
|
|
|
3942
|
-
const encodeDecode =
|
|
4225
|
+
const encodeDecode = layout => {
|
|
3943
4226
|
const decode = layout.decode.bind(layout);
|
|
3944
4227
|
const encode = layout.encode.bind(layout);
|
|
3945
4228
|
return { decode, encode };
|
|
@@ -3947,7 +4230,7 @@ const encodeDecode = (layout) => {
|
|
|
3947
4230
|
|
|
3948
4231
|
var browser = {};
|
|
3949
4232
|
|
|
3950
|
-
Object.defineProperty(browser,
|
|
4233
|
+
Object.defineProperty(browser, '__esModule', { value: true });
|
|
3951
4234
|
/**
|
|
3952
4235
|
* Convert a little-endian buffer into a BigInt.
|
|
3953
4236
|
* @param buf The little-endian buffer to convert
|
|
@@ -3964,7 +4247,7 @@ function toBigIntLE(buf) {
|
|
|
3964
4247
|
return BigInt(`0x${hex}`);
|
|
3965
4248
|
}
|
|
3966
4249
|
}
|
|
3967
|
-
var toBigIntLE_1 = browser.toBigIntLE = toBigIntLE;
|
|
4250
|
+
var toBigIntLE_1 = (browser.toBigIntLE = toBigIntLE);
|
|
3968
4251
|
/**
|
|
3969
4252
|
* Convert a big-endian buffer into a BigInt
|
|
3970
4253
|
* @param buf The big-endian buffer to convert.
|
|
@@ -3989,12 +4272,15 @@ browser.toBigIntBE = toBigIntBE;
|
|
|
3989
4272
|
function toBufferLE(num, width) {
|
|
3990
4273
|
{
|
|
3991
4274
|
const hex = num.toString(16);
|
|
3992
|
-
const buffer = Buffer.from(
|
|
4275
|
+
const buffer = Buffer.from(
|
|
4276
|
+
hex.padStart(width * 2, '0').slice(0, width * 2),
|
|
4277
|
+
'hex',
|
|
4278
|
+
);
|
|
3993
4279
|
buffer.reverse();
|
|
3994
4280
|
return buffer;
|
|
3995
4281
|
}
|
|
3996
4282
|
}
|
|
3997
|
-
var toBufferLE_1 = browser.toBufferLE = toBufferLE;
|
|
4283
|
+
var toBufferLE_1 = (browser.toBufferLE = toBufferLE);
|
|
3998
4284
|
/**
|
|
3999
4285
|
* Convert a BigInt to a big-endian buffer.
|
|
4000
4286
|
* @param num The BigInt to convert.
|
|
@@ -4004,12 +4290,15 @@ var toBufferLE_1 = browser.toBufferLE = toBufferLE;
|
|
|
4004
4290
|
function toBufferBE(num, width) {
|
|
4005
4291
|
{
|
|
4006
4292
|
const hex = num.toString(16);
|
|
4007
|
-
return Buffer.from(
|
|
4293
|
+
return Buffer.from(
|
|
4294
|
+
hex.padStart(width * 2, '0').slice(0, width * 2),
|
|
4295
|
+
'hex',
|
|
4296
|
+
);
|
|
4008
4297
|
}
|
|
4009
4298
|
}
|
|
4010
4299
|
browser.toBufferBE = toBufferBE;
|
|
4011
4300
|
|
|
4012
|
-
const bigInt =
|
|
4301
|
+
const bigInt = length => property => {
|
|
4013
4302
|
const layout = blob(length, property);
|
|
4014
4303
|
const { encode, decode } = encodeDecode(layout);
|
|
4015
4304
|
const bigIntLayout = layout;
|
|
@@ -4025,7 +4314,7 @@ const bigInt = (length) => (property) => {
|
|
|
4025
4314
|
};
|
|
4026
4315
|
const u64 = bigInt(8);
|
|
4027
4316
|
|
|
4028
|
-
const bool =
|
|
4317
|
+
const bool = property => {
|
|
4029
4318
|
const layout = u8(property);
|
|
4030
4319
|
const { encode, decode } = encodeDecode(layout);
|
|
4031
4320
|
const boolLayout = layout;
|
|
@@ -4040,7 +4329,7 @@ const bool = (property) => {
|
|
|
4040
4329
|
return boolLayout;
|
|
4041
4330
|
};
|
|
4042
4331
|
|
|
4043
|
-
const publicKey =
|
|
4332
|
+
const publicKey = property => {
|
|
4044
4333
|
const layout = blob(32, property);
|
|
4045
4334
|
const { encode, decode } = encodeDecode(layout);
|
|
4046
4335
|
const publicKeyLayout = layout;
|
|
@@ -4114,73 +4403,112 @@ class TokenOwnerOffCurveError extends TokenError {
|
|
|
4114
4403
|
/** Instructions defined by the program */
|
|
4115
4404
|
var TokenInstruction;
|
|
4116
4405
|
(function (TokenInstruction) {
|
|
4117
|
-
TokenInstruction[TokenInstruction[
|
|
4118
|
-
|
|
4119
|
-
TokenInstruction[TokenInstruction[
|
|
4120
|
-
|
|
4121
|
-
TokenInstruction[TokenInstruction[
|
|
4122
|
-
|
|
4123
|
-
TokenInstruction[TokenInstruction[
|
|
4124
|
-
TokenInstruction[TokenInstruction[
|
|
4125
|
-
TokenInstruction[TokenInstruction[
|
|
4126
|
-
TokenInstruction[TokenInstruction[
|
|
4127
|
-
TokenInstruction[TokenInstruction[
|
|
4128
|
-
TokenInstruction[TokenInstruction[
|
|
4129
|
-
TokenInstruction[TokenInstruction[
|
|
4130
|
-
TokenInstruction[TokenInstruction[
|
|
4131
|
-
|
|
4132
|
-
TokenInstruction[TokenInstruction[
|
|
4133
|
-
TokenInstruction[TokenInstruction[
|
|
4134
|
-
|
|
4135
|
-
TokenInstruction[TokenInstruction[
|
|
4136
|
-
|
|
4137
|
-
TokenInstruction[TokenInstruction[
|
|
4138
|
-
|
|
4139
|
-
TokenInstruction[TokenInstruction[
|
|
4140
|
-
TokenInstruction[TokenInstruction[
|
|
4141
|
-
|
|
4142
|
-
TokenInstruction[TokenInstruction[
|
|
4143
|
-
TokenInstruction[TokenInstruction[
|
|
4144
|
-
|
|
4145
|
-
TokenInstruction[TokenInstruction[
|
|
4146
|
-
|
|
4147
|
-
TokenInstruction[TokenInstruction[
|
|
4148
|
-
|
|
4149
|
-
TokenInstruction[TokenInstruction[
|
|
4150
|
-
|
|
4151
|
-
TokenInstruction[TokenInstruction[
|
|
4152
|
-
|
|
4153
|
-
TokenInstruction[TokenInstruction[
|
|
4406
|
+
TokenInstruction[(TokenInstruction['InitializeMint'] = 0)] =
|
|
4407
|
+
'InitializeMint';
|
|
4408
|
+
TokenInstruction[(TokenInstruction['InitializeAccount'] = 1)] =
|
|
4409
|
+
'InitializeAccount';
|
|
4410
|
+
TokenInstruction[(TokenInstruction['InitializeMultisig'] = 2)] =
|
|
4411
|
+
'InitializeMultisig';
|
|
4412
|
+
TokenInstruction[(TokenInstruction['Transfer'] = 3)] = 'Transfer';
|
|
4413
|
+
TokenInstruction[(TokenInstruction['Approve'] = 4)] = 'Approve';
|
|
4414
|
+
TokenInstruction[(TokenInstruction['Revoke'] = 5)] = 'Revoke';
|
|
4415
|
+
TokenInstruction[(TokenInstruction['SetAuthority'] = 6)] = 'SetAuthority';
|
|
4416
|
+
TokenInstruction[(TokenInstruction['MintTo'] = 7)] = 'MintTo';
|
|
4417
|
+
TokenInstruction[(TokenInstruction['Burn'] = 8)] = 'Burn';
|
|
4418
|
+
TokenInstruction[(TokenInstruction['CloseAccount'] = 9)] = 'CloseAccount';
|
|
4419
|
+
TokenInstruction[(TokenInstruction['FreezeAccount'] = 10)] =
|
|
4420
|
+
'FreezeAccount';
|
|
4421
|
+
TokenInstruction[(TokenInstruction['ThawAccount'] = 11)] = 'ThawAccount';
|
|
4422
|
+
TokenInstruction[(TokenInstruction['TransferChecked'] = 12)] =
|
|
4423
|
+
'TransferChecked';
|
|
4424
|
+
TokenInstruction[(TokenInstruction['ApproveChecked'] = 13)] =
|
|
4425
|
+
'ApproveChecked';
|
|
4426
|
+
TokenInstruction[(TokenInstruction['MintToChecked'] = 14)] =
|
|
4427
|
+
'MintToChecked';
|
|
4428
|
+
TokenInstruction[(TokenInstruction['BurnChecked'] = 15)] = 'BurnChecked';
|
|
4429
|
+
TokenInstruction[(TokenInstruction['InitializeAccount2'] = 16)] =
|
|
4430
|
+
'InitializeAccount2';
|
|
4431
|
+
TokenInstruction[(TokenInstruction['SyncNative'] = 17)] = 'SyncNative';
|
|
4432
|
+
TokenInstruction[(TokenInstruction['InitializeAccount3'] = 18)] =
|
|
4433
|
+
'InitializeAccount3';
|
|
4434
|
+
TokenInstruction[(TokenInstruction['InitializeMultisig2'] = 19)] =
|
|
4435
|
+
'InitializeMultisig2';
|
|
4436
|
+
TokenInstruction[(TokenInstruction['InitializeMint2'] = 20)] =
|
|
4437
|
+
'InitializeMint2';
|
|
4438
|
+
TokenInstruction[(TokenInstruction['GetAccountDataSize'] = 21)] =
|
|
4439
|
+
'GetAccountDataSize';
|
|
4440
|
+
TokenInstruction[(TokenInstruction['InitializeImmutableOwner'] = 22)] =
|
|
4441
|
+
'InitializeImmutableOwner';
|
|
4442
|
+
TokenInstruction[(TokenInstruction['AmountToUiAmount'] = 23)] =
|
|
4443
|
+
'AmountToUiAmount';
|
|
4444
|
+
TokenInstruction[(TokenInstruction['UiAmountToAmount'] = 24)] =
|
|
4445
|
+
'UiAmountToAmount';
|
|
4446
|
+
TokenInstruction[(TokenInstruction['InitializeMintCloseAuthority'] = 25)] =
|
|
4447
|
+
'InitializeMintCloseAuthority';
|
|
4448
|
+
TokenInstruction[(TokenInstruction['TransferFeeExtension'] = 26)] =
|
|
4449
|
+
'TransferFeeExtension';
|
|
4450
|
+
TokenInstruction[(TokenInstruction['ConfidentialTransferExtension'] = 27)] =
|
|
4451
|
+
'ConfidentialTransferExtension';
|
|
4452
|
+
TokenInstruction[(TokenInstruction['DefaultAccountStateExtension'] = 28)] =
|
|
4453
|
+
'DefaultAccountStateExtension';
|
|
4454
|
+
TokenInstruction[(TokenInstruction['Reallocate'] = 29)] = 'Reallocate';
|
|
4455
|
+
TokenInstruction[(TokenInstruction['MemoTransferExtension'] = 30)] =
|
|
4456
|
+
'MemoTransferExtension';
|
|
4457
|
+
TokenInstruction[(TokenInstruction['CreateNativeMint'] = 31)] =
|
|
4458
|
+
'CreateNativeMint';
|
|
4459
|
+
TokenInstruction[(TokenInstruction['InitializeNonTransferableMint'] = 32)] =
|
|
4460
|
+
'InitializeNonTransferableMint';
|
|
4461
|
+
TokenInstruction[(TokenInstruction['InterestBearingMintExtension'] = 33)] =
|
|
4462
|
+
'InterestBearingMintExtension';
|
|
4463
|
+
TokenInstruction[(TokenInstruction['CpiGuardExtension'] = 34)] =
|
|
4464
|
+
'CpiGuardExtension';
|
|
4465
|
+
TokenInstruction[(TokenInstruction['InitializePermanentDelegate'] = 35)] =
|
|
4466
|
+
'InitializePermanentDelegate';
|
|
4467
|
+
TokenInstruction[(TokenInstruction['TransferHookExtension'] = 36)] =
|
|
4468
|
+
'TransferHookExtension';
|
|
4154
4469
|
// ConfidentialTransferFeeExtension = 37,
|
|
4155
4470
|
// WithdrawalExcessLamports = 38,
|
|
4156
|
-
TokenInstruction[TokenInstruction[
|
|
4157
|
-
|
|
4158
|
-
TokenInstruction[TokenInstruction[
|
|
4471
|
+
TokenInstruction[(TokenInstruction['MetadataPointerExtension'] = 39)] =
|
|
4472
|
+
'MetadataPointerExtension';
|
|
4473
|
+
TokenInstruction[(TokenInstruction['GroupPointerExtension'] = 40)] =
|
|
4474
|
+
'GroupPointerExtension';
|
|
4475
|
+
TokenInstruction[(TokenInstruction['GroupMemberPointerExtension'] = 41)] =
|
|
4476
|
+
'GroupMemberPointerExtension';
|
|
4159
4477
|
})(TokenInstruction || (TokenInstruction = {}));
|
|
4160
4478
|
|
|
4161
4479
|
/** @internal */
|
|
4162
4480
|
function addSigners(keys, ownerOrAuthority, multiSigners) {
|
|
4163
4481
|
if (multiSigners.length) {
|
|
4164
|
-
keys.push({
|
|
4482
|
+
keys.push({
|
|
4483
|
+
pubkey: ownerOrAuthority,
|
|
4484
|
+
isSigner: false,
|
|
4485
|
+
isWritable: false,
|
|
4486
|
+
});
|
|
4165
4487
|
for (const signer of multiSigners) {
|
|
4166
4488
|
keys.push({
|
|
4167
|
-
pubkey:
|
|
4489
|
+
pubkey:
|
|
4490
|
+
signer instanceof web3_js.PublicKey
|
|
4491
|
+
? signer
|
|
4492
|
+
: signer.publicKey,
|
|
4168
4493
|
isSigner: true,
|
|
4169
4494
|
isWritable: false,
|
|
4170
4495
|
});
|
|
4171
4496
|
}
|
|
4172
|
-
}
|
|
4173
|
-
|
|
4174
|
-
|
|
4497
|
+
} else {
|
|
4498
|
+
keys.push({
|
|
4499
|
+
pubkey: ownerOrAuthority,
|
|
4500
|
+
isSigner: true,
|
|
4501
|
+
isWritable: false,
|
|
4502
|
+
});
|
|
4175
4503
|
}
|
|
4176
4504
|
return keys;
|
|
4177
4505
|
}
|
|
4178
4506
|
|
|
4179
4507
|
var AccountType;
|
|
4180
4508
|
(function (AccountType) {
|
|
4181
|
-
AccountType[AccountType[
|
|
4182
|
-
AccountType[AccountType[
|
|
4183
|
-
AccountType[AccountType[
|
|
4509
|
+
AccountType[(AccountType['Uninitialized'] = 0)] = 'Uninitialized';
|
|
4510
|
+
AccountType[(AccountType['Mint'] = 1)] = 'Mint';
|
|
4511
|
+
AccountType[(AccountType['Account'] = 2)] = 'Account';
|
|
4184
4512
|
})(AccountType || (AccountType = {}));
|
|
4185
4513
|
const ACCOUNT_TYPE_SIZE = 1;
|
|
4186
4514
|
|
|
@@ -4207,9 +4535,9 @@ const MULTISIG_SIZE = MultisigLayout.span;
|
|
|
4207
4535
|
/** Token account state as stored by the program */
|
|
4208
4536
|
var AccountState;
|
|
4209
4537
|
(function (AccountState) {
|
|
4210
|
-
AccountState[AccountState[
|
|
4211
|
-
AccountState[AccountState[
|
|
4212
|
-
AccountState[AccountState[
|
|
4538
|
+
AccountState[(AccountState['Uninitialized'] = 0)] = 'Uninitialized';
|
|
4539
|
+
AccountState[(AccountState['Initialized'] = 1)] = 'Initialized';
|
|
4540
|
+
AccountState[(AccountState['Frozen'] = 2)] = 'Frozen';
|
|
4213
4541
|
})(AccountState || (AccountState = {}));
|
|
4214
4542
|
/** Buffer layout for de/serializing a token account */
|
|
4215
4543
|
const AccountLayout = struct([
|
|
@@ -4237,7 +4565,12 @@ const ACCOUNT_SIZE = AccountLayout.span;
|
|
|
4237
4565
|
*
|
|
4238
4566
|
* @return Token account information
|
|
4239
4567
|
*/
|
|
4240
|
-
async function getAccount(
|
|
4568
|
+
async function getAccount(
|
|
4569
|
+
connection,
|
|
4570
|
+
address,
|
|
4571
|
+
commitment,
|
|
4572
|
+
programId = TOKEN_PROGRAM_ID,
|
|
4573
|
+
) {
|
|
4241
4574
|
const info = await connection.getAccountInfo(address, commitment);
|
|
4242
4575
|
return unpackAccount(address, info, programId);
|
|
4243
4576
|
}
|
|
@@ -4251,8 +4584,7 @@ async function getAccount(connection, address, commitment, programId = TOKEN_PRO
|
|
|
4251
4584
|
* @return Unpacked token account
|
|
4252
4585
|
*/
|
|
4253
4586
|
function unpackAccount(address, info, programId = TOKEN_PROGRAM_ID) {
|
|
4254
|
-
if (!info)
|
|
4255
|
-
throw new TokenAccountNotFoundError();
|
|
4587
|
+
if (!info) throw new TokenAccountNotFoundError();
|
|
4256
4588
|
if (!info.owner.equals(programId))
|
|
4257
4589
|
throw new TokenInvalidAccountOwnerError();
|
|
4258
4590
|
if (info.data.length < ACCOUNT_SIZE)
|
|
@@ -4276,8 +4608,12 @@ function unpackAccount(address, info, programId = TOKEN_PROGRAM_ID) {
|
|
|
4276
4608
|
isInitialized: rawAccount.state !== AccountState.Uninitialized,
|
|
4277
4609
|
isFrozen: rawAccount.state === AccountState.Frozen,
|
|
4278
4610
|
isNative: !!rawAccount.isNativeOption,
|
|
4279
|
-
rentExemptReserve: rawAccount.isNativeOption
|
|
4280
|
-
|
|
4611
|
+
rentExemptReserve: rawAccount.isNativeOption
|
|
4612
|
+
? rawAccount.isNative
|
|
4613
|
+
: null,
|
|
4614
|
+
closeAuthority: rawAccount.closeAuthorityOption
|
|
4615
|
+
? rawAccount.closeAuthority
|
|
4616
|
+
: null,
|
|
4281
4617
|
tlvData,
|
|
4282
4618
|
};
|
|
4283
4619
|
}
|
|
@@ -4305,10 +4641,19 @@ const MINT_SIZE = MintLayout.span;
|
|
|
4305
4641
|
*
|
|
4306
4642
|
* @return Address of the associated token account
|
|
4307
4643
|
*/
|
|
4308
|
-
function getAssociatedTokenAddressSync(
|
|
4644
|
+
function getAssociatedTokenAddressSync(
|
|
4645
|
+
mint,
|
|
4646
|
+
owner,
|
|
4647
|
+
allowOwnerOffCurve = false,
|
|
4648
|
+
programId = TOKEN_PROGRAM_ID,
|
|
4649
|
+
associatedTokenProgramId = ASSOCIATED_TOKEN_PROGRAM_ID,
|
|
4650
|
+
) {
|
|
4309
4651
|
if (!allowOwnerOffCurve && !web3_js.PublicKey.isOnCurve(owner.toBuffer()))
|
|
4310
4652
|
throw new TokenOwnerOffCurveError();
|
|
4311
|
-
const [address] = web3_js.PublicKey.findProgramAddressSync(
|
|
4653
|
+
const [address] = web3_js.PublicKey.findProgramAddressSync(
|
|
4654
|
+
[owner.toBuffer(), programId.toBuffer(), mint.toBuffer()],
|
|
4655
|
+
associatedTokenProgramId,
|
|
4656
|
+
);
|
|
4312
4657
|
return address;
|
|
4313
4658
|
}
|
|
4314
4659
|
|
|
@@ -4328,8 +4673,7 @@ class COptionPublicKeyLayout extends Layout_2 {
|
|
|
4328
4673
|
if (src === null) {
|
|
4329
4674
|
buffer[offset] = 0;
|
|
4330
4675
|
return 1;
|
|
4331
|
-
}
|
|
4332
|
-
else {
|
|
4676
|
+
} else {
|
|
4333
4677
|
buffer[offset] = 1;
|
|
4334
4678
|
this.publicKeyLayout.encode(src, buffer, offset + 1);
|
|
4335
4679
|
return 33;
|
|
@@ -4356,16 +4700,43 @@ class COptionPublicKeyLayout extends Layout_2 {
|
|
|
4356
4700
|
*
|
|
4357
4701
|
* @return Instruction to add to a transaction
|
|
4358
4702
|
*/
|
|
4359
|
-
function createAssociatedTokenAccountInstruction(
|
|
4360
|
-
|
|
4703
|
+
function createAssociatedTokenAccountInstruction(
|
|
4704
|
+
payer,
|
|
4705
|
+
associatedToken,
|
|
4706
|
+
owner,
|
|
4707
|
+
mint,
|
|
4708
|
+
programId = TOKEN_PROGRAM_ID,
|
|
4709
|
+
associatedTokenProgramId = ASSOCIATED_TOKEN_PROGRAM_ID,
|
|
4710
|
+
) {
|
|
4711
|
+
return buildAssociatedTokenAccountInstruction(
|
|
4712
|
+
payer,
|
|
4713
|
+
associatedToken,
|
|
4714
|
+
owner,
|
|
4715
|
+
mint,
|
|
4716
|
+
Buffer.alloc(0),
|
|
4717
|
+
programId,
|
|
4718
|
+
associatedTokenProgramId,
|
|
4719
|
+
);
|
|
4361
4720
|
}
|
|
4362
|
-
function buildAssociatedTokenAccountInstruction(
|
|
4721
|
+
function buildAssociatedTokenAccountInstruction(
|
|
4722
|
+
payer,
|
|
4723
|
+
associatedToken,
|
|
4724
|
+
owner,
|
|
4725
|
+
mint,
|
|
4726
|
+
instructionData,
|
|
4727
|
+
programId = TOKEN_PROGRAM_ID,
|
|
4728
|
+
associatedTokenProgramId = ASSOCIATED_TOKEN_PROGRAM_ID,
|
|
4729
|
+
) {
|
|
4363
4730
|
const keys = [
|
|
4364
4731
|
{ pubkey: payer, isSigner: true, isWritable: true },
|
|
4365
4732
|
{ pubkey: associatedToken, isSigner: false, isWritable: true },
|
|
4366
4733
|
{ pubkey: owner, isSigner: false, isWritable: false },
|
|
4367
4734
|
{ pubkey: mint, isSigner: false, isWritable: false },
|
|
4368
|
-
{
|
|
4735
|
+
{
|
|
4736
|
+
pubkey: web3_js.SystemProgram.programId,
|
|
4737
|
+
isSigner: false,
|
|
4738
|
+
isWritable: false,
|
|
4739
|
+
},
|
|
4369
4740
|
{ pubkey: programId, isSigner: false, isWritable: false },
|
|
4370
4741
|
];
|
|
4371
4742
|
return new web3_js.TransactionInstruction({
|
|
@@ -4393,15 +4764,24 @@ const initializeMint2InstructionData = struct([
|
|
|
4393
4764
|
*
|
|
4394
4765
|
* @return Instruction to add to a transaction
|
|
4395
4766
|
*/
|
|
4396
|
-
function createInitializeMint2Instruction(
|
|
4767
|
+
function createInitializeMint2Instruction(
|
|
4768
|
+
mint,
|
|
4769
|
+
decimals,
|
|
4770
|
+
mintAuthority,
|
|
4771
|
+
freezeAuthority,
|
|
4772
|
+
programId = TOKEN_PROGRAM_ID,
|
|
4773
|
+
) {
|
|
4397
4774
|
const keys = [{ pubkey: mint, isSigner: false, isWritable: true }];
|
|
4398
4775
|
const data = Buffer.alloc(initializeMint2InstructionData.span);
|
|
4399
|
-
initializeMint2InstructionData.encode(
|
|
4400
|
-
|
|
4401
|
-
|
|
4402
|
-
|
|
4403
|
-
|
|
4404
|
-
|
|
4776
|
+
initializeMint2InstructionData.encode(
|
|
4777
|
+
{
|
|
4778
|
+
instruction: TokenInstruction.InitializeMint2,
|
|
4779
|
+
decimals,
|
|
4780
|
+
mintAuthority,
|
|
4781
|
+
freezeAuthority,
|
|
4782
|
+
},
|
|
4783
|
+
data,
|
|
4784
|
+
);
|
|
4405
4785
|
return new web3_js.TransactionInstruction({ keys, programId, data });
|
|
4406
4786
|
}
|
|
4407
4787
|
|
|
@@ -4420,39 +4800,77 @@ function createInitializeMint2Instruction(mint, decimals, mintAuthority, freezeA
|
|
|
4420
4800
|
*
|
|
4421
4801
|
* @return Address of the new associated token account
|
|
4422
4802
|
*/
|
|
4423
|
-
async function getOrCreateAssociatedTokenAccount(
|
|
4424
|
-
|
|
4803
|
+
async function getOrCreateAssociatedTokenAccount(
|
|
4804
|
+
connection,
|
|
4805
|
+
payer,
|
|
4806
|
+
mint,
|
|
4807
|
+
owner,
|
|
4808
|
+
allowOwnerOffCurve = false,
|
|
4809
|
+
commitment,
|
|
4810
|
+
confirmOptions,
|
|
4811
|
+
programId = TOKEN_PROGRAM_ID,
|
|
4812
|
+
associatedTokenProgramId = ASSOCIATED_TOKEN_PROGRAM_ID,
|
|
4813
|
+
) {
|
|
4814
|
+
const associatedToken = getAssociatedTokenAddressSync(
|
|
4815
|
+
mint,
|
|
4816
|
+
owner,
|
|
4817
|
+
allowOwnerOffCurve,
|
|
4818
|
+
programId,
|
|
4819
|
+
associatedTokenProgramId,
|
|
4820
|
+
);
|
|
4425
4821
|
// This is the optimal logic, considering TX fee, client-side computation, RPC roundtrips and guaranteed idempotent.
|
|
4426
4822
|
// Sadly we can't do this atomically.
|
|
4427
4823
|
let account;
|
|
4428
4824
|
try {
|
|
4429
|
-
account = await getAccount(
|
|
4430
|
-
|
|
4431
|
-
|
|
4825
|
+
account = await getAccount(
|
|
4826
|
+
connection,
|
|
4827
|
+
associatedToken,
|
|
4828
|
+
commitment,
|
|
4829
|
+
programId,
|
|
4830
|
+
);
|
|
4831
|
+
} catch (error) {
|
|
4432
4832
|
// TokenAccountNotFoundError can be possible if the associated address has already received some lamports,
|
|
4433
4833
|
// becoming a system account. Assuming program derived addressing is safe, this is the only case for the
|
|
4434
4834
|
// TokenInvalidAccountOwnerError in this code path.
|
|
4435
|
-
if (
|
|
4835
|
+
if (
|
|
4836
|
+
error instanceof TokenAccountNotFoundError ||
|
|
4837
|
+
error instanceof TokenInvalidAccountOwnerError
|
|
4838
|
+
) {
|
|
4436
4839
|
// As this isn't atomic, it's possible others can create associated accounts meanwhile.
|
|
4437
4840
|
try {
|
|
4438
|
-
const transaction = new web3_js.Transaction().add(
|
|
4439
|
-
|
|
4440
|
-
|
|
4441
|
-
|
|
4841
|
+
const transaction = new web3_js.Transaction().add(
|
|
4842
|
+
createAssociatedTokenAccountInstruction(
|
|
4843
|
+
payer.publicKey,
|
|
4844
|
+
associatedToken,
|
|
4845
|
+
owner,
|
|
4846
|
+
mint,
|
|
4847
|
+
programId,
|
|
4848
|
+
associatedTokenProgramId,
|
|
4849
|
+
),
|
|
4850
|
+
);
|
|
4851
|
+
await web3_js.sendAndConfirmTransaction(
|
|
4852
|
+
connection,
|
|
4853
|
+
transaction,
|
|
4854
|
+
[payer],
|
|
4855
|
+
confirmOptions,
|
|
4856
|
+
);
|
|
4857
|
+
} catch (error) {
|
|
4442
4858
|
// Ignore all errors; for now there is no API-compatible way to selectively ignore the expected
|
|
4443
4859
|
// instruction error if the associated account exists already.
|
|
4444
4860
|
}
|
|
4445
4861
|
// Now this should always succeed
|
|
4446
|
-
account = await getAccount(
|
|
4447
|
-
|
|
4448
|
-
|
|
4862
|
+
account = await getAccount(
|
|
4863
|
+
connection,
|
|
4864
|
+
associatedToken,
|
|
4865
|
+
commitment,
|
|
4866
|
+
programId,
|
|
4867
|
+
);
|
|
4868
|
+
} else {
|
|
4449
4869
|
throw error;
|
|
4450
4870
|
}
|
|
4451
4871
|
}
|
|
4452
|
-
if (!account.mint.equals(mint))
|
|
4453
|
-
|
|
4454
|
-
if (!account.owner.equals(owner))
|
|
4455
|
-
throw new TokenInvalidOwnerError();
|
|
4872
|
+
if (!account.mint.equals(mint)) throw new TokenInvalidMintError();
|
|
4873
|
+
if (!account.owner.equals(owner)) throw new TokenInvalidOwnerError();
|
|
4456
4874
|
return account;
|
|
4457
4875
|
}
|
|
4458
4876
|
|
|
@@ -4470,29 +4888,46 @@ const mintToInstructionData = struct([u8('instruction'), u64('amount')]);
|
|
|
4470
4888
|
*
|
|
4471
4889
|
* @return Instruction to add to a transaction
|
|
4472
4890
|
*/
|
|
4473
|
-
function createMintToInstruction(
|
|
4474
|
-
|
|
4475
|
-
|
|
4476
|
-
|
|
4477
|
-
|
|
4891
|
+
function createMintToInstruction(
|
|
4892
|
+
mint,
|
|
4893
|
+
destination,
|
|
4894
|
+
authority,
|
|
4895
|
+
amount,
|
|
4896
|
+
multiSigners = [],
|
|
4897
|
+
programId = TOKEN_PROGRAM_ID,
|
|
4898
|
+
) {
|
|
4899
|
+
const keys = addSigners(
|
|
4900
|
+
[
|
|
4901
|
+
{ pubkey: mint, isSigner: false, isWritable: true },
|
|
4902
|
+
{ pubkey: destination, isSigner: false, isWritable: true },
|
|
4903
|
+
],
|
|
4904
|
+
authority,
|
|
4905
|
+
multiSigners,
|
|
4906
|
+
);
|
|
4478
4907
|
const data = Buffer.alloc(mintToInstructionData.span);
|
|
4479
|
-
mintToInstructionData.encode(
|
|
4480
|
-
|
|
4481
|
-
|
|
4482
|
-
|
|
4908
|
+
mintToInstructionData.encode(
|
|
4909
|
+
{
|
|
4910
|
+
instruction: TokenInstruction.MintTo,
|
|
4911
|
+
amount: BigInt(amount),
|
|
4912
|
+
},
|
|
4913
|
+
data,
|
|
4914
|
+
);
|
|
4483
4915
|
return new web3_js.TransactionInstruction({ keys, programId, data });
|
|
4484
4916
|
}
|
|
4485
4917
|
|
|
4486
4918
|
/**
|
|
4487
4919
|
* Sum up the token amounts of the compressed token accounts
|
|
4488
4920
|
*/
|
|
4489
|
-
const sumUpTokenAmount =
|
|
4490
|
-
return accounts.reduce(
|
|
4921
|
+
const sumUpTokenAmount = accounts => {
|
|
4922
|
+
return accounts.reduce(
|
|
4923
|
+
(acc, account) => acc.add(account.parsed.amount),
|
|
4924
|
+
stateless_js.bn(0),
|
|
4925
|
+
);
|
|
4491
4926
|
};
|
|
4492
4927
|
/**
|
|
4493
4928
|
* Validate that all the compressed token accounts are owned by the same owner.
|
|
4494
4929
|
*/
|
|
4495
|
-
const validateSameTokenOwner =
|
|
4930
|
+
const validateSameTokenOwner = accounts => {
|
|
4496
4931
|
const owner = accounts[0].parsed.owner;
|
|
4497
4932
|
accounts.forEach(acc => {
|
|
4498
4933
|
if (!acc.parsed.owner.equals(owner)) {
|
|
@@ -4503,7 +4938,7 @@ const validateSameTokenOwner = (accounts) => {
|
|
|
4503
4938
|
/**
|
|
4504
4939
|
* Parse compressed token accounts to get the mint, current owner and delegate.
|
|
4505
4940
|
*/
|
|
4506
|
-
const parseTokenData =
|
|
4941
|
+
const parseTokenData = compressedTokenAccounts => {
|
|
4507
4942
|
const mint = compressedTokenAccounts[0].parsed.mint;
|
|
4508
4943
|
const currentOwner = compressedTokenAccounts[0].parsed.owner;
|
|
4509
4944
|
const delegate = compressedTokenAccounts[0].parsed.delegate;
|
|
@@ -4517,13 +4952,22 @@ const parseTokenData = (compressedTokenAccounts) => {
|
|
|
4517
4952
|
* @returns Output token data for the transfer
|
|
4518
4953
|
* instruction
|
|
4519
4954
|
*/
|
|
4520
|
-
function createTransferOutputState(
|
|
4955
|
+
function createTransferOutputState(
|
|
4956
|
+
inputCompressedTokenAccounts,
|
|
4957
|
+
toAddress,
|
|
4958
|
+
amount,
|
|
4959
|
+
) {
|
|
4521
4960
|
amount = stateless_js.bn(amount);
|
|
4522
4961
|
const inputAmount = sumUpTokenAmount(inputCompressedTokenAccounts);
|
|
4523
|
-
const inputLamports = stateless_js.sumUpLamports(
|
|
4962
|
+
const inputLamports = stateless_js.sumUpLamports(
|
|
4963
|
+
inputCompressedTokenAccounts.map(acc => acc.compressedAccount),
|
|
4964
|
+
);
|
|
4524
4965
|
const changeAmount = inputAmount.sub(amount);
|
|
4525
4966
|
stateless_js.validateSufficientBalance(changeAmount);
|
|
4526
|
-
if (
|
|
4967
|
+
if (
|
|
4968
|
+
changeAmount.eq(stateless_js.bn(0)) &&
|
|
4969
|
+
inputLamports.eq(stateless_js.bn(0))
|
|
4970
|
+
) {
|
|
4527
4971
|
return [
|
|
4528
4972
|
{
|
|
4529
4973
|
owner: toAddress,
|
|
@@ -4534,7 +4978,9 @@ function createTransferOutputState(inputCompressedTokenAccounts, toAddress, amou
|
|
|
4534
4978
|
];
|
|
4535
4979
|
}
|
|
4536
4980
|
/// validates token program
|
|
4537
|
-
stateless_js.validateSameOwner(
|
|
4981
|
+
stateless_js.validateSameOwner(
|
|
4982
|
+
inputCompressedTokenAccounts.map(acc => acc.compressedAccount),
|
|
4983
|
+
);
|
|
4538
4984
|
validateSameTokenOwner(inputCompressedTokenAccounts);
|
|
4539
4985
|
const outputCompressedAccounts = [
|
|
4540
4986
|
{
|
|
@@ -4561,15 +5007,22 @@ function createTransferOutputState(inputCompressedTokenAccounts, toAddress, amou
|
|
|
4561
5007
|
*/
|
|
4562
5008
|
function createDecompressOutputState(inputCompressedTokenAccounts, amount) {
|
|
4563
5009
|
amount = stateless_js.bn(amount);
|
|
4564
|
-
const inputLamports = stateless_js.sumUpLamports(
|
|
5010
|
+
const inputLamports = stateless_js.sumUpLamports(
|
|
5011
|
+
inputCompressedTokenAccounts.map(acc => acc.compressedAccount),
|
|
5012
|
+
);
|
|
4565
5013
|
const inputAmount = sumUpTokenAmount(inputCompressedTokenAccounts);
|
|
4566
5014
|
const changeAmount = inputAmount.sub(amount);
|
|
4567
5015
|
stateless_js.validateSufficientBalance(changeAmount);
|
|
4568
5016
|
/// lamports gets decompressed
|
|
4569
|
-
if (
|
|
5017
|
+
if (
|
|
5018
|
+
changeAmount.eq(stateless_js.bn(0)) &&
|
|
5019
|
+
inputLamports.eq(stateless_js.bn(0))
|
|
5020
|
+
) {
|
|
4570
5021
|
return [];
|
|
4571
5022
|
}
|
|
4572
|
-
stateless_js.validateSameOwner(
|
|
5023
|
+
stateless_js.validateSameOwner(
|
|
5024
|
+
inputCompressedTokenAccounts.map(acc => acc.compressedAccount),
|
|
5025
|
+
);
|
|
4573
5026
|
validateSameTokenOwner(inputCompressedTokenAccounts);
|
|
4574
5027
|
const tokenTransferOutputs = [
|
|
4575
5028
|
{
|
|
@@ -4585,7 +5038,7 @@ class CompressedTokenProgram {
|
|
|
4585
5038
|
/**
|
|
4586
5039
|
* @internal
|
|
4587
5040
|
*/
|
|
4588
|
-
constructor() {
|
|
5041
|
+
constructor() {}
|
|
4589
5042
|
/** @internal */
|
|
4590
5043
|
static get program() {
|
|
4591
5044
|
if (!this._program) {
|
|
@@ -4603,21 +5056,38 @@ class CompressedTokenProgram {
|
|
|
4603
5056
|
/// program only for serde and building instructions, not for
|
|
4604
5057
|
/// interacting with the network.
|
|
4605
5058
|
const mockKeypair = web3_js.Keypair.generate();
|
|
4606
|
-
const mockConnection = new web3_js.Connection(
|
|
4607
|
-
|
|
5059
|
+
const mockConnection = new web3_js.Connection(
|
|
5060
|
+
'http://127.0.0.1:8899',
|
|
5061
|
+
'confirmed',
|
|
5062
|
+
);
|
|
5063
|
+
const mockProvider = new anchor.AnchorProvider(
|
|
5064
|
+
mockConnection,
|
|
5065
|
+
stateless_js.useWallet(mockKeypair),
|
|
5066
|
+
stateless_js.confirmConfig,
|
|
5067
|
+
);
|
|
4608
5068
|
anchor.setProvider(mockProvider);
|
|
4609
|
-
this._program = new anchor.Program(
|
|
5069
|
+
this._program = new anchor.Program(
|
|
5070
|
+
IDL,
|
|
5071
|
+
this.programId,
|
|
5072
|
+
mockProvider,
|
|
5073
|
+
);
|
|
4610
5074
|
}
|
|
4611
5075
|
}
|
|
4612
5076
|
/** @internal */
|
|
4613
5077
|
static deriveTokenPoolPda(mint) {
|
|
4614
5078
|
const seeds = [POOL_SEED, mint.toBuffer()];
|
|
4615
|
-
const [address, _] = web3_js.PublicKey.findProgramAddressSync(
|
|
5079
|
+
const [address, _] = web3_js.PublicKey.findProgramAddressSync(
|
|
5080
|
+
seeds,
|
|
5081
|
+
this.programId,
|
|
5082
|
+
);
|
|
4616
5083
|
return address;
|
|
4617
5084
|
}
|
|
4618
5085
|
/** @internal */
|
|
4619
5086
|
static get deriveCpiAuthorityPda() {
|
|
4620
|
-
const [address, _] = web3_js.PublicKey.findProgramAddressSync(
|
|
5087
|
+
const [address, _] = web3_js.PublicKey.findProgramAddressSync(
|
|
5088
|
+
[CPI_AUTHORITY_SEED],
|
|
5089
|
+
this.programId,
|
|
5090
|
+
);
|
|
4621
5091
|
return address;
|
|
4622
5092
|
}
|
|
4623
5093
|
/**
|
|
@@ -4626,14 +5096,21 @@ class CompressedTokenProgram {
|
|
|
4626
5096
|
static async createMint(params) {
|
|
4627
5097
|
const { mint, authority, feePayer, rentExemptBalance } = params;
|
|
4628
5098
|
/// Create and initialize SPL Mint account
|
|
4629
|
-
const createMintAccountInstruction =
|
|
4630
|
-
|
|
4631
|
-
|
|
4632
|
-
|
|
4633
|
-
|
|
4634
|
-
|
|
4635
|
-
|
|
4636
|
-
|
|
5099
|
+
const createMintAccountInstruction =
|
|
5100
|
+
web3_js.SystemProgram.createAccount({
|
|
5101
|
+
fromPubkey: feePayer,
|
|
5102
|
+
lamports: rentExemptBalance,
|
|
5103
|
+
newAccountPubkey: mint,
|
|
5104
|
+
programId: TOKEN_PROGRAM_ID,
|
|
5105
|
+
space: MINT_SIZE,
|
|
5106
|
+
});
|
|
5107
|
+
const initializeMintInstruction = createInitializeMint2Instruction(
|
|
5108
|
+
mint,
|
|
5109
|
+
params.decimals,
|
|
5110
|
+
authority,
|
|
5111
|
+
params.freezeAuthority,
|
|
5112
|
+
TOKEN_PROGRAM_ID,
|
|
5113
|
+
);
|
|
4637
5114
|
const ix = await this.createTokenPool({
|
|
4638
5115
|
feePayer,
|
|
4639
5116
|
mint,
|
|
@@ -4650,13 +5127,13 @@ class CompressedTokenProgram {
|
|
|
4650
5127
|
const ix = await this.program.methods
|
|
4651
5128
|
.createTokenPool()
|
|
4652
5129
|
.accounts({
|
|
4653
|
-
|
|
4654
|
-
|
|
4655
|
-
|
|
4656
|
-
|
|
4657
|
-
|
|
4658
|
-
|
|
4659
|
-
|
|
5130
|
+
mint,
|
|
5131
|
+
feePayer,
|
|
5132
|
+
tokenPoolPda,
|
|
5133
|
+
systemProgram: web3_js.SystemProgram.programId,
|
|
5134
|
+
tokenProgram: TOKEN_PROGRAM_ID,
|
|
5135
|
+
cpiAuthorityPda: this.deriveCpiAuthorityPda,
|
|
5136
|
+
})
|
|
4660
5137
|
.instruction();
|
|
4661
5138
|
return ix;
|
|
4662
5139
|
}
|
|
@@ -4665,31 +5142,41 @@ class CompressedTokenProgram {
|
|
|
4665
5142
|
*/
|
|
4666
5143
|
static async mintTo(params) {
|
|
4667
5144
|
const systemKeys = stateless_js.defaultStaticAccountsStruct();
|
|
4668
|
-
const { mint, feePayer, authority, merkleTree, toPubkey, amount } =
|
|
5145
|
+
const { mint, feePayer, authority, merkleTree, toPubkey, amount } =
|
|
5146
|
+
params;
|
|
4669
5147
|
const tokenPoolPda = this.deriveTokenPoolPda(mint);
|
|
4670
|
-
const amounts = stateless_js
|
|
5148
|
+
const amounts = stateless_js
|
|
5149
|
+
.toArray(amount)
|
|
5150
|
+
.map(amount => stateless_js.bn(amount));
|
|
4671
5151
|
const toPubkeys = stateless_js.toArray(toPubkey);
|
|
4672
5152
|
if (amounts.length !== toPubkeys.length) {
|
|
4673
|
-
throw new Error(
|
|
5153
|
+
throw new Error(
|
|
5154
|
+
'Amount and toPubkey arrays must have the same length',
|
|
5155
|
+
);
|
|
4674
5156
|
}
|
|
4675
5157
|
const instruction = await this.program.methods
|
|
4676
5158
|
.mintTo(toPubkeys, amounts, null)
|
|
4677
5159
|
.accounts({
|
|
4678
|
-
|
|
4679
|
-
|
|
4680
|
-
|
|
4681
|
-
|
|
4682
|
-
|
|
4683
|
-
|
|
4684
|
-
|
|
4685
|
-
|
|
4686
|
-
|
|
4687
|
-
|
|
4688
|
-
|
|
4689
|
-
|
|
4690
|
-
|
|
4691
|
-
|
|
4692
|
-
|
|
5160
|
+
feePayer,
|
|
5161
|
+
authority,
|
|
5162
|
+
cpiAuthorityPda: this.deriveCpiAuthorityPda,
|
|
5163
|
+
mint,
|
|
5164
|
+
tokenPoolPda,
|
|
5165
|
+
tokenProgram: TOKEN_PROGRAM_ID,
|
|
5166
|
+
lightSystemProgram: stateless_js.LightSystemProgram.programId,
|
|
5167
|
+
registeredProgramPda: systemKeys.registeredProgramPda,
|
|
5168
|
+
noopProgram: systemKeys.noopProgram,
|
|
5169
|
+
accountCompressionAuthority:
|
|
5170
|
+
systemKeys.accountCompressionAuthority,
|
|
5171
|
+
accountCompressionProgram: systemKeys.accountCompressionProgram,
|
|
5172
|
+
merkleTree:
|
|
5173
|
+
merkleTree !== null && merkleTree !== void 0
|
|
5174
|
+
? merkleTree
|
|
5175
|
+
: stateless_js.defaultTestStateTreeAccounts()
|
|
5176
|
+
.merkleTree,
|
|
5177
|
+
selfProgram: this.programId,
|
|
5178
|
+
solPoolPda: null,
|
|
5179
|
+
})
|
|
4693
5180
|
.instruction();
|
|
4694
5181
|
return instruction;
|
|
4695
5182
|
}
|
|
@@ -4698,10 +5185,22 @@ class CompressedTokenProgram {
|
|
|
4698
5185
|
* Mint tokens from registed SPL mint account to a compressed account
|
|
4699
5186
|
*/
|
|
4700
5187
|
static async approveAndMintTo(params) {
|
|
4701
|
-
const {
|
|
5188
|
+
const {
|
|
5189
|
+
mint,
|
|
5190
|
+
feePayer,
|
|
5191
|
+
authorityTokenAccount,
|
|
5192
|
+
authority,
|
|
5193
|
+
merkleTree,
|
|
5194
|
+
toPubkey,
|
|
5195
|
+
} = params;
|
|
4702
5196
|
const amount = BigInt(params.amount.toString());
|
|
4703
5197
|
/// 1. Mint to existing ATA of mintAuthority.
|
|
4704
|
-
const splMintToInstruction = createMintToInstruction(
|
|
5198
|
+
const splMintToInstruction = createMintToInstruction(
|
|
5199
|
+
mint,
|
|
5200
|
+
authorityTokenAccount,
|
|
5201
|
+
authority,
|
|
5202
|
+
amount,
|
|
5203
|
+
);
|
|
4705
5204
|
/// 2. Compress from mint authority ATA to recipient compressed account
|
|
4706
5205
|
const compressInstruction = await this.compress({
|
|
4707
5206
|
payer: feePayer,
|
|
@@ -4718,15 +5217,33 @@ class CompressedTokenProgram {
|
|
|
4718
5217
|
* Construct transfer instruction for compressed tokens
|
|
4719
5218
|
*/
|
|
4720
5219
|
static async transfer(params) {
|
|
4721
|
-
const {
|
|
4722
|
-
|
|
4723
|
-
|
|
5220
|
+
const {
|
|
5221
|
+
payer,
|
|
5222
|
+
inputCompressedTokenAccounts,
|
|
5223
|
+
recentInputStateRootIndices,
|
|
5224
|
+
recentValidityProof,
|
|
5225
|
+
amount,
|
|
5226
|
+
outputStateTrees,
|
|
5227
|
+
toAddress,
|
|
5228
|
+
} = params;
|
|
5229
|
+
const tokenTransferOutputs = createTransferOutputState(
|
|
5230
|
+
inputCompressedTokenAccounts,
|
|
5231
|
+
toAddress,
|
|
5232
|
+
amount,
|
|
5233
|
+
);
|
|
5234
|
+
const {
|
|
5235
|
+
inputTokenDataWithContext,
|
|
5236
|
+
packedOutputTokenData,
|
|
5237
|
+
remainingAccountMetas,
|
|
5238
|
+
} = packCompressedTokenAccounts({
|
|
4724
5239
|
inputCompressedTokenAccounts,
|
|
4725
5240
|
outputStateTrees,
|
|
4726
5241
|
rootIndices: recentInputStateRootIndices,
|
|
4727
5242
|
tokenTransferOutputs,
|
|
4728
5243
|
});
|
|
4729
|
-
const { mint, currentOwner } = parseTokenData(
|
|
5244
|
+
const { mint, currentOwner } = parseTokenData(
|
|
5245
|
+
inputCompressedTokenAccounts,
|
|
5246
|
+
);
|
|
4730
5247
|
const data = {
|
|
4731
5248
|
proof: recentValidityProof,
|
|
4732
5249
|
mint,
|
|
@@ -4738,24 +5255,32 @@ class CompressedTokenProgram {
|
|
|
4738
5255
|
cpiContext: null,
|
|
4739
5256
|
lamportsChangeAccountMerkleTreeIndex: null,
|
|
4740
5257
|
};
|
|
4741
|
-
const encodedData = this.program.coder.types.encode(
|
|
4742
|
-
|
|
5258
|
+
const encodedData = this.program.coder.types.encode(
|
|
5259
|
+
'CompressedTokenInstructionDataTransfer',
|
|
5260
|
+
data,
|
|
5261
|
+
);
|
|
5262
|
+
const {
|
|
5263
|
+
accountCompressionAuthority,
|
|
5264
|
+
noopProgram,
|
|
5265
|
+
registeredProgramPda,
|
|
5266
|
+
accountCompressionProgram,
|
|
5267
|
+
} = stateless_js.defaultStaticAccountsStruct();
|
|
4743
5268
|
const instruction = await this.program.methods
|
|
4744
5269
|
.transfer(encodedData)
|
|
4745
5270
|
.accounts({
|
|
4746
|
-
|
|
4747
|
-
|
|
4748
|
-
|
|
4749
|
-
|
|
4750
|
-
|
|
4751
|
-
|
|
4752
|
-
|
|
4753
|
-
|
|
4754
|
-
|
|
4755
|
-
|
|
4756
|
-
|
|
4757
|
-
|
|
4758
|
-
|
|
5271
|
+
feePayer: payer,
|
|
5272
|
+
authority: currentOwner,
|
|
5273
|
+
cpiAuthorityPda: this.deriveCpiAuthorityPda,
|
|
5274
|
+
lightSystemProgram: stateless_js.LightSystemProgram.programId,
|
|
5275
|
+
registeredProgramPda: registeredProgramPda,
|
|
5276
|
+
noopProgram: noopProgram,
|
|
5277
|
+
accountCompressionAuthority: accountCompressionAuthority,
|
|
5278
|
+
accountCompressionProgram: accountCompressionProgram,
|
|
5279
|
+
selfProgram: this.programId,
|
|
5280
|
+
tokenPoolPda: null,
|
|
5281
|
+
compressOrDecompressTokenAccount: null,
|
|
5282
|
+
tokenProgram: null,
|
|
5283
|
+
})
|
|
4759
5284
|
.remainingAccounts(remainingAccountMetas)
|
|
4760
5285
|
.instruction();
|
|
4761
5286
|
return instruction;
|
|
@@ -4764,12 +5289,14 @@ class CompressedTokenProgram {
|
|
|
4764
5289
|
* Create lookup table instructions for the token program's default accounts.
|
|
4765
5290
|
*/
|
|
4766
5291
|
static async createTokenProgramLookupTable(params) {
|
|
4767
|
-
const { authority, mints, recentSlot, payer, remainingAccounts } =
|
|
4768
|
-
|
|
4769
|
-
|
|
4770
|
-
|
|
4771
|
-
|
|
4772
|
-
|
|
5292
|
+
const { authority, mints, recentSlot, payer, remainingAccounts } =
|
|
5293
|
+
params;
|
|
5294
|
+
const [createInstruction, lookupTableAddress] =
|
|
5295
|
+
web3_js.AddressLookupTableProgram.createLookupTable({
|
|
5296
|
+
authority,
|
|
5297
|
+
payer: authority,
|
|
5298
|
+
recentSlot,
|
|
5299
|
+
});
|
|
4773
5300
|
let optionalMintKeys = [];
|
|
4774
5301
|
if (mints) {
|
|
4775
5302
|
optionalMintKeys = [
|
|
@@ -4777,28 +5304,35 @@ class CompressedTokenProgram {
|
|
|
4777
5304
|
...mints.map(mint => this.deriveTokenPoolPda(mint)),
|
|
4778
5305
|
];
|
|
4779
5306
|
}
|
|
4780
|
-
const extendInstruction =
|
|
4781
|
-
|
|
4782
|
-
|
|
4783
|
-
lookupTable: lookupTableAddress,
|
|
4784
|
-
addresses: [
|
|
4785
|
-
this.deriveCpiAuthorityPda,
|
|
4786
|
-
stateless_js.LightSystemProgram.programId,
|
|
4787
|
-
stateless_js.defaultStaticAccountsStruct().registeredProgramPda,
|
|
4788
|
-
stateless_js.defaultStaticAccountsStruct().noopProgram,
|
|
4789
|
-
stateless_js.defaultStaticAccountsStruct().accountCompressionAuthority,
|
|
4790
|
-
stateless_js.defaultStaticAccountsStruct().accountCompressionProgram,
|
|
4791
|
-
stateless_js.defaultTestStateTreeAccounts().merkleTree,
|
|
4792
|
-
stateless_js.defaultTestStateTreeAccounts().nullifierQueue,
|
|
4793
|
-
stateless_js.defaultTestStateTreeAccounts().addressTree,
|
|
4794
|
-
stateless_js.defaultTestStateTreeAccounts().addressQueue,
|
|
4795
|
-
this.programId,
|
|
4796
|
-
TOKEN_PROGRAM_ID,
|
|
5307
|
+
const extendInstruction =
|
|
5308
|
+
web3_js.AddressLookupTableProgram.extendLookupTable({
|
|
5309
|
+
payer,
|
|
4797
5310
|
authority,
|
|
4798
|
-
|
|
4799
|
-
|
|
4800
|
-
|
|
4801
|
-
|
|
5311
|
+
lookupTable: lookupTableAddress,
|
|
5312
|
+
addresses: [
|
|
5313
|
+
this.deriveCpiAuthorityPda,
|
|
5314
|
+
stateless_js.LightSystemProgram.programId,
|
|
5315
|
+
stateless_js.defaultStaticAccountsStruct()
|
|
5316
|
+
.registeredProgramPda,
|
|
5317
|
+
stateless_js.defaultStaticAccountsStruct().noopProgram,
|
|
5318
|
+
stateless_js.defaultStaticAccountsStruct()
|
|
5319
|
+
.accountCompressionAuthority,
|
|
5320
|
+
stateless_js.defaultStaticAccountsStruct()
|
|
5321
|
+
.accountCompressionProgram,
|
|
5322
|
+
stateless_js.defaultTestStateTreeAccounts().merkleTree,
|
|
5323
|
+
stateless_js.defaultTestStateTreeAccounts().nullifierQueue,
|
|
5324
|
+
stateless_js.defaultTestStateTreeAccounts().addressTree,
|
|
5325
|
+
stateless_js.defaultTestStateTreeAccounts().addressQueue,
|
|
5326
|
+
this.programId,
|
|
5327
|
+
TOKEN_PROGRAM_ID,
|
|
5328
|
+
authority,
|
|
5329
|
+
...optionalMintKeys,
|
|
5330
|
+
...(remainingAccounts !== null &&
|
|
5331
|
+
remainingAccounts !== void 0
|
|
5332
|
+
? remainingAccounts
|
|
5333
|
+
: []),
|
|
5334
|
+
],
|
|
5335
|
+
});
|
|
4802
5336
|
return {
|
|
4803
5337
|
instructions: [createInstruction, extendInstruction],
|
|
4804
5338
|
address: lookupTableAddress,
|
|
@@ -4809,14 +5343,19 @@ class CompressedTokenProgram {
|
|
|
4809
5343
|
* @returns compressInstruction
|
|
4810
5344
|
*/
|
|
4811
5345
|
static async compress(params) {
|
|
4812
|
-
const { payer, owner, source, toAddress, mint, outputStateTree } =
|
|
5346
|
+
const { payer, owner, source, toAddress, mint, outputStateTree } =
|
|
5347
|
+
params;
|
|
4813
5348
|
if (Array.isArray(params.amount) !== Array.isArray(params.toAddress)) {
|
|
4814
|
-
throw new Error(
|
|
5349
|
+
throw new Error(
|
|
5350
|
+
'Both amount and toAddress must be arrays or both must be single values',
|
|
5351
|
+
);
|
|
4815
5352
|
}
|
|
4816
5353
|
let tokenTransferOutputs;
|
|
4817
5354
|
if (Array.isArray(params.amount) && Array.isArray(params.toAddress)) {
|
|
4818
5355
|
if (params.amount.length !== params.toAddress.length) {
|
|
4819
|
-
throw new Error(
|
|
5356
|
+
throw new Error(
|
|
5357
|
+
'Amount and toAddress arrays must have the same length',
|
|
5358
|
+
);
|
|
4820
5359
|
}
|
|
4821
5360
|
tokenTransferOutputs = params.amount.map((amt, index) => {
|
|
4822
5361
|
const amount = stateless_js.bn(amt);
|
|
@@ -4827,8 +5366,7 @@ class CompressedTokenProgram {
|
|
|
4827
5366
|
tlv: null,
|
|
4828
5367
|
};
|
|
4829
5368
|
});
|
|
4830
|
-
}
|
|
4831
|
-
else {
|
|
5369
|
+
} else {
|
|
4832
5370
|
tokenTransferOutputs = [
|
|
4833
5371
|
{
|
|
4834
5372
|
owner: toAddress,
|
|
@@ -4838,7 +5376,11 @@ class CompressedTokenProgram {
|
|
|
4838
5376
|
},
|
|
4839
5377
|
];
|
|
4840
5378
|
}
|
|
4841
|
-
const {
|
|
5379
|
+
const {
|
|
5380
|
+
inputTokenDataWithContext,
|
|
5381
|
+
packedOutputTokenData,
|
|
5382
|
+
remainingAccountMetas,
|
|
5383
|
+
} = packCompressedTokenAccounts({
|
|
4842
5384
|
inputCompressedTokenAccounts: [],
|
|
4843
5385
|
outputStateTrees: outputStateTree,
|
|
4844
5386
|
rootIndices: [],
|
|
@@ -4852,30 +5394,40 @@ class CompressedTokenProgram {
|
|
|
4852
5394
|
outputCompressedAccounts: packedOutputTokenData,
|
|
4853
5395
|
compressOrDecompressAmount: Array.isArray(params.amount)
|
|
4854
5396
|
? params.amount
|
|
4855
|
-
|
|
4856
|
-
|
|
5397
|
+
.map(amt => new anchor.BN(amt))
|
|
5398
|
+
.reduce((sum, amt) => sum.add(amt), new anchor.BN(0))
|
|
4857
5399
|
: new anchor.BN(params.amount),
|
|
4858
5400
|
isCompress: true,
|
|
4859
5401
|
cpiContext: null,
|
|
4860
5402
|
lamportsChangeAccountMerkleTreeIndex: null,
|
|
4861
5403
|
};
|
|
4862
|
-
const encodedData = this.program.coder.types.encode(
|
|
5404
|
+
const encodedData = this.program.coder.types.encode(
|
|
5405
|
+
'CompressedTokenInstructionDataTransfer',
|
|
5406
|
+
data,
|
|
5407
|
+
);
|
|
4863
5408
|
const instruction = await this.program.methods
|
|
4864
5409
|
.transfer(encodedData)
|
|
4865
5410
|
.accounts({
|
|
4866
|
-
|
|
4867
|
-
|
|
4868
|
-
|
|
4869
|
-
|
|
4870
|
-
|
|
4871
|
-
|
|
4872
|
-
|
|
4873
|
-
|
|
4874
|
-
|
|
4875
|
-
|
|
4876
|
-
|
|
4877
|
-
|
|
4878
|
-
|
|
5411
|
+
feePayer: payer,
|
|
5412
|
+
authority: owner,
|
|
5413
|
+
cpiAuthorityPda: this.deriveCpiAuthorityPda,
|
|
5414
|
+
lightSystemProgram: stateless_js.LightSystemProgram.programId,
|
|
5415
|
+
registeredProgramPda:
|
|
5416
|
+
stateless_js.defaultStaticAccountsStruct()
|
|
5417
|
+
.registeredProgramPda,
|
|
5418
|
+
noopProgram:
|
|
5419
|
+
stateless_js.defaultStaticAccountsStruct().noopProgram,
|
|
5420
|
+
accountCompressionAuthority:
|
|
5421
|
+
stateless_js.defaultStaticAccountsStruct()
|
|
5422
|
+
.accountCompressionAuthority,
|
|
5423
|
+
accountCompressionProgram:
|
|
5424
|
+
stateless_js.defaultStaticAccountsStruct()
|
|
5425
|
+
.accountCompressionProgram,
|
|
5426
|
+
selfProgram: this.programId,
|
|
5427
|
+
tokenPoolPda: this.deriveTokenPoolPda(mint),
|
|
5428
|
+
compressOrDecompressTokenAccount: source, // token
|
|
5429
|
+
tokenProgram: TOKEN_PROGRAM_ID,
|
|
5430
|
+
})
|
|
4879
5431
|
.remainingAccounts(remainingAccountMetas)
|
|
4880
5432
|
.instruction();
|
|
4881
5433
|
return instruction;
|
|
@@ -4884,17 +5436,33 @@ class CompressedTokenProgram {
|
|
|
4884
5436
|
* Construct decompress instruction
|
|
4885
5437
|
*/
|
|
4886
5438
|
static async decompress(params) {
|
|
4887
|
-
const {
|
|
5439
|
+
const {
|
|
5440
|
+
payer,
|
|
5441
|
+
inputCompressedTokenAccounts,
|
|
5442
|
+
toAddress,
|
|
5443
|
+
outputStateTree,
|
|
5444
|
+
recentValidityProof,
|
|
5445
|
+
recentInputStateRootIndices,
|
|
5446
|
+
} = params;
|
|
4888
5447
|
const amount = stateless_js.bn(params.amount);
|
|
4889
|
-
const tokenTransferOutputs = createDecompressOutputState(
|
|
5448
|
+
const tokenTransferOutputs = createDecompressOutputState(
|
|
5449
|
+
inputCompressedTokenAccounts,
|
|
5450
|
+
amount,
|
|
5451
|
+
);
|
|
4890
5452
|
/// Pack
|
|
4891
|
-
const {
|
|
5453
|
+
const {
|
|
5454
|
+
inputTokenDataWithContext,
|
|
5455
|
+
packedOutputTokenData,
|
|
5456
|
+
remainingAccountMetas,
|
|
5457
|
+
} = packCompressedTokenAccounts({
|
|
4892
5458
|
inputCompressedTokenAccounts,
|
|
4893
5459
|
outputStateTrees: outputStateTree,
|
|
4894
5460
|
rootIndices: recentInputStateRootIndices,
|
|
4895
5461
|
tokenTransferOutputs: tokenTransferOutputs,
|
|
4896
5462
|
});
|
|
4897
|
-
const { mint, currentOwner } = parseTokenData(
|
|
5463
|
+
const { mint, currentOwner } = parseTokenData(
|
|
5464
|
+
inputCompressedTokenAccounts,
|
|
5465
|
+
);
|
|
4898
5466
|
const data = {
|
|
4899
5467
|
proof: recentValidityProof,
|
|
4900
5468
|
mint,
|
|
@@ -4906,30 +5474,45 @@ class CompressedTokenProgram {
|
|
|
4906
5474
|
cpiContext: null,
|
|
4907
5475
|
lamportsChangeAccountMerkleTreeIndex: null,
|
|
4908
5476
|
};
|
|
4909
|
-
const encodedData = this.program.coder.types.encode(
|
|
4910
|
-
|
|
5477
|
+
const encodedData = this.program.coder.types.encode(
|
|
5478
|
+
'CompressedTokenInstructionDataTransfer',
|
|
5479
|
+
data,
|
|
5480
|
+
);
|
|
5481
|
+
const {
|
|
5482
|
+
accountCompressionAuthority,
|
|
5483
|
+
noopProgram,
|
|
5484
|
+
registeredProgramPda,
|
|
5485
|
+
accountCompressionProgram,
|
|
5486
|
+
} = stateless_js.defaultStaticAccountsStruct();
|
|
4911
5487
|
const instruction = await this.program.methods
|
|
4912
5488
|
.transfer(encodedData)
|
|
4913
5489
|
.accounts({
|
|
4914
|
-
|
|
4915
|
-
|
|
4916
|
-
|
|
4917
|
-
|
|
4918
|
-
|
|
4919
|
-
|
|
4920
|
-
|
|
4921
|
-
|
|
4922
|
-
|
|
4923
|
-
|
|
4924
|
-
|
|
4925
|
-
|
|
4926
|
-
|
|
5490
|
+
feePayer: payer,
|
|
5491
|
+
authority: currentOwner,
|
|
5492
|
+
cpiAuthorityPda: this.deriveCpiAuthorityPda,
|
|
5493
|
+
lightSystemProgram: stateless_js.LightSystemProgram.programId,
|
|
5494
|
+
registeredProgramPda: registeredProgramPda,
|
|
5495
|
+
noopProgram: noopProgram,
|
|
5496
|
+
accountCompressionAuthority: accountCompressionAuthority,
|
|
5497
|
+
accountCompressionProgram: accountCompressionProgram,
|
|
5498
|
+
selfProgram: this.programId,
|
|
5499
|
+
tokenPoolPda: this.deriveTokenPoolPda(mint),
|
|
5500
|
+
compressOrDecompressTokenAccount: toAddress,
|
|
5501
|
+
tokenProgram: TOKEN_PROGRAM_ID,
|
|
5502
|
+
})
|
|
4927
5503
|
.remainingAccounts(remainingAccountMetas)
|
|
4928
5504
|
.instruction();
|
|
4929
5505
|
return instruction;
|
|
4930
5506
|
}
|
|
4931
5507
|
static async mergeTokenAccounts(params) {
|
|
4932
|
-
const {
|
|
5508
|
+
const {
|
|
5509
|
+
payer,
|
|
5510
|
+
owner,
|
|
5511
|
+
inputCompressedTokenAccounts,
|
|
5512
|
+
outputStateTree,
|
|
5513
|
+
recentValidityProof,
|
|
5514
|
+
recentInputStateRootIndices,
|
|
5515
|
+
} = params;
|
|
4933
5516
|
if (inputCompressedTokenAccounts.length > 3) {
|
|
4934
5517
|
throw new Error('Cannot merge more than 3 token accounts at once');
|
|
4935
5518
|
}
|
|
@@ -4937,7 +5520,10 @@ class CompressedTokenProgram {
|
|
|
4937
5520
|
payer,
|
|
4938
5521
|
inputCompressedTokenAccounts,
|
|
4939
5522
|
toAddress: owner,
|
|
4940
|
-
amount: inputCompressedTokenAccounts.reduce(
|
|
5523
|
+
amount: inputCompressedTokenAccounts.reduce(
|
|
5524
|
+
(sum, account) => sum.add(account.parsed.amount),
|
|
5525
|
+
new anchor.BN(0),
|
|
5526
|
+
),
|
|
4941
5527
|
outputStateTrees: outputStateTree,
|
|
4942
5528
|
recentInputStateRootIndices,
|
|
4943
5529
|
recentValidityProof,
|
|
@@ -4948,7 +5534,9 @@ class CompressedTokenProgram {
|
|
|
4948
5534
|
/**
|
|
4949
5535
|
* Public key that identifies the CompressedPda program
|
|
4950
5536
|
*/
|
|
4951
|
-
CompressedTokenProgram.programId = new web3_js.PublicKey(
|
|
5537
|
+
CompressedTokenProgram.programId = new web3_js.PublicKey(
|
|
5538
|
+
'cTokenmWW8bLPjZEBAUgYy3zKxQZW6VKi7bqNFEVv3m',
|
|
5539
|
+
);
|
|
4952
5540
|
CompressedTokenProgram._program = null;
|
|
4953
5541
|
|
|
4954
5542
|
/**
|
|
@@ -4966,8 +5554,22 @@ CompressedTokenProgram._program = null;
|
|
|
4966
5554
|
*
|
|
4967
5555
|
* @return Signature of the confirmed transaction
|
|
4968
5556
|
*/
|
|
4969
|
-
async function approveAndMintTo(
|
|
4970
|
-
|
|
5557
|
+
async function approveAndMintTo(
|
|
5558
|
+
rpc,
|
|
5559
|
+
payer,
|
|
5560
|
+
mint,
|
|
5561
|
+
destination,
|
|
5562
|
+
authority,
|
|
5563
|
+
amount,
|
|
5564
|
+
merkleTree,
|
|
5565
|
+
confirmOptions,
|
|
5566
|
+
) {
|
|
5567
|
+
const authorityTokenAccount = await getOrCreateAssociatedTokenAccount(
|
|
5568
|
+
rpc,
|
|
5569
|
+
payer,
|
|
5570
|
+
mint,
|
|
5571
|
+
authority.publicKey,
|
|
5572
|
+
);
|
|
4971
5573
|
const ixs = await CompressedTokenProgram.approveAndMintTo({
|
|
4972
5574
|
feePayer: payer.publicKey,
|
|
4973
5575
|
mint,
|
|
@@ -4979,10 +5581,17 @@ async function approveAndMintTo(rpc, payer, mint, destination, authority, amount
|
|
|
4979
5581
|
});
|
|
4980
5582
|
const { blockhash } = await rpc.getLatestBlockhash();
|
|
4981
5583
|
const additionalSigners = stateless_js.dedupeSigner(payer, [authority]);
|
|
4982
|
-
const tx = stateless_js.buildAndSignTx(
|
|
4983
|
-
|
|
4984
|
-
|
|
4985
|
-
|
|
5584
|
+
const tx = stateless_js.buildAndSignTx(
|
|
5585
|
+
[
|
|
5586
|
+
web3_js.ComputeBudgetProgram.setComputeUnitLimit({
|
|
5587
|
+
units: 1000000,
|
|
5588
|
+
}),
|
|
5589
|
+
...ixs,
|
|
5590
|
+
],
|
|
5591
|
+
payer,
|
|
5592
|
+
blockhash,
|
|
5593
|
+
additionalSigners,
|
|
5594
|
+
);
|
|
4986
5595
|
const txId = await stateless_js.sendAndConfirmTx(rpc, tx, confirmOptions);
|
|
4987
5596
|
return txId;
|
|
4988
5597
|
}
|
|
@@ -5005,7 +5614,17 @@ async function approveAndMintTo(rpc, payer, mint, destination, authority, amount
|
|
|
5005
5614
|
*
|
|
5006
5615
|
* @return Signature of the confirmed transaction
|
|
5007
5616
|
*/
|
|
5008
|
-
async function compress(
|
|
5617
|
+
async function compress(
|
|
5618
|
+
rpc,
|
|
5619
|
+
payer,
|
|
5620
|
+
mint,
|
|
5621
|
+
amount,
|
|
5622
|
+
owner,
|
|
5623
|
+
sourceTokenAccount,
|
|
5624
|
+
toAddress,
|
|
5625
|
+
merkleTree,
|
|
5626
|
+
confirmOptions,
|
|
5627
|
+
) {
|
|
5009
5628
|
const compressIx = await CompressedTokenProgram.compress({
|
|
5010
5629
|
payer: payer.publicKey,
|
|
5011
5630
|
owner: owner.publicKey,
|
|
@@ -5017,13 +5636,23 @@ async function compress(rpc, payer, mint, amount, owner, sourceTokenAccount, toA
|
|
|
5017
5636
|
});
|
|
5018
5637
|
const blockhashCtx = await rpc.getLatestBlockhash();
|
|
5019
5638
|
const additionalSigners = stateless_js.dedupeSigner(payer, [owner]);
|
|
5020
|
-
const signedTx = stateless_js.buildAndSignTx(
|
|
5021
|
-
|
|
5022
|
-
|
|
5023
|
-
|
|
5024
|
-
|
|
5025
|
-
|
|
5026
|
-
|
|
5639
|
+
const signedTx = stateless_js.buildAndSignTx(
|
|
5640
|
+
[
|
|
5641
|
+
web3_js.ComputeBudgetProgram.setComputeUnitLimit({
|
|
5642
|
+
units: 1000000,
|
|
5643
|
+
}),
|
|
5644
|
+
compressIx,
|
|
5645
|
+
],
|
|
5646
|
+
payer,
|
|
5647
|
+
blockhashCtx.blockhash,
|
|
5648
|
+
additionalSigners,
|
|
5649
|
+
);
|
|
5650
|
+
const txId = await stateless_js.sendAndConfirmTx(
|
|
5651
|
+
rpc,
|
|
5652
|
+
signedTx,
|
|
5653
|
+
confirmOptions,
|
|
5654
|
+
blockhashCtx,
|
|
5655
|
+
);
|
|
5027
5656
|
return txId;
|
|
5028
5657
|
}
|
|
5029
5658
|
|
|
@@ -5044,15 +5673,33 @@ async function compress(rpc, payer, mint, amount, owner, sourceTokenAccount, toA
|
|
|
5044
5673
|
*
|
|
5045
5674
|
* @return Signature of the confirmed transaction
|
|
5046
5675
|
*/
|
|
5047
|
-
async function transfer(
|
|
5048
|
-
|
|
5049
|
-
|
|
5676
|
+
async function transfer(
|
|
5677
|
+
rpc,
|
|
5678
|
+
payer,
|
|
5679
|
+
mint,
|
|
5680
|
+
amount,
|
|
5681
|
+
owner,
|
|
5682
|
+
toAddress,
|
|
5683
|
+
/// TODO: allow multiple
|
|
5684
|
+
merkleTree,
|
|
5685
|
+
confirmOptions,
|
|
5686
|
+
) {
|
|
5050
5687
|
amount = stateless_js.bn(amount);
|
|
5051
|
-
const compressedTokenAccounts = await rpc.getCompressedTokenAccountsByOwner(
|
|
5052
|
-
|
|
5053
|
-
|
|
5054
|
-
|
|
5055
|
-
|
|
5688
|
+
const compressedTokenAccounts = await rpc.getCompressedTokenAccountsByOwner(
|
|
5689
|
+
owner.publicKey,
|
|
5690
|
+
{
|
|
5691
|
+
mint,
|
|
5692
|
+
},
|
|
5693
|
+
);
|
|
5694
|
+
const [inputAccounts] = selectMinCompressedTokenAccountsForTransfer(
|
|
5695
|
+
compressedTokenAccounts.items,
|
|
5696
|
+
amount,
|
|
5697
|
+
);
|
|
5698
|
+
const proof = await rpc.getValidityProof(
|
|
5699
|
+
inputAccounts.map(account =>
|
|
5700
|
+
stateless_js.bn(account.compressedAccount.hash),
|
|
5701
|
+
),
|
|
5702
|
+
);
|
|
5056
5703
|
const ix = await CompressedTokenProgram.transfer({
|
|
5057
5704
|
payer: payer.publicKey,
|
|
5058
5705
|
inputCompressedTokenAccounts: inputAccounts,
|
|
@@ -5064,8 +5711,22 @@ merkleTree, confirmOptions) {
|
|
|
5064
5711
|
});
|
|
5065
5712
|
const { blockhash } = await rpc.getLatestBlockhash();
|
|
5066
5713
|
const additionalSigners = stateless_js.dedupeSigner(payer, [owner]);
|
|
5067
|
-
const signedTx = stateless_js.buildAndSignTx(
|
|
5068
|
-
|
|
5714
|
+
const signedTx = stateless_js.buildAndSignTx(
|
|
5715
|
+
[
|
|
5716
|
+
web3_js.ComputeBudgetProgram.setComputeUnitLimit({
|
|
5717
|
+
units: 1000000,
|
|
5718
|
+
}),
|
|
5719
|
+
ix,
|
|
5720
|
+
],
|
|
5721
|
+
payer,
|
|
5722
|
+
blockhash,
|
|
5723
|
+
additionalSigners,
|
|
5724
|
+
);
|
|
5725
|
+
const txId = await stateless_js.sendAndConfirmTx(
|
|
5726
|
+
rpc,
|
|
5727
|
+
signedTx,
|
|
5728
|
+
confirmOptions,
|
|
5729
|
+
);
|
|
5069
5730
|
return txId;
|
|
5070
5731
|
}
|
|
5071
5732
|
/**
|
|
@@ -5081,14 +5742,17 @@ function selectMinCompressedTokenAccountsForTransfer(accounts, transferAmount) {
|
|
|
5081
5742
|
const selectedAccounts = [];
|
|
5082
5743
|
accounts.sort((a, b) => b.parsed.amount.cmp(a.parsed.amount));
|
|
5083
5744
|
for (const account of accounts) {
|
|
5084
|
-
if (accumulatedAmount.gte(stateless_js.bn(transferAmount)))
|
|
5085
|
-
break;
|
|
5745
|
+
if (accumulatedAmount.gte(stateless_js.bn(transferAmount))) break;
|
|
5086
5746
|
accumulatedAmount = accumulatedAmount.add(account.parsed.amount);
|
|
5087
|
-
accumulatedLamports = accumulatedLamports.add(
|
|
5747
|
+
accumulatedLamports = accumulatedLamports.add(
|
|
5748
|
+
account.compressedAccount.lamports,
|
|
5749
|
+
);
|
|
5088
5750
|
selectedAccounts.push(account);
|
|
5089
5751
|
}
|
|
5090
5752
|
if (accumulatedAmount.lt(stateless_js.bn(transferAmount))) {
|
|
5091
|
-
throw new Error(
|
|
5753
|
+
throw new Error(
|
|
5754
|
+
`Not enough balance for transfer. Required: ${transferAmount.toString()}, available: ${accumulatedAmount.toString()}`,
|
|
5755
|
+
);
|
|
5092
5756
|
}
|
|
5093
5757
|
return [
|
|
5094
5758
|
selectedAccounts,
|
|
@@ -5115,16 +5779,34 @@ function selectMinCompressedTokenAccountsForTransfer(accounts, transferAmount) {
|
|
|
5115
5779
|
*
|
|
5116
5780
|
* @return Signature of the confirmed transaction
|
|
5117
5781
|
*/
|
|
5118
|
-
async function decompress(
|
|
5119
|
-
|
|
5120
|
-
|
|
5782
|
+
async function decompress(
|
|
5783
|
+
rpc,
|
|
5784
|
+
payer,
|
|
5785
|
+
mint,
|
|
5786
|
+
amount,
|
|
5787
|
+
owner,
|
|
5788
|
+
toAddress,
|
|
5789
|
+
/// TODO: allow multiple
|
|
5790
|
+
merkleTree,
|
|
5791
|
+
confirmOptions,
|
|
5792
|
+
) {
|
|
5121
5793
|
amount = stateless_js.bn(amount);
|
|
5122
|
-
const compressedTokenAccounts = await rpc.getCompressedTokenAccountsByOwner(
|
|
5123
|
-
|
|
5124
|
-
|
|
5794
|
+
const compressedTokenAccounts = await rpc.getCompressedTokenAccountsByOwner(
|
|
5795
|
+
owner.publicKey,
|
|
5796
|
+
{
|
|
5797
|
+
mint,
|
|
5798
|
+
},
|
|
5799
|
+
);
|
|
5125
5800
|
/// TODO: consider using a different selection algorithm
|
|
5126
|
-
const [inputAccounts] = selectMinCompressedTokenAccountsForTransfer(
|
|
5127
|
-
|
|
5801
|
+
const [inputAccounts] = selectMinCompressedTokenAccountsForTransfer(
|
|
5802
|
+
compressedTokenAccounts.items,
|
|
5803
|
+
amount,
|
|
5804
|
+
);
|
|
5805
|
+
const proof = await rpc.getValidityProof(
|
|
5806
|
+
inputAccounts.map(account =>
|
|
5807
|
+
stateless_js.bn(account.compressedAccount.hash),
|
|
5808
|
+
),
|
|
5809
|
+
);
|
|
5128
5810
|
const ix = await CompressedTokenProgram.decompress({
|
|
5129
5811
|
payer: payer.publicKey,
|
|
5130
5812
|
inputCompressedTokenAccounts: inputAccounts,
|
|
@@ -5136,8 +5818,22 @@ merkleTree, confirmOptions) {
|
|
|
5136
5818
|
});
|
|
5137
5819
|
const { blockhash } = await rpc.getLatestBlockhash();
|
|
5138
5820
|
const additionalSigners = stateless_js.dedupeSigner(payer, [owner]);
|
|
5139
|
-
const signedTx = stateless_js.buildAndSignTx(
|
|
5140
|
-
|
|
5821
|
+
const signedTx = stateless_js.buildAndSignTx(
|
|
5822
|
+
[
|
|
5823
|
+
web3_js.ComputeBudgetProgram.setComputeUnitLimit({
|
|
5824
|
+
units: 1000000,
|
|
5825
|
+
}),
|
|
5826
|
+
ix,
|
|
5827
|
+
],
|
|
5828
|
+
payer,
|
|
5829
|
+
blockhash,
|
|
5830
|
+
additionalSigners,
|
|
5831
|
+
);
|
|
5832
|
+
const txId = await stateless_js.sendAndConfirmTx(
|
|
5833
|
+
rpc,
|
|
5834
|
+
signedTx,
|
|
5835
|
+
confirmOptions,
|
|
5836
|
+
);
|
|
5141
5837
|
return txId;
|
|
5142
5838
|
}
|
|
5143
5839
|
|
|
@@ -5153,8 +5849,16 @@ merkleTree, confirmOptions) {
|
|
|
5153
5849
|
*
|
|
5154
5850
|
* @return Address of the new mint and the transaction signature
|
|
5155
5851
|
*/
|
|
5156
|
-
async function createMint(
|
|
5157
|
-
|
|
5852
|
+
async function createMint(
|
|
5853
|
+
rpc,
|
|
5854
|
+
payer,
|
|
5855
|
+
mintAuthority,
|
|
5856
|
+
decimals,
|
|
5857
|
+
keypair = web3_js.Keypair.generate(),
|
|
5858
|
+
confirmOptions,
|
|
5859
|
+
) {
|
|
5860
|
+
const rentExemptBalance =
|
|
5861
|
+
await rpc.getMinimumBalanceForRentExemption(MINT_SIZE);
|
|
5158
5862
|
const ixs = await CompressedTokenProgram.createMint({
|
|
5159
5863
|
feePayer: payer.publicKey,
|
|
5160
5864
|
mint: keypair.publicKey,
|
|
@@ -5165,7 +5869,12 @@ async function createMint(rpc, payer, mintAuthority, decimals, keypair = web3_js
|
|
|
5165
5869
|
});
|
|
5166
5870
|
const { blockhash } = await rpc.getLatestBlockhash();
|
|
5167
5871
|
const additionalSigners = stateless_js.dedupeSigner(payer, [keypair]);
|
|
5168
|
-
const tx = stateless_js.buildAndSignTx(
|
|
5872
|
+
const tx = stateless_js.buildAndSignTx(
|
|
5873
|
+
ixs,
|
|
5874
|
+
payer,
|
|
5875
|
+
blockhash,
|
|
5876
|
+
additionalSigners,
|
|
5877
|
+
);
|
|
5169
5878
|
const txId = await stateless_js.sendAndConfirmTx(rpc, tx, confirmOptions);
|
|
5170
5879
|
return { mint: keypair.publicKey, transactionSignature: txId };
|
|
5171
5880
|
}
|
|
@@ -5187,7 +5896,16 @@ async function createMint(rpc, payer, mintAuthority, decimals, keypair = web3_js
|
|
|
5187
5896
|
*
|
|
5188
5897
|
* @return Signature of the confirmed transaction
|
|
5189
5898
|
*/
|
|
5190
|
-
async function mintTo(
|
|
5899
|
+
async function mintTo(
|
|
5900
|
+
rpc,
|
|
5901
|
+
payer,
|
|
5902
|
+
mint,
|
|
5903
|
+
destination,
|
|
5904
|
+
authority,
|
|
5905
|
+
amount,
|
|
5906
|
+
merkleTree,
|
|
5907
|
+
confirmOptions,
|
|
5908
|
+
) {
|
|
5191
5909
|
const additionalSigners = stateless_js.dedupeSigner(payer, [authority]);
|
|
5192
5910
|
const ix = await CompressedTokenProgram.mintTo({
|
|
5193
5911
|
feePayer: payer.publicKey,
|
|
@@ -5198,7 +5916,17 @@ async function mintTo(rpc, payer, mint, destination, authority, amount, merkleTr
|
|
|
5198
5916
|
merkleTree,
|
|
5199
5917
|
});
|
|
5200
5918
|
const { blockhash } = await rpc.getLatestBlockhash();
|
|
5201
|
-
const tx = stateless_js.buildAndSignTx(
|
|
5919
|
+
const tx = stateless_js.buildAndSignTx(
|
|
5920
|
+
[
|
|
5921
|
+
web3_js.ComputeBudgetProgram.setComputeUnitLimit({
|
|
5922
|
+
units: 1000000,
|
|
5923
|
+
}),
|
|
5924
|
+
ix,
|
|
5925
|
+
],
|
|
5926
|
+
payer,
|
|
5927
|
+
blockhash,
|
|
5928
|
+
additionalSigners,
|
|
5929
|
+
);
|
|
5202
5930
|
const txId = await stateless_js.sendAndConfirmTx(rpc, tx, confirmOptions);
|
|
5203
5931
|
return txId;
|
|
5204
5932
|
}
|
|
@@ -5216,35 +5944,67 @@ async function mintTo(rpc, payer, mint, destination, authority, amount, merkleTr
|
|
|
5216
5944
|
*
|
|
5217
5945
|
* @return Array of transaction signatures
|
|
5218
5946
|
*/
|
|
5219
|
-
async function mergeTokenAccounts(
|
|
5220
|
-
|
|
5947
|
+
async function mergeTokenAccounts(
|
|
5948
|
+
rpc,
|
|
5949
|
+
payer,
|
|
5950
|
+
mint,
|
|
5951
|
+
owner,
|
|
5952
|
+
merkleTree,
|
|
5953
|
+
confirmOptions,
|
|
5954
|
+
) {
|
|
5955
|
+
const compressedTokenAccounts = await rpc.getCompressedTokenAccountsByOwner(
|
|
5956
|
+
owner.publicKey,
|
|
5957
|
+
{ mint },
|
|
5958
|
+
);
|
|
5221
5959
|
if (compressedTokenAccounts.items.length === 0) {
|
|
5222
|
-
throw new Error(
|
|
5960
|
+
throw new Error(
|
|
5961
|
+
`No compressed token accounts found for mint ${mint.toBase58()}`,
|
|
5962
|
+
);
|
|
5223
5963
|
}
|
|
5224
5964
|
if (compressedTokenAccounts.items.length >= 6) {
|
|
5225
|
-
throw new Error(
|
|
5965
|
+
throw new Error(
|
|
5966
|
+
`Too many compressed token accounts used for mint ${mint.toBase58()}`,
|
|
5967
|
+
);
|
|
5226
5968
|
}
|
|
5227
5969
|
const instructions = [
|
|
5228
5970
|
web3_js.ComputeBudgetProgram.setComputeUnitLimit({ units: 1000000 }),
|
|
5229
5971
|
];
|
|
5230
|
-
for (
|
|
5972
|
+
for (
|
|
5973
|
+
let i = 0;
|
|
5974
|
+
i < compressedTokenAccounts.items.slice(0, 6).length;
|
|
5975
|
+
i += 3
|
|
5976
|
+
) {
|
|
5231
5977
|
const batch = compressedTokenAccounts.items.slice(i, i + 3);
|
|
5232
|
-
const proof = await rpc.getValidityProof(
|
|
5233
|
-
|
|
5234
|
-
|
|
5235
|
-
|
|
5236
|
-
|
|
5237
|
-
|
|
5238
|
-
|
|
5239
|
-
|
|
5240
|
-
|
|
5241
|
-
|
|
5978
|
+
const proof = await rpc.getValidityProof(
|
|
5979
|
+
batch.map(account =>
|
|
5980
|
+
stateless_js.bn(account.compressedAccount.hash),
|
|
5981
|
+
),
|
|
5982
|
+
);
|
|
5983
|
+
const batchInstructions =
|
|
5984
|
+
await CompressedTokenProgram.mergeTokenAccounts({
|
|
5985
|
+
payer: payer.publicKey,
|
|
5986
|
+
owner: owner.publicKey,
|
|
5987
|
+
mint,
|
|
5988
|
+
inputCompressedTokenAccounts: batch,
|
|
5989
|
+
outputStateTree: merkleTree,
|
|
5990
|
+
recentValidityProof: proof.compressedProof,
|
|
5991
|
+
recentInputStateRootIndices: proof.rootIndices,
|
|
5992
|
+
});
|
|
5242
5993
|
instructions.push(...batchInstructions);
|
|
5243
5994
|
}
|
|
5244
5995
|
const { blockhash } = await rpc.getLatestBlockhash();
|
|
5245
5996
|
const additionalSigners = stateless_js.dedupeSigner(payer, [owner]);
|
|
5246
|
-
const signedTx = stateless_js.buildAndSignTx(
|
|
5247
|
-
|
|
5997
|
+
const signedTx = stateless_js.buildAndSignTx(
|
|
5998
|
+
instructions,
|
|
5999
|
+
payer,
|
|
6000
|
+
blockhash,
|
|
6001
|
+
additionalSigners,
|
|
6002
|
+
);
|
|
6003
|
+
const txId = await stateless_js.sendAndConfirmTx(
|
|
6004
|
+
rpc,
|
|
6005
|
+
signedTx,
|
|
6006
|
+
confirmOptions,
|
|
6007
|
+
);
|
|
5248
6008
|
return txId;
|
|
5249
6009
|
}
|
|
5250
6010
|
|
|
@@ -5283,23 +6043,50 @@ async function createTokenPool(rpc, payer, mintAddress, confirmOptions) {
|
|
|
5283
6043
|
*
|
|
5284
6044
|
* @return Transaction signatures and the address of the created lookup table
|
|
5285
6045
|
*/
|
|
5286
|
-
async function createTokenProgramLookupTable(
|
|
6046
|
+
async function createTokenProgramLookupTable(
|
|
6047
|
+
rpc,
|
|
6048
|
+
payer,
|
|
6049
|
+
authority,
|
|
6050
|
+
mints,
|
|
6051
|
+
additionalAccounts,
|
|
6052
|
+
) {
|
|
5287
6053
|
const recentSlot = await rpc.getSlot('finalized');
|
|
5288
|
-
const { instructions, address } =
|
|
5289
|
-
|
|
5290
|
-
|
|
5291
|
-
|
|
5292
|
-
|
|
5293
|
-
|
|
5294
|
-
|
|
6054
|
+
const { instructions, address } =
|
|
6055
|
+
await CompressedTokenProgram.createTokenProgramLookupTable({
|
|
6056
|
+
payer: payer.publicKey,
|
|
6057
|
+
authority: authority.publicKey,
|
|
6058
|
+
mints,
|
|
6059
|
+
remainingAccounts: additionalAccounts,
|
|
6060
|
+
recentSlot,
|
|
6061
|
+
});
|
|
5295
6062
|
const additionalSigners = stateless_js.dedupeSigner(payer, [authority]);
|
|
5296
6063
|
const blockhashCtx = await rpc.getLatestBlockhash();
|
|
5297
|
-
const signedTx = stateless_js.buildAndSignTx(
|
|
6064
|
+
const signedTx = stateless_js.buildAndSignTx(
|
|
6065
|
+
[instructions[0]],
|
|
6066
|
+
payer,
|
|
6067
|
+
blockhashCtx.blockhash,
|
|
6068
|
+
additionalSigners,
|
|
6069
|
+
);
|
|
5298
6070
|
/// Must wait for the first instruction to be finalized.
|
|
5299
|
-
const txId = await stateless_js.sendAndConfirmTx(
|
|
6071
|
+
const txId = await stateless_js.sendAndConfirmTx(
|
|
6072
|
+
rpc,
|
|
6073
|
+
signedTx,
|
|
6074
|
+
{ commitment: 'finalized' },
|
|
6075
|
+
blockhashCtx,
|
|
6076
|
+
);
|
|
5300
6077
|
const blockhashCtx2 = await rpc.getLatestBlockhash();
|
|
5301
|
-
const signedTx2 = stateless_js.buildAndSignTx(
|
|
5302
|
-
|
|
6078
|
+
const signedTx2 = stateless_js.buildAndSignTx(
|
|
6079
|
+
[instructions[1]],
|
|
6080
|
+
payer,
|
|
6081
|
+
blockhashCtx2.blockhash,
|
|
6082
|
+
additionalSigners,
|
|
6083
|
+
);
|
|
6084
|
+
const txId2 = await stateless_js.sendAndConfirmTx(
|
|
6085
|
+
rpc,
|
|
6086
|
+
signedTx2,
|
|
6087
|
+
{ commitment: 'finalized' },
|
|
6088
|
+
blockhashCtx2,
|
|
6089
|
+
);
|
|
5303
6090
|
return { txIds: [txId, txId2], address };
|
|
5304
6091
|
}
|
|
5305
6092
|
|
|
@@ -5320,7 +6107,8 @@ exports.mergeTokenAccounts = mergeTokenAccounts;
|
|
|
5320
6107
|
exports.mintTo = mintTo;
|
|
5321
6108
|
exports.packCompressedTokenAccounts = packCompressedTokenAccounts;
|
|
5322
6109
|
exports.parseTokenData = parseTokenData;
|
|
5323
|
-
exports.selectMinCompressedTokenAccountsForTransfer =
|
|
6110
|
+
exports.selectMinCompressedTokenAccountsForTransfer =
|
|
6111
|
+
selectMinCompressedTokenAccountsForTransfer;
|
|
5324
6112
|
exports.sumUpTokenAmount = sumUpTokenAmount;
|
|
5325
6113
|
exports.transfer = transfer;
|
|
5326
6114
|
exports.validateSameTokenOwner = validateSameTokenOwner;
|