@solana/transactions 2.0.0-experimental.f07dced → 2.0.0-experimental.f27be12

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 (53) hide show
  1. package/dist/index.browser.cjs +229 -113
  2. package/dist/index.browser.cjs.map +1 -1
  3. package/dist/index.browser.js +223 -115
  4. package/dist/index.browser.js.map +1 -1
  5. package/dist/index.development.js +282 -163
  6. package/dist/index.development.js.map +1 -1
  7. package/dist/index.native.js +225 -115
  8. package/dist/index.native.js.map +1 -1
  9. package/dist/index.node.cjs +229 -113
  10. package/dist/index.node.cjs.map +1 -1
  11. package/dist/index.node.js +223 -115
  12. package/dist/index.node.js.map +1 -1
  13. package/dist/index.production.min.js +16 -8
  14. package/dist/types/accounts.d.ts +2 -2
  15. package/dist/types/accounts.d.ts.map +1 -0
  16. package/dist/types/blockhash.d.ts +6 -1
  17. package/dist/types/blockhash.d.ts.map +1 -0
  18. package/dist/types/compile-address-table-lookups.d.ts +1 -1
  19. package/dist/types/compile-address-table-lookups.d.ts.map +1 -0
  20. package/dist/types/compile-header.d.ts.map +1 -0
  21. package/dist/types/compile-instructions.d.ts +1 -1
  22. package/dist/types/compile-instructions.d.ts.map +1 -0
  23. package/dist/types/compile-lifetime-token.d.ts.map +1 -0
  24. package/dist/types/compile-static-accounts.d.ts +1 -1
  25. package/dist/types/compile-static-accounts.d.ts.map +1 -0
  26. package/dist/types/compile-transaction.d.ts.map +1 -0
  27. package/dist/types/create-transaction.d.ts.map +1 -0
  28. package/dist/types/durable-nonce.d.ts +20 -7
  29. package/dist/types/durable-nonce.d.ts.map +1 -0
  30. package/dist/types/fee-payer.d.ts +3 -2
  31. package/dist/types/fee-payer.d.ts.map +1 -0
  32. package/dist/types/index.d.ts +1 -0
  33. package/dist/types/index.d.ts.map +1 -0
  34. package/dist/types/instructions.d.ts.map +1 -0
  35. package/dist/types/message.d.ts.map +1 -0
  36. package/dist/types/serializers/address-table-lookup.d.ts.map +1 -0
  37. package/dist/types/serializers/header.d.ts.map +1 -0
  38. package/dist/types/serializers/index.d.ts +2 -0
  39. package/dist/types/serializers/index.d.ts.map +1 -0
  40. package/dist/types/serializers/instruction.d.ts.map +1 -0
  41. package/dist/types/serializers/message.d.ts.map +1 -0
  42. package/dist/types/serializers/transaction-version.d.ts.map +1 -0
  43. package/dist/types/serializers/transaction.d.ts.map +1 -0
  44. package/dist/types/serializers/unimplemented.d.ts.map +1 -0
  45. package/dist/types/signatures.d.ts +13 -6
  46. package/dist/types/signatures.d.ts.map +1 -0
  47. package/dist/types/types.d.ts +7 -0
  48. package/dist/types/types.d.ts.map +1 -0
  49. package/dist/types/unsigned-transaction.d.ts +4 -0
  50. package/dist/types/unsigned-transaction.d.ts.map +1 -0
  51. package/dist/types/wire-transaction.d.ts +1 -1
  52. package/dist/types/wire-transaction.d.ts.map +1 -0
  53. package/package.json +18 -18
@@ -1,10 +1,27 @@
1
1
  'use strict';
2
2
 
3
3
  var umiSerializers = require('@metaplex-foundation/umi-serializers');
4
+ var addresses = require('@solana/addresses');
4
5
  var keys = require('@solana/keys');
5
6
 
6
7
  // ../build-scripts/env-shim.ts
7
8
  var __DEV__ = /* @__PURE__ */ (() => process["env"].NODE_ENV === "development")();
