@solana/transactions 2.0.0-experimental.c9bd7fa → 2.0.0-experimental.ca243cb

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 (36) hide show
  1. package/dist/index.browser.cjs +176 -134
  2. package/dist/index.browser.cjs.map +1 -1
  3. package/dist/index.browser.js +177 -138
  4. package/dist/index.browser.js.map +1 -1
  5. package/dist/index.native.js +177 -138
  6. package/dist/index.native.js.map +1 -1
  7. package/dist/index.node.cjs +176 -134
  8. package/dist/index.node.cjs.map +1 -1
  9. package/dist/index.node.js +177 -138
  10. package/dist/index.node.js.map +1 -1
  11. package/dist/types/blockhash.d.ts +1 -4
  12. package/dist/types/blockhash.d.ts.map +1 -1
  13. package/dist/types/compilable-transaction.d.ts +4 -4
  14. package/dist/types/compile-address-table-lookups.d.ts +1 -1
  15. package/dist/types/compile-header.d.ts +1 -1
  16. package/dist/types/compile-instructions.d.ts +1 -1
  17. package/dist/types/compile-lifetime-token.d.ts +1 -1
  18. package/dist/types/compile-static-accounts.d.ts +1 -1
  19. package/dist/types/decompile-transaction.d.ts +9 -1
  20. package/dist/types/decompile-transaction.d.ts.map +1 -1
  21. package/dist/types/fee-payer.d.ts +2 -2
  22. package/dist/types/index.d.ts +1 -0
  23. package/dist/types/index.d.ts.map +1 -1
  24. package/dist/types/message.d.ts +6 -6
  25. package/dist/types/serializers/address-table-lookup.d.ts +1 -1
  26. package/dist/types/serializers/header.d.ts +1 -1
  27. package/dist/types/serializers/instruction.d.ts +1 -1
  28. package/dist/types/serializers/transaction-version.d.ts +1 -1
  29. package/dist/types/serializers/transaction.d.ts +5 -2
  30. package/dist/types/serializers/transaction.d.ts.map +1 -1
  31. package/dist/types/signatures.d.ts.map +1 -1
  32. package/dist/types/unsigned-transaction.d.ts +1 -1
  33. package/package.json +12 -11
  34. package/dist/index.development.js +0 -1628
  35. package/dist/index.development.js.map +0 -1
  36. package/dist/index.production.min.js +0 -32
@@ -2,29 +2,13 @@
2
2
 
3
3
  var codecsStrings = require('@solana/codecs-strings');
4
4
  var addresses = require('@solana/addresses');
5
+ var functional = require('@solana/functional');
5
6
  var codecsCore = require('@solana/codecs-core');
6
7
  var codecsDataStructures = require('@solana/codecs-data-structures');
7
8
  var codecsNumbers = require('@solana/codecs-numbers');
8
- var functional = require('@solana/functional');
9
9
  var keys = require('@solana/keys');
10
10
 
11
- // src/blockhash.ts
12
-
13
- // src/unsigned-transaction.ts
14
- function getUnsignedTransaction(transaction) {
15
- if ("signatures" in transaction) {
16
- const {
17
- signatures: _,
18
- // eslint-disable-line @typescript-eslint/no-unused-vars
19
- ...unsignedTransaction
20
- } = transaction;
21
- return unsignedTransaction;
22
- } else {
23
- return transaction;
24
- }
25
- }
26
-
27
- // src/blockhash.ts
11
+ // ../rpc-types/dist/index.browser.js
28
12
  var base58Encoder;
29
13
  function assertIsBlockhash(putativeBlockhash) {
30
14
  if (!base58Encoder)
@@ -48,6 +32,22 @@ function assertIsBlockhash(putativeBlockhash) {
48
32
  });
49
33
  }
50
34
  }
