@solana/web3.js 1.57.0 → 1.57.1

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.
@@ -608,6 +608,44 @@ function encodeLength(bytes, len) {
608
608
  }
609
609
  }
610
610
 
611
+ const END_OF_BUFFER_ERROR_MESSAGE = 'Reached end of buffer unexpectedly';
612
+ /**
613
+ * Delegates to `Array#shift`, but throws if the array is zero-length.
614
+ */
615
+
616
+ function guardedShift(byteArray) {
617
+ if (byteArray.length === 0) {
618
+ throw new Error(END_OF_BUFFER_ERROR_MESSAGE);
619
+ }
620
+
621
+ return byteArray.shift();
622
+ }
623
+ /**
624
+ * Delegates to `Array#splice`, but throws if the section being spliced out extends past the end of
625
+ * the array.
626
+ */
627
+
628
+ function guardedSplice(byteArray, ...args) {
629
+ var _args$;
630
+
631
+ const [start] = args;
632
+
633
+ if (args.length === 2 // Implies that `deleteCount` was supplied
634
+ ? start + ((_args$ = args[1]) !== null && _args$ !== void 0 ? _args$ : 0) > byteArray.length : start >= byteArray.length) {
635
+ throw new Error(END_OF_BUFFER_ERROR_MESSAGE);
636
+ }
637
+
638
+ return byteArray.splice(...args);
639
+ }
640
+
641
+ /**
642
+ * An instruction to execute by a program
643
+ *
644
+ * @property {number} programIdIndex
645
+ * @property {number[]} accounts
646
+ * @property {string} data
647
+ */
648
+
611
649
  /**
612
650
  * List of instructions to be processed atomically
613
651
  */
