@solana/web3.js 1.90.1 → 1.90.2

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,
@@ -1875,8 +1896,7 @@ class Transaction {
1875
1896
  const signatureCount = decodeLength(byteArray);
1876
1897
  let signatures = [];
1877
1898
  for (let i = 0; i < signatureCount; i++) {
1878
- const signature = byteArray.slice(0, SIGNATURE_LENGTH_IN_BYTES);
1879
- byteArray = byteArray.slice(SIGNATURE_LENGTH_IN_BYTES);
1899
+ const signature = guardedSplice(byteArray, 0, SIGNATURE_LENGTH_IN_BYTES);
1880
1900
  signatures.push(bs58.encode(Buffer.from(signature)));
1881
1901
  }
1882
1902
  return Transaction.populate(Message.from(byteArray), signatures);
@@ -2051,7 +2071,7 @@ class VersionedTransaction {
2051
2071
  const signatures = [];
2052
2072
  const signaturesLength = decodeLength(byteArray);
2053
2073
  for (let i = 0; i < signaturesLength; i++) {
2054
- signatures.push(new Uint8Array(byteArray.splice(0, SIGNATURE_LENGTH_IN_BYTES)));
2074
+ signatures.push(new Uint8Array(guardedSplice(byteArray, 0, SIGNATURE_LENGTH_IN_BYTES)));
2055
2075
  }
2056
2076
  const message = VersionedMessage.deserialize(new Uint8Array(byteArray));
2057
2077
  return new VersionedTransaction(message, signatures);
@@ -5791,7 +5811,7 @@ const LogsNotificationResult = type({
5791
5811
 
5792
5812
  /** @internal */
5793
5813
  const COMMON_HTTP_HEADERS = {
5794
- 'solana-client': `js/${"0.0.0-development" }`
5814
+ 'solana-client': `js/${"1.90.2" }`
5795
5815
  };
5796
5816
 
5797
5817
  /**
@@ -10751,10 +10771,8 @@ class ValidatorInfo {
10751
10771
  if (configKeyCount !== 2) return null;
10752
10772
  const configKeys = [];
10753
10773
  for (let i = 0; i < 2; i++) {
10754
- const publicKey = new PublicKey(byteArray.slice(0, PUBLIC_KEY_LENGTH));
10755
- byteArray = byteArray.slice(PUBLIC_KEY_LENGTH);
10756
- const isSigner = byteArray.slice(0, 1)[0] === 1;
10757
- byteArray = byteArray.slice(1);
10774
+ const publicKey = new PublicKey(guardedSplice(byteArray, 0, PUBLIC_KEY_LENGTH));
10775
+ const isSigner = guardedShift(byteArray) === 1;
10758
10776
  configKeys.push({
10759
10777
  publicKey,
10760
10778
  isSigner