@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.
@@ -672,6 +672,31 @@ class CompiledKeys {
672
672
  }
673
673
  }
674
674
 
675
+ const END_OF_BUFFER_ERROR_MESSAGE = 'Reached end of buffer unexpectedly';
676
+
677
+ /**
678
+ * Delegates to `Array#shift`, but throws if the array is zero-length.
679
+ */
680
+ function guardedShift(byteArray) {
681
+ if (byteArray.length === 0) {
682
+ throw new Error(END_OF_BUFFER_ERROR_MESSAGE);
683
+ }
684
+ return byteArray.shift();
685
+ }
686
+
687
+ /**
688
+ * Delegates to `Array#splice`, but throws if the section being spliced out extends past the end of
689
+ * the array.
690
+ */
691
+ function guardedSplice(byteArray, ...args) {
692
+ const [start] = args;
693
+ if (args.length === 2 // Implies that `deleteCount` was supplied
694
+ ? start + (args[1] ?? 0) > byteArray.length : start >= byteArray.length) {
695
+ throw new Error(END_OF_BUFFER_ERROR_MESSAGE);
696
+ }
697
+ return byteArray.splice(...args);
698
+ }
699
+
675
700
  /**
676
701
  * An instruction to execute by a program
677
702
  *
@@ -813,27 +838,27 @@ class Message {
813
838
  static from(buffer) {
814
839
  // Slice up wire data
815
840
  let byteArray = [...buffer];
816
- const numRequiredSignatures = byteArray.shift();
841
+ const numRequiredSignatures = guardedShift(byteArray);
817
842
  if (numRequiredSignatures !== (numRequiredSignatures & VERSION_PREFIX_MASK)) {
818
843
  throw new Error('Versioned messages must be deserialized with VersionedMessage.deserialize()');
819
844
  }
820
- const numReadonlySignedAccounts = byteArray.shift();
821
- const numReadonlyUnsignedAccounts = byteArray.shift();
845
+ const numReadonlySignedAccounts = guardedShift(byteArray);
846
+ const numReadonlyUnsignedAccounts = guardedShift(byteArray);
822
847
  const accountCount = decodeLength(byteArray);
823
848
  let accountKeys = [];
824
849
  for (let i = 0; i < accountCount; i++) {
825
- const account = byteArray.splice(0, PUBLIC_KEY_LENGTH);
850
+ const account = guardedSplice(byteArray, 0, PUBLIC_KEY_LENGTH);
826
851
  accountKeys.push(new PublicKey(Buffer.from(account)));
827
852
  }
828
- const recentBlockhash = byteArray.splice(0, PUBLIC_KEY_LENGTH);
853
+ const recentBlockhash = guardedSplice(byteArray, 0, PUBLIC_KEY_LENGTH);
829
854
  const instructionCount = decodeLength(byteArray);
830
855
  let instructions = [];
831
856
  for (let i = 0; i < instructionCount; i++) {
832
- const programIdIndex = byteArray.shift();
857
+ const programIdIndex = guardedShift(byteArray);
833
858
  const accountCount = decodeLength(byteArray);
834
- const accounts = byteArray.splice(0, accountCount);
859
+ const accounts = guardedSplice(byteArray, 0, accountCount);
835
860
  const dataLength = decodeLength(byteArray);
836
- const dataSlice = byteArray.splice(0, dataLength);
861
+ const dataSlice = guardedSplice(byteArray, 0, dataLength);
837
862
  const data = bs58.encode(Buffer.from(dataSlice));
838
863
  instructions.push({
839
864
  programIdIndex,
@@ -1039,30 +1064,30 @@ class MessageV0 {
1039
1064
  }
1040
1065
  static deserialize(serializedMessage) {
1041
1066
  let byteArray = [...serializedMessage];
1042
- const prefix = byteArray.shift();
1067
+ const prefix = guardedShift(byteArray);
1043
1068
  const maskedPrefix = prefix & VERSION_PREFIX_MASK;
1044
1069
  assert(prefix !== maskedPrefix, `Expected versioned message but received legacy message`);
1045
1070
  const version = maskedPrefix;
1046
1071
  assert(version === 0, `Expected versioned message with version 0 but found version ${version}`);
1047
1072
  const header = {
1048
- numRequiredSignatures: byteArray.shift(),
1049
- numReadonlySignedAccounts: byteArray.shift(),
1050
- numReadonlyUnsignedAccounts: byteArray.shift()
1073
+ numRequiredSignatures: guardedShift(byteArray),
1074
+ numReadonlySignedAccounts: guardedShift(byteArray),
1075
+ numReadonlyUnsignedAccounts: guardedShift(byteArray)
1051
1076
  };
1052
1077
  const staticAccountKeys = [];
1053
1078
  const staticAccountKeysLength = decodeLength(byteArray);
1054
1079
  for (let i = 0; i < staticAccountKeysLength; i++) {
1055
- staticAccountKeys.push(new PublicKey(byteArray.splice(0, PUBLIC_KEY_LENGTH)));
1080
+ staticAccountKeys.push(new PublicKey(guardedSplice(byteArray, 0, PUBLIC_KEY_LENGTH)));
1056
1081
  }
1057
- const recentBlockhash = bs58.encode(byteArray.splice(0, PUBLIC_KEY_LENGTH));
1082
+ const recentBlockhash = bs58.encode(guardedSplice(byteArray, 0, PUBLIC_KEY_LENGTH));
1058
1083
  const instructionCount = decodeLength(byteArray);
1059
1084
  const compiledInstructions = [];
1060
1085
  for (let i = 0; i < instructionCount; i++) {
1061
- const programIdIndex = byteArray.shift();
1086
+ const programIdIndex = guardedShift(byteArray);
1062
1087
  const accountKeyIndexesLength = decodeLength(byteArray);
1063
- const accountKeyIndexes = byteArray.splice(0, accountKeyIndexesLength);
1088
+ const accountKeyIndexes = guardedSplice(byteArray, 0, accountKeyIndexesLength);
1064
1089
  const dataLength = decodeLength(byteArray);
1065
- const data = new Uint8Array(byteArray.splice(0, dataLength));
1090
+ const data = new Uint8Array(guardedSplice(byteArray, 0, dataLength));
1066
1091
  compiledInstructions.push({
1067
1092
  programIdIndex,
1068
1093
  accountKeyIndexes,
@@ -1072,11 +1097,11 @@ class MessageV0 {
1072
1097
  const addressTableLookupsCount = decodeLength(byteArray);
1073
1098
  const addressTableLookups = [];
1074
1099
  for (let i = 0; i < addressTableLookupsCount; i++) {
1075
- const accountKey = new PublicKey(byteArray.splice(0, PUBLIC_KEY_LENGTH));
1100
+ const accountKey = new PublicKey(guardedSplice(byteArray, 0, PUBLIC_KEY_LENGTH));
1076
1101
  const writableIndexesLength = decodeLength(byteArray);
1077
- const writableIndexes = byteArray.splice(0, writableIndexesLength);
1102
+ const writableIndexes = guardedSplice(byteArray, 0, writableIndexesLength);
1078
1103
  const readonlyIndexesLength = decodeLength(byteArray);
1079
- const readonlyIndexes = byteArray.splice(0, readonlyIndexesLength);
1104
+ const readonlyIndexes = guardedSplice(byteArray, 0, readonlyIndexesLength);
1080
1105
  addressTableLookups.push({
1081
1106
  accountKey,
1082
1107
  writableIndexes,
@@ -1867,7 +1892,7 @@ class Transaction {
1867
1892
  const signatureCount = decodeLength(byteArray);
1868
1893
  let signatures = [];
1869
1894
  for (let i = 0; i < signatureCount; i++) {
1870
- const signature = byteArray.splice(0, SIGNATURE_LENGTH_IN_BYTES);
1895
+ const signature = guardedSplice(byteArray, 0, SIGNATURE_LENGTH_IN_BYTES);
1871
1896
  signatures.push(bs58.encode(Buffer.from(signature)));
1872
1897
  }
1873
1898
  return Transaction.populate(Message.from(byteArray), signatures);
@@ -2042,7 +2067,7 @@ class VersionedTransaction {
2042
2067
  const signatures = [];
2043
2068
  const signaturesLength = decodeLength(byteArray);
2044
2069
  for (let i = 0; i < signaturesLength; i++) {
2045
- signatures.push(new Uint8Array(byteArray.splice(0, SIGNATURE_LENGTH_IN_BYTES)));
2070
+ signatures.push(new Uint8Array(guardedSplice(byteArray, 0, SIGNATURE_LENGTH_IN_BYTES)));
2046
2071
  }
2047
2072
  const message = VersionedMessage.deserialize(new Uint8Array(byteArray));
2048
2073
  return new VersionedTransaction(message, signatures);
@@ -10092,8 +10117,8 @@ class ValidatorInfo {
10092
10117
  if (configKeyCount !== 2) return null;
10093
10118
  const configKeys = [];
10094
10119
  for (let i = 0; i < 2; i++) {
10095
- const publicKey = new PublicKey(byteArray.splice(0, PUBLIC_KEY_LENGTH));
10096
- const isSigner = byteArray.splice(0, 1)[0] === 1;
10120
+ const publicKey = new PublicKey(guardedSplice(byteArray, 0, PUBLIC_KEY_LENGTH));
10121
+ const isSigner = guardedShift(byteArray) === 1;
10097
10122
  configKeys.push({
10098
10123
  publicKey,
10099
10124
  isSigner