@magicblock-labs/ephemeral-rollups-kit 0.10.0 → 0.10.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/__test__/instructions.test.js +110 -34
- package/lib/__test__/instructions.test.js.map +1 -1
- package/lib/instructions/ephemeral-spl-token-program/ephemeralAta.d.ts +11 -11
- package/lib/instructions/ephemeral-spl-token-program/ephemeralAta.d.ts.map +1 -1
- package/lib/instructions/ephemeral-spl-token-program/ephemeralAta.js +157 -92
- package/lib/instructions/ephemeral-spl-token-program/ephemeralAta.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.getEphemeralAtaEncoder = getEphemeralAtaEncoder;
|
|
4
37
|
exports.getEphemeralAtaDecoder = getEphemeralAtaDecoder;
|
|
@@ -40,12 +73,16 @@ exports.transferSpl = transferSpl;
|
|
|
40
73
|
exports.withdrawSpl = withdrawSpl;
|
|
41
74
|
const kit_1 = require("@solana/kit");
|
|
42
75
|
const system_1 = require("@solana-program/system");
|
|
76
|
+
const blake2b_1 = require("@noble/hashes/blake2b");
|
|
77
|
+
const ed25519_1 = require("@noble/curves/ed25519");
|
|
78
|
+
const nacl = __importStar(require("tweetnacl"));
|
|
43
79
|
const constants_1 = require("../../constants");
|
|
44
80
|
const pda_1 = require("../../pda");
|
|
45
81
|
const transferQueue_1 = require("./transferQueue");
|
|
46
82
|
const U64_ENCODER = (0, kit_1.getU64Encoder)();
|
|
47
83
|
const TOKEN_PROGRAM_ADDRESS = "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA";
|
|
48
84
|
const ASSOCIATED_TOKEN_PROGRAM_ADDRESS = "ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL";
|
|
85
|
+
const QUEUED_TRANSFER_FLAG_CREATE_IDEMPOTENT_ATA = 1 << 0;
|
|
49
86
|
async function getAssociatedTokenAddressSync(mint, owner, allowOwnerOffCurve = false) {
|
|
50
87
|
const addressEncoder = (0, kit_1.getAddressEncoder)();
|
|
51
88
|
const [ata] = await (0, kit_1.getProgramDerivedAddress)({
|
|
@@ -69,6 +106,44 @@ function createTransferInstruction(source, destination, owner, amount) {
|
|
|
69
106
|
programAddress: TOKEN_PROGRAM_ADDRESS,
|
|
70
107
|
};
|
|
71
108
|
}
|
|
109
|
+
function encryptEd25519Recipient(plaintext, recipient) {
|
|
110
|
+
const recipientBytes = (0, kit_1.getAddressEncoder)().encode(recipient);
|
|
111
|
+
const recipientX25519 = (0, ed25519_1.edwardsToMontgomeryPub)(new Uint8Array(recipientBytes));
|
|
112
|
+
const ephemeral = nacl.box.keyPair();
|
|
113
|
+
const nonce = (0, blake2b_1.blake2b)(Buffer.concat([
|
|
114
|
+
Buffer.from(ephemeral.publicKey),
|
|
115
|
+
Buffer.from(recipientX25519),
|
|
116
|
+
]), { dkLen: nacl.box.nonceLength });
|
|
117
|
+
const ciphertext = nacl.box(plaintext, nonce, recipientX25519, ephemeral.secretKey);
|
|
118
|
+
return Buffer.concat([
|
|
119
|
+
Buffer.from(ephemeral.publicKey),
|
|
120
|
+
Buffer.from(ciphertext),
|
|
121
|
+
]);
|
|
122
|
+
}
|
|
123
|
+
function encodeLengthPrefixedBytes(bytes) {
|
|
124
|
+
if (bytes.length > 0xff) {
|
|
125
|
+
throw new Error("encrypted private transfer payload exceeds u8 length");
|
|
126
|
+
}
|
|
127
|
+
return Buffer.concat([Buffer.from([bytes.length]), Buffer.from(bytes)]);
|
|
128
|
+
}
|
|
129
|
+
function packPrivateTransferSuffix(minDelayMs, maxDelayMs, split, flags = 0) {
|
|
130
|
+
const suffix = Buffer.alloc(8 + 8 + 4 + 1);
|
|
131
|
+
suffix.writeBigUInt64LE(minDelayMs, 0);
|
|
132
|
+
suffix.writeBigUInt64LE(maxDelayMs, 8);
|
|
133
|
+
suffix.writeUInt32LE(split, 16);
|
|
134
|
+
suffix.writeUInt8(flags, 20);
|
|
135
|
+
return suffix;
|
|
136
|
+
}
|
|
137
|
+
function u32leBuffer(value) {
|
|
138
|
+
const out = Buffer.alloc(4);
|
|
139
|
+
out.writeUInt32LE(value, 0);
|
|
140
|
+
return out;
|
|
141
|
+
}
|
|
142
|
+
function u64leBuffer(value) {
|
|
143
|
+
const out = Buffer.alloc(8);
|
|
144
|
+
out.writeBigUInt64LE(value, 0);
|
|
145
|
+
return out;
|
|
146
|
+
}
|
|
72
147
|
function getEphemeralAtaEncoder() {
|
|
73
148
|
return (0, kit_1.getStructEncoder)([
|
|
74
149
|
["owner", (0, kit_1.getAddressEncoder)()],
|
|
@@ -162,7 +237,7 @@ async function deriveShuttleAta(shuttleEphemeralAta, mint) {
|
|
|
162
237
|
async function deriveShuttleWalletAta(mint, shuttleEphemeralAta) {
|
|
163
238
|
return getAssociatedTokenAddressSync(mint, shuttleEphemeralAta, true);
|
|
164
239
|
}
|
|
165
|
-
function initEphemeralAtaIx(ephemeralAta, owner, mint, payer
|
|
240
|
+
function initEphemeralAtaIx(ephemeralAta, owner, mint, payer) {
|
|
166
241
|
return {
|
|
167
242
|
accounts: [
|
|
168
243
|
{ address: ephemeralAta, role: kit_1.AccountRole.WRITABLE },
|
|
@@ -171,7 +246,7 @@ function initEphemeralAtaIx(ephemeralAta, owner, mint, payer, bump) {
|
|
|
171
246
|
{ address: mint, role: kit_1.AccountRole.READONLY },
|
|
172
247
|
{ address: system_1.SYSTEM_PROGRAM_ADDRESS, role: kit_1.AccountRole.READONLY },
|
|
173
248
|
],
|
|
174
|
-
data: new Uint8Array([0
|
|
249
|
+
data: new Uint8Array([0]),
|
|
175
250
|
programAddress: constants_1.EPHEMERAL_SPL_TOKEN_PROGRAM_ID,
|
|
176
251
|
};
|
|
177
252
|
}
|
|
@@ -189,7 +264,7 @@ function initVaultAtaIx(payer, vaultAta, vault, mint) {
|
|
|
189
264
|
programAddress: ASSOCIATED_TOKEN_PROGRAM_ADDRESS,
|
|
190
265
|
};
|
|
191
266
|
}
|
|
192
|
-
function initVaultIx(vault, mint, payer,
|
|
267
|
+
function initVaultIx(vault, mint, payer, vaultEphemeralAta, vaultAta) {
|
|
193
268
|
return {
|
|
194
269
|
accounts: [
|
|
195
270
|
{ address: vault, role: kit_1.AccountRole.WRITABLE },
|
|
@@ -204,7 +279,7 @@ function initVaultIx(vault, mint, payer, bump, vaultEphemeralAta, vaultAta) {
|
|
|
204
279
|
},
|
|
205
280
|
{ address: system_1.SYSTEM_PROGRAM_ADDRESS, role: kit_1.AccountRole.READONLY },
|
|
206
281
|
],
|
|
207
|
-
data: new Uint8Array([1
|
|
282
|
+
data: new Uint8Array([1]),
|
|
208
283
|
programAddress: constants_1.EPHEMERAL_SPL_TOKEN_PROGRAM_ID,
|
|
209
284
|
};
|
|
210
285
|
}
|
|
@@ -234,23 +309,21 @@ function transferToVaultIx(ephemeralAta, vault, mint, sourceAta, vaultAta, owner
|
|
|
234
309
|
programAddress: constants_1.EPHEMERAL_SPL_TOKEN_PROGRAM_ID,
|
|
235
310
|
};
|
|
236
311
|
}
|
|
237
|
-
async function delegateIx(payer, ephemeralAta,
|
|
312
|
+
async function delegateIx(payer, ephemeralAta, validator) {
|
|
238
313
|
const delegateBuffer = await (0, pda_1.delegateBufferPdaFromDelegatedAccountAndOwnerProgram)(ephemeralAta, constants_1.EPHEMERAL_SPL_TOKEN_PROGRAM_ID);
|
|
239
314
|
const delegationRecord = await (0, pda_1.delegationRecordPdaFromDelegatedAccount)(ephemeralAta);
|
|
240
315
|
const delegationMetadata = await (0, pda_1.delegationMetadataPdaFromDelegatedAccount)(ephemeralAta);
|
|
241
316
|
const encoder = (0, kit_1.getAddressEncoder)();
|
|
242
317
|
let data;
|
|
243
318
|
if (validator) {
|
|
244
|
-
data = new Uint8Array(
|
|
319
|
+
data = new Uint8Array(33);
|
|
245
320
|
data[0] = 4;
|
|
246
|
-
data[1] = bump;
|
|
247
321
|
const validatorBytes = encoder.encode(validator);
|
|
248
|
-
data.set(validatorBytes,
|
|
322
|
+
data.set(validatorBytes, 1);
|
|
249
323
|
}
|
|
250
324
|
else {
|
|
251
|
-
data = new Uint8Array(
|
|
325
|
+
data = new Uint8Array(1);
|
|
252
326
|
data[0] = 4;
|
|
253
|
-
data[1] = bump;
|
|
254
327
|
}
|
|
255
328
|
return {
|
|
256
329
|
accounts: [
|
|
@@ -267,16 +340,15 @@ async function delegateIx(payer, ephemeralAta, bump, validator) {
|
|
|
267
340
|
programAddress: constants_1.EPHEMERAL_SPL_TOKEN_PROGRAM_ID,
|
|
268
341
|
};
|
|
269
342
|
}
|
|
270
|
-
function initShuttleEphemeralAtaIx(payer, shuttleEphemeralAta, shuttleAta, shuttleWalletAta, owner, mint, shuttleId
|
|
343
|
+
function initShuttleEphemeralAtaIx(payer, shuttleEphemeralAta, shuttleAta, shuttleWalletAta, owner, mint, shuttleId) {
|
|
271
344
|
if (!Number.isInteger(shuttleId) ||
|
|
272
345
|
shuttleId < 0 ||
|
|
273
346
|
shuttleId > 4294967295) {
|
|
274
347
|
throw new Error("shuttleId must fit in u32");
|
|
275
348
|
}
|
|
276
|
-
const data = new Uint8Array(
|
|
349
|
+
const data = new Uint8Array(5);
|
|
277
350
|
data[0] = 11;
|
|
278
351
|
new DataView(data.buffer).setUint32(1, shuttleId, true);
|
|
279
|
-
data[5] = bump;
|
|
280
352
|
return {
|
|
281
353
|
accounts: [
|
|
282
354
|
{ address: payer, role: kit_1.AccountRole.WRITABLE_SIGNER },
|
|
@@ -296,22 +368,20 @@ function initShuttleEphemeralAtaIx(payer, shuttleEphemeralAta, shuttleAta, shutt
|
|
|
296
368
|
programAddress: constants_1.EPHEMERAL_SPL_TOKEN_PROGRAM_ID,
|
|
297
369
|
};
|
|
298
370
|
}
|
|
299
|
-
async function delegateShuttleEphemeralAtaIx(payer, shuttleEphemeralAta, shuttleAta,
|
|
371
|
+
async function delegateShuttleEphemeralAtaIx(payer, shuttleEphemeralAta, shuttleAta, validator) {
|
|
300
372
|
const delegateBuffer = await (0, pda_1.delegateBufferPdaFromDelegatedAccountAndOwnerProgram)(shuttleAta, constants_1.EPHEMERAL_SPL_TOKEN_PROGRAM_ID);
|
|
301
373
|
const delegationRecord = await (0, pda_1.delegationRecordPdaFromDelegatedAccount)(shuttleAta);
|
|
302
374
|
const delegationMetadata = await (0, pda_1.delegationMetadataPdaFromDelegatedAccount)(shuttleAta);
|
|
303
375
|
const addressEncoder = (0, kit_1.getAddressEncoder)();
|
|
304
376
|
let data;
|
|
305
377
|
if (validator) {
|
|
306
|
-
data = new Uint8Array(
|
|
378
|
+
data = new Uint8Array(33);
|
|
307
379
|
data[0] = 13;
|
|
308
|
-
data
|
|
309
|
-
data.set(addressEncoder.encode(validator), 2);
|
|
380
|
+
data.set(addressEncoder.encode(validator), 1);
|
|
310
381
|
}
|
|
311
382
|
else {
|
|
312
|
-
data = new Uint8Array(
|
|
383
|
+
data = new Uint8Array(1);
|
|
313
384
|
data[0] = 13;
|
|
314
|
-
data[1] = bump;
|
|
315
385
|
}
|
|
316
386
|
return {
|
|
317
387
|
accounts: [
|
|
@@ -329,7 +399,7 @@ async function delegateShuttleEphemeralAtaIx(payer, shuttleEphemeralAta, shuttle
|
|
|
329
399
|
programAddress: constants_1.EPHEMERAL_SPL_TOKEN_PROGRAM_ID,
|
|
330
400
|
};
|
|
331
401
|
}
|
|
332
|
-
async function setupAndDelegateShuttleEphemeralAtaWithMergeIx(payer, shuttleEphemeralAta, shuttleAta, owner, sourceAta, destinationAta, shuttleWalletAta, mint, shuttleId,
|
|
402
|
+
async function setupAndDelegateShuttleEphemeralAtaWithMergeIx(payer, shuttleEphemeralAta, shuttleAta, owner, sourceAta, destinationAta, shuttleWalletAta, mint, shuttleId, amount, validator) {
|
|
333
403
|
if (!Number.isInteger(shuttleId) ||
|
|
334
404
|
shuttleId < 0 ||
|
|
335
405
|
shuttleId > 4294967295) {
|
|
@@ -342,14 +412,13 @@ async function setupAndDelegateShuttleEphemeralAtaWithMergeIx(payer, shuttleEphe
|
|
|
342
412
|
const delegationRecord = await (0, pda_1.delegationRecordPdaFromDelegatedAccount)(shuttleAta);
|
|
343
413
|
const delegationMetadata = await (0, pda_1.delegationMetadataPdaFromDelegatedAccount)(shuttleAta);
|
|
344
414
|
const addressEncoder = (0, kit_1.getAddressEncoder)();
|
|
345
|
-
const data = new Uint8Array(validator ?
|
|
415
|
+
const data = new Uint8Array(validator ? 45 : 13);
|
|
346
416
|
const dataView = new DataView(data.buffer, data.byteOffset, data.byteLength);
|
|
347
417
|
data[0] = 24;
|
|
348
418
|
dataView.setUint32(1, shuttleId, true);
|
|
349
|
-
|
|
350
|
-
dataView.setBigUint64(6, amount, true);
|
|
419
|
+
dataView.setBigUint64(5, amount, true);
|
|
351
420
|
if (validator) {
|
|
352
|
-
data.set(addressEncoder.encode(validator),
|
|
421
|
+
data.set(addressEncoder.encode(validator), 13);
|
|
353
422
|
}
|
|
354
423
|
return {
|
|
355
424
|
accounts: [
|
|
@@ -380,7 +449,7 @@ async function setupAndDelegateShuttleEphemeralAtaWithMergeIx(payer, shuttleEphe
|
|
|
380
449
|
programAddress: constants_1.EPHEMERAL_SPL_TOKEN_PROGRAM_ID,
|
|
381
450
|
};
|
|
382
451
|
}
|
|
383
|
-
async function depositAndDelegateShuttleEphemeralAtaWithMergeAndPrivateTransferIx(payer, shuttleEphemeralAta, shuttleAta, owner, sourceAta,
|
|
452
|
+
async function depositAndDelegateShuttleEphemeralAtaWithMergeAndPrivateTransferIx(payer, shuttleEphemeralAta, shuttleAta, owner, sourceAta, destinationOwner, shuttleWalletAta, mint, shuttleId, amount, minDelayMs, maxDelayMs, split, validator) {
|
|
384
453
|
if (!Number.isInteger(shuttleId) ||
|
|
385
454
|
shuttleId < 0 ||
|
|
386
455
|
shuttleId > 4294967295) {
|
|
@@ -395,6 +464,9 @@ async function depositAndDelegateShuttleEphemeralAtaWithMergeAndPrivateTransferI
|
|
|
395
464
|
if (maxDelayMs < minDelayMs) {
|
|
396
465
|
throw new Error("maxDelayMs must be greater than or equal to minDelayMs");
|
|
397
466
|
}
|
|
467
|
+
if (validator == null) {
|
|
468
|
+
throw new Error("validator is required for encrypted private transfers");
|
|
469
|
+
}
|
|
398
470
|
if (!Number.isInteger(split) || split <= 0 || split > 4294967295) {
|
|
399
471
|
throw new Error("split must fit in u32 and be positive");
|
|
400
472
|
}
|
|
@@ -406,18 +478,16 @@ async function depositAndDelegateShuttleEphemeralAtaWithMergeAndPrivateTransferI
|
|
|
406
478
|
const delegationRecord = await (0, pda_1.delegationRecordPdaFromDelegatedAccount)(shuttleAta);
|
|
407
479
|
const delegationMetadata = await (0, pda_1.delegationMetadataPdaFromDelegatedAccount)(shuttleAta);
|
|
408
480
|
const addressEncoder = (0, kit_1.getAddressEncoder)();
|
|
409
|
-
const
|
|
410
|
-
const
|
|
411
|
-
data
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
data.set(addressEncoder.encode(validator), 34);
|
|
420
|
-
}
|
|
481
|
+
const encryptedDestination = encryptEd25519Recipient(new Uint8Array(addressEncoder.encode(destinationOwner)), validator);
|
|
482
|
+
const encryptedSuffix = encryptEd25519Recipient(packPrivateTransferSuffix(minDelayMs, maxDelayMs, split, QUEUED_TRANSFER_FLAG_CREATE_IDEMPOTENT_ATA), validator);
|
|
483
|
+
const data = Buffer.concat([
|
|
484
|
+
Buffer.from([25]),
|
|
485
|
+
u32leBuffer(shuttleId),
|
|
486
|
+
u64leBuffer(amount),
|
|
487
|
+
encodeLengthPrefixedBytes(new Uint8Array(addressEncoder.encode(validator))),
|
|
488
|
+
encodeLengthPrefixedBytes(encryptedDestination),
|
|
489
|
+
encodeLengthPrefixedBytes(encryptedSuffix),
|
|
490
|
+
]);
|
|
421
491
|
return {
|
|
422
492
|
accounts: [
|
|
423
493
|
{ address: payer, role: kit_1.AccountRole.WRITABLE_SIGNER },
|
|
@@ -436,7 +506,6 @@ async function depositAndDelegateShuttleEphemeralAtaWithMergeAndPrivateTransferI
|
|
|
436
506
|
role: kit_1.AccountRole.READONLY,
|
|
437
507
|
},
|
|
438
508
|
{ address: system_1.SYSTEM_PROGRAM_ADDRESS, role: kit_1.AccountRole.READONLY },
|
|
439
|
-
{ address: destinationAta, role: kit_1.AccountRole.WRITABLE },
|
|
440
509
|
{ address: mint, role: kit_1.AccountRole.READONLY },
|
|
441
510
|
{ address: TOKEN_PROGRAM_ADDRESS, role: kit_1.AccountRole.READONLY },
|
|
442
511
|
{ address: vault, role: kit_1.AccountRole.READONLY },
|
|
@@ -448,7 +517,7 @@ async function depositAndDelegateShuttleEphemeralAtaWithMergeAndPrivateTransferI
|
|
|
448
517
|
programAddress: constants_1.EPHEMERAL_SPL_TOKEN_PROGRAM_ID,
|
|
449
518
|
};
|
|
450
519
|
}
|
|
451
|
-
async function withdrawThroughDelegatedShuttleWithMergeIx(payer, shuttleEphemeralAta, shuttleAta, owner, ownerAta, shuttleWalletAta, mint, shuttleId,
|
|
520
|
+
async function withdrawThroughDelegatedShuttleWithMergeIx(payer, shuttleEphemeralAta, shuttleAta, owner, ownerAta, shuttleWalletAta, mint, shuttleId, amount, validator) {
|
|
452
521
|
if (!Number.isInteger(shuttleId) ||
|
|
453
522
|
shuttleId < 0 ||
|
|
454
523
|
shuttleId > 4294967295) {
|
|
@@ -462,14 +531,13 @@ async function withdrawThroughDelegatedShuttleWithMergeIx(payer, shuttleEphemera
|
|
|
462
531
|
const delegationRecord = await (0, pda_1.delegationRecordPdaFromDelegatedAccount)(shuttleAta);
|
|
463
532
|
const delegationMetadata = await (0, pda_1.delegationMetadataPdaFromDelegatedAccount)(shuttleAta);
|
|
464
533
|
const addressEncoder = (0, kit_1.getAddressEncoder)();
|
|
465
|
-
const data = new Uint8Array(validator ?
|
|
534
|
+
const data = new Uint8Array(validator ? 45 : 13);
|
|
466
535
|
const dataView = new DataView(data.buffer, data.byteOffset, data.byteLength);
|
|
467
536
|
data[0] = 26;
|
|
468
537
|
dataView.setUint32(1, shuttleId, true);
|
|
469
|
-
|
|
470
|
-
dataView.setBigUint64(6, amount, true);
|
|
538
|
+
dataView.setBigUint64(5, amount, true);
|
|
471
539
|
if (validator) {
|
|
472
|
-
data.set(addressEncoder.encode(validator),
|
|
540
|
+
data.set(addressEncoder.encode(validator), 13);
|
|
473
541
|
}
|
|
474
542
|
return {
|
|
475
543
|
accounts: [
|
|
@@ -535,7 +603,7 @@ function undelegateAndCloseShuttleEphemeralAtaIx(payer, shuttleEphemeralAta, shu
|
|
|
535
603
|
}
|
|
536
604
|
async function withdrawSplIx(owner, mint, amount) {
|
|
537
605
|
const [ephemeralAta] = await deriveEphemeralAta(owner, mint);
|
|
538
|
-
const [vault
|
|
606
|
+
const [vault] = await deriveVault(mint);
|
|
539
607
|
const vaultAta = await deriveVaultAta(mint, vault);
|
|
540
608
|
const userDestAta = await getAssociatedTokenAddressSync(mint, owner);
|
|
541
609
|
return {
|
|
@@ -548,7 +616,7 @@ async function withdrawSplIx(owner, mint, amount) {
|
|
|
548
616
|
{ address: userDestAta, role: kit_1.AccountRole.WRITABLE },
|
|
549
617
|
{ address: TOKEN_PROGRAM_ADDRESS, role: kit_1.AccountRole.READONLY },
|
|
550
618
|
],
|
|
551
|
-
data: encodeAmountInstructionData(3, amount
|
|
619
|
+
data: encodeAmountInstructionData(3, amount),
|
|
552
620
|
programAddress: constants_1.EPHEMERAL_SPL_TOKEN_PROGRAM_ID,
|
|
553
621
|
};
|
|
554
622
|
}
|
|
@@ -582,7 +650,7 @@ async function undelegateIx(owner, mint) {
|
|
|
582
650
|
programAddress: constants_1.EPHEMERAL_SPL_TOKEN_PROGRAM_ID,
|
|
583
651
|
};
|
|
584
652
|
}
|
|
585
|
-
async function createEataPermissionIx(ephemeralAta, payer,
|
|
653
|
+
async function createEataPermissionIx(ephemeralAta, payer, flags = 0) {
|
|
586
654
|
const permission = await (0, pda_1.permissionPdaFromAccount)(ephemeralAta);
|
|
587
655
|
return {
|
|
588
656
|
accounts: [
|
|
@@ -592,11 +660,11 @@ async function createEataPermissionIx(ephemeralAta, payer, bump, flags = 0) {
|
|
|
592
660
|
{ address: system_1.SYSTEM_PROGRAM_ADDRESS, role: kit_1.AccountRole.READONLY },
|
|
593
661
|
{ address: constants_1.PERMISSION_PROGRAM_ID, role: kit_1.AccountRole.READONLY },
|
|
594
662
|
],
|
|
595
|
-
data: new Uint8Array([6,
|
|
663
|
+
data: new Uint8Array([6, flags]),
|
|
596
664
|
programAddress: constants_1.EPHEMERAL_SPL_TOKEN_PROGRAM_ID,
|
|
597
665
|
};
|
|
598
666
|
}
|
|
599
|
-
async function resetEataPermissionIx(ephemeralAta, payer,
|
|
667
|
+
async function resetEataPermissionIx(ephemeralAta, payer, flags = 0) {
|
|
600
668
|
const permission = await (0, pda_1.permissionPdaFromAccount)(ephemeralAta);
|
|
601
669
|
return {
|
|
602
670
|
accounts: [
|
|
@@ -605,11 +673,11 @@ async function resetEataPermissionIx(ephemeralAta, payer, bump, flags = 0) {
|
|
|
605
673
|
{ address: payer, role: kit_1.AccountRole.READONLY_SIGNER },
|
|
606
674
|
{ address: constants_1.PERMISSION_PROGRAM_ID, role: kit_1.AccountRole.READONLY },
|
|
607
675
|
],
|
|
608
|
-
data: new Uint8Array([9,
|
|
676
|
+
data: new Uint8Array([9, flags]),
|
|
609
677
|
programAddress: constants_1.EPHEMERAL_SPL_TOKEN_PROGRAM_ID,
|
|
610
678
|
};
|
|
611
679
|
}
|
|
612
|
-
async function delegateEataPermissionIx(payer, ephemeralAta,
|
|
680
|
+
async function delegateEataPermissionIx(payer, ephemeralAta, validator) {
|
|
613
681
|
const permission = await (0, pda_1.permissionPdaFromAccount)(ephemeralAta);
|
|
614
682
|
return {
|
|
615
683
|
accounts: [
|
|
@@ -633,7 +701,7 @@ async function delegateEataPermissionIx(payer, ephemeralAta, bump, validator) {
|
|
|
633
701
|
{ address: constants_1.DELEGATION_PROGRAM_ID, role: kit_1.AccountRole.READONLY },
|
|
634
702
|
{ address: validator, role: kit_1.AccountRole.READONLY },
|
|
635
703
|
],
|
|
636
|
-
data: new Uint8Array([7
|
|
704
|
+
data: new Uint8Array([7]),
|
|
637
705
|
programAddress: constants_1.EPHEMERAL_SPL_TOKEN_PROGRAM_ID,
|
|
638
706
|
};
|
|
639
707
|
}
|
|
@@ -668,22 +736,22 @@ async function buildDelegateSplInstructions(owner, mint, amount, opts) {
|
|
|
668
736
|
const initVaultIfMissing = opts?.initVaultIfMissing ?? false;
|
|
669
737
|
const isPrivate = opts?.private ?? false;
|
|
670
738
|
const instructions = [];
|
|
671
|
-
const [ephemeralAta
|
|
672
|
-
const [vault
|
|
673
|
-
const [vaultEphemeralAta
|
|
739
|
+
const [ephemeralAta] = await deriveEphemeralAta(owner, mint);
|
|
740
|
+
const [vault] = await deriveVault(mint);
|
|
741
|
+
const [vaultEphemeralAta] = await deriveEphemeralAta(vault, mint);
|
|
674
742
|
const vaultAta = await deriveVaultAta(mint, vault);
|
|
675
743
|
const ownerAta = await getAssociatedTokenAddressSync(mint, owner);
|
|
676
744
|
if (initIfMissing) {
|
|
677
|
-
instructions.push(initEphemeralAtaIx(ephemeralAta, owner, mint, payer
|
|
745
|
+
instructions.push(initEphemeralAtaIx(ephemeralAta, owner, mint, payer));
|
|
678
746
|
}
|
|
679
747
|
if (initVaultIfMissing) {
|
|
680
|
-
instructions.push(initVaultIx(vault, mint, payer,
|
|
748
|
+
instructions.push(initVaultIx(vault, mint, payer, vaultEphemeralAta, vaultAta), initVaultAtaIx(payer, vaultAta, vault, mint), await delegateIx(payer, vaultEphemeralAta, validator));
|
|
681
749
|
}
|
|
682
750
|
instructions.push(transferToVaultIx(ephemeralAta, vault, mint, ownerAta, vaultAta, owner, amount));
|
|
683
751
|
if (isPrivate) {
|
|
684
|
-
instructions.push(await createEataPermissionIx(ephemeralAta, payer
|
|
752
|
+
instructions.push(await createEataPermissionIx(ephemeralAta, payer));
|
|
685
753
|
}
|
|
686
|
-
instructions.push(await delegateIx(payer, ephemeralAta,
|
|
754
|
+
instructions.push(await delegateIx(payer, ephemeralAta, validator));
|
|
687
755
|
return instructions;
|
|
688
756
|
}
|
|
689
757
|
async function buildIdempotentDelegateSplInstructions(owner, mint, amount, opts) {
|
|
@@ -695,32 +763,32 @@ async function buildIdempotentDelegateSplInstructions(owner, mint, amount, opts)
|
|
|
695
763
|
const isPrivate = opts?.private ?? false;
|
|
696
764
|
const shuttleId = opts?.shuttleId ?? randomShuttleId();
|
|
697
765
|
const instructions = [];
|
|
698
|
-
const [ephemeralAta
|
|
699
|
-
const [vault
|
|
700
|
-
const [vaultEphemeralAta
|
|
766
|
+
const [ephemeralAta] = await deriveEphemeralAta(owner, mint);
|
|
767
|
+
const [vault] = await deriveVault(mint);
|
|
768
|
+
const [vaultEphemeralAta] = await deriveEphemeralAta(vault, mint);
|
|
701
769
|
const vaultAta = await deriveVaultAta(mint, vault);
|
|
702
770
|
const ownerAta = await getAssociatedTokenAddressSync(mint, owner);
|
|
703
|
-
const [shuttleEphemeralAta
|
|
704
|
-
const [shuttleAta
|
|
771
|
+
const [shuttleEphemeralAta] = await deriveShuttleEphemeralAta(owner, mint, shuttleId);
|
|
772
|
+
const [shuttleAta] = await deriveShuttleAta(shuttleEphemeralAta, mint);
|
|
705
773
|
const shuttleWalletAta = await deriveShuttleWalletAta(mint, shuttleEphemeralAta);
|
|
706
774
|
if (initVaultIfMissing) {
|
|
707
|
-
instructions.push(initVaultIx(vault, mint, payer,
|
|
775
|
+
instructions.push(initVaultIx(vault, mint, payer, vaultEphemeralAta, vaultAta), initVaultAtaIx(payer, vaultAta, vault, mint), await delegateIx(payer, vaultEphemeralAta, validator));
|
|
708
776
|
}
|
|
709
777
|
if (initAtasIfMissing) {
|
|
710
778
|
instructions.push(initVaultAtaIx(payer, ownerAta, owner, mint));
|
|
711
779
|
}
|
|
712
780
|
if (initIfMissing) {
|
|
713
|
-
instructions.push(initEphemeralAtaIx(ephemeralAta, owner, mint, payer
|
|
781
|
+
instructions.push(initEphemeralAtaIx(ephemeralAta, owner, mint, payer));
|
|
714
782
|
}
|
|
715
783
|
if (isPrivate) {
|
|
716
|
-
instructions.push(await createEataPermissionIx(ephemeralAta, payer
|
|
784
|
+
instructions.push(await createEataPermissionIx(ephemeralAta, payer));
|
|
717
785
|
}
|
|
718
|
-
instructions.push(await delegateIx(payer, ephemeralAta,
|
|
786
|
+
instructions.push(await delegateIx(payer, ephemeralAta, validator));
|
|
719
787
|
if (amount > 0n) {
|
|
720
|
-
instructions.push(await setupAndDelegateShuttleEphemeralAtaWithMergeIx(payer, shuttleEphemeralAta, shuttleAta, owner, ownerAta, ownerAta, shuttleWalletAta, mint, shuttleId,
|
|
788
|
+
instructions.push(await setupAndDelegateShuttleEphemeralAtaWithMergeIx(payer, shuttleEphemeralAta, shuttleAta, owner, ownerAta, ownerAta, shuttleWalletAta, mint, shuttleId, amount, validator));
|
|
721
789
|
}
|
|
722
790
|
else {
|
|
723
|
-
instructions.push(initShuttleEphemeralAtaIx(payer, shuttleEphemeralAta, shuttleAta, shuttleWalletAta, owner, mint, shuttleId
|
|
791
|
+
instructions.push(initShuttleEphemeralAtaIx(payer, shuttleEphemeralAta, shuttleAta, shuttleWalletAta, owner, mint, shuttleId), await delegateShuttleEphemeralAtaIx(payer, shuttleEphemeralAta, shuttleAta, validator));
|
|
724
792
|
}
|
|
725
793
|
return instructions;
|
|
726
794
|
}
|
|
@@ -742,17 +810,17 @@ async function delegateSplWithPrivateTransfer(owner, mint, amount, opts) {
|
|
|
742
810
|
const maxDelayMs = opts?.maxDelayMs ?? minDelayMs;
|
|
743
811
|
const split = opts?.split ?? 1;
|
|
744
812
|
const instructions = [];
|
|
745
|
-
const [ephemeralAta
|
|
746
|
-
const [vault
|
|
747
|
-
const [vaultEphemeralAta
|
|
813
|
+
const [ephemeralAta] = await deriveEphemeralAta(owner, mint);
|
|
814
|
+
const [vault] = await deriveVault(mint);
|
|
815
|
+
const [vaultEphemeralAta] = await deriveEphemeralAta(vault, mint);
|
|
748
816
|
const vaultAta = await deriveVaultAta(mint, vault);
|
|
749
817
|
const [queue] = await (0, transferQueue_1.deriveTransferQueue)(mint);
|
|
750
818
|
const ownerAta = await getAssociatedTokenAddressSync(mint, owner);
|
|
751
|
-
const [shuttleEphemeralAta
|
|
819
|
+
const [shuttleEphemeralAta] = await deriveShuttleEphemeralAta(owner, mint, shuttleId);
|
|
752
820
|
const [shuttleAta] = await deriveShuttleAta(shuttleEphemeralAta, mint);
|
|
753
821
|
const shuttleWalletAta = await deriveShuttleWalletAta(mint, shuttleEphemeralAta);
|
|
754
822
|
if (initVaultIfMissing) {
|
|
755
|
-
instructions.push(initVaultIx(vault, mint, payer,
|
|
823
|
+
instructions.push(initVaultIx(vault, mint, payer, vaultEphemeralAta, vaultAta), initVaultAtaIx(payer, vaultAta, vault, mint), await delegateIx(payer, vaultEphemeralAta, validator));
|
|
756
824
|
}
|
|
757
825
|
if (initTransferQueueIfMissing) {
|
|
758
826
|
instructions.push((0, transferQueue_1.initTransferQueueIx)(payer, queue, mint));
|
|
@@ -761,9 +829,9 @@ async function delegateSplWithPrivateTransfer(owner, mint, amount, opts) {
|
|
|
761
829
|
instructions.push(initVaultAtaIx(payer, ownerAta, owner, mint));
|
|
762
830
|
}
|
|
763
831
|
if (initIfMissing) {
|
|
764
|
-
instructions.push(initEphemeralAtaIx(ephemeralAta, owner, mint, payer
|
|
832
|
+
instructions.push(initEphemeralAtaIx(ephemeralAta, owner, mint, payer));
|
|
765
833
|
}
|
|
766
|
-
instructions.push(await delegateIx(payer, ephemeralAta,
|
|
834
|
+
instructions.push(await delegateIx(payer, ephemeralAta, validator), await depositAndDelegateShuttleEphemeralAtaWithMergeAndPrivateTransferIx(payer, shuttleEphemeralAta, shuttleAta, owner, ownerAta, owner, shuttleWalletAta, mint, shuttleId, amount, minDelayMs, maxDelayMs, split, validator));
|
|
767
835
|
return instructions;
|
|
768
836
|
}
|
|
769
837
|
async function transferSpl(from, to, mint, amount, opts) {
|
|
@@ -802,10 +870,10 @@ async function transferSpl(from, to, mint, amount, opts) {
|
|
|
802
870
|
}
|
|
803
871
|
const instructions = [];
|
|
804
872
|
if (initVaultIfMissing) {
|
|
805
|
-
const [vault
|
|
806
|
-
const [vaultEphemeralAta
|
|
873
|
+
const [vault] = await deriveVault(mint);
|
|
874
|
+
const [vaultEphemeralAta] = await deriveEphemeralAta(vault, mint);
|
|
807
875
|
const vaultAta = await deriveVaultAta(mint, vault);
|
|
808
|
-
instructions.push(initVaultIx(vault, mint, payer,
|
|
876
|
+
instructions.push(initVaultIx(vault, mint, payer, vaultEphemeralAta, vaultAta), initVaultAtaIx(payer, vaultAta, vault, mint), await delegateIx(payer, vaultEphemeralAta, validator));
|
|
809
877
|
}
|
|
810
878
|
if (opts.fromBalance === "base" && initAtasIfMissing) {
|
|
811
879
|
instructions.push(initVaultAtaIx(payer, fromAta, from, mint));
|
|
@@ -813,28 +881,25 @@ async function transferSpl(from, to, mint, amount, opts) {
|
|
|
813
881
|
switch (opts.visibility) {
|
|
814
882
|
case "private":
|
|
815
883
|
if (opts.fromBalance === "base" && opts.toBalance === "base") {
|
|
816
|
-
|
|
817
|
-
instructions.push(initVaultAtaIx(payer, toAta, to, mint));
|
|
818
|
-
}
|
|
819
|
-
const [shuttleEphemeralAta, shuttleBump] = await deriveShuttleEphemeralAta(from, mint, shuttleId);
|
|
884
|
+
const [shuttleEphemeralAta] = await deriveShuttleEphemeralAta(from, mint, shuttleId);
|
|
820
885
|
const [shuttleAta] = await deriveShuttleAta(shuttleEphemeralAta, mint);
|
|
821
886
|
const shuttleWalletAta = await deriveShuttleWalletAta(mint, shuttleEphemeralAta);
|
|
822
887
|
return [
|
|
823
888
|
...instructions,
|
|
824
|
-
await depositAndDelegateShuttleEphemeralAtaWithMergeAndPrivateTransferIx(payer, shuttleEphemeralAta, shuttleAta, from, fromAta,
|
|
889
|
+
await depositAndDelegateShuttleEphemeralAtaWithMergeAndPrivateTransferIx(payer, shuttleEphemeralAta, shuttleAta, from, fromAta, to, shuttleWalletAta, mint, shuttleId, amount, minDelayMs, maxDelayMs, split, validator),
|
|
825
890
|
];
|
|
826
891
|
}
|
|
827
892
|
if (opts.fromBalance === "base" && opts.toBalance === "ephemeral") {
|
|
828
893
|
if (initIfMissing) {
|
|
829
|
-
const [toEphemeralAta
|
|
830
|
-
instructions.push(initVaultAtaIx(payer, toAta, to, mint), initEphemeralAtaIx(toEphemeralAta, to, mint, payer
|
|
894
|
+
const [toEphemeralAta] = await deriveEphemeralAta(to, mint);
|
|
895
|
+
instructions.push(initVaultAtaIx(payer, toAta, to, mint), initEphemeralAtaIx(toEphemeralAta, to, mint, payer), await delegateIx(payer, toEphemeralAta, validator));
|
|
831
896
|
}
|
|
832
|
-
const [shuttleEphemeralAta
|
|
897
|
+
const [shuttleEphemeralAta] = await deriveShuttleEphemeralAta(from, mint, shuttleId);
|
|
833
898
|
const [shuttleAta] = await deriveShuttleAta(shuttleEphemeralAta, mint);
|
|
834
899
|
const shuttleWalletAta = await deriveShuttleWalletAta(mint, shuttleEphemeralAta);
|
|
835
900
|
return [
|
|
836
901
|
...instructions,
|
|
837
|
-
await setupAndDelegateShuttleEphemeralAtaWithMergeIx(payer, shuttleEphemeralAta, shuttleAta, from, fromAta, toAta, shuttleWalletAta, mint, shuttleId,
|
|
902
|
+
await setupAndDelegateShuttleEphemeralAtaWithMergeIx(payer, shuttleEphemeralAta, shuttleAta, from, fromAta, toAta, shuttleWalletAta, mint, shuttleId, amount, validator),
|
|
838
903
|
];
|
|
839
904
|
}
|
|
840
905
|
break;
|
|
@@ -856,18 +921,18 @@ async function buildIdempotentWithdrawSplInstructions(owner, mint, amount, opts)
|
|
|
856
921
|
const initAtasIfMissing = opts?.initAtasIfMissing ?? false;
|
|
857
922
|
const shuttleId = opts?.shuttleId ?? randomShuttleId();
|
|
858
923
|
const instructions = [];
|
|
859
|
-
const [ephemeralAta
|
|
924
|
+
const [ephemeralAta] = await deriveEphemeralAta(owner, mint);
|
|
860
925
|
const ownerAta = await getAssociatedTokenAddressSync(mint, owner);
|
|
861
|
-
const [shuttleEphemeralAta
|
|
926
|
+
const [shuttleEphemeralAta] = await deriveShuttleEphemeralAta(owner, mint, shuttleId);
|
|
862
927
|
const [shuttleAta] = await deriveShuttleAta(shuttleEphemeralAta, mint);
|
|
863
928
|
const shuttleWalletAta = await deriveShuttleWalletAta(mint, shuttleEphemeralAta);
|
|
864
929
|
if (initAtasIfMissing) {
|
|
865
930
|
instructions.push(initVaultAtaIx(payer, ownerAta, owner, mint));
|
|
866
931
|
}
|
|
867
932
|
if (initIfMissing) {
|
|
868
|
-
instructions.push(initEphemeralAtaIx(ephemeralAta, owner, mint, payer
|
|
933
|
+
instructions.push(initEphemeralAtaIx(ephemeralAta, owner, mint, payer));
|
|
869
934
|
}
|
|
870
|
-
instructions.push(await delegateIx(payer, ephemeralAta,
|
|
935
|
+
instructions.push(await delegateIx(payer, ephemeralAta, validator), await withdrawThroughDelegatedShuttleWithMergeIx(payer, shuttleEphemeralAta, shuttleAta, owner, ownerAta, shuttleWalletAta, mint, shuttleId, amount, validator));
|
|
871
936
|
return instructions;
|
|
872
937
|
}
|
|
873
938
|
async function withdrawSpl(owner, mint, amount, opts) {
|