@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.
@@ -700,6 +700,31 @@ class CompiledKeys {
700
700
  }
701
701
  }
702
702
 
703
+ const END_OF_BUFFER_ERROR_MESSAGE = 'Reached end of buffer unexpectedly';
704
+
705
+ /**
706
+ * Delegates to `Array#shift`, but throws if the array is zero-length.
707
+ */
708
+ function guardedShift(byteArray) {
709
+ if (byteArray.length === 0) {
710
+ throw new Error(END_OF_BUFFER_ERROR_MESSAGE);
711
+ }
712
+ return byteArray.shift();
713
+ }
714
+
715
+ /**
716
+ * Delegates to `Array#splice`, but throws if the section being spliced out extends past the end of
717
+ * the array.
718
+ */
719
+ function guardedSplice(byteArray, ...args) {
720
+ const [start] = args;
721
+ if (args.length === 2 // Implies that `deleteCount` was supplied
722
+ ? start + (args[1] ?? 0) > byteArray.length : start >= byteArray.length) {
723
+ throw new Error(END_OF_BUFFER_ERROR_MESSAGE);
724
+ }
725
+ return byteArray.splice(...args);
726
+ }
727
+
703
728
  /**
704
729
  * An instruction to execute by a program
705
730
  *
@@ -841,27 +866,27 @@ class Message {
841
866
  static from(buffer$1) {
842
867
  // Slice up wire data
843
868
  let byteArray = [...buffer$1];
844
- const numRequiredSignatures = byteArray.shift();
869
+ const numRequiredSignatures = guardedShift(byteArray);
845
870
  if (numRequiredSignatures !== (numRequiredSignatures & VERSION_PREFIX_MASK)) {
846
871
  throw new Error('Versioned messages must be deserialized with VersionedMessage.deserialize()');
847
872
  }
848
- const numReadonlySignedAccounts = byteArray.shift();
849
- const numReadonlyUnsignedAccounts = byteArray.shift();
873
+ const numReadonlySignedAccounts = guardedShift(byteArray);
874
+ const numReadonlyUnsignedAccounts = guardedShift(byteArray);
850
875
  const accountCount = decodeLength(byteArray);
851
876
  let accountKeys = [];
852
877
  for (let i = 0; i < accountCount; i++) {
853
- const account = byteArray.splice(0, PUBLIC_KEY_LENGTH);
878
+ const account = guardedSplice(byteArray, 0, PUBLIC_KEY_LENGTH);
854
879
  accountKeys.push(new PublicKey(buffer.Buffer.from(account)));
855
880
  }
856
- const recentBlockhash = byteArray.splice(0, PUBLIC_KEY_LENGTH);
881
+ const recentBlockhash = guardedSplice(byteArray, 0, PUBLIC_KEY_LENGTH);
857
882
  const instructionCount = decodeLength(byteArray);
858
883
  let instructions = [];
859
884
  for (let i = 0; i < instructionCount; i++) {
860
- const programIdIndex = byteArray.shift();
885
+ const programIdIndex = guardedShift(byteArray);
861
886
  const accountCount = decodeLength(byteArray);
862
- const accounts = byteArray.splice(0, accountCount);
887
+ const accounts = guardedSplice(byteArray, 0, accountCount);
863
888
  const dataLength = decodeLength(byteArray);
864
- const dataSlice = byteArray.splice(0, dataLength);
889
+ const dataSlice = guardedSplice(byteArray, 0, dataLength);
865
890
  const data = bs58__default.default.encode(buffer.Buffer.from(dataSlice));
866
891
  instructions.push({
867
892
  programIdIndex,
@@ -1067,30 +1092,30 @@ class MessageV0 {
1067
1092
  }
1068
1093
  static deserialize(serializedMessage) {
1069
1094
  let byteArray = [...serializedMessage];
1070
- const prefix = byteArray.shift();
1095
+ const prefix = guardedShift(byteArray);
1071
1096
  const maskedPrefix = prefix & VERSION_PREFIX_MASK;
1072
1097
  assert(prefix !== maskedPrefix, `Expected versioned message but received legacy message`);
1073
1098
  const version = maskedPrefix;
1074
1099
  assert(version === 0, `Expected versioned message with version 0 but found version ${version}`);
1075
1100
  const header = {
1076
- numRequiredSignatures: byteArray.shift(),
1077
- numReadonlySignedAccounts: byteArray.shift(),
1078
- numReadonlyUnsignedAccounts: byteArray.shift()
1101
+ numRequiredSignatures: guardedShift(byteArray),
1102
+ numReadonlySignedAccounts: guardedShift(byteArray),
1103
+ numReadonlyUnsignedAccounts: guardedShift(byteArray)
1079
1104
  };
1080
1105
  const staticAccountKeys = [];
1081
1106
  const staticAccountKeysLength = decodeLength(byteArray);
1082
1107
  for (let i = 0; i < staticAccountKeysLength; i++) {
1083
- staticAccountKeys.push(new PublicKey(byteArray.splice(0, PUBLIC_KEY_LENGTH)));
1108
+ staticAccountKeys.push(new PublicKey(guardedSplice(byteArray, 0, PUBLIC_KEY_LENGTH)));
1084
1109
  }
1085
- const recentBlockhash = bs58__default.default.encode(byteArray.splice(0, PUBLIC_KEY_LENGTH));
1110
+ const recentBlockhash = bs58__default.default.encode(guardedSplice(byteArray, 0, PUBLIC_KEY_LENGTH));
1086
1111
  const instructionCount = decodeLength(byteArray);
1087
1112
  const compiledInstructions = [];
1088
1113
  for (let i = 0; i < instructionCount; i++) {
1089
- const programIdIndex = byteArray.shift();
1114
+ const programIdIndex = guardedShift(byteArray);
1090
1115
  const accountKeyIndexesLength = decodeLength(byteArray);
1091
- const accountKeyIndexes = byteArray.splice(0, accountKeyIndexesLength);
1116
+ const accountKeyIndexes = guardedSplice(byteArray, 0, accountKeyIndexesLength);
1092
1117
  const dataLength = decodeLength(byteArray);
1093
- const data = new Uint8Array(byteArray.splice(0, dataLength));
1118
+ const data = new Uint8Array(guardedSplice(byteArray, 0, dataLength));
1094
1119
  compiledInstructions.push({
1095
1120
  programIdIndex,
1096
1121
  accountKeyIndexes,
@@ -1100,11 +1125,11 @@ class MessageV0 {
1100
1125
  const addressTableLookupsCount = decodeLength(byteArray);
1101
1126
  const addressTableLookups = [];
1102
1127
  for (let i = 0; i < addressTableLookupsCount; i++) {
1103
- const accountKey = new PublicKey(byteArray.splice(0, PUBLIC_KEY_LENGTH));
1128
+ const accountKey = new PublicKey(guardedSplice(byteArray, 0, PUBLIC_KEY_LENGTH));
1104
1129
  const writableIndexesLength = decodeLength(byteArray);
1105
- const writableIndexes = byteArray.splice(0, writableIndexesLength);
1130
+ const writableIndexes = guardedSplice(byteArray, 0, writableIndexesLength);
1106
1131
  const readonlyIndexesLength = decodeLength(byteArray);
1107
- const readonlyIndexes = byteArray.splice(0, readonlyIndexesLength);
1132
+ const readonlyIndexes = guardedSplice(byteArray, 0, readonlyIndexesLength);
1108
1133
  addressTableLookups.push({
1109
1134
  accountKey,
1110
1135
  writableIndexes,
@@ -1895,7 +1920,7 @@ class Transaction {
1895
1920
  const signatureCount = decodeLength(byteArray);
1896
1921
  let signatures = [];
1897
1922
  for (let i = 0; i < signatureCount; i++) {
1898
- const signature = byteArray.splice(0, SIGNATURE_LENGTH_IN_BYTES);
1923
+ const signature = guardedSplice(byteArray, 0, SIGNATURE_LENGTH_IN_BYTES);
1899
1924
  signatures.push(bs58__default.default.encode(buffer.Buffer.from(signature)));
1900
1925
  }
1901
1926
  return Transaction.populate(Message.from(byteArray), signatures);
@@ -2070,7 +2095,7 @@ class VersionedTransaction {
2070
2095
  const signatures = [];
2071
2096
  const signaturesLength = decodeLength(byteArray);
2072
2097
  for (let i = 0; i < signaturesLength; i++) {
2073
- signatures.push(new Uint8Array(byteArray.splice(0, SIGNATURE_LENGTH_IN_BYTES)));
2098
+ signatures.push(new Uint8Array(guardedSplice(byteArray, 0, SIGNATURE_LENGTH_IN_BYTES)));
2074
2099
  }
2075
2100
  const message = VersionedMessage.deserialize(new Uint8Array(byteArray));
2076
2101
  return new VersionedTransaction(message, signatures);
@@ -10120,8 +10145,8 @@ class ValidatorInfo {
10120
10145
  if (configKeyCount !== 2) return null;
10121
10146
  const configKeys = [];
10122
10147
  for (let i = 0; i < 2; i++) {
10123
- const publicKey = new PublicKey(byteArray.splice(0, PUBLIC_KEY_LENGTH));
10124
- const isSigner = byteArray.splice(0, 1)[0] === 1;
10148
+ const publicKey = new PublicKey(guardedSplice(byteArray, 0, PUBLIC_KEY_LENGTH));
10149
+ const isSigner = guardedShift(byteArray) === 1;
10125
10150
  configKeys.push({
10126
10151
  publicKey,
10127
10152
  isSigner