@solana/transactions 2.0.0-experimental.b927e84 → 2.0.0-experimental.ba21818

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 +171 -78
  3. package/dist/index.browser.cjs.map +1 -1
  4. package/dist/index.browser.js +170 -80
  5. package/dist/index.browser.js.map +1 -1
  6. package/dist/index.development.js +595 -127
  7. package/dist/index.development.js.map +1 -1
  8. package/dist/index.native.js +170 -80
  9. package/dist/index.native.js.map +1 -1
  10. package/dist/index.node.cjs +171 -78
  11. package/dist/index.node.cjs.map +1 -1
  12. package/dist/index.node.js +170 -80
  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/accounts.d.ts.map +1 -0
  17. package/dist/types/blockhash.d.ts +1 -1
  18. package/dist/types/blockhash.d.ts.map +1 -0
  19. package/dist/types/compile-address-table-lookups.d.ts +1 -1
  20. package/dist/types/compile-address-table-lookups.d.ts.map +1 -0
  21. package/dist/types/compile-header.d.ts.map +1 -0
  22. package/dist/types/compile-instructions.d.ts +1 -1
  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 +1 -1
  26. package/dist/types/compile-static-accounts.d.ts.map +1 -0
  27. package/dist/types/compile-transaction.d.ts.map +1 -0
  28. package/dist/types/create-transaction.d.ts.map +1 -0
  29. package/dist/types/durable-nonce.d.ts +5 -4
  30. package/dist/types/durable-nonce.d.ts.map +1 -0
  31. package/dist/types/fee-payer.d.ts +3 -2
  32. package/dist/types/fee-payer.d.ts.map +1 -0
  33. package/dist/types/index.d.ts +1 -0
  34. package/dist/types/index.d.ts.map +1 -0
  35. package/dist/types/instructions.d.ts.map +1 -0
  36. package/dist/types/message.d.ts.map +1 -0
  37. package/dist/types/serializers/address-table-lookup.d.ts.map +1 -0
  38. package/dist/types/serializers/header.d.ts.map +1 -0
  39. package/dist/types/serializers/index.d.ts +2 -0
  40. package/dist/types/serializers/index.d.ts.map +1 -0
  41. package/dist/types/serializers/instruction.d.ts.map +1 -0
  42. package/dist/types/serializers/message.d.ts.map +1 -0
  43. package/dist/types/serializers/transaction-version.d.ts +4 -4
  44. package/dist/types/serializers/transaction-version.d.ts.map +1 -0
  45. package/dist/types/serializers/transaction.d.ts.map +1 -0
  46. package/dist/types/serializers/unimplemented.d.ts.map +1 -0
  47. package/dist/types/signatures.d.ts +12 -3
  48. package/dist/types/signatures.d.ts.map +1 -0
  49. package/dist/types/types.d.ts +7 -0
  50. package/dist/types/types.d.ts.map +1 -0
  51. package/dist/types/unsigned-transaction.d.ts.map +1 -0
  52. package/dist/types/wire-transaction.d.ts +1 -1
  53. package/dist/types/wire-transaction.d.ts.map +1 -0
  54. package/package.json +19 -18
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,8 @@
1
1
  'use strict';
2
2
 
3
3
  var umiSerializers = require('@metaplex-foundation/umi-serializers');
4
+ var addresses = require('@solana/addresses');
5
+ var codecsCore = require('@solana/codecs-core');
4
6
  var keys = require('@solana/keys');
5
7
 
6
8
  // ../build-scripts/env-shim.ts
