@magicblock-labs/ephemeral-rollups-sdk 0.10.0 → 0.10.2
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/lib/__test__/instructions.test.js +145 -36
- package/lib/__test__/instructions.test.js.map +1 -1
- package/lib/instructions/ephemeral-spl-token-program/ephemeralAta.d.ts +12 -12
- package/lib/instructions/ephemeral-spl-token-program/ephemeralAta.d.ts.map +1 -1
- package/lib/instructions/ephemeral-spl-token-program/ephemeralAta.js +168 -91
- package/lib/instructions/ephemeral-spl-token-program/ephemeralAta.js.map +1 -1
- package/lib/instructions/ephemeral-spl-token-program/transferQueue.d.ts +12 -5
- package/lib/instructions/ephemeral-spl-token-program/transferQueue.d.ts.map +1 -1
- package/lib/instructions/ephemeral-spl-token-program/transferQueue.js +57 -25
- package/lib/instructions/ephemeral-spl-token-program/transferQueue.js.map +1 -1
- package/lib/pda.d.ts +2 -1
- package/lib/pda.d.ts.map +1 -1
- package/lib/pda.js +4 -0
- package/lib/pda.js.map +1 -1
- package/package.json +3 -1
|
@@ -1,4 +1,37 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
2
35
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
36
|
exports.decodeEphemeralAta = decodeEphemeralAta;
|
|
4
37
|
exports.encodeEphemeralAta = encodeEphemeralAta;
|
|
@@ -36,11 +69,15 @@ exports.delegateSplWithPrivateTransfer = delegateSplWithPrivateTransfer;
|
|
|
36
69
|
exports.transferSpl = transferSpl;
|
|
37
70
|
exports.withdrawSpl = withdrawSpl;
|
|
38
71
|
const web3_js_1 = require("@solana/web3.js");
|
|
72
|
+
const blake2b_1 = require("@noble/hashes/blake2b");
|
|
73
|
+
const ed25519_1 = require("@noble/curves/ed25519");
|
|
74
|
+
const nacl = __importStar(require("tweetnacl"));
|
|
39
75
|
const constants_js_1 = require("../../constants.js");
|
|
40
76
|
const pda_js_1 = require("../../pda.js");
|
|
41
77
|
const transferQueue_js_1 = require("./transferQueue.js");
|
|
42
78
|
const TOKEN_PROGRAM_ID = new web3_js_1.PublicKey("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA");
|
|
43
79
|
const ASSOCIATED_TOKEN_PROGRAM_ID = new web3_js_1.PublicKey("ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL");
|
|
80
|
+
const QUEUED_TRANSFER_FLAG_CREATE_IDEMPOTENT_ATA = 1 << 0;
|
|
44
81
|
function getAssociatedTokenAddressSync(mint, owner, allowOwnerOffCurve = false, programId = TOKEN_PROGRAM_ID, associatedTokenProgramId = ASSOCIATED_TOKEN_PROGRAM_ID) {
|
|
45
82
|
if (!allowOwnerOffCurve && !web3_js_1.PublicKey.isOnCurve(owner)) {
|
|
46
83
|
throw new Error("Owner public key is off-curve");
|
|
@@ -86,6 +123,43 @@ function createTransferInstruction(source, destination, owner, amount, multiSign
|
|
|
86
123
|
data,
|
|
87
124
|
});
|
|
88
125
|
}
|
|
126
|
+
function encryptEd25519Recipient(plaintext, recipient) {
|
|
127
|
+
const recipientX25519 = (0, ed25519_1.edwardsToMontgomeryPub)(recipient.toBytes());
|
|
128
|
+
const ephemeral = nacl.box.keyPair();
|
|
129
|
+
const nonce = (0, blake2b_1.blake2b)(Buffer.concat([
|
|
130
|
+
Buffer.from(ephemeral.publicKey),
|
|
131
|
+
Buffer.from(recipientX25519),
|
|
132
|
+
]), { dkLen: nacl.box.nonceLength });
|
|
133
|
+
const ciphertext = nacl.box(plaintext, nonce, recipientX25519, ephemeral.secretKey);
|
|
134
|
+
return Buffer.concat([
|
|
135
|
+
Buffer.from(ephemeral.publicKey),
|
|
136
|
+
Buffer.from(ciphertext),
|
|
137
|
+
]);
|
|
138
|
+
}
|
|
139
|
+
function encodeLengthPrefixedBytes(bytes) {
|
|
140
|
+
if (bytes.length > 0xff) {
|
|
141
|
+
throw new Error("encrypted private transfer payload exceeds u8 length");
|
|
142
|
+
}
|
|
143
|
+
return Buffer.concat([Buffer.from([bytes.length]), Buffer.from(bytes)]);
|
|
144
|
+
}
|
|
145
|
+
function packPrivateTransferSuffix(minDelayMs, maxDelayMs, split, flags = 0) {
|
|
146
|
+
const suffix = Buffer.alloc(8 + 8 + 4 + 1);
|
|
147
|
+
suffix.writeBigUInt64LE(minDelayMs, 0);
|
|
148
|
+
suffix.writeBigUInt64LE(maxDelayMs, 8);
|
|
149
|
+
suffix.writeUInt32LE(split, 16);
|
|
150
|
+
suffix.writeUInt8(flags, 20);
|
|
151
|
+
return suffix;
|
|
152
|
+
}
|
|
153
|
+
function u32leBuffer(value) {
|
|
154
|
+
const out = Buffer.alloc(4);
|
|
155
|
+
out.writeUInt32LE(value, 0);
|
|
156
|
+
return out;
|
|
157
|
+
}
|
|
158
|
+
function u64leBuffer(value) {
|
|
159
|
+
const out = Buffer.alloc(8);
|
|
160
|
+
out.writeBigUInt64LE(value, 0);
|
|
161
|
+
return out;
|
|
162
|
+
}
|
|
89
163
|
function decodeEphemeralAta(info) {
|
|
90
164
|
if (info.data.length < 72) {
|
|
91
165
|
throw new Error("Invalid EphemeralAta account data length");
|
|
@@ -146,7 +220,7 @@ function deriveShuttleAta(shuttleEphemeralAta, mint) {
|
|
|
146
220
|
function deriveShuttleWalletAta(mint, shuttleEphemeralAta) {
|
|
147
221
|
return getAssociatedTokenAddressSync(mint, shuttleEphemeralAta, true);
|
|
148
222
|
}
|
|
149
|
-
function initEphemeralAtaIx(ephemeralAta, owner, mint, payer
|
|
223
|
+
function initEphemeralAtaIx(ephemeralAta, owner, mint, payer) {
|
|
150
224
|
return new web3_js_1.TransactionInstruction({
|
|
151
225
|
programId: constants_js_1.EPHEMERAL_SPL_TOKEN_PROGRAM_ID,
|
|
152
226
|
keys: [
|
|
@@ -156,13 +230,13 @@ function initEphemeralAtaIx(ephemeralAta, owner, mint, payer, bump) {
|
|
|
156
230
|
{ pubkey: mint, isSigner: false, isWritable: false },
|
|
157
231
|
{ pubkey: web3_js_1.SystemProgram.programId, isSigner: false, isWritable: false },
|
|
158
232
|
],
|
|
159
|
-
data: Buffer.from([0
|
|
233
|
+
data: Buffer.from([0]),
|
|
160
234
|
});
|
|
161
235
|
}
|
|
162
236
|
function initVaultAtaIx(payer, vaultAta, vault, mint) {
|
|
163
237
|
return createAssociatedTokenAccountIdempotentInstruction(payer, vaultAta, vault, mint);
|
|
164
238
|
}
|
|
165
|
-
function initVaultIx(vault, mint, payer
|
|
239
|
+
function initVaultIx(vault, mint, payer) {
|
|
166
240
|
const [vaultEphemeralAta] = deriveEphemeralAta(vault, mint);
|
|
167
241
|
const vaultAta = deriveVaultAta(mint, vault);
|
|
168
242
|
return new web3_js_1.TransactionInstruction({
|
|
@@ -181,7 +255,7 @@ function initVaultIx(vault, mint, payer, bump) {
|
|
|
181
255
|
},
|
|
182
256
|
{ pubkey: web3_js_1.SystemProgram.programId, isSigner: false, isWritable: false },
|
|
183
257
|
],
|
|
184
|
-
data: Buffer.from([1
|
|
258
|
+
data: Buffer.from([1]),
|
|
185
259
|
});
|
|
186
260
|
}
|
|
187
261
|
function initRentPdaIx(payer, rentPda) {
|
|
@@ -213,10 +287,10 @@ function transferToVaultIx(ephemeralAta, vault, mint, sourceAta, vaultAta, owner
|
|
|
213
287
|
function depositSplTokensIx(ephemeralAta, vault, mint, sourceAta, vaultAta, owner, amount) {
|
|
214
288
|
return transferToVaultIx(ephemeralAta, vault, mint, sourceAta, vaultAta, owner, amount);
|
|
215
289
|
}
|
|
216
|
-
function delegateEphemeralAtaIx(payer, ephemeralAta,
|
|
290
|
+
function delegateEphemeralAtaIx(payer, ephemeralAta, validator) {
|
|
217
291
|
const data = validator
|
|
218
|
-
? Buffer.concat([Buffer.from([4
|
|
219
|
-
: Buffer.from([4
|
|
292
|
+
? Buffer.concat([Buffer.from([4]), validator.toBuffer()])
|
|
293
|
+
: Buffer.from([4]);
|
|
220
294
|
return new web3_js_1.TransactionInstruction({
|
|
221
295
|
programId: constants_js_1.EPHEMERAL_SPL_TOKEN_PROGRAM_ID,
|
|
222
296
|
keys: [
|
|
@@ -248,16 +322,15 @@ function delegateEphemeralAtaIx(payer, ephemeralAta, bump, validator) {
|
|
|
248
322
|
data,
|
|
249
323
|
});
|
|
250
324
|
}
|
|
251
|
-
function initShuttleEphemeralAtaIx(payer, shuttleEphemeralAta, shuttleAta, shuttleWalletAta, owner, mint, shuttleId
|
|
325
|
+
function initShuttleEphemeralAtaIx(payer, shuttleEphemeralAta, shuttleAta, shuttleWalletAta, owner, mint, shuttleId) {
|
|
252
326
|
if (!Number.isInteger(shuttleId) ||
|
|
253
327
|
shuttleId < 0 ||
|
|
254
328
|
shuttleId > 4294967295) {
|
|
255
329
|
throw new Error("shuttleId must fit in u32");
|
|
256
330
|
}
|
|
257
|
-
const data = Buffer.alloc(
|
|
331
|
+
const data = Buffer.alloc(5);
|
|
258
332
|
data[0] = 11;
|
|
259
333
|
data.writeUInt32LE(shuttleId, 1);
|
|
260
|
-
data[5] = bump;
|
|
261
334
|
return new web3_js_1.TransactionInstruction({
|
|
262
335
|
programId: constants_js_1.EPHEMERAL_SPL_TOKEN_PROGRAM_ID,
|
|
263
336
|
keys: [
|
|
@@ -278,10 +351,10 @@ function initShuttleEphemeralAtaIx(payer, shuttleEphemeralAta, shuttleAta, shutt
|
|
|
278
351
|
data,
|
|
279
352
|
});
|
|
280
353
|
}
|
|
281
|
-
function delegateShuttleEphemeralAtaIx(payer, shuttleEphemeralAta, shuttleAta,
|
|
354
|
+
function delegateShuttleEphemeralAtaIx(payer, shuttleEphemeralAta, shuttleAta, validator) {
|
|
282
355
|
const data = validator
|
|
283
|
-
? Buffer.concat([Buffer.from([13
|
|
284
|
-
: Buffer.from([13
|
|
356
|
+
? Buffer.concat([Buffer.from([13]), validator.toBuffer()])
|
|
357
|
+
: Buffer.from([13]);
|
|
285
358
|
return new web3_js_1.TransactionInstruction({
|
|
286
359
|
programId: constants_js_1.EPHEMERAL_SPL_TOKEN_PROGRAM_ID,
|
|
287
360
|
keys: [
|
|
@@ -314,7 +387,7 @@ function delegateShuttleEphemeralAtaIx(payer, shuttleEphemeralAta, shuttleAta, b
|
|
|
314
387
|
data,
|
|
315
388
|
});
|
|
316
389
|
}
|
|
317
|
-
function setupAndDelegateShuttleEphemeralAtaWithMergeIx(payer, shuttleEphemeralAta, shuttleAta, owner, sourceAta, destinationAta, shuttleWalletAta, mint, shuttleId,
|
|
390
|
+
function setupAndDelegateShuttleEphemeralAtaWithMergeIx(payer, shuttleEphemeralAta, shuttleAta, owner, sourceAta, destinationAta, shuttleWalletAta, mint, shuttleId, amount, validator) {
|
|
318
391
|
if (!Number.isInteger(shuttleId) ||
|
|
319
392
|
shuttleId < 0 ||
|
|
320
393
|
shuttleId > 4294967295) {
|
|
@@ -323,13 +396,12 @@ function setupAndDelegateShuttleEphemeralAtaWithMergeIx(payer, shuttleEphemeralA
|
|
|
323
396
|
const [rentPda] = deriveRentPda();
|
|
324
397
|
const [vault] = deriveVault(mint);
|
|
325
398
|
const vaultAta = deriveVaultAta(mint, vault);
|
|
326
|
-
const data = validator ? Buffer.alloc(
|
|
399
|
+
const data = validator ? Buffer.alloc(45) : Buffer.alloc(13);
|
|
327
400
|
data[0] = 24;
|
|
328
401
|
data.writeUInt32LE(shuttleId, 1);
|
|
329
|
-
data
|
|
330
|
-
data.writeBigUInt64LE(amount, 6);
|
|
402
|
+
data.writeBigUInt64LE(amount, 5);
|
|
331
403
|
if (validator) {
|
|
332
|
-
validator.toBuffer().copy(data,
|
|
404
|
+
validator.toBuffer().copy(data, 13);
|
|
333
405
|
}
|
|
334
406
|
return new web3_js_1.TransactionInstruction({
|
|
335
407
|
programId: constants_js_1.EPHEMERAL_SPL_TOKEN_PROGRAM_ID,
|
|
@@ -377,7 +449,7 @@ function setupAndDelegateShuttleEphemeralAtaWithMergeIx(payer, shuttleEphemeralA
|
|
|
377
449
|
data,
|
|
378
450
|
});
|
|
379
451
|
}
|
|
380
|
-
function depositAndDelegateShuttleEphemeralAtaWithMergeAndPrivateTransferIx(payer, shuttleEphemeralAta, shuttleAta, owner, sourceAta,
|
|
452
|
+
function depositAndDelegateShuttleEphemeralAtaWithMergeAndPrivateTransferIx(payer, shuttleEphemeralAta, shuttleAta, owner, sourceAta, destinationOwner, shuttleWalletAta, mint, shuttleId, amount, minDelayMs, maxDelayMs, split, validator) {
|
|
381
453
|
if (!Number.isInteger(shuttleId) ||
|
|
382
454
|
shuttleId < 0 ||
|
|
383
455
|
shuttleId > 4294967295) {
|
|
@@ -392,21 +464,23 @@ function depositAndDelegateShuttleEphemeralAtaWithMergeAndPrivateTransferIx(paye
|
|
|
392
464
|
if (maxDelayMs < minDelayMs) {
|
|
393
465
|
throw new Error("maxDelayMs must be greater than or equal to minDelayMs");
|
|
394
466
|
}
|
|
467
|
+
if (validator == null) {
|
|
468
|
+
throw new Error("validator is required for encrypted private transfers");
|
|
469
|
+
}
|
|
395
470
|
const [rentPda] = deriveRentPda();
|
|
396
471
|
const [vault] = deriveVault(mint);
|
|
397
472
|
const vaultAta = deriveVaultAta(mint, vault);
|
|
398
|
-
const [queue] = (0, transferQueue_js_1.deriveTransferQueue)(mint);
|
|
399
|
-
const
|
|
400
|
-
|
|
401
|
-
data.
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
}
|
|
473
|
+
const [queue] = (0, transferQueue_js_1.deriveTransferQueue)(mint, validator);
|
|
474
|
+
const encryptedDestination = encryptEd25519Recipient(destinationOwner.toBytes(), validator);
|
|
475
|
+
const encryptedSuffix = encryptEd25519Recipient(packPrivateTransferSuffix(minDelayMs, maxDelayMs, split, QUEUED_TRANSFER_FLAG_CREATE_IDEMPOTENT_ATA), validator);
|
|
476
|
+
const data = Buffer.concat([
|
|
477
|
+
Buffer.from([25]),
|
|
478
|
+
u32leBuffer(shuttleId),
|
|
479
|
+
u64leBuffer(amount),
|
|
480
|
+
encodeLengthPrefixedBytes(validator.toBytes()),
|
|
481
|
+
encodeLengthPrefixedBytes(encryptedDestination),
|
|
482
|
+
encodeLengthPrefixedBytes(encryptedSuffix),
|
|
483
|
+
]);
|
|
410
484
|
return new web3_js_1.TransactionInstruction({
|
|
411
485
|
programId: constants_js_1.EPHEMERAL_SPL_TOKEN_PROGRAM_ID,
|
|
412
486
|
keys: [
|
|
@@ -443,7 +517,6 @@ function depositAndDelegateShuttleEphemeralAtaWithMergeAndPrivateTransferIx(paye
|
|
|
443
517
|
isWritable: false,
|
|
444
518
|
},
|
|
445
519
|
{ pubkey: web3_js_1.SystemProgram.programId, isSigner: false, isWritable: false },
|
|
446
|
-
{ pubkey: destinationAta, isSigner: false, isWritable: true },
|
|
447
520
|
{ pubkey: mint, isSigner: false, isWritable: false },
|
|
448
521
|
{ pubkey: TOKEN_PROGRAM_ID, isSigner: false, isWritable: false },
|
|
449
522
|
{ pubkey: vault, isSigner: false, isWritable: false },
|
|
@@ -454,7 +527,7 @@ function depositAndDelegateShuttleEphemeralAtaWithMergeAndPrivateTransferIx(paye
|
|
|
454
527
|
data,
|
|
455
528
|
});
|
|
456
529
|
}
|
|
457
|
-
function withdrawThroughDelegatedShuttleWithMergeIx(payer, shuttleEphemeralAta, shuttleAta, owner, ownerAta, shuttleWalletAta, mint, shuttleId,
|
|
530
|
+
function withdrawThroughDelegatedShuttleWithMergeIx(payer, shuttleEphemeralAta, shuttleAta, owner, ownerAta, shuttleWalletAta, mint, shuttleId, amount, validator) {
|
|
458
531
|
if (!Number.isInteger(shuttleId) ||
|
|
459
532
|
shuttleId < 0 ||
|
|
460
533
|
shuttleId > 4294967295) {
|
|
@@ -464,13 +537,12 @@ function withdrawThroughDelegatedShuttleWithMergeIx(payer, shuttleEphemeralAta,
|
|
|
464
537
|
throw new Error("amount must be non-negative");
|
|
465
538
|
}
|
|
466
539
|
const [rentPda] = deriveRentPda();
|
|
467
|
-
const data = validator ? Buffer.alloc(
|
|
540
|
+
const data = validator ? Buffer.alloc(45) : Buffer.alloc(13);
|
|
468
541
|
data[0] = 26;
|
|
469
542
|
data.writeUInt32LE(shuttleId, 1);
|
|
470
|
-
data
|
|
471
|
-
data.writeBigUInt64LE(amount, 6);
|
|
543
|
+
data.writeBigUInt64LE(amount, 5);
|
|
472
544
|
if (validator) {
|
|
473
|
-
validator.toBuffer().copy(data,
|
|
545
|
+
validator.toBuffer().copy(data, 13);
|
|
474
546
|
}
|
|
475
547
|
return new web3_js_1.TransactionInstruction({
|
|
476
548
|
programId: constants_js_1.EPHEMERAL_SPL_TOKEN_PROGRAM_ID,
|
|
@@ -529,7 +601,7 @@ function mergeShuttleIntoAtaIx(owner, destinationAta, shuttleEphemeralAta, shutt
|
|
|
529
601
|
data: Buffer.from([15]),
|
|
530
602
|
});
|
|
531
603
|
}
|
|
532
|
-
function undelegateAndCloseShuttleEphemeralAtaIx(payer, shuttleEphemeralAta, shuttleAta, shuttleWalletAta, escrowIndex) {
|
|
604
|
+
function undelegateAndCloseShuttleEphemeralAtaIx(payer, rentReimbursement, shuttleEphemeralAta, shuttleAta, shuttleWalletAta, destinationAta, escrowIndex) {
|
|
533
605
|
const data = escrowIndex === undefined
|
|
534
606
|
? Buffer.from([14])
|
|
535
607
|
: Buffer.from([14, escrowIndex]);
|
|
@@ -537,9 +609,11 @@ function undelegateAndCloseShuttleEphemeralAtaIx(payer, shuttleEphemeralAta, shu
|
|
|
537
609
|
programId: constants_js_1.EPHEMERAL_SPL_TOKEN_PROGRAM_ID,
|
|
538
610
|
keys: [
|
|
539
611
|
{ pubkey: payer, isSigner: true, isWritable: true },
|
|
612
|
+
{ pubkey: rentReimbursement, isSigner: false, isWritable: true },
|
|
540
613
|
{ pubkey: shuttleEphemeralAta, isSigner: false, isWritable: false },
|
|
541
614
|
{ pubkey: shuttleAta, isSigner: false, isWritable: false },
|
|
542
615
|
{ pubkey: shuttleWalletAta, isSigner: false, isWritable: true },
|
|
616
|
+
{ pubkey: destinationAta, isSigner: false, isWritable: true },
|
|
543
617
|
{ pubkey: TOKEN_PROGRAM_ID, isSigner: false, isWritable: false },
|
|
544
618
|
{ pubkey: constants_js_1.MAGIC_CONTEXT_ID, isSigner: false, isWritable: true },
|
|
545
619
|
{ pubkey: constants_js_1.MAGIC_PROGRAM_ID, isSigner: false, isWritable: false },
|
|
@@ -549,7 +623,7 @@ function undelegateAndCloseShuttleEphemeralAtaIx(payer, shuttleEphemeralAta, shu
|
|
|
549
623
|
}
|
|
550
624
|
function withdrawSplIx(owner, mint, amount) {
|
|
551
625
|
const [ephemeralAta] = deriveEphemeralAta(owner, mint);
|
|
552
|
-
const [vault
|
|
626
|
+
const [vault] = deriveVault(mint);
|
|
553
627
|
const vaultAta = deriveVaultAta(mint, vault);
|
|
554
628
|
const userDestAta = getAssociatedTokenAddressSync(mint, owner);
|
|
555
629
|
return new web3_js_1.TransactionInstruction({
|
|
@@ -563,7 +637,7 @@ function withdrawSplIx(owner, mint, amount) {
|
|
|
563
637
|
{ pubkey: userDestAta, isSigner: false, isWritable: true },
|
|
564
638
|
{ pubkey: TOKEN_PROGRAM_ID, isSigner: false, isWritable: false },
|
|
565
639
|
],
|
|
566
|
-
data: encodeAmountInstructionData(3, amount
|
|
640
|
+
data: encodeAmountInstructionData(3, amount),
|
|
567
641
|
});
|
|
568
642
|
}
|
|
569
643
|
function undelegateIx(owner, mint) {
|
|
@@ -601,7 +675,7 @@ function undelegateIx(owner, mint) {
|
|
|
601
675
|
data: Buffer.from([5]),
|
|
602
676
|
});
|
|
603
677
|
}
|
|
604
|
-
function createEataPermissionIx(ephemeralAta, payer,
|
|
678
|
+
function createEataPermissionIx(ephemeralAta, payer, flags = 0) {
|
|
605
679
|
const permission = (0, pda_js_1.permissionPdaFromAccount)(ephemeralAta);
|
|
606
680
|
return new web3_js_1.TransactionInstruction({
|
|
607
681
|
programId: constants_js_1.EPHEMERAL_SPL_TOKEN_PROGRAM_ID,
|
|
@@ -612,10 +686,10 @@ function createEataPermissionIx(ephemeralAta, payer, bump, flags = 0) {
|
|
|
612
686
|
{ pubkey: web3_js_1.SystemProgram.programId, isSigner: false, isWritable: false },
|
|
613
687
|
{ pubkey: constants_js_1.PERMISSION_PROGRAM_ID, isSigner: false, isWritable: false },
|
|
614
688
|
],
|
|
615
|
-
data: Buffer.from([6,
|
|
689
|
+
data: Buffer.from([6, flags]),
|
|
616
690
|
});
|
|
617
691
|
}
|
|
618
|
-
function resetEataPermissionIx(ephemeralAta, payer,
|
|
692
|
+
function resetEataPermissionIx(ephemeralAta, payer, flags = 0) {
|
|
619
693
|
const permission = (0, pda_js_1.permissionPdaFromAccount)(ephemeralAta);
|
|
620
694
|
return new web3_js_1.TransactionInstruction({
|
|
621
695
|
programId: constants_js_1.EPHEMERAL_SPL_TOKEN_PROGRAM_ID,
|
|
@@ -625,10 +699,10 @@ function resetEataPermissionIx(ephemeralAta, payer, bump, flags = 0) {
|
|
|
625
699
|
{ pubkey: payer, isSigner: true, isWritable: false },
|
|
626
700
|
{ pubkey: constants_js_1.PERMISSION_PROGRAM_ID, isSigner: false, isWritable: false },
|
|
627
701
|
],
|
|
628
|
-
data: Buffer.from([9,
|
|
702
|
+
data: Buffer.from([9, flags]),
|
|
629
703
|
});
|
|
630
704
|
}
|
|
631
|
-
function delegateEataPermissionIx(payer, ephemeralAta,
|
|
705
|
+
function delegateEataPermissionIx(payer, ephemeralAta, validator) {
|
|
632
706
|
const permission = (0, pda_js_1.permissionPdaFromAccount)(ephemeralAta);
|
|
633
707
|
return new web3_js_1.TransactionInstruction({
|
|
634
708
|
programId: constants_js_1.EPHEMERAL_SPL_TOKEN_PROGRAM_ID,
|
|
@@ -656,7 +730,7 @@ function delegateEataPermissionIx(payer, ephemeralAta, bump, validator) {
|
|
|
656
730
|
{ pubkey: constants_js_1.DELEGATION_PROGRAM_ID, isSigner: false, isWritable: false },
|
|
657
731
|
{ pubkey: validator, isSigner: false, isWritable: false },
|
|
658
732
|
],
|
|
659
|
-
data: Buffer.from([7
|
|
733
|
+
data: Buffer.from([7]),
|
|
660
734
|
});
|
|
661
735
|
}
|
|
662
736
|
function undelegateEataPermissionIx(owner, ephemeralAta) {
|
|
@@ -690,22 +764,22 @@ async function buildDelegateSplInstructions(owner, mint, amount, opts) {
|
|
|
690
764
|
const initVaultIfMissing = opts?.initVaultIfMissing ?? initIfMissing;
|
|
691
765
|
const isPrivate = opts?.private ?? false;
|
|
692
766
|
const instructions = [];
|
|
693
|
-
const [ephemeralAta
|
|
694
|
-
const [vault
|
|
695
|
-
const [vaultEphemeralAta
|
|
767
|
+
const [ephemeralAta] = deriveEphemeralAta(owner, mint);
|
|
768
|
+
const [vault] = deriveVault(mint);
|
|
769
|
+
const [vaultEphemeralAta] = deriveEphemeralAta(vault, mint);
|
|
696
770
|
const vaultAta = deriveVaultAta(mint, vault);
|
|
697
771
|
const ownerAta = getAssociatedTokenAddressSync(mint, owner);
|
|
698
772
|
if (initIfMissing) {
|
|
699
|
-
instructions.push(initEphemeralAtaIx(ephemeralAta, owner, mint, payer
|
|
773
|
+
instructions.push(initEphemeralAtaIx(ephemeralAta, owner, mint, payer));
|
|
700
774
|
}
|
|
701
775
|
if (initVaultIfMissing) {
|
|
702
|
-
instructions.push(initVaultIx(vault, mint, payer
|
|
776
|
+
instructions.push(initVaultIx(vault, mint, payer), initVaultAtaIx(payer, vaultAta, vault, mint), delegateEphemeralAtaIx(payer, vaultEphemeralAta, validator));
|
|
703
777
|
}
|
|
704
778
|
instructions.push(transferToVaultIx(ephemeralAta, vault, mint, ownerAta, vaultAta, owner, amount));
|
|
705
779
|
if (isPrivate) {
|
|
706
|
-
instructions.push(createEataPermissionIx(ephemeralAta, payer
|
|
780
|
+
instructions.push(createEataPermissionIx(ephemeralAta, payer));
|
|
707
781
|
}
|
|
708
|
-
instructions.push(delegateEphemeralAtaIx(payer, ephemeralAta,
|
|
782
|
+
instructions.push(delegateEphemeralAtaIx(payer, ephemeralAta, validator));
|
|
709
783
|
return instructions;
|
|
710
784
|
}
|
|
711
785
|
async function buildIdempotentDelegateSplInstructions(owner, mint, amount, opts) {
|
|
@@ -717,32 +791,32 @@ async function buildIdempotentDelegateSplInstructions(owner, mint, amount, opts)
|
|
|
717
791
|
const isPrivate = opts?.private ?? false;
|
|
718
792
|
const shuttleId = opts?.shuttleId ?? randomShuttleId();
|
|
719
793
|
const instructions = [];
|
|
720
|
-
const [ephemeralAta
|
|
721
|
-
const [vault
|
|
722
|
-
const [vaultEphemeralAta
|
|
794
|
+
const [ephemeralAta] = deriveEphemeralAta(owner, mint);
|
|
795
|
+
const [vault] = deriveVault(mint);
|
|
796
|
+
const [vaultEphemeralAta] = deriveEphemeralAta(vault, mint);
|
|
723
797
|
const vaultAta = deriveVaultAta(mint, vault);
|
|
724
798
|
const ownerAta = getAssociatedTokenAddressSync(mint, owner);
|
|
725
|
-
const [shuttleEphemeralAta
|
|
726
|
-
const [shuttleAta
|
|
799
|
+
const [shuttleEphemeralAta] = deriveShuttleEphemeralAta(owner, mint, shuttleId);
|
|
800
|
+
const [shuttleAta] = deriveShuttleAta(shuttleEphemeralAta, mint);
|
|
727
801
|
const shuttleWalletAta = deriveShuttleWalletAta(mint, shuttleEphemeralAta);
|
|
728
802
|
if (initVaultIfMissing) {
|
|
729
|
-
instructions.push(initVaultIx(vault, mint, payer
|
|
803
|
+
instructions.push(initVaultIx(vault, mint, payer), initVaultAtaIx(payer, vaultAta, vault, mint), delegateEphemeralAtaIx(payer, vaultEphemeralAta, validator));
|
|
730
804
|
}
|
|
731
805
|
if (initAtasIfMissing) {
|
|
732
806
|
instructions.push(createAssociatedTokenAccountIdempotentInstruction(payer, ownerAta, owner, mint));
|
|
733
807
|
}
|
|
734
808
|
if (initIfMissing) {
|
|
735
|
-
instructions.push(initEphemeralAtaIx(ephemeralAta, owner, mint, payer
|
|
809
|
+
instructions.push(initEphemeralAtaIx(ephemeralAta, owner, mint, payer));
|
|
736
810
|
}
|
|
737
811
|
if (isPrivate) {
|
|
738
|
-
instructions.push(createEataPermissionIx(ephemeralAta, payer
|
|
812
|
+
instructions.push(createEataPermissionIx(ephemeralAta, payer));
|
|
739
813
|
}
|
|
740
|
-
instructions.push(delegateEphemeralAtaIx(payer, ephemeralAta,
|
|
814
|
+
instructions.push(delegateEphemeralAtaIx(payer, ephemeralAta, validator));
|
|
741
815
|
if (amount > 0n) {
|
|
742
|
-
instructions.push(setupAndDelegateShuttleEphemeralAtaWithMergeIx(payer, shuttleEphemeralAta, shuttleAta, owner, ownerAta, ownerAta, shuttleWalletAta, mint, shuttleId,
|
|
816
|
+
instructions.push(setupAndDelegateShuttleEphemeralAtaWithMergeIx(payer, shuttleEphemeralAta, shuttleAta, owner, ownerAta, ownerAta, shuttleWalletAta, mint, shuttleId, amount, validator));
|
|
743
817
|
}
|
|
744
818
|
else {
|
|
745
|
-
instructions.push(initShuttleEphemeralAtaIx(payer, shuttleEphemeralAta, shuttleAta, shuttleWalletAta, owner, mint, shuttleId
|
|
819
|
+
instructions.push(initShuttleEphemeralAtaIx(payer, shuttleEphemeralAta, shuttleAta, shuttleWalletAta, owner, mint, shuttleId), delegateShuttleEphemeralAtaIx(payer, shuttleEphemeralAta, shuttleAta, validator));
|
|
746
820
|
}
|
|
747
821
|
return instructions;
|
|
748
822
|
}
|
|
@@ -763,29 +837,32 @@ async function delegateSplWithPrivateTransfer(owner, mint, amount, opts) {
|
|
|
763
837
|
const minDelayMs = opts?.minDelayMs ?? 0n;
|
|
764
838
|
const maxDelayMs = opts?.maxDelayMs ?? minDelayMs;
|
|
765
839
|
const split = opts?.split ?? 1;
|
|
840
|
+
if (validator == null) {
|
|
841
|
+
throw new Error("validator is required for encrypted private transfers");
|
|
842
|
+
}
|
|
766
843
|
const instructions = [];
|
|
767
|
-
const [ephemeralAta
|
|
768
|
-
const [vault
|
|
769
|
-
const [vaultEphemeralAta
|
|
844
|
+
const [ephemeralAta] = deriveEphemeralAta(owner, mint);
|
|
845
|
+
const [vault] = deriveVault(mint);
|
|
846
|
+
const [vaultEphemeralAta] = deriveEphemeralAta(vault, mint);
|
|
770
847
|
const vaultAta = deriveVaultAta(mint, vault);
|
|
771
|
-
const [queue] = (0, transferQueue_js_1.deriveTransferQueue)(mint);
|
|
848
|
+
const [queue] = (0, transferQueue_js_1.deriveTransferQueue)(mint, validator);
|
|
772
849
|
const ownerAta = getAssociatedTokenAddressSync(mint, owner);
|
|
773
|
-
const [shuttleEphemeralAta
|
|
850
|
+
const [shuttleEphemeralAta] = deriveShuttleEphemeralAta(owner, mint, shuttleId);
|
|
774
851
|
const [shuttleAta] = deriveShuttleAta(shuttleEphemeralAta, mint);
|
|
775
852
|
const shuttleWalletAta = deriveShuttleWalletAta(mint, shuttleEphemeralAta);
|
|
776
853
|
if (initVaultIfMissing) {
|
|
777
|
-
instructions.push(initVaultIx(vault, mint, payer
|
|
854
|
+
instructions.push(initVaultIx(vault, mint, payer), initVaultAtaIx(payer, vaultAta, vault, mint), delegateEphemeralAtaIx(payer, vaultEphemeralAta, validator));
|
|
778
855
|
}
|
|
779
856
|
if (initTransferQueueIfMissing) {
|
|
780
|
-
instructions.push((0, transferQueue_js_1.initTransferQueueIx)(payer, queue, mint));
|
|
857
|
+
instructions.push((0, transferQueue_js_1.toTransactionInstruction)((0, transferQueue_js_1.initTransferQueueIx)(payer, queue, mint, validator)));
|
|
781
858
|
}
|
|
782
859
|
if (initAtasIfMissing) {
|
|
783
860
|
instructions.push(createAssociatedTokenAccountIdempotentInstruction(payer, ownerAta, owner, mint));
|
|
784
861
|
}
|
|
785
862
|
if (initIfMissing) {
|
|
786
|
-
instructions.push(initEphemeralAtaIx(ephemeralAta, owner, mint, payer
|
|
863
|
+
instructions.push(initEphemeralAtaIx(ephemeralAta, owner, mint, payer));
|
|
787
864
|
}
|
|
788
|
-
instructions.push(delegateEphemeralAtaIx(payer, ephemeralAta,
|
|
865
|
+
instructions.push(delegateEphemeralAtaIx(payer, ephemeralAta, validator), depositAndDelegateShuttleEphemeralAtaWithMergeAndPrivateTransferIx(payer, shuttleEphemeralAta, shuttleAta, owner, ownerAta, owner, shuttleWalletAta, mint, shuttleId, amount, minDelayMs, maxDelayMs, split, validator));
|
|
789
866
|
return instructions;
|
|
790
867
|
}
|
|
791
868
|
async function transferSpl(from, to, mint, amount, opts) {
|
|
@@ -804,11 +881,14 @@ async function transferSpl(from, to, mint, amount, opts) {
|
|
|
804
881
|
switch (opts.visibility) {
|
|
805
882
|
case "private":
|
|
806
883
|
if (opts.toBalance === "base") {
|
|
807
|
-
|
|
884
|
+
if (validator == null) {
|
|
885
|
+
throw new Error("validator is required for private ephemeral-to-base transfers");
|
|
886
|
+
}
|
|
887
|
+
const [queue] = (0, transferQueue_js_1.deriveTransferQueue)(mint, validator);
|
|
808
888
|
const [vault] = deriveVault(mint);
|
|
809
889
|
const vaultAta = deriveVaultAta(mint, vault);
|
|
810
890
|
return [
|
|
811
|
-
(0, transferQueue_js_1.depositAndQueueTransferIx)(queue, vault, mint, fromAta, vaultAta, toAta, from, amount, minDelayMs, maxDelayMs, split),
|
|
891
|
+
(0, transferQueue_js_1.toTransactionInstruction)((0, transferQueue_js_1.depositAndQueueTransferIx)(queue, vault, mint, fromAta, vaultAta, toAta, from, amount, minDelayMs, maxDelayMs, split)),
|
|
812
892
|
];
|
|
813
893
|
}
|
|
814
894
|
if (opts.toBalance === "ephemeral") {
|
|
@@ -824,10 +904,10 @@ async function transferSpl(from, to, mint, amount, opts) {
|
|
|
824
904
|
}
|
|
825
905
|
const instructions = [];
|
|
826
906
|
if (initVaultIfMissing) {
|
|
827
|
-
const [vault
|
|
828
|
-
const [vaultEphemeralAta
|
|
907
|
+
const [vault] = deriveVault(mint);
|
|
908
|
+
const [vaultEphemeralAta] = deriveEphemeralAta(vault, mint);
|
|
829
909
|
const vaultAta = deriveVaultAta(mint, vault);
|
|
830
|
-
instructions.push(initVaultIx(vault, mint, payer
|
|
910
|
+
instructions.push(initVaultIx(vault, mint, payer), initVaultAtaIx(payer, vaultAta, vault, mint), delegateEphemeralAtaIx(payer, vaultEphemeralAta, validator));
|
|
831
911
|
}
|
|
832
912
|
if (opts.fromBalance === "base" && initAtasIfMissing) {
|
|
833
913
|
instructions.push(createAssociatedTokenAccountIdempotentInstruction(payer, fromAta, from, mint));
|
|
@@ -835,28 +915,25 @@ async function transferSpl(from, to, mint, amount, opts) {
|
|
|
835
915
|
switch (opts.visibility) {
|
|
836
916
|
case "private":
|
|
837
917
|
if (opts.fromBalance === "base" && opts.toBalance === "base") {
|
|
838
|
-
|
|
839
|
-
instructions.push(createAssociatedTokenAccountIdempotentInstruction(payer, toAta, to, mint));
|
|
840
|
-
}
|
|
841
|
-
const [shuttleEphemeralAta, shuttleBump] = deriveShuttleEphemeralAta(from, mint, shuttleId);
|
|
918
|
+
const [shuttleEphemeralAta] = deriveShuttleEphemeralAta(from, mint, shuttleId);
|
|
842
919
|
const [shuttleAta] = deriveShuttleAta(shuttleEphemeralAta, mint);
|
|
843
920
|
const shuttleWalletAta = deriveShuttleWalletAta(mint, shuttleEphemeralAta);
|
|
844
921
|
return [
|
|
845
922
|
...instructions,
|
|
846
|
-
depositAndDelegateShuttleEphemeralAtaWithMergeAndPrivateTransferIx(payer, shuttleEphemeralAta, shuttleAta, from, fromAta,
|
|
923
|
+
depositAndDelegateShuttleEphemeralAtaWithMergeAndPrivateTransferIx(payer, shuttleEphemeralAta, shuttleAta, from, fromAta, to, shuttleWalletAta, mint, shuttleId, amount, minDelayMs, maxDelayMs, split, validator),
|
|
847
924
|
];
|
|
848
925
|
}
|
|
849
926
|
if (opts.fromBalance === "base" && opts.toBalance === "ephemeral") {
|
|
850
927
|
if (initIfMissing) {
|
|
851
|
-
const [toEphemeralAta
|
|
852
|
-
instructions.push(createAssociatedTokenAccountIdempotentInstruction(payer, toAta, to, mint), initEphemeralAtaIx(toEphemeralAta, to, mint, payer
|
|
928
|
+
const [toEphemeralAta] = deriveEphemeralAta(to, mint);
|
|
929
|
+
instructions.push(createAssociatedTokenAccountIdempotentInstruction(payer, toAta, to, mint), initEphemeralAtaIx(toEphemeralAta, to, mint, payer), delegateEphemeralAtaIx(payer, toEphemeralAta, validator));
|
|
853
930
|
}
|
|
854
|
-
const [shuttleEphemeralAta
|
|
931
|
+
const [shuttleEphemeralAta] = deriveShuttleEphemeralAta(from, mint, shuttleId);
|
|
855
932
|
const [shuttleAta] = deriveShuttleAta(shuttleEphemeralAta, mint);
|
|
856
933
|
const shuttleWalletAta = deriveShuttleWalletAta(mint, shuttleEphemeralAta);
|
|
857
934
|
return [
|
|
858
935
|
...instructions,
|
|
859
|
-
setupAndDelegateShuttleEphemeralAtaWithMergeIx(payer, shuttleEphemeralAta, shuttleAta, from, fromAta, toAta, shuttleWalletAta, mint, shuttleId,
|
|
936
|
+
setupAndDelegateShuttleEphemeralAtaWithMergeIx(payer, shuttleEphemeralAta, shuttleAta, from, fromAta, toAta, shuttleWalletAta, mint, shuttleId, amount, validator),
|
|
860
937
|
];
|
|
861
938
|
}
|
|
862
939
|
break;
|
|
@@ -878,18 +955,18 @@ async function buildIdempotentWithdrawSplInstructions(owner, mint, amount, opts)
|
|
|
878
955
|
const initAtasIfMissing = opts?.initAtasIfMissing ?? false;
|
|
879
956
|
const shuttleId = opts?.shuttleId ?? randomShuttleId();
|
|
880
957
|
const instructions = [];
|
|
881
|
-
const [ephemeralAta
|
|
958
|
+
const [ephemeralAta] = deriveEphemeralAta(owner, mint);
|
|
882
959
|
const ownerAta = getAssociatedTokenAddressSync(mint, owner);
|
|
883
|
-
const [shuttleEphemeralAta
|
|
960
|
+
const [shuttleEphemeralAta] = deriveShuttleEphemeralAta(owner, mint, shuttleId);
|
|
884
961
|
const [shuttleAta] = deriveShuttleAta(shuttleEphemeralAta, mint);
|
|
885
962
|
const shuttleWalletAta = deriveShuttleWalletAta(mint, shuttleEphemeralAta);
|
|
886
963
|
if (initAtasIfMissing) {
|
|
887
964
|
instructions.push(createAssociatedTokenAccountIdempotentInstruction(payer, ownerAta, owner, mint));
|
|
888
965
|
}
|
|
889
966
|
if (initIfMissing) {
|
|
890
|
-
instructions.push(initEphemeralAtaIx(ephemeralAta, owner, mint, payer
|
|
967
|
+
instructions.push(initEphemeralAtaIx(ephemeralAta, owner, mint, payer));
|
|
891
968
|
}
|
|
892
|
-
instructions.push(delegateEphemeralAtaIx(payer, ephemeralAta,
|
|
969
|
+
instructions.push(delegateEphemeralAtaIx(payer, ephemeralAta, validator), withdrawThroughDelegatedShuttleWithMergeIx(payer, shuttleEphemeralAta, shuttleAta, owner, ownerAta, shuttleWalletAta, mint, shuttleId, amount, validator));
|
|
893
970
|
return instructions;
|
|
894
971
|
}
|
|
895
972
|
async function withdrawSpl(owner, mint, amount, opts) {
|