@lightprotocol/compressed-token 0.12.0 → 0.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/browser/index.cjs +3059 -2187
- package/dist/cjs/browser/index.cjs.map +1 -1
- package/dist/cjs/node/index.cjs +1242 -602
- package/dist/cjs/node/index.cjs.map +1 -1
- package/dist/es/browser/index.js +2986 -2150
- package/dist/es/browser/index.js.map +1 -1
- package/dist/types/index.d.ts +303 -212
- package/package.json +5 -4
package/dist/cjs/node/index.cjs
CHANGED
|
@@ -6,7 +6,7 @@ var anchor = require('@coral-xyz/anchor');
|
|
|
6
6
|
var require$$0 = require('buffer');
|
|
7
7
|
|
|
8
8
|
const IDL = {
|
|
9
|
-
version: '1.
|
|
9
|
+
version: '1.2.0',
|
|
10
10
|
name: 'light_compressed_token',
|
|
11
11
|
instructions: [
|
|
12
12
|
{
|
|
@@ -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,128 +1561,23 @@ const IDL = {
|
|
|
1556
1561
|
errors: [
|
|
1557
1562
|
{
|
|
1558
1563
|
code: 6000,
|
|
1559
|
-
name: '
|
|
1560
|
-
msg: '
|
|
1564
|
+
name: 'SignerCheckFailed',
|
|
1565
|
+
msg: 'Signer check failed',
|
|
1561
1566
|
},
|
|
1562
1567
|
{
|
|
1563
1568
|
code: 6001,
|
|
1564
|
-
name: '
|
|
1565
|
-
msg: '
|
|
1569
|
+
name: 'CreateTransferInstructionFailed',
|
|
1570
|
+
msg: 'Create transfer instruction failed',
|
|
1566
1571
|
},
|
|
1567
1572
|
{
|
|
1568
1573
|
code: 6002,
|
|
1569
|
-
name: '
|
|
1570
|
-
msg: '
|
|
1574
|
+
name: 'AccountNotFound',
|
|
1575
|
+
msg: 'Account not found',
|
|
1571
1576
|
},
|
|
1572
1577
|
{
|
|
1573
1578
|
code: 6003,
|
|
1574
|
-
name: '
|
|
1575
|
-
msg: '
|
|
1576
|
-
},
|
|
1577
|
-
{
|
|
1578
|
-
code: 6004,
|
|
1579
|
-
name: 'ComputeDecompressSumFailed',
|
|
1580
|
-
msg: 'ComputeDecompressSumFailed',
|
|
1581
|
-
},
|
|
1582
|
-
{
|
|
1583
|
-
code: 6005,
|
|
1584
|
-
name: 'SumCheckFailed',
|
|
1585
|
-
msg: 'SumCheckFailed',
|
|
1586
|
-
},
|
|
1587
|
-
{
|
|
1588
|
-
code: 6006,
|
|
1589
|
-
name: 'DecompressRecipientUndefinedForDecompress',
|
|
1590
|
-
msg: 'DecompressRecipientUndefinedForDecompress',
|
|
1591
|
-
},
|
|
1592
|
-
{
|
|
1593
|
-
code: 6007,
|
|
1594
|
-
name: 'CompressedPdaUndefinedForDecompress',
|
|
1595
|
-
msg: 'CompressedPdaUndefinedForDecompress',
|
|
1596
|
-
},
|
|
1597
|
-
{
|
|
1598
|
-
code: 6008,
|
|
1599
|
-
name: 'DeCompressAmountUndefinedForDecompress',
|
|
1600
|
-
msg: 'DeCompressAmountUndefinedForDecompress',
|
|
1601
|
-
},
|
|
1602
|
-
{
|
|
1603
|
-
code: 6009,
|
|
1604
|
-
name: 'CompressedPdaUndefinedForCompress',
|
|
1605
|
-
msg: 'CompressedPdaUndefinedForCompress',
|
|
1606
|
-
},
|
|
1607
|
-
{
|
|
1608
|
-
code: 6010,
|
|
1609
|
-
name: 'DeCompressAmountUndefinedForCompress',
|
|
1610
|
-
msg: 'DeCompressAmountUndefinedForCompress',
|
|
1611
|
-
},
|
|
1612
|
-
{
|
|
1613
|
-
code: 6011,
|
|
1614
|
-
name: 'DelegateSignerCheckFailed',
|
|
1615
|
-
msg: 'DelegateSignerCheckFailed',
|
|
1616
|
-
},
|
|
1617
|
-
{
|
|
1618
|
-
code: 6012,
|
|
1619
|
-
name: 'MintTooLarge',
|
|
1620
|
-
msg: 'Minted amount greater than u64::MAX',
|
|
1621
|
-
},
|
|
1622
|
-
{
|
|
1623
|
-
code: 6013,
|
|
1624
|
-
name: 'SplTokenSupplyMismatch',
|
|
1625
|
-
msg: 'SplTokenSupplyMismatch',
|
|
1626
|
-
},
|
|
1627
|
-
{
|
|
1628
|
-
code: 6014,
|
|
1629
|
-
name: 'HeapMemoryCheckFailed',
|
|
1630
|
-
msg: 'HeapMemoryCheckFailed',
|
|
1631
|
-
},
|
|
1632
|
-
{
|
|
1633
|
-
code: 6015,
|
|
1634
|
-
name: 'InstructionNotCallable',
|
|
1635
|
-
msg: 'The instruction is not callable',
|
|
1636
|
-
},
|
|
1637
|
-
{
|
|
1638
|
-
code: 6016,
|
|
1639
|
-
name: 'ArithmeticUnderflow',
|
|
1640
|
-
msg: 'ArithmeticUnderflow',
|
|
1641
|
-
},
|
|
1642
|
-
{
|
|
1643
|
-
code: 6017,
|
|
1644
|
-
name: 'HashToFieldError',
|
|
1645
|
-
msg: 'HashToFieldError',
|
|
1646
|
-
},
|
|
1647
|
-
{
|
|
1648
|
-
code: 6018,
|
|
1649
|
-
name: 'InvalidAuthorityMint',
|
|
1650
|
-
msg: 'Expected the authority to be also a mint authority',
|
|
1651
|
-
},
|
|
1652
|
-
{
|
|
1653
|
-
code: 6019,
|
|
1654
|
-
name: 'InvalidFreezeAuthority',
|
|
1655
|
-
msg: 'Provided authority is not the freeze authority',
|
|
1656
|
-
},
|
|
1657
|
-
{
|
|
1658
|
-
code: 6020,
|
|
1659
|
-
name: 'InvalidDelegateIndex',
|
|
1660
|
-
},
|
|
1661
|
-
{
|
|
1662
|
-
code: 6021,
|
|
1663
|
-
name: 'TokenPoolPdaUndefined',
|
|
1664
|
-
},
|
|
1665
|
-
{
|
|
1666
|
-
code: 6022,
|
|
1667
|
-
name: 'IsTokenPoolPda',
|
|
1668
|
-
msg: 'Compress or decompress recipient is the same account as the token pool pda.',
|
|
1669
|
-
},
|
|
1670
|
-
{
|
|
1671
|
-
code: 6023,
|
|
1672
|
-
name: 'InvalidTokenPoolPda',
|
|
1673
|
-
},
|
|
1674
|
-
{
|
|
1675
|
-
code: 6024,
|
|
1676
|
-
name: 'NoInputTokenAccountsProvided',
|
|
1677
|
-
},
|
|
1678
|
-
{
|
|
1679
|
-
code: 6025,
|
|
1680
|
-
name: 'NoInputsProvided',
|
|
1579
|
+
name: 'SerializationError',
|
|
1580
|
+
msg: 'Serialization error',
|
|
1681
1581
|
},
|
|
1682
1582
|
],
|
|
1683
1583
|
};
|
|
@@ -1687,20 +1587,37 @@ const IDL = {
|
|
|
1687
1587
|
* Packs Compressed Token Accounts.
|
|
1688
1588
|
*/
|
|
1689
1589
|
function packCompressedTokenAccounts(params) {
|
|
1690
|
-
const {
|
|
1590
|
+
const {
|
|
1591
|
+
inputCompressedTokenAccounts,
|
|
1592
|
+
outputStateTrees,
|
|
1593
|
+
remainingAccounts = [],
|
|
1594
|
+
rootIndices,
|
|
1595
|
+
tokenTransferOutputs,
|
|
1596
|
+
} = params;
|
|
1691
1597
|
const _remainingAccounts = remainingAccounts.slice();
|
|
1692
1598
|
let delegateIndex = null;
|
|
1693
|
-
if (
|
|
1694
|
-
inputCompressedTokenAccounts
|
|
1695
|
-
|
|
1599
|
+
if (
|
|
1600
|
+
inputCompressedTokenAccounts.length > 0 &&
|
|
1601
|
+
inputCompressedTokenAccounts[0].parsed.delegate
|
|
1602
|
+
) {
|
|
1603
|
+
delegateIndex = stateless_js.getIndexOrAdd(
|
|
1604
|
+
_remainingAccounts,
|
|
1605
|
+
inputCompressedTokenAccounts[0].parsed.delegate,
|
|
1606
|
+
);
|
|
1696
1607
|
}
|
|
1697
1608
|
/// TODO: move pubkeyArray to remainingAccounts
|
|
1698
1609
|
/// Currently just packs 'delegate' to pubkeyArray
|
|
1699
1610
|
const packedInputTokenData = [];
|
|
1700
1611
|
/// pack inputs
|
|
1701
1612
|
inputCompressedTokenAccounts.forEach((account, index) => {
|
|
1702
|
-
const merkleTreePubkeyIndex = stateless_js.getIndexOrAdd(
|
|
1703
|
-
|
|
1613
|
+
const merkleTreePubkeyIndex = stateless_js.getIndexOrAdd(
|
|
1614
|
+
_remainingAccounts,
|
|
1615
|
+
account.compressedAccount.merkleTree,
|
|
1616
|
+
);
|
|
1617
|
+
const nullifierQueuePubkeyIndex = stateless_js.getIndexOrAdd(
|
|
1618
|
+
_remainingAccounts,
|
|
1619
|
+
account.compressedAccount.nullifierQueue,
|
|
1620
|
+
);
|
|
1704
1621
|
packedInputTokenData.push({
|
|
1705
1622
|
amount: account.parsed.amount,
|
|
1706
1623
|
delegateIndex,
|
|
@@ -1718,15 +1635,27 @@ function packCompressedTokenAccounts(params) {
|
|
|
1718
1635
|
});
|
|
1719
1636
|
});
|
|
1720
1637
|
/// pack output state trees
|
|
1721
|
-
const paddedOutputStateMerkleTrees = stateless_js.padOutputStateMerkleTrees(
|
|
1638
|
+
const paddedOutputStateMerkleTrees = stateless_js.padOutputStateMerkleTrees(
|
|
1639
|
+
outputStateTrees,
|
|
1640
|
+
tokenTransferOutputs.length,
|
|
1641
|
+
inputCompressedTokenAccounts.map(acc => acc.compressedAccount),
|
|
1642
|
+
);
|
|
1722
1643
|
const packedOutputTokenData = [];
|
|
1723
1644
|
paddedOutputStateMerkleTrees.forEach((account, index) => {
|
|
1724
1645
|
var _a;
|
|
1725
|
-
const merkleTreeIndex = stateless_js.getIndexOrAdd(
|
|
1646
|
+
const merkleTreeIndex = stateless_js.getIndexOrAdd(
|
|
1647
|
+
_remainingAccounts,
|
|
1648
|
+
account,
|
|
1649
|
+
);
|
|
1726
1650
|
packedOutputTokenData.push({
|
|
1727
1651
|
owner: tokenTransferOutputs[index].owner,
|
|
1728
1652
|
amount: tokenTransferOutputs[index].amount,
|
|
1729
|
-
lamports: (
|
|
1653
|
+
lamports: (
|
|
1654
|
+
(_a = tokenTransferOutputs[index].lamports) === null ||
|
|
1655
|
+
_a === void 0
|
|
1656
|
+
? void 0
|
|
1657
|
+
: _a.eq(stateless_js.bn(0))
|
|
1658
|
+
)
|
|
1730
1659
|
? null
|
|
1731
1660
|
: tokenTransferOutputs[index].lamports,
|
|
1732
1661
|
merkleTreeIndex,
|
|
@@ -1734,7 +1663,7 @@ function packCompressedTokenAccounts(params) {
|
|
|
1734
1663
|
});
|
|
1735
1664
|
});
|
|
1736
1665
|
// to meta
|
|
1737
|
-
const remainingAccountMetas = _remainingAccounts.map(
|
|
1666
|
+
const remainingAccountMetas = _remainingAccounts.map(account => ({
|
|
1738
1667
|
pubkey: account,
|
|
1739
1668
|
isWritable: true,
|
|
1740
1669
|
isSigner: false,
|
|
@@ -1751,11 +1680,15 @@ const CPI_AUTHORITY_SEED = Buffer.from('cpi_authority');
|
|
|
1751
1680
|
const SPL_TOKEN_MINT_RENT_EXEMPT_BALANCE = 1461600;
|
|
1752
1681
|
|
|
1753
1682
|
/** Address of the SPL Token program */
|
|
1754
|
-
const TOKEN_PROGRAM_ID = new web3_js.PublicKey(
|
|
1683
|
+
const TOKEN_PROGRAM_ID = new web3_js.PublicKey(
|
|
1684
|
+
'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA',
|
|
1685
|
+
);
|
|
1755
1686
|
/** Address of the SPL Token 2022 program */
|
|
1756
1687
|
new web3_js.PublicKey('TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb');
|
|
1757
1688
|
/** Address of the SPL Associated Token Account program */
|
|
1758
|
-
const ASSOCIATED_TOKEN_PROGRAM_ID = new web3_js.PublicKey(
|
|
1689
|
+
const ASSOCIATED_TOKEN_PROGRAM_ID = new web3_js.PublicKey(
|
|
1690
|
+
'ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL',
|
|
1691
|
+
);
|
|
1759
1692
|
/** Address of the special mint for wrapped native SOL in spl-token */
|
|
1760
1693
|
new web3_js.PublicKey('So11111111111111111111111111111111111111112');
|
|
1761
1694
|
/** Address of the special mint for wrapped native SOL in spl-token-2022 */
|
|
@@ -1785,9 +1718,88 @@ var Layout$1 = {};
|
|
|
1785
1718
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
1786
1719
|
* THE SOFTWARE.
|
|
1787
1720
|
*/
|
|
1788
|
-
Object.defineProperty(Layout$1,
|
|
1789
|
-
Layout$1.s16 =
|
|
1790
|
-
Layout$1.
|
|
1721
|
+
Object.defineProperty(Layout$1, '__esModule', { value: true });
|
|
1722
|
+
Layout$1.s16 =
|
|
1723
|
+
Layout$1.s8 =
|
|
1724
|
+
Layout$1.nu64be =
|
|
1725
|
+
Layout$1.u48be =
|
|
1726
|
+
Layout$1.u40be =
|
|
1727
|
+
Layout$1.u32be =
|
|
1728
|
+
Layout$1.u24be =
|
|
1729
|
+
Layout$1.u16be =
|
|
1730
|
+
Layout$1.nu64 =
|
|
1731
|
+
Layout$1.u48 =
|
|
1732
|
+
Layout$1.u40 =
|
|
1733
|
+
u32 =
|
|
1734
|
+
Layout$1.u32 =
|
|
1735
|
+
Layout$1.u24 =
|
|
1736
|
+
Layout$1.u16 =
|
|
1737
|
+
u8 =
|
|
1738
|
+
Layout$1.u8 =
|
|
1739
|
+
Layout$1.offset =
|
|
1740
|
+
Layout$1.greedy =
|
|
1741
|
+
Layout$1.Constant =
|
|
1742
|
+
Layout$1.UTF8 =
|
|
1743
|
+
Layout$1.CString =
|
|
1744
|
+
Layout$1.Blob =
|
|
1745
|
+
Layout$1.Boolean =
|
|
1746
|
+
Layout$1.BitField =
|
|
1747
|
+
Layout$1.BitStructure =
|
|
1748
|
+
Layout$1.VariantLayout =
|
|
1749
|
+
Layout$1.Union =
|
|
1750
|
+
Layout$1.UnionLayoutDiscriminator =
|
|
1751
|
+
Layout$1.UnionDiscriminator =
|
|
1752
|
+
Layout$1.Structure =
|
|
1753
|
+
Layout$1.Sequence =
|
|
1754
|
+
Layout$1.DoubleBE =
|
|
1755
|
+
Layout$1.Double =
|
|
1756
|
+
Layout$1.FloatBE =
|
|
1757
|
+
Layout$1.Float =
|
|
1758
|
+
Layout$1.NearInt64BE =
|
|
1759
|
+
Layout$1.NearInt64 =
|
|
1760
|
+
Layout$1.NearUInt64BE =
|
|
1761
|
+
Layout$1.NearUInt64 =
|
|
1762
|
+
Layout$1.IntBE =
|
|
1763
|
+
Layout$1.Int =
|
|
1764
|
+
Layout$1.UIntBE =
|
|
1765
|
+
Layout$1.UInt =
|
|
1766
|
+
Layout$1.OffsetLayout =
|
|
1767
|
+
Layout$1.GreedyCount =
|
|
1768
|
+
Layout$1.ExternalLayout =
|
|
1769
|
+
Layout$1.bindConstructorLayout =
|
|
1770
|
+
Layout$1.nameWithProperty =
|
|
1771
|
+
Layout_2 =
|
|
1772
|
+
Layout$1.Layout =
|
|
1773
|
+
Layout$1.uint8ArrayToBuffer =
|
|
1774
|
+
Layout$1.checkUint8Array =
|
|
1775
|
+
void 0;
|
|
1776
|
+
Layout$1.constant =
|
|
1777
|
+
Layout$1.utf8 =
|
|
1778
|
+
Layout$1.cstr =
|
|
1779
|
+
blob =
|
|
1780
|
+
Layout$1.blob =
|
|
1781
|
+
Layout$1.unionLayoutDiscriminator =
|
|
1782
|
+
Layout$1.union =
|
|
1783
|
+
Layout$1.seq =
|
|
1784
|
+
Layout$1.bits =
|
|
1785
|
+
struct =
|
|
1786
|
+
Layout$1.struct =
|
|
1787
|
+
Layout$1.f64be =
|
|
1788
|
+
Layout$1.f64 =
|
|
1789
|
+
Layout$1.f32be =
|
|
1790
|
+
Layout$1.f32 =
|
|
1791
|
+
Layout$1.ns64be =
|
|
1792
|
+
Layout$1.s48be =
|
|
1793
|
+
Layout$1.s40be =
|
|
1794
|
+
Layout$1.s32be =
|
|
1795
|
+
Layout$1.s24be =
|
|
1796
|
+
Layout$1.s16be =
|
|
1797
|
+
Layout$1.ns64 =
|
|
1798
|
+
Layout$1.s48 =
|
|
1799
|
+
Layout$1.s40 =
|
|
1800
|
+
Layout$1.s32 =
|
|
1801
|
+
Layout$1.s24 =
|
|
1802
|
+
void 0;
|
|
1791
1803
|
const buffer_1 = require$$0;
|
|
1792
1804
|
/* Check if a value is a Uint8Array.
|
|
1793
1805
|
*
|
|
@@ -1933,7 +1945,7 @@ class Layout {
|
|
|
1933
1945
|
return undefined;
|
|
1934
1946
|
}
|
|
1935
1947
|
}
|
|
1936
|
-
var Layout_2 = Layout$1.Layout = Layout;
|
|
1948
|
+
var Layout_2 = (Layout$1.Layout = Layout);
|
|
1937
1949
|
/* Provide text that carries a name (such as for a function that will
|
|
1938
1950
|
* be throwing an error) annotated with the property of a given layout
|
|
1939
1951
|
* (such as one for which the value was unacceptable).
|
|
@@ -1982,7 +1994,7 @@ function bindConstructorLayout(Class, layout) {
|
|
|
1982
1994
|
if (Object.prototype.hasOwnProperty.call(Class, 'layout_')) {
|
|
1983
1995
|
throw new Error('Class is already bound to a layout');
|
|
1984
1996
|
}
|
|
1985
|
-
if (!(layout &&
|
|
1997
|
+
if (!(layout && layout instanceof Layout)) {
|
|
1986
1998
|
throw new TypeError('layout must be a Layout');
|
|
1987
1999
|
}
|
|
1988
2000
|
if (Object.prototype.hasOwnProperty.call(layout, 'boundConstructor_')) {
|
|
@@ -1990,7 +2002,7 @@ function bindConstructorLayout(Class, layout) {
|
|
|
1990
2002
|
}
|
|
1991
2003
|
Class.layout_ = layout;
|
|
1992
2004
|
layout.boundConstructor_ = Class;
|
|
1993
|
-
layout.makeDestinationObject = (
|
|
2005
|
+
layout.makeDestinationObject = () => new Class();
|
|
1994
2006
|
Object.defineProperty(Class.prototype, 'encode', {
|
|
1995
2007
|
value(b, offset) {
|
|
1996
2008
|
return layout.encode(this, b, offset);
|
|
@@ -2060,7 +2072,7 @@ Layout$1.ExternalLayout = ExternalLayout;
|
|
|
2060
2072
|
*/
|
|
2061
2073
|
class GreedyCount extends ExternalLayout {
|
|
2062
2074
|
constructor(elementSpan = 1, property) {
|
|
2063
|
-
if (
|
|
2075
|
+
if (!Number.isInteger(elementSpan) || 0 >= elementSpan) {
|
|
2064
2076
|
throw new TypeError('elementSpan must be a (positive) integer');
|
|
2065
2077
|
}
|
|
2066
2078
|
super(-1, property);
|
|
@@ -2126,8 +2138,7 @@ class OffsetLayout extends ExternalLayout {
|
|
|
2126
2138
|
}
|
|
2127
2139
|
/** @override */
|
|
2128
2140
|
isCount() {
|
|
2129
|
-
return
|
|
2130
|
-
|| (this.layout instanceof UIntBE));
|
|
2141
|
+
return this.layout instanceof UInt || this.layout instanceof UIntBE;
|
|
2131
2142
|
}
|
|
2132
2143
|
/** @override */
|
|
2133
2144
|
decode(b, offset = 0) {
|
|
@@ -2280,7 +2291,7 @@ const V2E32 = Math.pow(2, 32);
|
|
|
2280
2291
|
* non-negative. */
|
|
2281
2292
|
function divmodInt64(src) {
|
|
2282
2293
|
const hi32 = Math.floor(src / V2E32);
|
|
2283
|
-
const lo32 = src -
|
|
2294
|
+
const lo32 = src - hi32 * V2E32;
|
|
2284
2295
|
return { hi32, lo32 };
|
|
2285
2296
|
}
|
|
2286
2297
|
/* Reconstruct Number from quotient and non-negative remainder */
|
|
@@ -2537,14 +2548,19 @@ class Sequence extends Layout {
|
|
|
2537
2548
|
if (!(elementLayout instanceof Layout)) {
|
|
2538
2549
|
throw new TypeError('elementLayout must be a Layout');
|
|
2539
2550
|
}
|
|
2540
|
-
if (
|
|
2541
|
-
|
|
2542
|
-
|
|
2543
|
-
|
|
2551
|
+
if (
|
|
2552
|
+
!(
|
|
2553
|
+
(count instanceof ExternalLayout && count.isCount()) ||
|
|
2554
|
+
(Number.isInteger(count) && 0 <= count)
|
|
2555
|
+
)
|
|
2556
|
+
) {
|
|
2557
|
+
throw new TypeError(
|
|
2558
|
+
'count must be non-negative integer ' +
|
|
2559
|
+
'or an unsigned integer ExternalLayout',
|
|
2560
|
+
);
|
|
2544
2561
|
}
|
|
2545
2562
|
let span = -1;
|
|
2546
|
-
if (
|
|
2547
|
-
&& (0 < elementLayout.span)) {
|
|
2563
|
+
if (!(count instanceof ExternalLayout) && 0 < elementLayout.span) {
|
|
2548
2564
|
span = count * elementLayout.span;
|
|
2549
2565
|
}
|
|
2550
2566
|
super(span, property);
|
|
@@ -2569,8 +2585,7 @@ class Sequence extends Layout {
|
|
|
2569
2585
|
}
|
|
2570
2586
|
if (0 < this.elementLayout.span) {
|
|
2571
2587
|
span = count * this.elementLayout.span;
|
|
2572
|
-
}
|
|
2573
|
-
else {
|
|
2588
|
+
} else {
|
|
2574
2589
|
let idx = 0;
|
|
2575
2590
|
while (idx < count) {
|
|
2576
2591
|
span += this.elementLayout.getSpan(b, offset + span);
|
|
@@ -2650,27 +2665,30 @@ Layout$1.Sequence = Sequence;
|
|
|
2650
2665
|
*/
|
|
2651
2666
|
class Structure extends Layout {
|
|
2652
2667
|
constructor(fields, property, decodePrefixes) {
|
|
2653
|
-
if (
|
|
2654
|
-
|
|
2668
|
+
if (
|
|
2669
|
+
!(
|
|
2670
|
+
Array.isArray(fields) &&
|
|
2671
|
+
fields.reduce((acc, v) => acc && v instanceof Layout, true)
|
|
2672
|
+
)
|
|
2673
|
+
) {
|
|
2655
2674
|
throw new TypeError('fields must be array of Layout instances');
|
|
2656
2675
|
}
|
|
2657
|
-
if (
|
|
2658
|
-
&& (undefined === decodePrefixes)) {
|
|
2676
|
+
if ('boolean' === typeof property && undefined === decodePrefixes) {
|
|
2659
2677
|
decodePrefixes = property;
|
|
2660
2678
|
property = undefined;
|
|
2661
2679
|
}
|
|
2662
2680
|
/* Verify absence of unnamed variable-length fields. */
|
|
2663
2681
|
for (const fd of fields) {
|
|
2664
|
-
if (
|
|
2665
|
-
|
|
2666
|
-
|
|
2682
|
+
if (0 > fd.span && undefined === fd.property) {
|
|
2683
|
+
throw new Error(
|
|
2684
|
+
'fields cannot contain unnamed variable-length layout',
|
|
2685
|
+
);
|
|
2667
2686
|
}
|
|
2668
2687
|
}
|
|
2669
2688
|
let span = -1;
|
|
2670
2689
|
try {
|
|
2671
2690
|
span = fields.reduce((span, fd) => span + fd.getSpan(), 0);
|
|
2672
|
-
}
|
|
2673
|
-
catch (e) {
|
|
2691
|
+
} catch (e) {
|
|
2674
2692
|
// ignore error
|
|
2675
2693
|
}
|
|
2676
2694
|
super(span, property);
|
|
@@ -2708,8 +2726,7 @@ class Structure extends Layout {
|
|
|
2708
2726
|
offset += fsp;
|
|
2709
2727
|
return span + fsp;
|
|
2710
2728
|
}, 0);
|
|
2711
|
-
}
|
|
2712
|
-
catch (e) {
|
|
2729
|
+
} catch (e) {
|
|
2713
2730
|
throw new RangeError('indeterminate span');
|
|
2714
2731
|
}
|
|
2715
2732
|
return span;
|
|
@@ -2723,8 +2740,7 @@ class Structure extends Layout {
|
|
|
2723
2740
|
dest[fd.property] = fd.decode(b, offset);
|
|
2724
2741
|
}
|
|
2725
2742
|
offset += fd.getSpan(b, offset);
|
|
2726
|
-
if (this.decodePrefixes
|
|
2727
|
-
&& (b.length === offset)) {
|
|
2743
|
+
if (this.decodePrefixes && b.length === offset) {
|
|
2728
2744
|
break;
|
|
2729
2745
|
}
|
|
2730
2746
|
}
|
|
@@ -2741,7 +2757,7 @@ class Structure extends Layout {
|
|
|
2741
2757
|
let lastWrote = 0;
|
|
2742
2758
|
for (const fd of this.fields) {
|
|
2743
2759
|
let span = fd.span;
|
|
2744
|
-
lastWrote =
|
|
2760
|
+
lastWrote = 0 < span ? span : 0;
|
|
2745
2761
|
if (undefined !== fd.property) {
|
|
2746
2762
|
const fv = src[fd.property];
|
|
2747
2763
|
if (undefined !== fv) {
|
|
@@ -2760,14 +2776,13 @@ class Structure extends Layout {
|
|
|
2760
2776
|
* item may have had a dynamic length and we don't want to include
|
|
2761
2777
|
* the padding between it and the end of the space reserved for
|
|
2762
2778
|
* it. */
|
|
2763
|
-
return
|
|
2779
|
+
return lastOffset + lastWrote - firstOffset;
|
|
2764
2780
|
}
|
|
2765
2781
|
/** @override */
|
|
2766
2782
|
fromArray(values) {
|
|
2767
2783
|
const dest = this.makeDestinationObject();
|
|
2768
2784
|
for (const fd of this.fields) {
|
|
2769
|
-
if (
|
|
2770
|
-
&& (0 < values.length)) {
|
|
2785
|
+
if (undefined !== fd.property && 0 < values.length) {
|
|
2771
2786
|
dest[fd.property] = values.shift();
|
|
2772
2787
|
}
|
|
2773
2788
|
}
|
|
@@ -2814,8 +2829,7 @@ class Structure extends Layout {
|
|
|
2814
2829
|
}
|
|
2815
2830
|
if (0 > fd.span) {
|
|
2816
2831
|
offset = -1;
|
|
2817
|
-
}
|
|
2818
|
-
else if (0 <= offset) {
|
|
2832
|
+
} else if (0 <= offset) {
|
|
2819
2833
|
offset += fd.span;
|
|
2820
2834
|
}
|
|
2821
2835
|
}
|
|
@@ -2880,9 +2894,10 @@ Layout$1.UnionDiscriminator = UnionDiscriminator;
|
|
|
2880
2894
|
*/
|
|
2881
2895
|
class UnionLayoutDiscriminator extends UnionDiscriminator {
|
|
2882
2896
|
constructor(layout, property) {
|
|
2883
|
-
if (!(
|
|
2884
|
-
|
|
2885
|
-
|
|
2897
|
+
if (!(layout instanceof ExternalLayout && layout.isCount())) {
|
|
2898
|
+
throw new TypeError(
|
|
2899
|
+
'layout must be an unsigned integer ExternalLayout',
|
|
2900
|
+
);
|
|
2886
2901
|
}
|
|
2887
2902
|
super(property || layout.property || 'variant');
|
|
2888
2903
|
/** The {@link ExternalLayout} used to access the discriminator
|
|
@@ -2961,26 +2976,24 @@ Layout$1.UnionLayoutDiscriminator = UnionLayoutDiscriminator;
|
|
|
2961
2976
|
class Union extends Layout {
|
|
2962
2977
|
constructor(discr, defaultLayout, property) {
|
|
2963
2978
|
let discriminator;
|
|
2964
|
-
if (
|
|
2965
|
-
|
|
2966
|
-
|
|
2967
|
-
|
|
2968
|
-
else if (
|
|
2969
|
-
&& discr.isCount()) {
|
|
2979
|
+
if (discr instanceof UInt || discr instanceof UIntBE) {
|
|
2980
|
+
discriminator = new UnionLayoutDiscriminator(
|
|
2981
|
+
new OffsetLayout(discr),
|
|
2982
|
+
);
|
|
2983
|
+
} else if (discr instanceof ExternalLayout && discr.isCount()) {
|
|
2970
2984
|
discriminator = new UnionLayoutDiscriminator(discr);
|
|
2971
|
-
}
|
|
2972
|
-
|
|
2973
|
-
|
|
2974
|
-
|
|
2975
|
-
|
|
2976
|
-
else {
|
|
2985
|
+
} else if (!(discr instanceof UnionDiscriminator)) {
|
|
2986
|
+
throw new TypeError(
|
|
2987
|
+
'discr must be a UnionDiscriminator ' +
|
|
2988
|
+
'or an unsigned integer layout',
|
|
2989
|
+
);
|
|
2990
|
+
} else {
|
|
2977
2991
|
discriminator = discr;
|
|
2978
2992
|
}
|
|
2979
2993
|
if (undefined === defaultLayout) {
|
|
2980
2994
|
defaultLayout = null;
|
|
2981
2995
|
}
|
|
2982
|
-
if (!(
|
|
2983
|
-
|| (defaultLayout instanceof Layout))) {
|
|
2996
|
+
if (!(null === defaultLayout || defaultLayout instanceof Layout)) {
|
|
2984
2997
|
throw new TypeError('defaultLayout must be null or a Layout');
|
|
2985
2998
|
}
|
|
2986
2999
|
if (null !== defaultLayout) {
|
|
@@ -2998,8 +3011,10 @@ class Union extends Layout {
|
|
|
2998
3011
|
let span = -1;
|
|
2999
3012
|
if (defaultLayout) {
|
|
3000
3013
|
span = defaultLayout.span;
|
|
3001
|
-
if (
|
|
3002
|
-
|
|
3014
|
+
if (
|
|
3015
|
+
0 <= span &&
|
|
3016
|
+
(discr instanceof UInt || discr instanceof UIntBE)
|
|
3017
|
+
) {
|
|
3003
3018
|
span += discriminator.layout.span;
|
|
3004
3019
|
}
|
|
3005
3020
|
}
|
|
@@ -3018,8 +3033,8 @@ class Union extends Layout {
|
|
|
3018
3033
|
*
|
|
3019
3034
|
* If `false` the discriminator is obtained from somewhere
|
|
3020
3035
|
* else. */
|
|
3021
|
-
this.usesPrefixDiscriminator =
|
|
3022
|
-
||
|
|
3036
|
+
this.usesPrefixDiscriminator =
|
|
3037
|
+
discr instanceof UInt || discr instanceof UIntBE;
|
|
3023
3038
|
/** The layout for non-discriminator content when the value of the
|
|
3024
3039
|
* discriminator is not recognized.
|
|
3025
3040
|
*
|
|
@@ -3084,7 +3099,9 @@ class Union extends Layout {
|
|
|
3084
3099
|
* determine the span. */
|
|
3085
3100
|
const vlo = this.getVariant(b, offset);
|
|
3086
3101
|
if (!vlo) {
|
|
3087
|
-
throw new Error(
|
|
3102
|
+
throw new Error(
|
|
3103
|
+
'unable to determine span for unrecognized variant',
|
|
3104
|
+
);
|
|
3088
3105
|
}
|
|
3089
3106
|
return vlo.getSpan(b, offset);
|
|
3090
3107
|
}
|
|
@@ -3118,22 +3135,41 @@ class Union extends Layout {
|
|
|
3118
3135
|
* registered variant.
|
|
3119
3136
|
*/
|
|
3120
3137
|
defaultGetSourceVariant(src) {
|
|
3121
|
-
if (
|
|
3122
|
-
|
|
3123
|
-
|
|
3138
|
+
if (
|
|
3139
|
+
Object.prototype.hasOwnProperty.call(
|
|
3140
|
+
src,
|
|
3141
|
+
this.discriminator.property,
|
|
3142
|
+
)
|
|
3143
|
+
) {
|
|
3144
|
+
if (
|
|
3145
|
+
this.defaultLayout &&
|
|
3146
|
+
this.defaultLayout.property &&
|
|
3147
|
+
Object.prototype.hasOwnProperty.call(
|
|
3148
|
+
src,
|
|
3149
|
+
this.defaultLayout.property,
|
|
3150
|
+
)
|
|
3151
|
+
) {
|
|
3124
3152
|
return undefined;
|
|
3125
3153
|
}
|
|
3126
3154
|
const vlo = this.registry[src[this.discriminator.property]];
|
|
3127
|
-
if (
|
|
3128
|
-
&&
|
|
3129
|
-
|
|
3155
|
+
if (
|
|
3156
|
+
vlo &&
|
|
3157
|
+
(!vlo.layout ||
|
|
3158
|
+
(vlo.property &&
|
|
3159
|
+
Object.prototype.hasOwnProperty.call(
|
|
3160
|
+
src,
|
|
3161
|
+
vlo.property,
|
|
3162
|
+
)))
|
|
3163
|
+
) {
|
|
3130
3164
|
return vlo;
|
|
3131
3165
|
}
|
|
3132
|
-
}
|
|
3133
|
-
else {
|
|
3166
|
+
} else {
|
|
3134
3167
|
for (const tag in this.registry) {
|
|
3135
3168
|
const vlo = this.registry[tag];
|
|
3136
|
-
if (
|
|
3169
|
+
if (
|
|
3170
|
+
vlo.property &&
|
|
3171
|
+
Object.prototype.hasOwnProperty.call(src, vlo.property)
|
|
3172
|
+
) {
|
|
3137
3173
|
return vlo;
|
|
3138
3174
|
}
|
|
3139
3175
|
}
|
|
@@ -3161,9 +3197,11 @@ class Union extends Layout {
|
|
|
3161
3197
|
dest[dlo.property] = discr;
|
|
3162
3198
|
// defaultLayout.property can be undefined, but this is allowed by buffer-layout
|
|
3163
3199
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
3164
|
-
dest[defaultLayout.property] = defaultLayout.decode(
|
|
3165
|
-
|
|
3166
|
-
|
|
3200
|
+
dest[defaultLayout.property] = defaultLayout.decode(
|
|
3201
|
+
b,
|
|
3202
|
+
offset + contentOffset,
|
|
3203
|
+
);
|
|
3204
|
+
} else {
|
|
3167
3205
|
dest = clo.decode(b, offset);
|
|
3168
3206
|
}
|
|
3169
3207
|
return dest;
|
|
@@ -3188,7 +3226,10 @@ class Union extends Layout {
|
|
|
3188
3226
|
dlo.encode(src[dlo.property], b, offset);
|
|
3189
3227
|
// clo.property is not undefined when vlo is undefined
|
|
3190
3228
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
3191
|
-
return
|
|
3229
|
+
return (
|
|
3230
|
+
contentOffset +
|
|
3231
|
+
clo.encode(src[clo.property], b, offset + contentOffset)
|
|
3232
|
+
);
|
|
3192
3233
|
}
|
|
3193
3234
|
return vlo.encode(src, b, offset);
|
|
3194
3235
|
}
|
|
@@ -3228,8 +3269,7 @@ class Union extends Layout {
|
|
|
3228
3269
|
let variant;
|
|
3229
3270
|
if (vb instanceof Uint8Array) {
|
|
3230
3271
|
variant = this.discriminator.decode(vb, offset);
|
|
3231
|
-
}
|
|
3232
|
-
else {
|
|
3272
|
+
} else {
|
|
3233
3273
|
variant = vb;
|
|
3234
3274
|
}
|
|
3235
3275
|
return this.registry[variant];
|
|
@@ -3270,11 +3310,10 @@ class VariantLayout extends Layout {
|
|
|
3270
3310
|
if (!(union instanceof Union)) {
|
|
3271
3311
|
throw new TypeError('union must be a Union');
|
|
3272
3312
|
}
|
|
3273
|
-
if (
|
|
3313
|
+
if (!Number.isInteger(variant) || 0 > variant) {
|
|
3274
3314
|
throw new TypeError('variant must be a (non-negative) integer');
|
|
3275
3315
|
}
|
|
3276
|
-
if (
|
|
3277
|
-
&& (undefined === property)) {
|
|
3316
|
+
if ('string' === typeof layout && undefined === property) {
|
|
3278
3317
|
property = layout;
|
|
3279
3318
|
layout = null;
|
|
3280
3319
|
}
|
|
@@ -3282,10 +3321,14 @@ class VariantLayout extends Layout {
|
|
|
3282
3321
|
if (!(layout instanceof Layout)) {
|
|
3283
3322
|
throw new TypeError('layout must be a Layout');
|
|
3284
3323
|
}
|
|
3285
|
-
if (
|
|
3286
|
-
|
|
3287
|
-
|
|
3288
|
-
|
|
3324
|
+
if (
|
|
3325
|
+
null !== union.defaultLayout &&
|
|
3326
|
+
0 <= layout.span &&
|
|
3327
|
+
layout.span > union.defaultLayout.span
|
|
3328
|
+
) {
|
|
3329
|
+
throw new Error(
|
|
3330
|
+
'variant span exceeds span of containing union',
|
|
3331
|
+
);
|
|
3289
3332
|
}
|
|
3290
3333
|
if ('string' !== typeof property) {
|
|
3291
3334
|
throw new TypeError('variant must have a String property');
|
|
@@ -3294,7 +3337,7 @@ class VariantLayout extends Layout {
|
|
|
3294
3337
|
let span = union.span;
|
|
3295
3338
|
if (0 > union.span) {
|
|
3296
3339
|
span = layout ? layout.span : 0;
|
|
3297
|
-
if (
|
|
3340
|
+
if (0 <= span && union.usesPrefixDiscriminator) {
|
|
3298
3341
|
span += union.discriminator.layout.span;
|
|
3299
3342
|
}
|
|
3300
3343
|
}
|
|
@@ -3341,11 +3384,9 @@ class VariantLayout extends Layout {
|
|
|
3341
3384
|
}
|
|
3342
3385
|
if (this.layout) {
|
|
3343
3386
|
dest[this.property] = this.layout.decode(b, offset + contentOffset);
|
|
3344
|
-
}
|
|
3345
|
-
else if (this.property) {
|
|
3387
|
+
} else if (this.property) {
|
|
3346
3388
|
dest[this.property] = true;
|
|
3347
|
-
}
|
|
3348
|
-
else if (this.union.usesPrefixDiscriminator) {
|
|
3389
|
+
} else if (this.union.usesPrefixDiscriminator) {
|
|
3349
3390
|
dest[this.union.discriminator.property] = this.variant;
|
|
3350
3391
|
}
|
|
3351
3392
|
return dest;
|
|
@@ -3356,8 +3397,10 @@ class VariantLayout extends Layout {
|
|
|
3356
3397
|
if (this.union.usesPrefixDiscriminator) {
|
|
3357
3398
|
contentOffset = this.union.discriminator.layout.span;
|
|
3358
3399
|
}
|
|
3359
|
-
if (
|
|
3360
|
-
|
|
3400
|
+
if (
|
|
3401
|
+
this.layout &&
|
|
3402
|
+
!Object.prototype.hasOwnProperty.call(src, this.property)
|
|
3403
|
+
) {
|
|
3361
3404
|
throw new TypeError('variant lacks property ' + this.property);
|
|
3362
3405
|
}
|
|
3363
3406
|
this.union.discriminator.encode(this.variant, b, offset);
|
|
@@ -3365,8 +3408,7 @@ class VariantLayout extends Layout {
|
|
|
3365
3408
|
if (this.layout) {
|
|
3366
3409
|
this.layout.encode(src[this.property], b, offset + contentOffset);
|
|
3367
3410
|
span += this.layout.getSpan(b, offset + contentOffset);
|
|
3368
|
-
if (
|
|
3369
|
-
&& (span > this.union.span)) {
|
|
3411
|
+
if (0 <= this.union.span && span > this.union.span) {
|
|
3370
3412
|
throw new Error('encoded variant overruns containing union');
|
|
3371
3413
|
}
|
|
3372
3414
|
}
|
|
@@ -3427,12 +3469,10 @@ function fixBitwiseResult(v) {
|
|
|
3427
3469
|
*/
|
|
3428
3470
|
class BitStructure extends Layout {
|
|
3429
3471
|
constructor(word, msb, property) {
|
|
3430
|
-
if (!(
|
|
3431
|
-
|| (word instanceof UIntBE))) {
|
|
3472
|
+
if (!(word instanceof UInt || word instanceof UIntBE)) {
|
|
3432
3473
|
throw new TypeError('word must be a UInt or UIntBE layout');
|
|
3433
3474
|
}
|
|
3434
|
-
if (
|
|
3435
|
-
&& (undefined === property)) {
|
|
3475
|
+
if ('string' === typeof msb && undefined === property) {
|
|
3436
3476
|
property = msb;
|
|
3437
3477
|
msb = false;
|
|
3438
3478
|
}
|
|
@@ -3576,15 +3616,19 @@ class BitField {
|
|
|
3576
3616
|
if (!(container instanceof BitStructure)) {
|
|
3577
3617
|
throw new TypeError('container must be a BitStructure');
|
|
3578
3618
|
}
|
|
3579
|
-
if (
|
|
3619
|
+
if (!Number.isInteger(bits) || 0 >= bits) {
|
|
3580
3620
|
throw new TypeError('bits must be positive integer');
|
|
3581
3621
|
}
|
|
3582
3622
|
const totalBits = 8 * container.span;
|
|
3583
3623
|
const usedBits = container.fields.reduce((sum, fd) => sum + fd.bits, 0);
|
|
3584
|
-
if (
|
|
3585
|
-
throw new Error(
|
|
3586
|
-
|
|
3587
|
-
|
|
3624
|
+
if (bits + usedBits > totalBits) {
|
|
3625
|
+
throw new Error(
|
|
3626
|
+
'bits too long for span remainder (' +
|
|
3627
|
+
(totalBits - usedBits) +
|
|
3628
|
+
' of ' +
|
|
3629
|
+
totalBits +
|
|
3630
|
+
' remain)',
|
|
3631
|
+
);
|
|
3588
3632
|
}
|
|
3589
3633
|
/** The {@link BitStructure} instance to which this bit field
|
|
3590
3634
|
* belongs. */
|
|
@@ -3597,8 +3641,9 @@ class BitField {
|
|
|
3597
3641
|
* That is, it masks a value that has not yet been shifted into
|
|
3598
3642
|
* position within its containing packed integer. */
|
|
3599
3643
|
this.valueMask = (1 << bits) - 1;
|
|
3600
|
-
if (32 === bits) {
|
|
3601
|
-
|
|
3644
|
+
if (32 === bits) {
|
|
3645
|
+
// shifted value out of range
|
|
3646
|
+
this.valueMask = 0xffffffff;
|
|
3602
3647
|
}
|
|
3603
3648
|
/** The offset of the value within the containing packed unsigned
|
|
3604
3649
|
* integer. The least significant bit of the packed value is at
|
|
@@ -3636,16 +3681,22 @@ class BitField {
|
|
|
3636
3681
|
* **NOTE** This is not a specialization of {@link
|
|
3637
3682
|
* Layout#encode|Layout.encode} and there is no return value. */
|
|
3638
3683
|
encode(value) {
|
|
3639
|
-
if (
|
|
3640
|
-
|
|
3641
|
-
|
|
3642
|
-
|
|
3643
|
-
|
|
3684
|
+
if (
|
|
3685
|
+
'number' !== typeof value ||
|
|
3686
|
+
!Number.isInteger(value) ||
|
|
3687
|
+
value !== fixBitwiseResult(value & this.valueMask)
|
|
3688
|
+
) {
|
|
3689
|
+
throw new TypeError(
|
|
3690
|
+
nameWithProperty('BitField.encode', this) +
|
|
3691
|
+
' value must be integer not exceeding ' +
|
|
3692
|
+
this.valueMask,
|
|
3693
|
+
);
|
|
3644
3694
|
}
|
|
3645
3695
|
const word = this.container._packedGetValue();
|
|
3646
3696
|
const wordValue = fixBitwiseResult(value << this.start);
|
|
3647
|
-
this.container._packedSetValue(
|
|
3648
|
-
| wordValue
|
|
3697
|
+
this.container._packedSetValue(
|
|
3698
|
+
fixBitwiseResult(word & ~this.wordMask) | wordValue,
|
|
3699
|
+
);
|
|
3649
3700
|
}
|
|
3650
3701
|
}
|
|
3651
3702
|
Layout$1.BitField = BitField;
|
|
@@ -3703,10 +3754,16 @@ Layout$1.Boolean = Boolean;
|
|
|
3703
3754
|
*/
|
|
3704
3755
|
class Blob extends Layout {
|
|
3705
3756
|
constructor(length, property) {
|
|
3706
|
-
if (
|
|
3707
|
-
|
|
3708
|
-
|
|
3709
|
-
|
|
3757
|
+
if (
|
|
3758
|
+
!(
|
|
3759
|
+
(length instanceof ExternalLayout && length.isCount()) ||
|
|
3760
|
+
(Number.isInteger(length) && 0 <= length)
|
|
3761
|
+
)
|
|
3762
|
+
) {
|
|
3763
|
+
throw new TypeError(
|
|
3764
|
+
'length must be positive integer ' +
|
|
3765
|
+
'or an unsigned integer ExternalLayout',
|
|
3766
|
+
);
|
|
3710
3767
|
}
|
|
3711
3768
|
let span = -1;
|
|
3712
3769
|
if (!(length instanceof ExternalLayout)) {
|
|
@@ -3747,14 +3804,23 @@ class Blob extends Layout {
|
|
|
3747
3804
|
span = src.length;
|
|
3748
3805
|
}
|
|
3749
3806
|
if (!(src instanceof Uint8Array && span === src.length)) {
|
|
3750
|
-
throw new TypeError(
|
|
3751
|
-
|
|
3807
|
+
throw new TypeError(
|
|
3808
|
+
nameWithProperty('Blob.encode', this) +
|
|
3809
|
+
' requires (length ' +
|
|
3810
|
+
span +
|
|
3811
|
+
') Uint8Array as src',
|
|
3812
|
+
);
|
|
3752
3813
|
}
|
|
3753
|
-
if (
|
|
3814
|
+
if (offset + span > b.length) {
|
|
3754
3815
|
throw new RangeError('encoding overruns Uint8Array');
|
|
3755
3816
|
}
|
|
3756
3817
|
const srcBuffer = uint8ArrayToBuffer(src);
|
|
3757
|
-
uint8ArrayToBuffer(b).write(
|
|
3818
|
+
uint8ArrayToBuffer(b).write(
|
|
3819
|
+
srcBuffer.toString('hex'),
|
|
3820
|
+
offset,
|
|
3821
|
+
span,
|
|
3822
|
+
'hex',
|
|
3823
|
+
);
|
|
3758
3824
|
if (this.length instanceof ExternalLayout) {
|
|
3759
3825
|
this.length.encode(span, b, offset);
|
|
3760
3826
|
}
|
|
@@ -3783,7 +3849,7 @@ class CString extends Layout {
|
|
|
3783
3849
|
getSpan(b, offset = 0) {
|
|
3784
3850
|
checkUint8Array(b);
|
|
3785
3851
|
let idx = offset;
|
|
3786
|
-
while (
|
|
3852
|
+
while (idx < b.length && 0 !== b[idx]) {
|
|
3787
3853
|
idx += 1;
|
|
3788
3854
|
}
|
|
3789
3855
|
return 1 + idx - offset;
|
|
@@ -3791,7 +3857,9 @@ class CString extends Layout {
|
|
|
3791
3857
|
/** @override */
|
|
3792
3858
|
decode(b, offset = 0) {
|
|
3793
3859
|
const span = this.getSpan(b, offset);
|
|
3794
|
-
return uint8ArrayToBuffer(b)
|
|
3860
|
+
return uint8ArrayToBuffer(b)
|
|
3861
|
+
.slice(offset, offset + span - 1)
|
|
3862
|
+
.toString('utf-8');
|
|
3795
3863
|
}
|
|
3796
3864
|
/** @override */
|
|
3797
3865
|
encode(src, b, offset = 0) {
|
|
@@ -3803,7 +3871,7 @@ class CString extends Layout {
|
|
|
3803
3871
|
}
|
|
3804
3872
|
const srcb = buffer_1.Buffer.from(src, 'utf8');
|
|
3805
3873
|
const span = srcb.length;
|
|
3806
|
-
if (
|
|
3874
|
+
if (offset + span > b.length) {
|
|
3807
3875
|
throw new RangeError('encoding overruns Buffer');
|
|
3808
3876
|
}
|
|
3809
3877
|
const buffer = uint8ArrayToBuffer(b);
|
|
@@ -3834,14 +3902,13 @@ Layout$1.CString = CString;
|
|
|
3834
3902
|
*/
|
|
3835
3903
|
class UTF8 extends Layout {
|
|
3836
3904
|
constructor(maxSpan, property) {
|
|
3837
|
-
if (
|
|
3905
|
+
if ('string' === typeof maxSpan && undefined === property) {
|
|
3838
3906
|
property = maxSpan;
|
|
3839
3907
|
maxSpan = undefined;
|
|
3840
3908
|
}
|
|
3841
3909
|
if (undefined === maxSpan) {
|
|
3842
3910
|
maxSpan = -1;
|
|
3843
|
-
}
|
|
3844
|
-
else if (!Number.isInteger(maxSpan)) {
|
|
3911
|
+
} else if (!Number.isInteger(maxSpan)) {
|
|
3845
3912
|
throw new TypeError('maxSpan must be an integer');
|
|
3846
3913
|
}
|
|
3847
3914
|
super(-1, property);
|
|
@@ -3863,11 +3930,12 @@ class UTF8 extends Layout {
|
|
|
3863
3930
|
/** @override */
|
|
3864
3931
|
decode(b, offset = 0) {
|
|
3865
3932
|
const span = this.getSpan(b, offset);
|
|
3866
|
-
if (
|
|
3867
|
-
&& (this.maxSpan < span)) {
|
|
3933
|
+
if (0 <= this.maxSpan && this.maxSpan < span) {
|
|
3868
3934
|
throw new RangeError('text length exceeds maxSpan');
|
|
3869
3935
|
}
|
|
3870
|
-
return uint8ArrayToBuffer(b)
|
|
3936
|
+
return uint8ArrayToBuffer(b)
|
|
3937
|
+
.slice(offset, offset + span)
|
|
3938
|
+
.toString('utf-8');
|
|
3871
3939
|
}
|
|
3872
3940
|
/** @override */
|
|
3873
3941
|
encode(src, b, offset = 0) {
|
|
@@ -3879,11 +3947,10 @@ class UTF8 extends Layout {
|
|
|
3879
3947
|
}
|
|
3880
3948
|
const srcb = buffer_1.Buffer.from(src, 'utf8');
|
|
3881
3949
|
const span = srcb.length;
|
|
3882
|
-
if (
|
|
3883
|
-
&& (this.maxSpan < span)) {
|
|
3950
|
+
if (0 <= this.maxSpan && this.maxSpan < span) {
|
|
3884
3951
|
throw new RangeError('text length exceeds maxSpan');
|
|
3885
3952
|
}
|
|
3886
|
-
if (
|
|
3953
|
+
if (offset + span > b.length) {
|
|
3887
3954
|
throw new RangeError('encoding overruns Buffer');
|
|
3888
3955
|
}
|
|
3889
3956
|
srcb.copy(uint8ArrayToBuffer(b), offset);
|
|
@@ -3936,115 +4003,121 @@ class Constant extends Layout {
|
|
|
3936
4003
|
}
|
|
3937
4004
|
Layout$1.Constant = Constant;
|
|
3938
4005
|
/** Factory for {@link GreedyCount}. */
|
|
3939
|
-
Layout$1.greedy = (
|
|
4006
|
+
Layout$1.greedy = (elementSpan, property) =>
|
|
4007
|
+
new GreedyCount(elementSpan, property);
|
|
3940
4008
|
/** Factory for {@link OffsetLayout}. */
|
|
3941
|
-
Layout$1.offset = (
|
|
4009
|
+
Layout$1.offset = (layout, offset, property) =>
|
|
4010
|
+
new OffsetLayout(layout, offset, property);
|
|
3942
4011
|
/** Factory for {@link UInt|unsigned int layouts} spanning one
|
|
3943
4012
|
* byte. */
|
|
3944
|
-
var u8 = Layout$1.u8 =
|
|
4013
|
+
var u8 = (Layout$1.u8 = property => new UInt(1, property));
|
|
3945
4014
|
/** Factory for {@link UInt|little-endian unsigned int layouts}
|
|
3946
4015
|
* spanning two bytes. */
|
|
3947
|
-
Layout$1.u16 =
|
|
4016
|
+
Layout$1.u16 = property => new UInt(2, property);
|
|
3948
4017
|
/** Factory for {@link UInt|little-endian unsigned int layouts}
|
|
3949
4018
|
* spanning three bytes. */
|
|
3950
|
-
Layout$1.u24 =
|
|
4019
|
+
Layout$1.u24 = property => new UInt(3, property);
|
|
3951
4020
|
/** Factory for {@link UInt|little-endian unsigned int layouts}
|
|
3952
4021
|
* spanning four bytes. */
|
|
3953
|
-
var u32 = Layout$1.u32 =
|
|
4022
|
+
var u32 = (Layout$1.u32 = property => new UInt(4, property));
|
|
3954
4023
|
/** Factory for {@link UInt|little-endian unsigned int layouts}
|
|
3955
4024
|
* spanning five bytes. */
|
|
3956
|
-
Layout$1.u40 =
|
|
4025
|
+
Layout$1.u40 = property => new UInt(5, property);
|
|
3957
4026
|
/** Factory for {@link UInt|little-endian unsigned int layouts}
|
|
3958
4027
|
* spanning six bytes. */
|
|
3959
|
-
Layout$1.u48 =
|
|
4028
|
+
Layout$1.u48 = property => new UInt(6, property);
|
|
3960
4029
|
/** Factory for {@link NearUInt64|little-endian unsigned int
|
|
3961
4030
|
* layouts} interpreted as Numbers. */
|
|
3962
|
-
Layout$1.nu64 =
|
|
4031
|
+
Layout$1.nu64 = property => new NearUInt64(property);
|
|
3963
4032
|
/** Factory for {@link UInt|big-endian unsigned int layouts}
|
|
3964
4033
|
* spanning two bytes. */
|
|
3965
|
-
Layout$1.u16be =
|
|
4034
|
+
Layout$1.u16be = property => new UIntBE(2, property);
|
|
3966
4035
|
/** Factory for {@link UInt|big-endian unsigned int layouts}
|
|
3967
4036
|
* spanning three bytes. */
|
|
3968
|
-
Layout$1.u24be =
|
|
4037
|
+
Layout$1.u24be = property => new UIntBE(3, property);
|
|
3969
4038
|
/** Factory for {@link UInt|big-endian unsigned int layouts}
|
|
3970
4039
|
* spanning four bytes. */
|
|
3971
|
-
Layout$1.u32be =
|
|
4040
|
+
Layout$1.u32be = property => new UIntBE(4, property);
|
|
3972
4041
|
/** Factory for {@link UInt|big-endian unsigned int layouts}
|
|
3973
4042
|
* spanning five bytes. */
|
|
3974
|
-
Layout$1.u40be =
|
|
4043
|
+
Layout$1.u40be = property => new UIntBE(5, property);
|
|
3975
4044
|
/** Factory for {@link UInt|big-endian unsigned int layouts}
|
|
3976
4045
|
* spanning six bytes. */
|
|
3977
|
-
Layout$1.u48be =
|
|
4046
|
+
Layout$1.u48be = property => new UIntBE(6, property);
|
|
3978
4047
|
/** Factory for {@link NearUInt64BE|big-endian unsigned int
|
|
3979
4048
|
* layouts} interpreted as Numbers. */
|
|
3980
|
-
Layout$1.nu64be =
|
|
4049
|
+
Layout$1.nu64be = property => new NearUInt64BE(property);
|
|
3981
4050
|
/** Factory for {@link Int|signed int layouts} spanning one
|
|
3982
4051
|
* byte. */
|
|
3983
|
-
Layout$1.s8 =
|
|
4052
|
+
Layout$1.s8 = property => new Int(1, property);
|
|
3984
4053
|
/** Factory for {@link Int|little-endian signed int layouts}
|
|
3985
4054
|
* spanning two bytes. */
|
|
3986
|
-
Layout$1.s16 =
|
|
4055
|
+
Layout$1.s16 = property => new Int(2, property);
|
|
3987
4056
|
/** Factory for {@link Int|little-endian signed int layouts}
|
|
3988
4057
|
* spanning three bytes. */
|
|
3989
|
-
Layout$1.s24 =
|
|
4058
|
+
Layout$1.s24 = property => new Int(3, property);
|
|
3990
4059
|
/** Factory for {@link Int|little-endian signed int layouts}
|
|
3991
4060
|
* spanning four bytes. */
|
|
3992
|
-
Layout$1.s32 =
|
|
4061
|
+
Layout$1.s32 = property => new Int(4, property);
|
|
3993
4062
|
/** Factory for {@link Int|little-endian signed int layouts}
|
|
3994
4063
|
* spanning five bytes. */
|
|
3995
|
-
Layout$1.s40 =
|
|
4064
|
+
Layout$1.s40 = property => new Int(5, property);
|
|
3996
4065
|
/** Factory for {@link Int|little-endian signed int layouts}
|
|
3997
4066
|
* spanning six bytes. */
|
|
3998
|
-
Layout$1.s48 =
|
|
4067
|
+
Layout$1.s48 = property => new Int(6, property);
|
|
3999
4068
|
/** Factory for {@link NearInt64|little-endian signed int layouts}
|
|
4000
4069
|
* interpreted as Numbers. */
|
|
4001
|
-
Layout$1.ns64 =
|
|
4070
|
+
Layout$1.ns64 = property => new NearInt64(property);
|
|
4002
4071
|
/** Factory for {@link Int|big-endian signed int layouts}
|
|
4003
4072
|
* spanning two bytes. */
|
|
4004
|
-
Layout$1.s16be =
|
|
4073
|
+
Layout$1.s16be = property => new IntBE(2, property);
|
|
4005
4074
|
/** Factory for {@link Int|big-endian signed int layouts}
|
|
4006
4075
|
* spanning three bytes. */
|
|
4007
|
-
Layout$1.s24be =
|
|
4076
|
+
Layout$1.s24be = property => new IntBE(3, property);
|
|
4008
4077
|
/** Factory for {@link Int|big-endian signed int layouts}
|
|
4009
4078
|
* spanning four bytes. */
|
|
4010
|
-
Layout$1.s32be =
|
|
4079
|
+
Layout$1.s32be = property => new IntBE(4, property);
|
|
4011
4080
|
/** Factory for {@link Int|big-endian signed int layouts}
|
|
4012
4081
|
* spanning five bytes. */
|
|
4013
|
-
Layout$1.s40be =
|
|
4082
|
+
Layout$1.s40be = property => new IntBE(5, property);
|
|
4014
4083
|
/** Factory for {@link Int|big-endian signed int layouts}
|
|
4015
4084
|
* spanning six bytes. */
|
|
4016
|
-
Layout$1.s48be =
|
|
4085
|
+
Layout$1.s48be = property => new IntBE(6, property);
|
|
4017
4086
|
/** Factory for {@link NearInt64BE|big-endian signed int layouts}
|
|
4018
4087
|
* interpreted as Numbers. */
|
|
4019
|
-
Layout$1.ns64be =
|
|
4088
|
+
Layout$1.ns64be = property => new NearInt64BE(property);
|
|
4020
4089
|
/** Factory for {@link Float|little-endian 32-bit floating point} values. */
|
|
4021
|
-
Layout$1.f32 =
|
|
4090
|
+
Layout$1.f32 = property => new Float(property);
|
|
4022
4091
|
/** Factory for {@link FloatBE|big-endian 32-bit floating point} values. */
|
|
4023
|
-
Layout$1.f32be =
|
|
4092
|
+
Layout$1.f32be = property => new FloatBE(property);
|
|
4024
4093
|
/** Factory for {@link Double|little-endian 64-bit floating point} values. */
|
|
4025
|
-
Layout$1.f64 =
|
|
4094
|
+
Layout$1.f64 = property => new Double(property);
|
|
4026
4095
|
/** Factory for {@link DoubleBE|big-endian 64-bit floating point} values. */
|
|
4027
|
-
Layout$1.f64be =
|
|
4096
|
+
Layout$1.f64be = property => new DoubleBE(property);
|
|
4028
4097
|
/** Factory for {@link Structure} values. */
|
|
4029
|
-
var struct = Layout$1.struct = (
|
|
4098
|
+
var struct = (Layout$1.struct = (fields, property, decodePrefixes) =>
|
|
4099
|
+
new Structure(fields, property, decodePrefixes));
|
|
4030
4100
|
/** Factory for {@link BitStructure} values. */
|
|
4031
|
-
Layout$1.bits = (
|
|
4101
|
+
Layout$1.bits = (word, msb, property) => new BitStructure(word, msb, property);
|
|
4032
4102
|
/** Factory for {@link Sequence} values. */
|
|
4033
|
-
Layout$1.seq = (
|
|
4103
|
+
Layout$1.seq = (elementLayout, count, property) =>
|
|
4104
|
+
new Sequence(elementLayout, count, property);
|
|
4034
4105
|
/** Factory for {@link Union} values. */
|
|
4035
|
-
Layout$1.union = (
|
|
4106
|
+
Layout$1.union = (discr, defaultLayout, property) =>
|
|
4107
|
+
new Union(discr, defaultLayout, property);
|
|
4036
4108
|
/** Factory for {@link UnionLayoutDiscriminator} values. */
|
|
4037
|
-
Layout$1.unionLayoutDiscriminator = (
|
|
4109
|
+
Layout$1.unionLayoutDiscriminator = (layout, property) =>
|
|
4110
|
+
new UnionLayoutDiscriminator(layout, property);
|
|
4038
4111
|
/** Factory for {@link Blob} values. */
|
|
4039
|
-
var blob = Layout$1.blob = (
|
|
4112
|
+
var blob = (Layout$1.blob = (length, property) => new Blob(length, property));
|
|
4040
4113
|
/** Factory for {@link CString} values. */
|
|
4041
|
-
Layout$1.cstr =
|
|
4114
|
+
Layout$1.cstr = property => new CString(property);
|
|
4042
4115
|
/** Factory for {@link UTF8} values. */
|
|
4043
|
-
Layout$1.utf8 = (
|
|
4116
|
+
Layout$1.utf8 = (maxSpan, property) => new UTF8(maxSpan, property);
|
|
4044
4117
|
/** Factory for {@link Constant} values. */
|
|
4045
|
-
Layout$1.constant = (
|
|
4118
|
+
Layout$1.constant = (value, property) => new Constant(value, property);
|
|
4046
4119
|
|
|
4047
|
-
const encodeDecode =
|
|
4120
|
+
const encodeDecode = layout => {
|
|
4048
4121
|
const decode = layout.decode.bind(layout);
|
|
4049
4122
|
const encode = layout.encode.bind(layout);
|
|
4050
4123
|
return { decode, encode };
|
|
@@ -4052,7 +4125,7 @@ const encodeDecode = (layout) => {
|
|
|
4052
4125
|
|
|
4053
4126
|
var browser = {};
|
|
4054
4127
|
|
|
4055
|
-
Object.defineProperty(browser,
|
|
4128
|
+
Object.defineProperty(browser, '__esModule', { value: true });
|
|
4056
4129
|
/**
|
|
4057
4130
|
* Convert a little-endian buffer into a BigInt.
|
|
4058
4131
|
* @param buf The little-endian buffer to convert
|
|
@@ -4069,7 +4142,7 @@ function toBigIntLE(buf) {
|
|
|
4069
4142
|
return BigInt(`0x${hex}`);
|
|
4070
4143
|
}
|
|
4071
4144
|
}
|
|
4072
|
-
var toBigIntLE_1 = browser.toBigIntLE = toBigIntLE;
|
|
4145
|
+
var toBigIntLE_1 = (browser.toBigIntLE = toBigIntLE);
|
|
4073
4146
|
/**
|
|
4074
4147
|
* Convert a big-endian buffer into a BigInt
|
|
4075
4148
|
* @param buf The big-endian buffer to convert.
|
|
@@ -4094,12 +4167,15 @@ browser.toBigIntBE = toBigIntBE;
|
|
|
4094
4167
|
function toBufferLE(num, width) {
|
|
4095
4168
|
{
|
|
4096
4169
|
const hex = num.toString(16);
|
|
4097
|
-
const buffer = Buffer.from(
|
|
4170
|
+
const buffer = Buffer.from(
|
|
4171
|
+
hex.padStart(width * 2, '0').slice(0, width * 2),
|
|
4172
|
+
'hex',
|
|
4173
|
+
);
|
|
4098
4174
|
buffer.reverse();
|
|
4099
4175
|
return buffer;
|
|
4100
4176
|
}
|
|
4101
4177
|
}
|
|
4102
|
-
var toBufferLE_1 = browser.toBufferLE = toBufferLE;
|
|
4178
|
+
var toBufferLE_1 = (browser.toBufferLE = toBufferLE);
|
|
4103
4179
|
/**
|
|
4104
4180
|
* Convert a BigInt to a big-endian buffer.
|
|
4105
4181
|
* @param num The BigInt to convert.
|
|
@@ -4109,12 +4185,15 @@ var toBufferLE_1 = browser.toBufferLE = toBufferLE;
|
|
|
4109
4185
|
function toBufferBE(num, width) {
|
|
4110
4186
|
{
|
|
4111
4187
|
const hex = num.toString(16);
|
|
4112
|
-
return Buffer.from(
|
|
4188
|
+
return Buffer.from(
|
|
4189
|
+
hex.padStart(width * 2, '0').slice(0, width * 2),
|
|
4190
|
+
'hex',
|
|
4191
|
+
);
|
|
4113
4192
|
}
|
|
4114
4193
|
}
|
|
4115
4194
|
browser.toBufferBE = toBufferBE;
|
|
4116
4195
|
|
|
4117
|
-
const bigInt =
|
|
4196
|
+
const bigInt = length => property => {
|
|
4118
4197
|
const layout = blob(length, property);
|
|
4119
4198
|
const { encode, decode } = encodeDecode(layout);
|
|
4120
4199
|
const bigIntLayout = layout;
|
|
@@ -4130,7 +4209,7 @@ const bigInt = (length) => (property) => {
|
|
|
4130
4209
|
};
|
|
4131
4210
|
const u64 = bigInt(8);
|
|
4132
4211
|
|
|
4133
|
-
const bool =
|
|
4212
|
+
const bool = property => {
|
|
4134
4213
|
const layout = u8(property);
|
|
4135
4214
|
const { encode, decode } = encodeDecode(layout);
|
|
4136
4215
|
const boolLayout = layout;
|
|
@@ -4145,7 +4224,7 @@ const bool = (property) => {
|
|
|
4145
4224
|
return boolLayout;
|
|
4146
4225
|
};
|
|
4147
4226
|
|
|
4148
|
-
const publicKey =
|
|
4227
|
+
const publicKey = property => {
|
|
4149
4228
|
const layout = blob(32, property);
|
|
4150
4229
|
const { encode, decode } = encodeDecode(layout);
|
|
4151
4230
|
const publicKeyLayout = layout;
|
|
@@ -4219,73 +4298,112 @@ class TokenOwnerOffCurveError extends TokenError {
|
|
|
4219
4298
|
/** Instructions defined by the program */
|
|
4220
4299
|
var TokenInstruction;
|
|
4221
4300
|
(function (TokenInstruction) {
|
|
4222
|
-
TokenInstruction[TokenInstruction[
|
|
4223
|
-
|
|
4224
|
-
TokenInstruction[TokenInstruction[
|
|
4225
|
-
|
|
4226
|
-
TokenInstruction[TokenInstruction[
|
|
4227
|
-
|
|
4228
|
-
TokenInstruction[TokenInstruction[
|
|
4229
|
-
TokenInstruction[TokenInstruction[
|
|
4230
|
-
TokenInstruction[TokenInstruction[
|
|
4231
|
-
TokenInstruction[TokenInstruction[
|
|
4232
|
-
TokenInstruction[TokenInstruction[
|
|
4233
|
-
TokenInstruction[TokenInstruction[
|
|
4234
|
-
TokenInstruction[TokenInstruction[
|
|
4235
|
-
TokenInstruction[TokenInstruction[
|
|
4236
|
-
|
|
4237
|
-
TokenInstruction[TokenInstruction[
|
|
4238
|
-
TokenInstruction[TokenInstruction[
|
|
4239
|
-
|
|
4240
|
-
TokenInstruction[TokenInstruction[
|
|
4241
|
-
|
|
4242
|
-
TokenInstruction[TokenInstruction[
|
|
4243
|
-
|
|
4244
|
-
TokenInstruction[TokenInstruction[
|
|
4245
|
-
TokenInstruction[TokenInstruction[
|
|
4246
|
-
|
|
4247
|
-
TokenInstruction[TokenInstruction[
|
|
4248
|
-
TokenInstruction[TokenInstruction[
|
|
4249
|
-
|
|
4250
|
-
TokenInstruction[TokenInstruction[
|
|
4251
|
-
|
|
4252
|
-
TokenInstruction[TokenInstruction[
|
|
4253
|
-
|
|
4254
|
-
TokenInstruction[TokenInstruction[
|
|
4255
|
-
|
|
4256
|
-
TokenInstruction[TokenInstruction[
|
|
4257
|
-
|
|
4258
|
-
TokenInstruction[TokenInstruction[
|
|
4301
|
+
TokenInstruction[(TokenInstruction['InitializeMint'] = 0)] =
|
|
4302
|
+
'InitializeMint';
|
|
4303
|
+
TokenInstruction[(TokenInstruction['InitializeAccount'] = 1)] =
|
|
4304
|
+
'InitializeAccount';
|
|
4305
|
+
TokenInstruction[(TokenInstruction['InitializeMultisig'] = 2)] =
|
|
4306
|
+
'InitializeMultisig';
|
|
4307
|
+
TokenInstruction[(TokenInstruction['Transfer'] = 3)] = 'Transfer';
|
|
4308
|
+
TokenInstruction[(TokenInstruction['Approve'] = 4)] = 'Approve';
|
|
4309
|
+
TokenInstruction[(TokenInstruction['Revoke'] = 5)] = 'Revoke';
|
|
4310
|
+
TokenInstruction[(TokenInstruction['SetAuthority'] = 6)] = 'SetAuthority';
|
|
4311
|
+
TokenInstruction[(TokenInstruction['MintTo'] = 7)] = 'MintTo';
|
|
4312
|
+
TokenInstruction[(TokenInstruction['Burn'] = 8)] = 'Burn';
|
|
4313
|
+
TokenInstruction[(TokenInstruction['CloseAccount'] = 9)] = 'CloseAccount';
|
|
4314
|
+
TokenInstruction[(TokenInstruction['FreezeAccount'] = 10)] =
|
|
4315
|
+
'FreezeAccount';
|
|
4316
|
+
TokenInstruction[(TokenInstruction['ThawAccount'] = 11)] = 'ThawAccount';
|
|
4317
|
+
TokenInstruction[(TokenInstruction['TransferChecked'] = 12)] =
|
|
4318
|
+
'TransferChecked';
|
|
4319
|
+
TokenInstruction[(TokenInstruction['ApproveChecked'] = 13)] =
|
|
4320
|
+
'ApproveChecked';
|
|
4321
|
+
TokenInstruction[(TokenInstruction['MintToChecked'] = 14)] =
|
|
4322
|
+
'MintToChecked';
|
|
4323
|
+
TokenInstruction[(TokenInstruction['BurnChecked'] = 15)] = 'BurnChecked';
|
|
4324
|
+
TokenInstruction[(TokenInstruction['InitializeAccount2'] = 16)] =
|
|
4325
|
+
'InitializeAccount2';
|
|
4326
|
+
TokenInstruction[(TokenInstruction['SyncNative'] = 17)] = 'SyncNative';
|
|
4327
|
+
TokenInstruction[(TokenInstruction['InitializeAccount3'] = 18)] =
|
|
4328
|
+
'InitializeAccount3';
|
|
4329
|
+
TokenInstruction[(TokenInstruction['InitializeMultisig2'] = 19)] =
|
|
4330
|
+
'InitializeMultisig2';
|
|
4331
|
+
TokenInstruction[(TokenInstruction['InitializeMint2'] = 20)] =
|
|
4332
|
+
'InitializeMint2';
|
|
4333
|
+
TokenInstruction[(TokenInstruction['GetAccountDataSize'] = 21)] =
|
|
4334
|
+
'GetAccountDataSize';
|
|
4335
|
+
TokenInstruction[(TokenInstruction['InitializeImmutableOwner'] = 22)] =
|
|
4336
|
+
'InitializeImmutableOwner';
|
|
4337
|
+
TokenInstruction[(TokenInstruction['AmountToUiAmount'] = 23)] =
|
|
4338
|
+
'AmountToUiAmount';
|
|
4339
|
+
TokenInstruction[(TokenInstruction['UiAmountToAmount'] = 24)] =
|
|
4340
|
+
'UiAmountToAmount';
|
|
4341
|
+
TokenInstruction[(TokenInstruction['InitializeMintCloseAuthority'] = 25)] =
|
|
4342
|
+
'InitializeMintCloseAuthority';
|
|
4343
|
+
TokenInstruction[(TokenInstruction['TransferFeeExtension'] = 26)] =
|
|
4344
|
+
'TransferFeeExtension';
|
|
4345
|
+
TokenInstruction[(TokenInstruction['ConfidentialTransferExtension'] = 27)] =
|
|
4346
|
+
'ConfidentialTransferExtension';
|
|
4347
|
+
TokenInstruction[(TokenInstruction['DefaultAccountStateExtension'] = 28)] =
|
|
4348
|
+
'DefaultAccountStateExtension';
|
|
4349
|
+
TokenInstruction[(TokenInstruction['Reallocate'] = 29)] = 'Reallocate';
|
|
4350
|
+
TokenInstruction[(TokenInstruction['MemoTransferExtension'] = 30)] =
|
|
4351
|
+
'MemoTransferExtension';
|
|
4352
|
+
TokenInstruction[(TokenInstruction['CreateNativeMint'] = 31)] =
|
|
4353
|
+
'CreateNativeMint';
|
|
4354
|
+
TokenInstruction[(TokenInstruction['InitializeNonTransferableMint'] = 32)] =
|
|
4355
|
+
'InitializeNonTransferableMint';
|
|
4356
|
+
TokenInstruction[(TokenInstruction['InterestBearingMintExtension'] = 33)] =
|
|
4357
|
+
'InterestBearingMintExtension';
|
|
4358
|
+
TokenInstruction[(TokenInstruction['CpiGuardExtension'] = 34)] =
|
|
4359
|
+
'CpiGuardExtension';
|
|
4360
|
+
TokenInstruction[(TokenInstruction['InitializePermanentDelegate'] = 35)] =
|
|
4361
|
+
'InitializePermanentDelegate';
|
|
4362
|
+
TokenInstruction[(TokenInstruction['TransferHookExtension'] = 36)] =
|
|
4363
|
+
'TransferHookExtension';
|
|
4259
4364
|
// ConfidentialTransferFeeExtension = 37,
|
|
4260
4365
|
// WithdrawalExcessLamports = 38,
|
|
4261
|
-
TokenInstruction[TokenInstruction[
|
|
4262
|
-
|
|
4263
|
-
TokenInstruction[TokenInstruction[
|
|
4366
|
+
TokenInstruction[(TokenInstruction['MetadataPointerExtension'] = 39)] =
|
|
4367
|
+
'MetadataPointerExtension';
|
|
4368
|
+
TokenInstruction[(TokenInstruction['GroupPointerExtension'] = 40)] =
|
|
4369
|
+
'GroupPointerExtension';
|
|
4370
|
+
TokenInstruction[(TokenInstruction['GroupMemberPointerExtension'] = 41)] =
|
|
4371
|
+
'GroupMemberPointerExtension';
|
|
4264
4372
|
})(TokenInstruction || (TokenInstruction = {}));
|
|
4265
4373
|
|
|
4266
4374
|
/** @internal */
|
|
4267
4375
|
function addSigners(keys, ownerOrAuthority, multiSigners) {
|
|
4268
4376
|
if (multiSigners.length) {
|
|
4269
|
-
keys.push({
|
|
4377
|
+
keys.push({
|
|
4378
|
+
pubkey: ownerOrAuthority,
|
|
4379
|
+
isSigner: false,
|
|
4380
|
+
isWritable: false,
|
|
4381
|
+
});
|
|
4270
4382
|
for (const signer of multiSigners) {
|
|
4271
4383
|
keys.push({
|
|
4272
|
-
pubkey:
|
|
4384
|
+
pubkey:
|
|
4385
|
+
signer instanceof web3_js.PublicKey
|
|
4386
|
+
? signer
|
|
4387
|
+
: signer.publicKey,
|
|
4273
4388
|
isSigner: true,
|
|
4274
4389
|
isWritable: false,
|
|
4275
4390
|
});
|
|
4276
4391
|
}
|
|
4277
|
-
}
|
|
4278
|
-
|
|
4279
|
-
|
|
4392
|
+
} else {
|
|
4393
|
+
keys.push({
|
|
4394
|
+
pubkey: ownerOrAuthority,
|
|
4395
|
+
isSigner: true,
|
|
4396
|
+
isWritable: false,
|
|
4397
|
+
});
|
|
4280
4398
|
}
|
|
4281
4399
|
return keys;
|
|
4282
4400
|
}
|
|
4283
4401
|
|
|
4284
4402
|
var AccountType;
|
|
4285
4403
|
(function (AccountType) {
|
|
4286
|
-
AccountType[AccountType[
|
|
4287
|
-
AccountType[AccountType[
|
|
4288
|
-
AccountType[AccountType[
|
|
4404
|
+
AccountType[(AccountType['Uninitialized'] = 0)] = 'Uninitialized';
|
|
4405
|
+
AccountType[(AccountType['Mint'] = 1)] = 'Mint';
|
|
4406
|
+
AccountType[(AccountType['Account'] = 2)] = 'Account';
|
|
4289
4407
|
})(AccountType || (AccountType = {}));
|
|
4290
4408
|
const ACCOUNT_TYPE_SIZE = 1;
|
|
4291
4409
|
|
|
@@ -4312,9 +4430,9 @@ const MULTISIG_SIZE = MultisigLayout.span;
|
|
|
4312
4430
|
/** Token account state as stored by the program */
|
|
4313
4431
|
var AccountState;
|
|
4314
4432
|
(function (AccountState) {
|
|
4315
|
-
AccountState[AccountState[
|
|
4316
|
-
AccountState[AccountState[
|
|
4317
|
-
AccountState[AccountState[
|
|
4433
|
+
AccountState[(AccountState['Uninitialized'] = 0)] = 'Uninitialized';
|
|
4434
|
+
AccountState[(AccountState['Initialized'] = 1)] = 'Initialized';
|
|
4435
|
+
AccountState[(AccountState['Frozen'] = 2)] = 'Frozen';
|
|
4318
4436
|
})(AccountState || (AccountState = {}));
|
|
4319
4437
|
/** Buffer layout for de/serializing a token account */
|
|
4320
4438
|
const AccountLayout = struct([
|
|
@@ -4342,7 +4460,12 @@ const ACCOUNT_SIZE = AccountLayout.span;
|
|
|
4342
4460
|
*
|
|
4343
4461
|
* @return Token account information
|
|
4344
4462
|
*/
|
|
4345
|
-
async function getAccount(
|
|
4463
|
+
async function getAccount(
|
|
4464
|
+
connection,
|
|
4465
|
+
address,
|
|
4466
|
+
commitment,
|
|
4467
|
+
programId = TOKEN_PROGRAM_ID,
|
|
4468
|
+
) {
|
|
4346
4469
|
const info = await connection.getAccountInfo(address, commitment);
|
|
4347
4470
|
return unpackAccount(address, info, programId);
|
|
4348
4471
|
}
|
|
@@ -4356,8 +4479,7 @@ async function getAccount(connection, address, commitment, programId = TOKEN_PRO
|
|
|
4356
4479
|
* @return Unpacked token account
|
|
4357
4480
|
*/
|
|
4358
4481
|
function unpackAccount(address, info, programId = TOKEN_PROGRAM_ID) {
|
|
4359
|
-
if (!info)
|
|
4360
|
-
throw new TokenAccountNotFoundError();
|
|
4482
|
+
if (!info) throw new TokenAccountNotFoundError();
|
|
4361
4483
|
if (!info.owner.equals(programId))
|
|
4362
4484
|
throw new TokenInvalidAccountOwnerError();
|
|
4363
4485
|
if (info.data.length < ACCOUNT_SIZE)
|
|
@@ -4381,8 +4503,12 @@ function unpackAccount(address, info, programId = TOKEN_PROGRAM_ID) {
|
|
|
4381
4503
|
isInitialized: rawAccount.state !== AccountState.Uninitialized,
|
|
4382
4504
|
isFrozen: rawAccount.state === AccountState.Frozen,
|
|
4383
4505
|
isNative: !!rawAccount.isNativeOption,
|
|
4384
|
-
rentExemptReserve: rawAccount.isNativeOption
|
|
4385
|
-
|
|
4506
|
+
rentExemptReserve: rawAccount.isNativeOption
|
|
4507
|
+
? rawAccount.isNative
|
|
4508
|
+
: null,
|
|
4509
|
+
closeAuthority: rawAccount.closeAuthorityOption
|
|
4510
|
+
? rawAccount.closeAuthority
|
|
4511
|
+
: null,
|
|
4386
4512
|
tlvData,
|
|
4387
4513
|
};
|
|
4388
4514
|
}
|
|
@@ -4410,10 +4536,19 @@ const MINT_SIZE = MintLayout.span;
|
|
|
4410
4536
|
*
|
|
4411
4537
|
* @return Address of the associated token account
|
|
4412
4538
|
*/
|
|
4413
|
-
function getAssociatedTokenAddressSync(
|
|
4539
|
+
function getAssociatedTokenAddressSync(
|
|
4540
|
+
mint,
|
|
4541
|
+
owner,
|
|
4542
|
+
allowOwnerOffCurve = false,
|
|
4543
|
+
programId = TOKEN_PROGRAM_ID,
|
|
4544
|
+
associatedTokenProgramId = ASSOCIATED_TOKEN_PROGRAM_ID,
|
|
4545
|
+
) {
|
|
4414
4546
|
if (!allowOwnerOffCurve && !web3_js.PublicKey.isOnCurve(owner.toBuffer()))
|
|
4415
4547
|
throw new TokenOwnerOffCurveError();
|
|
4416
|
-
const [address] = web3_js.PublicKey.findProgramAddressSync(
|
|
4548
|
+
const [address] = web3_js.PublicKey.findProgramAddressSync(
|
|
4549
|
+
[owner.toBuffer(), programId.toBuffer(), mint.toBuffer()],
|
|
4550
|
+
associatedTokenProgramId,
|
|
4551
|
+
);
|
|
4417
4552
|
return address;
|
|
4418
4553
|
}
|
|
4419
4554
|
|
|
@@ -4433,8 +4568,7 @@ class COptionPublicKeyLayout extends Layout_2 {
|
|
|
4433
4568
|
if (src === null) {
|
|
4434
4569
|
buffer[offset] = 0;
|
|
4435
4570
|
return 1;
|
|
4436
|
-
}
|
|
4437
|
-
else {
|
|
4571
|
+
} else {
|
|
4438
4572
|
buffer[offset] = 1;
|
|
4439
4573
|
this.publicKeyLayout.encode(src, buffer, offset + 1);
|
|
4440
4574
|
return 33;
|
|
@@ -4461,16 +4595,43 @@ class COptionPublicKeyLayout extends Layout_2 {
|
|
|
4461
4595
|
*
|
|
4462
4596
|
* @return Instruction to add to a transaction
|
|
4463
4597
|
*/
|
|
4464
|
-
function createAssociatedTokenAccountInstruction(
|
|
4465
|
-
|
|
4598
|
+
function createAssociatedTokenAccountInstruction(
|
|
4599
|
+
payer,
|
|
4600
|
+
associatedToken,
|
|
4601
|
+
owner,
|
|
4602
|
+
mint,
|
|
4603
|
+
programId = TOKEN_PROGRAM_ID,
|
|
4604
|
+
associatedTokenProgramId = ASSOCIATED_TOKEN_PROGRAM_ID,
|
|
4605
|
+
) {
|
|
4606
|
+
return buildAssociatedTokenAccountInstruction(
|
|
4607
|
+
payer,
|
|
4608
|
+
associatedToken,
|
|
4609
|
+
owner,
|
|
4610
|
+
mint,
|
|
4611
|
+
Buffer.alloc(0),
|
|
4612
|
+
programId,
|
|
4613
|
+
associatedTokenProgramId,
|
|
4614
|
+
);
|
|
4466
4615
|
}
|
|
4467
|
-
function buildAssociatedTokenAccountInstruction(
|
|
4616
|
+
function buildAssociatedTokenAccountInstruction(
|
|
4617
|
+
payer,
|
|
4618
|
+
associatedToken,
|
|
4619
|
+
owner,
|
|
4620
|
+
mint,
|
|
4621
|
+
instructionData,
|
|
4622
|
+
programId = TOKEN_PROGRAM_ID,
|
|
4623
|
+
associatedTokenProgramId = ASSOCIATED_TOKEN_PROGRAM_ID,
|
|
4624
|
+
) {
|
|
4468
4625
|
const keys = [
|
|
4469
4626
|
{ pubkey: payer, isSigner: true, isWritable: true },
|
|
4470
4627
|
{ pubkey: associatedToken, isSigner: false, isWritable: true },
|
|
4471
4628
|
{ pubkey: owner, isSigner: false, isWritable: false },
|
|
4472
4629
|
{ pubkey: mint, isSigner: false, isWritable: false },
|
|
4473
|
-
{
|
|
4630
|
+
{
|
|
4631
|
+
pubkey: web3_js.SystemProgram.programId,
|
|
4632
|
+
isSigner: false,
|
|
4633
|
+
isWritable: false,
|
|
4634
|
+
},
|
|
4474
4635
|
{ pubkey: programId, isSigner: false, isWritable: false },
|
|
4475
4636
|
];
|
|
4476
4637
|
return new web3_js.TransactionInstruction({
|
|
@@ -4498,15 +4659,24 @@ const initializeMint2InstructionData = struct([
|
|
|
4498
4659
|
*
|
|
4499
4660
|
* @return Instruction to add to a transaction
|
|
4500
4661
|
*/
|
|
4501
|
-
function createInitializeMint2Instruction(
|
|
4662
|
+
function createInitializeMint2Instruction(
|
|
4663
|
+
mint,
|
|
4664
|
+
decimals,
|
|
4665
|
+
mintAuthority,
|
|
4666
|
+
freezeAuthority,
|
|
4667
|
+
programId = TOKEN_PROGRAM_ID,
|
|
4668
|
+
) {
|
|
4502
4669
|
const keys = [{ pubkey: mint, isSigner: false, isWritable: true }];
|
|
4503
4670
|
const data = Buffer.alloc(initializeMint2InstructionData.span);
|
|
4504
|
-
initializeMint2InstructionData.encode(
|
|
4505
|
-
|
|
4506
|
-
|
|
4507
|
-
|
|
4508
|
-
|
|
4509
|
-
|
|
4671
|
+
initializeMint2InstructionData.encode(
|
|
4672
|
+
{
|
|
4673
|
+
instruction: TokenInstruction.InitializeMint2,
|
|
4674
|
+
decimals,
|
|
4675
|
+
mintAuthority,
|
|
4676
|
+
freezeAuthority,
|
|
4677
|
+
},
|
|
4678
|
+
data,
|
|
4679
|
+
);
|
|
4510
4680
|
return new web3_js.TransactionInstruction({ keys, programId, data });
|
|
4511
4681
|
}
|
|
4512
4682
|
|
|
@@ -4525,39 +4695,77 @@ function createInitializeMint2Instruction(mint, decimals, mintAuthority, freezeA
|
|
|
4525
4695
|
*
|
|
4526
4696
|
* @return Address of the new associated token account
|
|
4527
4697
|
*/
|
|
4528
|
-
async function getOrCreateAssociatedTokenAccount(
|
|
4529
|
-
|
|
4698
|
+
async function getOrCreateAssociatedTokenAccount(
|
|
4699
|
+
connection,
|
|
4700
|
+
payer,
|
|
4701
|
+
mint,
|
|
4702
|
+
owner,
|
|
4703
|
+
allowOwnerOffCurve = false,
|
|
4704
|
+
commitment,
|
|
4705
|
+
confirmOptions,
|
|
4706
|
+
programId = TOKEN_PROGRAM_ID,
|
|
4707
|
+
associatedTokenProgramId = ASSOCIATED_TOKEN_PROGRAM_ID,
|
|
4708
|
+
) {
|
|
4709
|
+
const associatedToken = getAssociatedTokenAddressSync(
|
|
4710
|
+
mint,
|
|
4711
|
+
owner,
|
|
4712
|
+
allowOwnerOffCurve,
|
|
4713
|
+
programId,
|
|
4714
|
+
associatedTokenProgramId,
|
|
4715
|
+
);
|
|
4530
4716
|
// This is the optimal logic, considering TX fee, client-side computation, RPC roundtrips and guaranteed idempotent.
|
|
4531
4717
|
// Sadly we can't do this atomically.
|
|
4532
4718
|
let account;
|
|
4533
4719
|
try {
|
|
4534
|
-
account = await getAccount(
|
|
4535
|
-
|
|
4536
|
-
|
|
4720
|
+
account = await getAccount(
|
|
4721
|
+
connection,
|
|
4722
|
+
associatedToken,
|
|
4723
|
+
commitment,
|
|
4724
|
+
programId,
|
|
4725
|
+
);
|
|
4726
|
+
} catch (error) {
|
|
4537
4727
|
// TokenAccountNotFoundError can be possible if the associated address has already received some lamports,
|
|
4538
4728
|
// becoming a system account. Assuming program derived addressing is safe, this is the only case for the
|
|
4539
4729
|
// TokenInvalidAccountOwnerError in this code path.
|
|
4540
|
-
if (
|
|
4730
|
+
if (
|
|
4731
|
+
error instanceof TokenAccountNotFoundError ||
|
|
4732
|
+
error instanceof TokenInvalidAccountOwnerError
|
|
4733
|
+
) {
|
|
4541
4734
|
// As this isn't atomic, it's possible others can create associated accounts meanwhile.
|
|
4542
4735
|
try {
|
|
4543
|
-
const transaction = new web3_js.Transaction().add(
|
|
4544
|
-
|
|
4545
|
-
|
|
4546
|
-
|
|
4736
|
+
const transaction = new web3_js.Transaction().add(
|
|
4737
|
+
createAssociatedTokenAccountInstruction(
|
|
4738
|
+
payer.publicKey,
|
|
4739
|
+
associatedToken,
|
|
4740
|
+
owner,
|
|
4741
|
+
mint,
|
|
4742
|
+
programId,
|
|
4743
|
+
associatedTokenProgramId,
|
|
4744
|
+
),
|
|
4745
|
+
);
|
|
4746
|
+
await web3_js.sendAndConfirmTransaction(
|
|
4747
|
+
connection,
|
|
4748
|
+
transaction,
|
|
4749
|
+
[payer],
|
|
4750
|
+
confirmOptions,
|
|
4751
|
+
);
|
|
4752
|
+
} catch (error) {
|
|
4547
4753
|
// Ignore all errors; for now there is no API-compatible way to selectively ignore the expected
|
|
4548
4754
|
// instruction error if the associated account exists already.
|
|
4549
4755
|
}
|
|
4550
4756
|
// Now this should always succeed
|
|
4551
|
-
account = await getAccount(
|
|
4552
|
-
|
|
4553
|
-
|
|
4757
|
+
account = await getAccount(
|
|
4758
|
+
connection,
|
|
4759
|
+
associatedToken,
|
|
4760
|
+
commitment,
|
|
4761
|
+
programId,
|
|
4762
|
+
);
|
|
4763
|
+
} else {
|
|
4554
4764
|
throw error;
|
|
4555
4765
|
}
|
|
4556
4766
|
}
|
|
4557
|
-
if (!account.mint.equals(mint))
|
|
4558
|
-
|
|
4559
|
-
if (!account.owner.equals(owner))
|
|
4560
|
-
throw new TokenInvalidOwnerError();
|
|
4767
|
+
if (!account.mint.equals(mint)) throw new TokenInvalidMintError();
|
|
4768
|
+
if (!account.owner.equals(owner)) throw new TokenInvalidOwnerError();
|
|
4561
4769
|
return account;
|
|
4562
4770
|
}
|
|
4563
4771
|
|
|
@@ -4575,29 +4783,46 @@ const mintToInstructionData = struct([u8('instruction'), u64('amount')]);
|
|
|
4575
4783
|
*
|
|
4576
4784
|
* @return Instruction to add to a transaction
|
|
4577
4785
|
*/
|
|
4578
|
-
function createMintToInstruction(
|
|
4579
|
-
|
|
4580
|
-
|
|
4581
|
-
|
|
4582
|
-
|
|
4786
|
+
function createMintToInstruction(
|
|
4787
|
+
mint,
|
|
4788
|
+
destination,
|
|
4789
|
+
authority,
|
|
4790
|
+
amount,
|
|
4791
|
+
multiSigners = [],
|
|
4792
|
+
programId = TOKEN_PROGRAM_ID,
|
|
4793
|
+
) {
|
|
4794
|
+
const keys = addSigners(
|
|
4795
|
+
[
|
|
4796
|
+
{ pubkey: mint, isSigner: false, isWritable: true },
|
|
4797
|
+
{ pubkey: destination, isSigner: false, isWritable: true },
|
|
4798
|
+
],
|
|
4799
|
+
authority,
|
|
4800
|
+
multiSigners,
|
|
4801
|
+
);
|
|
4583
4802
|
const data = Buffer.alloc(mintToInstructionData.span);
|
|
4584
|
-
mintToInstructionData.encode(
|
|
4585
|
-
|
|
4586
|
-
|
|
4587
|
-
|
|
4803
|
+
mintToInstructionData.encode(
|
|
4804
|
+
{
|
|
4805
|
+
instruction: TokenInstruction.MintTo,
|
|
4806
|
+
amount: BigInt(amount),
|
|
4807
|
+
},
|
|
4808
|
+
data,
|
|
4809
|
+
);
|
|
4588
4810
|
return new web3_js.TransactionInstruction({ keys, programId, data });
|
|
4589
4811
|
}
|
|
4590
4812
|
|
|
4591
4813
|
/**
|
|
4592
4814
|
* Sum up the token amounts of the compressed token accounts
|
|
4593
4815
|
*/
|
|
4594
|
-
const sumUpTokenAmount =
|
|
4595
|
-
return accounts.reduce(
|
|
4816
|
+
const sumUpTokenAmount = accounts => {
|
|
4817
|
+
return accounts.reduce(
|
|
4818
|
+
(acc, account) => acc.add(account.parsed.amount),
|
|
4819
|
+
stateless_js.bn(0),
|
|
4820
|
+
);
|
|
4596
4821
|
};
|
|
4597
4822
|
/**
|
|
4598
4823
|
* Validate that all the compressed token accounts are owned by the same owner.
|
|
4599
4824
|
*/
|
|
4600
|
-
const validateSameTokenOwner =
|
|
4825
|
+
const validateSameTokenOwner = accounts => {
|
|
4601
4826
|
const owner = accounts[0].parsed.owner;
|
|
4602
4827
|
accounts.forEach(acc => {
|
|
4603
4828
|
if (!acc.parsed.owner.equals(owner)) {
|
|
@@ -4608,7 +4833,7 @@ const validateSameTokenOwner = (accounts) => {
|
|
|
4608
4833
|
/**
|
|
4609
4834
|
* Parse compressed token accounts to get the mint, current owner and delegate.
|
|
4610
4835
|
*/
|
|
4611
|
-
const parseTokenData =
|
|
4836
|
+
const parseTokenData = compressedTokenAccounts => {
|
|
4612
4837
|
const mint = compressedTokenAccounts[0].parsed.mint;
|
|
4613
4838
|
const currentOwner = compressedTokenAccounts[0].parsed.owner;
|
|
4614
4839
|
const delegate = compressedTokenAccounts[0].parsed.delegate;
|
|
@@ -4622,13 +4847,22 @@ const parseTokenData = (compressedTokenAccounts) => {
|
|
|
4622
4847
|
* @returns Output token data for the transfer
|
|
4623
4848
|
* instruction
|
|
4624
4849
|
*/
|
|
4625
|
-
function createTransferOutputState(
|
|
4850
|
+
function createTransferOutputState(
|
|
4851
|
+
inputCompressedTokenAccounts,
|
|
4852
|
+
toAddress,
|
|
4853
|
+
amount,
|
|
4854
|
+
) {
|
|
4626
4855
|
amount = stateless_js.bn(amount);
|
|
4627
4856
|
const inputAmount = sumUpTokenAmount(inputCompressedTokenAccounts);
|
|
4628
|
-
const inputLamports = stateless_js.sumUpLamports(
|
|
4857
|
+
const inputLamports = stateless_js.sumUpLamports(
|
|
4858
|
+
inputCompressedTokenAccounts.map(acc => acc.compressedAccount),
|
|
4859
|
+
);
|
|
4629
4860
|
const changeAmount = inputAmount.sub(amount);
|
|
4630
4861
|
stateless_js.validateSufficientBalance(changeAmount);
|
|
4631
|
-
if (
|
|
4862
|
+
if (
|
|
4863
|
+
changeAmount.eq(stateless_js.bn(0)) &&
|
|
4864
|
+
inputLamports.eq(stateless_js.bn(0))
|
|
4865
|
+
) {
|
|
4632
4866
|
return [
|
|
4633
4867
|
{
|
|
4634
4868
|
owner: toAddress,
|
|
@@ -4639,7 +4873,9 @@ function createTransferOutputState(inputCompressedTokenAccounts, toAddress, amou
|
|
|
4639
4873
|
];
|
|
4640
4874
|
}
|
|
4641
4875
|
/// validates token program
|
|
4642
|
-
stateless_js.validateSameOwner(
|
|
4876
|
+
stateless_js.validateSameOwner(
|
|
4877
|
+
inputCompressedTokenAccounts.map(acc => acc.compressedAccount),
|
|
4878
|
+
);
|
|
4643
4879
|
validateSameTokenOwner(inputCompressedTokenAccounts);
|
|
4644
4880
|
const outputCompressedAccounts = [
|
|
4645
4881
|
{
|
|
@@ -4666,15 +4902,22 @@ function createTransferOutputState(inputCompressedTokenAccounts, toAddress, amou
|
|
|
4666
4902
|
*/
|
|
4667
4903
|
function createDecompressOutputState(inputCompressedTokenAccounts, amount) {
|
|
4668
4904
|
amount = stateless_js.bn(amount);
|
|
4669
|
-
const inputLamports = stateless_js.sumUpLamports(
|
|
4905
|
+
const inputLamports = stateless_js.sumUpLamports(
|
|
4906
|
+
inputCompressedTokenAccounts.map(acc => acc.compressedAccount),
|
|
4907
|
+
);
|
|
4670
4908
|
const inputAmount = sumUpTokenAmount(inputCompressedTokenAccounts);
|
|
4671
4909
|
const changeAmount = inputAmount.sub(amount);
|
|
4672
4910
|
stateless_js.validateSufficientBalance(changeAmount);
|
|
4673
4911
|
/// lamports gets decompressed
|
|
4674
|
-
if (
|
|
4912
|
+
if (
|
|
4913
|
+
changeAmount.eq(stateless_js.bn(0)) &&
|
|
4914
|
+
inputLamports.eq(stateless_js.bn(0))
|
|
4915
|
+
) {
|
|
4675
4916
|
return [];
|
|
4676
4917
|
}
|
|
4677
|
-
stateless_js.validateSameOwner(
|
|
4918
|
+
stateless_js.validateSameOwner(
|
|
4919
|
+
inputCompressedTokenAccounts.map(acc => acc.compressedAccount),
|
|
4920
|
+
);
|
|
4678
4921
|
validateSameTokenOwner(inputCompressedTokenAccounts);
|
|
4679
4922
|
const tokenTransferOutputs = [
|
|
4680
4923
|
{
|
|
@@ -4690,7 +4933,7 @@ class CompressedTokenProgram {
|
|
|
4690
4933
|
/**
|
|
4691
4934
|
* @internal
|
|
4692
4935
|
*/
|
|
4693
|
-
constructor() {
|
|
4936
|
+
constructor() {}
|
|
4694
4937
|
/** @internal */
|
|
4695
4938
|
static get program() {
|
|
4696
4939
|
if (!this._program) {
|
|
@@ -4708,21 +4951,38 @@ class CompressedTokenProgram {
|
|
|
4708
4951
|
/// program only for serde and building instructions, not for
|
|
4709
4952
|
/// interacting with the network.
|
|
4710
4953
|
const mockKeypair = web3_js.Keypair.generate();
|
|
4711
|
-
const mockConnection = new web3_js.Connection(
|
|
4712
|
-
|
|
4954
|
+
const mockConnection = new web3_js.Connection(
|
|
4955
|
+
'http://127.0.0.1:8899',
|
|
4956
|
+
'confirmed',
|
|
4957
|
+
);
|
|
4958
|
+
const mockProvider = new anchor.AnchorProvider(
|
|
4959
|
+
mockConnection,
|
|
4960
|
+
stateless_js.useWallet(mockKeypair),
|
|
4961
|
+
stateless_js.confirmConfig,
|
|
4962
|
+
);
|
|
4713
4963
|
anchor.setProvider(mockProvider);
|
|
4714
|
-
this._program = new anchor.Program(
|
|
4964
|
+
this._program = new anchor.Program(
|
|
4965
|
+
IDL,
|
|
4966
|
+
this.programId,
|
|
4967
|
+
mockProvider,
|
|
4968
|
+
);
|
|
4715
4969
|
}
|
|
4716
4970
|
}
|
|
4717
4971
|
/** @internal */
|
|
4718
4972
|
static deriveTokenPoolPda(mint) {
|
|
4719
4973
|
const seeds = [POOL_SEED, mint.toBuffer()];
|
|
4720
|
-
const [address, _] = web3_js.PublicKey.findProgramAddressSync(
|
|
4974
|
+
const [address, _] = web3_js.PublicKey.findProgramAddressSync(
|
|
4975
|
+
seeds,
|
|
4976
|
+
this.programId,
|
|
4977
|
+
);
|
|
4721
4978
|
return address;
|
|
4722
4979
|
}
|
|
4723
4980
|
/** @internal */
|
|
4724
4981
|
static get deriveCpiAuthorityPda() {
|
|
4725
|
-
const [address, _] = web3_js.PublicKey.findProgramAddressSync(
|
|
4982
|
+
const [address, _] = web3_js.PublicKey.findProgramAddressSync(
|
|
4983
|
+
[CPI_AUTHORITY_SEED],
|
|
4984
|
+
this.programId,
|
|
4985
|
+
);
|
|
4726
4986
|
return address;
|
|
4727
4987
|
}
|
|
4728
4988
|
/**
|
|
@@ -4731,14 +4991,21 @@ class CompressedTokenProgram {
|
|
|
4731
4991
|
static async createMint(params) {
|
|
4732
4992
|
const { mint, authority, feePayer, rentExemptBalance } = params;
|
|
4733
4993
|
/// Create and initialize SPL Mint account
|
|
4734
|
-
const createMintAccountInstruction =
|
|
4735
|
-
|
|
4736
|
-
|
|
4737
|
-
|
|
4738
|
-
|
|
4739
|
-
|
|
4740
|
-
|
|
4741
|
-
|
|
4994
|
+
const createMintAccountInstruction =
|
|
4995
|
+
web3_js.SystemProgram.createAccount({
|
|
4996
|
+
fromPubkey: feePayer,
|
|
4997
|
+
lamports: rentExemptBalance,
|
|
4998
|
+
newAccountPubkey: mint,
|
|
4999
|
+
programId: TOKEN_PROGRAM_ID,
|
|
5000
|
+
space: MINT_SIZE,
|
|
5001
|
+
});
|
|
5002
|
+
const initializeMintInstruction = createInitializeMint2Instruction(
|
|
5003
|
+
mint,
|
|
5004
|
+
params.decimals,
|
|
5005
|
+
authority,
|
|
5006
|
+
params.freezeAuthority,
|
|
5007
|
+
TOKEN_PROGRAM_ID,
|
|
5008
|
+
);
|
|
4742
5009
|
const ix = await this.createTokenPool({
|
|
4743
5010
|
feePayer,
|
|
4744
5011
|
mint,
|
|
@@ -4755,13 +5022,13 @@ class CompressedTokenProgram {
|
|
|
4755
5022
|
const ix = await this.program.methods
|
|
4756
5023
|
.createTokenPool()
|
|
4757
5024
|
.accounts({
|
|
4758
|
-
|
|
4759
|
-
|
|
4760
|
-
|
|
4761
|
-
|
|
4762
|
-
|
|
4763
|
-
|
|
4764
|
-
|
|
5025
|
+
mint,
|
|
5026
|
+
feePayer,
|
|
5027
|
+
tokenPoolPda,
|
|
5028
|
+
systemProgram: web3_js.SystemProgram.programId,
|
|
5029
|
+
tokenProgram: TOKEN_PROGRAM_ID,
|
|
5030
|
+
cpiAuthorityPda: this.deriveCpiAuthorityPda,
|
|
5031
|
+
})
|
|
4765
5032
|
.instruction();
|
|
4766
5033
|
return ix;
|
|
4767
5034
|
}
|
|
@@ -4770,31 +5037,41 @@ class CompressedTokenProgram {
|
|
|
4770
5037
|
*/
|
|
4771
5038
|
static async mintTo(params) {
|
|
4772
5039
|
const systemKeys = stateless_js.defaultStaticAccountsStruct();
|
|
4773
|
-
const { mint, feePayer, authority, merkleTree, toPubkey, amount } =
|
|
5040
|
+
const { mint, feePayer, authority, merkleTree, toPubkey, amount } =
|
|
5041
|
+
params;
|
|
4774
5042
|
const tokenPoolPda = this.deriveTokenPoolPda(mint);
|
|
4775
|
-
const amounts = stateless_js
|
|
5043
|
+
const amounts = stateless_js
|
|
5044
|
+
.toArray(amount)
|
|
5045
|
+
.map(amount => stateless_js.bn(amount));
|
|
4776
5046
|
const toPubkeys = stateless_js.toArray(toPubkey);
|
|
4777
5047
|
if (amounts.length !== toPubkeys.length) {
|
|
4778
|
-
throw new Error(
|
|
5048
|
+
throw new Error(
|
|
5049
|
+
'Amount and toPubkey arrays must have the same length',
|
|
5050
|
+
);
|
|
4779
5051
|
}
|
|
4780
5052
|
const instruction = await this.program.methods
|
|
4781
5053
|
.mintTo(toPubkeys, amounts, null)
|
|
4782
5054
|
.accounts({
|
|
4783
|
-
|
|
4784
|
-
|
|
4785
|
-
|
|
4786
|
-
|
|
4787
|
-
|
|
4788
|
-
|
|
4789
|
-
|
|
4790
|
-
|
|
4791
|
-
|
|
4792
|
-
|
|
4793
|
-
|
|
4794
|
-
|
|
4795
|
-
|
|
4796
|
-
|
|
4797
|
-
|
|
5055
|
+
feePayer,
|
|
5056
|
+
authority,
|
|
5057
|
+
cpiAuthorityPda: this.deriveCpiAuthorityPda,
|
|
5058
|
+
mint,
|
|
5059
|
+
tokenPoolPda,
|
|
5060
|
+
tokenProgram: TOKEN_PROGRAM_ID,
|
|
5061
|
+
lightSystemProgram: stateless_js.LightSystemProgram.programId,
|
|
5062
|
+
registeredProgramPda: systemKeys.registeredProgramPda,
|
|
5063
|
+
noopProgram: systemKeys.noopProgram,
|
|
5064
|
+
accountCompressionAuthority:
|
|
5065
|
+
systemKeys.accountCompressionAuthority,
|
|
5066
|
+
accountCompressionProgram: systemKeys.accountCompressionProgram,
|
|
5067
|
+
merkleTree:
|
|
5068
|
+
merkleTree !== null && merkleTree !== void 0
|
|
5069
|
+
? merkleTree
|
|
5070
|
+
: stateless_js.defaultTestStateTreeAccounts()
|
|
5071
|
+
.merkleTree,
|
|
5072
|
+
selfProgram: this.programId,
|
|
5073
|
+
solPoolPda: null,
|
|
5074
|
+
})
|
|
4798
5075
|
.instruction();
|
|
4799
5076
|
return instruction;
|
|
4800
5077
|
}
|
|
@@ -4803,10 +5080,22 @@ class CompressedTokenProgram {
|
|
|
4803
5080
|
* Mint tokens from registed SPL mint account to a compressed account
|
|
4804
5081
|
*/
|
|
4805
5082
|
static async approveAndMintTo(params) {
|
|
4806
|
-
const {
|
|
5083
|
+
const {
|
|
5084
|
+
mint,
|
|
5085
|
+
feePayer,
|
|
5086
|
+
authorityTokenAccount,
|
|
5087
|
+
authority,
|
|
5088
|
+
merkleTree,
|
|
5089
|
+
toPubkey,
|
|
5090
|
+
} = params;
|
|
4807
5091
|
const amount = BigInt(params.amount.toString());
|
|
4808
5092
|
/// 1. Mint to existing ATA of mintAuthority.
|
|
4809
|
-
const splMintToInstruction = createMintToInstruction(
|
|
5093
|
+
const splMintToInstruction = createMintToInstruction(
|
|
5094
|
+
mint,
|
|
5095
|
+
authorityTokenAccount,
|
|
5096
|
+
authority,
|
|
5097
|
+
amount,
|
|
5098
|
+
);
|
|
4810
5099
|
/// 2. Compress from mint authority ATA to recipient compressed account
|
|
4811
5100
|
const compressInstruction = await this.compress({
|
|
4812
5101
|
payer: feePayer,
|
|
@@ -4823,15 +5112,33 @@ class CompressedTokenProgram {
|
|
|
4823
5112
|
* Construct transfer instruction for compressed tokens
|
|
4824
5113
|
*/
|
|
4825
5114
|
static async transfer(params) {
|
|
4826
|
-
const {
|
|
4827
|
-
|
|
4828
|
-
|
|
5115
|
+
const {
|
|
5116
|
+
payer,
|
|
5117
|
+
inputCompressedTokenAccounts,
|
|
5118
|
+
recentInputStateRootIndices,
|
|
5119
|
+
recentValidityProof,
|
|
5120
|
+
amount,
|
|
5121
|
+
outputStateTrees,
|
|
5122
|
+
toAddress,
|
|
5123
|
+
} = params;
|
|
5124
|
+
const tokenTransferOutputs = createTransferOutputState(
|
|
5125
|
+
inputCompressedTokenAccounts,
|
|
5126
|
+
toAddress,
|
|
5127
|
+
amount,
|
|
5128
|
+
);
|
|
5129
|
+
const {
|
|
5130
|
+
inputTokenDataWithContext,
|
|
5131
|
+
packedOutputTokenData,
|
|
5132
|
+
remainingAccountMetas,
|
|
5133
|
+
} = packCompressedTokenAccounts({
|
|
4829
5134
|
inputCompressedTokenAccounts,
|
|
4830
5135
|
outputStateTrees,
|
|
4831
5136
|
rootIndices: recentInputStateRootIndices,
|
|
4832
5137
|
tokenTransferOutputs,
|
|
4833
5138
|
});
|
|
4834
|
-
const { mint, currentOwner } = parseTokenData(
|
|
5139
|
+
const { mint, currentOwner } = parseTokenData(
|
|
5140
|
+
inputCompressedTokenAccounts,
|
|
5141
|
+
);
|
|
4835
5142
|
const data = {
|
|
4836
5143
|
proof: recentValidityProof,
|
|
4837
5144
|
mint,
|
|
@@ -4843,24 +5150,32 @@ class CompressedTokenProgram {
|
|
|
4843
5150
|
cpiContext: null,
|
|
4844
5151
|
lamportsChangeAccountMerkleTreeIndex: null,
|
|
4845
5152
|
};
|
|
4846
|
-
const encodedData = this.program.coder.types.encode(
|
|
4847
|
-
|
|
5153
|
+
const encodedData = this.program.coder.types.encode(
|
|
5154
|
+
'CompressedTokenInstructionDataTransfer',
|
|
5155
|
+
data,
|
|
5156
|
+
);
|
|
5157
|
+
const {
|
|
5158
|
+
accountCompressionAuthority,
|
|
5159
|
+
noopProgram,
|
|
5160
|
+
registeredProgramPda,
|
|
5161
|
+
accountCompressionProgram,
|
|
5162
|
+
} = stateless_js.defaultStaticAccountsStruct();
|
|
4848
5163
|
const instruction = await this.program.methods
|
|
4849
5164
|
.transfer(encodedData)
|
|
4850
5165
|
.accounts({
|
|
4851
|
-
|
|
4852
|
-
|
|
4853
|
-
|
|
4854
|
-
|
|
4855
|
-
|
|
4856
|
-
|
|
4857
|
-
|
|
4858
|
-
|
|
4859
|
-
|
|
4860
|
-
|
|
4861
|
-
|
|
4862
|
-
|
|
4863
|
-
|
|
5166
|
+
feePayer: payer,
|
|
5167
|
+
authority: currentOwner,
|
|
5168
|
+
cpiAuthorityPda: this.deriveCpiAuthorityPda,
|
|
5169
|
+
lightSystemProgram: stateless_js.LightSystemProgram.programId,
|
|
5170
|
+
registeredProgramPda: registeredProgramPda,
|
|
5171
|
+
noopProgram: noopProgram,
|
|
5172
|
+
accountCompressionAuthority: accountCompressionAuthority,
|
|
5173
|
+
accountCompressionProgram: accountCompressionProgram,
|
|
5174
|
+
selfProgram: this.programId,
|
|
5175
|
+
tokenPoolPda: null,
|
|
5176
|
+
compressOrDecompressTokenAccount: null,
|
|
5177
|
+
tokenProgram: null,
|
|
5178
|
+
})
|
|
4864
5179
|
.remainingAccounts(remainingAccountMetas)
|
|
4865
5180
|
.instruction();
|
|
4866
5181
|
return instruction;
|
|
@@ -4869,12 +5184,14 @@ class CompressedTokenProgram {
|
|
|
4869
5184
|
* Create lookup table instructions for the token program's default accounts.
|
|
4870
5185
|
*/
|
|
4871
5186
|
static async createTokenProgramLookupTable(params) {
|
|
4872
|
-
const { authority, mints, recentSlot, payer, remainingAccounts } =
|
|
4873
|
-
|
|
4874
|
-
|
|
4875
|
-
|
|
4876
|
-
|
|
4877
|
-
|
|
5187
|
+
const { authority, mints, recentSlot, payer, remainingAccounts } =
|
|
5188
|
+
params;
|
|
5189
|
+
const [createInstruction, lookupTableAddress] =
|
|
5190
|
+
web3_js.AddressLookupTableProgram.createLookupTable({
|
|
5191
|
+
authority,
|
|
5192
|
+
payer: authority,
|
|
5193
|
+
recentSlot,
|
|
5194
|
+
});
|
|
4878
5195
|
let optionalMintKeys = [];
|
|
4879
5196
|
if (mints) {
|
|
4880
5197
|
optionalMintKeys = [
|
|
@@ -4882,28 +5199,35 @@ class CompressedTokenProgram {
|
|
|
4882
5199
|
...mints.map(mint => this.deriveTokenPoolPda(mint)),
|
|
4883
5200
|
];
|
|
4884
5201
|
}
|
|
4885
|
-
const extendInstruction =
|
|
4886
|
-
|
|
4887
|
-
|
|
4888
|
-
lookupTable: lookupTableAddress,
|
|
4889
|
-
addresses: [
|
|
4890
|
-
this.deriveCpiAuthorityPda,
|
|
4891
|
-
stateless_js.LightSystemProgram.programId,
|
|
4892
|
-
stateless_js.defaultStaticAccountsStruct().registeredProgramPda,
|
|
4893
|
-
stateless_js.defaultStaticAccountsStruct().noopProgram,
|
|
4894
|
-
stateless_js.defaultStaticAccountsStruct().accountCompressionAuthority,
|
|
4895
|
-
stateless_js.defaultStaticAccountsStruct().accountCompressionProgram,
|
|
4896
|
-
stateless_js.defaultTestStateTreeAccounts().merkleTree,
|
|
4897
|
-
stateless_js.defaultTestStateTreeAccounts().nullifierQueue,
|
|
4898
|
-
stateless_js.defaultTestStateTreeAccounts().addressTree,
|
|
4899
|
-
stateless_js.defaultTestStateTreeAccounts().addressQueue,
|
|
4900
|
-
this.programId,
|
|
4901
|
-
TOKEN_PROGRAM_ID,
|
|
5202
|
+
const extendInstruction =
|
|
5203
|
+
web3_js.AddressLookupTableProgram.extendLookupTable({
|
|
5204
|
+
payer,
|
|
4902
5205
|
authority,
|
|
4903
|
-
|
|
4904
|
-
|
|
4905
|
-
|
|
4906
|
-
|
|
5206
|
+
lookupTable: lookupTableAddress,
|
|
5207
|
+
addresses: [
|
|
5208
|
+
this.deriveCpiAuthorityPda,
|
|
5209
|
+
stateless_js.LightSystemProgram.programId,
|
|
5210
|
+
stateless_js.defaultStaticAccountsStruct()
|
|
5211
|
+
.registeredProgramPda,
|
|
5212
|
+
stateless_js.defaultStaticAccountsStruct().noopProgram,
|
|
5213
|
+
stateless_js.defaultStaticAccountsStruct()
|
|
5214
|
+
.accountCompressionAuthority,
|
|
5215
|
+
stateless_js.defaultStaticAccountsStruct()
|
|
5216
|
+
.accountCompressionProgram,
|
|
5217
|
+
stateless_js.defaultTestStateTreeAccounts().merkleTree,
|
|
5218
|
+
stateless_js.defaultTestStateTreeAccounts().nullifierQueue,
|
|
5219
|
+
stateless_js.defaultTestStateTreeAccounts().addressTree,
|
|
5220
|
+
stateless_js.defaultTestStateTreeAccounts().addressQueue,
|
|
5221
|
+
this.programId,
|
|
5222
|
+
TOKEN_PROGRAM_ID,
|
|
5223
|
+
authority,
|
|
5224
|
+
...optionalMintKeys,
|
|
5225
|
+
...(remainingAccounts !== null &&
|
|
5226
|
+
remainingAccounts !== void 0
|
|
5227
|
+
? remainingAccounts
|
|
5228
|
+
: []),
|
|
5229
|
+
],
|
|
5230
|
+
});
|
|
4907
5231
|
return {
|
|
4908
5232
|
instructions: [createInstruction, extendInstruction],
|
|
4909
5233
|
address: lookupTableAddress,
|
|
@@ -4914,14 +5238,19 @@ class CompressedTokenProgram {
|
|
|
4914
5238
|
* @returns compressInstruction
|
|
4915
5239
|
*/
|
|
4916
5240
|
static async compress(params) {
|
|
4917
|
-
const { payer, owner, source, toAddress, mint, outputStateTree } =
|
|
5241
|
+
const { payer, owner, source, toAddress, mint, outputStateTree } =
|
|
5242
|
+
params;
|
|
4918
5243
|
if (Array.isArray(params.amount) !== Array.isArray(params.toAddress)) {
|
|
4919
|
-
throw new Error(
|
|
5244
|
+
throw new Error(
|
|
5245
|
+
'Both amount and toAddress must be arrays or both must be single values',
|
|
5246
|
+
);
|
|
4920
5247
|
}
|
|
4921
5248
|
let tokenTransferOutputs;
|
|
4922
5249
|
if (Array.isArray(params.amount) && Array.isArray(params.toAddress)) {
|
|
4923
5250
|
if (params.amount.length !== params.toAddress.length) {
|
|
4924
|
-
throw new Error(
|
|
5251
|
+
throw new Error(
|
|
5252
|
+
'Amount and toAddress arrays must have the same length',
|
|
5253
|
+
);
|
|
4925
5254
|
}
|
|
4926
5255
|
tokenTransferOutputs = params.amount.map((amt, index) => {
|
|
4927
5256
|
const amount = stateless_js.bn(amt);
|
|
@@ -4932,8 +5261,7 @@ class CompressedTokenProgram {
|
|
|
4932
5261
|
tlv: null,
|
|
4933
5262
|
};
|
|
4934
5263
|
});
|
|
4935
|
-
}
|
|
4936
|
-
else {
|
|
5264
|
+
} else {
|
|
4937
5265
|
tokenTransferOutputs = [
|
|
4938
5266
|
{
|
|
4939
5267
|
owner: toAddress,
|
|
@@ -4943,7 +5271,11 @@ class CompressedTokenProgram {
|
|
|
4943
5271
|
},
|
|
4944
5272
|
];
|
|
4945
5273
|
}
|
|
4946
|
-
const {
|
|
5274
|
+
const {
|
|
5275
|
+
inputTokenDataWithContext,
|
|
5276
|
+
packedOutputTokenData,
|
|
5277
|
+
remainingAccountMetas,
|
|
5278
|
+
} = packCompressedTokenAccounts({
|
|
4947
5279
|
inputCompressedTokenAccounts: [],
|
|
4948
5280
|
outputStateTrees: outputStateTree,
|
|
4949
5281
|
rootIndices: [],
|
|
@@ -4957,30 +5289,40 @@ class CompressedTokenProgram {
|
|
|
4957
5289
|
outputCompressedAccounts: packedOutputTokenData,
|
|
4958
5290
|
compressOrDecompressAmount: Array.isArray(params.amount)
|
|
4959
5291
|
? params.amount
|
|
4960
|
-
|
|
4961
|
-
|
|
5292
|
+
.map(amt => new anchor.BN(amt))
|
|
5293
|
+
.reduce((sum, amt) => sum.add(amt), new anchor.BN(0))
|
|
4962
5294
|
: new anchor.BN(params.amount),
|
|
4963
5295
|
isCompress: true,
|
|
4964
5296
|
cpiContext: null,
|
|
4965
5297
|
lamportsChangeAccountMerkleTreeIndex: null,
|
|
4966
5298
|
};
|
|
4967
|
-
const encodedData = this.program.coder.types.encode(
|
|
5299
|
+
const encodedData = this.program.coder.types.encode(
|
|
5300
|
+
'CompressedTokenInstructionDataTransfer',
|
|
5301
|
+
data,
|
|
5302
|
+
);
|
|
4968
5303
|
const instruction = await this.program.methods
|
|
4969
5304
|
.transfer(encodedData)
|
|
4970
5305
|
.accounts({
|
|
4971
|
-
|
|
4972
|
-
|
|
4973
|
-
|
|
4974
|
-
|
|
4975
|
-
|
|
4976
|
-
|
|
4977
|
-
|
|
4978
|
-
|
|
4979
|
-
|
|
4980
|
-
|
|
4981
|
-
|
|
4982
|
-
|
|
4983
|
-
|
|
5306
|
+
feePayer: payer,
|
|
5307
|
+
authority: owner,
|
|
5308
|
+
cpiAuthorityPda: this.deriveCpiAuthorityPda,
|
|
5309
|
+
lightSystemProgram: stateless_js.LightSystemProgram.programId,
|
|
5310
|
+
registeredProgramPda:
|
|
5311
|
+
stateless_js.defaultStaticAccountsStruct()
|
|
5312
|
+
.registeredProgramPda,
|
|
5313
|
+
noopProgram:
|
|
5314
|
+
stateless_js.defaultStaticAccountsStruct().noopProgram,
|
|
5315
|
+
accountCompressionAuthority:
|
|
5316
|
+
stateless_js.defaultStaticAccountsStruct()
|
|
5317
|
+
.accountCompressionAuthority,
|
|
5318
|
+
accountCompressionProgram:
|
|
5319
|
+
stateless_js.defaultStaticAccountsStruct()
|
|
5320
|
+
.accountCompressionProgram,
|
|
5321
|
+
selfProgram: this.programId,
|
|
5322
|
+
tokenPoolPda: this.deriveTokenPoolPda(mint),
|
|
5323
|
+
compressOrDecompressTokenAccount: source, // token
|
|
5324
|
+
tokenProgram: TOKEN_PROGRAM_ID,
|
|
5325
|
+
})
|
|
4984
5326
|
.remainingAccounts(remainingAccountMetas)
|
|
4985
5327
|
.instruction();
|
|
4986
5328
|
return instruction;
|
|
@@ -4989,17 +5331,33 @@ class CompressedTokenProgram {
|
|
|
4989
5331
|
* Construct decompress instruction
|
|
4990
5332
|
*/
|
|
4991
5333
|
static async decompress(params) {
|
|
4992
|
-
const {
|
|
5334
|
+
const {
|
|
5335
|
+
payer,
|
|
5336
|
+
inputCompressedTokenAccounts,
|
|
5337
|
+
toAddress,
|
|
5338
|
+
outputStateTree,
|
|
5339
|
+
recentValidityProof,
|
|
5340
|
+
recentInputStateRootIndices,
|
|
5341
|
+
} = params;
|
|
4993
5342
|
const amount = stateless_js.bn(params.amount);
|
|
4994
|
-
const tokenTransferOutputs = createDecompressOutputState(
|
|
5343
|
+
const tokenTransferOutputs = createDecompressOutputState(
|
|
5344
|
+
inputCompressedTokenAccounts,
|
|
5345
|
+
amount,
|
|
5346
|
+
);
|
|
4995
5347
|
/// Pack
|
|
4996
|
-
const {
|
|
5348
|
+
const {
|
|
5349
|
+
inputTokenDataWithContext,
|
|
5350
|
+
packedOutputTokenData,
|
|
5351
|
+
remainingAccountMetas,
|
|
5352
|
+
} = packCompressedTokenAccounts({
|
|
4997
5353
|
inputCompressedTokenAccounts,
|
|
4998
5354
|
outputStateTrees: outputStateTree,
|
|
4999
5355
|
rootIndices: recentInputStateRootIndices,
|
|
5000
5356
|
tokenTransferOutputs: tokenTransferOutputs,
|
|
5001
5357
|
});
|
|
5002
|
-
const { mint, currentOwner } = parseTokenData(
|
|
5358
|
+
const { mint, currentOwner } = parseTokenData(
|
|
5359
|
+
inputCompressedTokenAccounts,
|
|
5360
|
+
);
|
|
5003
5361
|
const data = {
|
|
5004
5362
|
proof: recentValidityProof,
|
|
5005
5363
|
mint,
|
|
@@ -5011,33 +5369,69 @@ class CompressedTokenProgram {
|
|
|
5011
5369
|
cpiContext: null,
|
|
5012
5370
|
lamportsChangeAccountMerkleTreeIndex: null,
|
|
5013
5371
|
};
|
|
5014
|
-
const encodedData = this.program.coder.types.encode(
|
|
5015
|
-
|
|
5372
|
+
const encodedData = this.program.coder.types.encode(
|
|
5373
|
+
'CompressedTokenInstructionDataTransfer',
|
|
5374
|
+
data,
|
|
5375
|
+
);
|
|
5376
|
+
const {
|
|
5377
|
+
accountCompressionAuthority,
|
|
5378
|
+
noopProgram,
|
|
5379
|
+
registeredProgramPda,
|
|
5380
|
+
accountCompressionProgram,
|
|
5381
|
+
} = stateless_js.defaultStaticAccountsStruct();
|
|
5016
5382
|
const instruction = await this.program.methods
|
|
5017
5383
|
.transfer(encodedData)
|
|
5018
5384
|
.accounts({
|
|
5019
|
-
|
|
5020
|
-
|
|
5021
|
-
|
|
5022
|
-
|
|
5023
|
-
|
|
5024
|
-
|
|
5025
|
-
|
|
5026
|
-
|
|
5027
|
-
|
|
5028
|
-
|
|
5029
|
-
|
|
5030
|
-
|
|
5031
|
-
|
|
5385
|
+
feePayer: payer,
|
|
5386
|
+
authority: currentOwner,
|
|
5387
|
+
cpiAuthorityPda: this.deriveCpiAuthorityPda,
|
|
5388
|
+
lightSystemProgram: stateless_js.LightSystemProgram.programId,
|
|
5389
|
+
registeredProgramPda: registeredProgramPda,
|
|
5390
|
+
noopProgram: noopProgram,
|
|
5391
|
+
accountCompressionAuthority: accountCompressionAuthority,
|
|
5392
|
+
accountCompressionProgram: accountCompressionProgram,
|
|
5393
|
+
selfProgram: this.programId,
|
|
5394
|
+
tokenPoolPda: this.deriveTokenPoolPda(mint),
|
|
5395
|
+
compressOrDecompressTokenAccount: toAddress,
|
|
5396
|
+
tokenProgram: TOKEN_PROGRAM_ID,
|
|
5397
|
+
})
|
|
5032
5398
|
.remainingAccounts(remainingAccountMetas)
|
|
5033
5399
|
.instruction();
|
|
5034
5400
|
return instruction;
|
|
5035
5401
|
}
|
|
5402
|
+
static async mergeTokenAccounts(params) {
|
|
5403
|
+
const {
|
|
5404
|
+
payer,
|
|
5405
|
+
owner,
|
|
5406
|
+
inputCompressedTokenAccounts,
|
|
5407
|
+
outputStateTree,
|
|
5408
|
+
recentValidityProof,
|
|
5409
|
+
recentInputStateRootIndices,
|
|
5410
|
+
} = params;
|
|
5411
|
+
if (inputCompressedTokenAccounts.length > 3) {
|
|
5412
|
+
throw new Error('Cannot merge more than 3 token accounts at once');
|
|
5413
|
+
}
|
|
5414
|
+
const ix = await this.transfer({
|
|
5415
|
+
payer,
|
|
5416
|
+
inputCompressedTokenAccounts,
|
|
5417
|
+
toAddress: owner,
|
|
5418
|
+
amount: inputCompressedTokenAccounts.reduce(
|
|
5419
|
+
(sum, account) => sum.add(account.parsed.amount),
|
|
5420
|
+
new anchor.BN(0),
|
|
5421
|
+
),
|
|
5422
|
+
outputStateTrees: outputStateTree,
|
|
5423
|
+
recentInputStateRootIndices,
|
|
5424
|
+
recentValidityProof,
|
|
5425
|
+
});
|
|
5426
|
+
return [ix];
|
|
5427
|
+
}
|
|
5036
5428
|
}
|
|
5037
5429
|
/**
|
|
5038
5430
|
* Public key that identifies the CompressedPda program
|
|
5039
5431
|
*/
|
|
5040
|
-
CompressedTokenProgram.programId = new web3_js.PublicKey(
|
|
5432
|
+
CompressedTokenProgram.programId = new web3_js.PublicKey(
|
|
5433
|
+
'cTokenmWW8bLPjZEBAUgYy3zKxQZW6VKi7bqNFEVv3m',
|
|
5434
|
+
);
|
|
5041
5435
|
CompressedTokenProgram._program = null;
|
|
5042
5436
|
|
|
5043
5437
|
/**
|
|
@@ -5055,8 +5449,22 @@ CompressedTokenProgram._program = null;
|
|
|
5055
5449
|
*
|
|
5056
5450
|
* @return Signature of the confirmed transaction
|
|
5057
5451
|
*/
|
|
5058
|
-
async function approveAndMintTo(
|
|
5059
|
-
|
|
5452
|
+
async function approveAndMintTo(
|
|
5453
|
+
rpc,
|
|
5454
|
+
payer,
|
|
5455
|
+
mint,
|
|
5456
|
+
destination,
|
|
5457
|
+
authority,
|
|
5458
|
+
amount,
|
|
5459
|
+
merkleTree,
|
|
5460
|
+
confirmOptions,
|
|
5461
|
+
) {
|
|
5462
|
+
const authorityTokenAccount = await getOrCreateAssociatedTokenAccount(
|
|
5463
|
+
rpc,
|
|
5464
|
+
payer,
|
|
5465
|
+
mint,
|
|
5466
|
+
authority.publicKey,
|
|
5467
|
+
);
|
|
5060
5468
|
const ixs = await CompressedTokenProgram.approveAndMintTo({
|
|
5061
5469
|
feePayer: payer.publicKey,
|
|
5062
5470
|
mint,
|
|
@@ -5068,10 +5476,17 @@ async function approveAndMintTo(rpc, payer, mint, destination, authority, amount
|
|
|
5068
5476
|
});
|
|
5069
5477
|
const { blockhash } = await rpc.getLatestBlockhash();
|
|
5070
5478
|
const additionalSigners = stateless_js.dedupeSigner(payer, [authority]);
|
|
5071
|
-
const tx = stateless_js.buildAndSignTx(
|
|
5072
|
-
|
|
5073
|
-
|
|
5074
|
-
|
|
5479
|
+
const tx = stateless_js.buildAndSignTx(
|
|
5480
|
+
[
|
|
5481
|
+
web3_js.ComputeBudgetProgram.setComputeUnitLimit({
|
|
5482
|
+
units: 1000000,
|
|
5483
|
+
}),
|
|
5484
|
+
...ixs,
|
|
5485
|
+
],
|
|
5486
|
+
payer,
|
|
5487
|
+
blockhash,
|
|
5488
|
+
additionalSigners,
|
|
5489
|
+
);
|
|
5075
5490
|
const txId = await stateless_js.sendAndConfirmTx(rpc, tx, confirmOptions);
|
|
5076
5491
|
return txId;
|
|
5077
5492
|
}
|
|
@@ -5094,7 +5509,17 @@ async function approveAndMintTo(rpc, payer, mint, destination, authority, amount
|
|
|
5094
5509
|
*
|
|
5095
5510
|
* @return Signature of the confirmed transaction
|
|
5096
5511
|
*/
|
|
5097
|
-
async function compress(
|
|
5512
|
+
async function compress(
|
|
5513
|
+
rpc,
|
|
5514
|
+
payer,
|
|
5515
|
+
mint,
|
|
5516
|
+
amount,
|
|
5517
|
+
owner,
|
|
5518
|
+
sourceTokenAccount,
|
|
5519
|
+
toAddress,
|
|
5520
|
+
merkleTree,
|
|
5521
|
+
confirmOptions,
|
|
5522
|
+
) {
|
|
5098
5523
|
const compressIx = await CompressedTokenProgram.compress({
|
|
5099
5524
|
payer: payer.publicKey,
|
|
5100
5525
|
owner: owner.publicKey,
|
|
@@ -5106,13 +5531,23 @@ async function compress(rpc, payer, mint, amount, owner, sourceTokenAccount, toA
|
|
|
5106
5531
|
});
|
|
5107
5532
|
const blockhashCtx = await rpc.getLatestBlockhash();
|
|
5108
5533
|
const additionalSigners = stateless_js.dedupeSigner(payer, [owner]);
|
|
5109
|
-
const signedTx = stateless_js.buildAndSignTx(
|
|
5110
|
-
|
|
5111
|
-
|
|
5112
|
-
|
|
5113
|
-
|
|
5114
|
-
|
|
5115
|
-
|
|
5534
|
+
const signedTx = stateless_js.buildAndSignTx(
|
|
5535
|
+
[
|
|
5536
|
+
web3_js.ComputeBudgetProgram.setComputeUnitLimit({
|
|
5537
|
+
units: 1000000,
|
|
5538
|
+
}),
|
|
5539
|
+
compressIx,
|
|
5540
|
+
],
|
|
5541
|
+
payer,
|
|
5542
|
+
blockhashCtx.blockhash,
|
|
5543
|
+
additionalSigners,
|
|
5544
|
+
);
|
|
5545
|
+
const txId = await stateless_js.sendAndConfirmTx(
|
|
5546
|
+
rpc,
|
|
5547
|
+
signedTx,
|
|
5548
|
+
confirmOptions,
|
|
5549
|
+
blockhashCtx,
|
|
5550
|
+
);
|
|
5116
5551
|
return txId;
|
|
5117
5552
|
}
|
|
5118
5553
|
|
|
@@ -5133,15 +5568,33 @@ async function compress(rpc, payer, mint, amount, owner, sourceTokenAccount, toA
|
|
|
5133
5568
|
*
|
|
5134
5569
|
* @return Signature of the confirmed transaction
|
|
5135
5570
|
*/
|
|
5136
|
-
async function transfer(
|
|
5137
|
-
|
|
5138
|
-
|
|
5571
|
+
async function transfer(
|
|
5572
|
+
rpc,
|
|
5573
|
+
payer,
|
|
5574
|
+
mint,
|
|
5575
|
+
amount,
|
|
5576
|
+
owner,
|
|
5577
|
+
toAddress,
|
|
5578
|
+
/// TODO: allow multiple
|
|
5579
|
+
merkleTree,
|
|
5580
|
+
confirmOptions,
|
|
5581
|
+
) {
|
|
5139
5582
|
amount = stateless_js.bn(amount);
|
|
5140
|
-
const compressedTokenAccounts = await rpc.getCompressedTokenAccountsByOwner(
|
|
5141
|
-
|
|
5142
|
-
|
|
5143
|
-
|
|
5144
|
-
|
|
5583
|
+
const compressedTokenAccounts = await rpc.getCompressedTokenAccountsByOwner(
|
|
5584
|
+
owner.publicKey,
|
|
5585
|
+
{
|
|
5586
|
+
mint,
|
|
5587
|
+
},
|
|
5588
|
+
);
|
|
5589
|
+
const [inputAccounts] = selectMinCompressedTokenAccountsForTransfer(
|
|
5590
|
+
compressedTokenAccounts.items,
|
|
5591
|
+
amount,
|
|
5592
|
+
);
|
|
5593
|
+
const proof = await rpc.getValidityProof(
|
|
5594
|
+
inputAccounts.map(account =>
|
|
5595
|
+
stateless_js.bn(account.compressedAccount.hash),
|
|
5596
|
+
),
|
|
5597
|
+
);
|
|
5145
5598
|
const ix = await CompressedTokenProgram.transfer({
|
|
5146
5599
|
payer: payer.publicKey,
|
|
5147
5600
|
inputCompressedTokenAccounts: inputAccounts,
|
|
@@ -5153,8 +5606,22 @@ merkleTree, confirmOptions) {
|
|
|
5153
5606
|
});
|
|
5154
5607
|
const { blockhash } = await rpc.getLatestBlockhash();
|
|
5155
5608
|
const additionalSigners = stateless_js.dedupeSigner(payer, [owner]);
|
|
5156
|
-
const signedTx = stateless_js.buildAndSignTx(
|
|
5157
|
-
|
|
5609
|
+
const signedTx = stateless_js.buildAndSignTx(
|
|
5610
|
+
[
|
|
5611
|
+
web3_js.ComputeBudgetProgram.setComputeUnitLimit({
|
|
5612
|
+
units: 1000000,
|
|
5613
|
+
}),
|
|
5614
|
+
ix,
|
|
5615
|
+
],
|
|
5616
|
+
payer,
|
|
5617
|
+
blockhash,
|
|
5618
|
+
additionalSigners,
|
|
5619
|
+
);
|
|
5620
|
+
const txId = await stateless_js.sendAndConfirmTx(
|
|
5621
|
+
rpc,
|
|
5622
|
+
signedTx,
|
|
5623
|
+
confirmOptions,
|
|
5624
|
+
);
|
|
5158
5625
|
return txId;
|
|
5159
5626
|
}
|
|
5160
5627
|
/**
|
|
@@ -5170,14 +5637,17 @@ function selectMinCompressedTokenAccountsForTransfer(accounts, transferAmount) {
|
|
|
5170
5637
|
const selectedAccounts = [];
|
|
5171
5638
|
accounts.sort((a, b) => b.parsed.amount.cmp(a.parsed.amount));
|
|
5172
5639
|
for (const account of accounts) {
|
|
5173
|
-
if (accumulatedAmount.gte(stateless_js.bn(transferAmount)))
|
|
5174
|
-
break;
|
|
5640
|
+
if (accumulatedAmount.gte(stateless_js.bn(transferAmount))) break;
|
|
5175
5641
|
accumulatedAmount = accumulatedAmount.add(account.parsed.amount);
|
|
5176
|
-
accumulatedLamports = accumulatedLamports.add(
|
|
5642
|
+
accumulatedLamports = accumulatedLamports.add(
|
|
5643
|
+
account.compressedAccount.lamports,
|
|
5644
|
+
);
|
|
5177
5645
|
selectedAccounts.push(account);
|
|
5178
5646
|
}
|
|
5179
5647
|
if (accumulatedAmount.lt(stateless_js.bn(transferAmount))) {
|
|
5180
|
-
throw new Error(
|
|
5648
|
+
throw new Error(
|
|
5649
|
+
`Not enough balance for transfer. Required: ${transferAmount.toString()}, available: ${accumulatedAmount.toString()}`,
|
|
5650
|
+
);
|
|
5181
5651
|
}
|
|
5182
5652
|
return [
|
|
5183
5653
|
selectedAccounts,
|
|
@@ -5204,16 +5674,34 @@ function selectMinCompressedTokenAccountsForTransfer(accounts, transferAmount) {
|
|
|
5204
5674
|
*
|
|
5205
5675
|
* @return Signature of the confirmed transaction
|
|
5206
5676
|
*/
|
|
5207
|
-
async function decompress(
|
|
5208
|
-
|
|
5209
|
-
|
|
5677
|
+
async function decompress(
|
|
5678
|
+
rpc,
|
|
5679
|
+
payer,
|
|
5680
|
+
mint,
|
|
5681
|
+
amount,
|
|
5682
|
+
owner,
|
|
5683
|
+
toAddress,
|
|
5684
|
+
/// TODO: allow multiple
|
|
5685
|
+
merkleTree,
|
|
5686
|
+
confirmOptions,
|
|
5687
|
+
) {
|
|
5210
5688
|
amount = stateless_js.bn(amount);
|
|
5211
|
-
const compressedTokenAccounts = await rpc.getCompressedTokenAccountsByOwner(
|
|
5212
|
-
|
|
5213
|
-
|
|
5689
|
+
const compressedTokenAccounts = await rpc.getCompressedTokenAccountsByOwner(
|
|
5690
|
+
owner.publicKey,
|
|
5691
|
+
{
|
|
5692
|
+
mint,
|
|
5693
|
+
},
|
|
5694
|
+
);
|
|
5214
5695
|
/// TODO: consider using a different selection algorithm
|
|
5215
|
-
const [inputAccounts] = selectMinCompressedTokenAccountsForTransfer(
|
|
5216
|
-
|
|
5696
|
+
const [inputAccounts] = selectMinCompressedTokenAccountsForTransfer(
|
|
5697
|
+
compressedTokenAccounts.items,
|
|
5698
|
+
amount,
|
|
5699
|
+
);
|
|
5700
|
+
const proof = await rpc.getValidityProof(
|
|
5701
|
+
inputAccounts.map(account =>
|
|
5702
|
+
stateless_js.bn(account.compressedAccount.hash),
|
|
5703
|
+
),
|
|
5704
|
+
);
|
|
5217
5705
|
const ix = await CompressedTokenProgram.decompress({
|
|
5218
5706
|
payer: payer.publicKey,
|
|
5219
5707
|
inputCompressedTokenAccounts: inputAccounts,
|
|
@@ -5225,8 +5713,22 @@ merkleTree, confirmOptions) {
|
|
|
5225
5713
|
});
|
|
5226
5714
|
const { blockhash } = await rpc.getLatestBlockhash();
|
|
5227
5715
|
const additionalSigners = stateless_js.dedupeSigner(payer, [owner]);
|
|
5228
|
-
const signedTx = stateless_js.buildAndSignTx(
|
|
5229
|
-
|
|
5716
|
+
const signedTx = stateless_js.buildAndSignTx(
|
|
5717
|
+
[
|
|
5718
|
+
web3_js.ComputeBudgetProgram.setComputeUnitLimit({
|
|
5719
|
+
units: 1000000,
|
|
5720
|
+
}),
|
|
5721
|
+
ix,
|
|
5722
|
+
],
|
|
5723
|
+
payer,
|
|
5724
|
+
blockhash,
|
|
5725
|
+
additionalSigners,
|
|
5726
|
+
);
|
|
5727
|
+
const txId = await stateless_js.sendAndConfirmTx(
|
|
5728
|
+
rpc,
|
|
5729
|
+
signedTx,
|
|
5730
|
+
confirmOptions,
|
|
5731
|
+
);
|
|
5230
5732
|
return txId;
|
|
5231
5733
|
}
|
|
5232
5734
|
|
|
@@ -5242,8 +5744,16 @@ merkleTree, confirmOptions) {
|
|
|
5242
5744
|
*
|
|
5243
5745
|
* @return Address of the new mint and the transaction signature
|
|
5244
5746
|
*/
|
|
5245
|
-
async function createMint(
|
|
5246
|
-
|
|
5747
|
+
async function createMint(
|
|
5748
|
+
rpc,
|
|
5749
|
+
payer,
|
|
5750
|
+
mintAuthority,
|
|
5751
|
+
decimals,
|
|
5752
|
+
keypair = web3_js.Keypair.generate(),
|
|
5753
|
+
confirmOptions,
|
|
5754
|
+
) {
|
|
5755
|
+
const rentExemptBalance =
|
|
5756
|
+
await rpc.getMinimumBalanceForRentExemption(MINT_SIZE);
|
|
5247
5757
|
const ixs = await CompressedTokenProgram.createMint({
|
|
5248
5758
|
feePayer: payer.publicKey,
|
|
5249
5759
|
mint: keypair.publicKey,
|
|
@@ -5254,7 +5764,12 @@ async function createMint(rpc, payer, mintAuthority, decimals, keypair = web3_js
|
|
|
5254
5764
|
});
|
|
5255
5765
|
const { blockhash } = await rpc.getLatestBlockhash();
|
|
5256
5766
|
const additionalSigners = stateless_js.dedupeSigner(payer, [keypair]);
|
|
5257
|
-
const tx = stateless_js.buildAndSignTx(
|
|
5767
|
+
const tx = stateless_js.buildAndSignTx(
|
|
5768
|
+
ixs,
|
|
5769
|
+
payer,
|
|
5770
|
+
blockhash,
|
|
5771
|
+
additionalSigners,
|
|
5772
|
+
);
|
|
5258
5773
|
const txId = await stateless_js.sendAndConfirmTx(rpc, tx, confirmOptions);
|
|
5259
5774
|
return { mint: keypair.publicKey, transactionSignature: txId };
|
|
5260
5775
|
}
|
|
@@ -5276,7 +5791,16 @@ async function createMint(rpc, payer, mintAuthority, decimals, keypair = web3_js
|
|
|
5276
5791
|
*
|
|
5277
5792
|
* @return Signature of the confirmed transaction
|
|
5278
5793
|
*/
|
|
5279
|
-
async function mintTo(
|
|
5794
|
+
async function mintTo(
|
|
5795
|
+
rpc,
|
|
5796
|
+
payer,
|
|
5797
|
+
mint,
|
|
5798
|
+
destination,
|
|
5799
|
+
authority,
|
|
5800
|
+
amount,
|
|
5801
|
+
merkleTree,
|
|
5802
|
+
confirmOptions,
|
|
5803
|
+
) {
|
|
5280
5804
|
const additionalSigners = stateless_js.dedupeSigner(payer, [authority]);
|
|
5281
5805
|
const ix = await CompressedTokenProgram.mintTo({
|
|
5282
5806
|
feePayer: payer.publicKey,
|
|
@@ -5287,11 +5811,98 @@ async function mintTo(rpc, payer, mint, destination, authority, amount, merkleTr
|
|
|
5287
5811
|
merkleTree,
|
|
5288
5812
|
});
|
|
5289
5813
|
const { blockhash } = await rpc.getLatestBlockhash();
|
|
5290
|
-
const tx = stateless_js.buildAndSignTx(
|
|
5814
|
+
const tx = stateless_js.buildAndSignTx(
|
|
5815
|
+
[
|
|
5816
|
+
web3_js.ComputeBudgetProgram.setComputeUnitLimit({
|
|
5817
|
+
units: 1000000,
|
|
5818
|
+
}),
|
|
5819
|
+
ix,
|
|
5820
|
+
],
|
|
5821
|
+
payer,
|
|
5822
|
+
blockhash,
|
|
5823
|
+
additionalSigners,
|
|
5824
|
+
);
|
|
5291
5825
|
const txId = await stateless_js.sendAndConfirmTx(rpc, tx, confirmOptions);
|
|
5292
5826
|
return txId;
|
|
5293
5827
|
}
|
|
5294
5828
|
|
|
5829
|
+
/**
|
|
5830
|
+
* Merge multiple compressed token accounts for a given mint into a single
|
|
5831
|
+
* account
|
|
5832
|
+
*
|
|
5833
|
+
* @param rpc RPC to use
|
|
5834
|
+
* @param payer Payer of the transaction fees
|
|
5835
|
+
* @param mint Public key of the token's mint
|
|
5836
|
+
* @param owner Owner of the token accounts to be merged
|
|
5837
|
+
* @param merkleTree Optional merkle tree for compressed tokens
|
|
5838
|
+
* @param confirmOptions Options for confirming the transaction
|
|
5839
|
+
*
|
|
5840
|
+
* @return Array of transaction signatures
|
|
5841
|
+
*/
|
|
5842
|
+
async function mergeTokenAccounts(
|
|
5843
|
+
rpc,
|
|
5844
|
+
payer,
|
|
5845
|
+
mint,
|
|
5846
|
+
owner,
|
|
5847
|
+
merkleTree,
|
|
5848
|
+
confirmOptions,
|
|
5849
|
+
) {
|
|
5850
|
+
const compressedTokenAccounts = await rpc.getCompressedTokenAccountsByOwner(
|
|
5851
|
+
owner.publicKey,
|
|
5852
|
+
{ mint },
|
|
5853
|
+
);
|
|
5854
|
+
if (compressedTokenAccounts.items.length === 0) {
|
|
5855
|
+
throw new Error(
|
|
5856
|
+
`No compressed token accounts found for mint ${mint.toBase58()}`,
|
|
5857
|
+
);
|
|
5858
|
+
}
|
|
5859
|
+
if (compressedTokenAccounts.items.length >= 6) {
|
|
5860
|
+
throw new Error(
|
|
5861
|
+
`Too many compressed token accounts used for mint ${mint.toBase58()}`,
|
|
5862
|
+
);
|
|
5863
|
+
}
|
|
5864
|
+
const instructions = [
|
|
5865
|
+
web3_js.ComputeBudgetProgram.setComputeUnitLimit({ units: 1000000 }),
|
|
5866
|
+
];
|
|
5867
|
+
for (
|
|
5868
|
+
let i = 0;
|
|
5869
|
+
i < compressedTokenAccounts.items.slice(0, 6).length;
|
|
5870
|
+
i += 3
|
|
5871
|
+
) {
|
|
5872
|
+
const batch = compressedTokenAccounts.items.slice(i, i + 3);
|
|
5873
|
+
const proof = await rpc.getValidityProof(
|
|
5874
|
+
batch.map(account =>
|
|
5875
|
+
stateless_js.bn(account.compressedAccount.hash),
|
|
5876
|
+
),
|
|
5877
|
+
);
|
|
5878
|
+
const batchInstructions =
|
|
5879
|
+
await CompressedTokenProgram.mergeTokenAccounts({
|
|
5880
|
+
payer: payer.publicKey,
|
|
5881
|
+
owner: owner.publicKey,
|
|
5882
|
+
mint,
|
|
5883
|
+
inputCompressedTokenAccounts: batch,
|
|
5884
|
+
outputStateTree: merkleTree,
|
|
5885
|
+
recentValidityProof: proof.compressedProof,
|
|
5886
|
+
recentInputStateRootIndices: proof.rootIndices,
|
|
5887
|
+
});
|
|
5888
|
+
instructions.push(...batchInstructions);
|
|
5889
|
+
}
|
|
5890
|
+
const { blockhash } = await rpc.getLatestBlockhash();
|
|
5891
|
+
const additionalSigners = stateless_js.dedupeSigner(payer, [owner]);
|
|
5892
|
+
const signedTx = stateless_js.buildAndSignTx(
|
|
5893
|
+
instructions,
|
|
5894
|
+
payer,
|
|
5895
|
+
blockhash,
|
|
5896
|
+
additionalSigners,
|
|
5897
|
+
);
|
|
5898
|
+
const txId = await stateless_js.sendAndConfirmTx(
|
|
5899
|
+
rpc,
|
|
5900
|
+
signedTx,
|
|
5901
|
+
confirmOptions,
|
|
5902
|
+
);
|
|
5903
|
+
return txId;
|
|
5904
|
+
}
|
|
5905
|
+
|
|
5295
5906
|
/**
|
|
5296
5907
|
* Register an existing mint with the CompressedToken program
|
|
5297
5908
|
*
|
|
@@ -5327,23 +5938,50 @@ async function createTokenPool(rpc, payer, mintAddress, confirmOptions) {
|
|
|
5327
5938
|
*
|
|
5328
5939
|
* @return Transaction signatures and the address of the created lookup table
|
|
5329
5940
|
*/
|
|
5330
|
-
async function createTokenProgramLookupTable(
|
|
5941
|
+
async function createTokenProgramLookupTable(
|
|
5942
|
+
rpc,
|
|
5943
|
+
payer,
|
|
5944
|
+
authority,
|
|
5945
|
+
mints,
|
|
5946
|
+
additionalAccounts,
|
|
5947
|
+
) {
|
|
5331
5948
|
const recentSlot = await rpc.getSlot('finalized');
|
|
5332
|
-
const { instructions, address } =
|
|
5333
|
-
|
|
5334
|
-
|
|
5335
|
-
|
|
5336
|
-
|
|
5337
|
-
|
|
5338
|
-
|
|
5949
|
+
const { instructions, address } =
|
|
5950
|
+
await CompressedTokenProgram.createTokenProgramLookupTable({
|
|
5951
|
+
payer: payer.publicKey,
|
|
5952
|
+
authority: authority.publicKey,
|
|
5953
|
+
mints,
|
|
5954
|
+
remainingAccounts: additionalAccounts,
|
|
5955
|
+
recentSlot,
|
|
5956
|
+
});
|
|
5339
5957
|
const additionalSigners = stateless_js.dedupeSigner(payer, [authority]);
|
|
5340
5958
|
const blockhashCtx = await rpc.getLatestBlockhash();
|
|
5341
|
-
const signedTx = stateless_js.buildAndSignTx(
|
|
5959
|
+
const signedTx = stateless_js.buildAndSignTx(
|
|
5960
|
+
[instructions[0]],
|
|
5961
|
+
payer,
|
|
5962
|
+
blockhashCtx.blockhash,
|
|
5963
|
+
additionalSigners,
|
|
5964
|
+
);
|
|
5342
5965
|
/// Must wait for the first instruction to be finalized.
|
|
5343
|
-
const txId = await stateless_js.sendAndConfirmTx(
|
|
5966
|
+
const txId = await stateless_js.sendAndConfirmTx(
|
|
5967
|
+
rpc,
|
|
5968
|
+
signedTx,
|
|
5969
|
+
{ commitment: 'finalized' },
|
|
5970
|
+
blockhashCtx,
|
|
5971
|
+
);
|
|
5344
5972
|
const blockhashCtx2 = await rpc.getLatestBlockhash();
|
|
5345
|
-
const signedTx2 = stateless_js.buildAndSignTx(
|
|
5346
|
-
|
|
5973
|
+
const signedTx2 = stateless_js.buildAndSignTx(
|
|
5974
|
+
[instructions[1]],
|
|
5975
|
+
payer,
|
|
5976
|
+
blockhashCtx2.blockhash,
|
|
5977
|
+
additionalSigners,
|
|
5978
|
+
);
|
|
5979
|
+
const txId2 = await stateless_js.sendAndConfirmTx(
|
|
5980
|
+
rpc,
|
|
5981
|
+
signedTx2,
|
|
5982
|
+
{ commitment: 'finalized' },
|
|
5983
|
+
blockhashCtx2,
|
|
5984
|
+
);
|
|
5347
5985
|
return { txIds: [txId, txId2], address };
|
|
5348
5986
|
}
|
|
5349
5987
|
|
|
@@ -5360,10 +5998,12 @@ exports.createTokenPool = createTokenPool;
|
|
|
5360
5998
|
exports.createTokenProgramLookupTable = createTokenProgramLookupTable;
|
|
5361
5999
|
exports.createTransferOutputState = createTransferOutputState;
|
|
5362
6000
|
exports.decompress = decompress;
|
|
6001
|
+
exports.mergeTokenAccounts = mergeTokenAccounts;
|
|
5363
6002
|
exports.mintTo = mintTo;
|
|
5364
6003
|
exports.packCompressedTokenAccounts = packCompressedTokenAccounts;
|
|
5365
6004
|
exports.parseTokenData = parseTokenData;
|
|
5366
|
-
exports.selectMinCompressedTokenAccountsForTransfer =
|
|
6005
|
+
exports.selectMinCompressedTokenAccountsForTransfer =
|
|
6006
|
+
selectMinCompressedTokenAccountsForTransfer;
|
|
5367
6007
|
exports.sumUpTokenAmount = sumUpTokenAmount;
|
|
5368
6008
|
exports.transfer = transfer;
|
|
5369
6009
|
exports.validateSameTokenOwner = validateSameTokenOwner;
|