@@ -720,37 +758,33 @@ class Message {
720
758
  static from(buffer) {
721
759
  // Slice up wire data
722
760
  let byteArray = [...buffer];
723
- const numRequiredSignatures = byteArray.shift();
761
+ const numRequiredSignatures = guardedShift(byteArray);
724
762
 
725
763
  if (numRequiredSignatures !== (numRequiredSignatures & VERSION_PREFIX_MASK)) {
726
764
  throw new Error('Versioned messages must be deserialized with VersionedMessage.deserialize()');
727
765
  }
728
766
 
729
- const numReadonlySignedAccounts = byteArray.shift();
730
- const numReadonlyUnsignedAccounts = byteArray.shift();
767
+ const numReadonlySignedAccounts = guardedShift(byteArray);
768
+ const numReadonlyUnsignedAccounts = guardedShift(byteArray);
731
769
  const accountCount = decodeLength(byteArray);
732
770
  let accountKeys = [];
733
771
 
734
772
  for (let i = 0; i < accountCount; i++) {
735
- const account = byteArray.slice(0, PUBLIC_KEY_LENGTH);
736
- byteArray = byteArray.slice(PUBLIC_KEY_LENGTH);
773
+ const account = guardedSplice(byteArray, 0, PUBLIC_KEY_LENGTH);
737
774
  accountKeys.push(bs58.encode(Buffer.from(account)));
738
775
  }
739
776
 
740
- const recentBlockhash = byteArray.slice(0, PUBLIC_KEY_LENGTH);
741
- byteArray = byteArray.slice(PUBLIC_KEY_LENGTH);
777
+ const recentBlockhash = guardedSplice(byteArray, 0, PUBLIC_KEY_LENGTH);
742
778
  const instructionCount = decodeLength(byteArray);
743
779
  let instructions = [];
744
780
 
745
781
  for (let i = 0; i < instructionCount; i++) {
746
- const programIdIndex = byteArray.shift();
782
+ const programIdIndex = guardedShift(byteArray);
747
783
  const accountCount = decodeLength(byteArray);
748
- const accounts = byteArray.slice(0, accountCount);
749
- byteArray = byteArray.slice(accountCount);
784
+ const accounts = guardedSplice(byteArray, 0, accountCount);
750
785
  const dataLength = decodeLength(byteArray);
751
- const dataSlice = byteArray.slice(0, dataLength);
786
+ const dataSlice = guardedSplice(byteArray, 0, dataLength);
752
787
  const data = bs58.encode(Buffer.from(dataSlice));
753
- byteArray = byteArray.slice(dataLength);
754
788
  instructions.push({
755
789
  programIdIndex,
756
790
  accounts,
@@ -1017,33 +1051,33 @@ class MessageV0 {
1017
1051
 
1018
1052
  static deserialize(serializedMessage) {
1019
1053
  let byteArray = [...serializedMessage];
1020
- const prefix = byteArray.shift();
1054
+ const prefix = guardedShift(byteArray);
1021
1055
  const maskedPrefix = prefix & VERSION_PREFIX_MASK;
1022
1056
  assert(prefix !== maskedPrefix, `Expected versioned message but received legacy message`);
1023
1057
  const version = maskedPrefix;
1024
1058
  assert(version === 0, `Expected versioned message with version 0 but found version ${version}`);
1025
1059
  const header = {
1026
- numRequiredSignatures: byteArray.shift(),
1027
- numReadonlySignedAccounts: byteArray.shift(),
1028
- numReadonlyUnsignedAccounts: byteArray.shift()
1060
+ numRequiredSignatures: guardedShift(byteArray),
1061
+ numReadonlySignedAccounts: guardedShift(byteArray),
1062
+ numReadonlyUnsignedAccounts: guardedShift(byteArray)
1029
1063
  };
1030
1064
  const staticAccountKeys = [];
1031
1065
  const staticAccountKeysLength = decodeLength(byteArray);
1032
1066
 
1033
1067
  for (let i = 0; i < staticAccountKeysLength; i++) {
1034
- staticAccountKeys.push(new PublicKey(byteArray.splice(0, PUBLIC_KEY_LENGTH)));
1068
+ staticAccountKeys.push(new PublicKey(guardedSplice(byteArray, 0, PUBLIC_KEY_LENGTH)));
1035
1069
  }
1036
1070
 
1037
- const recentBlockhash = bs58.encode(byteArray.splice(0, PUBLIC_KEY_LENGTH));
1071
+ const recentBlockhash = bs58.encode(guardedSplice(byteArray, 0, PUBLIC_KEY_LENGTH));
1038
1072
  const instructionCount = decodeLength(byteArray);
1039
1073
  const compiledInstructions = [];
1040
1074
 
1041
1075
  for (let i = 0; i < instructionCount; i++) {
1042
- const programIdIndex = byteArray.shift();
1076
+ const programIdIndex = guardedShift(byteArray);
1043
1077
  const accountKeyIndexesLength = decodeLength(byteArray);
1044
- const accountKeyIndexes = byteArray.splice(0, accountKeyIndexesLength);
1078
+ const accountKeyIndexes = guardedSplice(byteArray, 0, accountKeyIndexesLength);
1045
1079
  const dataLength = decodeLength(byteArray);
1046
- const data = new Uint8Array(byteArray.splice(0, dataLength));
1080
+ const data = new Uint8Array(guardedSplice(byteArray, 0, dataLength));
1047
1081
  compiledInstructions.push({
1048
1082
  programIdIndex,
1049
1083
  accountKeyIndexes,
@@ -1055,11 +1089,11 @@ class MessageV0 {
1055
1089
  const addressTableLookups = [];
1056
1090
 
1057
1091
  for (let i = 0; i < addressTableLookupsCount; i++) {
1058
- const accountKey = new PublicKey(byteArray.splice(0, PUBLIC_KEY_LENGTH));
1092
+ const accountKey = new PublicKey(guardedSplice(byteArray, 0, PUBLIC_KEY_LENGTH));
1059
1093
  const writableIndexesLength = decodeLength(byteArray);
1060
- const writableIndexes = byteArray.splice(0, writableIndexesLength);
1094
+ const writableIndexes = guardedSplice(byteArray, 0, writableIndexesLength);
1061
1095
  const readonlyIndexesLength = decodeLength(byteArray);
1062
- const readonlyIndexes = byteArray.splice(0, readonlyIndexesLength);
1096
+ const readonlyIndexes = guardedSplice(byteArray, 0, readonlyIndexesLength);
1063
1097
  addressTableLookups.push({
1064
1098
  accountKey,
1065
1099
  writableIndexes,
@@ -1790,8 +1824,7 @@ class Transaction {
1790
1824
  let signatures = [];
1791
1825
 
1792
1826
  for (let i = 0; i < signatureCount; i++) {
1793
- const signature = byteArray.slice(0, SIGNATURE_LENGTH_IN_BYTES);
1794
- byteArray = byteArray.slice(SIGNATURE_LENGTH_IN_BYTES);
1827
+ const signature = guardedSplice(byteArray, 0, SIGNATURE_LENGTH_IN_BYTES);
1795
1828
  signatures.push(bs58.encode(Buffer.from(signature)));
1796
1829
  }
1797
1830
 
@@ -1883,7 +1916,7 @@ class VersionedTransaction {
1883
1916
  const signaturesLength = decodeLength(byteArray);
1884
1917
 
1885
1918
  for (let i = 0; i < signaturesLength; i++) {
1886
- signatures.push(new Uint8Array(byteArray.splice(0, SIGNATURE_LENGTH_IN_BYTES)));
1919
+ signatures.push(new Uint8Array(guardedSplice(byteArray, 0, SIGNATURE_LENGTH_IN_BYTES)));
1887
1920
  }
1888
1921
 
1889
1922
  const message = VersionedMessage.deserialize(new Uint8Array(byteArray));
@@ -4282,7 +4315,7 @@ const LogsNotificationResult = type({
4282
4315
 
4283
4316
  /** @internal */
4284
4317
  const COMMON_HTTP_HEADERS = {
4285
- 'solana-client': `js/${(_process$env$npm_pack = "0.0.0-development") !== null && _process$env$npm_pack !== void 0 ? _process$env$npm_pack : 'UNKNOWN'}`
4318
+ 'solana-client': `js/${(_process$env$npm_pack = "1.57.1") !== null && _process$env$npm_pack !== void 0 ? _process$env$npm_pack : 'UNKNOWN'}`
4286
4319
  };
4287
4320
  /**
4288
4321
  * A connection to a fullnode JSON RPC endpoint
@@ -9060,10 +9093,8 @@ class ValidatorInfo {
9060
9093
  const configKeys = [];
9061
9094
 
9062
9095
  for (let i = 0; i < 2; i++) {
9063
- const publicKey = new PublicKey(byteArray.slice(0, PUBLIC_KEY_LENGTH));
9064
- byteArray = byteArray.slice(PUBLIC_KEY_LENGTH);
9065
- const isSigner = byteArray.slice(0, 1)[0] === 1;
9066
- byteArray = byteArray.slice(1);
9096
+ const publicKey = new PublicKey(guardedSplice(byteArray, 0, PUBLIC_KEY_LENGTH));
9097
+ const isSigner = guardedShift(byteArray) === 1;
9067
9098
  configKeys.push({
9068
9099
  publicKey,
9069
9100
  isSigner