@solana/transactions 2.0.0-experimental.211879d → 2.0.0-experimental.228d375

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 (54) hide show
  1. package/LICENSE +1 -1
  2. package/dist/index.browser.cjs +187 -107
  3. package/dist/index.browser.cjs.map +1 -1
  4. package/dist/index.browser.js +184 -109
  5. package/dist/index.browser.js.map +1 -1
  6. package/dist/index.development.js +661 -155
  7. package/dist/index.development.js.map +1 -1
  8. package/dist/index.native.js +184 -109
  9. package/dist/index.native.js.map +1 -1
  10. package/dist/index.node.cjs +189 -107
  11. package/dist/index.node.cjs.map +1 -1
  12. package/dist/index.node.js +184 -109
  13. package/dist/index.node.js.map +1 -1
  14. package/dist/index.production.min.js +16 -11
  15. package/dist/types/accounts.d.ts +2 -2
  16. package/dist/types/blockhash.d.ts +1 -1
  17. package/dist/types/compile-address-table-lookups.d.ts +1 -1
  18. package/dist/types/compile-instructions.d.ts +1 -1
  19. package/dist/types/compile-static-accounts.d.ts +1 -1
  20. package/dist/types/durable-nonce.d.ts +5 -4
  21. package/dist/types/fee-payer.d.ts +3 -2
  22. package/dist/types/index.d.ts +1 -0
  23. package/dist/types/serializers/header.d.ts +4 -2
  24. package/dist/types/serializers/index.d.ts +2 -0
  25. package/dist/types/serializers/transaction-version.d.ts +4 -4
  26. package/dist/types/signatures.d.ts +12 -3
  27. package/dist/types/types.d.ts +7 -0
  28. package/dist/types/wire-transaction.d.ts +1 -1
  29. package/package.json +21 -18
  30. package/dist/types/accounts.d.ts.map +0 -1
  31. package/dist/types/blockhash.d.ts.map +0 -1
  32. package/dist/types/compile-address-table-lookups.d.ts.map +0 -1
  33. package/dist/types/compile-header.d.ts.map +0 -1
  34. package/dist/types/compile-instructions.d.ts.map +0 -1
  35. package/dist/types/compile-lifetime-token.d.ts.map +0 -1
  36. package/dist/types/compile-static-accounts.d.ts.map +0 -1
  37. package/dist/types/compile-transaction.d.ts.map +0 -1
  38. package/dist/types/create-transaction.d.ts.map +0 -1
  39. package/dist/types/durable-nonce.d.ts.map +0 -1
  40. package/dist/types/fee-payer.d.ts.map +0 -1
  41. package/dist/types/index.d.ts.map +0 -1
  42. package/dist/types/instructions.d.ts.map +0 -1
  43. package/dist/types/message.d.ts.map +0 -1
  44. package/dist/types/serializers/address-table-lookup.d.ts.map +0 -1
  45. package/dist/types/serializers/header.d.ts.map +0 -1
  46. package/dist/types/serializers/instruction.d.ts.map +0 -1
  47. package/dist/types/serializers/message.d.ts.map +0 -1
  48. package/dist/types/serializers/transaction-version.d.ts.map +0 -1
  49. package/dist/types/serializers/transaction.d.ts.map +0 -1
  50. package/dist/types/serializers/unimplemented.d.ts.map +0 -1
  51. package/dist/types/signatures.d.ts.map +0 -1
  52. package/dist/types/types.d.ts.map +0 -1
  53. package/dist/types/unsigned-transaction.d.ts.map +0 -1
  54. package/dist/types/wire-transaction.d.ts.map +0 -1
package/LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2018 Solana Labs, Inc
1
+ Copyright (c) 2023 Solana Labs, Inc
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
@@ -1,6 +1,10 @@
1
1
  'use strict';
2
2
 
3
3
  var umiSerializers = require('@metaplex-foundation/umi-serializers');
