@solana/web3.js 1.67.1 → 1.67.3

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
@@ -786,6 +786,36 @@ class CompiledKeys {
786
786
 
787
787
  }
788
788
 
789
+ const END_OF_BUFFER_ERROR_MESSAGE = 'Reached end of buffer unexpectedly';
790
+ /**
791
+ * Delegates to `Array#shift`, but throws if the array is zero-length.
792
+ */
793
+
794
+ function guardedShift(byteArray) {
795
+ if (byteArray.length === 0) {
796
+ throw new Error(END_OF_BUFFER_ERROR_MESSAGE);
797
+ }
798
+
799
+ return byteArray.shift();
800
+ }
801
+ /**
802
+ * Delegates to `Array#splice`, but throws if the section being spliced out extends past the end of
803
+ * the array.
804
+ */
805
+
806
+ function guardedSplice(byteArray, ...args) {
807
+ var _args$;
808
+
809
+ const [start] = args;
810
+
811
+ if (args.length === 2 // Implies that `deleteCount` was supplied
812
+ ? start + ((_args$ = args[1]) !== null && _args$ !== void 0 ? _args$ : 0) > byteArray.length : start >= byteArray.length) {
813
+ throw new Error(END_OF_BUFFER_ERROR_MESSAGE);
814
+ }
815
+
816
+ return byteArray.splice(...args);
817
+ }
818
+
789
819
  /**
790
820
  * An instruction to execute by a program
791
821
  *
@@ -937,37 +967,33 @@ class Message {
937
967
  static from(buffer$1) {
938
968
  // Slice up wire data
939
969
  let byteArray = [...buffer$1];
940
- const numRequiredSignatures = byteArray.shift();
970
+ const numRequiredSignatures = guardedShift(byteArray);
941
971
 
942
972
  if (numRequiredSignatures !== (numRequiredSignatures & VERSION_PREFIX_MASK)) {
943
973
  throw new Error('Versioned messages must be deserialized with VersionedMessage.deserialize()');
944
974
  }
945
975
 
946
- const numReadonlySignedAccounts = byteArray.shift();
947
- const numReadonlyUnsignedAccounts = byteArray.shift();
976
+ const numReadonlySignedAccounts = guardedShift(byteArray);
977
+ const numReadonlyUnsignedAccounts = guardedShift(byteArray);
948
978
  const accountCount = decodeLength(byteArray);
949
979
  let accountKeys = [];
950
980
 
951
981
  for (let i = 0; i < accountCount; i++) {
952
- const account = byteArray.slice(0, PUBLIC_KEY_LENGTH);
953
- byteArray = byteArray.slice(PUBLIC_KEY_LENGTH);
982
+ const account = guardedSplice(byteArray, 0, PUBLIC_KEY_LENGTH);
954
983
  accountKeys.push(new PublicKey(buffer.Buffer.from(account)));
955
984
  }
956
985
 
957
- const recentBlockhash = byteArray.slice(0, PUBLIC_KEY_LENGTH);
958
- byteArray = byteArray.slice(PUBLIC_KEY_LENGTH);
986
+ const recentBlockhash = guardedSplice(byteArray, 0, PUBLIC_KEY_LENGTH);
959
987
  const instructionCount = decodeLength(byteArray);
960
988
  let instructions = [];
961
989
 
962
990
  for (let i = 0; i < instructionCount; i++) {
963
- const programIdIndex = byteArray.shift();
991
+ const programIdIndex = guardedShift(byteArray);
964
992
  const accountCount = decodeLength(byteArray);
965
- const accounts = byteArray.slice(0, accountCount);
966
- byteArray = byteArray.slice(accountCount);
993
+ const accounts = guardedSplice(byteArray, 0, accountCount);
967
994
  const dataLength = decodeLength(byteArray);
968
- const dataSlice = byteArray.slice(0, dataLength);
995
+ const dataSlice = guardedSplice(byteArray, 0, dataLength);
969
996
  const data = bs58__default["default"].encode(buffer.Buffer.from(dataSlice));
970
- byteArray = byteArray.slice(dataLength);
971
997
  instructions.push({
972
998
  programIdIndex,
973
999
  accounts,
@@ -1203,33 +1229,33 @@ class MessageV0 {
1203
1229
 
1204
1230
  static deserialize(serializedMessage) {
1205
1231
  let byteArray = [...serializedMessage];
1206
- const prefix = byteArray.shift();
1232
+ const prefix = guardedShift(byteArray);
1207
1233
  const maskedPrefix = prefix & VERSION_PREFIX_MASK;
1208
1234
  assert(prefix !== maskedPrefix, `Expected versioned message but received legacy message`);
1209
1235
  const version = maskedPrefix;
1210
1236
  assert(version === 0, `Expected versioned message with version 0 but found version ${version}`);
1211
1237
  const header = {
1212
- numRequiredSignatures: byteArray.shift(),
1213
- numReadonlySignedAccounts: byteArray.shift(),
1214
- numReadonlyUnsignedAccounts: byteArray.shift()
1238
+ numRequiredSignatures: guardedShift(byteArray),
1239
+ numReadonlySignedAccounts: guardedShift(byteArray),
1240
+ numReadonlyUnsignedAccounts: guardedShift(byteArray)
1215
1241
  };
1216
1242
  const staticAccountKeys = [];
1217
1243
  const staticAccountKeysLength = decodeLength(byteArray);
1218
1244
 
1219
1245
  for (let i = 0; i < staticAccountKeysLength; i++) {
1220
- staticAccountKeys.push(new PublicKey(byteArray.splice(0, PUBLIC_KEY_LENGTH)));
1246
+ staticAccountKeys.push(new PublicKey(guardedSplice(byteArray, 0, PUBLIC_KEY_LENGTH)));
1221
1247
  }
1222
1248
 
1223
- const recentBlockhash = bs58__default["default"].encode(byteArray.splice(0, PUBLIC_KEY_LENGTH));
1249
+ const recentBlockhash = bs58__default["default"].encode(guardedSplice(byteArray, 0, PUBLIC_KEY_LENGTH));
1224
1250
  const instructionCount = decodeLength(byteArray);
1225
1251
  const compiledInstructions = [];
1226
1252
 
1227
1253
  for (let i = 0; i < instructionCount; i++) {
1228
- const programIdIndex = byteArray.shift();
1254
+ const programIdIndex = guardedShift(byteArray);
1229
1255
  const accountKeyIndexesLength = decodeLength(byteArray);
1230
- const accountKeyIndexes = byteArray.splice(0, accountKeyIndexesLength);
1256
+ const accountKeyIndexes = guardedSplice(byteArray, 0, accountKeyIndexesLength);
1231
1257
  const dataLength = decodeLength(byteArray);
1232
- const data = new Uint8Array(byteArray.splice(0, dataLength));
1258
+ const data = new Uint8Array(guardedSplice(byteArray, 0, dataLength));
1233
1259
  compiledInstructions.push({
1234
1260
  programIdIndex,
1235
1261
  accountKeyIndexes,
@@ -1241,11 +1267,11 @@ class MessageV0 {
1241
1267
  const addressTableLookups = [];
1242
1268
 
1243
1269
  for (let i = 0; i < addressTableLookupsCount; i++) {
1244
- const accountKey = new PublicKey(byteArray.splice(0, PUBLIC_KEY_LENGTH));
1270
+ const accountKey = new PublicKey(guardedSplice(byteArray, 0, PUBLIC_KEY_LENGTH));
1245
1271
  const writableIndexesLength = decodeLength(byteArray);
1246
- const writableIndexes = byteArray.splice(0, writableIndexesLength);
1272
+ const writableIndexes = guardedSplice(byteArray, 0, writableIndexesLength);
1247
1273
  const readonlyIndexesLength = decodeLength(byteArray);
1248
- const readonlyIndexes = byteArray.splice(0, readonlyIndexesLength);
1274
+ const readonlyIndexes = guardedSplice(byteArray, 0, readonlyIndexesLength);
1249
1275
  addressTableLookups.push({
1250
1276
  accountKey,
1251
1277
  writableIndexes,
@@ -1985,8 +2011,7 @@ class Transaction {
1985
2011
  let signatures = [];
1986
2012
 
1987
2013
  for (let i = 0; i < signatureCount; i++) {
1988
- const signature = byteArray.slice(0, SIGNATURE_LENGTH_IN_BYTES);
1989
- byteArray = byteArray.slice(SIGNATURE_LENGTH_IN_BYTES);
2014
+ const signature = guardedSplice(byteArray, 0, SIGNATURE_LENGTH_IN_BYTES);
1990
2015
  signatures.push(bs58__default["default"].encode(buffer.Buffer.from(signature)));
1991
2016
  }
1992
2017
 
@@ -2184,7 +2209,7 @@ class VersionedTransaction {
2184
2209
  const signaturesLength = decodeLength(byteArray);
2185
2210
 
2186
2211
  for (let i = 0; i < signaturesLength; i++) {
2187
- signatures.push(new Uint8Array(byteArray.splice(0, SIGNATURE_LENGTH_IN_BYTES)));
2212
+ signatures.push(new Uint8Array(guardedSplice(byteArray, 0, SIGNATURE_LENGTH_IN_BYTES)));
2188
2213
  }
2189
2214
 
2190
2215
  const message = VersionedMessage.deserialize(new Uint8Array(byteArray));
@@ -4697,7 +4722,7 @@ const LogsNotificationResult = superstruct.type({
4697
4722
 
4698
4723
  /** @internal */
