@solana/web3.js 1.87.5 → 1.87.7

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
@@ -712,6 +712,31 @@ class CompiledKeys {
712
712
  }
713
713
  }
714
714
 
715
+ const END_OF_BUFFER_ERROR_MESSAGE = 'Reached end of buffer unexpectedly';
716
+
717
+ /**
718
+ * Delegates to `Array#shift`, but throws if the array is zero-length.
719
+ */
720
+ function guardedShift(byteArray) {
721
+ if (byteArray.length === 0) {
722
+ throw new Error(END_OF_BUFFER_ERROR_MESSAGE);
723
+ }
724
+ return byteArray.shift();
725
+ }
726
+
727
+ /**
728
+ * Delegates to `Array#splice`, but throws if the section being spliced out extends past the end of
729
+ * the array.
730
+ */
731
+ function guardedSplice(byteArray, ...args) {
732
+ const [start] = args;
733
+ if (args.length === 2 // Implies that `deleteCount` was supplied
734
+ ? start + (args[1] ?? 0) > byteArray.length : start >= byteArray.length) {
735
+ throw new Error(END_OF_BUFFER_ERROR_MESSAGE);
736
+ }
737
+ return byteArray.splice(...args);
738
+ }
739
+
715
740
  /**
716
741
  * An instruction to execute by a program
717
742
  *
@@ -853,32 +878,28 @@ class Message {
853
878
  static from(buffer$1) {
854
879
  // Slice up wire data
855
880
  let byteArray = [...buffer$1];
856
- const numRequiredSignatures = byteArray.shift();
881
+ const numRequiredSignatures = guardedShift(byteArray);
857
882
  if (numRequiredSignatures !== (numRequiredSignatures & VERSION_PREFIX_MASK)) {
858
883
  throw new Error('Versioned messages must be deserialized with VersionedMessage.deserialize()');
859
884
  }
860
- const numReadonlySignedAccounts = byteArray.shift();
861
- const numReadonlyUnsignedAccounts = byteArray.shift();
885
+ const numReadonlySignedAccounts = guardedShift(byteArray);
886
+ const numReadonlyUnsignedAccounts = guardedShift(byteArray);
862
887
  const accountCount = decodeLength(byteArray);
863
888
  let accountKeys = [];
864
889
  for (let i = 0; i < accountCount; i++) {
865
- const account = byteArray.slice(0, PUBLIC_KEY_LENGTH);
866
- byteArray = byteArray.slice(PUBLIC_KEY_LENGTH);
890
+ const account = guardedSplice(byteArray, 0, PUBLIC_KEY_LENGTH);
867
891
  accountKeys.push(new PublicKey(buffer.Buffer.from(account)));
868
892
  }
869
- const recentBlockhash = byteArray.slice(0, PUBLIC_KEY_LENGTH);
870
- byteArray = byteArray.slice(PUBLIC_KEY_LENGTH);
893
+ const recentBlockhash = guardedSplice(byteArray, 0, PUBLIC_KEY_LENGTH);
871
894
  const instructionCount = decodeLength(byteArray);
872
895
  let instructions = [];
873
896
  for (let i = 0; i < instructionCount; i++) {
874
- const programIdIndex = byteArray.shift();
897
+ const programIdIndex = guardedShift(byteArray);
875
898
  const accountCount = decodeLength(byteArray);
876
- const accounts = byteArray.slice(0, accountCount);
877
- byteArray = byteArray.slice(accountCount);
899
+ const accounts = guardedSplice(byteArray, 0, accountCount);
878
900
  const dataLength = decodeLength(byteArray);
879
- const dataSlice = byteArray.slice(0, dataLength);
901
+ const dataSlice = guardedSplice(byteArray, 0, dataLength);
880
902
  const data = bs58__default.default.encode(buffer.Buffer.from(dataSlice));
881
- byteArray = byteArray.slice(dataLength);
882
903
  instructions.push({
883
904
  programIdIndex,
884
905
  accounts,
@@ -1083,30 +1104,30 @@ class MessageV0 {
1083
1104
  }
1084
1105
  static deserialize(serializedMessage) {
1085
1106
  let byteArray = [...serializedMessage];
1086
- const prefix = byteArray.shift();
1107
+ const prefix = guardedShift(byteArray);
1087
1108
  const maskedPrefix = prefix & VERSION_PREFIX_MASK;
1088
1109
  assert(prefix !== maskedPrefix, `Expected versioned message but received legacy message`);
1089
1110
  const version = maskedPrefix;
1090
1111
  assert(version === 0, `Expected versioned message with version 0 but found version ${version}`);
1091
1112
  const header = {
1092
- numRequiredSignatures: byteArray.shift(),
1093
- numReadonlySignedAccounts: byteArray.shift(),
1094
- numReadonlyUnsignedAccounts: byteArray.shift()
1113
+ numRequiredSignatures: guardedShift(byteArray),
1114
+ numReadonlySignedAccounts: guardedShift(byteArray),
1115
+ numReadonlyUnsignedAccounts: guardedShift(byteArray)
1095
1116
  };
1096
1117
  const staticAccountKeys = [];
1097
1118
  const staticAccountKeysLength = decodeLength(byteArray);
1098
1119
  for (let i = 0; i < staticAccountKeysLength; i++) {
1099
- staticAccountKeys.push(new PublicKey(byteArray.splice(0, PUBLIC_KEY_LENGTH)));
1120
+ staticAccountKeys.push(new PublicKey(guardedSplice(byteArray, 0, PUBLIC_KEY_LENGTH)));
1100
1121
  }
1101
- const recentBlockhash = bs58__default.default.encode(byteArray.splice(0, PUBLIC_KEY_LENGTH));
1122
+ const recentBlockhash = bs58__default.default.encode(guardedSplice(byteArray, 0, PUBLIC_KEY_LENGTH));
1102
1123
  const instructionCount = decodeLength(byteArray);
1103
1124
  const compiledInstructions = [];
1104
1125
  for (let i = 0; i < instructionCount; i++) {
1105
- const programIdIndex = byteArray.shift();
1126
+ const programIdIndex = guardedShift(byteArray);
1106
1127
  const accountKeyIndexesLength = decodeLength(byteArray);
1107
- const accountKeyIndexes = byteArray.splice(0, accountKeyIndexesLength);
1128
+ const accountKeyIndexes = guardedSplice(byteArray, 0, accountKeyIndexesLength);
1108
1129
  const dataLength = decodeLength(byteArray);
1109
- const data = new Uint8Array(byteArray.splice(0, dataLength));
1130
+ const data = new Uint8Array(guardedSplice(byteArray, 0, dataLength));
1110
1131
  compiledInstructions.push({
1111
1132
  programIdIndex,
1112
1133
  accountKeyIndexes,
@@ -1116,11 +1137,11 @@ class MessageV0 {
1116
1137
  const addressTableLookupsCount = decodeLength(byteArray);
1117
1138
  const addressTableLookups = [];
1118
1139
  for (let i = 0; i < addressTableLookupsCount; i++) {
1119
- const accountKey = new PublicKey(byteArray.splice(0, PUBLIC_KEY_LENGTH));
1140
+ const accountKey = new PublicKey(guardedSplice(byteArray, 0, PUBLIC_KEY_LENGTH));
1120
1141
  const writableIndexesLength = decodeLength(byteArray);
1121
- const writableIndexes = byteArray.splice(0, writableIndexesLength);
1142
+ const writableIndexes = guardedSplice(byteArray, 0, writableIndexesLength);
1122
1143
  const readonlyIndexesLength = decodeLength(byteArray);
1123
- const readonlyIndexes = byteArray.splice(0, readonlyIndexesLength);
1144
+ const readonlyIndexes = guardedSplice(byteArray, 0, readonlyIndexesLength);
1124
1145
  addressTableLookups.push({
1125
1146
  accountKey,
1126
1147
  writableIndexes,
@@ -1892,8 +1913,7 @@ class Transaction {
1892
1913
  const signatureCount = decodeLength(byteArray);
1893
1914
  let signatures = [];
1894
1915
  for (let i = 0; i < signatureCount; i++) {
1895
- const signature = byteArray.slice(0, SIGNATURE_LENGTH_IN_BYTES);
1896
- byteArray = byteArray.slice(SIGNATURE_LENGTH_IN_BYTES);
1916
+ const signature = guardedSplice(byteArray, 0, SIGNATURE_LENGTH_IN_BYTES);
1897
1917
  signatures.push(bs58__default.default.encode(buffer.Buffer.from(signature)));
1898
1918
  }
1899
1919
  return Transaction.populate(Message.from(byteArray), signatures);
@@ -2068,7 +2088,7 @@ class VersionedTransaction {
2068
2088
  const signatures = [];
2069
2089
  const signaturesLength = decodeLength(byteArray);
2070
2090
  for (let i = 0; i < signaturesLength; i++) {
2071
- signatures.push(new Uint8Array(byteArray.splice(0, SIGNATURE_LENGTH_IN_BYTES)));
2091
+ signatures.push(new Uint8Array(guardedSplice(byteArray, 0, SIGNATURE_LENGTH_IN_BYTES)));
2072
2092
  }
2073
2093
  const message = VersionedMessage.deserialize(new Uint8Array(byteArray));
2074
2094
  return new VersionedTransaction(message, signatures);
@@ -5833,7 +5853,7 @@ function makeWebsocketUrl(endpoint) {
5833
5853
  const startPort = portWithColon == null ? null : parseInt(portWithColon.slice(1), 10);
5834
5854
  const websocketPort =
5835
5855
  // Only shift the port by +1 as a convention for ws(s) only if given endpoint
5836
- // is explictly specifying the endpoint port (HTTP-based RPC), assuming
5856
+ // is explicitly specifying the endpoint port (HTTP-based RPC), assuming
5837
5857
  // we're directly trying to connect to solana-validator's ws listening port.
5838
5858
  // When the endpoint omits the port, we're connecting to the protocol
5839
5859
  // default ports: http(80) or https(443) and it's assumed we're behind a reverse
@@ -7371,7 +7391,7 @@ const LogsNotificationResult = superstruct.type({
7371
7391
 
7372
7392
  /** @internal */
7373
7393
  const COMMON_HTTP_HEADERS = {
7374
- 'solana-client': `js/${"0.0.0-development" }`
7394
+ 'solana-client': `js/${"1.87.7" }`
7375
7395
  };
7376
7396
 
7377
7397
  /**
@@ -12306,10 +12326,8 @@ class ValidatorInfo {
12306
12326
  if (configKeyCount !== 2) return null;
12307
12327
  const configKeys = [];
12308
12328
  for (let i = 0; i < 2; i++) {
12309
- const publicKey = new PublicKey(byteArray.slice(0, PUBLIC_KEY_LENGTH));
12310
- byteArray = byteArray.slice(PUBLIC_KEY_LENGTH);
12311
- const isSigner = byteArray.slice(0, 1)[0] === 1;
12312
- byteArray = byteArray.slice(1);
12329
+ const publicKey = new PublicKey(guardedSplice(byteArray, 0, PUBLIC_KEY_LENGTH));
12330
+ const isSigner = guardedShift(byteArray) === 1;
12313
12331
  configKeys.push({
12314
12332
  publicKey,
12315
12333
  isSigner