@solana/transactions 2.0.0-experimental.0108bc7

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.
Files changed (66) hide show
  1. package/LICENSE +20 -0
  2. package/README.md +25 -0
  3. package/dist/index.browser.cjs +771 -0
  4. package/dist/index.browser.cjs.map +1 -0
  5. package/dist/index.browser.js +760 -0
  6. package/dist/index.browser.js.map +1 -0
  7. package/dist/index.development.js +1327 -0
  8. package/dist/index.development.js.map +1 -0
  9. package/dist/index.native.js +760 -0
  10. package/dist/index.native.js.map +1 -0
  11. package/dist/index.node.cjs +773 -0
  12. package/dist/index.node.cjs.map +1 -0
  13. package/dist/index.node.js +762 -0
  14. package/dist/index.node.js.map +1 -0
  15. package/dist/index.production.min.js +20 -0
  16. package/dist/types/accounts.d.ts +28 -0
  17. package/dist/types/accounts.d.ts.map +1 -0
  18. package/dist/types/blockhash.d.ts +18 -0
  19. package/dist/types/blockhash.d.ts.map +1 -0
  20. package/dist/types/compile-address-table-lookups.d.ts +10 -0
  21. package/dist/types/compile-address-table-lookups.d.ts.map +1 -0
  22. package/dist/types/compile-header.d.ts +9 -0
  23. package/dist/types/compile-header.d.ts.map +1 -0
  24. package/dist/types/compile-instructions.d.ts +10 -0
  25. package/dist/types/compile-instructions.d.ts.map +1 -0
  26. package/dist/types/compile-lifetime-token.d.ts +3 -0
  27. package/dist/types/compile-lifetime-token.d.ts.map +1 -0
  28. package/dist/types/compile-static-accounts.d.ts +4 -0
  29. package/dist/types/compile-static-accounts.d.ts.map +1 -0
  30. package/dist/types/compile-transaction.d.ts +11 -0
  31. package/dist/types/compile-transaction.d.ts.map +1 -0
  32. package/dist/types/create-transaction.d.ts +9 -0
  33. package/dist/types/create-transaction.d.ts.map +1 -0
  34. package/dist/types/durable-nonce.d.ts +34 -0
  35. package/dist/types/durable-nonce.d.ts.map +1 -0
  36. package/dist/types/fee-payer.d.ts +8 -0
  37. package/dist/types/fee-payer.d.ts.map +1 -0
  38. package/dist/types/index.d.ts +9 -0
  39. package/dist/types/index.d.ts.map +1 -0
  40. package/dist/types/instructions.d.ts +5 -0
  41. package/dist/types/instructions.d.ts.map +1 -0
  42. package/dist/types/message.d.ts +30 -0
  43. package/dist/types/message.d.ts.map +1 -0
  44. package/dist/types/serializers/address-table-lookup.d.ts +6 -0
  45. package/dist/types/serializers/address-table-lookup.d.ts.map +1 -0
  46. package/dist/types/serializers/header.d.ts +6 -0
  47. package/dist/types/serializers/header.d.ts.map +1 -0
  48. package/dist/types/serializers/instruction.d.ts +6 -0
  49. package/dist/types/serializers/instruction.d.ts.map +1 -0
  50. package/dist/types/serializers/message.d.ts +6 -0
  51. package/dist/types/serializers/message.d.ts.map +1 -0
  52. package/dist/types/serializers/transaction-version.d.ts +6 -0
  53. package/dist/types/serializers/transaction-version.d.ts.map +1 -0
  54. package/dist/types/serializers/transaction.d.ts +6 -0
  55. package/dist/types/serializers/transaction.d.ts.map +1 -0
  56. package/dist/types/serializers/unimplemented.d.ts +3 -0
  57. package/dist/types/serializers/unimplemented.d.ts.map +1 -0
  58. package/dist/types/signatures.d.ts +11 -0
  59. package/dist/types/signatures.d.ts.map +1 -0
  60. package/dist/types/types.d.ts +19 -0
  61. package/dist/types/types.d.ts.map +1 -0
  62. package/dist/types/unsigned-transaction.d.ts +4 -0
  63. package/dist/types/unsigned-transaction.d.ts.map +1 -0
  64. package/dist/types/wire-transaction.d.ts +6 -0
  65. package/dist/types/wire-transaction.d.ts.map +1 -0
  66. package/package.json +97 -0