4699
4724
  const COMMON_HTTP_HEADERS = {
4700
- 'solana-client': `js/${(_process$env$npm_pack = "0.0.0-development") !== null && _process$env$npm_pack !== void 0 ? _process$env$npm_pack : 'UNKNOWN'}`
4725
+ 'solana-client': `js/${(_process$env$npm_pack = "1.67.3") !== null && _process$env$npm_pack !== void 0 ? _process$env$npm_pack : 'UNKNOWN'}`
4701
4726
  };
4702
4727
  /**
4703
4728
  * A connection to a fullnode JSON RPC endpoint
@@ -6079,7 +6104,7 @@ class Connection {
6079
6104
 
6080
6105
 
6081
6106
  async getFeeForMessage(message, commitment) {
6082
- const wireMessage = message.serialize().toString('base64');
6107
+ const wireMessage = toBuffer(message.serialize()).toString('base64');
6083
6108
 
6084
6109
  const args = this._buildArgs([wireMessage], commitment);
6085
6110
 
@@ -10020,10 +10045,8 @@ class ValidatorInfo {
10020
10045
  const configKeys = [];
10021
10046
 
10022
10047
  for (let i = 0; i < 2; i++) {
10023
- const publicKey = new PublicKey(byteArray.slice(0, PUBLIC_KEY_LENGTH));
10024
- byteArray = byteArray.slice(PUBLIC_KEY_LENGTH);
10025
- const isSigner = byteArray.slice(0, 1)[0] === 1;
10026
- byteArray = byteArray.slice(1);
10048
+ const publicKey = new PublicKey(guardedSplice(byteArray, 0, PUBLIC_KEY_LENGTH));
10049
+ const isSigner = guardedShift(byteArray) === 1;
10027
10050
  configKeys.push({
10028
10051
  publicKey,
10029
10052
  isSigner