@solana/transactions 2.0.0-experimental.eeb355e → 2.0.0-experimental.ef09aec

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.
@@ -1,10 +1,10 @@
1
1
  'use strict';
2
2
 
3
3
  var codecsStrings = require('@solana/codecs-strings');
4
+ var addresses = require('@solana/addresses');
4
5
  var codecsCore = require('@solana/codecs-core');
5
6
  var codecsDataStructures = require('@solana/codecs-data-structures');
6
7
  var codecsNumbers = require('@solana/codecs-numbers');
7
- var addresses = require('@solana/addresses');
8
8
  var functional = require('@solana/functional');
9
9
  var keys = require('@solana/keys');
10
10
 
@@ -254,7 +254,7 @@ function getAddressMapFromInstructions(feePayer, instructions) {
254
254
  const shouldReplaceEntry = (
255
255
  // Consider using the new LOOKUP_TABLE if its address is different...
256
256
  entry.lookupTableAddress !== accountMeta.lookupTableAddress && // ...and sorts before the existing one.
257
- (addressComparator || (addressComparator = addresses.getAddressComparator()))(
257
+ (addressComparator ||= addresses.getAddressComparator())(
258
258
  accountMeta.lookupTableAddress,
259
259
  entry.lookupTableAddress
260
260
  ) < 0
@@ -360,7 +360,7 @@ function getOrderedAccountsFromAddressMap(addressMap) {
360
360
  if (leftIsWritable !== isWritableRole(rightEntry.role)) {
361
361
  return leftIsWritable ? -1 : 1;
362
362
  }
363
- addressComparator || (addressComparator = addresses.getAddressComparator());
363
+ addressComparator ||= addresses.getAddressComparator();
364
364
  if (leftEntry[TYPE] === 1 /* LOOKUP_TABLE */ && rightEntry[TYPE] === 1 /* LOOKUP_TABLE */ && leftEntry.lookupTableAddress !== rightEntry.lookupTableAddress) {
365
365
  return addressComparator(leftEntry.lookupTableAddress, rightEntry.lookupTableAddress);
366
366
  } else {
@@ -373,16 +373,15 @@ function getOrderedAccountsFromAddressMap(addressMap) {
373
373
  return orderedAccounts;
374
374
  }
375
375
  function getCompiledAddressTableLookups(orderedAccounts) {
376
- var _a;
377
376
  const index = {};
378
377
  for (const account of orderedAccounts) {
379
378
  if (!("lookupTableAddress" in account)) {
380
379
  continue;
381
380
  }
382
- const entry = index[_a = account.lookupTableAddress] || (index[_a] = {
381
+ const entry = index[account.lookupTableAddress] ||= {
383
382
  readableIndices: [],
384
383
  writableIndices: []
385
- });
384
+ };
386
385
  if (account.role === AccountRole.WRITABLE) {
387
386
  entry.writableIndices.push(account.addressIndex);
388
387
  } else {
@@ -468,134 +467,6 @@ function compileMessage(transaction) {
468
467
  version: transaction.version
469
468
  };
470
469
  }
471
-
472
- // src/compile-transaction.ts
473
- function getCompiledTransaction(transaction) {
474
- const compiledMessage = compileMessage(transaction);
475
- let signatures;
476
- if ("signatures" in transaction) {
477
- signatures = [];
478
- for (let ii = 0; ii < compiledMessage.header.numSignerAccounts; ii++) {
479
- signatures[ii] = transaction.signatures[compiledMessage.staticAccounts[ii]] ?? new Uint8Array(Array(64).fill(0));
480
- }
481
- } else {
482
- signatures = Array(compiledMessage.header.numSignerAccounts).fill(new Uint8Array(Array(64).fill(0)));
483
- }
484
- return {
485
- compiledMessage,
486
- signatures
487
- };
488
- }
489
- function getAccountMetas(message) {
490
- const { header } = message;
491
- const numWritableSignerAccounts = header.numSignerAccounts - header.numReadonlySignerAccounts;
492
- const numWritableNonSignerAccounts = message.staticAccounts.length - header.numSignerAccounts - header.numReadonlyNonSignerAccounts;
493
- const accountMetas = [];
494
- let accountIndex = 0;
495
- for (let i = 0; i < numWritableSignerAccounts; i++) {
496
- accountMetas.push({
497
- address: message.staticAccounts[accountIndex],
498
- role: AccountRole.WRITABLE_SIGNER
499
- });
500
- accountIndex++;
501
- }
502
- for (let i = 0; i < header.numReadonlySignerAccounts; i++) {
503
- accountMetas.push({
504
- address: message.staticAccounts[accountIndex],
505
- role: AccountRole.READONLY_SIGNER
506
- });
507
- accountIndex++;
508
- }
509
- for (let i = 0; i < numWritableNonSignerAccounts; i++) {
510
- accountMetas.push({
511
- address: message.staticAccounts[accountIndex],
512
- role: AccountRole.WRITABLE
513
- });
514
- accountIndex++;
515
- }
516
- for (let i = 0; i < header.numReadonlyNonSignerAccounts; i++) {
517
- accountMetas.push({
518
- address: message.staticAccounts[accountIndex],
519
- role: AccountRole.READONLY
520
- });
521
- accountIndex++;
522
- }
523
- return accountMetas;
524
- }
525
- function convertInstruction(instruction, accountMetas) {
526
- const programAddress = accountMetas[instruction.programAddressIndex]?.address;
527
- if (!programAddress) {
528
- throw new Error(`Could not find program address at index ${instruction.programAddressIndex}`);
529
- }
530
- const accounts = instruction.accountIndices?.map((accountIndex) => accountMetas[accountIndex]);
531
- const { data } = instruction;
532
- return {
533
- programAddress,
534
- ...accounts && accounts.length ? { accounts } : {},
535
- ...data && data.length ? { data } : {}
536
- };
537
- }
538
- function getLifetimeConstraint(messageLifetimeToken, firstInstruction, lastValidBlockHeight) {
539
- if (!firstInstruction || !isAdvanceNonceAccountInstruction(firstInstruction)) {
540
- return {
541
- blockhash: messageLifetimeToken,
542
- lastValidBlockHeight: lastValidBlockHeight ?? 2n ** 64n - 1n
543
- // U64 MAX
544
- };
545
- } else {
546
- const nonceAccountAddress = firstInstruction.accounts[0].address;
547
- addresses.assertIsAddress(nonceAccountAddress);
548
- const nonceAuthorityAddress = firstInstruction.accounts[2].address;
549
- addresses.assertIsAddress(nonceAuthorityAddress);
550
- return {
551
- nonce: messageLifetimeToken,
552
- nonceAccountAddress,
553
- nonceAuthorityAddress
554
- };
555
- }
556
- }
557
- function convertSignatures(compiledTransaction) {
558
- const {
559
- compiledMessage: { staticAccounts },
560
- signatures
561
- } = compiledTransaction;
562
- return signatures.reduce((acc, sig, index) => {
563
- const allZeros = sig.every((byte) => byte === 0);
564
- if (allZeros)
565
- return acc;
566
- const address = staticAccounts[index];
567
- return { ...acc, [address]: sig };
568
- }, {});
569
- }
570
- function decompileTransaction(compiledTransaction, lastValidBlockHeight) {
571
- const { compiledMessage } = compiledTransaction;
572
- if ("addressTableLookups" in compiledMessage && compiledMessage.addressTableLookups.length > 0) {
573
- throw new Error("Cannot convert transaction with addressTableLookups");
574
- }
575
- const feePayer = compiledMessage.staticAccounts[0];
576
- if (!feePayer)
577
- throw new Error("No fee payer set in CompiledTransaction");
578
- const accountMetas = getAccountMetas(compiledMessage);
579
- const instructions = compiledMessage.instructions.map(
580
- (compiledInstruction) => convertInstruction(compiledInstruction, accountMetas)
581
- );
582
- const firstInstruction = instructions[0];
583
- const lifetimeConstraint = getLifetimeConstraint(
584
- compiledMessage.lifetimeToken,
585
- firstInstruction,
586
- lastValidBlockHeight
587
- );
588
- const signatures = convertSignatures(compiledTransaction);
589
- return functional.pipe(
590
- createTransaction({ version: compiledMessage.version }),
591
- (tx) => setTransactionFeePayer(feePayer, tx),
592
- (tx) => instructions.reduce((acc, instruction) => {
593
- return appendTransactionInstruction(instruction, acc);
594
- }, tx),
595
- (tx) => "blockhash" in lifetimeConstraint ? setTransactionLifetimeUsingBlockhash(lifetimeConstraint, tx) : setTransactionLifetimeUsingDurableNonce(lifetimeConstraint, tx),
596
- (tx) => compiledTransaction.signatures.length ? { ...tx, signatures } : tx
597
- );
598
- }
599
470
  var lookupTableAddressDescription = __DEV__ ? "The address of the address lookup table account from which instruction addresses should be looked up" : "lookupTableAddress";
600
471
  var writableIndicesDescription = __DEV__ ? "The indices of the accounts in the lookup table that should be loaded as writeable" : "writableIndices";
601
472
  var readableIndicesDescription = __DEV__ ? "The indices of the accounts in the lookup table that should be loaded as read-only" : "readableIndices";
@@ -930,6 +801,137 @@ function getCompiledMessageDecoder() {
930
801
  }
931
802
  );
932
803
  }
804
+ function getCompiledMessageCodec() {
805
+ return codecsCore.combineCodec(getCompiledMessageEncoder(), getCompiledMessageDecoder());
806
+ }
807
+
808
+ // src/compile-transaction.ts
809
+ function getCompiledTransaction(transaction) {
810
+ const compiledMessage = compileMessage(transaction);
811
+ let signatures;
812
+ if ("signatures" in transaction) {
813
+ signatures = [];
814
+ for (let ii = 0; ii < compiledMessage.header.numSignerAccounts; ii++) {
815
+ signatures[ii] = transaction.signatures[compiledMessage.staticAccounts[ii]] ?? new Uint8Array(Array(64).fill(0));
816
+ }
817
+ } else {
818
+ signatures = Array(compiledMessage.header.numSignerAccounts).fill(new Uint8Array(Array(64).fill(0)));
819
+ }
820
+ return {
821
+ compiledMessage,
822
+ signatures
823
+ };
824
+ }
825
+ function getAccountMetas(message) {
826
+ const { header } = message;
827
+ const numWritableSignerAccounts = header.numSignerAccounts - header.numReadonlySignerAccounts;
828
+ const numWritableNonSignerAccounts = message.staticAccounts.length - header.numSignerAccounts - header.numReadonlyNonSignerAccounts;
829
+ const accountMetas = [];
830
+ let accountIndex = 0;
831
+ for (let i = 0; i < numWritableSignerAccounts; i++) {
832
+ accountMetas.push({
833
+ address: message.staticAccounts[accountIndex],
834
+ role: AccountRole.WRITABLE_SIGNER
835
+ });
836
+ accountIndex++;
837
+ }
838
+ for (let i = 0; i < header.numReadonlySignerAccounts; i++) {
839
+ accountMetas.push({
840
+ address: message.staticAccounts[accountIndex],
841
+ role: AccountRole.READONLY_SIGNER
842
+ });
843
+ accountIndex++;
844
+ }
845
+ for (let i = 0; i < numWritableNonSignerAccounts; i++) {
846
+ accountMetas.push({
847
+ address: message.staticAccounts[accountIndex],
848
+ role: AccountRole.WRITABLE
849
+ });
850
+ accountIndex++;
851
+ }
852
+ for (let i = 0; i < header.numReadonlyNonSignerAccounts; i++) {
853
+ accountMetas.push({
854
+ address: message.staticAccounts[accountIndex],
855
+ role: AccountRole.READONLY
856
+ });
857
+ accountIndex++;
858
+ }
859
+ return accountMetas;
860
+ }
861
+ function convertInstruction(instruction, accountMetas) {
862
+ const programAddress = accountMetas[instruction.programAddressIndex]?.address;
863
+ if (!programAddress) {
864
+ throw new Error(`Could not find program address at index ${instruction.programAddressIndex}`);
865
+ }
866
+ const accounts = instruction.accountIndices?.map((accountIndex) => accountMetas[accountIndex]);
867
+ const { data } = instruction;
868
+ return {
869
+ programAddress,
870
+ ...accounts && accounts.length ? { accounts } : {},
871
+ ...data && data.length ? { data } : {}
872
+ };
873
+ }
874
+ function getLifetimeConstraint(messageLifetimeToken, firstInstruction, lastValidBlockHeight) {
875
+ if (!firstInstruction || !isAdvanceNonceAccountInstruction(firstInstruction)) {
876
+ return {
877
+ blockhash: messageLifetimeToken,
878
+ lastValidBlockHeight: lastValidBlockHeight ?? 2n ** 64n - 1n
879
+ // U64 MAX
880
+ };
881
+ } else {
882
+ const nonceAccountAddress = firstInstruction.accounts[0].address;
883
+ addresses.assertIsAddress(nonceAccountAddress);
884
+ const nonceAuthorityAddress = firstInstruction.accounts[2].address;
885
+ addresses.assertIsAddress(nonceAuthorityAddress);
886
+ return {
887
+ nonce: messageLifetimeToken,
888
+ nonceAccountAddress,
889
+ nonceAuthorityAddress
890
+ };
891
+ }
892
+ }
893
+ function convertSignatures(compiledTransaction) {
894
+ const {
895
+ compiledMessage: { staticAccounts },
896
+ signatures
897
+ } = compiledTransaction;
898
+ return signatures.reduce((acc, sig, index) => {
899
+ const allZeros = sig.every((byte) => byte === 0);
900
+ if (allZeros)
901
+ return acc;
902
+ const address = staticAccounts[index];
903
+ return { ...acc, [address]: sig };
904
+ }, {});
905
+ }
906
+ function decompileTransaction(compiledTransaction, lastValidBlockHeight) {
907
+ const { compiledMessage } = compiledTransaction;
908
+ if ("addressTableLookups" in compiledMessage && compiledMessage.addressTableLookups.length > 0) {
909
+ throw new Error("Cannot convert transaction with addressTableLookups");
910
+ }
911
+ const feePayer = compiledMessage.staticAccounts[0];
912
+ if (!feePayer)
913
+ throw new Error("No fee payer set in CompiledTransaction");
914
+ const accountMetas = getAccountMetas(compiledMessage);
915
+ const instructions = compiledMessage.instructions.map(
916
+ (compiledInstruction) => convertInstruction(compiledInstruction, accountMetas)
917
+ );
918
+ const firstInstruction = instructions[0];
919
+ const lifetimeConstraint = getLifetimeConstraint(
920
+ compiledMessage.lifetimeToken,
921
+ firstInstruction,
922
+ lastValidBlockHeight
923
+ );
924
+ const signatures = convertSignatures(compiledTransaction);
925
+ return functional.pipe(
926
+ createTransaction({ version: compiledMessage.version }),
927
+ (tx) => setTransactionFeePayer(feePayer, tx),
928
+ (tx) => instructions.reduce((acc, instruction) => {
929
+ return appendTransactionInstruction(instruction, acc);
930
+ }, tx),
931
+ (tx) => "blockhash" in lifetimeConstraint ? setTransactionLifetimeUsingBlockhash(lifetimeConstraint, tx) : setTransactionLifetimeUsingDurableNonce(lifetimeConstraint, tx),
932
+ (tx) => compiledTransaction.signatures.length ? { ...tx, signatures } : tx
933
+ );
934
+ }
933
935
 
934
936
  // src/serializers/transaction.ts
935
937
  var signaturesDescription = __DEV__ ? "A compact array of 64-byte, base-64 encoded Ed25519 signatures" : "signatures";
@@ -1039,8 +1041,12 @@ exports.appendTransactionInstruction = appendTransactionInstruction;
1039
1041
  exports.assertIsBlockhash = assertIsBlockhash;
1040
1042
  exports.assertIsDurableNonceTransaction = assertIsDurableNonceTransaction;
1041
1043
  exports.assertTransactionIsFullySigned = assertTransactionIsFullySigned;
1044
+ exports.compileMessage = compileMessage;
1042
1045
  exports.createTransaction = createTransaction;
1043
1046
  exports.getBase64EncodedWireTransaction = getBase64EncodedWireTransaction;
1047
+ exports.getCompiledMessageCodec = getCompiledMessageCodec;
1048
+ exports.getCompiledMessageDecoder = getCompiledMessageDecoder;
1049
+ exports.getCompiledMessageEncoder = getCompiledMessageEncoder;
1044
1050
  exports.getSignatureFromTransaction = getSignatureFromTransaction;
1045
1051
  exports.getTransactionCodec = getTransactionCodec;
1046
1052
  exports.getTransactionDecoder = getTransactionDecoder;
@@ -1052,5 +1058,3 @@ exports.setTransactionFeePayer = setTransactionFeePayer;
1052
1058
  exports.setTransactionLifetimeUsingBlockhash = setTransactionLifetimeUsingBlockhash;
1053
1059
  exports.setTransactionLifetimeUsingDurableNonce = setTransactionLifetimeUsingDurableNonce;
1054
1060
  exports.signTransaction = signTransaction;
1055
- //# sourceMappingURL=out.js.map
1056
- //# sourceMappingURL=index.browser.cjs.map