@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
@@ -1,5 +1,9 @@
1
1
  import { base58, struct, array, bytes, shortU16, mapSerializer, string, u8 } from '@metaplex-foundation/umi-serializers';
2
- import { getBase58EncodedAddressFromPublicKey, signBytes, getBase58EncodedAddressComparator, getBase58EncodedAddressCodec } from '@solana/keys';
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';
6
+ import { signBytes } from '@solana/keys';
3
7
 
4
8
  // ../build-scripts/env-shim.ts
5
9
  var __DEV__ = /* @__PURE__ */ (() => process["env"].NODE_ENV === "development")();
@@ -227,7 +231,7 @@ function getAddressMapFromInstructions(feePayer, instructions) {
227
231
  const shouldReplaceEntry = (
228
232
  // Consider using the new LOOKUP_TABLE if its address is different...
229
233
  entry.lookupTableAddress !== accountMeta.lookupTableAddress && // ...and sorts before the existing one.
230
- (addressComparator || (addressComparator = getBase58EncodedAddressComparator()))(
234
+ (addressComparator || (addressComparator = getAddressComparator()))(
231
235
  accountMeta.lookupTableAddress,
232
236
  entry.lookupTableAddress
233
237
  ) < 0
@@ -333,7 +337,7 @@ function getOrderedAccountsFromAddressMap(addressMap) {
333
337
  if (leftIsWritable !== isWritableRole(rightEntry.role)) {
334
338
  return leftIsWritable ? -1 : 1;
335
339
  }
336
- addressComparator || (addressComparator = getBase58EncodedAddressComparator());
340
+ addressComparator || (addressComparator = getAddressComparator());
337
341
  if (leftEntry[TYPE] === 1 /* LOOKUP_TABLE */ && rightEntry[TYPE] === 1 /* LOOKUP_TABLE */ && leftEntry.lookupTableAddress !== rightEntry.lookupTableAddress) {
338
342
  return addressComparator(leftEntry.lookupTableAddress, rightEntry.lookupTableAddress);
339
343
  } else {
@@ -362,7 +366,7 @@ function getCompiledAddressTableLookups(orderedAccounts) {
362
366
  entry.readableIndices.push(account.addressIndex);
363
367
  }
364
368
  }
365
- return Object.keys(index).sort(getBase58EncodedAddressComparator()).map((lookupTableAddress) => ({
369
+ return Object.keys(index).sort(getAddressComparator()).map((lookupTableAddress) => ({
366
370
  lookupTableAddress,
367
371
  ...index[lookupTableAddress]
368
372
  }));
@@ -441,12 +445,40 @@ function compileMessage(transaction) {
441
445
  version: transaction.version
442
446
  };
443
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 = 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
+ }
444
476
  function getAddressTableLookupCodec() {
445
477
  return struct(
446
478
  [
447
479
  [
448
480
  "lookupTableAddress",
449
- getBase58EncodedAddressCodec(
481
+ addressSerializerCompat(
450
482
  __DEV__ ? {
451
483
  description: "The address of the address lookup table account from which instruction addresses should be looked up"
452
484
  } : void 0
@@ -476,37 +508,33 @@ function getAddressTableLookupCodec() {
476
508
  } : void 0
477
509
  );
478
510
  }
511
+ var memoizedU8Codec;
512
+ function getMemoizedU8Codec() {
513
+ if (!memoizedU8Codec)
514
+ memoizedU8Codec = getU8Codec();
515
+ return memoizedU8Codec;
516
+ }
517
+ function getMemoizedU8CodecDescription(description) {
518
+ const codec = getMemoizedU8Codec();
519
+ return {
520
+ ...codec,
521
+ description: description ?? codec.description
522
+ };
523
+ }
524
+ 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
+ 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
+ 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
+ var messageHeaderDescription = __DEV__ ? "The transaction message header containing counts of the signer, readonly-signer, and readonly-nonsigner account addresses" : void 0;
479
528
  function getMessageHeaderCodec() {
480
- return struct(
529
+ return getStructCodec(
481
530
  [
482
- [
483
- "numSignerAccounts",
484
- u8(
485
- __DEV__ ? {
486
- description: "The expected number of addresses in the static address list belonging to accounts that are required to sign this transaction"
487
- } : void 0
488
- )
489
- ],
490
- [
491
- "numReadonlySignerAccounts",
492
- u8(
493
- __DEV__ ? {
494
- 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"
495
- } : void 0
496
- )
497
- ],
498
- [
499
- "numReadonlyNonSignerAccounts",
500
- u8(
501
- __DEV__ ? {
502
- description: "The expected number of addresses in the static address list belonging to accounts that are neither signers, nor writable"
503
- } : void 0
504
- )
505
- ]
531
+ ["numSignerAccounts", getMemoizedU8CodecDescription(numSignerAccountsDescription)],
532
+ ["numReadonlySignerAccounts", getMemoizedU8CodecDescription(numReadonlySignerAccountsDescription)],
533
+ ["numReadonlyNonSignerAccounts", getMemoizedU8CodecDescription(numReadonlyNonSignerAccountsDescription)]
506
534
  ],
507
- __DEV__ ? {
508
- description: "The transaction message header containing counts of the signer, readonly-signer, and readonly-nonsigner account addresses"
509
- } : void 0
535
+ {
536
+ description: messageHeaderDescription
537
+ }
510
538
  );
511
539
  }
512
540
  function getInstructionCodec() {
@@ -521,7 +549,7 @@ function getInstructionCodec() {
521
549
  )
522
550
  ],
523
551
  [
524
- "addressIndices",
552
+ "accountIndices",
525
553
  array(
526
554
  u8({
527
555
  description: __DEV__ ? "The index of an account, according to the well-ordered accounts list for this transaction" : ""
@@ -541,50 +569,35 @@ function getInstructionCodec() {
541
569
  ]
542
570
  ]),
543
571
  (value) => {
544
- if (value.addressIndices !== void 0 && value.data !== void 0) {
572
+ if (value.accountIndices !== void 0 && value.data !== void 0) {
545
573
  return value;
546
574
  }
547
575
  return {
548
576
  ...value,
549
- addressIndices: value.addressIndices ?? [],
577
+ accountIndices: value.accountIndices ?? [],
550
578
  data: value.data ?? new Uint8Array(0)
551
579
  };
552
580
  },
553
581
  (value) => {
554
- if (value.addressIndices.length && value.data.byteLength) {
582
+ if (value.accountIndices.length && value.data.byteLength) {
555
583
  return value;
556
584
  }
557
- const { addressIndices, data, ...rest } = value;
585
+ const { accountIndices, data, ...rest } = value;
558
586
  return {
559
587
  ...rest,
560
- ...addressIndices.length ? { addressIndices } : null,
588
+ ...accountIndices.length ? { accountIndices } : null,
561
589
  ...data.byteLength ? { data } : null
562
590
  };
563
591
  }
564
592
  );
565
593
  }
566
-
567
- // src/serializers/unimplemented.ts
568
- function getError(type, name) {
569
- const functionSuffix = name + type[0].toUpperCase() + type.slice(1);
570
- return new Error(
571
- `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}`
572
- );
573
- }
574
- function getUnimplementedDecoder(name) {
575
- return () => {
576
- throw getError("decoder", name);
577
- };
578
- }
579
-
580
- // src/serializers/transaction-version.ts
581
594
  var VERSION_FLAG_MASK = 128;
582
595
  var BASE_CONFIG = {
583
596
  description: __DEV__ ? "A single byte that encodes the version of the transaction" : "",
584
597
  fixedSize: null,
585
598
  maxSize: 1
586
599
  };
587
- function deserialize(bytes3, offset = 0) {
600
+ function decode(bytes3, offset = 0) {
588
601
  const firstByte = bytes3[offset];
589
602
  if ((firstByte & VERSION_FLAG_MASK) === 0) {
590
603
  return ["legacy", offset];
@@ -593,7 +606,7 @@ function deserialize(bytes3, offset = 0) {
593
606
  return [version, offset + 1];
594
607
  }
595
608
  }
596
- function serialize(value) {
609
+ function encode(value) {
597
610
  if (value === "legacy") {
598
611
  return new Uint8Array();
599
612
  }
@@ -602,11 +615,32 @@ function serialize(value) {
602
615
  }
603
616
  return new Uint8Array([value | VERSION_FLAG_MASK]);
604
617
  }
605
- function getTransactionVersionCodec() {
618
+ function getTransactionVersionDecoder() {
606
619
  return {
607
620
  ...BASE_CONFIG,
608
- deserialize,
609
- serialize
621
+ decode
622
+ };
623
+ }
624
+ function getTransactionVersionEncoder() {
625
+ return {
626
+ ...BASE_CONFIG,
627
+ encode
628
+ };
629
+ }
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);
610
644
  };
611
645
  }
612
646
 
@@ -616,7 +650,7 @@ var BASE_CONFIG2 = {
616
650
  fixedSize: null,
617
651
  maxSize: null
618
652
  };
619
- function serialize2(compiledMessage) {
653
+ function serialize(compiledMessage) {
620
654
  if (compiledMessage.version === "legacy") {
621
655
  return struct(getPreludeStructSerializerTuple()).serialize(compiledMessage);
622
656
  } else {
@@ -637,13 +671,22 @@ function serialize2(compiledMessage) {
637
671
  ).serialize(compiledMessage);
638
672
  }
639
673
  }
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
+ };
682
+ }
640
683
  function getPreludeStructSerializerTuple() {
641
684
  return [
642
- ["version", getTransactionVersionCodec()],
643
- ["header", getMessageHeaderCodec()],
685
+ ["version", toSerializer(getTransactionVersionCodec())],
686
+ ["header", toSerializer(getMessageHeaderCodec())],
644
687
  [
645
688
  "staticAccounts",
646
- array(getBase58EncodedAddressCodec(), {
689
+ array(toSerializer(getAddressCodec()), {
647
690
  description: __DEV__ ? "A compact-array of static account addresses belonging to this transaction" : "",
648
691
  size: shortU16()
649
692
  })
@@ -675,49 +718,7 @@ function getCompiledMessageEncoder() {
675
718
  return {
676
719
  ...BASE_CONFIG2,
677
720
  deserialize: getUnimplementedDecoder("CompiledMessage"),
678
- serialize: serialize2
679
- };
680
- }
681
-
682
- // src/signatures.ts
683
- async function getCompiledMessageSignature(message, secretKey) {
684
- const wireMessageBytes = getCompiledMessageEncoder().serialize(message);
685
- const signature = await signBytes(secretKey, wireMessageBytes);
686
- return signature;
687
- }
688
- async function signTransaction(keyPair, transaction) {
689
- const compiledMessage = compileMessage(transaction);
690
- const [signerPublicKey, signature] = await Promise.all([
691
- getBase58EncodedAddressFromPublicKey(keyPair.publicKey),
692
- getCompiledMessageSignature(compiledMessage, keyPair.privateKey)
693
- ]);
694
- const nextSignatures = {
695
- ..."signatures" in transaction ? transaction.signatures : null,
696
- ...{ [signerPublicKey]: signature }
697
- };
698
- const out = {
699
- ...transaction,
700
- signatures: nextSignatures
701
- };
702
- Object.freeze(out);
703
- return out;
704
- }
705
-
706
- // src/compile-transaction.ts
707
- function getCompiledTransaction(transaction) {
708
- const compiledMessage = compileMessage(transaction);
709
- let signatures;
710
- if ("signatures" in transaction) {
711
- signatures = [];
712
- for (let ii = 0; ii < compiledMessage.header.numSignerAccounts; ii++) {
713
- signatures[ii] = transaction.signatures[compiledMessage.staticAccounts[ii]] ?? new Uint8Array(Array(64).fill(0));
714
- }
715
- } else {
716
- signatures = Array(compiledMessage.header.numSignerAccounts).fill(new Uint8Array(Array(64).fill(0)));
717
- }
718
- return {
719
- compiledMessage,
720
- signatures
721
+ serialize
721
722
  };
722
723
  }
723
724
 
@@ -727,7 +728,7 @@ var BASE_CONFIG3 = {
727
728
  fixedSize: null,
728
729
  maxSize: null
729
730
  };
730
- function serialize3(transaction) {
731
+ function serialize2(transaction) {
731
732
  const compiledTransaction = getCompiledTransaction(transaction);
732
733
  return struct([
733
734
  [
@@ -744,8 +745,82 @@ function getTransactionEncoder() {
744
745
  return {
745
746
  ...BASE_CONFIG3,
746
747
  deserialize: getUnimplementedDecoder("CompiledMessage"),
747
- serialize: serialize3
748
+ serialize: serialize2
749
+ };
750
+ }
751
+ function assertIsTransactionSignature(putativeTransactionSignature) {
752
+ try {
753
+ if (
754
+ // Lowest value (64 bytes of zeroes)
755
+ putativeTransactionSignature.length < 64 || // Highest value (64 bytes of 255)
756
+ putativeTransactionSignature.length > 88
757
+ ) {
758
+ throw new Error("Expected input string to decode to a byte array of length 64.");
759
+ }
760
+ const bytes3 = base58.serialize(putativeTransactionSignature);
761
+ const numBytes = bytes3.byteLength;
762
+ if (numBytes !== 64) {
763
+ throw new Error(`Expected input string to decode to a byte array of length 64. Actual length: ${numBytes}`);
764
+ }
765
+ } catch (e) {
766
+ throw new Error(`\`${putativeTransactionSignature}\` is not a transaction signature`, {
767
+ cause: e
768
+ });
769
+ }
770
+ }
771
+ function isTransactionSignature(putativeTransactionSignature) {
772
+ if (
773
+ // Lowest value (64 bytes of zeroes)
774
+ putativeTransactionSignature.length < 64 || // Highest value (64 bytes of 255)
775
+ putativeTransactionSignature.length > 88
776
+ ) {
777
+ return false;
778
+ }
779
+ const bytes3 = base58.serialize(putativeTransactionSignature);
780
+ const numBytes = bytes3.byteLength;
781
+ if (numBytes !== 64) {
782
+ return false;
783
+ }
784
+ return true;
785
+ }
786
+ async function getCompiledMessageSignature(message, secretKey) {
787
+ const wireMessageBytes = getCompiledMessageEncoder().serialize(message);
788
+ const signature = await signBytes(secretKey, wireMessageBytes);
789
+ return signature;
790
+ }
791
+ function getSignatureFromTransaction(transaction) {
792
+ const signature = transaction.signatures[transaction.feePayer];
793
+ if (!signature) {
794
+ throw new Error(
795
+ "Could not determine this transaction's signature. Make sure that the transaction has been signed by its fee payer."
796
+ );
797
+ }
798
+ return signature;
799
+ }
800
+ async function signTransaction(keyPairs, transaction) {
801
+ const compiledMessage = compileMessage(transaction);
802
+ const nextSignatures = "signatures" in transaction ? { ...transaction.signatures } : {};
803
+ const publicKeySignaturePairs = await Promise.all(
804
+ keyPairs.map(
805
+ (keyPair) => Promise.all([
806
+ getAddressFromPublicKey(keyPair.publicKey),
807
+ getCompiledMessageSignature(compiledMessage, keyPair.privateKey)
808
+ ])
809
+ )
810
+ );
811
+ for (const [signerPublicKey, signature] of publicKeySignaturePairs) {
812
+ nextSignatures[signerPublicKey] = signature;
813
+ }
814
+ const out = {
815
+ ...transaction,
816
+ signatures: nextSignatures
748
817
  };
818
+ Object.freeze(out);
819
+ return out;
820
+ }
821
+ function transactionSignature(putativeTransactionSignature) {
822
+ assertIsTransactionSignature(putativeTransactionSignature);
823
+ return putativeTransactionSignature;
749
824
  }
750
825
 
751
826
  // src/wire-transaction.ts
@@ -756,6 +831,6 @@ function getBase64EncodedWireTransaction(transaction) {
756
831
  }
757
832
  }
758
833
 
759
- export { appendTransactionInstruction, assertIsBlockhash, assertIsDurableNonceTransaction, createTransaction, getBase64EncodedWireTransaction, prependTransactionInstruction, setTransactionFeePayer, setTransactionLifetimeUsingBlockhash, setTransactionLifetimeUsingDurableNonce, signTransaction };
834
+ export { appendTransactionInstruction, assertIsBlockhash, assertIsDurableNonceTransaction, assertIsTransactionSignature, createTransaction, getBase64EncodedWireTransaction, getSignatureFromTransaction, getTransactionEncoder, isTransactionSignature, prependTransactionInstruction, setTransactionFeePayer, setTransactionLifetimeUsingBlockhash, setTransactionLifetimeUsingDurableNonce, signTransaction, transactionSignature };
760
835
  //# sourceMappingURL=out.js.map
761
836
  //# sourceMappingURL=index.browser.js.map