@solana/web3.js 1.58.0 → 1.58.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
@@ -726,6 +726,36 @@ class CompiledKeys {
726
726
 
727
727
  }
728
728
 
729
+ const END_OF_BUFFER_ERROR_MESSAGE = 'Reached end of buffer unexpectedly';
730
+ /**
731
+ * Delegates to `Array#shift`, but throws if the array is zero-length.
732
+ */
733
+
734
+ function guardedShift(byteArray) {
735
+ if (byteArray.length === 0) {
736
+ throw new Error(END_OF_BUFFER_ERROR_MESSAGE);
737
+ }
738
+
739
+ return byteArray.shift();
740
+ }
741
+ /**
742
+ * Delegates to `Array#splice`, but throws if the section being spliced out extends past the end of
743
+ * the array.
744
+ */
745
+
746
+ function guardedSplice(byteArray, ...args) {
747
+ var _args$;
748
+
749
+ const [start] = args;
750
+
751
+ if (args.length === 2 // Implies that `deleteCount` was supplied
752
+ ? start + ((_args$ = args[1]) !== null && _args$ !== void 0 ? _args$ : 0) > byteArray.length : start >= byteArray.length) {
753
+ throw new Error(END_OF_BUFFER_ERROR_MESSAGE);
754
+ }
755
+
756
+ return byteArray.splice(...args);
757
+ }
758
+
729
759
  /**
730
760
  * An instruction to execute by a program
731
761
  *
@@ -867,37 +897,33 @@ class Message {
867
897
  static from(buffer) {
868
898
  // Slice up wire data
869
899
  let byteArray = [...buffer];
870
- const numRequiredSignatures = byteArray.shift();
900
+ const numRequiredSignatures = guardedShift(byteArray);
871
901
 
872
902
  if (numRequiredSignatures !== (numRequiredSignatures & VERSION_PREFIX_MASK)) {
873
903
  throw new Error('Versioned messages must be deserialized with VersionedMessage.deserialize()');
874
904
  }
875
905
 
876
- const numReadonlySignedAccounts = byteArray.shift();
877
- const numReadonlyUnsignedAccounts = byteArray.shift();
906
+ const numReadonlySignedAccounts = guardedShift(byteArray);
907
+ const numReadonlyUnsignedAccounts = guardedShift(byteArray);
878
908
  const accountCount = decodeLength(byteArray);
879
909
  let accountKeys = [];
880
910
 
881
911
  for (let i = 0; i < accountCount; i++) {
882
- const account = byteArray.slice(0, PUBLIC_KEY_LENGTH);
883
- byteArray = byteArray.slice(PUBLIC_KEY_LENGTH);
912
+ const account = guardedSplice(byteArray, 0, PUBLIC_KEY_LENGTH);
884
913
  accountKeys.push(new PublicKey(Buffer.from(account)));
885
914
  }
886
915
 
887
- const recentBlockhash = byteArray.slice(0, PUBLIC_KEY_LENGTH);
888
- byteArray = byteArray.slice(PUBLIC_KEY_LENGTH);
916
+ const recentBlockhash = guardedSplice(byteArray, 0, PUBLIC_KEY_LENGTH);
889
917
  const instructionCount = decodeLength(byteArray);
890
918
  let instructions = [];
891
919
 
892
920
  for (let i = 0; i < instructionCount; i++) {
893
- const programIdIndex = byteArray.shift();
921
+ const programIdIndex = guardedShift(byteArray);
894
922
  const accountCount = decodeLength(byteArray);
895
- const accounts = byteArray.slice(0, accountCount);
896
- byteArray = byteArray.slice(accountCount);
923
+ const accounts = guardedSplice(byteArray, 0, accountCount);
897
924
  const dataLength = decodeLength(byteArray);
898
- const dataSlice = byteArray.slice(0, dataLength);
925
+ const dataSlice = guardedSplice(byteArray, 0, dataLength);
899
926
  const data = bs58.encode(Buffer.from(dataSlice));
900
- byteArray = byteArray.slice(dataLength);
901
927
  instructions.push({
902
928
  programIdIndex,
903
929
  accounts,
@@ -1110,33 +1136,33 @@ class MessageV0 {
1110
1136
 
1111
1137
  static deserialize(serializedMessage) {
1112
1138
  let byteArray = [...serializedMessage];
1113
- const prefix = byteArray.shift();
1139
+ const prefix = guardedShift(byteArray);
1114
1140
  const maskedPrefix = prefix & VERSION_PREFIX_MASK;
1115
1141
  assert(prefix !== maskedPrefix, `Expected versioned message but received legacy message`);
1116
1142
  const version = maskedPrefix;
1117
1143
  assert(version === 0, `Expected versioned message with version 0 but found version ${version}`);
1118
1144
  const header = {
1119
- numRequiredSignatures: byteArray.shift(),
1120
- numReadonlySignedAccounts: byteArray.shift(),
1121
- numReadonlyUnsignedAccounts: byteArray.shift()
1145
+ numRequiredSignatures: guardedShift(byteArray),
1146
+ numReadonlySignedAccounts: guardedShift(byteArray),
1147
+ numReadonlyUnsignedAccounts: guardedShift(byteArray)
1122
1148
  };
1123
1149
  const staticAccountKeys = [];
1124
1150
  const staticAccountKeysLength = decodeLength(byteArray);
1125
1151
 
1126
1152
  for (let i = 0; i < staticAccountKeysLength; i++) {
1127
- staticAccountKeys.push(new PublicKey(byteArray.splice(0, PUBLIC_KEY_LENGTH)));
1153
+ staticAccountKeys.push(new PublicKey(guardedSplice(byteArray, 0, PUBLIC_KEY_LENGTH)));
1128
1154
  }
1129
1155
 
1130
- const recentBlockhash = bs58.encode(byteArray.splice(0, PUBLIC_KEY_LENGTH));
1156
+ const recentBlockhash = bs58.encode(guardedSplice(byteArray, 0, PUBLIC_KEY_LENGTH));
1131
1157
  const instructionCount = decodeLength(byteArray);
1132
1158
  const compiledInstructions = [];
1133
1159
 
1134
1160
  for (let i = 0; i < instructionCount; i++) {
1135
- const programIdIndex = byteArray.shift();
1161
+ const programIdIndex = guardedShift(byteArray);
1136
1162
  const accountKeyIndexesLength = decodeLength(byteArray);
1137
- const accountKeyIndexes = byteArray.splice(0, accountKeyIndexesLength);
1163
+ const accountKeyIndexes = guardedSplice(byteArray, 0, accountKeyIndexesLength);
1138
1164
  const dataLength = decodeLength(byteArray);
1139
- const data = new Uint8Array(byteArray.splice(0, dataLength));
1165
+ const data = new Uint8Array(guardedSplice(byteArray, 0, dataLength));
1140
1166
  compiledInstructions.push({
1141
1167
  programIdIndex,
1142
1168
  accountKeyIndexes,
@@ -1148,11 +1174,11 @@ class MessageV0 {
1148
1174
  const addressTableLookups = [];
1149
1175
 
1150
1176
  for (let i = 0; i < addressTableLookupsCount; i++) {
1151
- const accountKey = new PublicKey(byteArray.splice(0, PUBLIC_KEY_LENGTH));
1177
+ const accountKey = new PublicKey(guardedSplice(byteArray, 0, PUBLIC_KEY_LENGTH));
1152
1178
  const writableIndexesLength = decodeLength(byteArray);
1153
- const writableIndexes = byteArray.splice(0, writableIndexesLength);
1179
+ const writableIndexes = guardedSplice(byteArray, 0, writableIndexesLength);
1154
1180
  const readonlyIndexesLength = decodeLength(byteArray);
1155
- const readonlyIndexes = byteArray.splice(0, readonlyIndexesLength);
1181
+ const readonlyIndexes = guardedSplice(byteArray, 0, readonlyIndexesLength);
1156
1182
  addressTableLookups.push({
1157
1183
  accountKey,
1158
1184
  writableIndexes,
@@ -1883,8 +1909,7 @@ class Transaction {
1883
1909
  let signatures = [];
1884
1910
 
1885
1911
  for (let i = 0; i < signatureCount; i++) {
1886
- const signature = byteArray.slice(0, SIGNATURE_LENGTH_IN_BYTES);
1887
- byteArray = byteArray.slice(SIGNATURE_LENGTH_IN_BYTES);
1912
+ const signature = guardedSplice(byteArray, 0, SIGNATURE_LENGTH_IN_BYTES);
1888
1913
  signatures.push(bs58.encode(Buffer.from(signature)));
1889
1914
  }
1890
1915
 
@@ -2084,7 +2109,7 @@ class VersionedTransaction {
2084
2109
  const signaturesLength = decodeLength(byteArray);
2085
2110
 
2086
2111
  for (let i = 0; i < signaturesLength; i++) {
2087
- signatures.push(new Uint8Array(byteArray.splice(0, SIGNATURE_LENGTH_IN_BYTES)));
2112
+ signatures.push(new Uint8Array(guardedSplice(byteArray, 0, SIGNATURE_LENGTH_IN_BYTES)));
2088
2113
  }
2089
2114
 
2090
2115
  const message = VersionedMessage.deserialize(new Uint8Array(byteArray));
@@ -4543,7 +4568,7 @@ const LogsNotificationResult = type({
4543
4568
 
4544
4569
  /** @internal */
