@solana/transactions 2.0.0-experimental.6cedd3a → 2.0.0-experimental.6d18167

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