35
+
36
+ // src/unsigned-transaction.ts
37
+ function getUnsignedTransaction(transaction) {
38
+ if ("signatures" in transaction) {
39
+ const {
40
+ signatures: _,
41
+ // eslint-disable-line @typescript-eslint/no-unused-vars
42
+ ...unsignedTransaction
43
+ } = transaction;
44
+ return unsignedTransaction;
45
+ } else {
46
+ return transaction;
47
+ }
48
+ }
49
+
50
+ // src/blockhash.ts
51
51
  function isTransactionWithBlockhashLifetime(transaction) {
52
52
  const lifetimeConstraintShapeMatches = "lifetimeConstraint" in transaction && typeof transaction.lifetimeConstraint.blockhash === "string" && typeof transaction.lifetimeConstraint.lastValidBlockHeight === "bigint";
53
53
  if (!lifetimeConstraintShapeMatches)
@@ -217,6 +217,151 @@ function prependTransactionInstruction(instruction, transaction) {
217
217
  Object.freeze(out);
218
218
  return out;
219
219
  }
220
+
221
+ // src/decompile-transaction.ts
222
+ function getAccountMetas(message) {
223
+ const { header } = message;
224
+ const numWritableSignerAccounts = header.numSignerAccounts - header.numReadonlySignerAccounts;
225
+ const numWritableNonSignerAccounts = message.staticAccounts.length - header.numSignerAccounts - header.numReadonlyNonSignerAccounts;
226
+ const accountMetas = [];
227
+ let accountIndex = 0;
228
+ for (let i = 0; i < numWritableSignerAccounts; i++) {
229
+ accountMetas.push({
230
+ address: message.staticAccounts[accountIndex],
231
+ role: AccountRole.WRITABLE_SIGNER
232
+ });
233
+ accountIndex++;
234
+ }
235
+ for (let i = 0; i < header.numReadonlySignerAccounts; i++) {
236
+ accountMetas.push({
237
+ address: message.staticAccounts[accountIndex],
238
+ role: AccountRole.READONLY_SIGNER
239
+ });
240
+ accountIndex++;
241
+ }
242
+ for (let i = 0; i < numWritableNonSignerAccounts; i++) {
243
+ accountMetas.push({
244
+ address: message.staticAccounts[accountIndex],
245
+ role: AccountRole.WRITABLE
246
+ });
247
+ accountIndex++;
248
+ }
249
+ for (let i = 0; i < header.numReadonlyNonSignerAccounts; i++) {
250
+ accountMetas.push({
251
+ address: message.staticAccounts[accountIndex],
252
+ role: AccountRole.READONLY
253
+ });
254
+ accountIndex++;
255
+ }
256
+ return accountMetas;
257
+ }
258
+ function getAddressLookupMetas(compiledAddressTableLookups, addressesByLookupTableAddress) {
259
+ const compiledAddressTableLookupAddresses = compiledAddressTableLookups.map((l) => l.lookupTableAddress);
260
+ const missing = compiledAddressTableLookupAddresses.filter((a) => addressesByLookupTableAddress[a] === void 0);
261
+ if (missing.length > 0) {
262
+ const missingAddresses = missing.join(", ");
263
+ throw new Error(`Addresses not provided for lookup tables: [${missingAddresses}]`);
264
+ }
265
+ const readOnlyMetas = [];
266
+ const writableMetas = [];
267
+ for (const lookup of compiledAddressTableLookups) {
268
+ const addresses = addressesByLookupTableAddress[lookup.lookupTableAddress];
269
+ const highestIndex = Math.max(...lookup.readableIndices, ...lookup.writableIndices);
270
+ if (highestIndex >= addresses.length) {
271
+ throw new Error(
272
+ `Cannot look up index ${highestIndex} in lookup table [${lookup.lookupTableAddress}]. The lookup table may have been extended since the addresses provided were retrieved.`
273
+ );
274
+ }
275
+ const readOnlyForLookup = lookup.readableIndices.map((r) => ({
276
+ address: addresses[r],
277
+ addressIndex: r,
278
+ lookupTableAddress: lookup.lookupTableAddress,
279
+ role: AccountRole.READONLY
280
+ }));
281
+ readOnlyMetas.push(...readOnlyForLookup);
282
+ const writableForLookup = lookup.writableIndices.map((w) => ({
283
+ address: addresses[w],
284
+ addressIndex: w,
285
+ lookupTableAddress: lookup.lookupTableAddress,
286
+ role: AccountRole.WRITABLE
287
+ }));
288
+ writableMetas.push(...writableForLookup);
289
+ }
290
+ return [...writableMetas, ...readOnlyMetas];
291
+ }
292
+ function convertInstruction(instruction, accountMetas) {
293
+ const programAddress = accountMetas[instruction.programAddressIndex]?.address;
294
+ if (!programAddress) {
295
+ throw new Error(`Could not find program address at index ${instruction.programAddressIndex}`);
296
+ }
297
+ const accounts = instruction.accountIndices?.map((accountIndex) => accountMetas[accountIndex]);
298
+ const { data } = instruction;
299
+ return {
300
+ programAddress,
301
+ ...accounts && accounts.length ? { accounts } : {},
302
+ ...data && data.length ? { data } : {}
303
+ };
304
+ }
305
+ function getLifetimeConstraint(messageLifetimeToken, firstInstruction, lastValidBlockHeight) {
306
+ if (!firstInstruction || !isAdvanceNonceAccountInstruction(firstInstruction)) {
307
+ return {
308
+ blockhash: messageLifetimeToken,
309
+ lastValidBlockHeight: lastValidBlockHeight ?? 2n ** 64n - 1n
310
+ // U64 MAX
311
+ };
312
+ } else {
313
+ const nonceAccountAddress = firstInstruction.accounts[0].address;
314
+ addresses.assertIsAddress(nonceAccountAddress);
315
+ const nonceAuthorityAddress = firstInstruction.accounts[2].address;
316
+ addresses.assertIsAddress(nonceAuthorityAddress);
317
+ return {
318
+ nonce: messageLifetimeToken,
319
+ nonceAccountAddress,
320
+ nonceAuthorityAddress
321
+ };
322
+ }
323
+ }
324
+ function convertSignatures(compiledTransaction) {
325
+ const {
326
+ compiledMessage: { staticAccounts },
327
+ signatures
328
+ } = compiledTransaction;
329
+ return signatures.reduce((acc, sig, index) => {
330
+ const allZeros = sig.every((byte) => byte === 0);
331
+ if (allZeros)
332
+ return acc;
333
+ const address = staticAccounts[index];
334
+ return { ...acc, [address]: sig };
335
+ }, {});
336
+ }
337
+ function decompileTransaction(compiledTransaction, config) {
338
+ const { compiledMessage } = compiledTransaction;
339
+ const feePayer = compiledMessage.staticAccounts[0];
340
+ if (!feePayer)
341
+ throw new Error("No fee payer set in CompiledTransaction");
342
+ const accountMetas = getAccountMetas(compiledMessage);
343
+ const accountLookupMetas = "addressTableLookups" in compiledMessage && compiledMessage.addressTableLookups !== void 0 && compiledMessage.addressTableLookups.length > 0 ? getAddressLookupMetas(compiledMessage.addressTableLookups, config?.addressesByLookupTableAddress ?? {}) : [];
344
+ const transactionMetas = [...accountMetas, ...accountLookupMetas];
345
+ const instructions = compiledMessage.instructions.map(
346
+ (compiledInstruction) => convertInstruction(compiledInstruction, transactionMetas)
347
+ );
348
+ const firstInstruction = instructions[0];
349
+ const lifetimeConstraint = getLifetimeConstraint(
350
+ compiledMessage.lifetimeToken,
351
+ firstInstruction,
352
+ config?.lastValidBlockHeight
353
+ );
354
+ const signatures = convertSignatures(compiledTransaction);
355
+ return functional.pipe(
356
+ createTransaction({ version: compiledMessage.version }),
357
+ (tx) => setTransactionFeePayer(feePayer, tx),
358
+ (tx) => instructions.reduce((acc, instruction) => {
359
+ return appendTransactionInstruction(instruction, acc);
360
+ }, tx),
361
+ (tx) => "blockhash" in lifetimeConstraint ? setTransactionLifetimeUsingBlockhash(lifetimeConstraint, tx) : setTransactionLifetimeUsingDurableNonce(lifetimeConstraint, tx),
362
+ (tx) => Object.keys(signatures).length > 0 ? { ...tx, signatures } : tx
363
+ );
364
+ }
220
365
  function upsert(addressMap, address, update) {
221
366
  addressMap[address] = update(addressMap[address] ?? { role: AccountRole.READONLY });
222
367
  }
@@ -710,116 +855,6 @@ function getCompiledTransaction(transaction) {
710
855
  signatures
711
856
  };
712
857
  }