4545
4570
  const COMMON_HTTP_HEADERS = {
4546
- 'solana-client': `js/${(_process$env$npm_pack = "0.0.0-development") !== null && _process$env$npm_pack !== void 0 ? _process$env$npm_pack : 'UNKNOWN'}`
4571
+ 'solana-client': `js/${(_process$env$npm_pack = "1.58.1") !== null && _process$env$npm_pack !== void 0 ? _process$env$npm_pack : 'UNKNOWN'}`
4547
4572
  };
4548
4573
  /**
4549
4574
  * A connection to a fullnode JSON RPC endpoint
@@ -9383,10 +9408,8 @@ class ValidatorInfo {
9383
9408
  const configKeys = [];
9384
9409
 
9385
9410
  for (let i = 0; i < 2; i++) {
9386
- const publicKey = new PublicKey(byteArray.slice(0, PUBLIC_KEY_LENGTH));
9387
- byteArray = byteArray.slice(PUBLIC_KEY_LENGTH);
9388
- const isSigner = byteArray.slice(0, 1)[0] === 1;
9389
- byteArray = byteArray.slice(1);
9411
+ const publicKey = new PublicKey(guardedSplice(byteArray, 0, PUBLIC_KEY_LENGTH));
9412
+ const isSigner = guardedShift(byteArray) === 1;
9390
9413
  configKeys.push({
9391
9414
  publicKey,
9392
9415
  isSigner