@solana/web3.js 1.88.0 → 1.88.1

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,32 +842,28 @@ 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.slice(0, PUBLIC_KEY_LENGTH);
830
- byteArray = byteArray.slice(PUBLIC_KEY_LENGTH);
854
+ const account = guardedSplice(byteArray, 0, PUBLIC_KEY_LENGTH);
831
855
  accountKeys.push(new PublicKey(Buffer.from(account)));
832
856
  }
833
- const recentBlockhash = byteArray.slice(0, PUBLIC_KEY_LENGTH);
834
- byteArray = byteArray.slice(PUBLIC_KEY_LENGTH);
857
+ const recentBlockhash = guardedSplice(byteArray, 0, PUBLIC_KEY_LENGTH);
835
858
  const instructionCount = decodeLength(byteArray);
836
859
  let instructions = [];
837
860
  for (let i = 0; i < instructionCount; i++) {
838
- const programIdIndex = byteArray.shift();
861
+ const programIdIndex = guardedShift(byteArray);
839
862
  const accountCount = decodeLength(byteArray);
840
- const accounts = byteArray.slice(0, accountCount);
841
- byteArray = byteArray.slice(accountCount);
863
+ const accounts = guardedSplice(byteArray, 0, accountCount);
842
864
  const dataLength = decodeLength(byteArray);
843
- const dataSlice = byteArray.slice(0, dataLength);
865
+ const dataSlice = guardedSplice(byteArray, 0, dataLength);
844
866
  const data = bs58.encode(Buffer.from(dataSlice));
845
- byteArray = byteArray.slice(dataLength);
846
867
  instructions.push({
847
868
  programIdIndex,
848
869
  accounts,
@@ -1047,30 +1068,30 @@ class MessageV0 {
1047
1068
  }
1048
1069
  static deserialize(serializedMessage) {
1049
1070
  let byteArray = [...serializedMessage];
1050
- const prefix = byteArray.shift();
1071
+ const prefix = guardedShift(byteArray);
1051
1072
  const maskedPrefix = prefix & VERSION_PREFIX_MASK;
1052
1073
  assert(prefix !== maskedPrefix, `Expected versioned message but received legacy message`);
1053
1074
  const version = maskedPrefix;
1054
1075
  assert(version === 0, `Expected versioned message with version 0 but found version ${version}`);
1055
1076
  const header = {
1056
- numRequiredSignatures: byteArray.shift(),
1057
- numReadonlySignedAccounts: byteArray.shift(),
1058
- numReadonlyUnsignedAccounts: byteArray.shift()
1077
+ numRequiredSignatures: guardedShift(byteArray),
1078
+ numReadonlySignedAccounts: guardedShift(byteArray),
1079
+ numReadonlyUnsignedAccounts: guardedShift(byteArray)
1059
1080
  };
1060
1081
  const staticAccountKeys = [];
1061
1082
  const staticAccountKeysLength = decodeLength(byteArray);
1062
1083
  for (let i = 0; i < staticAccountKeysLength; i++) {
1063
- staticAccountKeys.push(new PublicKey(byteArray.splice(0, PUBLIC_KEY_LENGTH)));
1084
+ staticAccountKeys.push(new PublicKey(guardedSplice(byteArray, 0, PUBLIC_KEY_LENGTH)));
1064
1085
  }
1065
- const recentBlockhash = bs58.encode(byteArray.splice(0, PUBLIC_KEY_LENGTH));
1086
+ const recentBlockhash = bs58.encode(guardedSplice(byteArray, 0, PUBLIC_KEY_LENGTH));
1066
1087
  const instructionCount = decodeLength(byteArray);
1067
1088
  const compiledInstructions = [];
1068
1089
  for (let i = 0; i < instructionCount; i++) {
1069
- const programIdIndex = byteArray.shift();
1090
+ const programIdIndex = guardedShift(byteArray);
1070
1091
  const accountKeyIndexesLength = decodeLength(byteArray);
1071
- const accountKeyIndexes = byteArray.splice(0, accountKeyIndexesLength);
1092
+ const accountKeyIndexes = guardedSplice(byteArray, 0, accountKeyIndexesLength);
1072
1093
  const dataLength = decodeLength(byteArray);
1073
- const data = new Uint8Array(byteArray.splice(0, dataLength));
1094
+ const data = new Uint8Array(guardedSplice(byteArray, 0, dataLength));
1074
1095
  compiledInstructions.push({
1075
1096
  programIdIndex,
1076
1097
  accountKeyIndexes,
@@ -1080,11 +1101,11 @@ class MessageV0 {
1080
1101
  const addressTableLookupsCount = decodeLength(byteArray);
1081
1102
  const addressTableLookups = [];
1082
1103
  for (let i = 0; i < addressTableLookupsCount; i++) {
1083
- const accountKey = new PublicKey(byteArray.splice(0, PUBLIC_KEY_LENGTH));
1104
+ const accountKey = new PublicKey(guardedSplice(byteArray, 0, PUBLIC_KEY_LENGTH));
1084
1105
  const writableIndexesLength = decodeLength(byteArray);
1085
- const writableIndexes = byteArray.splice(0, writableIndexesLength);
1106
+ const writableIndexes = guardedSplice(byteArray, 0, writableIndexesLength);
1086
1107
  const readonlyIndexesLength = decodeLength(byteArray);
1087
- const readonlyIndexes = byteArray.splice(0, readonlyIndexesLength);
1108
+ const readonlyIndexes = guardedSplice(byteArray, 0, readonlyIndexesLength);
1088
1109
  addressTableLookups.push({
1089
1110
  accountKey,
1090
1111
  writableIndexes,
@@ -1870,8 +1891,7 @@ class Transaction {
1870
1891
  const signatureCount = decodeLength(byteArray);
1871
1892
  let signatures = [];
1872
1893
  for (let i = 0; i < signatureCount; i++) {
1873
- const signature = byteArray.slice(0, SIGNATURE_LENGTH_IN_BYTES);
1874
- byteArray = byteArray.slice(SIGNATURE_LENGTH_IN_BYTES);
1894
+ const signature = guardedSplice(byteArray, 0, SIGNATURE_LENGTH_IN_BYTES);
1875
1895
  signatures.push(bs58.encode(Buffer.from(signature)));
1876
1896
  }
1877
1897
  return Transaction.populate(Message.from(byteArray), signatures);
@@ -2046,7 +2066,7 @@ class VersionedTransaction {
2046
2066
  const signatures = [];
2047
2067
  const signaturesLength = decodeLength(byteArray);
2048
2068
  for (let i = 0; i < signaturesLength; i++) {
2049
- signatures.push(new Uint8Array(byteArray.splice(0, SIGNATURE_LENGTH_IN_BYTES)));
2069
+ signatures.push(new Uint8Array(guardedSplice(byteArray, 0, SIGNATURE_LENGTH_IN_BYTES)));
2050
2070
  }
2051
2071
  const message = VersionedMessage.deserialize(new Uint8Array(byteArray));
2052
2072
  return new VersionedTransaction(message, signatures);
@@ -5757,7 +5777,7 @@ const LogsNotificationResult = type({
5757
5777
 
5758
5778
  /** @internal */
5759
5779
  const COMMON_HTTP_HEADERS = {
5760
- 'solana-client': `js/${"0.0.0-development" }`
5780
+ 'solana-client': `js/${"1.88.1" }`
5761
5781
  };
5762
5782
 
5763
5783
  /**
@@ -10692,10 +10712,8 @@ class ValidatorInfo {
10692
10712
  if (configKeyCount !== 2) return null;
10693
10713
  const configKeys = [];
10694
10714
  for (let i = 0; i < 2; i++) {
10695
- const publicKey = new PublicKey(byteArray.slice(0, PUBLIC_KEY_LENGTH));
10696
- byteArray = byteArray.slice(PUBLIC_KEY_LENGTH);
10697
- const isSigner = byteArray.slice(0, 1)[0] === 1;
10698
- byteArray = byteArray.slice(1);
10715
+ const publicKey = new PublicKey(guardedSplice(byteArray, 0, PUBLIC_KEY_LENGTH));
10716
+ const isSigner = guardedShift(byteArray) === 1;
10699
10717
  configKeys.push({
10700
10718
  publicKey,
10701
10719
  isSigner