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