9
+
10
+ // src/unsigned-transaction.ts
11
+ function getUnsignedTransaction(transaction) {
12
+ if ("signatures" in transaction) {
13
+ const {
14
+ signatures: _,
15
+ // eslint-disable-line @typescript-eslint/no-unused-vars
16
+ ...unsignedTransaction
17
+ } = transaction;
18
+ return unsignedTransaction;
19
+ } else {
20
+ return transaction;
21
+ }
22
+ }
23
+
24
+ // src/blockhash.ts
8
25
  function assertIsBlockhash(putativeBlockhash) {
9
26
  try {
10
27
  if (
@@ -25,6 +42,17 @@ function assertIsBlockhash(putativeBlockhash) {
25
42
  });
26
43
  }
27
44
  }
45
+ function setTransactionLifetimeUsingBlockhash(blockhashLifetimeConstraint, transaction) {
46
+ if ("lifetimeConstraint" in transaction && transaction.lifetimeConstraint.blockhash === blockhashLifetimeConstraint.blockhash && transaction.lifetimeConstraint.lastValidBlockHeight === blockhashLifetimeConstraint.lastValidBlockHeight) {
47
+ return transaction;
48
+ }
49
+ const out = {
50
+ ...getUnsignedTransaction(transaction),
51
+ lifetimeConstraint: blockhashLifetimeConstraint
52
+ };
53
+ Object.freeze(out);
54
+ return out;
55
+ }
28
56
 
29
57
  // src/create-transaction.ts