713
- function getAccountMetas(message) {
714
- const { header } = message;
715
- const numWritableSignerAccounts = header.numSignerAccounts - header.numReadonlySignerAccounts;
716
- const numWritableNonSignerAccounts = message.staticAccounts.length - header.numSignerAccounts - header.numReadonlyNonSignerAccounts;
717
- const accountMetas = [];
718
- let accountIndex = 0;
719
- for (let i = 0; i < numWritableSignerAccounts; i++) {
720
- accountMetas.push({
721
- address: message.staticAccounts[accountIndex],
722
- role: AccountRole.WRITABLE_SIGNER
723
- });
724
- accountIndex++;
725
- }
726
- for (let i = 0; i < header.numReadonlySignerAccounts; i++) {
727
- accountMetas.push({
728
- address: message.staticAccounts[accountIndex],
729
- role: AccountRole.READONLY_SIGNER
730
- });
731
- accountIndex++;
732
- }
733
- for (let i = 0; i < numWritableNonSignerAccounts; i++) {
734
- accountMetas.push({
735
- address: message.staticAccounts[accountIndex],
736
- role: AccountRole.WRITABLE
737
- });
738
- accountIndex++;
739
- }
740
- for (let i = 0; i < header.numReadonlyNonSignerAccounts; i++) {
741
- accountMetas.push({
742
- address: message.staticAccounts[accountIndex],
743
- role: AccountRole.READONLY
744
- });
745
- accountIndex++;
746
- }
747
- return accountMetas;
748
- }
749
- function convertInstruction(instruction, accountMetas) {
750
- const programAddress = accountMetas[instruction.programAddressIndex]?.address;
751
- if (!programAddress) {
752
- throw new Error(`Could not find program address at index ${instruction.programAddressIndex}`);
753
- }
754
- const accounts = instruction.accountIndices?.map((accountIndex) => accountMetas[accountIndex]);
755
- const { data } = instruction;
756
- return {
757
- programAddress,
758
- ...accounts && accounts.length ? { accounts } : {},
759
- ...data && data.length ? { data } : {}
760
- };
761
- }
762
- function getLifetimeConstraint(messageLifetimeToken, firstInstruction, lastValidBlockHeight) {
763
- if (!firstInstruction || !isAdvanceNonceAccountInstruction(firstInstruction)) {
764
- return {
765
- blockhash: messageLifetimeToken,
766
- lastValidBlockHeight: lastValidBlockHeight ?? 2n ** 64n - 1n
767
- // U64 MAX
768
- };
769
- } else {
770
- const nonceAccountAddress = firstInstruction.accounts[0].address;
771
- addresses.assertIsAddress(nonceAccountAddress);
772
- const nonceAuthorityAddress = firstInstruction.accounts[2].address;
773
- addresses.assertIsAddress(nonceAuthorityAddress);
774
- return {
775
- nonce: messageLifetimeToken,
776
- nonceAccountAddress,
777
- nonceAuthorityAddress
778
- };
779
- }
780
- }
781
- function convertSignatures(compiledTransaction) {
782
- const {
783
- compiledMessage: { staticAccounts },
784
- signatures
785
- } = compiledTransaction;
786
- return signatures.reduce((acc, sig, index) => {
787
- const allZeros = sig.every((byte) => byte === 0);
788
- if (allZeros)
789
- return acc;
790
- const address = staticAccounts[index];
791
- return { ...acc, [address]: sig };
792
- }, {});
793
- }
794
- function decompileTransaction(compiledTransaction, lastValidBlockHeight) {
795
- const { compiledMessage } = compiledTransaction;
796
- if ("addressTableLookups" in compiledMessage && compiledMessage.addressTableLookups.length > 0) {
797
- throw new Error("Cannot convert transaction with addressTableLookups");
798
- }
799
- const feePayer = compiledMessage.staticAccounts[0];
800
- if (!feePayer)
801
- throw new Error("No fee payer set in CompiledTransaction");
802
- const accountMetas = getAccountMetas(compiledMessage);
803
- const instructions = compiledMessage.instructions.map(
804
- (compiledInstruction) => convertInstruction(compiledInstruction, accountMetas)
805
- );
806
- const firstInstruction = instructions[0];
807
- const lifetimeConstraint = getLifetimeConstraint(
808
- compiledMessage.lifetimeToken,
809
- firstInstruction,
810
- lastValidBlockHeight
811
- );
812
- const signatures = convertSignatures(compiledTransaction);
813
- return functional.pipe(
814
- createTransaction({ version: compiledMessage.version }),
815
- (tx) => setTransactionFeePayer(feePayer, tx),
816
- (tx) => instructions.reduce((acc, instruction) => {
817
- return appendTransactionInstruction(instruction, acc);
818
- }, tx),
819
- (tx) => "blockhash" in lifetimeConstraint ? setTransactionLifetimeUsingBlockhash(lifetimeConstraint, tx) : setTransactionLifetimeUsingDurableNonce(lifetimeConstraint, tx),
820
- (tx) => compiledTransaction.signatures.length ? { ...tx, signatures } : tx
821
- );
822
- }
823
858
 
