@solana/transactions 2.0.0-experimental.25278c4 → 2.0.0-experimental.26136d9

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 (55) hide show
  1. package/README.md +272 -5
  2. package/dist/index.browser.cjs +342 -63
  3. package/dist/index.browser.cjs.map +1 -1
  4. package/dist/index.browser.js +341 -65
  5. package/dist/index.browser.js.map +1 -1
  6. package/dist/index.development.js +820 -460
  7. package/dist/index.development.js.map +1 -1
  8. package/dist/index.native.js +341 -65
  9. package/dist/index.native.js.map +1 -1
  10. package/dist/index.node.cjs +342 -63
  11. package/dist/index.node.cjs.map +1 -1
  12. package/dist/index.node.js +341 -65
  13. package/dist/index.node.js.map +1 -1
  14. package/dist/index.production.min.js +18 -17
  15. package/dist/types/accounts.d.ts +2 -2
  16. package/dist/types/accounts.d.ts.map +1 -0
  17. package/dist/types/blockhash.d.ts.map +1 -0
  18. package/dist/types/compilable-transaction.d.ts +7 -0
  19. package/dist/types/compilable-transaction.d.ts.map +1 -0
  20. package/dist/types/compile-address-table-lookups.d.ts +2 -2
  21. package/dist/types/compile-address-table-lookups.d.ts.map +1 -0
  22. package/dist/types/compile-header.d.ts.map +1 -0
  23. package/dist/types/compile-instructions.d.ts.map +1 -0
  24. package/dist/types/compile-lifetime-token.d.ts.map +1 -0
  25. package/dist/types/compile-static-accounts.d.ts +2 -2
  26. package/dist/types/compile-static-accounts.d.ts.map +1 -0
  27. package/dist/types/compile-transaction.d.ts +5 -6
  28. package/dist/types/compile-transaction.d.ts.map +1 -0
  29. package/dist/types/create-transaction.d.ts.map +1 -0
  30. package/dist/types/decompile-transaction.d.ts +5 -0
  31. package/dist/types/decompile-transaction.d.ts.map +1 -0
  32. package/dist/types/durable-nonce.d.ts +5 -6
  33. package/dist/types/durable-nonce.d.ts.map +1 -0
  34. package/dist/types/fee-payer.d.ts +4 -4
  35. package/dist/types/fee-payer.d.ts.map +1 -0
  36. package/dist/types/index.d.ts +1 -0
  37. package/dist/types/index.d.ts.map +1 -0
  38. package/dist/types/instructions.d.ts.map +1 -0
  39. package/dist/types/message.d.ts +1 -5
  40. package/dist/types/message.d.ts.map +1 -0
  41. package/dist/types/serializers/address-table-lookup.d.ts.map +1 -0
  42. package/dist/types/serializers/header.d.ts.map +1 -0
  43. package/dist/types/serializers/index.d.ts.map +1 -0
  44. package/dist/types/serializers/instruction.d.ts.map +1 -0
  45. package/dist/types/serializers/message.d.ts.map +1 -0
  46. package/dist/types/serializers/transaction-version.d.ts.map +1 -0
  47. package/dist/types/serializers/transaction.d.ts +5 -5
  48. package/dist/types/serializers/transaction.d.ts.map +1 -0
  49. package/dist/types/signatures.d.ts +8 -12
  50. package/dist/types/signatures.d.ts.map +1 -0
  51. package/dist/types/types.d.ts +6 -13
  52. package/dist/types/types.d.ts.map +1 -0
  53. package/dist/types/unsigned-transaction.d.ts.map +1 -0
  54. package/dist/types/wire-transaction.d.ts.map +1 -0
  55. package/package.json +11 -11
@@ -1,9 +1,9 @@
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';
1
+ import { getBase58Encoder, getBase58Decoder, getStringEncoder, getStringDecoder } from '@solana/codecs-strings';
2
+ import { mapEncoder, mapDecoder, combineCodec } from '@solana/codecs-core';
3
+ import { getStructEncoder, getArrayEncoder, getBytesEncoder, getStructDecoder, getArrayDecoder, getBytesDecoder } from '@solana/codecs-data-structures';
4
+ import { getShortU16Encoder, getShortU16Decoder, getU8Encoder, getU8Decoder } from '@solana/codecs-numbers';
5
+ import { getAddressFromPublicKey, getAddressComparator, assertIsAddress, getAddressEncoder, getAddressDecoder } from '@solana/addresses';
6
+ import { pipe } from '@solana/functional';
7
7
  import { signBytes } from '@solana/keys';