@@ -0,0 +1,762 @@
1
+ import { base58, struct, array, bytes, shortU16, mapSerializer, string, u8 } from '@metaplex-foundation/umi-serializers';
2
+ import { getBase58EncodedAddressFromPublicKey, getBase58EncodedAddressComparator, getBase58EncodedAddressCodec } from '@solana/addresses';
3
+ import { signBytes } from '@solana/keys';
4
+
5
+ // ../build-scripts/env-shim.ts
6
+ var __DEV__ = /* @__PURE__ */ (() => process["env"].NODE_ENV === "development")();
7
+
8
+ // src/unsigned-transaction.ts
9
+ function getUnsignedTransaction(transaction) {
10
+ if ("signatures" in transaction) {
11
+ const {
12
+ signatures: _,
13
+ // eslint-disable-line @typescript-eslint/no-unused-vars
14
+ ...unsignedTransaction
15
+ } = transaction;
16
+ return unsignedTransaction;
17
+ } else {
18
+ return transaction;
19
+ }
20
+ }
21
+
22
+ // src/blockhash.ts
23
+ function assertIsBlockhash(putativeBlockhash) {
24
+ try {
25
+ if (
26
+ // Lowest value (32 bytes of zeroes)
27
+ putativeBlockhash.length < 32 || // Highest value (32 bytes of 255)
28
+ putativeBlockhash.length > 44
29
+ ) {
30
+ throw new Error("Expected input string to decode to a byte array of length 32.");
31
+ }
32
+ const bytes3 = base58.serialize(putativeBlockhash);
33
+ const numBytes = bytes3.byteLength;
34
+ if (numBytes !== 32) {
35
+ throw new Error(`Expected input string to decode to a byte array of length 32. Actual length: ${numBytes}`);
36
+ }
37
+ } catch (e) {
38
+ throw new Error(`\`${putativeBlockhash}\` is not a blockhash`, {
39
+ cause: e
40
+ });
41
+ }
42
+ }
43
+ function setTransactionLifetimeUsingBlockhash(blockhashLifetimeConstraint, transaction) {
44
+ if ("lifetimeConstraint" in transaction && transaction.lifetimeConstraint.blockhash === blockhashLifetimeConstraint.blockhash && transaction.lifetimeConstraint.lastValidBlockHeight === blockhashLifetimeConstraint.lastValidBlockHeight) {
45
+ return transaction;
46
+ }
47
+ const out = {
48
+ ...getUnsignedTransaction(transaction),
49
+ lifetimeConstraint: blockhashLifetimeConstraint
50
+ };
51
+ Object.freeze(out);
52
+ return out;
53
+ }
54
+
55
+ // src/create-transaction.ts
56
+ function createTransaction({
57
+ version
58
+ }) {
59
+ const out = {
60
+ instructions: [],
61
+ version
62
+ };
63
+ Object.freeze(out);
64
+ return out;
65
+ }
66
+
67
+ // ../instructions/dist/index.node.js
68
+ var AccountRole = /* @__PURE__ */ ((AccountRole2) => {
69
+ AccountRole2[AccountRole2["WRITABLE_SIGNER"] = /* 3 */
70
+ 3] = "WRITABLE_SIGNER";
71
+ AccountRole2[AccountRole2["READONLY_SIGNER"] = /* 2 */
72
+ 2] = "READONLY_SIGNER";
73
+ AccountRole2[AccountRole2["WRITABLE"] = /* 1 */
74
+ 1] = "WRITABLE";
75
+ AccountRole2[AccountRole2["READONLY"] = /* 0 */
76
+ 0] = "READONLY";
77
+ return AccountRole2;
78
+ })(AccountRole || {});
79
+ var IS_WRITABLE_BITMASK = 1;
80
+ function isSignerRole(role) {
81
+ return role >= 2;
82
+ }
83
+ function isWritableRole(role) {
84
+ return (role & IS_WRITABLE_BITMASK) !== 0;
85
+ }
86
+ function mergeRoles(roleA, roleB) {
87
+ return roleA | roleB;
88
+ }
89
+
90
+ // src/durable-nonce.ts
91
+ var RECENT_BLOCKHASHES_SYSVAR_ADDRESS = "SysvarRecentB1ockHashes11111111111111111111";
92
+ var SYSTEM_PROGRAM_ADDRESS = "11111111111111111111111111111111";
93
+ function assertIsDurableNonceTransaction(transaction) {
94
+ if (!isDurableNonceTransaction(transaction)) {
95
+ throw new Error("Transaction is not a durable nonce transaction");
96
+ }
97
+ }
98
+ function createAdvanceNonceAccountInstruction(nonceAccountAddress, nonceAuthorityAddress) {
99
+ return {
100
+ accounts: [
101
+ { address: nonceAccountAddress, role: AccountRole.WRITABLE },
102
+ {
103
+ address: RECENT_BLOCKHASHES_SYSVAR_ADDRESS,
104
+ role: AccountRole.READONLY
105
+ },
106
+ { address: nonceAuthorityAddress, role: AccountRole.READONLY_SIGNER }
107
+ ],
108
+ data: new Uint8Array([4, 0, 0, 0]),
109
+ programAddress: SYSTEM_PROGRAM_ADDRESS
110
+ };
111
+ }
112
+ function isAdvanceNonceAccountInstruction(instruction) {
113
+ return instruction.programAddress === SYSTEM_PROGRAM_ADDRESS && // Test for `AdvanceNonceAccount` instruction data
114
+ instruction.data != null && isAdvanceNonceAccountInstructionData(instruction.data) && // Test for exactly 3 accounts
115
+ instruction.accounts?.length === 3 && // First account is nonce account address
116
+ instruction.accounts[0].address != null && instruction.accounts[0].role === AccountRole.WRITABLE && // Second account is recent blockhashes sysvar
117
+ instruction.accounts[1].address === RECENT_BLOCKHASHES_SYSVAR_ADDRESS && instruction.accounts[1].role === AccountRole.READONLY && // Third account is nonce authority account
118
+ instruction.accounts[2].address != null && instruction.accounts[2].role === AccountRole.READONLY_SIGNER;
119
+ }
120
+ function isAdvanceNonceAccountInstructionData(data) {
121
+ return data.byteLength === 4 && data[0] === 4 && data[1] === 0 && data[2] === 0 && data[3] === 0;
122
+ }
123
+ function isDurableNonceTransaction(transaction) {
124
+ return "lifetimeConstraint" in transaction && typeof transaction.lifetimeConstraint.nonce === "string" && transaction.instructions[0] != null && isAdvanceNonceAccountInstruction(transaction.instructions[0]);
125
+ }
126
+ function setTransactionLifetimeUsingDurableNonce({
127
+ nonce,
128
+ nonceAccountAddress,
129
+ nonceAuthorityAddress
130
+ }, transaction) {
131
+ const isAlreadyDurableNonceTransaction = isDurableNonceTransaction(transaction);
132
+ if (isAlreadyDurableNonceTransaction && transaction.lifetimeConstraint.nonce === nonce && transaction.instructions[0].accounts[0].address === nonceAccountAddress && transaction.instructions[0].accounts[2].address === nonceAuthorityAddress) {
133
+ return transaction;
134
+ }
135
+ const out = {
136
+ ...getUnsignedTransaction(transaction),
137
+ instructions: [
138
+ createAdvanceNonceAccountInstruction(nonceAccountAddress, nonceAuthorityAddress),
139
+ ...isAlreadyDurableNonceTransaction ? transaction.instructions.slice(1) : transaction.instructions
140
+ ],
141
+ lifetimeConstraint: {
142
+ nonce
143
+ }
144
+ };
145
+ Object.freeze(out);
146
+ return out;
147
+ }
148
+
149
+ // src/fee-payer.ts
150
+ function setTransactionFeePayer(feePayer, transaction) {
151
+ if ("feePayer" in transaction && feePayer === transaction.feePayer) {
152
+ return transaction;
153
+ }
154
+ const out = {
155
+ ...getUnsignedTransaction(transaction),
156
+ feePayer
157
+ };
158
+ Object.freeze(out);
159
+ return out;
160
+ }
161
+
162
+ // src/instructions.ts
163
+ function appendTransactionInstruction(instruction, transaction) {
164
+ const out = {
165
+ ...getUnsignedTransaction(transaction),
166
+ instructions: [...transaction.instructions, instruction]
167
+ };
168
+ Object.freeze(out);
169
+ return out;
170
+ }
171
+ function prependTransactionInstruction(instruction, transaction) {
172
+ const out = {
173
+ ...getUnsignedTransaction(transaction),
174
+ instructions: [instruction, ...transaction.instructions]
175
+ };
176
+ Object.freeze(out);
177
+ return out;
178
+ }
179
+ function upsert(addressMap, address, update) {
180
+ addressMap[address] = update(addressMap[address] ?? { role: AccountRole.READONLY });
181
+ }
182
+ var TYPE = Symbol("AddressMapTypeProperty");
183
+ function getAddressMapFromInstructions(feePayer, instructions) {
184
+ const addressMap = {
185
+ [feePayer]: { [TYPE]: 0 /* FEE_PAYER */, role: AccountRole.WRITABLE_SIGNER }
186
+ };
187
+ const addressesOfInvokedPrograms = /* @__PURE__ */ new Set();
188
+ for (const instruction of instructions) {
189
+ upsert(addressMap, instruction.programAddress, (entry) => {
190
+ addressesOfInvokedPrograms.add(instruction.programAddress);
191
+ if (TYPE in entry) {
192
+ if (isWritableRole(entry.role)) {
193
+ switch (entry[TYPE]) {
194
+ case 0 /* FEE_PAYER */:
195
+ throw new Error(
196
+ `This transaction includes an address (\`${instruction.programAddress}\`) which is both invoked and set as the fee payer. Program addresses may not pay fees.`
197
+ );
198
+ default:
199
+ throw new Error(
200
+ `This transaction includes an address (\`${instruction.programAddress}\`) which is both invoked and marked writable. Program addresses may not be writable.`
201
+ );
202
+ }
203
+ }
204
+ if (entry[TYPE] === 2 /* STATIC */) {
205
+ return entry;
206
+ }
207
+ }
208
+ return { [TYPE]: 2 /* STATIC */, role: AccountRole.READONLY };
209
+ });
210
+ let addressComparator;
211
+ if (!instruction.accounts) {
212
+ continue;
213
+ }
214
+ for (const account of instruction.accounts) {
215
+ upsert(addressMap, account.address, (entry) => {
216
+ const {
217
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
218
+ address: _,
219
+ ...accountMeta
220
+ } = account;
221
+ if (TYPE in entry) {
222
+ switch (entry[TYPE]) {
223
+ case 0 /* FEE_PAYER */:
224
+ return entry;
225
+ case 1 /* LOOKUP_TABLE */: {
226
+ const nextRole = mergeRoles(entry.role, accountMeta.role);
227
+ if ("lookupTableAddress" in accountMeta) {
228
+ const shouldReplaceEntry = (
229
+ // Consider using the new LOOKUP_TABLE if its address is different...
230
+ entry.lookupTableAddress !== accountMeta.lookupTableAddress && // ...and sorts before the existing one.
231
+ (addressComparator || (addressComparator = getBase58EncodedAddressComparator()))(
232
+ accountMeta.lookupTableAddress,
233
+ entry.lookupTableAddress
234
+ ) < 0
235
+ );
236
+ if (shouldReplaceEntry) {
237
+ return {
238
+ [TYPE]: 1 /* LOOKUP_TABLE */,
239
+ ...accountMeta,
240
+ role: nextRole
241
+ };
242
+ }
243
+ } else if (isSignerRole(accountMeta.role)) {
244
+ return {
245
+ [TYPE]: 2 /* STATIC */,
246
+ role: nextRole
247
+ };
248
+ }
249
+ if (entry.role !== nextRole) {
250
+ return {
251
+ ...entry,
252
+ role: nextRole
253
+ };
254
+ } else {
255
+ return entry;
256
+ }
257
+ }
258
+ case 2 /* STATIC */: {
259
+ const nextRole = mergeRoles(entry.role, accountMeta.role);
260
+ if (
261
+ // Check to see if this address represents a program that is invoked
262
+ // in this transaction.
263
+ addressesOfInvokedPrograms.has(account.address)
264
+ ) {
265
+ if (isWritableRole(accountMeta.role)) {
266
+ throw new Error(
267
+ `This transaction includes an address (\`${account.address}\`) which is both invoked and marked writable. Program addresses may not be writable.`
268
+ );
269
+ }
270
+ if (entry.role !== nextRole) {
271
+ return {
272
+ ...entry,
273
+ role: nextRole
274
+ };
275
+ } else {
276
+ return entry;
277
+ }
278
+ } else if ("lookupTableAddress" in accountMeta && // Static accounts can be 'upgraded' to lookup table accounts as
279
+ // long as they are not require to sign the transaction.
280
+ !isSignerRole(entry.role)) {
281
+ return {
282
+ ...accountMeta,
283
+ [TYPE]: 1 /* LOOKUP_TABLE */,
284
+ role: nextRole
285
+ };
286
+ } else {
287
+ if (entry.role !== nextRole) {
288
+ return {
289
+ ...entry,
290
+ role: nextRole
291
+ };
292
+ } else {
293
+ return entry;
294
+ }
295
+ }
296
+ }
297
+ }
298
+ }
299
+ if ("lookupTableAddress" in accountMeta) {
300
+ return {
301
+ ...accountMeta,
302
+ [TYPE]: 1 /* LOOKUP_TABLE */
303
+ };
304
+ } else {
305
+ return {
306
+ ...accountMeta,
307
+ [TYPE]: 2 /* STATIC */
308
+ };
309
+ }
310
+ });
311
+ }
312
+ }
313
+ return addressMap;
314
+ }
315
+ function getOrderedAccountsFromAddressMap(addressMap) {
316
+ let addressComparator;
317
+ const orderedAccounts = Object.entries(addressMap).sort(([leftAddress, leftEntry], [rightAddress, rightEntry]) => {
318
+ if (leftEntry[TYPE] !== rightEntry[TYPE]) {
319
+ if (leftEntry[TYPE] === 0 /* FEE_PAYER */) {
320
+ return -1;
321
+ } else if (rightEntry[TYPE] === 0 /* FEE_PAYER */) {
322
+ return 1;
323
+ } else if (leftEntry[TYPE] === 2 /* STATIC */) {
324
+ return -1;
325
+ } else if (rightEntry[TYPE] === 2 /* STATIC */) {
326
+ return 1;
327
+ }
328
+ }
329
+ const leftIsSigner = isSignerRole(leftEntry.role);
330
+ if (leftIsSigner !== isSignerRole(rightEntry.role)) {
331
+ return leftIsSigner ? -1 : 1;
332
+ }
333
+ const leftIsWritable = isWritableRole(leftEntry.role);
334
+ if (leftIsWritable !== isWritableRole(rightEntry.role)) {
335
+ return leftIsWritable ? -1 : 1;
336
+ }
337
+ addressComparator || (addressComparator = getBase58EncodedAddressComparator());
338
+ if (leftEntry[TYPE] === 1 /* LOOKUP_TABLE */ && rightEntry[TYPE] === 1 /* LOOKUP_TABLE */ && leftEntry.lookupTableAddress !== rightEntry.lookupTableAddress) {
339
+ return addressComparator(leftEntry.lookupTableAddress, rightEntry.lookupTableAddress);
340
+ } else {
341
+ return addressComparator(leftAddress, rightAddress);
342
+ }
343
+ }).map(([address, addressMeta]) => ({
344
+ address,
345
+ ...addressMeta
346
+ }));
347
+ return orderedAccounts;
348
+ }
349
+ function getCompiledAddressTableLookups(orderedAccounts) {
350
+ var _a;
351
+ const index = {};
352
+ for (const account of orderedAccounts) {
353
+ if (!("lookupTableAddress" in account)) {
354
+ continue;
355
+ }
356
+ const entry = index[_a = account.lookupTableAddress] || (index[_a] = {
357
+ readableIndices: [],
358
+ writableIndices: []
359
+ });
360
+ if (account.role === AccountRole.WRITABLE) {
361
+ entry.writableIndices.push(account.addressIndex);
362
+ } else {
363
+ entry.readableIndices.push(account.addressIndex);
364
+ }
365
+ }
366
+ return Object.keys(index).sort(getBase58EncodedAddressComparator()).map((lookupTableAddress) => ({
367
+ lookupTableAddress,
368
+ ...index[lookupTableAddress]
369
+ }));
370
+ }
371
+
372
+ // src/compile-header.ts
373
+ function getCompiledMessageHeader(orderedAccounts) {
374
+ let numReadonlyNonSignerAccounts = 0;
375
+ let numReadonlySignerAccounts = 0;
376
+ let numSignerAccounts = 0;
377
+ for (const account of orderedAccounts) {
378
+ if ("lookupTableAddress" in account) {
379
+ break;
380
+ }
381
+ const accountIsWritable = isWritableRole(account.role);
382
+ if (isSignerRole(account.role)) {
383
+ numSignerAccounts++;
384
+ if (!accountIsWritable) {
385
+ numReadonlySignerAccounts++;
386
+ }
387
+ } else if (!accountIsWritable) {
388
+ numReadonlyNonSignerAccounts++;
389
+ }
390
+ }
391
+ return {
392
+ numReadonlyNonSignerAccounts,
393
+ numReadonlySignerAccounts,
394
+ numSignerAccounts
395
+ };
396
+ }
397
+
398
+ // src/compile-instructions.ts
399
+ function getAccountIndex(orderedAccounts) {
400
+ const out = {};
401
+ for (const [index, account] of orderedAccounts.entries()) {
402
+ out[account.address] = index;
403
+ }
404
+ return out;
405
+ }
406
+ function getCompiledInstructions(instructions, orderedAccounts) {
407
+ const accountIndex = getAccountIndex(orderedAccounts);
408
+ return instructions.map(({ accounts, data, programAddress }) => {
409
+ return {
410
+ programAddressIndex: accountIndex[programAddress],
411
+ ...accounts ? { accountIndices: accounts.map(({ address }) => accountIndex[address]) } : null,
412
+ ...data ? { data } : null
413
+ };
414
+ });
415
+ }
416
+
417
+ // src/compile-lifetime-token.ts
418
+ function getCompiledLifetimeToken(lifetimeConstraint) {
419
+ if ("nonce" in lifetimeConstraint) {
420
+ return lifetimeConstraint.nonce;
421
+ }
422
+ return lifetimeConstraint.blockhash;
423
+ }
424
+
425
+ // src/compile-static-accounts.ts
426
+ function getCompiledStaticAccounts(orderedAccounts) {
427
+ const firstLookupTableAccountIndex = orderedAccounts.findIndex((account) => "lookupTableAddress" in account);
428
+ const orderedStaticAccounts = firstLookupTableAccountIndex === -1 ? orderedAccounts : orderedAccounts.slice(0, firstLookupTableAccountIndex);
429
+ return orderedStaticAccounts.map(({ address }) => address);
430
+ }
431
+
432
+ // src/message.ts
433
+ function compileMessage(transaction) {
434
+ const addressMap = getAddressMapFromInstructions(transaction.feePayer, transaction.instructions);
435
+ const orderedAccounts = getOrderedAccountsFromAddressMap(addressMap);
436
+ return {
437
+ ...transaction.version !== "legacy" ? { addressTableLookups: getCompiledAddressTableLookups(orderedAccounts) } : null,
438
+ header: getCompiledMessageHeader(orderedAccounts),
439
+ instructions: getCompiledInstructions(transaction.instructions, orderedAccounts),
440
+ lifetimeToken: getCompiledLifetimeToken(transaction.lifetimeConstraint),
441
+ staticAccounts: getCompiledStaticAccounts(orderedAccounts),
442
+ version: transaction.version
443
+ };
444
+ }
445
+ function getAddressTableLookupCodec() {
446
+ return struct(
447
+ [
448
+ [
449
+ "lookupTableAddress",
450
+ getBase58EncodedAddressCodec(
451
+ __DEV__ ? {
452
+ description: "The address of the address lookup table account from which instruction addresses should be looked up"
453
+ } : void 0
454
+ )
455
+ ],
456
+ [
457
+ "writableIndices",
458
+ array(u8(), {
459
+ ...__DEV__ ? {
460
+ description: "The indices of the accounts in the lookup table that should be loaded as writeable"
461
+ } : null,
462
+ size: shortU16()
463
+ })
464
+ ],
465
+ [
466
+ "readableIndices",
467
+ array(u8(), {
468
+ ...__DEV__ ? {
469
+ description: "The indices of the accounts in the lookup table that should be loaded as read-only"
470
+ } : void 0,
471
+ size: shortU16()
472
+ })
473
+ ]
474
+ ],
475
+ __DEV__ ? {
476
+ description: "A pointer to the address of an address lookup table, along with the readonly/writeable indices of the addresses that should be loaded from it"
477
+ } : void 0
478
+ );
479
+ }
480
+ function getMessageHeaderCodec() {
481
+ return struct(
482
+ [
483
+ [
484
+ "numSignerAccounts",
485
+ u8(
486
+ __DEV__ ? {
487
+ description: "The expected number of addresses in the static address list belonging to accounts that are required to sign this transaction"
488
+ } : void 0
489
+ )
490
+ ],
491
+ [
492
+ "numReadonlySignerAccounts",
493
+ u8(
494
+ __DEV__ ? {
495
+ description: "The expected number of addresses in the static address list belonging to accounts that are required to sign this transaction, but may not be writable"
496
+ } : void 0
497
+ )
498
+ ],
499
+ [
500
+ "numReadonlyNonSignerAccounts",
501
+ u8(
502
+ __DEV__ ? {
503
+ description: "The expected number of addresses in the static address list belonging to accounts that are neither signers, nor writable"
504
+ } : void 0
505
+ )
506
+ ]
507
+ ],
508
+ __DEV__ ? {
509
+ description: "The transaction message header containing counts of the signer, readonly-signer, and readonly-nonsigner account addresses"
510
+ } : void 0
511
+ );
512
+ }
513
+ function getInstructionCodec() {
514
+ return mapSerializer(
515
+ struct([
516
+ [
517
+ "programAddressIndex",
518
+ u8(
519
+ __DEV__ ? {
520
+ description: "The index of the program being called, according to the well-ordered accounts list for this transaction"
521
+ } : void 0
522
+ )
523
+ ],
524
+ [
525
+ "accountIndices",
526
+ array(
527
+ u8({
528
+ description: __DEV__ ? "The index of an account, according to the well-ordered accounts list for this transaction" : ""
529
+ }),
530
+ {
531
+ description: __DEV__ ? "An optional list of account indices, according to the well-ordered accounts list for this transaction, in the order in which the program being called expects them" : "",
532
+ size: shortU16()
533
+ }
534
+ )
535
+ ],
536
+ [
537
+ "data",
538
+ bytes({
539
+ description: __DEV__ ? "An optional buffer of data passed to the instruction" : "",
540
+ size: shortU16()
541
+ })
542
+ ]
543
+ ]),
544
+ (value) => {
545
+ if (value.accountIndices !== void 0 && value.data !== void 0) {
546
+ return value;
547
+ }
548
+ return {
549
+ ...value,
550
+ accountIndices: value.accountIndices ?? [],
551
+ data: value.data ?? new Uint8Array(0)
552
+ };
553
+ },
554
+ (value) => {
555
+ if (value.accountIndices.length && value.data.byteLength) {
556
+ return value;
557
+ }
558
+ const { accountIndices, data, ...rest } = value;
559
+ return {
560
+ ...rest,
561
+ ...accountIndices.length ? { accountIndices } : null,
562
+ ...data.byteLength ? { data } : null
563
+ };
564
+ }
565
+ );
566
+ }
567
+
568
+ // src/serializers/unimplemented.ts
569
+ function getError(type, name) {
570
+ const functionSuffix = name + type[0].toUpperCase() + type.slice(1);
571
+ return new Error(
572
+ `No ${type} exists for ${name}. Use \`get${functionSuffix}()\` if you need a ${type}, and \`get${name}Codec()\` if you need to both encode and decode ${name}`
573
+ );
574
+ }
575
+ function getUnimplementedDecoder(name) {
576
+ return () => {
577
+ throw getError("decoder", name);
578
+ };
579
+ }
580
+
581
+ // src/serializers/transaction-version.ts
582
+ var VERSION_FLAG_MASK = 128;
583
+ var BASE_CONFIG = {
584
+ description: __DEV__ ? "A single byte that encodes the version of the transaction" : "",
585
+ fixedSize: null,
586
+ maxSize: 1
587
+ };
588
+ function deserialize(bytes3, offset = 0) {
589
+ const firstByte = bytes3[offset];
590
+ if ((firstByte & VERSION_FLAG_MASK) === 0) {
591
+ return ["legacy", offset];
592
+ } else {
593
+ const version = firstByte ^ VERSION_FLAG_MASK;
594
+ return [version, offset + 1];
595
+ }
596
+ }
597
+ function serialize(value) {
598
+ if (value === "legacy") {
599
+ return new Uint8Array();
600
+ }
601
+ if (value < 0 || value > 127) {
602
+ throw new Error(`Transaction version must be in the range [0, 127]. \`${value}\` given.`);
603
+ }
604
+ return new Uint8Array([value | VERSION_FLAG_MASK]);
605
+ }
606
+ function getTransactionVersionCodec() {
607
+ return {
608
+ ...BASE_CONFIG,
609
+ deserialize,
610
+ serialize
611
+ };
612
+ }
613
+
614
+ // src/serializers/message.ts
615
+ var BASE_CONFIG2 = {
616
+ description: __DEV__ ? "The wire format of a Solana transaction message" : "",
617
+ fixedSize: null,
618
+ maxSize: null
619
+ };
620
+ function serialize2(compiledMessage) {
621
+ if (compiledMessage.version === "legacy") {
622
+ return struct(getPreludeStructSerializerTuple()).serialize(compiledMessage);
623
+ } else {
624
+ return mapSerializer(
625
+ struct([
626
+ ...getPreludeStructSerializerTuple(),
627
+ ["addressTableLookups", getAddressTableLookupsSerializer()]
628
+ ]),
629
+ (value) => {
630
+ if (value.version === "legacy") {
631
+ return value;
632
+ }
633
+ return {
634
+ ...value,
635
+ addressTableLookups: value.addressTableLookups ?? []
636
+ };
637
+ }
638
+ ).serialize(compiledMessage);
639
+ }
640
+ }
641
+ function getPreludeStructSerializerTuple() {
642
+ return [
643
+ ["version", getTransactionVersionCodec()],
644
+ ["header", getMessageHeaderCodec()],
645
+ [
646
+ "staticAccounts",
647
+ array(getBase58EncodedAddressCodec(), {
648
+ description: __DEV__ ? "A compact-array of static account addresses belonging to this transaction" : "",
649
+ size: shortU16()
650
+ })
651
+ ],
652
+ [
653
+ "lifetimeToken",
654
+ string({
655
+ description: __DEV__ ? "A 32-byte token that specifies the lifetime of this transaction (eg. a recent blockhash, or a durable nonce)" : "",
656
+ encoding: base58,
657
+ size: 32
658
+ })
659
+ ],
660
+ [
661
+ "instructions",
662
+ array(getInstructionCodec(), {
663
+ description: __DEV__ ? "A compact-array of instructions belonging to this transaction" : "",
664
+ size: shortU16()
665
+ })
666
+ ]
667
+ ];
668
+ }
669
+ function getAddressTableLookupsSerializer() {
670
+ return array(getAddressTableLookupCodec(), {
671
+ ...__DEV__ ? { description: "A compact array of address table lookups belonging to this transaction" } : null,
672
+ size: shortU16()
673
+ });
674
+ }
675
+ function getCompiledMessageEncoder() {
676
+ return {
677
+ ...BASE_CONFIG2,
678
+ deserialize: getUnimplementedDecoder("CompiledMessage"),
679
+ serialize: serialize2
680
+ };
681
+ }
682
+
683
+ // src/signatures.ts
684
+ async function getCompiledMessageSignature(message, secretKey) {
685
+ const wireMessageBytes = getCompiledMessageEncoder().serialize(message);
686
+ const signature = await signBytes(secretKey, wireMessageBytes);
687
+ return signature;
688
+ }
689
+ async function signTransaction(keyPair, transaction) {
690
+ const compiledMessage = compileMessage(transaction);
691
+ const [signerPublicKey, signature] = await Promise.all([
692
+ getBase58EncodedAddressFromPublicKey(keyPair.publicKey),
693
+ getCompiledMessageSignature(compiledMessage, keyPair.privateKey)
694
+ ]);
695
+ const nextSignatures = {
696
+ ..."signatures" in transaction ? transaction.signatures : null,
697
+ ...{ [signerPublicKey]: signature }
698
+ };
699
+ const out = {
700
+ ...transaction,
701
+ signatures: nextSignatures
702
+ };
703
+ Object.freeze(out);
704
+ return out;
705
+ }
706
+
707
+ // src/compile-transaction.ts
708
+ function getCompiledTransaction(transaction) {
709
+ const compiledMessage = compileMessage(transaction);
710
+ let signatures;
711
+ if ("signatures" in transaction) {
712
+ signatures = [];
713
+ for (let ii = 0; ii < compiledMessage.header.numSignerAccounts; ii++) {
714
+ signatures[ii] = transaction.signatures[compiledMessage.staticAccounts[ii]] ?? new Uint8Array(Array(64).fill(0));
715
+ }
716
+ } else {
717
+ signatures = Array(compiledMessage.header.numSignerAccounts).fill(new Uint8Array(Array(64).fill(0)));
718
+ }
719
+ return {
720
+ compiledMessage,
721
+ signatures
722
+ };
723
+ }
724
+
725
+ // src/serializers/transaction.ts
726
+ var BASE_CONFIG3 = {
727
+ description: __DEV__ ? "The wire format of a Solana transaction" : "",
728
+ fixedSize: null,
729
+ maxSize: null
730
+ };
731
+ function serialize3(transaction) {
732
+ const compiledTransaction = getCompiledTransaction(transaction);
733
+ return struct([
734
+ [
735
+ "signatures",
736
+ array(bytes({ size: 64 }), {
737
+ ...__DEV__ ? { description: "A compact array of 64-byte, base-64 encoded Ed25519 signatures" } : null,
738
+ size: shortU16()
739
+ })
740
+ ],
741
+ ["compiledMessage", getCompiledMessageEncoder()]
742
+ ]).serialize(compiledTransaction);
743
+ }
744
+ function getTransactionEncoder() {
745
+ return {
746
+ ...BASE_CONFIG3,
747
+ deserialize: getUnimplementedDecoder("CompiledMessage"),
748
+ serialize: serialize3
749
+ };
750
+ }
751
+
752
+ // src/wire-transaction.ts
753
+ function getBase64EncodedWireTransaction(transaction) {
754
+ const wireTransactionBytes = getTransactionEncoder().serialize(transaction);
755
+ {
756
+ return Buffer.from(wireTransactionBytes).toString("base64");
757
+ }
758
+ }
759
+
760
+ export { appendTransactionInstruction, assertIsBlockhash, assertIsDurableNonceTransaction, createTransaction, getBase64EncodedWireTransaction, prependTransactionInstruction, setTransactionFeePayer, setTransactionLifetimeUsingBlockhash, setTransactionLifetimeUsingDurableNonce, signTransaction };
761
+ //# sourceMappingURL=out.js.map
762
+ //# sourceMappingURL=index.node.js.map