@solana/transactions 2.0.0-experimental.9bf9fdb → 2.0.0-experimental.9e133fd

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