@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.esm.js CHANGED
@@ -676,6 +676,31 @@ class CompiledKeys {
676
676
  }
677
677
  }
678
678
 
679
+ const END_OF_BUFFER_ERROR_MESSAGE = 'Reached end of buffer unexpectedly';
680
+
681
+ /**
682
+ * Delegates to `Array#shift`, but throws if the array is zero-length.
683
+ */
684
+ function guardedShift(byteArray) {
685
+ if (byteArray.length === 0) {
686
+ throw new Error(END_OF_BUFFER_ERROR_MESSAGE);
687
+ }
688
+ return byteArray.shift();
689
+ }
690
+
691
+ /**
692
+ * Delegates to `Array#splice`, but throws if the section being spliced out extends past the end of
693
+ * the array.
694
+ */
695
+ function guardedSplice(byteArray, ...args) {
696
+ const [start] = args;
697
+ if (args.length === 2 // Implies that `deleteCount` was supplied
698
+ ? start + (args[1] ?? 0) > byteArray.length : start >= byteArray.length) {
699
+ throw new Error(END_OF_BUFFER_ERROR_MESSAGE);
700
+ }
701
+ return byteArray.splice(...args);
702
+ }
703
+
679
704
  /**
680
705
  * An instruction to execute by a program
681
706
  *
@@ -817,27 +842,27 @@ class Message {
817
842
  static from(buffer) {
818
843
  // Slice up wire data
819
844
  let byteArray = [...buffer];
820
- const numRequiredSignatures = byteArray.shift();
845
+ const numRequiredSignatures = guardedShift(byteArray);
821
846
  if (numRequiredSignatures !== (numRequiredSignatures & VERSION_PREFIX_MASK)) {
822
847
  throw new Error('Versioned messages must be deserialized with VersionedMessage.deserialize()');
823
848
  }
824
- const numReadonlySignedAccounts = byteArray.shift();
825
- const numReadonlyUnsignedAccounts = byteArray.shift();
849
+ const numReadonlySignedAccounts = guardedShift(byteArray);
850
+ const numReadonlyUnsignedAccounts = guardedShift(byteArray);
826
851
  const accountCount = decodeLength(byteArray);
827
852
  let accountKeys = [];
828
853
  for (let i = 0; i < accountCount; i++) {
829
- const account = byteArray.splice(0, PUBLIC_KEY_LENGTH);
854
+ const account = guardedSplice(byteArray, 0, PUBLIC_KEY_LENGTH);
830
855
  accountKeys.push(new PublicKey(Buffer.from(account)));
831
856
  }
832
- const recentBlockhash = byteArray.splice(0, PUBLIC_KEY_LENGTH);
857
+ const recentBlockhash = guardedSplice(byteArray, 0, PUBLIC_KEY_LENGTH);
833
858
  const instructionCount = decodeLength(byteArray);
834
859
  let instructions = [];
835
860
  for (let i = 0; i < instructionCount; i++) {
836
- const programIdIndex = byteArray.shift();
861
+ const programIdIndex = guardedShift(byteArray);
837
862
  const accountCount = decodeLength(byteArray);
838
- const accounts = byteArray.splice(0, accountCount);
863
+ const accounts = guardedSplice(byteArray, 0, accountCount);
839
864
  const dataLength = decodeLength(byteArray);
840
- const dataSlice = byteArray.splice(0, dataLength);
865
+ const dataSlice = guardedSplice(byteArray, 0, dataLength);
841
866
  const data = bs58.encode(Buffer.from(dataSlice));
842
867
  instructions.push({
843
868
  programIdIndex,
@@ -1043,30 +1068,30 @@ class MessageV0 {
1043
1068
  }
1044
1069
  static deserialize(serializedMessage) {
1045
1070
  let byteArray = [...serializedMessage];
1046
- const prefix = byteArray.shift();
1071
+ const prefix = guardedShift(byteArray);
1047
1072
  const maskedPrefix = prefix & VERSION_PREFIX_MASK;
1048
1073
  assert(prefix !== maskedPrefix, `Expected versioned message but received legacy message`);
1049
1074
  const version = maskedPrefix;
1050
1075
  assert(version === 0, `Expected versioned message with version 0 but found version ${version}`);
1051
1076
  const header = {
1052
- numRequiredSignatures: byteArray.shift(),
1053
- numReadonlySignedAccounts: byteArray.shift(),
1054
- numReadonlyUnsignedAccounts: byteArray.shift()
1077
+ numRequiredSignatures: guardedShift(byteArray),
1078
+ numReadonlySignedAccounts: guardedShift(byteArray),
1079
+ numReadonlyUnsignedAccounts: guardedShift(byteArray)
1055
1080
  };
1056
1081
  const staticAccountKeys = [];
1057
1082
  const staticAccountKeysLength = decodeLength(byteArray);
1058
1083
  for (let i = 0; i < staticAccountKeysLength; i++) {
1059
- staticAccountKeys.push(new PublicKey(byteArray.splice(0, PUBLIC_KEY_LENGTH)));
1084
+ staticAccountKeys.push(new PublicKey(guardedSplice(byteArray, 0, PUBLIC_KEY_LENGTH)));
1060
1085
  }
1061
- const recentBlockhash = bs58.encode(byteArray.splice(0, PUBLIC_KEY_LENGTH));
1086
+ const recentBlockhash = bs58.encode(guardedSplice(byteArray, 0, PUBLIC_KEY_LENGTH));
1062
1087
  const instructionCount = decodeLength(byteArray);
1063
1088
  const compiledInstructions = [];
1064
1089
  for (let i = 0; i < instructionCount; i++) {
1065
- const programIdIndex = byteArray.shift();
1090
+ const programIdIndex = guardedShift(byteArray);
1066
1091
  const accountKeyIndexesLength = decodeLength(byteArray);
1067
- const accountKeyIndexes = byteArray.splice(0, accountKeyIndexesLength);
1092
+ const accountKeyIndexes = guardedSplice(byteArray, 0, accountKeyIndexesLength);
1068
1093
  const dataLength = decodeLength(byteArray);
1069
- const data = new Uint8Array(byteArray.splice(0, dataLength));
1094
+ const data = new Uint8Array(guardedSplice(byteArray, 0, dataLength));
1070
1095
  compiledInstructions.push({
1071
1096
  programIdIndex,
1072
1097
  accountKeyIndexes,
@@ -1076,11 +1101,11 @@ class MessageV0 {
1076
1101
  const addressTableLookupsCount = decodeLength(byteArray);
1077
1102
  const addressTableLookups = [];
1078
1103
  for (let i = 0; i < addressTableLookupsCount; i++) {
1079
- const accountKey = new PublicKey(byteArray.splice(0, PUBLIC_KEY_LENGTH));
1104
+ const accountKey = new PublicKey(guardedSplice(byteArray, 0, PUBLIC_KEY_LENGTH));
1080
1105
  const writableIndexesLength = decodeLength(byteArray);
1081
- const writableIndexes = byteArray.splice(0, writableIndexesLength);
1106
+ const writableIndexes = guardedSplice(byteArray, 0, writableIndexesLength);
1082
1107
  const readonlyIndexesLength = decodeLength(byteArray);
1083
- const readonlyIndexes = byteArray.splice(0, readonlyIndexesLength);
1108
+ const readonlyIndexes = guardedSplice(byteArray, 0, readonlyIndexesLength);
1084
1109
  addressTableLookups.push({
1085
1110
  accountKey,
1086
1111
  writableIndexes,
@@ -1871,7 +1896,7 @@ class Transaction {
1871
1896
  const signatureCount = decodeLength(byteArray);
1872
1897
  let signatures = [];
1873
1898
  for (let i = 0; i < signatureCount; i++) {
1874
- const signature = byteArray.splice(0, SIGNATURE_LENGTH_IN_BYTES);
1899
+ const signature = guardedSplice(byteArray, 0, SIGNATURE_LENGTH_IN_BYTES);
1875
1900
  signatures.push(bs58.encode(Buffer.from(signature)));
1876
1901
  }
1877
1902
  return Transaction.populate(Message.from(byteArray), signatures);
@@ -2046,7 +2071,7 @@ class VersionedTransaction {
2046
2071
  const signatures = [];
2047
2072
  const signaturesLength = decodeLength(byteArray);
2048
2073
  for (let i = 0; i < signaturesLength; i++) {
2049
- signatures.push(new Uint8Array(byteArray.splice(0, SIGNATURE_LENGTH_IN_BYTES)));
2074
+ signatures.push(new Uint8Array(guardedSplice(byteArray, 0, SIGNATURE_LENGTH_IN_BYTES)));
2050
2075
  }
2051
2076
  const message = VersionedMessage.deserialize(new Uint8Array(byteArray));
2052
2077
  return new VersionedTransaction(message, signatures);
@@ -10785,8 +10810,8 @@ class ValidatorInfo {
10785
10810
  if (configKeyCount !== 2) return null;
10786
10811
  const configKeys = [];
10787
10812
  for (let i = 0; i < 2; i++) {
10788
- const publicKey = new PublicKey(byteArray.splice(0, PUBLIC_KEY_LENGTH));
10789
- const isSigner = byteArray.splice(0, 1)[0] === 1;
10813
+ const publicKey = new PublicKey(guardedSplice(byteArray, 0, PUBLIC_KEY_LENGTH));
10814
+ const isSigner = guardedShift(byteArray) === 1;
10790
10815
  configKeys.push({
10791
10816
  publicKey,
10792
10817
  isSigner