@solana/web3.js 1.54.1 → 1.54.2

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.
@@ -531,6 +531,44 @@ function encodeLength(bytes, len) {
531
531
  }
532
532
  }
533
533
 
534
+ const END_OF_BUFFER_ERROR_MESSAGE = 'Reached end of buffer unexpectedly';
535
+ /**
536
+ * Delegates to `Array#shift`, but throws if the array is zero-length.
537
+ */
538
+
539
+ function guardedShift(byteArray) {
540
+ if (byteArray.length === 0) {
541
+ throw new Error(END_OF_BUFFER_ERROR_MESSAGE);
542
+ }
543
+
544
+ return byteArray.shift();
545
+ }
546
+ /**
547
+ * Delegates to `Array#splice`, but throws if the section being spliced out extends past the end of
548
+ * the array.
549
+ */
550
+
551
+ function guardedSplice(byteArray, ...args) {
552
+ var _args$;
553
+
554
+ const [start] = args;
555
+
556
+ if (args.length === 2 // Implies that `deleteCount` was supplied
557
+ ? start + ((_args$ = args[1]) !== null && _args$ !== void 0 ? _args$ : 0) > byteArray.length : start >= byteArray.length) {
558
+ throw new Error(END_OF_BUFFER_ERROR_MESSAGE);
559
+ }
560
+
561
+ return byteArray.splice(...args);
562
+ }
563
+
564
+ /**
565
+ * An instruction to execute by a program
566
+ *
567
+ * @property {number} programIdIndex
568
+ * @property {number[]} accounts
569
+ * @property {string} data
570
+ */
571
+
534
572
  /**
535
573
  * List of instructions to be processed atomically
536
574
  */
