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