4
+ var addresses = require('@solana/addresses');
5
+ var codecsDataStructures = require('@solana/codecs-data-structures');
6
+ var codecsNumbers = require('@solana/codecs-numbers');
7
+ var codecsCore = require('@solana/codecs-core');
4
8
  var keys = require('@solana/keys');
5
9
 
6
10
  // ../build-scripts/env-shim.ts
@@ -229,7 +233,7 @@ function getAddressMapFromInstructions(feePayer, instructions) {
229
233
  const shouldReplaceEntry = (
230
234
  // Consider using the new LOOKUP_TABLE if its address is different...
231
235
  entry.lookupTableAddress !== accountMeta.lookupTableAddress && // ...and sorts before the existing one.
232
- (addressComparator || (addressComparator = keys.getBase58EncodedAddressComparator()))(
236
+ (addressComparator || (addressComparator = addresses.getAddressComparator()))(
233
237
  accountMeta.lookupTableAddress,
234
238
  entry.lookupTableAddress
235
239
  ) < 0
@@ -335,7 +339,7 @@ function getOrderedAccountsFromAddressMap(addressMap) {
335
339
  if (leftIsWritable !== isWritableRole(rightEntry.role)) {
336
340
  return leftIsWritable ? -1 : 1;
337
341
  }
338
- addressComparator || (addressComparator = keys.getBase58EncodedAddressComparator());
342
+ addressComparator || (addressComparator = addresses.getAddressComparator());
339
343
  if (leftEntry[TYPE] === 1 /* LOOKUP_TABLE */ && rightEntry[TYPE] === 1 /* LOOKUP_TABLE */ && leftEntry.lookupTableAddress !== rightEntry.lookupTableAddress) {
340
344
  return addressComparator(leftEntry.lookupTableAddress, rightEntry.lookupTableAddress);
341
345
  } else {
@@ -364,7 +368,7 @@ function getCompiledAddressTableLookups(orderedAccounts) {
364
368
  entry.readableIndices.push(account.addressIndex);
365
369
  }
366
370
  }
367
- return Object.keys(index).sort(keys.getBase58EncodedAddressComparator()).map((lookupTableAddress) => ({
371
+ return Object.keys(index).sort(addresses.getAddressComparator()).map((lookupTableAddress) => ({
368
372
  lookupTableAddress,
369
373
  ...index[lookupTableAddress]
370
374
  }));
@@ -443,12 +447,40 @@ function compileMessage(transaction) {
443
447
  version: transaction.version
444
448
  };
445
449
  }
450
+
451
+ // src/compile-transaction.ts
452
+ function getCompiledTransaction(transaction) {
453
+ const compiledMessage = compileMessage(transaction);
454
+ let signatures;
455
+ if ("signatures" in transaction) {
456
+ signatures = [];
457
+ for (let ii = 0; ii < compiledMessage.header.numSignerAccounts; ii++) {
458
+ signatures[ii] = transaction.signatures[compiledMessage.staticAccounts[ii]] ?? new Uint8Array(Array(64).fill(0));
459
+ }
460
+ } else {
461
+ signatures = Array(compiledMessage.header.numSignerAccounts).fill(new Uint8Array(Array(64).fill(0)));
462
+ }
463
+ return {
464
+ compiledMessage,
465
+ signatures
466
+ };
467
+ }
468
+ function addressSerializerCompat(compat) {
469
+ const codec = addresses.getAddressCodec();
470
+ return {
471
+ description: compat?.description ?? codec.description,
472
+ deserialize: codec.decode,
473
+ fixedSize: codec.fixedSize,
474
+ maxSize: codec.maxSize,
475
+ serialize: codec.encode
476
+ };
477
+ }
446
478
  function getAddressTableLookupCodec() {
447
479
  return umiSerializers.struct(
448
480
  [
449
481
  [
450
482
  "lookupTableAddress",
451
- keys.getBase58EncodedAddressCodec(
483
+ addressSerializerCompat(
452
484
  __DEV__ ? {
453
485
  description: "The address of the address lookup table account from which instruction addresses should be looked up"
454
486
  } : void 0
@@ -478,37 +510,33 @@ function getAddressTableLookupCodec() {
478
510
  } : void 0
479
511
  );
480
512
  }
513
+ var memoizedU8Codec;
514
+ function getMemoizedU8Codec() {
515
+ if (!memoizedU8Codec)
516
+ memoizedU8Codec = codecsNumbers.getU8Codec();
517
+ return memoizedU8Codec;
518
+ }
519
+ function getMemoizedU8CodecDescription(description) {
520
+ const codec = getMemoizedU8Codec();
521
+ return {
522
+ ...codec,
523
+ description: description ?? codec.description
524
+ };
525
+ }
526
+ 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;
527
+ 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;
528
+ var numReadonlyNonSignerAccountsDescription = __DEV__ ? "The expected number of addresses in the static address list belonging to accounts that are neither signers, nor writable" : void 0;
529
+ var messageHeaderDescription = __DEV__ ? "The transaction message header containing counts of the signer, readonly-signer, and readonly-nonsigner account addresses" : void 0;
481
530
  function getMessageHeaderCodec() {
482
- return umiSerializers.struct(
531
+ return codecsDataStructures.getStructCodec(
483
532
  [
484
- [
485
- "numSignerAccounts",
486
- umiSerializers.u8(
487
- __DEV__ ? {
488
- description: "The expected number of addresses in the static address list belonging to accounts that are required to sign this transaction"
489
- } : void 0
490
- )
491
- ],
492
- [
493
- "numReadonlySignerAccounts",
494
- umiSerializers.u8(
495
- __DEV__ ? {
496
- description: "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"
497
- } : void 0
498
- )
499
- ],
500
- [
501
- "numReadonlyNonSignerAccounts",
502
- umiSerializers.u8(
503
- __DEV__ ? {
504
- description: "The expected number of addresses in the static address list belonging to accounts that are neither signers, nor writable"
505
- } : void 0
506
- )
507
- ]
533
+ ["numSignerAccounts", getMemoizedU8CodecDescription(numSignerAccountsDescription)],
534
+ ["numReadonlySignerAccounts", getMemoizedU8CodecDescription(numReadonlySignerAccountsDescription)],
535
+ ["numReadonlyNonSignerAccounts", getMemoizedU8CodecDescription(numReadonlyNonSignerAccountsDescription)]
508
536
  ],
509
- __DEV__ ? {
510
- description: "The transaction message header containing counts of the signer, readonly-signer, and readonly-nonsigner account addresses"
511
- } : void 0
537
+ {
538
+ description: messageHeaderDescription
539
+ }
512
540
  );
513
541
  }
514
542
  function getInstructionCodec() {
@@ -523,7 +551,7 @@ function getInstructionCodec() {
523
551
  )
524
552
  ],
525
553
  [
526
- "addressIndices",
554
+ "accountIndices",
527
555
  umiSerializers.array(
528
556
  umiSerializers.u8({
529
557
  description: __DEV__ ? "The index of an account, according to the well-ordered accounts list for this transaction" : ""
@@ -543,50 +571,35 @@ function getInstructionCodec() {
543
571
  ]
544
572
  ]),
545
573
  (value) => {
546
- if (value.addressIndices !== void 0 && value.data !== void 0) {
574
+ if (value.accountIndices !== void 0 && value.data !== void 0) {
547
575
  return value;
548
576
  }
549
577
  return {
550
578
  ...value,
551
- addressIndices: value.addressIndices ?? [],
579
+ accountIndices: value.accountIndices ?? [],
552
580
  data: value.data ?? new Uint8Array(0)
553
581
  };
554
582
  },
555
583
  (value) => {
556
- if (value.addressIndices.length && value.data.byteLength) {
584
+ if (value.accountIndices.length && value.data.byteLength) {
557
585
  return value;
558
586
  }
559
- const { addressIndices, data, ...rest } = value;
587
+ const { accountIndices, data, ...rest } = value;
560
588
  return {
561
589
  ...rest,
562
- ...addressIndices.length ? { addressIndices } : null,
590
+ ...accountIndices.length ? { accountIndices } : null,
563
591
  ...data.byteLength ? { data } : null
564
592
  };
565
593
  }
566
594
  );
567
595
  }
568
-
569
- // src/serializers/unimplemented.ts
570
- function getError(type, name) {
571
- const functionSuffix = name + type[0].toUpperCase() + type.slice(1);
572
- return new Error(
573
- `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}`
574
- );
575
- }
576
- function getUnimplementedDecoder(name) {
577
- return () => {
578
- throw getError("decoder", name);
579
- };
580
- }
581
-
582
- // src/serializers/transaction-version.ts
583
596
  var VERSION_FLAG_MASK = 128;
584
597
  var BASE_CONFIG = {
585
598
  description: __DEV__ ? "A single byte that encodes the version of the transaction" : "",
586
599
  fixedSize: null,
587
600
  maxSize: 1
588
601
  };
589
- function deserialize(bytes3, offset = 0) {
602
+ function decode(bytes3, offset = 0) {
590
603
  const firstByte = bytes3[offset];
591
604
  if ((firstByte & VERSION_FLAG_MASK) === 0) {
592
605
  return ["legacy", offset];
@@ -595,7 +608,7 @@ function deserialize(bytes3, offset = 0) {
595
608
  return [version, offset + 1];
596
609
  }
597
610
  }
598
- function serialize(value) {
611
+ function encode(value) {
599
612
  if (value === "legacy") {
600
613
  return new Uint8Array();
601
614
  }
@@ -604,11 +617,32 @@ function serialize(value) {
604
617
  }
605
618
  return new Uint8Array([value | VERSION_FLAG_MASK]);
606
619
  }
607
- function getTransactionVersionCodec() {
620
+ function getTransactionVersionDecoder() {
608
621
  return {
609
622
  ...BASE_CONFIG,
610
- deserialize,
611
- serialize
623
+ decode
624
+ };
625
+ }
626
+ function getTransactionVersionEncoder() {
627
+ return {
628
+ ...BASE_CONFIG,
629
+ encode
630
+ };
631
+ }
632
+ function getTransactionVersionCodec() {
633
+ return codecsCore.combineCodec(getTransactionVersionEncoder(), getTransactionVersionDecoder());
634
+ }
635
+
636
+ // src/serializers/unimplemented.ts
637
+ function getError(type, name) {
638
+ const functionSuffix = name + type[0].toUpperCase() + type.slice(1);
639
+ return new Error(
640
+ `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}`
641
+ );
642
+ }
643
+ function getUnimplementedDecoder(name) {
644
+ return () => {
645
+ throw getError("decoder", name);
612
646
  };
613
647
  }
614
648
 
@@ -618,7 +652,7 @@ var BASE_CONFIG2 = {
618
652
  fixedSize: null,
619
653
  maxSize: null
620
654
  };
621
- function serialize2(compiledMessage) {
655
+ function serialize(compiledMessage) {
622
656
  if (compiledMessage.version === "legacy") {
623
657
  return umiSerializers.struct(getPreludeStructSerializerTuple()).serialize(compiledMessage);
624
658
  } else {
@@ -639,13 +673,22 @@ function serialize2(compiledMessage) {
639
673
  ).serialize(compiledMessage);
640
674
  }
641
675
  }
676
+ function toSerializer(codec) {
677
+ return {
678
+ description: codec.description,
679
+ deserialize: codec.decode,
680
+ fixedSize: codec.fixedSize,
681
+ maxSize: codec.maxSize,
682
+ serialize: codec.encode
683
+ };
684
+ }
642
685
  function getPreludeStructSerializerTuple() {
643
686
  return [
644
- ["version", getTransactionVersionCodec()],
645
- ["header", getMessageHeaderCodec()],
687
+ ["version", toSerializer(getTransactionVersionCodec())],
688
+ ["header", toSerializer(getMessageHeaderCodec())],
646
689
  [
647
690
  "staticAccounts",
648
- umiSerializers.array(keys.getBase58EncodedAddressCodec(), {
691
+ umiSerializers.array(toSerializer(addresses.getAddressCodec()), {
649
692
  description: __DEV__ ? "A compact-array of static account addresses belonging to this transaction" : "",
650
693
  size: umiSerializers.shortU16()
651
694
  })
@@ -677,49 +720,7 @@ function getCompiledMessageEncoder() {
677
720
  return {
678
721
  ...BASE_CONFIG2,
679
722
  deserialize: getUnimplementedDecoder("CompiledMessage"),
680
- serialize: serialize2
681
- };
682
- }
683
-
684
- // src/signatures.ts
685
- async function getCompiledMessageSignature(message, secretKey) {
686
- const wireMessageBytes = getCompiledMessageEncoder().serialize(message);
687
- const signature = await keys.signBytes(secretKey, wireMessageBytes);
688
- return signature;
689
- }
690
- async function signTransaction(keyPair, transaction) {
691
- const compiledMessage = compileMessage(transaction);
692
- const [signerPublicKey, signature] = await Promise.all([
693
- keys.getBase58EncodedAddressFromPublicKey(keyPair.publicKey),
694
- getCompiledMessageSignature(compiledMessage, keyPair.privateKey)
695
- ]);
696
- const nextSignatures = {
697
- ..."signatures" in transaction ? transaction.signatures : null,
698
- ...{ [signerPublicKey]: signature }
699
- };
700
- const out = {
701
- ...transaction,
702
- signatures: nextSignatures
703
- };
704
- Object.freeze(out);
705
- return out;
706
- }
707
-
708
- // src/compile-transaction.ts
709
- function getCompiledTransaction(transaction) {
710
- const compiledMessage = compileMessage(transaction);
711
- let signatures;
712
- if ("signatures" in transaction) {
713
- signatures = [];
714
- for (let ii = 0; ii < compiledMessage.header.numSignerAccounts; ii++) {
715
- signatures[ii] = transaction.signatures[compiledMessage.staticAccounts[ii]] ?? new Uint8Array(Array(64).fill(0));
716
- }
717
- } else {
718
- signatures = Array(compiledMessage.header.numSignerAccounts).fill(new Uint8Array(Array(64).fill(0)));
719
- }
720
- return {
721
- compiledMessage,
722
- signatures
723
+ serialize
723
724
  };
724
725
  }
725
726
 
@@ -729,7 +730,7 @@ var BASE_CONFIG3 = {
729
730
  fixedSize: null,
730
731
  maxSize: null
731
732
  };
732
- function serialize3(transaction) {
733
+ function serialize2(transaction) {
733
734
  const compiledTransaction = getCompiledTransaction(transaction);
734
735
  return umiSerializers.struct([
735
736
  [
@@ -746,8 +747,82 @@ function getTransactionEncoder() {
746
747
  return {
747
748
  ...BASE_CONFIG3,
748
749
  deserialize: getUnimplementedDecoder("CompiledMessage"),
749
- serialize: serialize3
750
+ serialize: serialize2
751
+ };
752
+ }
753
+ function assertIsTransactionSignature(putativeTransactionSignature) {
754
+ try {
755
+ if (
756
+ // Lowest value (64 bytes of zeroes)
757
+ putativeTransactionSignature.length < 64 || // Highest value (64 bytes of 255)
758
+ putativeTransactionSignature.length > 88
759
+ ) {
760
+ throw new Error("Expected input string to decode to a byte array of length 64.");
761
+ }
762
+ const bytes3 = umiSerializers.base58.serialize(putativeTransactionSignature);
763
+ const numBytes = bytes3.byteLength;
764
+ if (numBytes !== 64) {
765
+ throw new Error(`Expected input string to decode to a byte array of length 64. Actual length: ${numBytes}`);
766
+ }
767
+ } catch (e) {
768
+ throw new Error(`\`${putativeTransactionSignature}\` is not a transaction signature`, {
769
+ cause: e
770
+ });
771
+ }
772
+ }
773
+ function isTransactionSignature(putativeTransactionSignature) {
774
+ if (
775
+ // Lowest value (64 bytes of zeroes)
776
+ putativeTransactionSignature.length < 64 || // Highest value (64 bytes of 255)
777
+ putativeTransactionSignature.length > 88
778
+ ) {
779
+ return false;
780
+ }
781
+ const bytes3 = umiSerializers.base58.serialize(putativeTransactionSignature);
782
+ const numBytes = bytes3.byteLength;
783
+ if (numBytes !== 64) {
784
+ return false;
785
+ }
786
+ return true;
787
+ }
788
+ async function getCompiledMessageSignature(message, secretKey) {
789
+ const wireMessageBytes = getCompiledMessageEncoder().serialize(message);
790
+ const signature = await keys.signBytes(secretKey, wireMessageBytes);
791
+ return signature;
792
+ }
793
+ function getSignatureFromTransaction(transaction) {
794
+ const signature = transaction.signatures[transaction.feePayer];
795
+ if (!signature) {
796
+ throw new Error(
797
+ "Could not determine this transaction's signature. Make sure that the transaction has been signed by its fee payer."
798
+ );
799
+ }
800
+ return signature;
801
+ }
802
+ async function signTransaction(keyPairs, transaction) {
803
+ const compiledMessage = compileMessage(transaction);
804
+ const nextSignatures = "signatures" in transaction ? { ...transaction.signatures } : {};
805
+ const publicKeySignaturePairs = await Promise.all(
806
+ keyPairs.map(
807
+ (keyPair) => Promise.all([
808
+ addresses.getAddressFromPublicKey(keyPair.publicKey),
809
+ getCompiledMessageSignature(compiledMessage, keyPair.privateKey)
810
+ ])
811
+ )
812
+ );
813
+ for (const [signerPublicKey, signature] of publicKeySignaturePairs) {
814
+ nextSignatures[signerPublicKey] = signature;
815
+ }
816
+ const out = {
817
+ ...transaction,
818
+ signatures: nextSignatures
750
819
  };
820
+ Object.freeze(out);
821
+ return out;
822
+ }
823
+ function transactionSignature(putativeTransactionSignature) {
824
+ assertIsTransactionSignature(putativeTransactionSignature);
825
+ return putativeTransactionSignature;
751
826
  }
752
827
 
753
828
  // src/wire-transaction.ts
@@ -761,12 +836,17 @@ function getBase64EncodedWireTransaction(transaction) {
761
836
  exports.appendTransactionInstruction = appendTransactionInstruction;
762
837
  exports.assertIsBlockhash = assertIsBlockhash;
763
838
  exports.assertIsDurableNonceTransaction = assertIsDurableNonceTransaction;
839
+ exports.assertIsTransactionSignature = assertIsTransactionSignature;
764
840
  exports.createTransaction = createTransaction;
765
841
  exports.getBase64EncodedWireTransaction = getBase64EncodedWireTransaction;
842
+ exports.getSignatureFromTransaction = getSignatureFromTransaction;
843
+ exports.getTransactionEncoder = getTransactionEncoder;
844
+ exports.isTransactionSignature = isTransactionSignature;
766
845
  exports.prependTransactionInstruction = prependTransactionInstruction;
767
846
  exports.setTransactionFeePayer = setTransactionFeePayer;
768
847
  exports.setTransactionLifetimeUsingBlockhash = setTransactionLifetimeUsingBlockhash;
769
848
  exports.setTransactionLifetimeUsingDurableNonce = setTransactionLifetimeUsingDurableNonce;
770
849
  exports.signTransaction = signTransaction;
850
+ exports.transactionSignature = transactionSignature;
771
851
  //# sourceMappingURL=out.js.map
772
852
  //# sourceMappingURL=index.browser.cjs.map