@solana/transactions 2.0.0-experimental.c29513e → 2.0.0-experimental.c5061ae

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