@@ -229,7 +231,7 @@ function getAddressMapFromInstructions(feePayer, instructions) {
229
231
  const shouldReplaceEntry = (
230
232
  // Consider using the new LOOKUP_TABLE if its address is different...
231
233
  entry.lookupTableAddress !== accountMeta.lookupTableAddress && // ...and sorts before the existing one.
232
- (addressComparator || (addressComparator = keys.getBase58EncodedAddressComparator()))(
234
+ (addressComparator || (addressComparator = addresses.getAddressComparator()))(
233
235
  accountMeta.lookupTableAddress,
234
236
  entry.lookupTableAddress
235
237
  ) < 0
@@ -335,7 +337,7 @@ function getOrderedAccountsFromAddressMap(addressMap) {
335
337
  if (leftIsWritable !== isWritableRole(rightEntry.role)) {
336
338
  return leftIsWritable ? -1 : 1;
337
339
  }
338
- addressComparator || (addressComparator = keys.getBase58EncodedAddressComparator());
340
+ addressComparator || (addressComparator = addresses.getAddressComparator());
339
341
  if (leftEntry[TYPE] === 1 /* LOOKUP_TABLE */ && rightEntry[TYPE] === 1 /* LOOKUP_TABLE */ && leftEntry.lookupTableAddress !== rightEntry.lookupTableAddress) {
340
342
  return addressComparator(leftEntry.lookupTableAddress, rightEntry.lookupTableAddress);
341
343
  } else {
@@ -364,7 +366,7 @@ function getCompiledAddressTableLookups(orderedAccounts) {
364
366
  entry.readableIndices.push(account.addressIndex);
365
367
  }
366
368
  }
367
- return Object.keys(index).sort(keys.getBase58EncodedAddressComparator()).map((lookupTableAddress) => ({
369
+ return Object.keys(index).sort(addresses.getAddressComparator()).map((lookupTableAddress) => ({
368
370
  lookupTableAddress,
369
371
  ...index[lookupTableAddress]
370
372
  }));
@@ -443,12 +445,40 @@ function compileMessage(transaction) {
443
445
  version: transaction.version
444
446
  };
445
447
  }
448
+
449
+ // src/compile-transaction.ts
450
+ function getCompiledTransaction(transaction) {
451
+ const compiledMessage = compileMessage(transaction);
452
+ let signatures;
453
+ if ("signatures" in transaction) {
454
+ signatures = [];
455
+ for (let ii = 0; ii < compiledMessage.header.numSignerAccounts; ii++) {
456
+ signatures[ii] = transaction.signatures[compiledMessage.staticAccounts[ii]] ?? new Uint8Array(Array(64).fill(0));
457
+ }
458
+ } else {
459
+ signatures = Array(compiledMessage.header.numSignerAccounts).fill(new Uint8Array(Array(64).fill(0)));
460
+ }
461
+ return {
462
+ compiledMessage,
463
+ signatures
464
+ };
465
+ }
466
+ function addressSerializerCompat(compat) {
467
+ const codec = addresses.getAddressCodec();
468
+ return {
469
+ description: compat?.description ?? codec.description,
470
+ deserialize: codec.decode,
471
+ fixedSize: codec.fixedSize,
472
+ maxSize: codec.maxSize,
473
+ serialize: codec.encode
474
+ };
475
+ }
446
476
  function getAddressTableLookupCodec() {
447
477
  return umiSerializers.struct(
448
478
  [
449
479
  [
450
480
  "lookupTableAddress",
451
- keys.getBase58EncodedAddressCodec(
481
+ addressSerializerCompat(
452
482
  __DEV__ ? {
453
483
  description: "The address of the address lookup table account from which instruction addresses should be looked up"
454
484
  } : void 0
@@ -523,7 +553,7 @@ function getInstructionCodec() {
523
553
  )
524
554
  ],
525
555
  [
526
- "addressIndices",
556
+ "accountIndices",
527
557
  umiSerializers.array(
528
558
  umiSerializers.u8({
529
559
  description: __DEV__ ? "The index of an account, according to the well-ordered accounts list for this transaction" : ""
@@ -543,50 +573,35 @@ function getInstructionCodec() {
543
573
  ]
544
574
  ]),
545
575
  (value) => {
546
- if (value.addressIndices !== void 0 && value.data !== void 0) {
576
+ if (value.accountIndices !== void 0 && value.data !== void 0) {
547
577
  return value;
548
578
  }
549
579
  return {
550
580
  ...value,
551
- addressIndices: value.addressIndices ?? [],
581
+ accountIndices: value.accountIndices ?? [],
552
582
  data: value.data ?? new Uint8Array(0)
553
583
  };
554
584
  },
555
585
  (value) => {
556
- if (value.addressIndices.length && value.data.byteLength) {
586
+ if (value.accountIndices.length && value.data.byteLength) {
557
587
  return value;
558
588
  }
559
- const { addressIndices, data, ...rest } = value;
589
+ const { accountIndices, data, ...rest } = value;
560
590
  return {
561
591
  ...rest,
562
- ...addressIndices.length ? { addressIndices } : null,
592
+ ...accountIndices.length ? { accountIndices } : null,
563
593
  ...data.byteLength ? { data } : null
564
594
  };
565
595
  }
566
596
  );
567
597
  }
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
598
  var VERSION_FLAG_MASK = 128;
584
599
  var BASE_CONFIG = {
585
600
  description: __DEV__ ? "A single byte that encodes the version of the transaction" : "",
586
601
  fixedSize: null,
587
602
  maxSize: 1
588
603
  };
589
- function deserialize(bytes3, offset = 0) {
604
+ function decode(bytes3, offset = 0) {
590
605
  const firstByte = bytes3[offset];
591
606
  if ((firstByte & VERSION_FLAG_MASK) === 0) {
592
607
  return ["legacy", offset];
@@ -595,7 +610,7 @@ function deserialize(bytes3, offset = 0) {
595
610
  return [version, offset + 1];
596
611
  }
597
612
  }
598
- function serialize(value) {
613
+ function encode(value) {
599
614
  if (value === "legacy") {
600
615
  return new Uint8Array();
601
616
  }
@@ -604,11 +619,32 @@ function serialize(value) {
604
619
  }
605
620
  return new Uint8Array([value | VERSION_FLAG_MASK]);
606
621
  }
607
- function getTransactionVersionCodec() {
622
+ function getTransactionVersionDecoder() {
608
623
  return {
609
624
  ...BASE_CONFIG,
610
- deserialize,
611
- serialize
625
+ decode
626
+ };
627
+ }
628
+ function getTransactionVersionEncoder() {
629
+ return {
630
+ ...BASE_CONFIG,
631
+ encode
632
+ };
633
+ }
634
+ function getTransactionVersionCodec() {
635
+ return codecsCore.combineCodec(getTransactionVersionEncoder(), getTransactionVersionDecoder());
636
+ }
637
+
638
+ // src/serializers/unimplemented.ts
639
+ function getError(type, name) {
640
+ const functionSuffix = name + type[0].toUpperCase() + type.slice(1);
641
+ return new Error(
642
+ `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}`
643
+ );
644
+ }
645
+ function getUnimplementedDecoder(name) {
646
+ return () => {
647
+ throw getError("decoder", name);
612
648
  };
613
649
  }
614
650
 
@@ -618,7 +654,7 @@ var BASE_CONFIG2 = {
618
654
  fixedSize: null,
619
655
  maxSize: null
620
656
  };
621
- function serialize2(compiledMessage) {
657
+ function serialize(compiledMessage) {
622
658
  if (compiledMessage.version === "legacy") {
623
659
  return umiSerializers.struct(getPreludeStructSerializerTuple()).serialize(compiledMessage);
624
660
  } else {
@@ -639,13 +675,33 @@ function serialize2(compiledMessage) {
639
675
  ).serialize(compiledMessage);
640
676
  }
641
677
  }
678
+ function addressSerializerCompat2() {
679
+ const codec = addresses.getAddressCodec();
680
+ return {
681
+ description: codec.description,
682
+ deserialize: codec.decode,
683
+ fixedSize: codec.fixedSize,
684
+ maxSize: codec.maxSize,
685
+ serialize: codec.encode
686
+ };
687
+ }
688
+ function transactionVersionSerializerCompat() {
689
+ const codec = getTransactionVersionCodec();
690
+ return {
691
+ description: codec.description,
692
+ deserialize: codec.decode,
693
+ fixedSize: codec.fixedSize,
694
+ maxSize: codec.maxSize,
695
+ serialize: codec.encode
696
+ };
697
+ }
642
698
  function getPreludeStructSerializerTuple() {
643
699
  return [
644
- ["version", getTransactionVersionCodec()],
700
+ ["version", transactionVersionSerializerCompat()],
645
701
  ["header", getMessageHeaderCodec()],
646
702
  [
647
703
  "staticAccounts",
648
- umiSerializers.array(keys.getBase58EncodedAddressCodec(), {
704
+ umiSerializers.array(addressSerializerCompat2(), {
649
705
  description: __DEV__ ? "A compact-array of static account addresses belonging to this transaction" : "",
650
706
  size: umiSerializers.shortU16()
651
707
  })
@@ -677,49 +733,7 @@ function getCompiledMessageEncoder() {
677
733
  return {
678
734
  ...BASE_CONFIG2,
679
735
  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
736
+ serialize
723
737
  };
724
738
  }
725
739
 
@@ -729,7 +743,7 @@ var BASE_CONFIG3 = {
729
743
  fixedSize: null,
730
744
  maxSize: null
731
745
  };
732
- function serialize3(transaction) {
746
+ function serialize2(transaction) {
733
747
  const compiledTransaction = getCompiledTransaction(transaction);
734
748
  return umiSerializers.struct([
735
749
  [
@@ -746,9 +760,83 @@ function getTransactionEncoder() {
746
760
  return {
747
761
  ...BASE_CONFIG3,
748
762
  deserialize: getUnimplementedDecoder("CompiledMessage"),
749
- serialize: serialize3
763
+ serialize: serialize2
750
764
  };
751
765
  }
766
+ function assertIsTransactionSignature(putativeTransactionSignature) {
767
+ try {
768
+ if (
769
+ // Lowest value (64 bytes of zeroes)
770
+ putativeTransactionSignature.length < 64 || // Highest value (64 bytes of 255)
771
+ putativeTransactionSignature.length > 88
772
+ ) {
773
+ throw new Error("Expected input string to decode to a byte array of length 64.");
774
+ }
775
+ const bytes3 = umiSerializers.base58.serialize(putativeTransactionSignature);
776
+ const numBytes = bytes3.byteLength;
777
+ if (numBytes !== 64) {
778
+ throw new Error(`Expected input string to decode to a byte array of length 64. Actual length: ${numBytes}`);
779
+ }
780
+ } catch (e) {
781
+ throw new Error(`\`${putativeTransactionSignature}\` is not a transaction signature`, {
782
+ cause: e
783
+ });
784
+ }
785
+ }
786
+ function isTransactionSignature(putativeTransactionSignature) {
787
+ if (
788
+ // Lowest value (64 bytes of zeroes)
789
+ putativeTransactionSignature.length < 64 || // Highest value (64 bytes of 255)
790
+ putativeTransactionSignature.length > 88
791
+ ) {
792
+ return false;
793
+ }
794
+ const bytes3 = umiSerializers.base58.serialize(putativeTransactionSignature);
795
+ const numBytes = bytes3.byteLength;
796
+ if (numBytes !== 64) {
797
+ return false;
798
+ }
799
+ return true;
800
+ }
801
+ async function getCompiledMessageSignature(message, secretKey) {
802
+ const wireMessageBytes = getCompiledMessageEncoder().serialize(message);
803
+ const signature = await keys.signBytes(secretKey, wireMessageBytes);
804
+ return signature;
805
+ }
806
+ function getSignatureFromTransaction(transaction) {
807
+ const signature = transaction.signatures[transaction.feePayer];
808
+ if (!signature) {
809
+ throw new Error(
810
+ "Could not determine this transaction's signature. Make sure that the transaction has been signed by its fee payer."
811
+ );
812
+ }
813
+ return signature;
814
+ }
815
+ async function signTransaction(keyPairs, transaction) {
816
+ const compiledMessage = compileMessage(transaction);
817
+ const nextSignatures = "signatures" in transaction ? { ...transaction.signatures } : {};
818
+ const publicKeySignaturePairs = await Promise.all(
819
+ keyPairs.map(
820
+ (keyPair) => Promise.all([
821
+ addresses.getAddressFromPublicKey(keyPair.publicKey),
822
+ getCompiledMessageSignature(compiledMessage, keyPair.privateKey)
823
+ ])
824
+ )
825
+ );
826
+ for (const [signerPublicKey, signature] of publicKeySignaturePairs) {
827
+ nextSignatures[signerPublicKey] = signature;
828
+ }
829
+ const out = {
830
+ ...transaction,
831
+ signatures: nextSignatures
832
+ };
833
+ Object.freeze(out);
834
+ return out;
835
+ }
836
+ function transactionSignature(putativeTransactionSignature) {
837
+ assertIsTransactionSignature(putativeTransactionSignature);
838
+ return putativeTransactionSignature;
839
+ }
752
840
 
753
841
  // src/wire-transaction.ts
754
842
  function getBase64EncodedWireTransaction(transaction) {
@@ -761,12 +849,17 @@ function getBase64EncodedWireTransaction(transaction) {
761
849
  exports.appendTransactionInstruction = appendTransactionInstruction;
762
850
  exports.assertIsBlockhash = assertIsBlockhash;
763
851
  exports.assertIsDurableNonceTransaction = assertIsDurableNonceTransaction;
852
+ exports.assertIsTransactionSignature = assertIsTransactionSignature;
764
853
  exports.createTransaction = createTransaction;
765
854
  exports.getBase64EncodedWireTransaction = getBase64EncodedWireTransaction;
855
+ exports.getSignatureFromTransaction = getSignatureFromTransaction;
856
+ exports.getTransactionEncoder = getTransactionEncoder;
857
+ exports.isTransactionSignature = isTransactionSignature;
766
858
  exports.prependTransactionInstruction = prependTransactionInstruction;
767
859
  exports.setTransactionFeePayer = setTransactionFeePayer;
768
860
  exports.setTransactionLifetimeUsingBlockhash = setTransactionLifetimeUsingBlockhash;
769
861
  exports.setTransactionLifetimeUsingDurableNonce = setTransactionLifetimeUsingDurableNonce;
770
862
  exports.signTransaction = signTransaction;
863
+ exports.transactionSignature = transactionSignature;
771
864
  //# sourceMappingURL=out.js.map
772
865
  //# sourceMappingURL=index.browser.cjs.map