8
8
 
9
9
  // ../build-scripts/env-shim.ts
@@ -24,7 +24,10 @@ function getUnsignedTransaction(transaction) {
24
24
  }
25
25
 
26
26
  // src/blockhash.ts
27
+ var base58Encoder;
27
28
  function assertIsBlockhash(putativeBlockhash) {
29
+ if (!base58Encoder)
30
+ base58Encoder = getBase58Encoder();
28
31
  try {
29
32
  if (
30
33
  // Lowest value (32 bytes of zeroes)
@@ -33,7 +36,7 @@ function assertIsBlockhash(putativeBlockhash) {
33
36
  ) {
34
37
  throw new Error("Expected input string to decode to a byte array of length 32.");
35
38
  }
36
- const bytes = base58.serialize(putativeBlockhash);
39
+ const bytes = base58Encoder.encode(putativeBlockhash);
37
40
  const numBytes = bytes.byteLength;
38
41
  if (numBytes !== 32) {
39
42
  throw new Error(`Expected input string to decode to a byte array of length 32. Actual length: ${numBytes}`);
@@ -119,7 +122,7 @@ function isAdvanceNonceAccountInstruction(instruction) {
119
122
  instruction.accounts?.length === 3 && // First account is nonce account address
120
123
  instruction.accounts[0].address != null && instruction.accounts[0].role === AccountRole.WRITABLE && // Second account is recent blockhashes sysvar
121
124
  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;
125
+ instruction.accounts[2].address != null && isSignerRole(instruction.accounts[2].role);
123
126
  }
124
127
  function isAdvanceNonceAccountInstructionData(data) {
125
128
  return data.byteLength === 4 && data[0] === 4 && data[1] === 0 && data[2] === 0 && data[3] === 0;
@@ -127,21 +130,38 @@ function isAdvanceNonceAccountInstructionData(data) {
127
130
  function isDurableNonceTransaction(transaction) {
128
131
  return "lifetimeConstraint" in transaction && typeof transaction.lifetimeConstraint.nonce === "string" && transaction.instructions[0] != null && isAdvanceNonceAccountInstruction(transaction.instructions[0]);
129
132
  }
133
+ function isAdvanceNonceAccountInstructionForNonce(instruction, nonceAccountAddress, nonceAuthorityAddress) {
134
+ return instruction.accounts[0].address === nonceAccountAddress && instruction.accounts[2].address === nonceAuthorityAddress;
135
+ }
130
136
  function setTransactionLifetimeUsingDurableNonce({
131
137
  nonce,
132
138
  nonceAccountAddress,
133
139
  nonceAuthorityAddress
134
140
  }, 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;
141
+ let newInstructions;
142
+ const firstInstruction = transaction.instructions[0];
143
+ if (firstInstruction && isAdvanceNonceAccountInstruction(firstInstruction)) {
144
+ if (isAdvanceNonceAccountInstructionForNonce(firstInstruction, nonceAccountAddress, nonceAuthorityAddress)) {
145
+ if (isDurableNonceTransaction(transaction) && transaction.lifetimeConstraint.nonce === nonce) {
146
+ return transaction;
147
+ } else {
148
+ newInstructions = [firstInstruction, ...transaction.instructions.slice(1)];
149
+ }
150
+ } else {
151
+ newInstructions = [
152
+ createAdvanceNonceAccountInstruction(nonceAccountAddress, nonceAuthorityAddress),
153
+ ...transaction.instructions.slice(1)
154
+ ];
155
+ }
156
+ } else {
157
+ newInstructions = [
158
+ createAdvanceNonceAccountInstruction(nonceAccountAddress, nonceAuthorityAddress),
159
+ ...transaction.instructions
160
+ ];
138
161
  }
139
162
  const out = {
140
163
  ...getUnsignedTransaction(transaction),
141
- instructions: [
142
- createAdvanceNonceAccountInstruction(nonceAccountAddress, nonceAuthorityAddress),
143
- ...isAlreadyDurableNonceTransaction ? transaction.instructions.slice(1) : transaction.instructions
144
- ],
164
+ instructions: newInstructions,
145
165
  lifetimeConstraint: {
146
166
  nonce
147
167
  }
@@ -464,6 +484,116 @@ function getCompiledTransaction(transaction) {
464
484
  signatures
465
485
  };
466
486
  }
487
+ function getAccountMetas(message) {
488
+ const { header } = message;
489
+ const numWritableSignerAccounts = header.numSignerAccounts - header.numReadonlySignerAccounts;
490
+ const numWritableNonSignerAccounts = message.staticAccounts.length - header.numSignerAccounts - header.numReadonlyNonSignerAccounts;
491
+ const accountMetas = [];
492
+ let accountIndex = 0;
493
+ for (let i = 0; i < numWritableSignerAccounts; i++) {
494
+ accountMetas.push({
495
+ address: message.staticAccounts[accountIndex],
496
+ role: AccountRole.WRITABLE_SIGNER
497
+ });
498
+ accountIndex++;
499
+ }
500
+ for (let i = 0; i < header.numReadonlySignerAccounts; i++) {
501
+ accountMetas.push({
502
+ address: message.staticAccounts[accountIndex],
503
+ role: AccountRole.READONLY_SIGNER
504
+ });
505
+ accountIndex++;
506
+ }
507
+ for (let i = 0; i < numWritableNonSignerAccounts; i++) {
508
+ accountMetas.push({
509
+ address: message.staticAccounts[accountIndex],
510
+ role: AccountRole.WRITABLE
511
+ });
512
+ accountIndex++;
513
+ }
514
+ for (let i = 0; i < header.numReadonlyNonSignerAccounts; i++) {
515
+ accountMetas.push({
516
+ address: message.staticAccounts[accountIndex],
517
+ role: AccountRole.READONLY
518
+ });
519
+ accountIndex++;
520
+ }
521
+ return accountMetas;
522
+ }
523
+ function convertInstruction(instruction, accountMetas) {
524
+ const programAddress = accountMetas[instruction.programAddressIndex]?.address;
525
+ if (!programAddress) {
526
+ throw new Error(`Could not find program address at index ${instruction.programAddressIndex}`);
527
+ }
528
+ const accounts = instruction.accountIndices?.map((accountIndex) => accountMetas[accountIndex]);
529
+ const { data } = instruction;
530
+ return {
531
+ programAddress,
532
+ ...accounts && accounts.length ? { accounts } : {},
533
+ ...data && data.length ? { data } : {}
534
+ };
535
+ }
536
+ function getLifetimeConstraint(messageLifetimeToken, firstInstruction, lastValidBlockHeight) {
537
+ if (!firstInstruction || !isAdvanceNonceAccountInstruction(firstInstruction)) {
538
+ return {
539
+ blockhash: messageLifetimeToken,
540
+ lastValidBlockHeight: lastValidBlockHeight ?? 2n ** 64n - 1n
541
+ // U64 MAX
542
+ };
543
+ } else {
544
+ const nonceAccountAddress = firstInstruction.accounts[0].address;
545
+ assertIsAddress(nonceAccountAddress);
546
+ const nonceAuthorityAddress = firstInstruction.accounts[2].address;
547
+ assertIsAddress(nonceAuthorityAddress);
548
+ return {
549
+ nonce: messageLifetimeToken,
550
+ nonceAccountAddress,
551
+ nonceAuthorityAddress
552
+ };
553
+ }
554
+ }
555
+ function convertSignatures(compiledTransaction) {
556
+ const {
557
+ compiledMessage: { staticAccounts },
558
+ signatures
559
+ } = compiledTransaction;
560
+ return signatures.reduce((acc, sig, index) => {
561
+ const allZeros = sig.every((byte) => byte === 0);
562
+ if (allZeros)
563
+ return acc;
564
+ const address = staticAccounts[index];
565
+ return { ...acc, [address]: sig };
566
+ }, {});
567
+ }
568
+ function decompileTransaction(compiledTransaction, lastValidBlockHeight) {
569
+ const { compiledMessage } = compiledTransaction;
570
+ if ("addressTableLookups" in compiledMessage && compiledMessage.addressTableLookups.length > 0) {
571
+ throw new Error("Cannot convert transaction with addressTableLookups");
572
+ }
573
+ const feePayer = compiledMessage.staticAccounts[0];
574
+ if (!feePayer)
575
+ throw new Error("No fee payer set in CompiledTransaction");
576
+ const accountMetas = getAccountMetas(compiledMessage);
577
+ const instructions = compiledMessage.instructions.map(
578
+ (compiledInstruction) => convertInstruction(compiledInstruction, accountMetas)
579
+ );
580
+ const firstInstruction = instructions[0];
581
+ const lifetimeConstraint = getLifetimeConstraint(
582
+ compiledMessage.lifetimeToken,
583
+ firstInstruction,
584
+ lastValidBlockHeight
585
+ );
586
+ const signatures = convertSignatures(compiledTransaction);
587
+ return pipe(
588
+ createTransaction({ version: compiledMessage.version }),
589
+ (tx) => setTransactionFeePayer(feePayer, tx),
590
+ (tx) => instructions.reduce((acc, instruction) => {
591
+ return appendTransactionInstruction(instruction, acc);
592
+ }, tx),
593
+ (tx) => "blockhash" in lifetimeConstraint ? setTransactionLifetimeUsingBlockhash(lifetimeConstraint, tx) : setTransactionLifetimeUsingDurableNonce(lifetimeConstraint, tx),
594
+ (tx) => compiledTransaction.signatures.length ? { ...tx, signatures } : tx
595
+ );
596
+ }
467
597
  var lookupTableAddressDescription = __DEV__ ? "The address of the address lookup table account from which instruction addresses should be looked up" : "lookupTableAddress";
468
598
  var writableIndicesDescription = __DEV__ ? "The indices of the accounts in the lookup table that should be loaded as writeable" : "writableIndices";
469
599
  var readableIndicesDescription = __DEV__ ? "The indices of the accounts in the lookup table that should be loaded as read-only" : "readableIndices";
@@ -494,6 +624,32 @@ function getAddressTableLookupEncoder() {
494
624
  }
495
625
  return memoizedAddressTableLookupEncoder;
496
626
  }
627
+ var memoizedAddressTableLookupDecoder;
628
+ function getAddressTableLookupDecoder() {
629
+ if (!memoizedAddressTableLookupDecoder) {
630
+ memoizedAddressTableLookupDecoder = getStructDecoder(
631
+ [
632
+ ["lookupTableAddress", getAddressDecoder({ description: lookupTableAddressDescription })],
633
+ [
634
+ "writableIndices",
635
+ getArrayDecoder(getU8Decoder(), {
636
+ description: writableIndicesDescription,
637
+ size: getShortU16Decoder()
638
+ })
639
+ ],
640
+ [
641
+ "readableIndices",
642
+ getArrayDecoder(getU8Decoder(), {
643
+ description: readableIndicesDescription,
644
+ size: getShortU16Decoder()
645
+ })
646
+ ]
647
+ ],
648
+ { description: addressTableLookupDescription }
649
+ );
650
+ }
651
+ return memoizedAddressTableLookupDecoder;
652
+ }
497
653
  var memoizedU8Encoder;
498
654
  function getMemoizedU8Encoder() {
499
655
  if (!memoizedU8Encoder)
@@ -507,6 +663,19 @@ function getMemoizedU8EncoderDescription(description) {
507
663
  description: description ?? encoder.description
508
664
  };
509
665
  }
666
+ var memoizedU8Decoder;
667
+ function getMemoizedU8Decoder() {
668
+ if (!memoizedU8Decoder)
669
+ memoizedU8Decoder = getU8Decoder();
670
+ return memoizedU8Decoder;
671
+ }
672
+ function getMemoizedU8DecoderDescription(description) {
673
+ const decoder = getMemoizedU8Decoder();
674
+ return {
675
+ ...decoder,
676
+ description: description ?? decoder.description
677
+ };
678
+ }
510
679
  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
680
  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
681
  var numReadonlyNonSignerAccountsDescription = __DEV__ ? "The expected number of addresses in the static address list belonging to accounts that are neither signers, nor writable" : void 0;
@@ -523,6 +692,18 @@ function getMessageHeaderEncoder() {
523
692
  }
524
693
  );
525
694
  }
695
+ function getMessageHeaderDecoder() {
696
+ return getStructDecoder(
697
+ [
698
+ ["numSignerAccounts", getMemoizedU8DecoderDescription(numSignerAccountsDescription)],
699
+ ["numReadonlySignerAccounts", getMemoizedU8DecoderDescription(numReadonlySignerAccountsDescription)],
700
+ ["numReadonlyNonSignerAccounts", getMemoizedU8DecoderDescription(numReadonlyNonSignerAccountsDescription)]
701
+ ],
702
+ {
703
+ description: messageHeaderDescription
704
+ }
705
+ );
706
+ }
526
707
  var programAddressIndexDescription = __DEV__ ? "The index of the program being called, according to the well-ordered accounts list for this transaction" : "programAddressIndex";
527
708
  var accountIndexDescription = __DEV__ ? "The index of an account, according to the well-ordered accounts list for this transaction" : void 0;
528
709
  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";
@@ -557,12 +738,52 @@ function getInstructionEncoder() {
557
738
  }
558
739
  return memoizedGetInstructionEncoder;
559
740
  }
741
+ var memoizedGetInstructionDecoder;
742
+ function getInstructionDecoder() {
743
+ if (!memoizedGetInstructionDecoder) {
744
+ memoizedGetInstructionDecoder = mapDecoder(
745
+ getStructDecoder([
746
+ ["programAddressIndex", getU8Decoder({ description: programAddressIndexDescription })],
747
+ [
748
+ "accountIndices",
749
+ getArrayDecoder(getU8Decoder({ description: accountIndexDescription }), {
750
+ description: accountIndicesDescription,
751
+ size: getShortU16Decoder()
752
+ })
753
+ ],
754
+ ["data", getBytesDecoder({ description: dataDescription, size: getShortU16Decoder() })]
755
+ ]),
756
+ // Convert an instruction to exclude optional fields if they are empty
757
+ (instruction) => {
758
+ if (instruction.accountIndices.length && instruction.data.byteLength) {
759
+ return instruction;
760
+ }
761
+ const { accountIndices, data, ...rest } = instruction;
762
+ return {
763
+ ...rest,
764
+ ...accountIndices.length ? { accountIndices } : null,
765
+ ...data.byteLength ? { data } : null
766
+ };
767
+ }
768
+ );
769
+ }
770
+ return memoizedGetInstructionDecoder;
771
+ }
560
772
  var VERSION_FLAG_MASK = 128;
561
773
  var BASE_CONFIG = {
562
774
  description: __DEV__ ? "A single byte that encodes the version of the transaction" : "",
563
775
  fixedSize: null,
564
776
  maxSize: 1
565
777
  };
778
+ function decode(bytes, offset = 0) {
779
+ const firstByte = bytes[offset];
780
+ if ((firstByte & VERSION_FLAG_MASK) === 0) {
781
+ return ["legacy", offset];
782
+ } else {
783
+ const version = firstByte ^ VERSION_FLAG_MASK;
784
+ return [version, offset + 1];
785
+ }
786
+ }
566
787
  function encode(value) {
567
788
  if (value === "legacy") {
568
789
  return new Uint8Array();
@@ -572,6 +793,12 @@ function encode(value) {
572
793
  }
573
794
  return new Uint8Array([value | VERSION_FLAG_MASK]);
574
795
  }
796
+ function getTransactionVersionDecoder() {
797
+ return {
798
+ ...BASE_CONFIG,
799
+ decode
800
+ };
801
+ }
575
802
  function getTransactionVersionEncoder() {
576
803
  return {
577
804
  ...BASE_CONFIG,
@@ -632,12 +859,47 @@ function getPreludeStructEncoderTuple() {
632
859
  ]
633
860
  ];
634
861
  }
862
+ function getPreludeStructDecoderTuple() {
863
+ return [
864
+ ["version", getTransactionVersionDecoder()],
865
+ ["header", getMessageHeaderDecoder()],
866
+ [
867
+ "staticAccounts",
868
+ getArrayDecoder(getAddressDecoder(), {
869
+ description: staticAccountsDescription,
870
+ size: getShortU16Decoder()
871
+ })
872
+ ],
873
+ [
874
+ "lifetimeToken",
875
+ getStringDecoder({
876
+ description: lifetimeTokenDescription,
877
+ encoding: getBase58Decoder(),
878
+ size: 32
879
+ })
880
+ ],
881
+ [
882
+ "instructions",
883
+ getArrayDecoder(getInstructionDecoder(), {
884
+ description: instructionsDescription,
885
+ size: getShortU16Decoder()
886
+ })
887
+ ],
888
+ ["addressTableLookups", getAddressTableLookupArrayDecoder()]
889
+ ];
890
+ }
635
891
  function getAddressTableLookupArrayEncoder() {
636
892
  return getArrayEncoder(getAddressTableLookupEncoder(), {
637
893
  description: addressTableLookupsDescription,
638
894
  size: getShortU16Encoder()
639
895
  });
640
896
  }
897
+ function getAddressTableLookupArrayDecoder() {
898
+ return getArrayDecoder(getAddressTableLookupDecoder(), {
899
+ description: addressTableLookupsDescription,
900
+ size: getShortU16Decoder()
901
+ });
902
+ }
641
903
  var messageDescription = __DEV__ ? "The wire format of a Solana transaction message" : "message";
642
904
  function getCompiledMessageEncoder() {
643
905
  return {
@@ -653,10 +915,23 @@ function getCompiledMessageEncoder() {
653
915
  maxSize: null
654
916
  };
655
917
  }
918
+ function getCompiledMessageDecoder() {
919
+ return mapDecoder(
920
+ getStructDecoder(getPreludeStructDecoderTuple(), {
921
+ description: messageDescription
922
+ }),
923
+ ({ addressTableLookups, ...restOfMessage }) => {
924
+ if (restOfMessage.version === "legacy" || !addressTableLookups?.length) {
925
+ return restOfMessage;
926
+ }
927
+ return { ...restOfMessage, addressTableLookups };
928
+ }
929
+ );
930
+ }
656
931
 
657
932
  // src/serializers/transaction.ts
658
- var transactionDescription = __DEV__ ? "The wire format of a Solana transaction" : "transaction";
659
933
  var signaturesDescription = __DEV__ ? "A compact array of 64-byte, base-64 encoded Ed25519 signatures" : "signatures";
934
+ var transactionDescription = __DEV__ ? "The wire format of a Solana transaction" : "transaction";
660
935
  function getCompiledTransactionEncoder() {
661
936
  return getStructEncoder(
662
937
  [
@@ -674,68 +949,58 @@ function getCompiledTransactionEncoder() {
674
949
  }
675
950
  );
676
951
  }
677
- function getTransactionEncoder() {
678
- return mapEncoder(getCompiledTransactionEncoder(), getCompiledTransaction);
952
+ function getSignatureDecoder() {
953
+ return mapDecoder(getBytesDecoder({ size: 64 }), (bytes) => bytes);
679
954
  }
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}`);
955
+ function getCompiledTransactionDecoder() {
956
+ return getStructDecoder(
957
+ [
958
+ [
959
+ "signatures",
960
+ getArrayDecoder(getSignatureDecoder(), {
961
+ description: signaturesDescription,
962
+ size: getShortU16Decoder()
963
+ })
964
+ ],
965
+ ["compiledMessage", getCompiledMessageDecoder()]
966
+ ],
967
+ {
968
+ description: transactionDescription
693
969
  }
694
- } catch (e) {
695
- throw new Error(`\`${putativeTransactionSignature}\` is not a transaction signature`, {
696
- cause: e
697
- });
698
- }
970
+ );
699
971
  }
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;
972
+ function getTransactionEncoder() {
973
+ return mapEncoder(getCompiledTransactionEncoder(), getCompiledTransaction);
714
974
  }
715
- async function getCompiledMessageSignature(message, secretKey) {
716
- const wireMessageBytes = getCompiledMessageEncoder().encode(message);
717
- const signature = await signBytes(secretKey, wireMessageBytes);
718
- return signature;
975
+ function getTransactionDecoder(lastValidBlockHeight) {
976
+ return mapDecoder(
977
+ getCompiledTransactionDecoder(),
978
+ (compiledTransaction) => decompileTransaction(compiledTransaction, lastValidBlockHeight)
979
+ );
980
+ }
981
+ function getTransactionCodec(lastValidBlockHeight) {
982
+ return combineCodec(getTransactionEncoder(), getTransactionDecoder(lastValidBlockHeight));
719
983
  }
984
+ var base58Decoder;
720
985
  function getSignatureFromTransaction(transaction) {
986
+ if (!base58Decoder)
987
+ base58Decoder = getBase58Decoder();
721
988
  const signatureBytes = transaction.signatures[transaction.feePayer];
722
989
  if (!signatureBytes) {
723
990
  throw new Error(
724
991
  "Could not determine this transaction's signature. Make sure that the transaction has been signed by its fee payer."
725
992
  );
726
993
  }
727
- const transactionSignature2 = base58.deserialize(signatureBytes)[0];
728
- return transactionSignature2;
994
+ const transactionSignature = base58Decoder.decode(signatureBytes)[0];
995
+ return transactionSignature;
729
996
  }
730
- async function signTransaction(keyPairs, transaction) {
997
+ async function partiallySignTransaction(keyPairs, transaction) {
731
998
  const compiledMessage = compileMessage(transaction);
732
999
  const nextSignatures = "signatures" in transaction ? { ...transaction.signatures } : {};
1000
+ const wireMessageBytes = getCompiledMessageEncoder().encode(compiledMessage);
733
1001
  const publicKeySignaturePairs = await Promise.all(
734
1002
  keyPairs.map(
735
- (keyPair) => Promise.all([
736
- getAddressFromPublicKey(keyPair.publicKey),
737
- getCompiledMessageSignature(compiledMessage, keyPair.privateKey)
738
- ])
1003
+ (keyPair) => Promise.all([getAddressFromPublicKey(keyPair.publicKey), signBytes(keyPair.privateKey, wireMessageBytes)])
739
1004
  )
740
1005
  );
741
1006
  for (const [signerPublicKey, signature] of publicKeySignaturePairs) {
@@ -748,9 +1013,20 @@ async function signTransaction(keyPairs, transaction) {
748
1013
  Object.freeze(out);
749
1014
  return out;
750
1015
  }
751
- function transactionSignature(putativeTransactionSignature) {
752
- assertIsTransactionSignature(putativeTransactionSignature);
753
- return putativeTransactionSignature;
1016
+ async function signTransaction(keyPairs, transaction) {
1017
+ const out = await partiallySignTransaction(keyPairs, transaction);
1018
+ assertTransactionIsFullySigned(out);
1019
+ Object.freeze(out);
1020
+ return out;
1021
+ }
1022
+ function assertTransactionIsFullySigned(transaction) {
1023
+ const signerAddressesFromInstructions = transaction.instructions.flatMap((i) => i.accounts?.filter((a) => isSignerRole(a.role)) ?? []).map((a) => a.address);
1024
+ const requiredSigners = /* @__PURE__ */ new Set([transaction.feePayer, ...signerAddressesFromInstructions]);
1025
+ requiredSigners.forEach((address) => {
1026
+ if (!transaction.signatures[address]) {
1027
+ throw new Error(`Transaction is missing signature for address \`${address}\``);
1028
+ }
1029
+ });
754
1030
  }
755
1031
 
756
1032
  // src/wire-transaction.ts
@@ -761,6 +1037,6 @@ function getBase64EncodedWireTransaction(transaction) {
761
1037
  }
762
1038
  }
763
1039
 
764
- export { appendTransactionInstruction, assertIsBlockhash, assertIsDurableNonceTransaction, assertIsTransactionSignature, createTransaction, getBase64EncodedWireTransaction, getSignatureFromTransaction, getTransactionEncoder, isAdvanceNonceAccountInstruction, isTransactionSignature, prependTransactionInstruction, setTransactionFeePayer, setTransactionLifetimeUsingBlockhash, setTransactionLifetimeUsingDurableNonce, signTransaction, transactionSignature };
1040
+ export { appendTransactionInstruction, assertIsBlockhash, assertIsDurableNonceTransaction, assertTransactionIsFullySigned, createTransaction, getBase64EncodedWireTransaction, getSignatureFromTransaction, getTransactionCodec, getTransactionDecoder, getTransactionEncoder, isAdvanceNonceAccountInstruction, partiallySignTransaction, prependTransactionInstruction, setTransactionFeePayer, setTransactionLifetimeUsingBlockhash, setTransactionLifetimeUsingDurableNonce, signTransaction };
765
1041
  //# sourceMappingURL=out.js.map
766
1042
  //# sourceMappingURL=index.browser.js.map