@solana/web3.js 1.56.2 → 1.56.3

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,
@@ -1569,8 +1603,7 @@ class Transaction {
1569
1603
  let signatures = [];
1570
1604
 
1571
1605
  for (let i = 0; i < signatureCount; i++) {
1572
- const signature = byteArray.slice(0, SIGNATURE_LENGTH_IN_BYTES);
1573
- byteArray = byteArray.slice(SIGNATURE_LENGTH_IN_BYTES);
1606
+ const signature = guardedSplice(byteArray, 0, SIGNATURE_LENGTH_IN_BYTES);
1574
1607
  signatures.push(bs58.encode(Buffer.from(signature)));
1575
1608
  }
1576
1609
 
@@ -1662,7 +1695,7 @@ class VersionedTransaction {
1662
1695
  const signaturesLength = decodeLength(byteArray);
1663
1696
 
1664
1697
  for (let i = 0; i < signaturesLength; i++) {
1665
- signatures.push(new Uint8Array(byteArray.splice(0, SIGNATURE_LENGTH_IN_BYTES)));
1698
+ signatures.push(new Uint8Array(guardedSplice(byteArray, 0, SIGNATURE_LENGTH_IN_BYTES)));
1666
1699
  }
1667
1700
 
1668
1701
  const message = VersionedMessage.deserialize(new Uint8Array(byteArray));
@@ -4061,7 +4094,7 @@ const LogsNotificationResult = type({
4061
4094
 
4062
4095
  /** @internal */
4063
4096
  const COMMON_HTTP_HEADERS = {
4064
- 'solana-client': `js/${(_process$env$npm_pack = "0.0.0-development") !== null && _process$env$npm_pack !== void 0 ? _process$env$npm_pack : 'UNKNOWN'}`
4097
+ 'solana-client': `js/${(_process$env$npm_pack = "1.56.3") !== null && _process$env$npm_pack !== void 0 ? _process$env$npm_pack : 'UNKNOWN'}`
4065
4098
  };
4066
4099
  /**
4067
4100
  * A connection to a fullnode JSON RPC endpoint
@@ -8839,10 +8872,8 @@ class ValidatorInfo {
8839
8872
  const configKeys = [];
8840
8873
 
8841
8874
  for (let i = 0; i < 2; i++) {
8842
- const publicKey = new PublicKey(byteArray.slice(0, PUBLIC_KEY_LENGTH));
8843
- byteArray = byteArray.slice(PUBLIC_KEY_LENGTH);
8844
- const isSigner = byteArray.slice(0, 1)[0] === 1;
8845
- byteArray = byteArray.slice(1);
8875
+ const publicKey = new PublicKey(guardedSplice(byteArray, 0, PUBLIC_KEY_LENGTH));
8876
+ const isSigner = guardedShift(byteArray) === 1;
8846
8877
  configKeys.push({
8847
8878
  publicKey,
8848
8879
  isSigner