@solana/web3.js 1.87.6 → 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.esm.js CHANGED
@@ -678,6 +678,31 @@ class CompiledKeys {
678
678
  }
679
679
  }
680
680
 
681
+ const END_OF_BUFFER_ERROR_MESSAGE = 'Reached end of buffer unexpectedly';
682
+
683
+ /**
684
+ * Delegates to `Array#shift`, but throws if the array is zero-length.
685
+ */
686
+ function guardedShift(byteArray) {
687
+ if (byteArray.length === 0) {
688
+ throw new Error(END_OF_BUFFER_ERROR_MESSAGE);
689
+ }
690
+ return byteArray.shift();
691
+ }
692
+
693
+ /**
694
+ * Delegates to `Array#splice`, but throws if the section being spliced out extends past the end of
695
+ * the array.
696
+ */
697
+ function guardedSplice(byteArray, ...args) {
698
+ const [start] = args;
699
+ if (args.length === 2 // Implies that `deleteCount` was supplied
700
+ ? start + (args[1] ?? 0) > byteArray.length : start >= byteArray.length) {
701
+ throw new Error(END_OF_BUFFER_ERROR_MESSAGE);
702
+ }
703
+ return byteArray.splice(...args);
704
+ }
705
+
681
706
  /**
682
707
  * An instruction to execute by a program
683
708
  *
@@ -819,32 +844,28 @@ class Message {
819
844
  static from(buffer) {
820
845
  // Slice up wire data
821
846
  let byteArray = [...buffer];
822
- const numRequiredSignatures = byteArray.shift();
847
+ const numRequiredSignatures = guardedShift(byteArray);
823
848
  if (numRequiredSignatures !== (numRequiredSignatures & VERSION_PREFIX_MASK)) {
824
849
  throw new Error('Versioned messages must be deserialized with VersionedMessage.deserialize()');
825
850
  }
826
- const numReadonlySignedAccounts = byteArray.shift();
827
- const numReadonlyUnsignedAccounts = byteArray.shift();
851
+ const numReadonlySignedAccounts = guardedShift(byteArray);
852
+ const numReadonlyUnsignedAccounts = guardedShift(byteArray);
828
853
  const accountCount = decodeLength(byteArray);
829
854
  let accountKeys = [];
830
855
  for (let i = 0; i < accountCount; i++) {
831
- const account = byteArray.slice(0, PUBLIC_KEY_LENGTH);
832
- byteArray = byteArray.slice(PUBLIC_KEY_LENGTH);
856
+ const account = guardedSplice(byteArray, 0, PUBLIC_KEY_LENGTH);
833
857
  accountKeys.push(new PublicKey(Buffer.from(account)));
834
858
  }
835
- const recentBlockhash = byteArray.slice(0, PUBLIC_KEY_LENGTH);
836
- byteArray = byteArray.slice(PUBLIC_KEY_LENGTH);
859
+ const recentBlockhash = guardedSplice(byteArray, 0, PUBLIC_KEY_LENGTH);
837
860
  const instructionCount = decodeLength(byteArray);
838
861
  let instructions = [];
839
862
  for (let i = 0; i < instructionCount; i++) {
840
- const programIdIndex = byteArray.shift();
863
+ const programIdIndex = guardedShift(byteArray);
841
864
  const accountCount = decodeLength(byteArray);
842
- const accounts = byteArray.slice(0, accountCount);
843
- byteArray = byteArray.slice(accountCount);
865
+ const accounts = guardedSplice(byteArray, 0, accountCount);
844
866
  const dataLength = decodeLength(byteArray);
845
- const dataSlice = byteArray.slice(0, dataLength);
867
+ const dataSlice = guardedSplice(byteArray, 0, dataLength);
846
868
  const data = bs58.encode(Buffer.from(dataSlice));
847
- byteArray = byteArray.slice(dataLength);
848
869
  instructions.push({
849
870
  programIdIndex,
850
871
  accounts,
@@ -1049,30 +1070,30 @@ class MessageV0 {
1049
1070
  }
1050
1071
  static deserialize(serializedMessage) {
1051
1072
  let byteArray = [...serializedMessage];
1052
- const prefix = byteArray.shift();
1073
+ const prefix = guardedShift(byteArray);
1053
1074
  const maskedPrefix = prefix & VERSION_PREFIX_MASK;
1054
1075
  assert(prefix !== maskedPrefix, `Expected versioned message but received legacy message`);
1055
1076
  const version = maskedPrefix;
1056
1077
  assert(version === 0, `Expected versioned message with version 0 but found version ${version}`);
1057
1078
  const header = {
1058
- numRequiredSignatures: byteArray.shift(),
1059
- numReadonlySignedAccounts: byteArray.shift(),
1060
- numReadonlyUnsignedAccounts: byteArray.shift()
1079
+ numRequiredSignatures: guardedShift(byteArray),
1080
+ numReadonlySignedAccounts: guardedShift(byteArray),
1081
+ numReadonlyUnsignedAccounts: guardedShift(byteArray)
1061
1082
  };
1062
1083
  const staticAccountKeys = [];
1063
1084
  const staticAccountKeysLength = decodeLength(byteArray);
1064
1085
  for (let i = 0; i < staticAccountKeysLength; i++) {
1065
- staticAccountKeys.push(new PublicKey(byteArray.splice(0, PUBLIC_KEY_LENGTH)));
1086
+ staticAccountKeys.push(new PublicKey(guardedSplice(byteArray, 0, PUBLIC_KEY_LENGTH)));
1066
1087
  }
1067
- const recentBlockhash = bs58.encode(byteArray.splice(0, PUBLIC_KEY_LENGTH));
1088
+ const recentBlockhash = bs58.encode(guardedSplice(byteArray, 0, PUBLIC_KEY_LENGTH));
1068
1089
  const instructionCount = decodeLength(byteArray);
1069
1090
  const compiledInstructions = [];
1070
1091
  for (let i = 0; i < instructionCount; i++) {
1071
- const programIdIndex = byteArray.shift();
1092
+ const programIdIndex = guardedShift(byteArray);
1072
1093
  const accountKeyIndexesLength = decodeLength(byteArray);
1073
- const accountKeyIndexes = byteArray.splice(0, accountKeyIndexesLength);
1094
+ const accountKeyIndexes = guardedSplice(byteArray, 0, accountKeyIndexesLength);
1074
1095
  const dataLength = decodeLength(byteArray);
1075
- const data = new Uint8Array(byteArray.splice(0, dataLength));
1096
+ const data = new Uint8Array(guardedSplice(byteArray, 0, dataLength));
1076
1097
  compiledInstructions.push({
1077
1098
  programIdIndex,
1078
1099
  accountKeyIndexes,
@@ -1082,11 +1103,11 @@ class MessageV0 {
1082
1103
  const addressTableLookupsCount = decodeLength(byteArray);
1083
1104
  const addressTableLookups = [];
1084
1105
  for (let i = 0; i < addressTableLookupsCount; i++) {
1085
- const accountKey = new PublicKey(byteArray.splice(0, PUBLIC_KEY_LENGTH));
1106
+ const accountKey = new PublicKey(guardedSplice(byteArray, 0, PUBLIC_KEY_LENGTH));
1086
1107
  const writableIndexesLength = decodeLength(byteArray);
1087
- const writableIndexes = byteArray.splice(0, writableIndexesLength);
1108
+ const writableIndexes = guardedSplice(byteArray, 0, writableIndexesLength);
1088
1109
  const readonlyIndexesLength = decodeLength(byteArray);
1089
- const readonlyIndexes = byteArray.splice(0, readonlyIndexesLength);
1110
+ const readonlyIndexes = guardedSplice(byteArray, 0, readonlyIndexesLength);
1090
1111
  addressTableLookups.push({
1091
1112
  accountKey,
1092
1113
  writableIndexes,
@@ -1858,8 +1879,7 @@ class Transaction {
1858
1879
  const signatureCount = decodeLength(byteArray);
1859
1880
  let signatures = [];
1860
1881
  for (let i = 0; i < signatureCount; i++) {
1861
- const signature = byteArray.slice(0, SIGNATURE_LENGTH_IN_BYTES);
1862
- byteArray = byteArray.slice(SIGNATURE_LENGTH_IN_BYTES);
1882
+ const signature = guardedSplice(byteArray, 0, SIGNATURE_LENGTH_IN_BYTES);
1863
1883
  signatures.push(bs58.encode(Buffer.from(signature)));
1864
1884
  }
1865
1885
  return Transaction.populate(Message.from(byteArray), signatures);
@@ -2034,7 +2054,7 @@ class VersionedTransaction {
2034
2054
  const signatures = [];
2035
2055
  const signaturesLength = decodeLength(byteArray);
2036
2056
  for (let i = 0; i < signaturesLength; i++) {
2037
- signatures.push(new Uint8Array(byteArray.splice(0, SIGNATURE_LENGTH_IN_BYTES)));
2057
+ signatures.push(new Uint8Array(guardedSplice(byteArray, 0, SIGNATURE_LENGTH_IN_BYTES)));
2038
2058
  }
2039
2059
  const message = VersionedMessage.deserialize(new Uint8Array(byteArray));
2040
2060
  return new VersionedTransaction(message, signatures);
@@ -7337,7 +7357,7 @@ const LogsNotificationResult = type({
7337
7357
 
7338
7358
  /** @internal */
7339
7359
  const COMMON_HTTP_HEADERS = {
7340
- 'solana-client': `js/${"0.0.0-development" }`
7360
+ 'solana-client': `js/${"1.87.7" }`
7341
7361
  };
7342
7362
 
7343
7363
  /**
@@ -12272,10 +12292,8 @@ class ValidatorInfo {
12272
12292
  if (configKeyCount !== 2) return null;
12273
12293
  const configKeys = [];
12274
12294
  for (let i = 0; i < 2; i++) {
12275
- const publicKey = new PublicKey(byteArray.slice(0, PUBLIC_KEY_LENGTH));
12276
- byteArray = byteArray.slice(PUBLIC_KEY_LENGTH);
12277
- const isSigner = byteArray.slice(0, 1)[0] === 1;
12278
- byteArray = byteArray.slice(1);
12295
+ const publicKey = new PublicKey(guardedSplice(byteArray, 0, PUBLIC_KEY_LENGTH));
12296
+ const isSigner = guardedShift(byteArray) === 1;
12279
12297
  configKeys.push({
12280
12298
  publicKey,
12281
12299
  isSigner