@solana/transactions 2.0.0-experimental.ca5fcbd → 2.0.0-experimental.cc545f9

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