30
58
  function createTransaction({
@@ -38,66 +66,6 @@ function createTransaction({
38
66
  return out;
39
67
  }
40
68
 
41
- // src/fee-payer.ts
42
- function setTransactionFeePayer(feePayer, transaction) {
43
- if ("feePayer" in transaction && feePayer === transaction.feePayer) {
44
- return transaction;
45
- }
46
- let out;
47
- if ("signatures" in transaction) {
48
- const {
49
- signatures: _,
50
- // eslint-disable-line @typescript-eslint/no-unused-vars
51
- ...unsignedTransaction
52
- } = transaction;
53
- out = {
54
- ...unsignedTransaction,
55
- feePayer
56
- };
57
- } else {
58
- out = {
59
- ...transaction,
60
- feePayer
61
- };
62
- }
63
- Object.freeze(out);
64
- return out;
65
- }
66
-
67
- // src/instructions.ts
68
- function replaceInstructions(transaction, nextInstructions) {
69
- let out;
70
- if ("signatures" in transaction) {
71
- const {
72
- signatures: _,
73
- // eslint-disable-line @typescript-eslint/no-unused-vars
74
- ...unsignedTransaction
75
- } = transaction;
76
- out = {
77
- ...unsignedTransaction,
78
- instructions: nextInstructions
79
- };
80
- } else {
81
- out = {
82
- ...transaction,
83
- instructions: nextInstructions
84
- };
85
- }
86
- return out;
87
- }
88
- function appendTransactionInstruction(instruction, transaction) {
89
- const nextInstructions = [...transaction.instructions, instruction];
90
- const out = replaceInstructions(transaction, nextInstructions);
91
- Object.freeze(out);
92
- return out;
93
- }
94
- function prependTransactionInstruction(instruction, transaction) {
95
- const nextInstructions = [instruction, ...transaction.instructions];
96
- const out = replaceInstructions(transaction, nextInstructions);
97
- Object.freeze(out);
98
- return out;
99
- }
100
-
101
69
  // ../instructions/dist/index.browser.js
102
70
  var AccountRole = /* @__PURE__ */ ((AccountRole2) => {
103
71
  AccountRole2[AccountRole2["WRITABLE_SIGNER"] = /* 3 */
@@ -120,6 +88,96 @@ function isWritableRole(role) {
120
88
  function mergeRoles(roleA, roleB) {
121
89
  return roleA | roleB;
122
90
  }
91
+
92
+ // src/durable-nonce.ts
93
+ var RECENT_BLOCKHASHES_SYSVAR_ADDRESS = "SysvarRecentB1ockHashes11111111111111111111";
94
+ var SYSTEM_PROGRAM_ADDRESS = "11111111111111111111111111111111";
95
+ function assertIsDurableNonceTransaction(transaction) {
96
+ if (!isDurableNonceTransaction(transaction)) {
97
+ throw new Error("Transaction is not a durable nonce transaction");
98
+ }
99
+ }
100
+ function createAdvanceNonceAccountInstruction(nonceAccountAddress, nonceAuthorityAddress) {
101
+ return {
102
+ accounts: [
103
+ { address: nonceAccountAddress, role: AccountRole.WRITABLE },
104
+ {
105
+ address: RECENT_BLOCKHASHES_SYSVAR_ADDRESS,
106
+ role: AccountRole.READONLY
107
+ },
108
+ { address: nonceAuthorityAddress, role: AccountRole.READONLY_SIGNER }
109
+ ],
110
+ data: new Uint8Array([4, 0, 0, 0]),
111
+ programAddress: SYSTEM_PROGRAM_ADDRESS
112
+ };
113
+ }
114
+ function isAdvanceNonceAccountInstruction(instruction) {
115
+ return instruction.programAddress === SYSTEM_PROGRAM_ADDRESS && // Test for `AdvanceNonceAccount` instruction data
116
+ instruction.data != null && isAdvanceNonceAccountInstructionData(instruction.data) && // Test for exactly 3 accounts
117
+ instruction.accounts?.length === 3 && // First account is nonce account address
118
+ instruction.accounts[0].address != null && instruction.accounts[0].role === AccountRole.WRITABLE && // Second account is recent blockhashes sysvar
119
+ instruction.accounts[1].address === RECENT_BLOCKHASHES_SYSVAR_ADDRESS && instruction.accounts[1].role === AccountRole.READONLY && // Third account is nonce authority account
120
+ instruction.accounts[2].address != null && instruction.accounts[2].role === AccountRole.READONLY_SIGNER;
121
+ }
122
+ function isAdvanceNonceAccountInstructionData(data) {
123
+ return data.byteLength === 4 && data[0] === 4 && data[1] === 0 && data[2] === 0 && data[3] === 0;
124
+ }
125
+ function isDurableNonceTransaction(transaction) {
126
+ return "lifetimeConstraint" in transaction && typeof transaction.lifetimeConstraint.nonce === "string" && transaction.instructions[0] != null && isAdvanceNonceAccountInstruction(transaction.instructions[0]);
127
+ }
128
+ function setTransactionLifetimeUsingDurableNonce({
129
+ nonce,
130
+ nonceAccountAddress,
131
+ nonceAuthorityAddress
132
+ }, transaction) {
133
+ const isAlreadyDurableNonceTransaction = isDurableNonceTransaction(transaction);
134
+ if (isAlreadyDurableNonceTransaction && transaction.lifetimeConstraint.nonce === nonce && transaction.instructions[0].accounts[0].address === nonceAccountAddress && transaction.instructions[0].accounts[2].address === nonceAuthorityAddress) {
135
+ return transaction;
136
+ }
137
+ const out = {
138
+ ...getUnsignedTransaction(transaction),
139
+ instructions: [
140
+ createAdvanceNonceAccountInstruction(nonceAccountAddress, nonceAuthorityAddress),
141
+ ...isAlreadyDurableNonceTransaction ? transaction.instructions.slice(1) : transaction.instructions
142
+ ],
143
+ lifetimeConstraint: {
144
+ nonce
145
+ }
146
+ };
147
+ Object.freeze(out);
148
+ return out;
149
+ }
150
+
151
+ // src/fee-payer.ts
152
+ function setTransactionFeePayer(feePayer, transaction) {
153
+ if ("feePayer" in transaction && feePayer === transaction.feePayer) {
154
+ return transaction;
155
+ }
156
+ const out = {
157
+ ...getUnsignedTransaction(transaction),
158
+ feePayer
159
+ };
160
+ Object.freeze(out);
161
+ return out;
162
+ }
163
+
164
+ // src/instructions.ts
165
+ function appendTransactionInstruction(instruction, transaction) {
166
+ const out = {
167
+ ...getUnsignedTransaction(transaction),
168
+ instructions: [...transaction.instructions, instruction]
169
+ };
170
+ Object.freeze(out);
171
+ return out;
172
+ }
173
+ function prependTransactionInstruction(instruction, transaction) {
174
+ const out = {
175
+ ...getUnsignedTransaction(transaction),
176
+ instructions: [instruction, ...transaction.instructions]
177
+ };
178
+ Object.freeze(out);
179
+ return out;
180
+ }
123
181
  function upsert(addressMap, address, update) {
124
182
  addressMap[address] = update(addressMap[address] ?? { role: AccountRole.READONLY });
125
183
  }
@@ -172,7 +230,7 @@ function getAddressMapFromInstructions(feePayer, instructions) {
172
230
  const shouldReplaceEntry = (
173
231
  // Consider using the new LOOKUP_TABLE if its address is different...
174
232
  entry.lookupTableAddress !== accountMeta.lookupTableAddress && // ...and sorts before the existing one.
175
- (addressComparator || (addressComparator = keys.getBase58EncodedAddressComparator()))(
233
+ (addressComparator || (addressComparator = addresses.getAddressComparator()))(
176
234
  accountMeta.lookupTableAddress,
177
235
  entry.lookupTableAddress
178
236
  ) < 0
@@ -278,7 +336,7 @@ function getOrderedAccountsFromAddressMap(addressMap) {
278
336
  if (leftIsWritable !== isWritableRole(rightEntry.role)) {
279
337
  return leftIsWritable ? -1 : 1;
280
338
  }
281
- addressComparator || (addressComparator = keys.getBase58EncodedAddressComparator());
339
+ addressComparator || (addressComparator = addresses.getAddressComparator());
282
340
  if (leftEntry[TYPE] === 1 /* LOOKUP_TABLE */ && rightEntry[TYPE] === 1 /* LOOKUP_TABLE */ && leftEntry.lookupTableAddress !== rightEntry.lookupTableAddress) {
283
341
  return addressComparator(leftEntry.lookupTableAddress, rightEntry.lookupTableAddress);
284
342
  } else {
@@ -307,7 +365,7 @@ function getCompiledAddressTableLookups(orderedAccounts) {
307
365
  entry.readableIndices.push(account.addressIndex);
308
366
  }
309
367
  }
310
- return Object.keys(index).sort(keys.getBase58EncodedAddressComparator()).map((lookupTableAddress) => ({
368
+ return Object.keys(index).sort(addresses.getAddressComparator()).map((lookupTableAddress) => ({
311
369
  lookupTableAddress,
312
370
  ...index[lookupTableAddress]
313
371
  }));
@@ -386,12 +444,30 @@ function compileMessage(transaction) {
386
444
  version: transaction.version
387
445
  };
388
446
  }
447
+
448
+ // src/compile-transaction.ts
449
+ function getCompiledTransaction(transaction) {
450
+ const compiledMessage = compileMessage(transaction);
451
+ let signatures;
452
+ if ("signatures" in transaction) {
453
+ signatures = [];
454
+ for (let ii = 0; ii < compiledMessage.header.numSignerAccounts; ii++) {
455
+ signatures[ii] = transaction.signatures[compiledMessage.staticAccounts[ii]] ?? new Uint8Array(Array(64).fill(0));
456
+ }
457
+ } else {
458
+ signatures = Array(compiledMessage.header.numSignerAccounts).fill(new Uint8Array(Array(64).fill(0)));
459
+ }
460
+ return {
461
+ compiledMessage,
462
+ signatures
463
+ };
464
+ }
389
465
  function getAddressTableLookupCodec() {
390
466
  return umiSerializers.struct(
391
467
  [
392
468
  [
393
469
  "lookupTableAddress",
394
- keys.getBase58EncodedAddressCodec(
470
+ addresses.getAddressCodec(
395
471
  __DEV__ ? {
396
472
  description: "The address of the address lookup table account from which instruction addresses should be looked up"
397
473
  } : void 0
@@ -466,7 +542,7 @@ function getInstructionCodec() {
466
542
  )
467
543
  ],
468
544
  [
469
- "addressIndices",
545
+ "accountIndices",
470
546
  umiSerializers.array(
471
547
  umiSerializers.u8({
472
548
  description: __DEV__ ? "The index of an account, according to the well-ordered accounts list for this transaction" : ""
@@ -486,23 +562,23 @@ function getInstructionCodec() {
486
562
  ]
487
563
  ]),
488
564
  (value) => {
489
- if (value.addressIndices !== void 0 && value.data !== void 0) {
565
+ if (value.accountIndices !== void 0 && value.data !== void 0) {
490
566
  return value;
491
567
  }
492
568
  return {
493
569
  ...value,
494
- addressIndices: value.addressIndices ?? [],
570
+ accountIndices: value.accountIndices ?? [],
495
571
  data: value.data ?? new Uint8Array(0)
496
572
  };
497
573
  },
498
574
  (value) => {
499
- if (value.addressIndices.length && value.data.byteLength) {
575
+ if (value.accountIndices.length && value.data.byteLength) {
500
576
  return value;
501
577
  }
502
- const { addressIndices, data, ...rest } = value;
578
+ const { accountIndices, data, ...rest } = value;
503
579
  return {
504
580
  ...rest,
505
- ...addressIndices.length ? { addressIndices } : null,
581
+ ...accountIndices.length ? { accountIndices } : null,
506
582
  ...data.byteLength ? { data } : null
507
583
  };
508
584
  }
@@ -588,7 +664,7 @@ function getPreludeStructSerializerTuple() {
588
664
  ["header", getMessageHeaderCodec()],
589
665
  [
590
666
  "staticAccounts",
591
- umiSerializers.array(keys.getBase58EncodedAddressCodec(), {
667
+ umiSerializers.array(addresses.getAddressCodec(), {
592
668
  description: __DEV__ ? "A compact-array of static account addresses belonging to this transaction" : "",
593
669
  size: umiSerializers.shortU16()
594
670
  })
@@ -624,48 +700,6 @@ function getCompiledMessageEncoder() {
624
700
  };
625
701
  }
626
702
 
627
- // src/signatures.ts
628
- async function getCompiledMessageSignature(message, secretKey) {
629
- const wireMessageBytes = getCompiledMessageEncoder().serialize(message);
630
- const signature = await keys.signBytes(secretKey, wireMessageBytes);
631
- return signature;
632
- }
633
- async function signTransaction(keyPair, transaction) {
634
- const compiledMessage = compileMessage(transaction);
635
- const [signerPublicKey, signature] = await Promise.all([
636
- keys.getBase58EncodedAddressFromPublicKey(keyPair.publicKey),
637
- getCompiledMessageSignature(compiledMessage, keyPair.privateKey)
638
- ]);
639
- const nextSignatures = {
640
- ..."signatures" in transaction ? transaction.signatures : null,
641
- ...{ [signerPublicKey]: signature }
642
- };
643
- const out = {
644
- ...transaction,
645
- signatures: nextSignatures
646
- };
647
- Object.freeze(out);
648
- return out;
649
- }
650
-
651
- // src/compile-transaction.ts
652
- function getCompiledTransaction(transaction) {
653
- const compiledMessage = compileMessage(transaction);
654
- let signatures;
655
- if ("signatures" in transaction) {
656
- signatures = [];
657
- for (let ii = 0; ii < compiledMessage.header.numSignerAccounts; ii++) {
658
- signatures[ii] = transaction.signatures[compiledMessage.staticAccounts[ii]] ?? new Uint8Array(Array(64).fill(0));
659
- }
660
- } else {
661
- signatures = Array(compiledMessage.header.numSignerAccounts).fill(new Uint8Array(Array(64).fill(0)));
662
- }
663
- return {
664
- compiledMessage,
665
- signatures
666
- };
667
- }
668
-
669
703
  // src/serializers/transaction.ts
670
704
  var BASE_CONFIG3 = {
671
705
  description: __DEV__ ? "The wire format of a Solana transaction" : "",
@@ -692,6 +726,80 @@ function getTransactionEncoder() {
692
726
  serialize: serialize3
693
727
  };
694
728
  }
729
+ function assertIsTransactionSignature(putativeTransactionSignature) {
730
+ try {
731
+ if (
732
+ // Lowest value (64 bytes of zeroes)
733
+ putativeTransactionSignature.length < 64 || // Highest value (64 bytes of 255)
734
+ putativeTransactionSignature.length > 88
735
+ ) {
736
+ throw new Error("Expected input string to decode to a byte array of length 64.");
737
+ }
738
+ const bytes3 = umiSerializers.base58.serialize(putativeTransactionSignature);
739
+ const numBytes = bytes3.byteLength;
740
+ if (numBytes !== 64) {
741
+ throw new Error(`Expected input string to decode to a byte array of length 64. Actual length: ${numBytes}`);
742
+ }
743
+ } catch (e) {
744
+ throw new Error(`\`${putativeTransactionSignature}\` is not a transaction signature`, {
745
+ cause: e
746
+ });
747
+ }
748
+ }
749
+ function isTransactionSignature(putativeTransactionSignature) {
750
+ if (
751
+ // Lowest value (64 bytes of zeroes)
752
+ putativeTransactionSignature.length < 64 || // Highest value (64 bytes of 255)
753
+ putativeTransactionSignature.length > 88
754
+ ) {
755
+ return false;
756
+ }
757
+ const bytes3 = umiSerializers.base58.serialize(putativeTransactionSignature);
758
+ const numBytes = bytes3.byteLength;
759
+ if (numBytes !== 64) {
760
+ return false;
761
+ }
762
+ return true;
763
+ }
764
+ async function getCompiledMessageSignature(message, secretKey) {
765
+ const wireMessageBytes = getCompiledMessageEncoder().serialize(message);
766
+ const signature = await keys.signBytes(secretKey, wireMessageBytes);
767
+ return signature;
768
+ }
769
+ function getSignatureFromTransaction(transaction) {
770
+ const signature = transaction.signatures[transaction.feePayer];
771
+ if (!signature) {
772
+ throw new Error(
773
+ "Could not determine this transaction's signature. Make sure that the transaction has been signed by its fee payer."
774
+ );
775
+ }
776
+ return signature;
777
+ }
778
+ async function signTransaction(keyPairs, transaction) {
779
+ const compiledMessage = compileMessage(transaction);
780
+ const nextSignatures = "signatures" in transaction ? { ...transaction.signatures } : {};
781
+ const publicKeySignaturePairs = await Promise.all(
782
+ keyPairs.map(
783
+ (keyPair) => Promise.all([
784
+ addresses.getAddressFromPublicKey(keyPair.publicKey),
785
+ getCompiledMessageSignature(compiledMessage, keyPair.privateKey)
786
+ ])
787
+ )
788
+ );
789
+ for (const [signerPublicKey, signature] of publicKeySignaturePairs) {
790
+ nextSignatures[signerPublicKey] = signature;
791
+ }
792
+ const out = {
793
+ ...transaction,
794
+ signatures: nextSignatures
795
+ };
796
+ Object.freeze(out);
797
+ return out;
798
+ }
799
+ function transactionSignature(putativeTransactionSignature) {
800
+ assertIsTransactionSignature(putativeTransactionSignature);
801
+ return putativeTransactionSignature;
802
+ }
695
803
 
696
804
  // src/wire-transaction.ts
697
805
  function getBase64EncodedWireTransaction(transaction) {
@@ -703,10 +811,18 @@ function getBase64EncodedWireTransaction(transaction) {
703
811
 
704
812
  exports.appendTransactionInstruction = appendTransactionInstruction;
705
813
  exports.assertIsBlockhash = assertIsBlockhash;
814
+ exports.assertIsDurableNonceTransaction = assertIsDurableNonceTransaction;
815
+ exports.assertIsTransactionSignature = assertIsTransactionSignature;
706
816
  exports.createTransaction = createTransaction;
707
817
  exports.getBase64EncodedWireTransaction = getBase64EncodedWireTransaction;
818
+ exports.getSignatureFromTransaction = getSignatureFromTransaction;
819
+ exports.getTransactionEncoder = getTransactionEncoder;
820
+ exports.isTransactionSignature = isTransactionSignature;
708
821
  exports.prependTransactionInstruction = prependTransactionInstruction;
709
822
  exports.setTransactionFeePayer = setTransactionFeePayer;
823
+ exports.setTransactionLifetimeUsingBlockhash = setTransactionLifetimeUsingBlockhash;
824
+ exports.setTransactionLifetimeUsingDurableNonce = setTransactionLifetimeUsingDurableNonce;
710
825
  exports.signTransaction = signTransaction;
826
+ exports.transactionSignature = transactionSignature;
711
827
  //# sourceMappingURL=out.js.map
712
828
  //# sourceMappingURL=index.browser.cjs.map