824
859
  // src/serializers/transaction.ts
825
860
  function getCompiledTransactionEncoder() {
@@ -842,14 +877,14 @@ function getCompiledTransactionDecoder() {
842
877
  function getTransactionEncoder() {
843
878
  return codecsCore.mapEncoder(getCompiledTransactionEncoder(), getCompiledTransaction);
844
879
  }
845
- function getTransactionDecoder(lastValidBlockHeight) {
880
+ function getTransactionDecoder(config) {
846
881
  return codecsCore.mapDecoder(
847
882
  getCompiledTransactionDecoder(),
848
- (compiledTransaction) => decompileTransaction(compiledTransaction, lastValidBlockHeight)
883
+ (compiledTransaction) => decompileTransaction(compiledTransaction, config)
849
884
  );
850
885
  }
851
- function getTransactionCodec(lastValidBlockHeight) {
852
- return codecsCore.combineCodec(getTransactionEncoder(), getTransactionDecoder(lastValidBlockHeight));
886
+ function getTransactionCodec(config) {
887
+ return codecsCore.combineCodec(getTransactionEncoder(), getTransactionDecoder(config));
853
888
  }
854
889
  var base58Decoder;
855
890
  function getSignatureFromTransaction(transaction) {
@@ -892,11 +927,15 @@ async function signTransaction(keyPairs, transaction) {
892
927
  function assertTransactionIsFullySigned(transaction) {
893
928
  const signerAddressesFromInstructions = transaction.instructions.flatMap((i) => i.accounts?.filter((a) => isSignerRole(a.role)) ?? []).map((a) => a.address);
894
929
  const requiredSigners = /* @__PURE__ */ new Set([transaction.feePayer, ...signerAddressesFromInstructions]);
930
+ const missingSigs = [];
895
931
  requiredSigners.forEach((address) => {
896
932
  if (!transaction.signatures[address]) {
897
- throw new Error(`Transaction is missing signature for address \`${address}\``);
933
+ missingSigs.push(address);
898
934
  }
899
935
  });
936
+ if (missingSigs.length > 0) {
937
+ throw new Error("Transaction is missing signatures for addresses: " + missingSigs.join(", "));
938
+ }
900
939
  }
901
940
  function getBase64EncodedWireTransaction(transaction) {
902
941
  const wireTransactionBytes = getTransactionEncoder().encode(transaction);
@@ -904,16 +943,17 @@ function getBase64EncodedWireTransaction(transaction) {
904
943
  }
905
944
 
906
945
  exports.appendTransactionInstruction = appendTransactionInstruction;
907
- exports.assertIsBlockhash = assertIsBlockhash;
908
946
  exports.assertIsDurableNonceTransaction = assertIsDurableNonceTransaction;
909
947
  exports.assertIsTransactionWithBlockhashLifetime = assertIsTransactionWithBlockhashLifetime;
910
948
  exports.assertTransactionIsFullySigned = assertTransactionIsFullySigned;
911
949
  exports.compileMessage = compileMessage;
912
950
  exports.createTransaction = createTransaction;
951
+ exports.decompileTransaction = decompileTransaction;
913
952
  exports.getBase64EncodedWireTransaction = getBase64EncodedWireTransaction;
914
953
  exports.getCompiledMessageCodec = getCompiledMessageCodec;
915
954
  exports.getCompiledMessageDecoder = getCompiledMessageDecoder;
916
955
  exports.getCompiledMessageEncoder = getCompiledMessageEncoder;
956
+ exports.getCompiledTransactionDecoder = getCompiledTransactionDecoder;
917
957
  exports.getSignatureFromTransaction = getSignatureFromTransaction;
918
958
  exports.getTransactionCodec = getTransactionCodec;
919
959
  exports.getTransactionDecoder = getTransactionDecoder;
@@ -925,3 +965,5 @@ exports.setTransactionFeePayer = setTransactionFeePayer;
925
965
  exports.setTransactionLifetimeUsingBlockhash = setTransactionLifetimeUsingBlockhash;
926
966
  exports.setTransactionLifetimeUsingDurableNonce = setTransactionLifetimeUsingDurableNonce;
927
967
  exports.signTransaction = signTransaction;
968
+ //# sourceMappingURL=out.js.map
969
+ //# sourceMappingURL=index.browser.cjs.map