@@ -643,37 +681,33 @@ class Message {
643
681
  static from(buffer) {
644
682
  // Slice up wire data
645
683
  let byteArray = [...buffer];
646
- const numRequiredSignatures = byteArray.shift();
684
+ const numRequiredSignatures = guardedShift(byteArray);
647
685
 
648
686
  if (numRequiredSignatures !== (numRequiredSignatures & VERSION_PREFIX_MASK)) {
649
687
  throw new Error('Versioned messages must be deserialized with VersionedMessage.deserialize()');
650
688
  }
651
689
 
652
- const numReadonlySignedAccounts = byteArray.shift();
653
- const numReadonlyUnsignedAccounts = byteArray.shift();
690
+ const numReadonlySignedAccounts = guardedShift(byteArray);
691
+ const numReadonlyUnsignedAccounts = guardedShift(byteArray);
654
692
  const accountCount = decodeLength(byteArray);
655
693
  let accountKeys = [];
656
694
 
657
695
  for (let i = 0; i < accountCount; i++) {
658
- const account = byteArray.slice(0, PUBLIC_KEY_LENGTH);
659
- byteArray = byteArray.slice(PUBLIC_KEY_LENGTH);
696
+ const account = guardedSplice(byteArray, 0, PUBLIC_KEY_LENGTH);
660
697
  accountKeys.push(bs58.encode(Buffer.from(account)));
661
698
  }
662
699
 
663
- const recentBlockhash = byteArray.slice(0, PUBLIC_KEY_LENGTH);
664
- byteArray = byteArray.slice(PUBLIC_KEY_LENGTH);
700
+ const recentBlockhash = guardedSplice(byteArray, 0, PUBLIC_KEY_LENGTH);
665
701
  const instructionCount = decodeLength(byteArray);
666
702
  let instructions = [];
667
703
 
668
704
  for (let i = 0; i < instructionCount; i++) {
669
- const programIdIndex = byteArray.shift();
705
+ const programIdIndex = guardedShift(byteArray);
670
706
  const accountCount = decodeLength(byteArray);
671
- const accounts = byteArray.slice(0, accountCount);
672
- byteArray = byteArray.slice(accountCount);
707
+ const accounts = guardedSplice(byteArray, 0, accountCount);
673
708
  const dataLength = decodeLength(byteArray);
674
- const dataSlice = byteArray.slice(0, dataLength);
709
+ const dataSlice = guardedSplice(byteArray, 0, dataLength);
675
710
  const data = bs58.encode(Buffer.from(dataSlice));
676
- byteArray = byteArray.slice(dataLength);
677
711
  instructions.push({
678
712
  programIdIndex,
679
713
  accounts,
@@ -796,33 +830,33 @@ class MessageV0 {
796
830
 
797
831
  static deserialize(serializedMessage) {
798
832
  let byteArray = [...serializedMessage];
799
- const prefix = byteArray.shift();
833
+ const prefix = guardedShift(byteArray);
800
834
  const maskedPrefix = prefix & VERSION_PREFIX_MASK;
801
835
  assert(prefix !== maskedPrefix, `Expected versioned message but received legacy message`);
802
836
  const version = maskedPrefix;
803
837
  assert(version === 0, `Expected versioned message with version 0 but found version ${version}`);
804
838
  const header = {
805
- numRequiredSignatures: byteArray.shift(),
806
- numReadonlySignedAccounts: byteArray.shift(),
807
- numReadonlyUnsignedAccounts: byteArray.shift()
839
+ numRequiredSignatures: guardedShift(byteArray),
840
+ numReadonlySignedAccounts: guardedShift(byteArray),
841
+ numReadonlyUnsignedAccounts: guardedShift(byteArray)
808
842
  };
809
843
  const staticAccountKeys = [];
810
844
  const staticAccountKeysLength = decodeLength(byteArray);
811
845
 
812
846
  for (let i = 0; i < staticAccountKeysLength; i++) {
813
- staticAccountKeys.push(new PublicKey(byteArray.splice(0, PUBLIC_KEY_LENGTH)));
847
+ staticAccountKeys.push(new PublicKey(guardedSplice(byteArray, 0, PUBLIC_KEY_LENGTH)));
814
848
  }
815
849
 
816
- const recentBlockhash = bs58.encode(byteArray.splice(0, PUBLIC_KEY_LENGTH));
850
+ const recentBlockhash = bs58.encode(guardedSplice(byteArray, 0, PUBLIC_KEY_LENGTH));
817
851
  const instructionCount = decodeLength(byteArray);
818
852
  const compiledInstructions = [];
819
853
 
820
854
  for (let i = 0; i < instructionCount; i++) {
821
- const programIdIndex = byteArray.shift();
855
+ const programIdIndex = guardedShift(byteArray);
822
856
  const accountKeyIndexesLength = decodeLength(byteArray);
823
- const accountKeyIndexes = byteArray.splice(0, accountKeyIndexesLength);
857
+ const accountKeyIndexes = guardedSplice(byteArray, 0, accountKeyIndexesLength);
824
858
  const dataLength = decodeLength(byteArray);
825
- const data = new Uint8Array(byteArray.splice(0, dataLength));
859
+ const data = new Uint8Array(guardedSplice(byteArray, 0, dataLength));
826
860
  compiledInstructions.push({
827
861
  programIdIndex,
828
862
  accountKeyIndexes,
@@ -834,11 +868,11 @@ class MessageV0 {
834
868
  const addressTableLookups = [];
835
869
 
836
870
  for (let i = 0; i < addressTableLookupsCount; i++) {
837
- const accountKey = new PublicKey(byteArray.splice(0, PUBLIC_KEY_LENGTH));
871
+ const accountKey = new PublicKey(guardedSplice(byteArray, 0, PUBLIC_KEY_LENGTH));
838
872
  const writableIndexesLength = decodeLength(byteArray);
839
- const writableIndexes = byteArray.splice(0, writableIndexesLength);
873
+ const writableIndexes = guardedSplice(byteArray, 0, writableIndexesLength);
840
874
  const readonlyIndexesLength = decodeLength(byteArray);
841
- const readonlyIndexes = byteArray.splice(0, readonlyIndexesLength);
875
+ const readonlyIndexes = guardedSplice(byteArray, 0, readonlyIndexesLength);
842
876
  addressTableLookups.push({
843
877
  accountKey,
844
878
  writableIndexes,
@@ -1561,8 +1595,7 @@ class Transaction {
1561
1595
  let signatures = [];
1562
1596
 
1563
1597
  for (let i = 0; i < signatureCount; i++) {
1564
- const signature = byteArray.slice(0, SIGNATURE_LENGTH_IN_BYTES);
1565
- byteArray = byteArray.slice(SIGNATURE_LENGTH_IN_BYTES);
1598
+ const signature = guardedSplice(byteArray, 0, SIGNATURE_LENGTH_IN_BYTES);
1566
1599
  signatures.push(bs58.encode(Buffer.from(signature)));
1567
1600
  }
1568
1601
 
@@ -1654,7 +1687,7 @@ class VersionedTransaction {
1654
1687
  const signaturesLength = decodeLength(byteArray);
1655
1688
 
1656
1689
  for (let i = 0; i < signaturesLength; i++) {
1657
- signatures.push(new Uint8Array(byteArray.splice(0, SIGNATURE_LENGTH_IN_BYTES)));
1690
+ signatures.push(new Uint8Array(guardedSplice(byteArray, 0, SIGNATURE_LENGTH_IN_BYTES)));
1658
1691
  }
1659
1692
 
1660
1693
  const message = VersionedMessage.deserialize(new Uint8Array(byteArray));
@@ -4018,7 +4051,7 @@ const LogsNotificationResult = type({
4018
4051
 
4019
4052
  /** @internal */
4020
4053
  const COMMON_HTTP_HEADERS = {
4021
- 'solana-client': `js/${(_process$env$npm_pack = "0.0.0-development") !== null && _process$env$npm_pack !== void 0 ? _process$env$npm_pack : 'UNKNOWN'}`
4054
+ 'solana-client': `js/${(_process$env$npm_pack = "1.54.2") !== null && _process$env$npm_pack !== void 0 ? _process$env$npm_pack : 'UNKNOWN'}`
4022
4055
  };
4023
4056
  /**
4024
4057
  * A connection to a fullnode JSON RPC endpoint
@@ -8772,10 +8805,8 @@ class ValidatorInfo {
8772
8805
  const configKeys = [];
8773
8806
 
8774
8807
  for (let i = 0; i < 2; i++) {
8775
- const publicKey = new PublicKey(byteArray.slice(0, PUBLIC_KEY_LENGTH));
8776
- byteArray = byteArray.slice(PUBLIC_KEY_LENGTH);
8777
- const isSigner = byteArray.slice(0, 1)[0] === 1;
8778
- byteArray = byteArray.slice(1);
8808
+ const publicKey = new PublicKey(guardedSplice(byteArray, 0, PUBLIC_KEY_LENGTH));
8809
+ const isSigner = guardedShift(byteArray) === 1;
8779
8810
  configKeys.push({
8780
8811
  publicKey,
8781
8812
  isSigner