@solana/web3.js 1.91.1 → 1.91.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.
package/lib/index.cjs.js CHANGED
@@ -708,6 +708,31 @@ class CompiledKeys {
708
708
  }
709
709
  }
710
710
 
711
+ const END_OF_BUFFER_ERROR_MESSAGE = 'Reached end of buffer unexpectedly';
712
+
713
+ /**
714
+ * Delegates to `Array#shift`, but throws if the array is zero-length.
715
+ */
716
+ function guardedShift(byteArray) {
717
+ if (byteArray.length === 0) {
718
+ throw new Error(END_OF_BUFFER_ERROR_MESSAGE);
719
+ }
720
+ return byteArray.shift();
721
+ }
722
+
723
+ /**
724
+ * Delegates to `Array#splice`, but throws if the section being spliced out extends past the end of
725
+ * the array.
726
+ */
727
+ function guardedSplice(byteArray, ...args) {
728
+ const [start] = args;
729
+ if (args.length === 2 // Implies that `deleteCount` was supplied
730
+ ? start + (args[1] ?? 0) > byteArray.length : start >= byteArray.length) {
731
+ throw new Error(END_OF_BUFFER_ERROR_MESSAGE);
732
+ }
733
+ return byteArray.splice(...args);
734
+ }
735
+
711
736
  /**
712
737
  * An instruction to execute by a program
713
738
  *
@@ -849,27 +874,27 @@ class Message {
849
874
  static from(buffer$1) {
850
875
  // Slice up wire data
851
876
  let byteArray = [...buffer$1];
852
- const numRequiredSignatures = byteArray.shift();
877
+ const numRequiredSignatures = guardedShift(byteArray);
853
878
  if (numRequiredSignatures !== (numRequiredSignatures & VERSION_PREFIX_MASK)) {
854
879
  throw new Error('Versioned messages must be deserialized with VersionedMessage.deserialize()');
855
880
  }
856
- const numReadonlySignedAccounts = byteArray.shift();
857
- const numReadonlyUnsignedAccounts = byteArray.shift();
881
+ const numReadonlySignedAccounts = guardedShift(byteArray);
882
+ const numReadonlyUnsignedAccounts = guardedShift(byteArray);
858
883
  const accountCount = decodeLength(byteArray);
859
884
  let accountKeys = [];
860
885
  for (let i = 0; i < accountCount; i++) {
861
- const account = byteArray.splice(0, PUBLIC_KEY_LENGTH);
886
+ const account = guardedSplice(byteArray, 0, PUBLIC_KEY_LENGTH);
862
887
  accountKeys.push(new PublicKey(buffer.Buffer.from(account)));
863
888
  }
864
- const recentBlockhash = byteArray.splice(0, PUBLIC_KEY_LENGTH);
889
+ const recentBlockhash = guardedSplice(byteArray, 0, PUBLIC_KEY_LENGTH);
865
890
  const instructionCount = decodeLength(byteArray);
866
891
  let instructions = [];
867
892
  for (let i = 0; i < instructionCount; i++) {
868
- const programIdIndex = byteArray.shift();
893
+ const programIdIndex = guardedShift(byteArray);
869
894
  const accountCount = decodeLength(byteArray);
870
- const accounts = byteArray.splice(0, accountCount);
895
+ const accounts = guardedSplice(byteArray, 0, accountCount);
871
896
  const dataLength = decodeLength(byteArray);
872
- const dataSlice = byteArray.splice(0, dataLength);
897
+ const dataSlice = guardedSplice(byteArray, 0, dataLength);
873
898
  const data = bs58__default.default.encode(buffer.Buffer.from(dataSlice));
874
899
  instructions.push({
875
900
  programIdIndex,
@@ -1075,30 +1100,30 @@ class MessageV0 {
1075
1100
  }
1076
1101
  static deserialize(serializedMessage) {
1077
1102
  let byteArray = [...serializedMessage];
1078
- const prefix = byteArray.shift();
1103
+ const prefix = guardedShift(byteArray);
1079
1104
  const maskedPrefix = prefix & VERSION_PREFIX_MASK;
1080
1105
  assert(prefix !== maskedPrefix, `Expected versioned message but received legacy message`);
1081
1106
  const version = maskedPrefix;
1082
1107
  assert(version === 0, `Expected versioned message with version 0 but found version ${version}`);
1083
1108
  const header = {
1084
- numRequiredSignatures: byteArray.shift(),
1085
- numReadonlySignedAccounts: byteArray.shift(),
1086
- numReadonlyUnsignedAccounts: byteArray.shift()
1109
+ numRequiredSignatures: guardedShift(byteArray),
1110
+ numReadonlySignedAccounts: guardedShift(byteArray),
1111
+ numReadonlyUnsignedAccounts: guardedShift(byteArray)
1087
1112
  };
1088
1113
  const staticAccountKeys = [];
1089
1114
  const staticAccountKeysLength = decodeLength(byteArray);
1090
1115
  for (let i = 0; i < staticAccountKeysLength; i++) {
1091
- staticAccountKeys.push(new PublicKey(byteArray.splice(0, PUBLIC_KEY_LENGTH)));
1116
+ staticAccountKeys.push(new PublicKey(guardedSplice(byteArray, 0, PUBLIC_KEY_LENGTH)));
1092
1117
  }
1093
- const recentBlockhash = bs58__default.default.encode(byteArray.splice(0, PUBLIC_KEY_LENGTH));
1118
+ const recentBlockhash = bs58__default.default.encode(guardedSplice(byteArray, 0, PUBLIC_KEY_LENGTH));
1094
1119
  const instructionCount = decodeLength(byteArray);
1095
1120
  const compiledInstructions = [];
1096
1121
  for (let i = 0; i < instructionCount; i++) {
1097
- const programIdIndex = byteArray.shift();
1122
+ const programIdIndex = guardedShift(byteArray);
1098
1123
  const accountKeyIndexesLength = decodeLength(byteArray);
1099
- const accountKeyIndexes = byteArray.splice(0, accountKeyIndexesLength);
1124
+ const accountKeyIndexes = guardedSplice(byteArray, 0, accountKeyIndexesLength);
1100
1125
  const dataLength = decodeLength(byteArray);
1101
- const data = new Uint8Array(byteArray.splice(0, dataLength));
1126
+ const data = new Uint8Array(guardedSplice(byteArray, 0, dataLength));
1102
1127
  compiledInstructions.push({
1103
1128
  programIdIndex,
1104
1129
  accountKeyIndexes,
@@ -1108,11 +1133,11 @@ class MessageV0 {
1108
1133
  const addressTableLookupsCount = decodeLength(byteArray);
1109
1134
  const addressTableLookups = [];
1110
1135
  for (let i = 0; i < addressTableLookupsCount; i++) {
1111
- const accountKey = new PublicKey(byteArray.splice(0, PUBLIC_KEY_LENGTH));
1136
+ const accountKey = new PublicKey(guardedSplice(byteArray, 0, PUBLIC_KEY_LENGTH));
1112
1137
  const writableIndexesLength = decodeLength(byteArray);
1113
- const writableIndexes = byteArray.splice(0, writableIndexesLength);
1138
+ const writableIndexes = guardedSplice(byteArray, 0, writableIndexesLength);
1114
1139
  const readonlyIndexesLength = decodeLength(byteArray);
1115
- const readonlyIndexes = byteArray.splice(0, readonlyIndexesLength);
1140
+ const readonlyIndexes = guardedSplice(byteArray, 0, readonlyIndexesLength);
1116
1141
  addressTableLookups.push({
1117
1142
  accountKey,
1118
1143
  writableIndexes,
@@ -1903,7 +1928,7 @@ class Transaction {
1903
1928
  const signatureCount = decodeLength(byteArray);
1904
1929
  let signatures = [];
1905
1930
  for (let i = 0; i < signatureCount; i++) {
1906
- const signature = byteArray.splice(0, SIGNATURE_LENGTH_IN_BYTES);
1931
+ const signature = guardedSplice(byteArray, 0, SIGNATURE_LENGTH_IN_BYTES);
1907
1932
  signatures.push(bs58__default.default.encode(buffer.Buffer.from(signature)));
1908
1933
  }
1909
1934
  return Transaction.populate(Message.from(byteArray), signatures);
@@ -2078,7 +2103,7 @@ class VersionedTransaction {
2078
2103
  const signatures = [];
2079
2104
  const signaturesLength = decodeLength(byteArray);
2080
2105
  for (let i = 0; i < signaturesLength; i++) {
2081
- signatures.push(new Uint8Array(byteArray.splice(0, SIGNATURE_LENGTH_IN_BYTES)));
2106
+ signatures.push(new Uint8Array(guardedSplice(byteArray, 0, SIGNATURE_LENGTH_IN_BYTES)));
2082
2107
  }
2083
2108
  const message = VersionedMessage.deserialize(new Uint8Array(byteArray));
2084
2109
  return new VersionedTransaction(message, signatures);
@@ -10817,8 +10842,8 @@ class ValidatorInfo {
10817
10842
  if (configKeyCount !== 2) return null;
10818
10843
  const configKeys = [];
10819
10844
  for (let i = 0; i < 2; i++) {
10820
- const publicKey = new PublicKey(byteArray.splice(0, PUBLIC_KEY_LENGTH));
10821
- const isSigner = byteArray.splice(0, 1)[0] === 1;
10845
+ const publicKey = new PublicKey(guardedSplice(byteArray, 0, PUBLIC_KEY_LENGTH));
10846
+ const isSigner = guardedShift(byteArray) === 1;
10822
10847
  configKeys.push({
10823
10848
  publicKey,
10824
10849
  isSigner