@solana/web3.js 1.6.0 → 1.6.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.
@@ -1255,6 +1255,36 @@ function encodeLength(bytes, len) {
1255
1255
  }
1256
1256
  }
1257
1257
 
1258
+ const END_OF_BUFFER_ERROR_MESSAGE = 'Reached end of buffer unexpectedly';
1259
+ /**
1260
+ * Delegates to `Array#shift`, but throws if the array is zero-length.
1261
+ */
1262
+
1263
+ function guardedShift(byteArray) {
1264
+ if (byteArray.length === 0) {
1265
+ throw new Error(END_OF_BUFFER_ERROR_MESSAGE);
1266
+ }
1267
+
1268
+ return byteArray.shift();
1269
+ }
1270
+ /**
1271
+ * Delegates to `Array#splice`, but throws if the section being spliced out extends past the end of
1272
+ * the array.
1273
+ */
1274
+
1275
+ function guardedSplice(byteArray, ...args) {
1276
+ var _args$;
1277
+
1278
+ const [start] = args;
1279
+
1280
+ if (args.length === 2 // Implies that `deleteCount` was supplied
1281
+ ? start + ((_args$ = args[1]) !== null && _args$ !== void 0 ? _args$ : 0) > byteArray.length : start >= byteArray.length) {
1282
+ throw new Error(END_OF_BUFFER_ERROR_MESSAGE);
1283
+ }
1284
+
1285
+ return byteArray.splice(...args);
1286
+ }
1287
+
1258
1288
  /**
1259
1289
  * The message header, identifying signed and read-only account
1260
1290
  */
@@ -1339,32 +1369,28 @@ class Message {
1339
1369
  static from(buffer) {
1340
1370
  // Slice up wire data
1341
1371
  let byteArray = [...buffer];
1342
- const numRequiredSignatures = byteArray.shift();
1343
- const numReadonlySignedAccounts = byteArray.shift();
1344
- const numReadonlyUnsignedAccounts = byteArray.shift();
1372
+ const numRequiredSignatures = guardedShift(byteArray);
1373
+ const numReadonlySignedAccounts = guardedShift(byteArray);
1374
+ const numReadonlyUnsignedAccounts = guardedShift(byteArray);
1345
1375
  const accountCount = decodeLength(byteArray);
1346
1376
  let accountKeys = [];
1347
1377
 
1348
1378
  for (let i = 0; i < accountCount; i++) {
1349
- const account = byteArray.slice(0, PUBKEY_LENGTH);
1350
- byteArray = byteArray.slice(PUBKEY_LENGTH);
1379
+ const account = guardedSplice(byteArray, 0, PUBKEY_LENGTH);
1351
1380
  accountKeys.push(bs58.encode(Buffer.from(account)));
1352
1381
  }
1353
1382
 
1354
- const recentBlockhash = byteArray.slice(0, PUBKEY_LENGTH);
1355
- byteArray = byteArray.slice(PUBKEY_LENGTH);
1383
+ const recentBlockhash = guardedSplice(byteArray, 0, PUBKEY_LENGTH);
1356
1384
  const instructionCount = decodeLength(byteArray);
1357
1385
  let instructions = [];
1358
1386
 
1359
1387
  for (let i = 0; i < instructionCount; i++) {
1360
- const programIdIndex = byteArray.shift();
1388
+ const programIdIndex = guardedShift(byteArray);
1361
1389
  const accountCount = decodeLength(byteArray);
1362
- const accounts = byteArray.slice(0, accountCount);
1363
- byteArray = byteArray.slice(accountCount);
1390
+ const accounts = guardedSplice(byteArray, 0, accountCount);
1364
1391
  const dataLength = decodeLength(byteArray);
1365
- const dataSlice = byteArray.slice(0, dataLength);
1392
+ const dataSlice = guardedSplice(byteArray, 0, dataLength);
1366
1393
  const data = bs58.encode(Buffer.from(dataSlice));
1367
- byteArray = byteArray.slice(dataLength);
1368
1394
  instructions.push({
1369
1395
  programIdIndex,
1370
1396
  accounts,
@@ -1984,8 +2010,7 @@ class Transaction {
1984
2010
  let signatures = [];
1985
2011
 
1986
2012
  for (let i = 0; i < signatureCount; i++) {
1987
- const signature = byteArray.slice(0, SIGNATURE_LENGTH);
1988
- byteArray = byteArray.slice(SIGNATURE_LENGTH);
2013
+ const signature = guardedSplice(byteArray, 0, SIGNATURE_LENGTH);
1989
2014
  signatures.push(bs58.encode(Buffer.from(signature)));
1990
2015
  }
1991
2016
 
@@ -7844,10 +7869,8 @@ class ValidatorInfo {
7844
7869
  const configKeys = [];
7845
7870
 
7846
7871
  for (let i = 0; i < 2; i++) {
7847
- const publicKey = new PublicKey(byteArray.slice(0, PUBKEY_LENGTH));
7848
- byteArray = byteArray.slice(PUBKEY_LENGTH);
7849
- const isSigner = byteArray.slice(0, 1)[0] === 1;
7850
- byteArray = byteArray.slice(1);
7872
+ const publicKey = new PublicKey(guardedSplice(byteArray, 0, PUBKEY_LENGTH));
7873
+ const isSigner = guardedShift(byteArray) === 1;
7851
7874
  configKeys.push({
7852
7875
  publicKey,
7853
7876
  isSigner