@solana/transactions 2.0.0-experimental.fc4e943 → 2.0.0-experimental.fd11bd1

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,8 +1,9 @@
1
- import { base58, struct, array, bytes, shortU16, mapSerializer, string, u8 } from '@metaplex-foundation/umi-serializers';
2
- import { getAddressFromPublicKey, getAddressComparator, getAddressCodec } from '@solana/addresses';
3
- import { getStructCodec } from '@solana/codecs-data-structures';
4
- import { getU8Codec } from '@solana/codecs-numbers';
5
- import { combineCodec } from '@solana/codecs-core';
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';
6
7
  import { signBytes } from '@solana/keys';
7
8
 
8
9
  // ../build-scripts/env-shim.ts
@@ -23,7 +24,10 @@ function getUnsignedTransaction(transaction) {
23
24
  }
24
25
 
25
26
  // src/blockhash.ts
27
+ var base58Encoder;
26
28
  function assertIsBlockhash(putativeBlockhash) {
29
+ if (!base58Encoder)
30
+ base58Encoder = getBase58Encoder();
27
31
  try {
28
32
  if (
29
33
  // Lowest value (32 bytes of zeroes)
@@ -32,8 +36,8 @@ function assertIsBlockhash(putativeBlockhash) {
32
36
  ) {
33
37
  throw new Error("Expected input string to decode to a byte array of length 32.");
34
38
  }
35
- const bytes3 = base58.serialize(putativeBlockhash);
36
- const numBytes = bytes3.byteLength;
39
+ const bytes = base58Encoder.encode(putativeBlockhash);
40
+ const numBytes = bytes.byteLength;
37
41
  if (numBytes !== 32) {
38
42
  throw new Error(`Expected input string to decode to a byte array of length 32. Actual length: ${numBytes}`);
39
43
  }
@@ -118,7 +122,7 @@ function isAdvanceNonceAccountInstruction(instruction) {
118
122
  instruction.accounts?.length === 3 && // First account is nonce account address
119
123
  instruction.accounts[0].address != null && instruction.accounts[0].role === AccountRole.WRITABLE && // Second account is recent blockhashes sysvar
120
124
  instruction.accounts[1].address === RECENT_BLOCKHASHES_SYSVAR_ADDRESS && instruction.accounts[1].role === AccountRole.READONLY && // Third account is nonce authority account
121
- instruction.accounts[2].address != null && instruction.accounts[2].role === AccountRole.READONLY_SIGNER;
125
+ instruction.accounts[2].address != null && isSignerRole(instruction.accounts[2].role);
122
126
  }
123
127
  function isAdvanceNonceAccountInstructionData(data) {
124
128
  return data.byteLength === 4 && data[0] === 4 && data[1] === 0 && data[2] === 0 && data[3] === 0;
@@ -126,21 +130,38 @@ function isAdvanceNonceAccountInstructionData(data) {
126
130
  function isDurableNonceTransaction(transaction) {
127
131
  return "lifetimeConstraint" in transaction && typeof transaction.lifetimeConstraint.nonce === "string" && transaction.instructions[0] != null && isAdvanceNonceAccountInstruction(transaction.instructions[0]);
128
132
  }
133
+ function isAdvanceNonceAccountInstructionForNonce(instruction, nonceAccountAddress, nonceAuthorityAddress) {
134
+ return instruction.accounts[0].address === nonceAccountAddress && instruction.accounts[2].address === nonceAuthorityAddress;
135
+ }
129
136
  function setTransactionLifetimeUsingDurableNonce({
130
137
  nonce,
131
138
  nonceAccountAddress,
132
139
  nonceAuthorityAddress
133
140
  }, transaction) {
134
- const isAlreadyDurableNonceTransaction = isDurableNonceTransaction(transaction);
135
- if (isAlreadyDurableNonceTransaction && transaction.lifetimeConstraint.nonce === nonce && transaction.instructions[0].accounts[0].address === nonceAccountAddress && transaction.instructions[0].accounts[2].address === nonceAuthorityAddress) {
136
- 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
+ ];
137
161
  }
138
162
  const out = {
139
163
  ...getUnsignedTransaction(transaction),
140
- instructions: [
141
- createAdvanceNonceAccountInstruction(nonceAccountAddress, nonceAuthorityAddress),
142
- ...isAlreadyDurableNonceTransaction ? transaction.instructions.slice(1) : transaction.instructions
143
- ],
164
+ instructions: newInstructions,
144
165
  lifetimeConstraint: {
145
166
  nonce
146
167
  }
@@ -463,142 +484,299 @@ function getCompiledTransaction(transaction) {
463
484
  signatures
464
485
  };
465
486
  }
466
- function addressSerializerCompat(compat) {
467
- const codec = getAddressCodec();
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;
468
530
  return {
469
- description: compat?.description ?? codec.description,
470
- deserialize: codec.decode,
471
- fixedSize: codec.fixedSize,
472
- maxSize: codec.maxSize,
473
- serialize: codec.encode
531
+ programAddress,
532
+ ...accounts && accounts.length ? { accounts } : {},
533
+ ...data && data.length ? { data } : {}
474
534
  };
475
535
  }
476
- function getAddressTableLookupCodec() {
477
- return struct(
478
- [
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
+ }
597
+ var lookupTableAddressDescription = __DEV__ ? "The address of the address lookup table account from which instruction addresses should be looked up" : "lookupTableAddress";
598
+ var writableIndicesDescription = __DEV__ ? "The indices of the accounts in the lookup table that should be loaded as writeable" : "writableIndices";
599
+ var readableIndicesDescription = __DEV__ ? "The indices of the accounts in the lookup table that should be loaded as read-only" : "readableIndices";
600
+ var addressTableLookupDescription = __DEV__ ? "A pointer to the address of an address lookup table, along with the readonly/writeable indices of the addresses that should be loaded from it" : "addressTableLookup";
601
+ var memoizedAddressTableLookupEncoder;
602
+ function getAddressTableLookupEncoder() {
603
+ if (!memoizedAddressTableLookupEncoder) {
604
+ memoizedAddressTableLookupEncoder = getStructEncoder(
479
605
  [
480
- "lookupTableAddress",
481
- addressSerializerCompat(
482
- __DEV__ ? {
483
- description: "The address of the address lookup table account from which instruction addresses should be looked up"
484
- } : void 0
485
- )
606
+ ["lookupTableAddress", getAddressEncoder({ description: lookupTableAddressDescription })],
607
+ [
608
+ "writableIndices",
609
+ getArrayEncoder(getU8Encoder(), {
610
+ description: writableIndicesDescription,
611
+ size: getShortU16Encoder()
612
+ })
613
+ ],
614
+ [
615
+ "readableIndices",
616
+ getArrayEncoder(getU8Encoder(), {
617
+ description: readableIndicesDescription,
618
+ size: getShortU16Encoder()
619
+ })
620
+ ]
486
621
  ],
622
+ { description: addressTableLookupDescription }
623
+ );
624
+ }
625
+ return memoizedAddressTableLookupEncoder;
626
+ }
627
+ var memoizedAddressTableLookupDecoder;
628
+ function getAddressTableLookupDecoder() {
629
+ if (!memoizedAddressTableLookupDecoder) {
630
+ memoizedAddressTableLookupDecoder = getStructDecoder(
487
631
  [
488
- "writableIndices",
489
- array(u8(), {
490
- ...__DEV__ ? {
491
- description: "The indices of the accounts in the lookup table that should be loaded as writeable"
492
- } : null,
493
- size: shortU16()
494
- })
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
+ ]
495
647
  ],
496
- [
497
- "readableIndices",
498
- array(u8(), {
499
- ...__DEV__ ? {
500
- description: "The indices of the accounts in the lookup table that should be loaded as read-only"
501
- } : void 0,
502
- size: shortU16()
503
- })
504
- ]
505
- ],
506
- __DEV__ ? {
507
- description: "A pointer to the address of an address lookup table, along with the readonly/writeable indices of the addresses that should be loaded from it"
508
- } : void 0
509
- );
648
+ { description: addressTableLookupDescription }
649
+ );
650
+ }
651
+ return memoizedAddressTableLookupDecoder;
652
+ }
653
+ var memoizedU8Encoder;
654
+ function getMemoizedU8Encoder() {
655
+ if (!memoizedU8Encoder)
656
+ memoizedU8Encoder = getU8Encoder();
657
+ return memoizedU8Encoder;
658
+ }
659
+ function getMemoizedU8EncoderDescription(description) {
660
+ const encoder = getMemoizedU8Encoder();
661
+ return {
662
+ ...encoder,
663
+ description: description ?? encoder.description
664
+ };
510
665
  }
511
- var memoizedU8Codec;
512
- function getMemoizedU8Codec() {
513
- if (!memoizedU8Codec)
514
- memoizedU8Codec = getU8Codec();
515
- return memoizedU8Codec;
666
+ var memoizedU8Decoder;
667
+ function getMemoizedU8Decoder() {
668
+ if (!memoizedU8Decoder)
669
+ memoizedU8Decoder = getU8Decoder();
670
+ return memoizedU8Decoder;
516
671
  }
517
- function getMemoizedU8CodecDescription(description) {
518
- const codec = getMemoizedU8Codec();
672
+ function getMemoizedU8DecoderDescription(description) {
673
+ const decoder = getMemoizedU8Decoder();
519
674
  return {
520
- ...codec,
521
- description: description ?? codec.description
675
+ ...decoder,
676
+ description: description ?? decoder.description
522
677
  };
523
678
  }
524
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;
525
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;
526
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;
527
682
  var messageHeaderDescription = __DEV__ ? "The transaction message header containing counts of the signer, readonly-signer, and readonly-nonsigner account addresses" : void 0;
528
- function getMessageHeaderCodec() {
529
- return getStructCodec(
683
+ function getMessageHeaderEncoder() {
684
+ return getStructEncoder(
530
685
  [
531
- ["numSignerAccounts", getMemoizedU8CodecDescription(numSignerAccountsDescription)],
532
- ["numReadonlySignerAccounts", getMemoizedU8CodecDescription(numReadonlySignerAccountsDescription)],
533
- ["numReadonlyNonSignerAccounts", getMemoizedU8CodecDescription(numReadonlyNonSignerAccountsDescription)]
686
+ ["numSignerAccounts", getMemoizedU8EncoderDescription(numSignerAccountsDescription)],
687
+ ["numReadonlySignerAccounts", getMemoizedU8EncoderDescription(numReadonlySignerAccountsDescription)],
688
+ ["numReadonlyNonSignerAccounts", getMemoizedU8EncoderDescription(numReadonlyNonSignerAccountsDescription)]
534
689
  ],
535
690
  {
536
691
  description: messageHeaderDescription
537
692
  }
538
693
  );
539
694
  }
540
- function getInstructionCodec() {
541
- return mapSerializer(
542
- struct([
543
- [
544
- "programAddressIndex",
545
- u8(
546
- __DEV__ ? {
547
- description: "The index of the program being called, according to the well-ordered accounts list for this transaction"
548
- } : void 0
549
- )
550
- ],
551
- [
552
- "accountIndices",
553
- array(
554
- u8({
555
- description: __DEV__ ? "The index of an account, according to the well-ordered accounts list for this transaction" : ""
556
- }),
557
- {
558
- description: __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" : "",
559
- size: shortU16()
560
- }
561
- )
562
- ],
563
- [
564
- "data",
565
- bytes({
566
- description: __DEV__ ? "An optional buffer of data passed to the instruction" : "",
567
- size: shortU16()
568
- })
569
- ]
570
- ]),
571
- (value) => {
572
- if (value.accountIndices !== void 0 && value.data !== void 0) {
573
- return value;
574
- }
575
- return {
576
- ...value,
577
- accountIndices: value.accountIndices ?? [],
578
- data: value.data ?? new Uint8Array(0)
579
- };
580
- },
581
- (value) => {
582
- if (value.accountIndices.length && value.data.byteLength) {
583
- return value;
584
- }
585
- const { accountIndices, data, ...rest } = value;
586
- return {
587
- ...rest,
588
- ...accountIndices.length ? { accountIndices } : null,
589
- ...data.byteLength ? { data } : null
590
- };
695
+ function getMessageHeaderDecoder() {
696
+ return getStructDecoder(
697
+ [
698
+ ["numSignerAccounts", getMemoizedU8DecoderDescription(numSignerAccountsDescription)],
699
+ ["numReadonlySignerAccounts", getMemoizedU8DecoderDescription(numReadonlySignerAccountsDescription)],
700
+ ["numReadonlyNonSignerAccounts", getMemoizedU8DecoderDescription(numReadonlyNonSignerAccountsDescription)]
701
+ ],
702
+ {
703
+ description: messageHeaderDescription
591
704
  }
592
705
  );
593
706
  }
707
+ var programAddressIndexDescription = __DEV__ ? "The index of the program being called, according to the well-ordered accounts list for this transaction" : "programAddressIndex";
708
+ var accountIndexDescription = __DEV__ ? "The index of an account, according to the well-ordered accounts list for this transaction" : void 0;
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";
710
+ var dataDescription = __DEV__ ? "An optional buffer of data passed to the instruction" : "data";
711
+ var memoizedGetInstructionEncoder;
712
+ function getInstructionEncoder() {
713
+ if (!memoizedGetInstructionEncoder) {
714
+ memoizedGetInstructionEncoder = mapEncoder(
715
+ getStructEncoder([
716
+ ["programAddressIndex", getU8Encoder({ description: programAddressIndexDescription })],
717
+ [
718
+ "accountIndices",
719
+ getArrayEncoder(getU8Encoder({ description: accountIndexDescription }), {
720
+ description: accountIndicesDescription,
721
+ size: getShortU16Encoder()
722
+ })
723
+ ],
724
+ ["data", getBytesEncoder({ description: dataDescription, size: getShortU16Encoder() })]
725
+ ]),
726
+ // Convert an instruction to have all fields defined
727
+ (instruction) => {
728
+ if (instruction.accountIndices !== void 0 && instruction.data !== void 0) {
729
+ return instruction;
730
+ }
731
+ return {
732
+ ...instruction,
733
+ accountIndices: instruction.accountIndices ?? [],
734
+ data: instruction.data ?? new Uint8Array(0)
735
+ };
736
+ }
737
+ );
738
+ }
739
+ return memoizedGetInstructionEncoder;
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
+ }
594
772
  var VERSION_FLAG_MASK = 128;
595
773
  var BASE_CONFIG = {
596
774
  description: __DEV__ ? "A single byte that encodes the version of the transaction" : "",
597
775
  fixedSize: null,
598
776
  maxSize: 1
599
777
  };
600
- function decode(bytes3, offset = 0) {
601
- const firstByte = bytes3[offset];
778
+ function decode(bytes, offset = 0) {
779
+ const firstByte = bytes[offset];
602
780
  if ((firstByte & VERSION_FLAG_MASK) === 0) {
603
781
  return ["legacy", offset];
604
782
  } else {
@@ -627,128 +805,187 @@ function getTransactionVersionEncoder() {
627
805
  encode
628
806
  };
629
807
  }
630
- function getTransactionVersionCodec() {
631
- return combineCodec(getTransactionVersionEncoder(), getTransactionVersionDecoder());
632
- }
633
-
634
- // src/serializers/unimplemented.ts
635
- function getError(type, name) {
636
- const functionSuffix = name + type[0].toUpperCase() + type.slice(1);
637
- return new Error(
638
- `No ${type} exists for ${name}. Use \`get${functionSuffix}()\` if you need a ${type}, and \`get${name}Codec()\` if you need to both encode and decode ${name}`
639
- );
640
- }
641
- function getUnimplementedDecoder(name) {
642
- return () => {
643
- throw getError("decoder", name);
644
- };
645
- }
646
808
 
647
809
  // src/serializers/message.ts
648
- var BASE_CONFIG2 = {
649
- description: __DEV__ ? "The wire format of a Solana transaction message" : "",
650
- fixedSize: null,
651
- maxSize: null
652
- };
653
- function serialize(compiledMessage) {
654
- if (compiledMessage.version === "legacy") {
655
- return struct(getPreludeStructSerializerTuple()).serialize(compiledMessage);
656
- } else {
657
- return mapSerializer(
658
- struct([
659
- ...getPreludeStructSerializerTuple(),
660
- ["addressTableLookups", getAddressTableLookupsSerializer()]
661
- ]),
662
- (value) => {
663
- if (value.version === "legacy") {
664
- return value;
665
- }
666
- return {
667
- ...value,
668
- addressTableLookups: value.addressTableLookups ?? []
669
- };
670
- }
671
- ).serialize(compiledMessage);
672
- }
810
+ var staticAccountsDescription = __DEV__ ? "A compact-array of static account addresses belonging to this transaction" : "staticAccounts";
811
+ var lifetimeTokenDescription = __DEV__ ? "A 32-byte token that specifies the lifetime of this transaction (eg. a recent blockhash, or a durable nonce)" : "lifetimeToken";
812
+ var instructionsDescription = __DEV__ ? "A compact-array of instructions belonging to this transaction" : "instructions";
813
+ var addressTableLookupsDescription = __DEV__ ? "A compact array of address table lookups belonging to this transaction" : "addressTableLookups";
814
+ function getCompiledMessageLegacyEncoder() {
815
+ return getStructEncoder(getPreludeStructEncoderTuple());
673
816
  }
674
- function toSerializer(codec) {
675
- return {
676
- description: codec.description,
677
- deserialize: codec.decode,
678
- fixedSize: codec.fixedSize,
679
- maxSize: codec.maxSize,
680
- serialize: codec.encode
681
- };
817
+ function getCompiledMessageVersionedEncoder() {
818
+ return mapEncoder(
819
+ getStructEncoder([
820
+ ...getPreludeStructEncoderTuple(),
821
+ ["addressTableLookups", getAddressTableLookupArrayEncoder()]
822
+ ]),
823
+ (value) => {
824
+ if (value.version === "legacy") {
825
+ return value;
826
+ }
827
+ return {
828
+ ...value,
829
+ addressTableLookups: value.addressTableLookups ?? []
830
+ };
831
+ }
832
+ );
682
833
  }
683
- function getPreludeStructSerializerTuple() {
834
+ function getPreludeStructEncoderTuple() {
684
835
  return [
685
- ["version", toSerializer(getTransactionVersionCodec())],
686
- ["header", toSerializer(getMessageHeaderCodec())],
836
+ ["version", getTransactionVersionEncoder()],
837
+ ["header", getMessageHeaderEncoder()],
687
838
  [
688
839
  "staticAccounts",
689
- array(toSerializer(getAddressCodec()), {
690
- description: __DEV__ ? "A compact-array of static account addresses belonging to this transaction" : "",
691
- size: shortU16()
840
+ getArrayEncoder(getAddressEncoder(), {
841
+ description: staticAccountsDescription,
842
+ size: getShortU16Encoder()
692
843
  })
693
844
  ],
694
845
  [
695
846
  "lifetimeToken",
696
- string({
697
- description: __DEV__ ? "A 32-byte token that specifies the lifetime of this transaction (eg. a recent blockhash, or a durable nonce)" : "",
698
- encoding: base58,
847
+ getStringEncoder({
848
+ description: lifetimeTokenDescription,
849
+ encoding: getBase58Encoder(),
699
850
  size: 32
700
851
  })
701
852
  ],
702
853
  [
703
854
  "instructions",
704
- array(getInstructionCodec(), {
705
- description: __DEV__ ? "A compact-array of instructions belonging to this transaction" : "",
706
- size: shortU16()
855
+ getArrayEncoder(getInstructionEncoder(), {
856
+ description: instructionsDescription,
857
+ size: getShortU16Encoder()
707
858
  })
708
859
  ]
709
860
  ];
710
861
  }
711
- function getAddressTableLookupsSerializer() {
712
- return array(getAddressTableLookupCodec(), {
713
- ...__DEV__ ? { description: "A compact array of address table lookups belonging to this transaction" } : null,
714
- size: shortU16()
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
+ }
891
+ function getAddressTableLookupArrayEncoder() {
892
+ return getArrayEncoder(getAddressTableLookupEncoder(), {
893
+ description: addressTableLookupsDescription,
894
+ size: getShortU16Encoder()
715
895
  });
716
896
  }
897
+ function getAddressTableLookupArrayDecoder() {
898
+ return getArrayDecoder(getAddressTableLookupDecoder(), {
899
+ description: addressTableLookupsDescription,
900
+ size: getShortU16Decoder()
901
+ });
902
+ }
903
+ var messageDescription = __DEV__ ? "The wire format of a Solana transaction message" : "message";
717
904
  function getCompiledMessageEncoder() {
718
905
  return {
719
- ...BASE_CONFIG2,
720
- deserialize: getUnimplementedDecoder("CompiledMessage"),
721
- serialize
906
+ description: messageDescription,
907
+ encode: (compiledMessage) => {
908
+ if (compiledMessage.version === "legacy") {
909
+ return getCompiledMessageLegacyEncoder().encode(compiledMessage);
910
+ } else {
911
+ return getCompiledMessageVersionedEncoder().encode(compiledMessage);
912
+ }
913
+ },
914
+ fixedSize: null,
915
+ maxSize: null
722
916
  };
723
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
+ }
724
931
 
725
932
  // src/serializers/transaction.ts
726
- var BASE_CONFIG3 = {
727
- description: __DEV__ ? "The wire format of a Solana transaction" : "",
728
- fixedSize: null,
729
- maxSize: null
730
- };
731
- function serialize2(transaction) {
732
- const compiledTransaction = getCompiledTransaction(transaction);
733
- return struct([
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";
935
+ function getCompiledTransactionEncoder() {
936
+ return getStructEncoder(
734
937
  [
735
- "signatures",
736
- array(bytes({ size: 64 }), {
737
- ...__DEV__ ? { description: "A compact array of 64-byte, base-64 encoded Ed25519 signatures" } : null,
738
- size: shortU16()
739
- })
938
+ [
939
+ "signatures",
940
+ getArrayEncoder(getBytesEncoder({ size: 64 }), {
941
+ description: signaturesDescription,
942
+ size: getShortU16Encoder()
943
+ })
944
+ ],
945
+ ["compiledMessage", getCompiledMessageEncoder()]
740
946
  ],
741
- ["compiledMessage", getCompiledMessageEncoder()]
742
- ]).serialize(compiledTransaction);
947
+ {
948
+ description: transactionDescription
949
+ }
950
+ );
951
+ }
952
+ function getSignatureDecoder() {
953
+ return mapDecoder(getBytesDecoder({ size: 64 }), (bytes) => bytes);
954
+ }
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
969
+ }
970
+ );
743
971
  }
744
972
  function getTransactionEncoder() {
745
- return {
746
- ...BASE_CONFIG3,
747
- deserialize: getUnimplementedDecoder("CompiledMessage"),
748
- serialize: serialize2
749
- };
973
+ return mapEncoder(getCompiledTransactionEncoder(), getCompiledTransaction);
974
+ }
975
+ function getTransactionDecoder(lastValidBlockHeight) {
976
+ return mapDecoder(
977
+ getCompiledTransactionDecoder(),
978
+ (compiledTransaction) => decompileTransaction(compiledTransaction, lastValidBlockHeight)
979
+ );
750
980
  }
981
+ function getTransactionCodec(lastValidBlockHeight) {
982
+ return combineCodec(getTransactionEncoder(), getTransactionDecoder(lastValidBlockHeight));
983
+ }
984
+ var base58Encoder2;
985
+ var base58Decoder;
751
986
  function assertIsTransactionSignature(putativeTransactionSignature) {
987
+ if (!base58Encoder2)
988
+ base58Encoder2 = getBase58Encoder();
752
989
  try {
753
990
  if (
754
991
  // Lowest value (64 bytes of zeroes)
@@ -757,8 +994,8 @@ function assertIsTransactionSignature(putativeTransactionSignature) {
757
994
  ) {
758
995
  throw new Error("Expected input string to decode to a byte array of length 64.");
759
996
  }
760
- const bytes3 = base58.serialize(putativeTransactionSignature);
761
- const numBytes = bytes3.byteLength;
997
+ const bytes = base58Encoder2.encode(putativeTransactionSignature);
998
+ const numBytes = bytes.byteLength;
762
999
  if (numBytes !== 64) {
763
1000
  throw new Error(`Expected input string to decode to a byte array of length 64. Actual length: ${numBytes}`);
764
1001
  }
@@ -769,6 +1006,8 @@ function assertIsTransactionSignature(putativeTransactionSignature) {
769
1006
  }
770
1007
  }
771
1008
  function isTransactionSignature(putativeTransactionSignature) {
1009
+ if (!base58Encoder2)
1010
+ base58Encoder2 = getBase58Encoder();
772
1011
  if (
773
1012
  // Lowest value (64 bytes of zeroes)
774
1013
  putativeTransactionSignature.length < 64 || // Highest value (64 bytes of 255)
@@ -776,36 +1015,32 @@ function isTransactionSignature(putativeTransactionSignature) {
776
1015
  ) {
777
1016
  return false;
778
1017
  }
779
- const bytes3 = base58.serialize(putativeTransactionSignature);
780
- const numBytes = bytes3.byteLength;
1018
+ const bytes = base58Encoder2.encode(putativeTransactionSignature);
1019
+ const numBytes = bytes.byteLength;
781
1020
  if (numBytes !== 64) {
782
1021
  return false;
783
1022
  }
784
1023
  return true;
785
1024
  }
786
- async function getCompiledMessageSignature(message, secretKey) {
787
- const wireMessageBytes = getCompiledMessageEncoder().serialize(message);
788
- const signature = await signBytes(secretKey, wireMessageBytes);
789
- return signature;
790
- }
791
1025
  function getSignatureFromTransaction(transaction) {
792
- const signature = transaction.signatures[transaction.feePayer];
793
- if (!signature) {
1026
+ if (!base58Decoder)
1027
+ base58Decoder = getBase58Decoder();
1028
+ const signatureBytes = transaction.signatures[transaction.feePayer];
1029
+ if (!signatureBytes) {
794
1030
  throw new Error(
795
1031
  "Could not determine this transaction's signature. Make sure that the transaction has been signed by its fee payer."
796
1032
  );
797
1033
  }
798
- return signature;
1034
+ const transactionSignature2 = base58Decoder.decode(signatureBytes)[0];
1035
+ return transactionSignature2;
799
1036
  }
800
1037
  async function signTransaction(keyPairs, transaction) {
801
1038
  const compiledMessage = compileMessage(transaction);
802
1039
  const nextSignatures = "signatures" in transaction ? { ...transaction.signatures } : {};
1040
+ const wireMessageBytes = getCompiledMessageEncoder().encode(compiledMessage);
803
1041
  const publicKeySignaturePairs = await Promise.all(
804
1042
  keyPairs.map(
805
- (keyPair) => Promise.all([
806
- getAddressFromPublicKey(keyPair.publicKey),
807
- getCompiledMessageSignature(compiledMessage, keyPair.privateKey)
808
- ])
1043
+ (keyPair) => Promise.all([getAddressFromPublicKey(keyPair.publicKey), signBytes(keyPair.privateKey, wireMessageBytes)])
809
1044
  )
810
1045
  );
811
1046
  for (const [signerPublicKey, signature] of publicKeySignaturePairs) {
@@ -822,15 +1057,24 @@ function transactionSignature(putativeTransactionSignature) {
822
1057
  assertIsTransactionSignature(putativeTransactionSignature);
823
1058
  return putativeTransactionSignature;
824
1059
  }
1060
+ function assertTransactionIsFullySigned(transaction) {
1061
+ const signerAddressesFromInstructions = transaction.instructions.flatMap((i) => i.accounts?.filter((a) => isSignerRole(a.role)) ?? []).map((a) => a.address);
1062
+ const requiredSigners = /* @__PURE__ */ new Set([transaction.feePayer, ...signerAddressesFromInstructions]);
1063
+ requiredSigners.forEach((address) => {
1064
+ if (!transaction.signatures[address]) {
1065
+ throw new Error(`Transaction is missing signature for address \`${address}\``);
1066
+ }
1067
+ });
1068
+ }
825
1069
 
826
1070
  // src/wire-transaction.ts
827
1071
  function getBase64EncodedWireTransaction(transaction) {
828
- const wireTransactionBytes = getTransactionEncoder().serialize(transaction);
1072
+ const wireTransactionBytes = getTransactionEncoder().encode(transaction);
829
1073
  {
830
1074
  return btoa(String.fromCharCode(...wireTransactionBytes));
831
1075
  }
832
1076
  }
833
1077
 
834
- export { appendTransactionInstruction, assertIsBlockhash, assertIsDurableNonceTransaction, assertIsTransactionSignature, createTransaction, getBase64EncodedWireTransaction, getSignatureFromTransaction, getTransactionEncoder, isTransactionSignature, prependTransactionInstruction, setTransactionFeePayer, setTransactionLifetimeUsingBlockhash, setTransactionLifetimeUsingDurableNonce, signTransaction, transactionSignature };
1078
+ export { appendTransactionInstruction, assertIsBlockhash, assertIsDurableNonceTransaction, assertIsTransactionSignature, assertTransactionIsFullySigned, createTransaction, getBase64EncodedWireTransaction, getSignatureFromTransaction, getTransactionCodec, getTransactionDecoder, getTransactionEncoder, isAdvanceNonceAccountInstruction, isTransactionSignature, prependTransactionInstruction, setTransactionFeePayer, setTransactionLifetimeUsingBlockhash, setTransactionLifetimeUsingDurableNonce, signTransaction, transactionSignature };
835
1079
  //# sourceMappingURL=out.js.map
836
1080
  //# sourceMappingURL=index.browser.js.map