@solana/transactions 2.0.0-experimental.87c9652 → 2.0.0-experimental.8894dc1

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 (50) hide show
  1. package/dist/index.browser.cjs +187 -80
  2. package/dist/index.browser.cjs.map +1 -1
  3. package/dist/index.browser.js +178 -77
  4. package/dist/index.browser.js.map +1 -1
  5. package/dist/index.development.js +227 -119
  6. package/dist/index.development.js.map +1 -1
  7. package/dist/index.native.js +180 -77
  8. package/dist/index.native.js.map +1 -1
  9. package/dist/index.node.cjs +187 -80
  10. package/dist/index.node.cjs.map +1 -1
  11. package/dist/index.node.js +178 -77
  12. package/dist/index.node.js.map +1 -1
  13. package/dist/index.production.min.js +14 -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 +19 -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.map +1 -0
  33. package/dist/types/instructions.d.ts.map +1 -0
  34. package/dist/types/message.d.ts.map +1 -0
  35. package/dist/types/serializers/address-table-lookup.d.ts.map +1 -0
  36. package/dist/types/serializers/header.d.ts.map +1 -0
  37. package/dist/types/serializers/instruction.d.ts.map +1 -0
  38. package/dist/types/serializers/message.d.ts.map +1 -0
  39. package/dist/types/serializers/transaction-version.d.ts.map +1 -0
  40. package/dist/types/serializers/transaction.d.ts.map +1 -0
  41. package/dist/types/serializers/unimplemented.d.ts.map +1 -0
  42. package/dist/types/signatures.d.ts +11 -6
  43. package/dist/types/signatures.d.ts.map +1 -0
  44. package/dist/types/types.d.ts +7 -0
  45. package/dist/types/types.d.ts.map +1 -0
  46. package/dist/types/unsigned-transaction.d.ts +4 -0
  47. package/dist/types/unsigned-transaction.d.ts.map +1 -0
  48. package/dist/types/wire-transaction.d.ts +1 -1
  49. package/dist/types/wire-transaction.d.ts.map +1 -0
  50. package/package.json +16 -16
@@ -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.getBase58EncodedAddressComparator()))(
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.getBase58EncodedAddressComparator());
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.getBase58EncodedAddressComparator()).map((lookupTableAddress) => ({
311
369
  lookupTableAddress,
312
370
  ...index[lookupTableAddress]
313
371
  }));
