@lightprotocol/compressed-token 0.13.0 → 0.14.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 +2092 -3007
- package/dist/cjs/node/index.cjs +507 -1190
- package/dist/es/browser/index.js +2054 -2934
- package/dist/types/index.d.ts +101 -248
- package/package.json +2 -2
package/dist/cjs/node/index.cjs
CHANGED
|
@@ -1062,8 +1062,7 @@ const IDL = {
|
|
|
1062
1062
|
name: 'inputCompressedAccountsWithMerkleContext',
|
|
1063
1063
|
type: {
|
|
1064
1064
|
vec: {
|
|
1065
|
-
defined:
|
|
1066
|
-
'PackedCompressedAccountWithMerkleContext',
|
|
1065
|
+
defined: 'PackedCompressedAccountWithMerkleContext',
|
|
1067
1066
|
},
|
|
1068
1067
|
},
|
|
1069
1068
|
},
|
|
@@ -1071,8 +1070,7 @@ const IDL = {
|
|
|
1071
1070
|
name: 'outputCompressedAccounts',
|
|
1072
1071
|
type: {
|
|
1073
1072
|
vec: {
|
|
1074
|
-
defined:
|
|
1075
|
-
'OutputCompressedAccountWithPackedContext',
|
|
1073
|
+
defined: 'OutputCompressedAccountWithPackedContext',
|
|
1076
1074
|
},
|
|
1077
1075
|
},
|
|
1078
1076
|
},
|
|
@@ -1128,8 +1126,7 @@ const IDL = {
|
|
|
1128
1126
|
name: 'inputCompressedAccountsWithMerkleContext',
|
|
1129
1127
|
type: {
|
|
1130
1128
|
vec: {
|
|
1131
|
-
defined:
|
|
1132
|
-
'PackedCompressedAccountWithMerkleContext',
|
|
1129
|
+
defined: 'PackedCompressedAccountWithMerkleContext',
|
|
1133
1130
|
},
|
|
1134
1131
|
},
|
|
1135
1132
|
},
|
|
@@ -1137,8 +1134,7 @@ const IDL = {
|
|
|
1137
1134
|
name: 'outputCompressedAccounts',
|
|
1138
1135
|
type: {
|
|
1139
1136
|
vec: {
|
|
1140
|
-
defined:
|
|
1141
|
-
'OutputCompressedAccountWithPackedContext',
|
|
1137
|
+
defined: 'OutputCompressedAccountWithPackedContext',
|
|
1142
1138
|
},
|
|
1143
1139
|
},
|
|
1144
1140
|
},
|
|
@@ -1383,8 +1379,7 @@ const IDL = {
|
|
|
1383
1379
|
name: 'outputCompressedAccounts',
|
|
1384
1380
|
type: {
|
|
1385
1381
|
vec: {
|
|
1386
|
-
defined:
|
|
1387
|
-
'OutputCompressedAccountWithPackedContext',
|
|
1382
|
+
defined: 'OutputCompressedAccountWithPackedContext',
|
|
1388
1383
|
},
|
|
1389
1384
|
},
|
|
1390
1385
|
},
|
|
@@ -1587,37 +1582,20 @@ const IDL = {
|
|
|
1587
1582
|
* Packs Compressed Token Accounts.
|
|
1588
1583
|
*/
|
|
1589
1584
|
function packCompressedTokenAccounts(params) {
|
|
1590
|
-
const {
|
|
1591
|
-
inputCompressedTokenAccounts,
|
|
1592
|
-
outputStateTrees,
|
|
1593
|
-
remainingAccounts = [],
|
|
1594
|
-
rootIndices,
|
|
1595
|
-
tokenTransferOutputs,
|
|
1596
|
-
} = params;
|
|
1585
|
+
const { inputCompressedTokenAccounts, outputStateTrees, remainingAccounts = [], rootIndices, tokenTransferOutputs, } = params;
|
|
1597
1586
|
const _remainingAccounts = remainingAccounts.slice();
|
|
1598
1587
|
let delegateIndex = null;
|
|
1599
|
-
if (
|
|
1600
|
-
inputCompressedTokenAccounts.
|
|
1601
|
-
inputCompressedTokenAccounts[0].parsed.delegate
|
|
1602
|
-
) {
|
|
1603
|
-
delegateIndex = stateless_js.getIndexOrAdd(
|
|
1604
|
-
_remainingAccounts,
|
|
1605
|
-
inputCompressedTokenAccounts[0].parsed.delegate,
|
|
1606
|
-
);
|
|
1588
|
+
if (inputCompressedTokenAccounts.length > 0 &&
|
|
1589
|
+
inputCompressedTokenAccounts[0].parsed.delegate) {
|
|
1590
|
+
delegateIndex = stateless_js.getIndexOrAdd(_remainingAccounts, inputCompressedTokenAccounts[0].parsed.delegate);
|
|
1607
1591
|
}
|
|
1608
1592
|
/// TODO: move pubkeyArray to remainingAccounts
|
|
1609
1593
|
/// Currently just packs 'delegate' to pubkeyArray
|
|
1610
1594
|
const packedInputTokenData = [];
|
|
1611
1595
|
/// pack inputs
|
|
1612
1596
|
inputCompressedTokenAccounts.forEach((account, index) => {
|
|
1613
|
-
const merkleTreePubkeyIndex = stateless_js.getIndexOrAdd(
|
|
1614
|
-
|
|
1615
|
-
account.compressedAccount.merkleTree,
|
|
1616
|
-
);
|
|
1617
|
-
const nullifierQueuePubkeyIndex = stateless_js.getIndexOrAdd(
|
|
1618
|
-
_remainingAccounts,
|
|
1619
|
-
account.compressedAccount.nullifierQueue,
|
|
1620
|
-
);
|
|
1597
|
+
const merkleTreePubkeyIndex = stateless_js.getIndexOrAdd(_remainingAccounts, account.compressedAccount.merkleTree);
|
|
1598
|
+
const nullifierQueuePubkeyIndex = stateless_js.getIndexOrAdd(_remainingAccounts, account.compressedAccount.nullifierQueue);
|
|
1621
1599
|
packedInputTokenData.push({
|
|
1622
1600
|
amount: account.parsed.amount,
|
|
1623
1601
|
delegateIndex,
|
|
@@ -1635,27 +1613,15 @@ function packCompressedTokenAccounts(params) {
|
|
|
1635
1613
|
});
|
|
1636
1614
|
});
|
|
1637
1615
|
/// pack output state trees
|
|
1638
|
-
const paddedOutputStateMerkleTrees = stateless_js.padOutputStateMerkleTrees(
|
|
1639
|
-
outputStateTrees,
|
|
1640
|
-
tokenTransferOutputs.length,
|
|
1641
|
-
inputCompressedTokenAccounts.map(acc => acc.compressedAccount),
|
|
1642
|
-
);
|
|
1616
|
+
const paddedOutputStateMerkleTrees = stateless_js.padOutputStateMerkleTrees(outputStateTrees, tokenTransferOutputs.length, inputCompressedTokenAccounts.map(acc => acc.compressedAccount));
|
|
1643
1617
|
const packedOutputTokenData = [];
|
|
1644
1618
|
paddedOutputStateMerkleTrees.forEach((account, index) => {
|
|
1645
1619
|
var _a;
|
|
1646
|
-
const merkleTreeIndex = stateless_js.getIndexOrAdd(
|
|
1647
|
-
_remainingAccounts,
|
|
1648
|
-
account,
|
|
1649
|
-
);
|
|
1620
|
+
const merkleTreeIndex = stateless_js.getIndexOrAdd(_remainingAccounts, account);
|
|
1650
1621
|
packedOutputTokenData.push({
|
|
1651
1622
|
owner: tokenTransferOutputs[index].owner,
|
|
1652
1623
|
amount: tokenTransferOutputs[index].amount,
|
|
1653
|
-
lamports: (
|
|
1654
|
-
(_a = tokenTransferOutputs[index].lamports) === null ||
|
|
1655
|
-
_a === void 0
|
|
1656
|
-
? void 0
|
|
1657
|
-
: _a.eq(stateless_js.bn(0))
|
|
1658
|
-
)
|
|
1624
|
+
lamports: ((_a = tokenTransferOutputs[index].lamports) === null || _a === void 0 ? void 0 : _a.eq(stateless_js.bn(0)))
|
|
1659
1625
|
? null
|
|
1660
1626
|
: tokenTransferOutputs[index].lamports,
|
|
1661
1627
|
merkleTreeIndex,
|
|
@@ -1663,7 +1629,7 @@ function packCompressedTokenAccounts(params) {
|
|
|
1663
1629
|
});
|
|
1664
1630
|
});
|
|
1665
1631
|
// to meta
|
|
1666
|
-
const remainingAccountMetas = _remainingAccounts.map(account => ({
|
|
1632
|
+
const remainingAccountMetas = _remainingAccounts.map((account) => ({
|
|
1667
1633
|
pubkey: account,
|
|
1668
1634
|
isWritable: true,
|
|
1669
1635
|
isSigner: false,
|
|
@@ -1680,15 +1646,11 @@ const CPI_AUTHORITY_SEED = Buffer.from('cpi_authority');
|
|
|
1680
1646
|
const SPL_TOKEN_MINT_RENT_EXEMPT_BALANCE = 1461600;
|
|
1681
1647
|
|
|
1682
1648
|
/** Address of the SPL Token program */
|
|
1683
|
-
const TOKEN_PROGRAM_ID = new web3_js.PublicKey(
|
|
1684
|
-
'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA',
|
|
1685
|
-
);
|
|
1649
|
+
const TOKEN_PROGRAM_ID = new web3_js.PublicKey('TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA');
|
|
1686
1650
|
/** Address of the SPL Token 2022 program */
|
|
1687
1651
|
new web3_js.PublicKey('TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb');
|
|
1688
1652
|
/** Address of the SPL Associated Token Account program */
|
|
1689
|
-
const ASSOCIATED_TOKEN_PROGRAM_ID = new web3_js.PublicKey(
|
|
1690
|
-
'ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL',
|
|
1691
|
-
);
|
|
1653
|
+
const ASSOCIATED_TOKEN_PROGRAM_ID = new web3_js.PublicKey('ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL');
|
|
1692
1654
|
/** Address of the special mint for wrapped native SOL in spl-token */
|
|
1693
1655
|
new web3_js.PublicKey('So11111111111111111111111111111111111111112');
|
|
1694
1656
|
/** Address of the special mint for wrapped native SOL in spl-token-2022 */
|
|
@@ -1718,88 +1680,9 @@ var Layout$1 = {};
|
|
|
1718
1680
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
1719
1681
|
* THE SOFTWARE.
|
|
1720
1682
|
*/
|
|
1721
|
-
Object.defineProperty(Layout$1,
|
|
1722
|
-
Layout$1.s16 =
|
|
1723
|
-
|
|
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;
|
|
1683
|
+
Object.defineProperty(Layout$1, "__esModule", { value: true });
|
|
1684
|
+
Layout$1.s16 = Layout$1.s8 = Layout$1.nu64be = Layout$1.u48be = Layout$1.u40be = Layout$1.u32be = Layout$1.u24be = Layout$1.u16be = Layout$1.nu64 = Layout$1.u48 = Layout$1.u40 = u32 = Layout$1.u32 = Layout$1.u24 = Layout$1.u16 = u8 = Layout$1.u8 = Layout$1.offset = Layout$1.greedy = Layout$1.Constant = Layout$1.UTF8 = Layout$1.CString = Layout$1.Blob = Layout$1.Boolean = Layout$1.BitField = Layout$1.BitStructure = Layout$1.VariantLayout = Layout$1.Union = Layout$1.UnionLayoutDiscriminator = Layout$1.UnionDiscriminator = Layout$1.Structure = Layout$1.Sequence = Layout$1.DoubleBE = Layout$1.Double = Layout$1.FloatBE = Layout$1.Float = Layout$1.NearInt64BE = Layout$1.NearInt64 = Layout$1.NearUInt64BE = Layout$1.NearUInt64 = Layout$1.IntBE = Layout$1.Int = Layout$1.UIntBE = Layout$1.UInt = Layout$1.OffsetLayout = Layout$1.GreedyCount = Layout$1.ExternalLayout = Layout$1.bindConstructorLayout = Layout$1.nameWithProperty = Layout_2 = Layout$1.Layout = Layout$1.uint8ArrayToBuffer = Layout$1.checkUint8Array = void 0;
|
|
1685
|
+
Layout$1.constant = Layout$1.utf8 = Layout$1.cstr = blob = Layout$1.blob = Layout$1.unionLayoutDiscriminator = Layout$1.union = Layout$1.seq = Layout$1.bits = struct = Layout$1.struct = Layout$1.f64be = Layout$1.f64 = Layout$1.f32be = Layout$1.f32 = Layout$1.ns64be = Layout$1.s48be = Layout$1.s40be = Layout$1.s32be = Layout$1.s24be = Layout$1.s16be = Layout$1.ns64 = Layout$1.s48 = Layout$1.s40 = Layout$1.s32 = Layout$1.s24 = void 0;
|
|
1803
1686
|
const buffer_1 = require$$0;
|
|
1804
1687
|
/* Check if a value is a Uint8Array.
|
|
1805
1688
|
*
|
|
@@ -1945,7 +1828,7 @@ class Layout {
|
|
|
1945
1828
|
return undefined;
|
|
1946
1829
|
}
|
|
1947
1830
|
}
|
|
1948
|
-
var Layout_2 =
|
|
1831
|
+
var Layout_2 = Layout$1.Layout = Layout;
|
|
1949
1832
|
/* Provide text that carries a name (such as for a function that will
|
|
1950
1833
|
* be throwing an error) annotated with the property of a given layout
|
|
1951
1834
|
* (such as one for which the value was unacceptable).
|
|
@@ -1994,7 +1877,7 @@ function bindConstructorLayout(Class, layout) {
|
|
|
1994
1877
|
if (Object.prototype.hasOwnProperty.call(Class, 'layout_')) {
|
|
1995
1878
|
throw new Error('Class is already bound to a layout');
|
|
1996
1879
|
}
|
|
1997
|
-
if (!(layout && layout instanceof Layout)) {
|
|
1880
|
+
if (!(layout && (layout instanceof Layout))) {
|
|
1998
1881
|
throw new TypeError('layout must be a Layout');
|
|
1999
1882
|
}
|
|
2000
1883
|
if (Object.prototype.hasOwnProperty.call(layout, 'boundConstructor_')) {
|
|
@@ -2002,7 +1885,7 @@ function bindConstructorLayout(Class, layout) {
|
|
|
2002
1885
|
}
|
|
2003
1886
|
Class.layout_ = layout;
|
|
2004
1887
|
layout.boundConstructor_ = Class;
|
|
2005
|
-
layout.makeDestinationObject = () => new Class();
|
|
1888
|
+
layout.makeDestinationObject = (() => new Class());
|
|
2006
1889
|
Object.defineProperty(Class.prototype, 'encode', {
|
|
2007
1890
|
value(b, offset) {
|
|
2008
1891
|
return layout.encode(this, b, offset);
|
|
@@ -2072,7 +1955,7 @@ Layout$1.ExternalLayout = ExternalLayout;
|
|
|
2072
1955
|
*/
|
|
2073
1956
|
class GreedyCount extends ExternalLayout {
|
|
2074
1957
|
constructor(elementSpan = 1, property) {
|
|
2075
|
-
if (!Number.isInteger(elementSpan) || 0 >= elementSpan) {
|
|
1958
|
+
if ((!Number.isInteger(elementSpan)) || (0 >= elementSpan)) {
|
|
2076
1959
|
throw new TypeError('elementSpan must be a (positive) integer');
|
|
2077
1960
|
}
|
|
2078
1961
|
super(-1, property);
|
|
@@ -2138,7 +2021,8 @@ class OffsetLayout extends ExternalLayout {
|
|
|
2138
2021
|
}
|
|
2139
2022
|
/** @override */
|
|
2140
2023
|
isCount() {
|
|
2141
|
-
return this.layout instanceof UInt
|
|
2024
|
+
return ((this.layout instanceof UInt)
|
|
2025
|
+
|| (this.layout instanceof UIntBE));
|
|
2142
2026
|
}
|
|
2143
2027
|
/** @override */
|
|
2144
2028
|
decode(b, offset = 0) {
|
|
@@ -2291,7 +2175,7 @@ const V2E32 = Math.pow(2, 32);
|
|
|
2291
2175
|
* non-negative. */
|
|
2292
2176
|
function divmodInt64(src) {
|
|
2293
2177
|
const hi32 = Math.floor(src / V2E32);
|
|
2294
|
-
const lo32 = src - hi32 * V2E32;
|
|
2178
|
+
const lo32 = src - (hi32 * V2E32);
|
|
2295
2179
|
return { hi32, lo32 };
|
|
2296
2180
|
}
|
|
2297
2181
|
/* Reconstruct Number from quotient and non-negative remainder */
|
|
@@ -2548,19 +2432,14 @@ class Sequence extends Layout {
|
|
|
2548
2432
|
if (!(elementLayout instanceof Layout)) {
|
|
2549
2433
|
throw new TypeError('elementLayout must be a Layout');
|
|
2550
2434
|
}
|
|
2551
|
-
if (
|
|
2552
|
-
|
|
2553
|
-
|
|
2554
|
-
|
|
2555
|
-
)
|
|
2556
|
-
) {
|
|
2557
|
-
throw new TypeError(
|
|
2558
|
-
'count must be non-negative integer ' +
|
|
2559
|
-
'or an unsigned integer ExternalLayout',
|
|
2560
|
-
);
|
|
2435
|
+
if (!(((count instanceof ExternalLayout) && count.isCount())
|
|
2436
|
+
|| (Number.isInteger(count) && (0 <= count)))) {
|
|
2437
|
+
throw new TypeError('count must be non-negative integer '
|
|
2438
|
+
+ 'or an unsigned integer ExternalLayout');
|
|
2561
2439
|
}
|
|
2562
2440
|
let span = -1;
|
|
2563
|
-
if (!(count instanceof ExternalLayout)
|
|
2441
|
+
if ((!(count instanceof ExternalLayout))
|
|
2442
|
+
&& (0 < elementLayout.span)) {
|
|
2564
2443
|
span = count * elementLayout.span;
|
|
2565
2444
|
}
|
|
2566
2445
|
super(span, property);
|
|
@@ -2585,7 +2464,8 @@ class Sequence extends Layout {
|
|
|
2585
2464
|
}
|
|
2586
2465
|
if (0 < this.elementLayout.span) {
|
|
2587
2466
|
span = count * this.elementLayout.span;
|
|
2588
|
-
}
|
|
2467
|
+
}
|
|
2468
|
+
else {
|
|
2589
2469
|
let idx = 0;
|
|
2590
2470
|
while (idx < count) {
|
|
2591
2471
|
span += this.elementLayout.getSpan(b, offset + span);
|
|
@@ -2665,30 +2545,27 @@ Layout$1.Sequence = Sequence;
|
|
|
2665
2545
|
*/
|
|
2666
2546
|
class Structure extends Layout {
|
|
2667
2547
|
constructor(fields, property, decodePrefixes) {
|
|
2668
|
-
if (
|
|
2669
|
-
|
|
2670
|
-
Array.isArray(fields) &&
|
|
2671
|
-
fields.reduce((acc, v) => acc && v instanceof Layout, true)
|
|
2672
|
-
)
|
|
2673
|
-
) {
|
|
2548
|
+
if (!(Array.isArray(fields)
|
|
2549
|
+
&& fields.reduce((acc, v) => acc && (v instanceof Layout), true))) {
|
|
2674
2550
|
throw new TypeError('fields must be array of Layout instances');
|
|
2675
2551
|
}
|
|
2676
|
-
if ('boolean' === typeof property
|
|
2552
|
+
if (('boolean' === typeof property)
|
|
2553
|
+
&& (undefined === decodePrefixes)) {
|
|
2677
2554
|
decodePrefixes = property;
|
|
2678
2555
|
property = undefined;
|
|
2679
2556
|
}
|
|
2680
2557
|
/* Verify absence of unnamed variable-length fields. */
|
|
2681
2558
|
for (const fd of fields) {
|
|
2682
|
-
if (0 > fd.span
|
|
2683
|
-
|
|
2684
|
-
|
|
2685
|
-
);
|
|
2559
|
+
if ((0 > fd.span)
|
|
2560
|
+
&& (undefined === fd.property)) {
|
|
2561
|
+
throw new Error('fields cannot contain unnamed variable-length layout');
|
|
2686
2562
|
}
|
|
2687
2563
|
}
|
|
2688
2564
|
let span = -1;
|
|
2689
2565
|
try {
|
|
2690
2566
|
span = fields.reduce((span, fd) => span + fd.getSpan(), 0);
|
|
2691
|
-
}
|
|
2567
|
+
}
|
|
2568
|
+
catch (e) {
|
|
2692
2569
|
// ignore error
|
|
2693
2570
|
}
|
|
2694
2571
|
super(span, property);
|
|
@@ -2726,7 +2603,8 @@ class Structure extends Layout {
|
|
|
2726
2603
|
offset += fsp;
|
|
2727
2604
|
return span + fsp;
|
|
2728
2605
|
}, 0);
|
|
2729
|
-
}
|
|
2606
|
+
}
|
|
2607
|
+
catch (e) {
|
|
2730
2608
|
throw new RangeError('indeterminate span');
|
|
2731
2609
|
}
|
|
2732
2610
|
return span;
|
|
@@ -2740,7 +2618,8 @@ class Structure extends Layout {
|
|
|
2740
2618
|
dest[fd.property] = fd.decode(b, offset);
|
|
2741
2619
|
}
|
|
2742
2620
|
offset += fd.getSpan(b, offset);
|
|
2743
|
-
if (this.decodePrefixes
|
|
2621
|
+
if (this.decodePrefixes
|
|
2622
|
+
&& (b.length === offset)) {
|
|
2744
2623
|
break;
|
|
2745
2624
|
}
|
|
2746
2625
|
}
|
|
@@ -2757,7 +2636,7 @@ class Structure extends Layout {
|
|
|
2757
2636
|
let lastWrote = 0;
|
|
2758
2637
|
for (const fd of this.fields) {
|
|
2759
2638
|
let span = fd.span;
|
|
2760
|
-
lastWrote = 0 < span ? span : 0;
|
|
2639
|
+
lastWrote = (0 < span) ? span : 0;
|
|
2761
2640
|
if (undefined !== fd.property) {
|
|
2762
2641
|
const fv = src[fd.property];
|
|
2763
2642
|
if (undefined !== fv) {
|
|
@@ -2776,13 +2655,14 @@ class Structure extends Layout {
|
|
|
2776
2655
|
* item may have had a dynamic length and we don't want to include
|
|
2777
2656
|
* the padding between it and the end of the space reserved for
|
|
2778
2657
|
* it. */
|
|
2779
|
-
return lastOffset + lastWrote - firstOffset;
|
|
2658
|
+
return (lastOffset + lastWrote) - firstOffset;
|
|
2780
2659
|
}
|
|
2781
2660
|
/** @override */
|
|
2782
2661
|
fromArray(values) {
|
|
2783
2662
|
const dest = this.makeDestinationObject();
|
|
2784
2663
|
for (const fd of this.fields) {
|
|
2785
|
-
if (undefined !== fd.property
|
|
2664
|
+
if ((undefined !== fd.property)
|
|
2665
|
+
&& (0 < values.length)) {
|
|
2786
2666
|
dest[fd.property] = values.shift();
|
|
2787
2667
|
}
|
|
2788
2668
|
}
|
|
@@ -2829,7 +2709,8 @@ class Structure extends Layout {
|
|
|
2829
2709
|
}
|
|
2830
2710
|
if (0 > fd.span) {
|
|
2831
2711
|
offset = -1;
|
|
2832
|
-
}
|
|
2712
|
+
}
|
|
2713
|
+
else if (0 <= offset) {
|
|
2833
2714
|
offset += fd.span;
|
|
2834
2715
|
}
|
|
2835
2716
|
}
|
|
@@ -2894,10 +2775,9 @@ Layout$1.UnionDiscriminator = UnionDiscriminator;
|
|
|
2894
2775
|
*/
|
|
2895
2776
|
class UnionLayoutDiscriminator extends UnionDiscriminator {
|
|
2896
2777
|
constructor(layout, property) {
|
|
2897
|
-
if (!(layout instanceof ExternalLayout
|
|
2898
|
-
|
|
2899
|
-
|
|
2900
|
-
);
|
|
2778
|
+
if (!((layout instanceof ExternalLayout)
|
|
2779
|
+
&& layout.isCount())) {
|
|
2780
|
+
throw new TypeError('layout must be an unsigned integer ExternalLayout');
|
|
2901
2781
|
}
|
|
2902
2782
|
super(property || layout.property || 'variant');
|
|
2903
2783
|
/** The {@link ExternalLayout} used to access the discriminator
|
|
@@ -2976,24 +2856,26 @@ Layout$1.UnionLayoutDiscriminator = UnionLayoutDiscriminator;
|
|
|
2976
2856
|
class Union extends Layout {
|
|
2977
2857
|
constructor(discr, defaultLayout, property) {
|
|
2978
2858
|
let discriminator;
|
|
2979
|
-
if (discr instanceof UInt
|
|
2980
|
-
|
|
2981
|
-
|
|
2982
|
-
|
|
2983
|
-
|
|
2859
|
+
if ((discr instanceof UInt)
|
|
2860
|
+
|| (discr instanceof UIntBE)) {
|
|
2861
|
+
discriminator = new UnionLayoutDiscriminator(new OffsetLayout(discr));
|
|
2862
|
+
}
|
|
2863
|
+
else if ((discr instanceof ExternalLayout)
|
|
2864
|
+
&& discr.isCount()) {
|
|
2984
2865
|
discriminator = new UnionLayoutDiscriminator(discr);
|
|
2985
|
-
}
|
|
2986
|
-
|
|
2987
|
-
|
|
2988
|
-
|
|
2989
|
-
|
|
2990
|
-
|
|
2866
|
+
}
|
|
2867
|
+
else if (!(discr instanceof UnionDiscriminator)) {
|
|
2868
|
+
throw new TypeError('discr must be a UnionDiscriminator '
|
|
2869
|
+
+ 'or an unsigned integer layout');
|
|
2870
|
+
}
|
|
2871
|
+
else {
|
|
2991
2872
|
discriminator = discr;
|
|
2992
2873
|
}
|
|
2993
2874
|
if (undefined === defaultLayout) {
|
|
2994
2875
|
defaultLayout = null;
|
|
2995
2876
|
}
|
|
2996
|
-
if (!(null === defaultLayout
|
|
2877
|
+
if (!((null === defaultLayout)
|
|
2878
|
+
|| (defaultLayout instanceof Layout))) {
|
|
2997
2879
|
throw new TypeError('defaultLayout must be null or a Layout');
|
|
2998
2880
|
}
|
|
2999
2881
|
if (null !== defaultLayout) {
|
|
@@ -3011,10 +2893,8 @@ class Union extends Layout {
|
|
|
3011
2893
|
let span = -1;
|
|
3012
2894
|
if (defaultLayout) {
|
|
3013
2895
|
span = defaultLayout.span;
|
|
3014
|
-
if (
|
|
3015
|
-
|
|
3016
|
-
(discr instanceof UInt || discr instanceof UIntBE)
|
|
3017
|
-
) {
|
|
2896
|
+
if ((0 <= span) && ((discr instanceof UInt)
|
|
2897
|
+
|| (discr instanceof UIntBE))) {
|
|
3018
2898
|
span += discriminator.layout.span;
|
|
3019
2899
|
}
|
|
3020
2900
|
}
|
|
@@ -3033,8 +2913,8 @@ class Union extends Layout {
|
|
|
3033
2913
|
*
|
|
3034
2914
|
* If `false` the discriminator is obtained from somewhere
|
|
3035
2915
|
* else. */
|
|
3036
|
-
this.usesPrefixDiscriminator =
|
|
3037
|
-
|
|
2916
|
+
this.usesPrefixDiscriminator = (discr instanceof UInt)
|
|
2917
|
+
|| (discr instanceof UIntBE);
|
|
3038
2918
|
/** The layout for non-discriminator content when the value of the
|
|
3039
2919
|
* discriminator is not recognized.
|
|
3040
2920
|
*
|
|
@@ -3099,9 +2979,7 @@ class Union extends Layout {
|
|
|
3099
2979
|
* determine the span. */
|
|
3100
2980
|
const vlo = this.getVariant(b, offset);
|
|
3101
2981
|
if (!vlo) {
|
|
3102
|
-
throw new Error(
|
|
3103
|
-
'unable to determine span for unrecognized variant',
|
|
3104
|
-
);
|
|
2982
|
+
throw new Error('unable to determine span for unrecognized variant');
|
|
3105
2983
|
}
|
|
3106
2984
|
return vlo.getSpan(b, offset);
|
|
3107
2985
|
}
|
|
@@ -3135,41 +3013,22 @@ class Union extends Layout {
|
|
|
3135
3013
|
* registered variant.
|
|
3136
3014
|
*/
|
|
3137
3015
|
defaultGetSourceVariant(src) {
|
|
3138
|
-
if (
|
|
3139
|
-
|
|
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
|
-
) {
|
|
3016
|
+
if (Object.prototype.hasOwnProperty.call(src, this.discriminator.property)) {
|
|
3017
|
+
if (this.defaultLayout && this.defaultLayout.property
|
|
3018
|
+
&& Object.prototype.hasOwnProperty.call(src, this.defaultLayout.property)) {
|
|
3152
3019
|
return undefined;
|
|
3153
3020
|
}
|
|
3154
3021
|
const vlo = this.registry[src[this.discriminator.property]];
|
|
3155
|
-
if (
|
|
3156
|
-
vlo
|
|
3157
|
-
|
|
3158
|
-
(vlo.property &&
|
|
3159
|
-
Object.prototype.hasOwnProperty.call(
|
|
3160
|
-
src,
|
|
3161
|
-
vlo.property,
|
|
3162
|
-
)))
|
|
3163
|
-
) {
|
|
3022
|
+
if (vlo
|
|
3023
|
+
&& ((!vlo.layout)
|
|
3024
|
+
|| (vlo.property && Object.prototype.hasOwnProperty.call(src, vlo.property)))) {
|
|
3164
3025
|
return vlo;
|
|
3165
3026
|
}
|
|
3166
|
-
}
|
|
3027
|
+
}
|
|
3028
|
+
else {
|
|
3167
3029
|
for (const tag in this.registry) {
|
|
3168
3030
|
const vlo = this.registry[tag];
|
|
3169
|
-
if (
|
|
3170
|
-
vlo.property &&
|
|
3171
|
-
Object.prototype.hasOwnProperty.call(src, vlo.property)
|
|
3172
|
-
) {
|
|
3031
|
+
if (vlo.property && Object.prototype.hasOwnProperty.call(src, vlo.property)) {
|
|
3173
3032
|
return vlo;
|
|
3174
3033
|
}
|
|
3175
3034
|
}
|
|
@@ -3197,11 +3056,9 @@ class Union extends Layout {
|
|
|
3197
3056
|
dest[dlo.property] = discr;
|
|
3198
3057
|
// defaultLayout.property can be undefined, but this is allowed by buffer-layout
|
|
3199
3058
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
3200
|
-
dest[defaultLayout.property] = defaultLayout.decode(
|
|
3201
|
-
|
|
3202
|
-
|
|
3203
|
-
);
|
|
3204
|
-
} else {
|
|
3059
|
+
dest[defaultLayout.property] = defaultLayout.decode(b, offset + contentOffset);
|
|
3060
|
+
}
|
|
3061
|
+
else {
|
|
3205
3062
|
dest = clo.decode(b, offset);
|
|
3206
3063
|
}
|
|
3207
3064
|
return dest;
|
|
@@ -3226,10 +3083,7 @@ class Union extends Layout {
|
|
|
3226
3083
|
dlo.encode(src[dlo.property], b, offset);
|
|
3227
3084
|
// clo.property is not undefined when vlo is undefined
|
|
3228
3085
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
3229
|
-
return (
|
|
3230
|
-
contentOffset +
|
|
3231
|
-
clo.encode(src[clo.property], b, offset + contentOffset)
|
|
3232
|
-
);
|
|
3086
|
+
return contentOffset + clo.encode(src[clo.property], b, offset + contentOffset);
|
|
3233
3087
|
}
|
|
3234
3088
|
return vlo.encode(src, b, offset);
|
|
3235
3089
|
}
|
|
@@ -3269,7 +3123,8 @@ class Union extends Layout {
|
|
|
3269
3123
|
let variant;
|
|
3270
3124
|
if (vb instanceof Uint8Array) {
|
|
3271
3125
|
variant = this.discriminator.decode(vb, offset);
|
|
3272
|
-
}
|
|
3126
|
+
}
|
|
3127
|
+
else {
|
|
3273
3128
|
variant = vb;
|
|
3274
3129
|
}
|
|
3275
3130
|
return this.registry[variant];
|
|
@@ -3310,10 +3165,11 @@ class VariantLayout extends Layout {
|
|
|
3310
3165
|
if (!(union instanceof Union)) {
|
|
3311
3166
|
throw new TypeError('union must be a Union');
|
|
3312
3167
|
}
|
|
3313
|
-
if (!Number.isInteger(variant) || 0 > variant) {
|
|
3168
|
+
if ((!Number.isInteger(variant)) || (0 > variant)) {
|
|
3314
3169
|
throw new TypeError('variant must be a (non-negative) integer');
|
|
3315
3170
|
}
|
|
3316
|
-
if ('string' === typeof layout
|
|
3171
|
+
if (('string' === typeof layout)
|
|
3172
|
+
&& (undefined === property)) {
|
|
3317
3173
|
property = layout;
|
|
3318
3174
|
layout = null;
|
|
3319
3175
|
}
|
|
@@ -3321,14 +3177,10 @@ class VariantLayout extends Layout {
|
|
|
3321
3177
|
if (!(layout instanceof Layout)) {
|
|
3322
3178
|
throw new TypeError('layout must be a Layout');
|
|
3323
3179
|
}
|
|
3324
|
-
if (
|
|
3325
|
-
|
|
3326
|
-
|
|
3327
|
-
|
|
3328
|
-
) {
|
|
3329
|
-
throw new Error(
|
|
3330
|
-
'variant span exceeds span of containing union',
|
|
3331
|
-
);
|
|
3180
|
+
if ((null !== union.defaultLayout)
|
|
3181
|
+
&& (0 <= layout.span)
|
|
3182
|
+
&& (layout.span > union.defaultLayout.span)) {
|
|
3183
|
+
throw new Error('variant span exceeds span of containing union');
|
|
3332
3184
|
}
|
|
3333
3185
|
if ('string' !== typeof property) {
|
|
3334
3186
|
throw new TypeError('variant must have a String property');
|
|
@@ -3337,7 +3189,7 @@ class VariantLayout extends Layout {
|
|
|
3337
3189
|
let span = union.span;
|
|
3338
3190
|
if (0 > union.span) {
|
|
3339
3191
|
span = layout ? layout.span : 0;
|
|
3340
|
-
if (0 <= span && union.usesPrefixDiscriminator) {
|
|
3192
|
+
if ((0 <= span) && union.usesPrefixDiscriminator) {
|
|
3341
3193
|
span += union.discriminator.layout.span;
|
|
3342
3194
|
}
|
|
3343
3195
|
}
|
|
@@ -3384,9 +3236,11 @@ class VariantLayout extends Layout {
|
|
|
3384
3236
|
}
|
|
3385
3237
|
if (this.layout) {
|
|
3386
3238
|
dest[this.property] = this.layout.decode(b, offset + contentOffset);
|
|
3387
|
-
}
|
|
3239
|
+
}
|
|
3240
|
+
else if (this.property) {
|
|
3388
3241
|
dest[this.property] = true;
|
|
3389
|
-
}
|
|
3242
|
+
}
|
|
3243
|
+
else if (this.union.usesPrefixDiscriminator) {
|
|
3390
3244
|
dest[this.union.discriminator.property] = this.variant;
|
|
3391
3245
|
}
|
|
3392
3246
|
return dest;
|
|
@@ -3397,10 +3251,8 @@ class VariantLayout extends Layout {
|
|
|
3397
3251
|
if (this.union.usesPrefixDiscriminator) {
|
|
3398
3252
|
contentOffset = this.union.discriminator.layout.span;
|
|
3399
3253
|
}
|
|
3400
|
-
if (
|
|
3401
|
-
this.
|
|
3402
|
-
!Object.prototype.hasOwnProperty.call(src, this.property)
|
|
3403
|
-
) {
|
|
3254
|
+
if (this.layout
|
|
3255
|
+
&& (!Object.prototype.hasOwnProperty.call(src, this.property))) {
|
|
3404
3256
|
throw new TypeError('variant lacks property ' + this.property);
|
|
3405
3257
|
}
|
|
3406
3258
|
this.union.discriminator.encode(this.variant, b, offset);
|
|
@@ -3408,7 +3260,8 @@ class VariantLayout extends Layout {
|
|
|
3408
3260
|
if (this.layout) {
|
|
3409
3261
|
this.layout.encode(src[this.property], b, offset + contentOffset);
|
|
3410
3262
|
span += this.layout.getSpan(b, offset + contentOffset);
|
|
3411
|
-
if (0 <= this.union.span
|
|
3263
|
+
if ((0 <= this.union.span)
|
|
3264
|
+
&& (span > this.union.span)) {
|
|
3412
3265
|
throw new Error('encoded variant overruns containing union');
|
|
3413
3266
|
}
|
|
3414
3267
|
}
|
|
@@ -3469,10 +3322,12 @@ function fixBitwiseResult(v) {
|
|
|
3469
3322
|
*/
|
|
3470
3323
|
class BitStructure extends Layout {
|
|
3471
3324
|
constructor(word, msb, property) {
|
|
3472
|
-
if (!(word instanceof UInt
|
|
3325
|
+
if (!((word instanceof UInt)
|
|
3326
|
+
|| (word instanceof UIntBE))) {
|
|
3473
3327
|
throw new TypeError('word must be a UInt or UIntBE layout');
|
|
3474
3328
|
}
|
|
3475
|
-
if ('string' === typeof msb
|
|
3329
|
+
if (('string' === typeof msb)
|
|
3330
|
+
&& (undefined === property)) {
|
|
3476
3331
|
property = msb;
|
|
3477
3332
|
msb = false;
|
|
3478
3333
|
}
|
|
@@ -3616,19 +3471,15 @@ class BitField {
|
|
|
3616
3471
|
if (!(container instanceof BitStructure)) {
|
|
3617
3472
|
throw new TypeError('container must be a BitStructure');
|
|
3618
3473
|
}
|
|
3619
|
-
if (!Number.isInteger(bits) || 0 >= bits) {
|
|
3474
|
+
if ((!Number.isInteger(bits)) || (0 >= bits)) {
|
|
3620
3475
|
throw new TypeError('bits must be positive integer');
|
|
3621
3476
|
}
|
|
3622
3477
|
const totalBits = 8 * container.span;
|
|
3623
3478
|
const usedBits = container.fields.reduce((sum, fd) => sum + fd.bits, 0);
|
|
3624
|
-
if (bits + usedBits > totalBits) {
|
|
3625
|
-
throw new Error(
|
|
3626
|
-
|
|
3627
|
-
|
|
3628
|
-
' of ' +
|
|
3629
|
-
totalBits +
|
|
3630
|
-
' remain)',
|
|
3631
|
-
);
|
|
3479
|
+
if ((bits + usedBits) > totalBits) {
|
|
3480
|
+
throw new Error('bits too long for span remainder ('
|
|
3481
|
+
+ (totalBits - usedBits) + ' of '
|
|
3482
|
+
+ totalBits + ' remain)');
|
|
3632
3483
|
}
|
|
3633
3484
|
/** The {@link BitStructure} instance to which this bit field
|
|
3634
3485
|
* belongs. */
|
|
@@ -3641,9 +3492,8 @@ class BitField {
|
|
|
3641
3492
|
* That is, it masks a value that has not yet been shifted into
|
|
3642
3493
|
* position within its containing packed integer. */
|
|
3643
3494
|
this.valueMask = (1 << bits) - 1;
|
|
3644
|
-
if (32 === bits) {
|
|
3645
|
-
|
|
3646
|
-
this.valueMask = 0xffffffff;
|
|
3495
|
+
if (32 === bits) { // shifted value out of range
|
|
3496
|
+
this.valueMask = 0xFFFFFFFF;
|
|
3647
3497
|
}
|
|
3648
3498
|
/** The offset of the value within the containing packed unsigned
|
|
3649
3499
|
* integer. The least significant bit of the packed value is at
|
|
@@ -3681,22 +3531,16 @@ class BitField {
|
|
|
3681
3531
|
* **NOTE** This is not a specialization of {@link
|
|
3682
3532
|
* Layout#encode|Layout.encode} and there is no return value. */
|
|
3683
3533
|
encode(value) {
|
|
3684
|
-
if (
|
|
3685
|
-
|
|
3686
|
-
|
|
3687
|
-
|
|
3688
|
-
|
|
3689
|
-
throw new TypeError(
|
|
3690
|
-
nameWithProperty('BitField.encode', this) +
|
|
3691
|
-
' value must be integer not exceeding ' +
|
|
3692
|
-
this.valueMask,
|
|
3693
|
-
);
|
|
3534
|
+
if ('number' !== typeof value
|
|
3535
|
+
|| !Number.isInteger(value)
|
|
3536
|
+
|| (value !== fixBitwiseResult(value & this.valueMask))) {
|
|
3537
|
+
throw new TypeError(nameWithProperty('BitField.encode', this)
|
|
3538
|
+
+ ' value must be integer not exceeding ' + this.valueMask);
|
|
3694
3539
|
}
|
|
3695
3540
|
const word = this.container._packedGetValue();
|
|
3696
3541
|
const wordValue = fixBitwiseResult(value << this.start);
|
|
3697
|
-
this.container._packedSetValue(
|
|
3698
|
-
|
|
3699
|
-
);
|
|
3542
|
+
this.container._packedSetValue(fixBitwiseResult(word & ~this.wordMask)
|
|
3543
|
+
| wordValue);
|
|
3700
3544
|
}
|
|
3701
3545
|
}
|
|
3702
3546
|
Layout$1.BitField = BitField;
|
|
@@ -3754,16 +3598,10 @@ Layout$1.Boolean = Boolean;
|
|
|
3754
3598
|
*/
|
|
3755
3599
|
class Blob extends Layout {
|
|
3756
3600
|
constructor(length, property) {
|
|
3757
|
-
if (
|
|
3758
|
-
|
|
3759
|
-
|
|
3760
|
-
|
|
3761
|
-
)
|
|
3762
|
-
) {
|
|
3763
|
-
throw new TypeError(
|
|
3764
|
-
'length must be positive integer ' +
|
|
3765
|
-
'or an unsigned integer ExternalLayout',
|
|
3766
|
-
);
|
|
3601
|
+
if (!(((length instanceof ExternalLayout) && length.isCount())
|
|
3602
|
+
|| (Number.isInteger(length) && (0 <= length)))) {
|
|
3603
|
+
throw new TypeError('length must be positive integer '
|
|
3604
|
+
+ 'or an unsigned integer ExternalLayout');
|
|
3767
3605
|
}
|
|
3768
3606
|
let span = -1;
|
|
3769
3607
|
if (!(length instanceof ExternalLayout)) {
|
|
@@ -3804,23 +3642,14 @@ class Blob extends Layout {
|
|
|
3804
3642
|
span = src.length;
|
|
3805
3643
|
}
|
|
3806
3644
|
if (!(src instanceof Uint8Array && span === src.length)) {
|
|
3807
|
-
throw new TypeError(
|
|
3808
|
-
|
|
3809
|
-
' requires (length ' +
|
|
3810
|
-
span +
|
|
3811
|
-
') Uint8Array as src',
|
|
3812
|
-
);
|
|
3645
|
+
throw new TypeError(nameWithProperty('Blob.encode', this)
|
|
3646
|
+
+ ' requires (length ' + span + ') Uint8Array as src');
|
|
3813
3647
|
}
|
|
3814
|
-
if (offset + span > b.length) {
|
|
3648
|
+
if ((offset + span) > b.length) {
|
|
3815
3649
|
throw new RangeError('encoding overruns Uint8Array');
|
|
3816
3650
|
}
|
|
3817
3651
|
const srcBuffer = uint8ArrayToBuffer(src);
|
|
3818
|
-
uint8ArrayToBuffer(b).write(
|
|
3819
|
-
srcBuffer.toString('hex'),
|
|
3820
|
-
offset,
|
|
3821
|
-
span,
|
|
3822
|
-
'hex',
|
|
3823
|
-
);
|
|
3652
|
+
uint8ArrayToBuffer(b).write(srcBuffer.toString('hex'), offset, span, 'hex');
|
|
3824
3653
|
if (this.length instanceof ExternalLayout) {
|
|
3825
3654
|
this.length.encode(span, b, offset);
|
|
3826
3655
|
}
|
|
@@ -3849,7 +3678,7 @@ class CString extends Layout {
|
|
|
3849
3678
|
getSpan(b, offset = 0) {
|
|
3850
3679
|
checkUint8Array(b);
|
|
3851
3680
|
let idx = offset;
|
|
3852
|
-
while (idx < b.length && 0 !== b[idx]) {
|
|
3681
|
+
while ((idx < b.length) && (0 !== b[idx])) {
|
|
3853
3682
|
idx += 1;
|
|
3854
3683
|
}
|
|
3855
3684
|
return 1 + idx - offset;
|
|
@@ -3857,9 +3686,7 @@ class CString extends Layout {
|
|
|
3857
3686
|
/** @override */
|
|
3858
3687
|
decode(b, offset = 0) {
|
|
3859
3688
|
const span = this.getSpan(b, offset);
|
|
3860
|
-
return uint8ArrayToBuffer(b)
|
|
3861
|
-
.slice(offset, offset + span - 1)
|
|
3862
|
-
.toString('utf-8');
|
|
3689
|
+
return uint8ArrayToBuffer(b).slice(offset, offset + span - 1).toString('utf-8');
|
|
3863
3690
|
}
|
|
3864
3691
|
/** @override */
|
|
3865
3692
|
encode(src, b, offset = 0) {
|
|
@@ -3871,7 +3698,7 @@ class CString extends Layout {
|
|
|
3871
3698
|
}
|
|
3872
3699
|
const srcb = buffer_1.Buffer.from(src, 'utf8');
|
|
3873
3700
|
const span = srcb.length;
|
|
3874
|
-
if (offset + span > b.length) {
|
|
3701
|
+
if ((offset + span) > b.length) {
|
|
3875
3702
|
throw new RangeError('encoding overruns Buffer');
|
|
3876
3703
|
}
|
|
3877
3704
|
const buffer = uint8ArrayToBuffer(b);
|
|
@@ -3902,13 +3729,14 @@ Layout$1.CString = CString;
|
|
|
3902
3729
|
*/
|
|
3903
3730
|
class UTF8 extends Layout {
|
|
3904
3731
|
constructor(maxSpan, property) {
|
|
3905
|
-
if ('string' === typeof maxSpan && undefined === property) {
|
|
3732
|
+
if (('string' === typeof maxSpan) && (undefined === property)) {
|
|
3906
3733
|
property = maxSpan;
|
|
3907
3734
|
maxSpan = undefined;
|
|
3908
3735
|
}
|
|
3909
3736
|
if (undefined === maxSpan) {
|
|
3910
3737
|
maxSpan = -1;
|
|
3911
|
-
}
|
|
3738
|
+
}
|
|
3739
|
+
else if (!Number.isInteger(maxSpan)) {
|
|
3912
3740
|
throw new TypeError('maxSpan must be an integer');
|
|
3913
3741
|
}
|
|
3914
3742
|
super(-1, property);
|
|
@@ -3930,12 +3758,11 @@ class UTF8 extends Layout {
|
|
|
3930
3758
|
/** @override */
|
|
3931
3759
|
decode(b, offset = 0) {
|
|
3932
3760
|
const span = this.getSpan(b, offset);
|
|
3933
|
-
if (0 <= this.maxSpan
|
|
3761
|
+
if ((0 <= this.maxSpan)
|
|
3762
|
+
&& (this.maxSpan < span)) {
|
|
3934
3763
|
throw new RangeError('text length exceeds maxSpan');
|
|
3935
3764
|
}
|
|
3936
|
-
return uint8ArrayToBuffer(b)
|
|
3937
|
-
.slice(offset, offset + span)
|
|
3938
|
-
.toString('utf-8');
|
|
3765
|
+
return uint8ArrayToBuffer(b).slice(offset, offset + span).toString('utf-8');
|
|
3939
3766
|
}
|
|
3940
3767
|
/** @override */
|
|
3941
3768
|
encode(src, b, offset = 0) {
|
|
@@ -3947,10 +3774,11 @@ class UTF8 extends Layout {
|
|
|
3947
3774
|
}
|
|
3948
3775
|
const srcb = buffer_1.Buffer.from(src, 'utf8');
|
|
3949
3776
|
const span = srcb.length;
|
|
3950
|
-
if (0 <= this.maxSpan
|
|
3777
|
+
if ((0 <= this.maxSpan)
|
|
3778
|
+
&& (this.maxSpan < span)) {
|
|
3951
3779
|
throw new RangeError('text length exceeds maxSpan');
|
|
3952
3780
|
}
|
|
3953
|
-
if (offset + span > b.length) {
|
|
3781
|
+
if ((offset + span) > b.length) {
|
|
3954
3782
|
throw new RangeError('encoding overruns Buffer');
|
|
3955
3783
|
}
|
|
3956
3784
|
srcb.copy(uint8ArrayToBuffer(b), offset);
|
|
@@ -4003,121 +3831,115 @@ class Constant extends Layout {
|
|
|
4003
3831
|
}
|
|
4004
3832
|
Layout$1.Constant = Constant;
|
|
4005
3833
|
/** Factory for {@link GreedyCount}. */
|
|
4006
|
-
Layout$1.greedy = (elementSpan, property) =>
|
|
4007
|
-
new GreedyCount(elementSpan, property);
|
|
3834
|
+
Layout$1.greedy = ((elementSpan, property) => new GreedyCount(elementSpan, property));
|
|
4008
3835
|
/** Factory for {@link OffsetLayout}. */
|
|
4009
|
-
Layout$1.offset = (layout, offset, property) =>
|
|
4010
|
-
new OffsetLayout(layout, offset, property);
|
|
3836
|
+
Layout$1.offset = ((layout, offset, property) => new OffsetLayout(layout, offset, property));
|
|
4011
3837
|
/** Factory for {@link UInt|unsigned int layouts} spanning one
|
|
4012
3838
|
* byte. */
|
|
4013
|
-
var u8 =
|
|
3839
|
+
var u8 = Layout$1.u8 = ((property) => new UInt(1, property));
|
|
4014
3840
|
/** Factory for {@link UInt|little-endian unsigned int layouts}
|
|
4015
3841
|
* spanning two bytes. */
|
|
4016
|
-
Layout$1.u16 = property => new UInt(2, property);
|
|
3842
|
+
Layout$1.u16 = ((property) => new UInt(2, property));
|
|
4017
3843
|
/** Factory for {@link UInt|little-endian unsigned int layouts}
|
|
4018
3844
|
* spanning three bytes. */
|
|
4019
|
-
Layout$1.u24 = property => new UInt(3, property);
|
|
3845
|
+
Layout$1.u24 = ((property) => new UInt(3, property));
|
|
4020
3846
|
/** Factory for {@link UInt|little-endian unsigned int layouts}
|
|
4021
3847
|
* spanning four bytes. */
|
|
4022
|
-
var u32 =
|
|
3848
|
+
var u32 = Layout$1.u32 = ((property) => new UInt(4, property));
|
|
4023
3849
|
/** Factory for {@link UInt|little-endian unsigned int layouts}
|
|
4024
3850
|
* spanning five bytes. */
|
|
4025
|
-
Layout$1.u40 = property => new UInt(5, property);
|
|
3851
|
+
Layout$1.u40 = ((property) => new UInt(5, property));
|
|
4026
3852
|
/** Factory for {@link UInt|little-endian unsigned int layouts}
|
|
4027
3853
|
* spanning six bytes. */
|
|
4028
|
-
Layout$1.u48 = property => new UInt(6, property);
|
|
3854
|
+
Layout$1.u48 = ((property) => new UInt(6, property));
|
|
4029
3855
|
/** Factory for {@link NearUInt64|little-endian unsigned int
|
|
4030
3856
|
* layouts} interpreted as Numbers. */
|
|
4031
|
-
Layout$1.nu64 = property => new NearUInt64(property);
|
|
3857
|
+
Layout$1.nu64 = ((property) => new NearUInt64(property));
|
|
4032
3858
|
/** Factory for {@link UInt|big-endian unsigned int layouts}
|
|
4033
3859
|
* spanning two bytes. */
|
|
4034
|
-
Layout$1.u16be = property => new UIntBE(2, property);
|
|
3860
|
+
Layout$1.u16be = ((property) => new UIntBE(2, property));
|
|
4035
3861
|
/** Factory for {@link UInt|big-endian unsigned int layouts}
|
|
4036
3862
|
* spanning three bytes. */
|
|
4037
|
-
Layout$1.u24be = property => new UIntBE(3, property);
|
|
3863
|
+
Layout$1.u24be = ((property) => new UIntBE(3, property));
|
|
4038
3864
|
/** Factory for {@link UInt|big-endian unsigned int layouts}
|
|
4039
3865
|
* spanning four bytes. */
|
|
4040
|
-
Layout$1.u32be = property => new UIntBE(4, property);
|
|
3866
|
+
Layout$1.u32be = ((property) => new UIntBE(4, property));
|
|
4041
3867
|
/** Factory for {@link UInt|big-endian unsigned int layouts}
|
|
4042
3868
|
* spanning five bytes. */
|
|
4043
|
-
Layout$1.u40be = property => new UIntBE(5, property);
|
|
3869
|
+
Layout$1.u40be = ((property) => new UIntBE(5, property));
|
|
4044
3870
|
/** Factory for {@link UInt|big-endian unsigned int layouts}
|
|
4045
3871
|
* spanning six bytes. */
|
|
4046
|
-
Layout$1.u48be = property => new UIntBE(6, property);
|
|
3872
|
+
Layout$1.u48be = ((property) => new UIntBE(6, property));
|
|
4047
3873
|
/** Factory for {@link NearUInt64BE|big-endian unsigned int
|
|
4048
3874
|
* layouts} interpreted as Numbers. */
|
|
4049
|
-
Layout$1.nu64be = property => new NearUInt64BE(property);
|
|
3875
|
+
Layout$1.nu64be = ((property) => new NearUInt64BE(property));
|
|
4050
3876
|
/** Factory for {@link Int|signed int layouts} spanning one
|
|
4051
3877
|
* byte. */
|
|
4052
|
-
Layout$1.s8 = property => new Int(1, property);
|
|
3878
|
+
Layout$1.s8 = ((property) => new Int(1, property));
|
|
4053
3879
|
/** Factory for {@link Int|little-endian signed int layouts}
|
|
4054
3880
|
* spanning two bytes. */
|
|
4055
|
-
Layout$1.s16 = property => new Int(2, property);
|
|
3881
|
+
Layout$1.s16 = ((property) => new Int(2, property));
|
|
4056
3882
|
/** Factory for {@link Int|little-endian signed int layouts}
|
|
4057
3883
|
* spanning three bytes. */
|
|
4058
|
-
Layout$1.s24 = property => new Int(3, property);
|
|
3884
|
+
Layout$1.s24 = ((property) => new Int(3, property));
|
|
4059
3885
|
/** Factory for {@link Int|little-endian signed int layouts}
|
|
4060
3886
|
* spanning four bytes. */
|
|
4061
|
-
Layout$1.s32 = property => new Int(4, property);
|
|
3887
|
+
Layout$1.s32 = ((property) => new Int(4, property));
|
|
4062
3888
|
/** Factory for {@link Int|little-endian signed int layouts}
|
|
4063
3889
|
* spanning five bytes. */
|
|
4064
|
-
Layout$1.s40 = property => new Int(5, property);
|
|
3890
|
+
Layout$1.s40 = ((property) => new Int(5, property));
|
|
4065
3891
|
/** Factory for {@link Int|little-endian signed int layouts}
|
|
4066
3892
|
* spanning six bytes. */
|
|
4067
|
-
Layout$1.s48 = property => new Int(6, property);
|
|
3893
|
+
Layout$1.s48 = ((property) => new Int(6, property));
|
|
4068
3894
|
/** Factory for {@link NearInt64|little-endian signed int layouts}
|
|
4069
3895
|
* interpreted as Numbers. */
|
|
4070
|
-
Layout$1.ns64 = property => new NearInt64(property);
|
|
3896
|
+
Layout$1.ns64 = ((property) => new NearInt64(property));
|
|
4071
3897
|
/** Factory for {@link Int|big-endian signed int layouts}
|
|
4072
3898
|
* spanning two bytes. */
|
|
4073
|
-
Layout$1.s16be = property => new IntBE(2, property);
|
|
3899
|
+
Layout$1.s16be = ((property) => new IntBE(2, property));
|
|
4074
3900
|
/** Factory for {@link Int|big-endian signed int layouts}
|
|
4075
3901
|
* spanning three bytes. */
|
|
4076
|
-
Layout$1.s24be = property => new IntBE(3, property);
|
|
3902
|
+
Layout$1.s24be = ((property) => new IntBE(3, property));
|
|
4077
3903
|
/** Factory for {@link Int|big-endian signed int layouts}
|
|
4078
3904
|
* spanning four bytes. */
|
|
4079
|
-
Layout$1.s32be = property => new IntBE(4, property);
|
|
3905
|
+
Layout$1.s32be = ((property) => new IntBE(4, property));
|
|
4080
3906
|
/** Factory for {@link Int|big-endian signed int layouts}
|
|
4081
3907
|
* spanning five bytes. */
|
|
4082
|
-
Layout$1.s40be = property => new IntBE(5, property);
|
|
3908
|
+
Layout$1.s40be = ((property) => new IntBE(5, property));
|
|
4083
3909
|
/** Factory for {@link Int|big-endian signed int layouts}
|
|
4084
3910
|
* spanning six bytes. */
|
|
4085
|
-
Layout$1.s48be = property => new IntBE(6, property);
|
|
3911
|
+
Layout$1.s48be = ((property) => new IntBE(6, property));
|
|
4086
3912
|
/** Factory for {@link NearInt64BE|big-endian signed int layouts}
|
|
4087
3913
|
* interpreted as Numbers. */
|
|
4088
|
-
Layout$1.ns64be = property => new NearInt64BE(property);
|
|
3914
|
+
Layout$1.ns64be = ((property) => new NearInt64BE(property));
|
|
4089
3915
|
/** Factory for {@link Float|little-endian 32-bit floating point} values. */
|
|
4090
|
-
Layout$1.f32 = property => new Float(property);
|
|
3916
|
+
Layout$1.f32 = ((property) => new Float(property));
|
|
4091
3917
|
/** Factory for {@link FloatBE|big-endian 32-bit floating point} values. */
|
|
4092
|
-
Layout$1.f32be = property => new FloatBE(property);
|
|
3918
|
+
Layout$1.f32be = ((property) => new FloatBE(property));
|
|
4093
3919
|
/** Factory for {@link Double|little-endian 64-bit floating point} values. */
|
|
4094
|
-
Layout$1.f64 = property => new Double(property);
|
|
3920
|
+
Layout$1.f64 = ((property) => new Double(property));
|
|
4095
3921
|
/** Factory for {@link DoubleBE|big-endian 64-bit floating point} values. */
|
|
4096
|
-
Layout$1.f64be = property => new DoubleBE(property);
|
|
3922
|
+
Layout$1.f64be = ((property) => new DoubleBE(property));
|
|
4097
3923
|
/** Factory for {@link Structure} values. */
|
|
4098
|
-
var struct =
|
|
4099
|
-
new Structure(fields, property, decodePrefixes));
|
|
3924
|
+
var struct = Layout$1.struct = ((fields, property, decodePrefixes) => new Structure(fields, property, decodePrefixes));
|
|
4100
3925
|
/** Factory for {@link BitStructure} values. */
|
|
4101
|
-
Layout$1.bits = (word, msb, property) => new BitStructure(word, msb, property);
|
|
3926
|
+
Layout$1.bits = ((word, msb, property) => new BitStructure(word, msb, property));
|
|
4102
3927
|
/** Factory for {@link Sequence} values. */
|
|
4103
|
-
Layout$1.seq = (elementLayout, count, property) =>
|
|
4104
|
-
new Sequence(elementLayout, count, property);
|
|
3928
|
+
Layout$1.seq = ((elementLayout, count, property) => new Sequence(elementLayout, count, property));
|
|
4105
3929
|
/** Factory for {@link Union} values. */
|
|
4106
|
-
Layout$1.union = (discr, defaultLayout, property) =>
|
|
4107
|
-
new Union(discr, defaultLayout, property);
|
|
3930
|
+
Layout$1.union = ((discr, defaultLayout, property) => new Union(discr, defaultLayout, property));
|
|
4108
3931
|
/** Factory for {@link UnionLayoutDiscriminator} values. */
|
|
4109
|
-
Layout$1.unionLayoutDiscriminator = (layout, property) =>
|
|
4110
|
-
new UnionLayoutDiscriminator(layout, property);
|
|
3932
|
+
Layout$1.unionLayoutDiscriminator = ((layout, property) => new UnionLayoutDiscriminator(layout, property));
|
|
4111
3933
|
/** Factory for {@link Blob} values. */
|
|
4112
|
-
var blob =
|
|
3934
|
+
var blob = Layout$1.blob = ((length, property) => new Blob(length, property));
|
|
4113
3935
|
/** Factory for {@link CString} values. */
|
|
4114
|
-
Layout$1.cstr = property => new CString(property);
|
|
3936
|
+
Layout$1.cstr = ((property) => new CString(property));
|
|
4115
3937
|
/** Factory for {@link UTF8} values. */
|
|
4116
|
-
Layout$1.utf8 = (maxSpan, property) => new UTF8(maxSpan, property);
|
|
3938
|
+
Layout$1.utf8 = ((maxSpan, property) => new UTF8(maxSpan, property));
|
|
4117
3939
|
/** Factory for {@link Constant} values. */
|
|
4118
|
-
Layout$1.constant = (value, property) => new Constant(value, property);
|
|
3940
|
+
Layout$1.constant = ((value, property) => new Constant(value, property));
|
|
4119
3941
|
|
|
4120
|
-
const encodeDecode = layout => {
|
|
3942
|
+
const encodeDecode = (layout) => {
|
|
4121
3943
|
const decode = layout.decode.bind(layout);
|
|
4122
3944
|
const encode = layout.encode.bind(layout);
|
|
4123
3945
|
return { decode, encode };
|
|
@@ -4125,7 +3947,7 @@ const encodeDecode = layout => {
|
|
|
4125
3947
|
|
|
4126
3948
|
var browser = {};
|
|
4127
3949
|
|
|
4128
|
-
Object.defineProperty(browser,
|
|
3950
|
+
Object.defineProperty(browser, "__esModule", { value: true });
|
|
4129
3951
|
/**
|
|
4130
3952
|
* Convert a little-endian buffer into a BigInt.
|
|
4131
3953
|
* @param buf The little-endian buffer to convert
|
|
@@ -4142,7 +3964,7 @@ function toBigIntLE(buf) {
|
|
|
4142
3964
|
return BigInt(`0x${hex}`);
|
|
4143
3965
|
}
|
|
4144
3966
|
}
|
|
4145
|
-
var toBigIntLE_1 =
|
|
3967
|
+
var toBigIntLE_1 = browser.toBigIntLE = toBigIntLE;
|
|
4146
3968
|
/**
|
|
4147
3969
|
* Convert a big-endian buffer into a BigInt
|
|
4148
3970
|
* @param buf The big-endian buffer to convert.
|
|
@@ -4167,15 +3989,12 @@ browser.toBigIntBE = toBigIntBE;
|
|
|
4167
3989
|
function toBufferLE(num, width) {
|
|
4168
3990
|
{
|
|
4169
3991
|
const hex = num.toString(16);
|
|
4170
|
-
const buffer = Buffer.from(
|
|
4171
|
-
hex.padStart(width * 2, '0').slice(0, width * 2),
|
|
4172
|
-
'hex',
|
|
4173
|
-
);
|
|
3992
|
+
const buffer = Buffer.from(hex.padStart(width * 2, '0').slice(0, width * 2), 'hex');
|
|
4174
3993
|
buffer.reverse();
|
|
4175
3994
|
return buffer;
|
|
4176
3995
|
}
|
|
4177
3996
|
}
|
|
4178
|
-
var toBufferLE_1 =
|
|
3997
|
+
var toBufferLE_1 = browser.toBufferLE = toBufferLE;
|
|
4179
3998
|
/**
|
|
4180
3999
|
* Convert a BigInt to a big-endian buffer.
|
|
4181
4000
|
* @param num The BigInt to convert.
|
|
@@ -4185,15 +4004,12 @@ var toBufferLE_1 = (browser.toBufferLE = toBufferLE);
|
|
|
4185
4004
|
function toBufferBE(num, width) {
|
|
4186
4005
|
{
|
|
4187
4006
|
const hex = num.toString(16);
|
|
4188
|
-
return Buffer.from(
|
|
4189
|
-
hex.padStart(width * 2, '0').slice(0, width * 2),
|
|
4190
|
-
'hex',
|
|
4191
|
-
);
|
|
4007
|
+
return Buffer.from(hex.padStart(width * 2, '0').slice(0, width * 2), 'hex');
|
|
4192
4008
|
}
|
|
4193
4009
|
}
|
|
4194
4010
|
browser.toBufferBE = toBufferBE;
|
|
4195
4011
|
|
|
4196
|
-
const bigInt = length => property => {
|
|
4012
|
+
const bigInt = (length) => (property) => {
|
|
4197
4013
|
const layout = blob(length, property);
|
|
4198
4014
|
const { encode, decode } = encodeDecode(layout);
|
|
4199
4015
|
const bigIntLayout = layout;
|
|
@@ -4209,7 +4025,7 @@ const bigInt = length => property => {
|
|
|
4209
4025
|
};
|
|
4210
4026
|
const u64 = bigInt(8);
|
|
4211
4027
|
|
|
4212
|
-
const bool = property => {
|
|
4028
|
+
const bool = (property) => {
|
|
4213
4029
|
const layout = u8(property);
|
|
4214
4030
|
const { encode, decode } = encodeDecode(layout);
|
|
4215
4031
|
const boolLayout = layout;
|
|
@@ -4224,7 +4040,7 @@ const bool = property => {
|
|
|
4224
4040
|
return boolLayout;
|
|
4225
4041
|
};
|
|
4226
4042
|
|
|
4227
|
-
const publicKey = property => {
|
|
4043
|
+
const publicKey = (property) => {
|
|
4228
4044
|
const layout = blob(32, property);
|
|
4229
4045
|
const { encode, decode } = encodeDecode(layout);
|
|
4230
4046
|
const publicKeyLayout = layout;
|
|
@@ -4298,112 +4114,73 @@ class TokenOwnerOffCurveError extends TokenError {
|
|
|
4298
4114
|
/** Instructions defined by the program */
|
|
4299
4115
|
var TokenInstruction;
|
|
4300
4116
|
(function (TokenInstruction) {
|
|
4301
|
-
TokenInstruction[
|
|
4302
|
-
|
|
4303
|
-
TokenInstruction[
|
|
4304
|
-
|
|
4305
|
-
TokenInstruction[
|
|
4306
|
-
|
|
4307
|
-
TokenInstruction[
|
|
4308
|
-
TokenInstruction[
|
|
4309
|
-
TokenInstruction[
|
|
4310
|
-
TokenInstruction[
|
|
4311
|
-
TokenInstruction[
|
|
4312
|
-
TokenInstruction[
|
|
4313
|
-
TokenInstruction[
|
|
4314
|
-
TokenInstruction[
|
|
4315
|
-
|
|
4316
|
-
TokenInstruction[
|
|
4317
|
-
TokenInstruction[
|
|
4318
|
-
|
|
4319
|
-
TokenInstruction[
|
|
4320
|
-
|
|
4321
|
-
TokenInstruction[
|
|
4322
|
-
|
|
4323
|
-
TokenInstruction[
|
|
4324
|
-
TokenInstruction[
|
|
4325
|
-
|
|
4326
|
-
TokenInstruction[
|
|
4327
|
-
TokenInstruction[
|
|
4328
|
-
|
|
4329
|
-
TokenInstruction[
|
|
4330
|
-
|
|
4331
|
-
TokenInstruction[
|
|
4332
|
-
|
|
4333
|
-
TokenInstruction[
|
|
4334
|
-
|
|
4335
|
-
TokenInstruction[
|
|
4336
|
-
|
|
4337
|
-
TokenInstruction[
|
|
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';
|
|
4117
|
+
TokenInstruction[TokenInstruction["InitializeMint"] = 0] = "InitializeMint";
|
|
4118
|
+
TokenInstruction[TokenInstruction["InitializeAccount"] = 1] = "InitializeAccount";
|
|
4119
|
+
TokenInstruction[TokenInstruction["InitializeMultisig"] = 2] = "InitializeMultisig";
|
|
4120
|
+
TokenInstruction[TokenInstruction["Transfer"] = 3] = "Transfer";
|
|
4121
|
+
TokenInstruction[TokenInstruction["Approve"] = 4] = "Approve";
|
|
4122
|
+
TokenInstruction[TokenInstruction["Revoke"] = 5] = "Revoke";
|
|
4123
|
+
TokenInstruction[TokenInstruction["SetAuthority"] = 6] = "SetAuthority";
|
|
4124
|
+
TokenInstruction[TokenInstruction["MintTo"] = 7] = "MintTo";
|
|
4125
|
+
TokenInstruction[TokenInstruction["Burn"] = 8] = "Burn";
|
|
4126
|
+
TokenInstruction[TokenInstruction["CloseAccount"] = 9] = "CloseAccount";
|
|
4127
|
+
TokenInstruction[TokenInstruction["FreezeAccount"] = 10] = "FreezeAccount";
|
|
4128
|
+
TokenInstruction[TokenInstruction["ThawAccount"] = 11] = "ThawAccount";
|
|
4129
|
+
TokenInstruction[TokenInstruction["TransferChecked"] = 12] = "TransferChecked";
|
|
4130
|
+
TokenInstruction[TokenInstruction["ApproveChecked"] = 13] = "ApproveChecked";
|
|
4131
|
+
TokenInstruction[TokenInstruction["MintToChecked"] = 14] = "MintToChecked";
|
|
4132
|
+
TokenInstruction[TokenInstruction["BurnChecked"] = 15] = "BurnChecked";
|
|
4133
|
+
TokenInstruction[TokenInstruction["InitializeAccount2"] = 16] = "InitializeAccount2";
|
|
4134
|
+
TokenInstruction[TokenInstruction["SyncNative"] = 17] = "SyncNative";
|
|
4135
|
+
TokenInstruction[TokenInstruction["InitializeAccount3"] = 18] = "InitializeAccount3";
|
|
4136
|
+
TokenInstruction[TokenInstruction["InitializeMultisig2"] = 19] = "InitializeMultisig2";
|
|
4137
|
+
TokenInstruction[TokenInstruction["InitializeMint2"] = 20] = "InitializeMint2";
|
|
4138
|
+
TokenInstruction[TokenInstruction["GetAccountDataSize"] = 21] = "GetAccountDataSize";
|
|
4139
|
+
TokenInstruction[TokenInstruction["InitializeImmutableOwner"] = 22] = "InitializeImmutableOwner";
|
|
4140
|
+
TokenInstruction[TokenInstruction["AmountToUiAmount"] = 23] = "AmountToUiAmount";
|
|
4141
|
+
TokenInstruction[TokenInstruction["UiAmountToAmount"] = 24] = "UiAmountToAmount";
|
|
4142
|
+
TokenInstruction[TokenInstruction["InitializeMintCloseAuthority"] = 25] = "InitializeMintCloseAuthority";
|
|
4143
|
+
TokenInstruction[TokenInstruction["TransferFeeExtension"] = 26] = "TransferFeeExtension";
|
|
4144
|
+
TokenInstruction[TokenInstruction["ConfidentialTransferExtension"] = 27] = "ConfidentialTransferExtension";
|
|
4145
|
+
TokenInstruction[TokenInstruction["DefaultAccountStateExtension"] = 28] = "DefaultAccountStateExtension";
|
|
4146
|
+
TokenInstruction[TokenInstruction["Reallocate"] = 29] = "Reallocate";
|
|
4147
|
+
TokenInstruction[TokenInstruction["MemoTransferExtension"] = 30] = "MemoTransferExtension";
|
|
4148
|
+
TokenInstruction[TokenInstruction["CreateNativeMint"] = 31] = "CreateNativeMint";
|
|
4149
|
+
TokenInstruction[TokenInstruction["InitializeNonTransferableMint"] = 32] = "InitializeNonTransferableMint";
|
|
4150
|
+
TokenInstruction[TokenInstruction["InterestBearingMintExtension"] = 33] = "InterestBearingMintExtension";
|
|
4151
|
+
TokenInstruction[TokenInstruction["CpiGuardExtension"] = 34] = "CpiGuardExtension";
|
|
4152
|
+
TokenInstruction[TokenInstruction["InitializePermanentDelegate"] = 35] = "InitializePermanentDelegate";
|
|
4153
|
+
TokenInstruction[TokenInstruction["TransferHookExtension"] = 36] = "TransferHookExtension";
|
|
4364
4154
|
// ConfidentialTransferFeeExtension = 37,
|
|
4365
4155
|
// WithdrawalExcessLamports = 38,
|
|
4366
|
-
TokenInstruction[
|
|
4367
|
-
|
|
4368
|
-
TokenInstruction[
|
|
4369
|
-
'GroupPointerExtension';
|
|
4370
|
-
TokenInstruction[(TokenInstruction['GroupMemberPointerExtension'] = 41)] =
|
|
4371
|
-
'GroupMemberPointerExtension';
|
|
4156
|
+
TokenInstruction[TokenInstruction["MetadataPointerExtension"] = 39] = "MetadataPointerExtension";
|
|
4157
|
+
TokenInstruction[TokenInstruction["GroupPointerExtension"] = 40] = "GroupPointerExtension";
|
|
4158
|
+
TokenInstruction[TokenInstruction["GroupMemberPointerExtension"] = 41] = "GroupMemberPointerExtension";
|
|
4372
4159
|
})(TokenInstruction || (TokenInstruction = {}));
|
|
4373
4160
|
|
|
4374
4161
|
/** @internal */
|
|
4375
4162
|
function addSigners(keys, ownerOrAuthority, multiSigners) {
|
|
4376
4163
|
if (multiSigners.length) {
|
|
4377
|
-
keys.push({
|
|
4378
|
-
pubkey: ownerOrAuthority,
|
|
4379
|
-
isSigner: false,
|
|
4380
|
-
isWritable: false,
|
|
4381
|
-
});
|
|
4164
|
+
keys.push({ pubkey: ownerOrAuthority, isSigner: false, isWritable: false });
|
|
4382
4165
|
for (const signer of multiSigners) {
|
|
4383
4166
|
keys.push({
|
|
4384
|
-
pubkey:
|
|
4385
|
-
signer instanceof web3_js.PublicKey
|
|
4386
|
-
? signer
|
|
4387
|
-
: signer.publicKey,
|
|
4167
|
+
pubkey: signer instanceof web3_js.PublicKey ? signer : signer.publicKey,
|
|
4388
4168
|
isSigner: true,
|
|
4389
4169
|
isWritable: false,
|
|
4390
4170
|
});
|
|
4391
4171
|
}
|
|
4392
|
-
}
|
|
4393
|
-
|
|
4394
|
-
|
|
4395
|
-
isSigner: true,
|
|
4396
|
-
isWritable: false,
|
|
4397
|
-
});
|
|
4172
|
+
}
|
|
4173
|
+
else {
|
|
4174
|
+
keys.push({ pubkey: ownerOrAuthority, isSigner: true, isWritable: false });
|
|
4398
4175
|
}
|
|
4399
4176
|
return keys;
|
|
4400
4177
|
}
|
|
4401
4178
|
|
|
4402
4179
|
var AccountType;
|
|
4403
4180
|
(function (AccountType) {
|
|
4404
|
-
AccountType[
|
|
4405
|
-
AccountType[
|
|
4406
|
-
AccountType[
|
|
4181
|
+
AccountType[AccountType["Uninitialized"] = 0] = "Uninitialized";
|
|
4182
|
+
AccountType[AccountType["Mint"] = 1] = "Mint";
|
|
4183
|
+
AccountType[AccountType["Account"] = 2] = "Account";
|
|
4407
4184
|
})(AccountType || (AccountType = {}));
|
|
4408
4185
|
const ACCOUNT_TYPE_SIZE = 1;
|
|
4409
4186
|
|
|
@@ -4430,9 +4207,9 @@ const MULTISIG_SIZE = MultisigLayout.span;
|
|
|
4430
4207
|
/** Token account state as stored by the program */
|
|
4431
4208
|
var AccountState;
|
|
4432
4209
|
(function (AccountState) {
|
|
4433
|
-
AccountState[
|
|
4434
|
-
AccountState[
|
|
4435
|
-
AccountState[
|
|
4210
|
+
AccountState[AccountState["Uninitialized"] = 0] = "Uninitialized";
|
|
4211
|
+
AccountState[AccountState["Initialized"] = 1] = "Initialized";
|
|
4212
|
+
AccountState[AccountState["Frozen"] = 2] = "Frozen";
|
|
4436
4213
|
})(AccountState || (AccountState = {}));
|
|
4437
4214
|
/** Buffer layout for de/serializing a token account */
|
|
4438
4215
|
const AccountLayout = struct([
|
|
@@ -4460,12 +4237,7 @@ const ACCOUNT_SIZE = AccountLayout.span;
|
|
|
4460
4237
|
*
|
|
4461
4238
|
* @return Token account information
|
|
4462
4239
|
*/
|
|
4463
|
-
async function getAccount(
|
|
4464
|
-
connection,
|
|
4465
|
-
address,
|
|
4466
|
-
commitment,
|
|
4467
|
-
programId = TOKEN_PROGRAM_ID,
|
|
4468
|
-
) {
|
|
4240
|
+
async function getAccount(connection, address, commitment, programId = TOKEN_PROGRAM_ID) {
|
|
4469
4241
|
const info = await connection.getAccountInfo(address, commitment);
|
|
4470
4242
|
return unpackAccount(address, info, programId);
|
|
4471
4243
|
}
|
|
@@ -4479,7 +4251,8 @@ async function getAccount(
|
|
|
4479
4251
|
* @return Unpacked token account
|
|
4480
4252
|
*/
|
|
4481
4253
|
function unpackAccount(address, info, programId = TOKEN_PROGRAM_ID) {
|
|
4482
|
-
if (!info)
|
|
4254
|
+
if (!info)
|
|
4255
|
+
throw new TokenAccountNotFoundError();
|
|
4483
4256
|
if (!info.owner.equals(programId))
|
|
4484
4257
|
throw new TokenInvalidAccountOwnerError();
|
|
4485
4258
|
if (info.data.length < ACCOUNT_SIZE)
|
|
@@ -4503,12 +4276,8 @@ function unpackAccount(address, info, programId = TOKEN_PROGRAM_ID) {
|
|
|
4503
4276
|
isInitialized: rawAccount.state !== AccountState.Uninitialized,
|
|
4504
4277
|
isFrozen: rawAccount.state === AccountState.Frozen,
|
|
4505
4278
|
isNative: !!rawAccount.isNativeOption,
|
|
4506
|
-
rentExemptReserve: rawAccount.isNativeOption
|
|
4507
|
-
|
|
4508
|
-
: null,
|
|
4509
|
-
closeAuthority: rawAccount.closeAuthorityOption
|
|
4510
|
-
? rawAccount.closeAuthority
|
|
4511
|
-
: null,
|
|
4279
|
+
rentExemptReserve: rawAccount.isNativeOption ? rawAccount.isNative : null,
|
|
4280
|
+
closeAuthority: rawAccount.closeAuthorityOption ? rawAccount.closeAuthority : null,
|
|
4512
4281
|
tlvData,
|
|
4513
4282
|
};
|
|
4514
4283
|
}
|
|
@@ -4536,19 +4305,10 @@ const MINT_SIZE = MintLayout.span;
|
|
|
4536
4305
|
*
|
|
4537
4306
|
* @return Address of the associated token account
|
|
4538
4307
|
*/
|
|
4539
|
-
function getAssociatedTokenAddressSync(
|
|
4540
|
-
mint,
|
|
4541
|
-
owner,
|
|
4542
|
-
allowOwnerOffCurve = false,
|
|
4543
|
-
programId = TOKEN_PROGRAM_ID,
|
|
4544
|
-
associatedTokenProgramId = ASSOCIATED_TOKEN_PROGRAM_ID,
|
|
4545
|
-
) {
|
|
4308
|
+
function getAssociatedTokenAddressSync(mint, owner, allowOwnerOffCurve = false, programId = TOKEN_PROGRAM_ID, associatedTokenProgramId = ASSOCIATED_TOKEN_PROGRAM_ID) {
|
|
4546
4309
|
if (!allowOwnerOffCurve && !web3_js.PublicKey.isOnCurve(owner.toBuffer()))
|
|
4547
4310
|
throw new TokenOwnerOffCurveError();
|
|
4548
|
-
const [address] = web3_js.PublicKey.findProgramAddressSync(
|
|
4549
|
-
[owner.toBuffer(), programId.toBuffer(), mint.toBuffer()],
|
|
4550
|
-
associatedTokenProgramId,
|
|
4551
|
-
);
|
|
4311
|
+
const [address] = web3_js.PublicKey.findProgramAddressSync([owner.toBuffer(), programId.toBuffer(), mint.toBuffer()], associatedTokenProgramId);
|
|
4552
4312
|
return address;
|
|
4553
4313
|
}
|
|
4554
4314
|
|
|
@@ -4568,7 +4328,8 @@ class COptionPublicKeyLayout extends Layout_2 {
|
|
|
4568
4328
|
if (src === null) {
|
|
4569
4329
|
buffer[offset] = 0;
|
|
4570
4330
|
return 1;
|
|
4571
|
-
}
|
|
4331
|
+
}
|
|
4332
|
+
else {
|
|
4572
4333
|
buffer[offset] = 1;
|
|
4573
4334
|
this.publicKeyLayout.encode(src, buffer, offset + 1);
|
|
4574
4335
|
return 33;
|
|
@@ -4595,43 +4356,16 @@ class COptionPublicKeyLayout extends Layout_2 {
|
|
|
4595
4356
|
*
|
|
4596
4357
|
* @return Instruction to add to a transaction
|
|
4597
4358
|
*/
|
|
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
|
-
);
|
|
4359
|
+
function createAssociatedTokenAccountInstruction(payer, associatedToken, owner, mint, programId = TOKEN_PROGRAM_ID, associatedTokenProgramId = ASSOCIATED_TOKEN_PROGRAM_ID) {
|
|
4360
|
+
return buildAssociatedTokenAccountInstruction(payer, associatedToken, owner, mint, Buffer.alloc(0), programId, associatedTokenProgramId);
|
|
4615
4361
|
}
|
|
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
|
-
) {
|
|
4362
|
+
function buildAssociatedTokenAccountInstruction(payer, associatedToken, owner, mint, instructionData, programId = TOKEN_PROGRAM_ID, associatedTokenProgramId = ASSOCIATED_TOKEN_PROGRAM_ID) {
|
|
4625
4363
|
const keys = [
|
|
4626
4364
|
{ pubkey: payer, isSigner: true, isWritable: true },
|
|
4627
4365
|
{ pubkey: associatedToken, isSigner: false, isWritable: true },
|
|
4628
4366
|
{ pubkey: owner, isSigner: false, isWritable: false },
|
|
4629
4367
|
{ pubkey: mint, isSigner: false, isWritable: false },
|
|
4630
|
-
{
|
|
4631
|
-
pubkey: web3_js.SystemProgram.programId,
|
|
4632
|
-
isSigner: false,
|
|
4633
|
-
isWritable: false,
|
|
4634
|
-
},
|
|
4368
|
+
{ pubkey: web3_js.SystemProgram.programId, isSigner: false, isWritable: false },
|
|
4635
4369
|
{ pubkey: programId, isSigner: false, isWritable: false },
|
|
4636
4370
|
];
|
|
4637
4371
|
return new web3_js.TransactionInstruction({
|
|
@@ -4659,24 +4393,15 @@ const initializeMint2InstructionData = struct([
|
|
|
4659
4393
|
*
|
|
4660
4394
|
* @return Instruction to add to a transaction
|
|
4661
4395
|
*/
|
|
4662
|
-
function createInitializeMint2Instruction(
|
|
4663
|
-
mint,
|
|
4664
|
-
decimals,
|
|
4665
|
-
mintAuthority,
|
|
4666
|
-
freezeAuthority,
|
|
4667
|
-
programId = TOKEN_PROGRAM_ID,
|
|
4668
|
-
) {
|
|
4396
|
+
function createInitializeMint2Instruction(mint, decimals, mintAuthority, freezeAuthority, programId = TOKEN_PROGRAM_ID) {
|
|
4669
4397
|
const keys = [{ pubkey: mint, isSigner: false, isWritable: true }];
|
|
4670
4398
|
const data = Buffer.alloc(initializeMint2InstructionData.span);
|
|
4671
|
-
initializeMint2InstructionData.encode(
|
|
4672
|
-
|
|
4673
|
-
|
|
4674
|
-
|
|
4675
|
-
|
|
4676
|
-
|
|
4677
|
-
},
|
|
4678
|
-
data,
|
|
4679
|
-
);
|
|
4399
|
+
initializeMint2InstructionData.encode({
|
|
4400
|
+
instruction: TokenInstruction.InitializeMint2,
|
|
4401
|
+
decimals,
|
|
4402
|
+
mintAuthority,
|
|
4403
|
+
freezeAuthority,
|
|
4404
|
+
}, data);
|
|
4680
4405
|
return new web3_js.TransactionInstruction({ keys, programId, data });
|
|
4681
4406
|
}
|
|
4682
4407
|
|
|
@@ -4695,77 +4420,39 @@ function createInitializeMint2Instruction(
|
|
|
4695
4420
|
*
|
|
4696
4421
|
* @return Address of the new associated token account
|
|
4697
4422
|
*/
|
|
4698
|
-
async function getOrCreateAssociatedTokenAccount(
|
|
4699
|
-
|
|
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
|
-
);
|
|
4423
|
+
async function getOrCreateAssociatedTokenAccount(connection, payer, mint, owner, allowOwnerOffCurve = false, commitment, confirmOptions, programId = TOKEN_PROGRAM_ID, associatedTokenProgramId = ASSOCIATED_TOKEN_PROGRAM_ID) {
|
|
4424
|
+
const associatedToken = getAssociatedTokenAddressSync(mint, owner, allowOwnerOffCurve, programId, associatedTokenProgramId);
|
|
4716
4425
|
// This is the optimal logic, considering TX fee, client-side computation, RPC roundtrips and guaranteed idempotent.
|
|
4717
4426
|
// Sadly we can't do this atomically.
|
|
4718
4427
|
let account;
|
|
4719
4428
|
try {
|
|
4720
|
-
account = await getAccount(
|
|
4721
|
-
|
|
4722
|
-
|
|
4723
|
-
commitment,
|
|
4724
|
-
programId,
|
|
4725
|
-
);
|
|
4726
|
-
} catch (error) {
|
|
4429
|
+
account = await getAccount(connection, associatedToken, commitment, programId);
|
|
4430
|
+
}
|
|
4431
|
+
catch (error) {
|
|
4727
4432
|
// TokenAccountNotFoundError can be possible if the associated address has already received some lamports,
|
|
4728
4433
|
// becoming a system account. Assuming program derived addressing is safe, this is the only case for the
|
|
4729
4434
|
// TokenInvalidAccountOwnerError in this code path.
|
|
4730
|
-
if (
|
|
4731
|
-
error instanceof TokenAccountNotFoundError ||
|
|
4732
|
-
error instanceof TokenInvalidAccountOwnerError
|
|
4733
|
-
) {
|
|
4435
|
+
if (error instanceof TokenAccountNotFoundError || error instanceof TokenInvalidAccountOwnerError) {
|
|
4734
4436
|
// As this isn't atomic, it's possible others can create associated accounts meanwhile.
|
|
4735
4437
|
try {
|
|
4736
|
-
const transaction = new web3_js.Transaction().add(
|
|
4737
|
-
|
|
4738
|
-
|
|
4739
|
-
|
|
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) {
|
|
4438
|
+
const transaction = new web3_js.Transaction().add(createAssociatedTokenAccountInstruction(payer.publicKey, associatedToken, owner, mint, programId, associatedTokenProgramId));
|
|
4439
|
+
await web3_js.sendAndConfirmTransaction(connection, transaction, [payer], confirmOptions);
|
|
4440
|
+
}
|
|
4441
|
+
catch (error) {
|
|
4753
4442
|
// Ignore all errors; for now there is no API-compatible way to selectively ignore the expected
|
|
4754
4443
|
// instruction error if the associated account exists already.
|
|
4755
4444
|
}
|
|
4756
4445
|
// Now this should always succeed
|
|
4757
|
-
account = await getAccount(
|
|
4758
|
-
|
|
4759
|
-
|
|
4760
|
-
commitment,
|
|
4761
|
-
programId,
|
|
4762
|
-
);
|
|
4763
|
-
} else {
|
|
4446
|
+
account = await getAccount(connection, associatedToken, commitment, programId);
|
|
4447
|
+
}
|
|
4448
|
+
else {
|
|
4764
4449
|
throw error;
|
|
4765
4450
|
}
|
|
4766
4451
|
}
|
|
4767
|
-
if (!account.mint.equals(mint))
|
|
4768
|
-
|
|
4452
|
+
if (!account.mint.equals(mint))
|
|
4453
|
+
throw new TokenInvalidMintError();
|
|
4454
|
+
if (!account.owner.equals(owner))
|
|
4455
|
+
throw new TokenInvalidOwnerError();
|
|
4769
4456
|
return account;
|
|
4770
4457
|
}
|
|
4771
4458
|
|
|
@@ -4783,46 +4470,29 @@ const mintToInstructionData = struct([u8('instruction'), u64('amount')]);
|
|
|
4783
4470
|
*
|
|
4784
4471
|
* @return Instruction to add to a transaction
|
|
4785
4472
|
*/
|
|
4786
|
-
function createMintToInstruction(
|
|
4787
|
-
|
|
4788
|
-
|
|
4789
|
-
|
|
4790
|
-
|
|
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
|
-
);
|
|
4473
|
+
function createMintToInstruction(mint, destination, authority, amount, multiSigners = [], programId = TOKEN_PROGRAM_ID) {
|
|
4474
|
+
const keys = addSigners([
|
|
4475
|
+
{ pubkey: mint, isSigner: false, isWritable: true },
|
|
4476
|
+
{ pubkey: destination, isSigner: false, isWritable: true },
|
|
4477
|
+
], authority, multiSigners);
|
|
4802
4478
|
const data = Buffer.alloc(mintToInstructionData.span);
|
|
4803
|
-
mintToInstructionData.encode(
|
|
4804
|
-
|
|
4805
|
-
|
|
4806
|
-
|
|
4807
|
-
},
|
|
4808
|
-
data,
|
|
4809
|
-
);
|
|
4479
|
+
mintToInstructionData.encode({
|
|
4480
|
+
instruction: TokenInstruction.MintTo,
|
|
4481
|
+
amount: BigInt(amount),
|
|
4482
|
+
}, data);
|
|
4810
4483
|
return new web3_js.TransactionInstruction({ keys, programId, data });
|
|
4811
4484
|
}
|
|
4812
4485
|
|
|
4813
4486
|
/**
|
|
4814
4487
|
* Sum up the token amounts of the compressed token accounts
|
|
4815
4488
|
*/
|
|
4816
|
-
const sumUpTokenAmount = accounts => {
|
|
4817
|
-
return accounts.reduce(
|
|
4818
|
-
(acc, account) => acc.add(account.parsed.amount),
|
|
4819
|
-
stateless_js.bn(0),
|
|
4820
|
-
);
|
|
4489
|
+
const sumUpTokenAmount = (accounts) => {
|
|
4490
|
+
return accounts.reduce((acc, account) => acc.add(account.parsed.amount), stateless_js.bn(0));
|
|
4821
4491
|
};
|
|
4822
4492
|
/**
|
|
4823
4493
|
* Validate that all the compressed token accounts are owned by the same owner.
|
|
4824
4494
|
*/
|
|
4825
|
-
const validateSameTokenOwner = accounts => {
|
|
4495
|
+
const validateSameTokenOwner = (accounts) => {
|
|
4826
4496
|
const owner = accounts[0].parsed.owner;
|
|
4827
4497
|
accounts.forEach(acc => {
|
|
4828
4498
|
if (!acc.parsed.owner.equals(owner)) {
|
|
@@ -4833,7 +4503,7 @@ const validateSameTokenOwner = accounts => {
|
|
|
4833
4503
|
/**
|
|
4834
4504
|
* Parse compressed token accounts to get the mint, current owner and delegate.
|
|
4835
4505
|
*/
|
|
4836
|
-
const parseTokenData = compressedTokenAccounts => {
|
|
4506
|
+
const parseTokenData = (compressedTokenAccounts) => {
|
|
4837
4507
|
const mint = compressedTokenAccounts[0].parsed.mint;
|
|
4838
4508
|
const currentOwner = compressedTokenAccounts[0].parsed.owner;
|
|
4839
4509
|
const delegate = compressedTokenAccounts[0].parsed.delegate;
|
|
@@ -4847,22 +4517,13 @@ const parseTokenData = compressedTokenAccounts => {
|
|
|
4847
4517
|
* @returns Output token data for the transfer
|
|
4848
4518
|
* instruction
|
|
4849
4519
|
*/
|
|
4850
|
-
function createTransferOutputState(
|
|
4851
|
-
inputCompressedTokenAccounts,
|
|
4852
|
-
toAddress,
|
|
4853
|
-
amount,
|
|
4854
|
-
) {
|
|
4520
|
+
function createTransferOutputState(inputCompressedTokenAccounts, toAddress, amount) {
|
|
4855
4521
|
amount = stateless_js.bn(amount);
|
|
4856
4522
|
const inputAmount = sumUpTokenAmount(inputCompressedTokenAccounts);
|
|
4857
|
-
const inputLamports = stateless_js.sumUpLamports(
|
|
4858
|
-
inputCompressedTokenAccounts.map(acc => acc.compressedAccount),
|
|
4859
|
-
);
|
|
4523
|
+
const inputLamports = stateless_js.sumUpLamports(inputCompressedTokenAccounts.map(acc => acc.compressedAccount));
|
|
4860
4524
|
const changeAmount = inputAmount.sub(amount);
|
|
4861
4525
|
stateless_js.validateSufficientBalance(changeAmount);
|
|
4862
|
-
if (
|
|
4863
|
-
changeAmount.eq(stateless_js.bn(0)) &&
|
|
4864
|
-
inputLamports.eq(stateless_js.bn(0))
|
|
4865
|
-
) {
|
|
4526
|
+
if (changeAmount.eq(stateless_js.bn(0)) && inputLamports.eq(stateless_js.bn(0))) {
|
|
4866
4527
|
return [
|
|
4867
4528
|
{
|
|
4868
4529
|
owner: toAddress,
|
|
@@ -4873,9 +4534,7 @@ function createTransferOutputState(
|
|
|
4873
4534
|
];
|
|
4874
4535
|
}
|
|
4875
4536
|
/// validates token program
|
|
4876
|
-
stateless_js.validateSameOwner(
|
|
4877
|
-
inputCompressedTokenAccounts.map(acc => acc.compressedAccount),
|
|
4878
|
-
);
|
|
4537
|
+
stateless_js.validateSameOwner(inputCompressedTokenAccounts.map(acc => acc.compressedAccount));
|
|
4879
4538
|
validateSameTokenOwner(inputCompressedTokenAccounts);
|
|
4880
4539
|
const outputCompressedAccounts = [
|
|
4881
4540
|
{
|
|
@@ -4902,22 +4561,15 @@ function createTransferOutputState(
|
|
|
4902
4561
|
*/
|
|
4903
4562
|
function createDecompressOutputState(inputCompressedTokenAccounts, amount) {
|
|
4904
4563
|
amount = stateless_js.bn(amount);
|
|
4905
|
-
const inputLamports = stateless_js.sumUpLamports(
|
|
4906
|
-
inputCompressedTokenAccounts.map(acc => acc.compressedAccount),
|
|
4907
|
-
);
|
|
4564
|
+
const inputLamports = stateless_js.sumUpLamports(inputCompressedTokenAccounts.map(acc => acc.compressedAccount));
|
|
4908
4565
|
const inputAmount = sumUpTokenAmount(inputCompressedTokenAccounts);
|
|
4909
4566
|
const changeAmount = inputAmount.sub(amount);
|
|
4910
4567
|
stateless_js.validateSufficientBalance(changeAmount);
|
|
4911
4568
|
/// lamports gets decompressed
|
|
4912
|
-
if (
|
|
4913
|
-
changeAmount.eq(stateless_js.bn(0)) &&
|
|
4914
|
-
inputLamports.eq(stateless_js.bn(0))
|
|
4915
|
-
) {
|
|
4569
|
+
if (changeAmount.eq(stateless_js.bn(0)) && inputLamports.eq(stateless_js.bn(0))) {
|
|
4916
4570
|
return [];
|
|
4917
4571
|
}
|
|
4918
|
-
stateless_js.validateSameOwner(
|
|
4919
|
-
inputCompressedTokenAccounts.map(acc => acc.compressedAccount),
|
|
4920
|
-
);
|
|
4572
|
+
stateless_js.validateSameOwner(inputCompressedTokenAccounts.map(acc => acc.compressedAccount));
|
|
4921
4573
|
validateSameTokenOwner(inputCompressedTokenAccounts);
|
|
4922
4574
|
const tokenTransferOutputs = [
|
|
4923
4575
|
{
|
|
@@ -4933,7 +4585,7 @@ class CompressedTokenProgram {
|
|
|
4933
4585
|
/**
|
|
4934
4586
|
* @internal
|
|
4935
4587
|
*/
|
|
4936
|
-
constructor() {}
|
|
4588
|
+
constructor() { }
|
|
4937
4589
|
/** @internal */
|
|
4938
4590
|
static get program() {
|
|
4939
4591
|
if (!this._program) {
|
|
@@ -4951,38 +4603,21 @@ class CompressedTokenProgram {
|
|
|
4951
4603
|
/// program only for serde and building instructions, not for
|
|
4952
4604
|
/// interacting with the network.
|
|
4953
4605
|
const mockKeypair = web3_js.Keypair.generate();
|
|
4954
|
-
const mockConnection = new web3_js.Connection(
|
|
4955
|
-
|
|
4956
|
-
'confirmed',
|
|
4957
|
-
);
|
|
4958
|
-
const mockProvider = new anchor.AnchorProvider(
|
|
4959
|
-
mockConnection,
|
|
4960
|
-
stateless_js.useWallet(mockKeypair),
|
|
4961
|
-
stateless_js.confirmConfig,
|
|
4962
|
-
);
|
|
4606
|
+
const mockConnection = new web3_js.Connection('http://127.0.0.1:8899', 'confirmed');
|
|
4607
|
+
const mockProvider = new anchor.AnchorProvider(mockConnection, stateless_js.useWallet(mockKeypair), stateless_js.confirmConfig);
|
|
4963
4608
|
anchor.setProvider(mockProvider);
|
|
4964
|
-
this._program = new anchor.Program(
|
|
4965
|
-
IDL,
|
|
4966
|
-
this.programId,
|
|
4967
|
-
mockProvider,
|
|
4968
|
-
);
|
|
4609
|
+
this._program = new anchor.Program(IDL, this.programId, mockProvider);
|
|
4969
4610
|
}
|
|
4970
4611
|
}
|
|
4971
4612
|
/** @internal */
|
|
4972
4613
|
static deriveTokenPoolPda(mint) {
|
|
4973
4614
|
const seeds = [POOL_SEED, mint.toBuffer()];
|
|
4974
|
-
const [address, _] = web3_js.PublicKey.findProgramAddressSync(
|
|
4975
|
-
seeds,
|
|
4976
|
-
this.programId,
|
|
4977
|
-
);
|
|
4615
|
+
const [address, _] = web3_js.PublicKey.findProgramAddressSync(seeds, this.programId);
|
|
4978
4616
|
return address;
|
|
4979
4617
|
}
|
|
4980
4618
|
/** @internal */
|
|
4981
4619
|
static get deriveCpiAuthorityPda() {
|
|
4982
|
-
const [address, _] = web3_js.PublicKey.findProgramAddressSync(
|
|
4983
|
-
[CPI_AUTHORITY_SEED],
|
|
4984
|
-
this.programId,
|
|
4985
|
-
);
|
|
4620
|
+
const [address, _] = web3_js.PublicKey.findProgramAddressSync([CPI_AUTHORITY_SEED], this.programId);
|
|
4986
4621
|
return address;
|
|
4987
4622
|
}
|
|
4988
4623
|
/**
|
|
@@ -4991,21 +4626,14 @@ class CompressedTokenProgram {
|
|
|
4991
4626
|
static async createMint(params) {
|
|
4992
4627
|
const { mint, authority, feePayer, rentExemptBalance } = params;
|
|
4993
4628
|
/// Create and initialize SPL Mint account
|
|
4994
|
-
const createMintAccountInstruction =
|
|
4995
|
-
|
|
4996
|
-
|
|
4997
|
-
|
|
4998
|
-
|
|
4999
|
-
|
|
5000
|
-
|
|
5001
|
-
|
|
5002
|
-
const initializeMintInstruction = createInitializeMint2Instruction(
|
|
5003
|
-
mint,
|
|
5004
|
-
params.decimals,
|
|
5005
|
-
authority,
|
|
5006
|
-
params.freezeAuthority,
|
|
5007
|
-
TOKEN_PROGRAM_ID,
|
|
5008
|
-
);
|
|
4629
|
+
const createMintAccountInstruction = web3_js.SystemProgram.createAccount({
|
|
4630
|
+
fromPubkey: feePayer,
|
|
4631
|
+
lamports: rentExemptBalance,
|
|
4632
|
+
newAccountPubkey: mint,
|
|
4633
|
+
programId: TOKEN_PROGRAM_ID,
|
|
4634
|
+
space: MINT_SIZE,
|
|
4635
|
+
});
|
|
4636
|
+
const initializeMintInstruction = createInitializeMint2Instruction(mint, params.decimals, authority, params.freezeAuthority, TOKEN_PROGRAM_ID);
|
|
5009
4637
|
const ix = await this.createTokenPool({
|
|
5010
4638
|
feePayer,
|
|
5011
4639
|
mint,
|
|
@@ -5022,13 +4650,13 @@ class CompressedTokenProgram {
|
|
|
5022
4650
|
const ix = await this.program.methods
|
|
5023
4651
|
.createTokenPool()
|
|
5024
4652
|
.accounts({
|
|
5025
|
-
|
|
5026
|
-
|
|
5027
|
-
|
|
5028
|
-
|
|
5029
|
-
|
|
5030
|
-
|
|
5031
|
-
|
|
4653
|
+
mint,
|
|
4654
|
+
feePayer,
|
|
4655
|
+
tokenPoolPda,
|
|
4656
|
+
systemProgram: web3_js.SystemProgram.programId,
|
|
4657
|
+
tokenProgram: TOKEN_PROGRAM_ID,
|
|
4658
|
+
cpiAuthorityPda: this.deriveCpiAuthorityPda,
|
|
4659
|
+
})
|
|
5032
4660
|
.instruction();
|
|
5033
4661
|
return ix;
|
|
5034
4662
|
}
|
|
@@ -5037,41 +4665,31 @@ class CompressedTokenProgram {
|
|
|
5037
4665
|
*/
|
|
5038
4666
|
static async mintTo(params) {
|
|
5039
4667
|
const systemKeys = stateless_js.defaultStaticAccountsStruct();
|
|
5040
|
-
const { mint, feePayer, authority, merkleTree, toPubkey, amount } =
|
|
5041
|
-
params;
|
|
4668
|
+
const { mint, feePayer, authority, merkleTree, toPubkey, amount } = params;
|
|
5042
4669
|
const tokenPoolPda = this.deriveTokenPoolPda(mint);
|
|
5043
|
-
const amounts = stateless_js
|
|
5044
|
-
.toArray(amount)
|
|
5045
|
-
.map(amount => stateless_js.bn(amount));
|
|
4670
|
+
const amounts = stateless_js.toArray(amount).map(amount => stateless_js.bn(amount));
|
|
5046
4671
|
const toPubkeys = stateless_js.toArray(toPubkey);
|
|
5047
4672
|
if (amounts.length !== toPubkeys.length) {
|
|
5048
|
-
throw new Error(
|
|
5049
|
-
'Amount and toPubkey arrays must have the same length',
|
|
5050
|
-
);
|
|
4673
|
+
throw new Error('Amount and toPubkey arrays must have the same length');
|
|
5051
4674
|
}
|
|
5052
4675
|
const instruction = await this.program.methods
|
|
5053
4676
|
.mintTo(toPubkeys, amounts, null)
|
|
5054
4677
|
.accounts({
|
|
5055
|
-
|
|
5056
|
-
|
|
5057
|
-
|
|
5058
|
-
|
|
5059
|
-
|
|
5060
|
-
|
|
5061
|
-
|
|
5062
|
-
|
|
5063
|
-
|
|
5064
|
-
|
|
5065
|
-
|
|
5066
|
-
|
|
5067
|
-
|
|
5068
|
-
|
|
5069
|
-
|
|
5070
|
-
: stateless_js.defaultTestStateTreeAccounts()
|
|
5071
|
-
.merkleTree,
|
|
5072
|
-
selfProgram: this.programId,
|
|
5073
|
-
solPoolPda: null,
|
|
5074
|
-
})
|
|
4678
|
+
feePayer,
|
|
4679
|
+
authority,
|
|
4680
|
+
cpiAuthorityPda: this.deriveCpiAuthorityPda,
|
|
4681
|
+
mint,
|
|
4682
|
+
tokenPoolPda,
|
|
4683
|
+
tokenProgram: TOKEN_PROGRAM_ID,
|
|
4684
|
+
lightSystemProgram: stateless_js.LightSystemProgram.programId,
|
|
4685
|
+
registeredProgramPda: systemKeys.registeredProgramPda,
|
|
4686
|
+
noopProgram: systemKeys.noopProgram,
|
|
4687
|
+
accountCompressionAuthority: systemKeys.accountCompressionAuthority,
|
|
4688
|
+
accountCompressionProgram: systemKeys.accountCompressionProgram,
|
|
4689
|
+
merkleTree: merkleTree !== null && merkleTree !== void 0 ? merkleTree : stateless_js.defaultTestStateTreeAccounts().merkleTree,
|
|
4690
|
+
selfProgram: this.programId,
|
|
4691
|
+
solPoolPda: null,
|
|
4692
|
+
})
|
|
5075
4693
|
.instruction();
|
|
5076
4694
|
return instruction;
|
|
5077
4695
|
}
|
|
@@ -5080,22 +4698,10 @@ class CompressedTokenProgram {
|
|
|
5080
4698
|
* Mint tokens from registed SPL mint account to a compressed account
|
|
5081
4699
|
*/
|
|
5082
4700
|
static async approveAndMintTo(params) {
|
|
5083
|
-
const {
|
|
5084
|
-
mint,
|
|
5085
|
-
feePayer,
|
|
5086
|
-
authorityTokenAccount,
|
|
5087
|
-
authority,
|
|
5088
|
-
merkleTree,
|
|
5089
|
-
toPubkey,
|
|
5090
|
-
} = params;
|
|
4701
|
+
const { mint, feePayer, authorityTokenAccount, authority, merkleTree, toPubkey, } = params;
|
|
5091
4702
|
const amount = BigInt(params.amount.toString());
|
|
5092
4703
|
/// 1. Mint to existing ATA of mintAuthority.
|
|
5093
|
-
const splMintToInstruction = createMintToInstruction(
|
|
5094
|
-
mint,
|
|
5095
|
-
authorityTokenAccount,
|
|
5096
|
-
authority,
|
|
5097
|
-
amount,
|
|
5098
|
-
);
|
|
4704
|
+
const splMintToInstruction = createMintToInstruction(mint, authorityTokenAccount, authority, amount);
|
|
5099
4705
|
/// 2. Compress from mint authority ATA to recipient compressed account
|
|
5100
4706
|
const compressInstruction = await this.compress({
|
|
5101
4707
|
payer: feePayer,
|
|
@@ -5112,33 +4718,15 @@ class CompressedTokenProgram {
|
|
|
5112
4718
|
* Construct transfer instruction for compressed tokens
|
|
5113
4719
|
*/
|
|
5114
4720
|
static async transfer(params) {
|
|
5115
|
-
const {
|
|
5116
|
-
|
|
5117
|
-
|
|
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({
|
|
4721
|
+
const { payer, inputCompressedTokenAccounts, recentInputStateRootIndices, recentValidityProof, amount, outputStateTrees, toAddress, } = params;
|
|
4722
|
+
const tokenTransferOutputs = createTransferOutputState(inputCompressedTokenAccounts, toAddress, amount);
|
|
4723
|
+
const { inputTokenDataWithContext, packedOutputTokenData, remainingAccountMetas, } = packCompressedTokenAccounts({
|
|
5134
4724
|
inputCompressedTokenAccounts,
|
|
5135
4725
|
outputStateTrees,
|
|
5136
4726
|
rootIndices: recentInputStateRootIndices,
|
|
5137
4727
|
tokenTransferOutputs,
|
|
5138
4728
|
});
|
|
5139
|
-
const { mint, currentOwner } = parseTokenData(
|
|
5140
|
-
inputCompressedTokenAccounts,
|
|
5141
|
-
);
|
|
4729
|
+
const { mint, currentOwner } = parseTokenData(inputCompressedTokenAccounts);
|
|
5142
4730
|
const data = {
|
|
5143
4731
|
proof: recentValidityProof,
|
|
5144
4732
|
mint,
|
|
@@ -5150,32 +4738,24 @@ class CompressedTokenProgram {
|
|
|
5150
4738
|
cpiContext: null,
|
|
5151
4739
|
lamportsChangeAccountMerkleTreeIndex: null,
|
|
5152
4740
|
};
|
|
5153
|
-
const encodedData = this.program.coder.types.encode(
|
|
5154
|
-
|
|
5155
|
-
data,
|
|
5156
|
-
);
|
|
5157
|
-
const {
|
|
5158
|
-
accountCompressionAuthority,
|
|
5159
|
-
noopProgram,
|
|
5160
|
-
registeredProgramPda,
|
|
5161
|
-
accountCompressionProgram,
|
|
5162
|
-
} = stateless_js.defaultStaticAccountsStruct();
|
|
4741
|
+
const encodedData = this.program.coder.types.encode('CompressedTokenInstructionDataTransfer', data);
|
|
4742
|
+
const { accountCompressionAuthority, noopProgram, registeredProgramPda, accountCompressionProgram, } = stateless_js.defaultStaticAccountsStruct();
|
|
5163
4743
|
const instruction = await this.program.methods
|
|
5164
4744
|
.transfer(encodedData)
|
|
5165
4745
|
.accounts({
|
|
5166
|
-
|
|
5167
|
-
|
|
5168
|
-
|
|
5169
|
-
|
|
5170
|
-
|
|
5171
|
-
|
|
5172
|
-
|
|
5173
|
-
|
|
5174
|
-
|
|
5175
|
-
|
|
5176
|
-
|
|
5177
|
-
|
|
5178
|
-
|
|
4746
|
+
feePayer: payer,
|
|
4747
|
+
authority: currentOwner,
|
|
4748
|
+
cpiAuthorityPda: this.deriveCpiAuthorityPda,
|
|
4749
|
+
lightSystemProgram: stateless_js.LightSystemProgram.programId,
|
|
4750
|
+
registeredProgramPda: registeredProgramPda,
|
|
4751
|
+
noopProgram: noopProgram,
|
|
4752
|
+
accountCompressionAuthority: accountCompressionAuthority,
|
|
4753
|
+
accountCompressionProgram: accountCompressionProgram,
|
|
4754
|
+
selfProgram: this.programId,
|
|
4755
|
+
tokenPoolPda: null,
|
|
4756
|
+
compressOrDecompressTokenAccount: null,
|
|
4757
|
+
tokenProgram: null,
|
|
4758
|
+
})
|
|
5179
4759
|
.remainingAccounts(remainingAccountMetas)
|
|
5180
4760
|
.instruction();
|
|
5181
4761
|
return instruction;
|
|
@@ -5184,14 +4764,12 @@ class CompressedTokenProgram {
|
|
|
5184
4764
|
* Create lookup table instructions for the token program's default accounts.
|
|
5185
4765
|
*/
|
|
5186
4766
|
static async createTokenProgramLookupTable(params) {
|
|
5187
|
-
const { authority, mints, recentSlot, payer, remainingAccounts } =
|
|
5188
|
-
|
|
5189
|
-
|
|
5190
|
-
|
|
5191
|
-
|
|
5192
|
-
|
|
5193
|
-
recentSlot,
|
|
5194
|
-
});
|
|
4767
|
+
const { authority, mints, recentSlot, payer, remainingAccounts } = params;
|
|
4768
|
+
const [createInstruction, lookupTableAddress] = web3_js.AddressLookupTableProgram.createLookupTable({
|
|
4769
|
+
authority,
|
|
4770
|
+
payer: authority,
|
|
4771
|
+
recentSlot,
|
|
4772
|
+
});
|
|
5195
4773
|
let optionalMintKeys = [];
|
|
5196
4774
|
if (mints) {
|
|
5197
4775
|
optionalMintKeys = [
|
|
@@ -5199,35 +4777,28 @@ class CompressedTokenProgram {
|
|
|
5199
4777
|
...mints.map(mint => this.deriveTokenPoolPda(mint)),
|
|
5200
4778
|
];
|
|
5201
4779
|
}
|
|
5202
|
-
const extendInstruction =
|
|
5203
|
-
|
|
5204
|
-
|
|
4780
|
+
const extendInstruction = web3_js.AddressLookupTableProgram.extendLookupTable({
|
|
4781
|
+
payer,
|
|
4782
|
+
authority,
|
|
4783
|
+
lookupTable: lookupTableAddress,
|
|
4784
|
+
addresses: [
|
|
4785
|
+
this.deriveCpiAuthorityPda,
|
|
4786
|
+
stateless_js.LightSystemProgram.programId,
|
|
4787
|
+
stateless_js.defaultStaticAccountsStruct().registeredProgramPda,
|
|
4788
|
+
stateless_js.defaultStaticAccountsStruct().noopProgram,
|
|
4789
|
+
stateless_js.defaultStaticAccountsStruct().accountCompressionAuthority,
|
|
4790
|
+
stateless_js.defaultStaticAccountsStruct().accountCompressionProgram,
|
|
4791
|
+
stateless_js.defaultTestStateTreeAccounts().merkleTree,
|
|
4792
|
+
stateless_js.defaultTestStateTreeAccounts().nullifierQueue,
|
|
4793
|
+
stateless_js.defaultTestStateTreeAccounts().addressTree,
|
|
4794
|
+
stateless_js.defaultTestStateTreeAccounts().addressQueue,
|
|
4795
|
+
this.programId,
|
|
4796
|
+
TOKEN_PROGRAM_ID,
|
|
5205
4797
|
authority,
|
|
5206
|
-
|
|
5207
|
-
|
|
5208
|
-
|
|
5209
|
-
|
|
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
|
-
});
|
|
4798
|
+
...optionalMintKeys,
|
|
4799
|
+
...(remainingAccounts !== null && remainingAccounts !== void 0 ? remainingAccounts : []),
|
|
4800
|
+
],
|
|
4801
|
+
});
|
|
5231
4802
|
return {
|
|
5232
4803
|
instructions: [createInstruction, extendInstruction],
|
|
5233
4804
|
address: lookupTableAddress,
|
|
@@ -5238,19 +4809,14 @@ class CompressedTokenProgram {
|
|
|
5238
4809
|
* @returns compressInstruction
|
|
5239
4810
|
*/
|
|
5240
4811
|
static async compress(params) {
|
|
5241
|
-
const { payer, owner, source, toAddress, mint, outputStateTree } =
|
|
5242
|
-
params;
|
|
4812
|
+
const { payer, owner, source, toAddress, mint, outputStateTree } = params;
|
|
5243
4813
|
if (Array.isArray(params.amount) !== Array.isArray(params.toAddress)) {
|
|
5244
|
-
throw new Error(
|
|
5245
|
-
'Both amount and toAddress must be arrays or both must be single values',
|
|
5246
|
-
);
|
|
4814
|
+
throw new Error('Both amount and toAddress must be arrays or both must be single values');
|
|
5247
4815
|
}
|
|
5248
4816
|
let tokenTransferOutputs;
|
|
5249
4817
|
if (Array.isArray(params.amount) && Array.isArray(params.toAddress)) {
|
|
5250
4818
|
if (params.amount.length !== params.toAddress.length) {
|
|
5251
|
-
throw new Error(
|
|
5252
|
-
'Amount and toAddress arrays must have the same length',
|
|
5253
|
-
);
|
|
4819
|
+
throw new Error('Amount and toAddress arrays must have the same length');
|
|
5254
4820
|
}
|
|
5255
4821
|
tokenTransferOutputs = params.amount.map((amt, index) => {
|
|
5256
4822
|
const amount = stateless_js.bn(amt);
|
|
@@ -5261,7 +4827,8 @@ class CompressedTokenProgram {
|
|
|
5261
4827
|
tlv: null,
|
|
5262
4828
|
};
|
|
5263
4829
|
});
|
|
5264
|
-
}
|
|
4830
|
+
}
|
|
4831
|
+
else {
|
|
5265
4832
|
tokenTransferOutputs = [
|
|
5266
4833
|
{
|
|
5267
4834
|
owner: toAddress,
|
|
@@ -5271,11 +4838,7 @@ class CompressedTokenProgram {
|
|
|
5271
4838
|
},
|
|
5272
4839
|
];
|
|
5273
4840
|
}
|
|
5274
|
-
const {
|
|
5275
|
-
inputTokenDataWithContext,
|
|
5276
|
-
packedOutputTokenData,
|
|
5277
|
-
remainingAccountMetas,
|
|
5278
|
-
} = packCompressedTokenAccounts({
|
|
4841
|
+
const { inputTokenDataWithContext, packedOutputTokenData, remainingAccountMetas, } = packCompressedTokenAccounts({
|
|
5279
4842
|
inputCompressedTokenAccounts: [],
|
|
5280
4843
|
outputStateTrees: outputStateTree,
|
|
5281
4844
|
rootIndices: [],
|
|
@@ -5289,40 +4852,30 @@ class CompressedTokenProgram {
|
|
|
5289
4852
|
outputCompressedAccounts: packedOutputTokenData,
|
|
5290
4853
|
compressOrDecompressAmount: Array.isArray(params.amount)
|
|
5291
4854
|
? params.amount
|
|
5292
|
-
|
|
5293
|
-
|
|
4855
|
+
.map(amt => new anchor.BN(amt))
|
|
4856
|
+
.reduce((sum, amt) => sum.add(amt), new anchor.BN(0))
|
|
5294
4857
|
: new anchor.BN(params.amount),
|
|
5295
4858
|
isCompress: true,
|
|
5296
4859
|
cpiContext: null,
|
|
5297
4860
|
lamportsChangeAccountMerkleTreeIndex: null,
|
|
5298
4861
|
};
|
|
5299
|
-
const encodedData = this.program.coder.types.encode(
|
|
5300
|
-
'CompressedTokenInstructionDataTransfer',
|
|
5301
|
-
data,
|
|
5302
|
-
);
|
|
4862
|
+
const encodedData = this.program.coder.types.encode('CompressedTokenInstructionDataTransfer', data);
|
|
5303
4863
|
const instruction = await this.program.methods
|
|
5304
4864
|
.transfer(encodedData)
|
|
5305
4865
|
.accounts({
|
|
5306
|
-
|
|
5307
|
-
|
|
5308
|
-
|
|
5309
|
-
|
|
5310
|
-
|
|
5311
|
-
|
|
5312
|
-
|
|
5313
|
-
|
|
5314
|
-
|
|
5315
|
-
|
|
5316
|
-
|
|
5317
|
-
|
|
5318
|
-
|
|
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
|
-
})
|
|
4866
|
+
feePayer: payer,
|
|
4867
|
+
authority: owner,
|
|
4868
|
+
cpiAuthorityPda: this.deriveCpiAuthorityPda,
|
|
4869
|
+
lightSystemProgram: stateless_js.LightSystemProgram.programId,
|
|
4870
|
+
registeredProgramPda: stateless_js.defaultStaticAccountsStruct().registeredProgramPda,
|
|
4871
|
+
noopProgram: stateless_js.defaultStaticAccountsStruct().noopProgram,
|
|
4872
|
+
accountCompressionAuthority: stateless_js.defaultStaticAccountsStruct().accountCompressionAuthority,
|
|
4873
|
+
accountCompressionProgram: stateless_js.defaultStaticAccountsStruct().accountCompressionProgram,
|
|
4874
|
+
selfProgram: this.programId,
|
|
4875
|
+
tokenPoolPda: this.deriveTokenPoolPda(mint),
|
|
4876
|
+
compressOrDecompressTokenAccount: source, // token
|
|
4877
|
+
tokenProgram: TOKEN_PROGRAM_ID,
|
|
4878
|
+
})
|
|
5326
4879
|
.remainingAccounts(remainingAccountMetas)
|
|
5327
4880
|
.instruction();
|
|
5328
4881
|
return instruction;
|
|
@@ -5331,33 +4884,17 @@ class CompressedTokenProgram {
|
|
|
5331
4884
|
* Construct decompress instruction
|
|
5332
4885
|
*/
|
|
5333
4886
|
static async decompress(params) {
|
|
5334
|
-
const {
|
|
5335
|
-
payer,
|
|
5336
|
-
inputCompressedTokenAccounts,
|
|
5337
|
-
toAddress,
|
|
5338
|
-
outputStateTree,
|
|
5339
|
-
recentValidityProof,
|
|
5340
|
-
recentInputStateRootIndices,
|
|
5341
|
-
} = params;
|
|
4887
|
+
const { payer, inputCompressedTokenAccounts, toAddress, outputStateTree, recentValidityProof, recentInputStateRootIndices, } = params;
|
|
5342
4888
|
const amount = stateless_js.bn(params.amount);
|
|
5343
|
-
const tokenTransferOutputs = createDecompressOutputState(
|
|
5344
|
-
inputCompressedTokenAccounts,
|
|
5345
|
-
amount,
|
|
5346
|
-
);
|
|
4889
|
+
const tokenTransferOutputs = createDecompressOutputState(inputCompressedTokenAccounts, amount);
|
|
5347
4890
|
/// Pack
|
|
5348
|
-
const {
|
|
5349
|
-
inputTokenDataWithContext,
|
|
5350
|
-
packedOutputTokenData,
|
|
5351
|
-
remainingAccountMetas,
|
|
5352
|
-
} = packCompressedTokenAccounts({
|
|
4891
|
+
const { inputTokenDataWithContext, packedOutputTokenData, remainingAccountMetas, } = packCompressedTokenAccounts({
|
|
5353
4892
|
inputCompressedTokenAccounts,
|
|
5354
4893
|
outputStateTrees: outputStateTree,
|
|
5355
4894
|
rootIndices: recentInputStateRootIndices,
|
|
5356
4895
|
tokenTransferOutputs: tokenTransferOutputs,
|
|
5357
4896
|
});
|
|
5358
|
-
const { mint, currentOwner } = parseTokenData(
|
|
5359
|
-
inputCompressedTokenAccounts,
|
|
5360
|
-
);
|
|
4897
|
+
const { mint, currentOwner } = parseTokenData(inputCompressedTokenAccounts);
|
|
5361
4898
|
const data = {
|
|
5362
4899
|
proof: recentValidityProof,
|
|
5363
4900
|
mint,
|
|
@@ -5369,45 +4906,30 @@ class CompressedTokenProgram {
|
|
|
5369
4906
|
cpiContext: null,
|
|
5370
4907
|
lamportsChangeAccountMerkleTreeIndex: null,
|
|
5371
4908
|
};
|
|
5372
|
-
const encodedData = this.program.coder.types.encode(
|
|
5373
|
-
|
|
5374
|
-
data,
|
|
5375
|
-
);
|
|
5376
|
-
const {
|
|
5377
|
-
accountCompressionAuthority,
|
|
5378
|
-
noopProgram,
|
|
5379
|
-
registeredProgramPda,
|
|
5380
|
-
accountCompressionProgram,
|
|
5381
|
-
} = stateless_js.defaultStaticAccountsStruct();
|
|
4909
|
+
const encodedData = this.program.coder.types.encode('CompressedTokenInstructionDataTransfer', data);
|
|
4910
|
+
const { accountCompressionAuthority, noopProgram, registeredProgramPda, accountCompressionProgram, } = stateless_js.defaultStaticAccountsStruct();
|
|
5382
4911
|
const instruction = await this.program.methods
|
|
5383
4912
|
.transfer(encodedData)
|
|
5384
4913
|
.accounts({
|
|
5385
|
-
|
|
5386
|
-
|
|
5387
|
-
|
|
5388
|
-
|
|
5389
|
-
|
|
5390
|
-
|
|
5391
|
-
|
|
5392
|
-
|
|
5393
|
-
|
|
5394
|
-
|
|
5395
|
-
|
|
5396
|
-
|
|
5397
|
-
|
|
4914
|
+
feePayer: payer,
|
|
4915
|
+
authority: currentOwner,
|
|
4916
|
+
cpiAuthorityPda: this.deriveCpiAuthorityPda,
|
|
4917
|
+
lightSystemProgram: stateless_js.LightSystemProgram.programId,
|
|
4918
|
+
registeredProgramPda: registeredProgramPda,
|
|
4919
|
+
noopProgram: noopProgram,
|
|
4920
|
+
accountCompressionAuthority: accountCompressionAuthority,
|
|
4921
|
+
accountCompressionProgram: accountCompressionProgram,
|
|
4922
|
+
selfProgram: this.programId,
|
|
4923
|
+
tokenPoolPda: this.deriveTokenPoolPda(mint),
|
|
4924
|
+
compressOrDecompressTokenAccount: toAddress,
|
|
4925
|
+
tokenProgram: TOKEN_PROGRAM_ID,
|
|
4926
|
+
})
|
|
5398
4927
|
.remainingAccounts(remainingAccountMetas)
|
|
5399
4928
|
.instruction();
|
|
5400
4929
|
return instruction;
|
|
5401
4930
|
}
|
|
5402
4931
|
static async mergeTokenAccounts(params) {
|
|
5403
|
-
const {
|
|
5404
|
-
payer,
|
|
5405
|
-
owner,
|
|
5406
|
-
inputCompressedTokenAccounts,
|
|
5407
|
-
outputStateTree,
|
|
5408
|
-
recentValidityProof,
|
|
5409
|
-
recentInputStateRootIndices,
|
|
5410
|
-
} = params;
|
|
4932
|
+
const { payer, owner, inputCompressedTokenAccounts, outputStateTree, recentValidityProof, recentInputStateRootIndices, } = params;
|
|
5411
4933
|
if (inputCompressedTokenAccounts.length > 3) {
|
|
5412
4934
|
throw new Error('Cannot merge more than 3 token accounts at once');
|
|
5413
4935
|
}
|
|
@@ -5415,10 +4937,7 @@ class CompressedTokenProgram {
|
|
|
5415
4937
|
payer,
|
|
5416
4938
|
inputCompressedTokenAccounts,
|
|
5417
4939
|
toAddress: owner,
|
|
5418
|
-
amount: inputCompressedTokenAccounts.reduce(
|
|
5419
|
-
(sum, account) => sum.add(account.parsed.amount),
|
|
5420
|
-
new anchor.BN(0),
|
|
5421
|
-
),
|
|
4940
|
+
amount: inputCompressedTokenAccounts.reduce((sum, account) => sum.add(account.parsed.amount), new anchor.BN(0)),
|
|
5422
4941
|
outputStateTrees: outputStateTree,
|
|
5423
4942
|
recentInputStateRootIndices,
|
|
5424
4943
|
recentValidityProof,
|
|
@@ -5429,9 +4948,7 @@ class CompressedTokenProgram {
|
|
|
5429
4948
|
/**
|
|
5430
4949
|
* Public key that identifies the CompressedPda program
|
|
5431
4950
|
*/
|
|
5432
|
-
CompressedTokenProgram.programId = new web3_js.PublicKey(
|
|
5433
|
-
'cTokenmWW8bLPjZEBAUgYy3zKxQZW6VKi7bqNFEVv3m',
|
|
5434
|
-
);
|
|
4951
|
+
CompressedTokenProgram.programId = new web3_js.PublicKey('cTokenmWW8bLPjZEBAUgYy3zKxQZW6VKi7bqNFEVv3m');
|
|
5435
4952
|
CompressedTokenProgram._program = null;
|
|
5436
4953
|
|
|
5437
4954
|
/**
|
|
@@ -5449,22 +4966,8 @@ CompressedTokenProgram._program = null;
|
|
|
5449
4966
|
*
|
|
5450
4967
|
* @return Signature of the confirmed transaction
|
|
5451
4968
|
*/
|
|
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
|
-
);
|
|
4969
|
+
async function approveAndMintTo(rpc, payer, mint, destination, authority, amount, merkleTree, confirmOptions) {
|
|
4970
|
+
const authorityTokenAccount = await getOrCreateAssociatedTokenAccount(rpc, payer, mint, authority.publicKey);
|
|
5468
4971
|
const ixs = await CompressedTokenProgram.approveAndMintTo({
|
|
5469
4972
|
feePayer: payer.publicKey,
|
|
5470
4973
|
mint,
|
|
@@ -5476,17 +4979,10 @@ async function approveAndMintTo(
|
|
|
5476
4979
|
});
|
|
5477
4980
|
const { blockhash } = await rpc.getLatestBlockhash();
|
|
5478
4981
|
const additionalSigners = stateless_js.dedupeSigner(payer, [authority]);
|
|
5479
|
-
const tx = stateless_js.buildAndSignTx(
|
|
5480
|
-
|
|
5481
|
-
|
|
5482
|
-
|
|
5483
|
-
}),
|
|
5484
|
-
...ixs,
|
|
5485
|
-
],
|
|
5486
|
-
payer,
|
|
5487
|
-
blockhash,
|
|
5488
|
-
additionalSigners,
|
|
5489
|
-
);
|
|
4982
|
+
const tx = stateless_js.buildAndSignTx([
|
|
4983
|
+
web3_js.ComputeBudgetProgram.setComputeUnitLimit({ units: 1000000 }),
|
|
4984
|
+
...ixs,
|
|
4985
|
+
], payer, blockhash, additionalSigners);
|
|
5490
4986
|
const txId = await stateless_js.sendAndConfirmTx(rpc, tx, confirmOptions);
|
|
5491
4987
|
return txId;
|
|
5492
4988
|
}
|
|
@@ -5509,17 +5005,7 @@ async function approveAndMintTo(
|
|
|
5509
5005
|
*
|
|
5510
5006
|
* @return Signature of the confirmed transaction
|
|
5511
5007
|
*/
|
|
5512
|
-
async function compress(
|
|
5513
|
-
rpc,
|
|
5514
|
-
payer,
|
|
5515
|
-
mint,
|
|
5516
|
-
amount,
|
|
5517
|
-
owner,
|
|
5518
|
-
sourceTokenAccount,
|
|
5519
|
-
toAddress,
|
|
5520
|
-
merkleTree,
|
|
5521
|
-
confirmOptions,
|
|
5522
|
-
) {
|
|
5008
|
+
async function compress(rpc, payer, mint, amount, owner, sourceTokenAccount, toAddress, merkleTree, confirmOptions) {
|
|
5523
5009
|
const compressIx = await CompressedTokenProgram.compress({
|
|
5524
5010
|
payer: payer.publicKey,
|
|
5525
5011
|
owner: owner.publicKey,
|
|
@@ -5531,23 +5017,13 @@ async function compress(
|
|
|
5531
5017
|
});
|
|
5532
5018
|
const blockhashCtx = await rpc.getLatestBlockhash();
|
|
5533
5019
|
const additionalSigners = stateless_js.dedupeSigner(payer, [owner]);
|
|
5534
|
-
const signedTx = stateless_js.buildAndSignTx(
|
|
5535
|
-
|
|
5536
|
-
|
|
5537
|
-
|
|
5538
|
-
|
|
5539
|
-
|
|
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
|
-
);
|
|
5020
|
+
const signedTx = stateless_js.buildAndSignTx([
|
|
5021
|
+
web3_js.ComputeBudgetProgram.setComputeUnitLimit({
|
|
5022
|
+
units: 1000000,
|
|
5023
|
+
}),
|
|
5024
|
+
compressIx,
|
|
5025
|
+
], payer, blockhashCtx.blockhash, additionalSigners);
|
|
5026
|
+
const txId = await stateless_js.sendAndConfirmTx(rpc, signedTx, confirmOptions, blockhashCtx);
|
|
5551
5027
|
return txId;
|
|
5552
5028
|
}
|
|
5553
5029
|
|
|
@@ -5568,33 +5044,15 @@ async function compress(
|
|
|
5568
5044
|
*
|
|
5569
5045
|
* @return Signature of the confirmed transaction
|
|
5570
5046
|
*/
|
|
5571
|
-
async function transfer(
|
|
5572
|
-
|
|
5573
|
-
|
|
5574
|
-
mint,
|
|
5575
|
-
amount,
|
|
5576
|
-
owner,
|
|
5577
|
-
toAddress,
|
|
5578
|
-
/// TODO: allow multiple
|
|
5579
|
-
merkleTree,
|
|
5580
|
-
confirmOptions,
|
|
5581
|
-
) {
|
|
5047
|
+
async function transfer(rpc, payer, mint, amount, owner, toAddress,
|
|
5048
|
+
/// TODO: allow multiple
|
|
5049
|
+
merkleTree, confirmOptions) {
|
|
5582
5050
|
amount = stateless_js.bn(amount);
|
|
5583
|
-
const compressedTokenAccounts = await rpc.getCompressedTokenAccountsByOwner(
|
|
5584
|
-
|
|
5585
|
-
|
|
5586
|
-
|
|
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
|
-
);
|
|
5051
|
+
const compressedTokenAccounts = await rpc.getCompressedTokenAccountsByOwner(owner.publicKey, {
|
|
5052
|
+
mint,
|
|
5053
|
+
});
|
|
5054
|
+
const [inputAccounts] = selectMinCompressedTokenAccountsForTransfer(compressedTokenAccounts.items, amount);
|
|
5055
|
+
const proof = await rpc.getValidityProof(inputAccounts.map(account => stateless_js.bn(account.compressedAccount.hash)));
|
|
5598
5056
|
const ix = await CompressedTokenProgram.transfer({
|
|
5599
5057
|
payer: payer.publicKey,
|
|
5600
5058
|
inputCompressedTokenAccounts: inputAccounts,
|
|
@@ -5606,22 +5064,8 @@ async function transfer(
|
|
|
5606
5064
|
});
|
|
5607
5065
|
const { blockhash } = await rpc.getLatestBlockhash();
|
|
5608
5066
|
const additionalSigners = stateless_js.dedupeSigner(payer, [owner]);
|
|
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
|
-
);
|
|
5067
|
+
const signedTx = stateless_js.buildAndSignTx([web3_js.ComputeBudgetProgram.setComputeUnitLimit({ units: 1000000 }), ix], payer, blockhash, additionalSigners);
|
|
5068
|
+
const txId = await stateless_js.sendAndConfirmTx(rpc, signedTx, confirmOptions);
|
|
5625
5069
|
return txId;
|
|
5626
5070
|
}
|
|
5627
5071
|
/**
|
|
@@ -5637,17 +5081,14 @@ function selectMinCompressedTokenAccountsForTransfer(accounts, transferAmount) {
|
|
|
5637
5081
|
const selectedAccounts = [];
|
|
5638
5082
|
accounts.sort((a, b) => b.parsed.amount.cmp(a.parsed.amount));
|
|
5639
5083
|
for (const account of accounts) {
|
|
5640
|
-
if (accumulatedAmount.gte(stateless_js.bn(transferAmount)))
|
|
5084
|
+
if (accumulatedAmount.gte(stateless_js.bn(transferAmount)))
|
|
5085
|
+
break;
|
|
5641
5086
|
accumulatedAmount = accumulatedAmount.add(account.parsed.amount);
|
|
5642
|
-
accumulatedLamports = accumulatedLamports.add(
|
|
5643
|
-
account.compressedAccount.lamports,
|
|
5644
|
-
);
|
|
5087
|
+
accumulatedLamports = accumulatedLamports.add(account.compressedAccount.lamports);
|
|
5645
5088
|
selectedAccounts.push(account);
|
|
5646
5089
|
}
|
|
5647
5090
|
if (accumulatedAmount.lt(stateless_js.bn(transferAmount))) {
|
|
5648
|
-
throw new Error(
|
|
5649
|
-
`Not enough balance for transfer. Required: ${transferAmount.toString()}, available: ${accumulatedAmount.toString()}`,
|
|
5650
|
-
);
|
|
5091
|
+
throw new Error(`Not enough balance for transfer. Required: ${transferAmount.toString()}, available: ${accumulatedAmount.toString()}`);
|
|
5651
5092
|
}
|
|
5652
5093
|
return [
|
|
5653
5094
|
selectedAccounts,
|
|
@@ -5674,34 +5115,16 @@ function selectMinCompressedTokenAccountsForTransfer(accounts, transferAmount) {
|
|
|
5674
5115
|
*
|
|
5675
5116
|
* @return Signature of the confirmed transaction
|
|
5676
5117
|
*/
|
|
5677
|
-
async function decompress(
|
|
5678
|
-
|
|
5679
|
-
|
|
5680
|
-
mint,
|
|
5681
|
-
amount,
|
|
5682
|
-
owner,
|
|
5683
|
-
toAddress,
|
|
5684
|
-
/// TODO: allow multiple
|
|
5685
|
-
merkleTree,
|
|
5686
|
-
confirmOptions,
|
|
5687
|
-
) {
|
|
5118
|
+
async function decompress(rpc, payer, mint, amount, owner, toAddress,
|
|
5119
|
+
/// TODO: allow multiple
|
|
5120
|
+
merkleTree, confirmOptions) {
|
|
5688
5121
|
amount = stateless_js.bn(amount);
|
|
5689
|
-
const compressedTokenAccounts = await rpc.getCompressedTokenAccountsByOwner(
|
|
5690
|
-
|
|
5691
|
-
|
|
5692
|
-
mint,
|
|
5693
|
-
},
|
|
5694
|
-
);
|
|
5122
|
+
const compressedTokenAccounts = await rpc.getCompressedTokenAccountsByOwner(owner.publicKey, {
|
|
5123
|
+
mint,
|
|
5124
|
+
});
|
|
5695
5125
|
/// TODO: consider using a different selection algorithm
|
|
5696
|
-
const [inputAccounts] = selectMinCompressedTokenAccountsForTransfer(
|
|
5697
|
-
|
|
5698
|
-
amount,
|
|
5699
|
-
);
|
|
5700
|
-
const proof = await rpc.getValidityProof(
|
|
5701
|
-
inputAccounts.map(account =>
|
|
5702
|
-
stateless_js.bn(account.compressedAccount.hash),
|
|
5703
|
-
),
|
|
5704
|
-
);
|
|
5126
|
+
const [inputAccounts] = selectMinCompressedTokenAccountsForTransfer(compressedTokenAccounts.items, amount);
|
|
5127
|
+
const proof = await rpc.getValidityProof(inputAccounts.map(account => stateless_js.bn(account.compressedAccount.hash)));
|
|
5705
5128
|
const ix = await CompressedTokenProgram.decompress({
|
|
5706
5129
|
payer: payer.publicKey,
|
|
5707
5130
|
inputCompressedTokenAccounts: inputAccounts,
|
|
@@ -5713,22 +5136,8 @@ async function decompress(
|
|
|
5713
5136
|
});
|
|
5714
5137
|
const { blockhash } = await rpc.getLatestBlockhash();
|
|
5715
5138
|
const additionalSigners = stateless_js.dedupeSigner(payer, [owner]);
|
|
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
|
-
);
|
|
5139
|
+
const signedTx = stateless_js.buildAndSignTx([web3_js.ComputeBudgetProgram.setComputeUnitLimit({ units: 1000000 }), ix], payer, blockhash, additionalSigners);
|
|
5140
|
+
const txId = await stateless_js.sendAndConfirmTx(rpc, signedTx, confirmOptions);
|
|
5732
5141
|
return txId;
|
|
5733
5142
|
}
|
|
5734
5143
|
|
|
@@ -5744,16 +5153,8 @@ async function decompress(
|
|
|
5744
5153
|
*
|
|
5745
5154
|
* @return Address of the new mint and the transaction signature
|
|
5746
5155
|
*/
|
|
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);
|
|
5156
|
+
async function createMint(rpc, payer, mintAuthority, decimals, keypair = web3_js.Keypair.generate(), confirmOptions) {
|
|
5157
|
+
const rentExemptBalance = await rpc.getMinimumBalanceForRentExemption(MINT_SIZE);
|
|
5757
5158
|
const ixs = await CompressedTokenProgram.createMint({
|
|
5758
5159
|
feePayer: payer.publicKey,
|
|
5759
5160
|
mint: keypair.publicKey,
|
|
@@ -5764,12 +5165,7 @@ async function createMint(
|
|
|
5764
5165
|
});
|
|
5765
5166
|
const { blockhash } = await rpc.getLatestBlockhash();
|
|
5766
5167
|
const additionalSigners = stateless_js.dedupeSigner(payer, [keypair]);
|
|
5767
|
-
const tx = stateless_js.buildAndSignTx(
|
|
5768
|
-
ixs,
|
|
5769
|
-
payer,
|
|
5770
|
-
blockhash,
|
|
5771
|
-
additionalSigners,
|
|
5772
|
-
);
|
|
5168
|
+
const tx = stateless_js.buildAndSignTx(ixs, payer, blockhash, additionalSigners);
|
|
5773
5169
|
const txId = await stateless_js.sendAndConfirmTx(rpc, tx, confirmOptions);
|
|
5774
5170
|
return { mint: keypair.publicKey, transactionSignature: txId };
|
|
5775
5171
|
}
|
|
@@ -5791,16 +5187,7 @@ async function createMint(
|
|
|
5791
5187
|
*
|
|
5792
5188
|
* @return Signature of the confirmed transaction
|
|
5793
5189
|
*/
|
|
5794
|
-
async function mintTo(
|
|
5795
|
-
rpc,
|
|
5796
|
-
payer,
|
|
5797
|
-
mint,
|
|
5798
|
-
destination,
|
|
5799
|
-
authority,
|
|
5800
|
-
amount,
|
|
5801
|
-
merkleTree,
|
|
5802
|
-
confirmOptions,
|
|
5803
|
-
) {
|
|
5190
|
+
async function mintTo(rpc, payer, mint, destination, authority, amount, merkleTree, confirmOptions) {
|
|
5804
5191
|
const additionalSigners = stateless_js.dedupeSigner(payer, [authority]);
|
|
5805
5192
|
const ix = await CompressedTokenProgram.mintTo({
|
|
5806
5193
|
feePayer: payer.publicKey,
|
|
@@ -5811,17 +5198,7 @@ async function mintTo(
|
|
|
5811
5198
|
merkleTree,
|
|
5812
5199
|
});
|
|
5813
5200
|
const { blockhash } = await rpc.getLatestBlockhash();
|
|
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
|
-
);
|
|
5201
|
+
const tx = stateless_js.buildAndSignTx([web3_js.ComputeBudgetProgram.setComputeUnitLimit({ units: 1000000 }), ix], payer, blockhash, additionalSigners);
|
|
5825
5202
|
const txId = await stateless_js.sendAndConfirmTx(rpc, tx, confirmOptions);
|
|
5826
5203
|
return txId;
|
|
5827
5204
|
}
|
|
@@ -5839,67 +5216,35 @@ async function mintTo(
|
|
|
5839
5216
|
*
|
|
5840
5217
|
* @return Array of transaction signatures
|
|
5841
5218
|
*/
|
|
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
|
-
);
|
|
5219
|
+
async function mergeTokenAccounts(rpc, payer, mint, owner, merkleTree, confirmOptions) {
|
|
5220
|
+
const compressedTokenAccounts = await rpc.getCompressedTokenAccountsByOwner(owner.publicKey, { mint });
|
|
5854
5221
|
if (compressedTokenAccounts.items.length === 0) {
|
|
5855
|
-
throw new Error(
|
|
5856
|
-
`No compressed token accounts found for mint ${mint.toBase58()}`,
|
|
5857
|
-
);
|
|
5222
|
+
throw new Error(`No compressed token accounts found for mint ${mint.toBase58()}`);
|
|
5858
5223
|
}
|
|
5859
5224
|
if (compressedTokenAccounts.items.length >= 6) {
|
|
5860
|
-
throw new Error(
|
|
5861
|
-
`Too many compressed token accounts used for mint ${mint.toBase58()}`,
|
|
5862
|
-
);
|
|
5225
|
+
throw new Error(`Too many compressed token accounts used for mint ${mint.toBase58()}`);
|
|
5863
5226
|
}
|
|
5864
5227
|
const instructions = [
|
|
5865
5228
|
web3_js.ComputeBudgetProgram.setComputeUnitLimit({ units: 1000000 }),
|
|
5866
5229
|
];
|
|
5867
|
-
for (
|
|
5868
|
-
let i = 0;
|
|
5869
|
-
i < compressedTokenAccounts.items.slice(0, 6).length;
|
|
5870
|
-
i += 3
|
|
5871
|
-
) {
|
|
5230
|
+
for (let i = 0; i < compressedTokenAccounts.items.slice(0, 6).length; i += 3) {
|
|
5872
5231
|
const batch = compressedTokenAccounts.items.slice(i, i + 3);
|
|
5873
|
-
const proof = await rpc.getValidityProof(
|
|
5874
|
-
|
|
5875
|
-
|
|
5876
|
-
|
|
5877
|
-
|
|
5878
|
-
|
|
5879
|
-
|
|
5880
|
-
|
|
5881
|
-
|
|
5882
|
-
|
|
5883
|
-
inputCompressedTokenAccounts: batch,
|
|
5884
|
-
outputStateTree: merkleTree,
|
|
5885
|
-
recentValidityProof: proof.compressedProof,
|
|
5886
|
-
recentInputStateRootIndices: proof.rootIndices,
|
|
5887
|
-
});
|
|
5232
|
+
const proof = await rpc.getValidityProof(batch.map(account => stateless_js.bn(account.compressedAccount.hash)));
|
|
5233
|
+
const batchInstructions = await CompressedTokenProgram.mergeTokenAccounts({
|
|
5234
|
+
payer: payer.publicKey,
|
|
5235
|
+
owner: owner.publicKey,
|
|
5236
|
+
mint,
|
|
5237
|
+
inputCompressedTokenAccounts: batch,
|
|
5238
|
+
outputStateTree: merkleTree,
|
|
5239
|
+
recentValidityProof: proof.compressedProof,
|
|
5240
|
+
recentInputStateRootIndices: proof.rootIndices,
|
|
5241
|
+
});
|
|
5888
5242
|
instructions.push(...batchInstructions);
|
|
5889
5243
|
}
|
|
5890
5244
|
const { blockhash } = await rpc.getLatestBlockhash();
|
|
5891
5245
|
const additionalSigners = stateless_js.dedupeSigner(payer, [owner]);
|
|
5892
|
-
const signedTx = stateless_js.buildAndSignTx(
|
|
5893
|
-
|
|
5894
|
-
payer,
|
|
5895
|
-
blockhash,
|
|
5896
|
-
additionalSigners,
|
|
5897
|
-
);
|
|
5898
|
-
const txId = await stateless_js.sendAndConfirmTx(
|
|
5899
|
-
rpc,
|
|
5900
|
-
signedTx,
|
|
5901
|
-
confirmOptions,
|
|
5902
|
-
);
|
|
5246
|
+
const signedTx = stateless_js.buildAndSignTx(instructions, payer, blockhash, additionalSigners);
|
|
5247
|
+
const txId = await stateless_js.sendAndConfirmTx(rpc, signedTx, confirmOptions);
|
|
5903
5248
|
return txId;
|
|
5904
5249
|
}
|
|
5905
5250
|
|
|
@@ -5938,50 +5283,23 @@ async function createTokenPool(rpc, payer, mintAddress, confirmOptions) {
|
|
|
5938
5283
|
*
|
|
5939
5284
|
* @return Transaction signatures and the address of the created lookup table
|
|
5940
5285
|
*/
|
|
5941
|
-
async function createTokenProgramLookupTable(
|
|
5942
|
-
rpc,
|
|
5943
|
-
payer,
|
|
5944
|
-
authority,
|
|
5945
|
-
mints,
|
|
5946
|
-
additionalAccounts,
|
|
5947
|
-
) {
|
|
5286
|
+
async function createTokenProgramLookupTable(rpc, payer, authority, mints, additionalAccounts) {
|
|
5948
5287
|
const recentSlot = await rpc.getSlot('finalized');
|
|
5949
|
-
const { instructions, address } =
|
|
5950
|
-
|
|
5951
|
-
|
|
5952
|
-
|
|
5953
|
-
|
|
5954
|
-
|
|
5955
|
-
|
|
5956
|
-
});
|
|
5288
|
+
const { instructions, address } = await CompressedTokenProgram.createTokenProgramLookupTable({
|
|
5289
|
+
payer: payer.publicKey,
|
|
5290
|
+
authority: authority.publicKey,
|
|
5291
|
+
mints,
|
|
5292
|
+
remainingAccounts: additionalAccounts,
|
|
5293
|
+
recentSlot,
|
|
5294
|
+
});
|
|
5957
5295
|
const additionalSigners = stateless_js.dedupeSigner(payer, [authority]);
|
|
5958
5296
|
const blockhashCtx = await rpc.getLatestBlockhash();
|
|
5959
|
-
const signedTx = stateless_js.buildAndSignTx(
|
|
5960
|
-
[instructions[0]],
|
|
5961
|
-
payer,
|
|
5962
|
-
blockhashCtx.blockhash,
|
|
5963
|
-
additionalSigners,
|
|
5964
|
-
);
|
|
5297
|
+
const signedTx = stateless_js.buildAndSignTx([instructions[0]], payer, blockhashCtx.blockhash, additionalSigners);
|
|
5965
5298
|
/// Must wait for the first instruction to be finalized.
|
|
5966
|
-
const txId = await stateless_js.sendAndConfirmTx(
|
|
5967
|
-
rpc,
|
|
5968
|
-
signedTx,
|
|
5969
|
-
{ commitment: 'finalized' },
|
|
5970
|
-
blockhashCtx,
|
|
5971
|
-
);
|
|
5299
|
+
const txId = await stateless_js.sendAndConfirmTx(rpc, signedTx, { commitment: 'finalized' }, blockhashCtx);
|
|
5972
5300
|
const blockhashCtx2 = await rpc.getLatestBlockhash();
|
|
5973
|
-
const signedTx2 = stateless_js.buildAndSignTx(
|
|
5974
|
-
|
|
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
|
-
);
|
|
5301
|
+
const signedTx2 = stateless_js.buildAndSignTx([instructions[1]], payer, blockhashCtx2.blockhash, additionalSigners);
|
|
5302
|
+
const txId2 = await stateless_js.sendAndConfirmTx(rpc, signedTx2, { commitment: 'finalized' }, blockhashCtx2);
|
|
5985
5303
|
return { txIds: [txId, txId2], address };
|
|
5986
5304
|
}
|
|
5987
5305
|
|
|
@@ -6002,8 +5320,7 @@ exports.mergeTokenAccounts = mergeTokenAccounts;
|
|
|
6002
5320
|
exports.mintTo = mintTo;
|
|
6003
5321
|
exports.packCompressedTokenAccounts = packCompressedTokenAccounts;
|
|
6004
5322
|
exports.parseTokenData = parseTokenData;
|
|
6005
|
-
exports.selectMinCompressedTokenAccountsForTransfer =
|
|
6006
|
-
selectMinCompressedTokenAccountsForTransfer;
|
|
5323
|
+
exports.selectMinCompressedTokenAccountsForTransfer = selectMinCompressedTokenAccountsForTransfer;
|
|
6007
5324
|
exports.sumUpTokenAmount = sumUpTokenAmount;
|
|
6008
5325
|
exports.transfer = transfer;
|
|
6009
5326
|
exports.validateSameTokenOwner = validateSameTokenOwner;
|