@@ -391,7 +449,7 @@ function getAddressTableLookupCodec() {
391
449
  [
392
450
  [
393
451
  "lookupTableAddress",
394
- keys.getBase58EncodedAddressCodec(
452
+ addresses.getBase58EncodedAddressCodec(
395
453
  __DEV__ ? {
396
454
  description: "The address of the address lookup table account from which instruction addresses should be looked up"
397
455
  } : void 0
@@ -466,7 +524,7 @@ function getInstructionCodec() {
466
524
  )
467
525
  ],
468
526
  [
469
- "addressIndices",
527
+ "accountIndices",
470
528
  umiSerializers.array(
471
529
  umiSerializers.u8({
472
530
  description: __DEV__ ? "The index of an account, according to the well-ordered accounts list for this transaction" : ""
@@ -486,23 +544,23 @@ function getInstructionCodec() {
486
544
  ]
487
545
  ]),
488
546
  (value) => {
489
- if (value.addressIndices !== void 0 && value.data !== void 0) {
547
+ if (value.accountIndices !== void 0 && value.data !== void 0) {
490
548
  return value;
491
549
  }
492
550
  return {
493
551
  ...value,
494
- addressIndices: value.addressIndices ?? [],
552
+ accountIndices: value.accountIndices ?? [],
495
553
  data: value.data ?? new Uint8Array(0)
496
554
  };
497
555
  },
498
556
  (value) => {
499
- if (value.addressIndices.length && value.data.byteLength) {
557
+ if (value.accountIndices.length && value.data.byteLength) {
500
558
  return value;
501
559
  }
502
- const { addressIndices, data, ...rest } = value;
560
+ const { accountIndices, data, ...rest } = value;
503
561
  return {
504
562
  ...rest,
505
- ...addressIndices.length ? { addressIndices } : null,
563
+ ...accountIndices.length ? { accountIndices } : null,
506
564
  ...data.byteLength ? { data } : null
507
565
  };
508
566
  }
@@ -588,7 +646,7 @@ function getPreludeStructSerializerTuple() {
588
646
  ["header", getMessageHeaderCodec()],
589
647
  [
590
648
  "staticAccounts",
591
- umiSerializers.array(keys.getBase58EncodedAddressCodec(), {
649
+ umiSerializers.array(addresses.getBase58EncodedAddressCodec(), {
592
650
  description: __DEV__ ? "A compact-array of static account addresses belonging to this transaction" : "",
593
651
  size: umiSerializers.shortU16()
594
652
  })
@@ -625,21 +683,60 @@ function getCompiledMessageEncoder() {
625
683
  }
626
684
 
627
685
  // src/signatures.ts
686
+ function assertIsTransactionSignature(putativeTransactionSignature) {
687
+ try {
688
+ if (
689
+ // Lowest value (64 bytes of zeroes)
690
+ putativeTransactionSignature.length < 64 || // Highest value (64 bytes of 255)
691
+ putativeTransactionSignature.length > 88
692
+ ) {
693
+ throw new Error("Expected input string to decode to a byte array of length 64.");
694
+ }
695
+ const bytes3 = umiSerializers.base58.serialize(putativeTransactionSignature);
696
+ const numBytes = bytes3.byteLength;
697
+ if (numBytes !== 64) {
698
+ throw new Error(`Expected input string to decode to a byte array of length 64. Actual length: ${numBytes}`);
699
+ }
700
+ } catch (e) {
701
+ throw new Error(`\`${putativeTransactionSignature}\` is not a transaction signature`, {
702
+ cause: e
703
+ });
704
+ }
705
+ }
706
+ function isTransactionSignature(putativeTransactionSignature) {
707
+ if (
708
+ // Lowest value (64 bytes of zeroes)
709
+ putativeTransactionSignature.length < 64 || // Highest value (64 bytes of 255)
710
+ putativeTransactionSignature.length > 88
711
+ ) {
712
+ return false;
713
+ }
714
+ const bytes3 = umiSerializers.base58.serialize(putativeTransactionSignature);
715
+ const numBytes = bytes3.byteLength;
716
+ if (numBytes !== 64) {
717
+ return false;
718
+ }
719
+ return true;
720
+ }
628
721
  async function getCompiledMessageSignature(message, secretKey) {
629
722
  const wireMessageBytes = getCompiledMessageEncoder().serialize(message);
630
723
  const signature = await keys.signBytes(secretKey, wireMessageBytes);
631
724
  return signature;
632
725
  }
633
- async function signTransaction(keyPair, transaction) {
726
+ async function signTransaction(keyPairs, transaction) {
634
727
  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
- };
728
+ const nextSignatures = "signatures" in transaction ? { ...transaction.signatures } : {};
729
+ const publicKeySignaturePairs = await Promise.all(
730
+ keyPairs.map(
731
+ (keyPair) => Promise.all([
732
+ addresses.getAddressFromPublicKey(keyPair.publicKey),
733
+ getCompiledMessageSignature(compiledMessage, keyPair.privateKey)
734
+ ])
735
+ )
736
+ );
737
+ for (const [signerPublicKey, signature] of publicKeySignaturePairs) {
738
+ nextSignatures[signerPublicKey] = signature;
739
+ }
643
740
  const out = {
644
741
  ...transaction,
645
742
  signatures: nextSignatures
@@ -647,6 +744,10 @@ async function signTransaction(keyPair, transaction) {
647
744
  Object.freeze(out);
648
745
  return out;
649
746
  }
747
+ function transactionSignature(putativeTransactionSignature) {
748
+ assertIsTransactionSignature(putativeTransactionSignature);
749
+ return putativeTransactionSignature;
750
+ }
650
751
 
651
752
  // src/compile-transaction.ts
652
753
  function getCompiledTransaction(transaction) {
@@ -703,10 +804,16 @@ function getBase64EncodedWireTransaction(transaction) {
703
804
 
704
805
  exports.appendTransactionInstruction = appendTransactionInstruction;
705
806
  exports.assertIsBlockhash = assertIsBlockhash;
807
+ exports.assertIsDurableNonceTransaction = assertIsDurableNonceTransaction;
808
+ exports.assertIsTransactionSignature = assertIsTransactionSignature;
706
809
  exports.createTransaction = createTransaction;
707
810
  exports.getBase64EncodedWireTransaction = getBase64EncodedWireTransaction;
811
+ exports.isTransactionSignature = isTransactionSignature;
708
812
  exports.prependTransactionInstruction = prependTransactionInstruction;
709
813
  exports.setTransactionFeePayer = setTransactionFeePayer;
814
+ exports.setTransactionLifetimeUsingBlockhash = setTransactionLifetimeUsingBlockhash;
815
+ exports.setTransactionLifetimeUsingDurableNonce = setTransactionLifetimeUsingDurableNonce;
710
816
  exports.signTransaction = signTransaction;
817
+ exports.transactionSignature = transactionSignature;
711
818
  //# sourceMappingURL=out.js.map
712
819
  //# sourceMappingURL=index.browser.cjs.map