@solana/web3.js 1.7.1 → 1.7.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.
@@ -1268,6 +1268,36 @@ function encodeLength(bytes, len) {
1268
1268
  }
1269
1269
  }
1270
1270
 
1271
+ const END_OF_BUFFER_ERROR_MESSAGE = 'Reached end of buffer unexpectedly';
1272
+ /**
1273
+ * Delegates to `Array#shift`, but throws if the array is zero-length.
1274
+ */
1275
+
1276
+ function guardedShift(byteArray) {
1277
+ if (byteArray.length === 0) {
1278
+ throw new Error(END_OF_BUFFER_ERROR_MESSAGE);
1279
+ }
1280
+
1281
+ return byteArray.shift();
1282
+ }
1283
+ /**
1284
+ * Delegates to `Array#splice`, but throws if the section being spliced out extends past the end of
1285
+ * the array.
1286
+ */
1287
+
1288
+ function guardedSplice(byteArray, ...args) {
1289
+ var _args$;
1290
+
1291
+ const [start] = args;
1292
+
1293
+ if (args.length === 2 // Implies that `deleteCount` was supplied
1294
+ ? start + ((_args$ = args[1]) !== null && _args$ !== void 0 ? _args$ : 0) > byteArray.length : start >= byteArray.length) {
1295
+ throw new Error(END_OF_BUFFER_ERROR_MESSAGE);
1296
+ }
1297
+
1298
+ return byteArray.splice(...args);
1299
+ }
1300
+
1271
1301
  /**
1272
1302
  * The message header, identifying signed and read-only account
1273
1303
  */
@@ -1352,32 +1382,28 @@ class Message {
1352
1382
  static from(buffer) {
1353
1383
  // Slice up wire data
1354
1384
  let byteArray = [...buffer];
1355
- const numRequiredSignatures = byteArray.shift();
1356
- const numReadonlySignedAccounts = byteArray.shift();
1357
- const numReadonlyUnsignedAccounts = byteArray.shift();
1385
+ const numRequiredSignatures = guardedShift(byteArray);
1386
+ const numReadonlySignedAccounts = guardedShift(byteArray);
1387
+ const numReadonlyUnsignedAccounts = guardedShift(byteArray);
1358
1388
  const accountCount = decodeLength(byteArray);
1359
1389
  let accountKeys = [];
1360
1390
 
1361
1391
  for (let i = 0; i < accountCount; i++) {
1362
- const account = byteArray.slice(0, PUBKEY_LENGTH);
1363
- byteArray = byteArray.slice(PUBKEY_LENGTH);
1392
+ const account = guardedSplice(byteArray, 0, PUBKEY_LENGTH);
1364
1393
  accountKeys.push(bs58.encode(Buffer.from(account)));
1365
1394
  }
1366
1395
 
1367
- const recentBlockhash = byteArray.slice(0, PUBKEY_LENGTH);
1368
- byteArray = byteArray.slice(PUBKEY_LENGTH);
1396
+ const recentBlockhash = guardedSplice(byteArray, 0, PUBKEY_LENGTH);
1369
1397
  const instructionCount = decodeLength(byteArray);
1370
1398
  let instructions = [];
1371
1399
 
1372
1400
  for (let i = 0; i < instructionCount; i++) {
1373
- const programIdIndex = byteArray.shift();
1401
+ const programIdIndex = guardedShift(byteArray);
1374
1402
  const accountCount = decodeLength(byteArray);
1375
- const accounts = byteArray.slice(0, accountCount);
1376
- byteArray = byteArray.slice(accountCount);
1403
+ const accounts = guardedSplice(byteArray, 0, accountCount);
1377
1404
  const dataLength = decodeLength(byteArray);
1378
- const dataSlice = byteArray.slice(0, dataLength);
1405
+ const dataSlice = guardedSplice(byteArray, 0, dataLength);
1379
1406
  const data = bs58.encode(Buffer.from(dataSlice));
1380
- byteArray = byteArray.slice(dataLength);
1381
1407
  instructions.push({
1382
1408
  programIdIndex,
1383
1409
  accounts,
@@ -1997,8 +2023,7 @@ class Transaction {
1997
2023
  let signatures = [];
1998
2024
 
1999
2025
  for (let i = 0; i < signatureCount; i++) {
2000
- const signature = byteArray.slice(0, SIGNATURE_LENGTH);
2001
- byteArray = byteArray.slice(SIGNATURE_LENGTH);
2026
+ const signature = guardedSplice(byteArray, 0, SIGNATURE_LENGTH);
2002
2027
  signatures.push(bs58.encode(Buffer.from(signature)));
2003
2028
  }
2004
2029
 
@@ -7896,10 +7921,8 @@ class ValidatorInfo {
7896
7921
  const configKeys = [];
7897
7922
 
7898
7923
  for (let i = 0; i < 2; i++) {
7899
- const publicKey = new PublicKey(byteArray.slice(0, PUBKEY_LENGTH));
7900
- byteArray = byteArray.slice(PUBKEY_LENGTH);
7901
- const isSigner = byteArray.slice(0, 1)[0] === 1;
7902
- byteArray = byteArray.slice(1);
7924
+ const publicKey = new PublicKey(guardedSplice(byteArray, 0, PUBKEY_LENGTH));
7925
+ const isSigner = guardedShift(byteArray) === 1;
7903
7926
  configKeys.push({
7904
7927
  publicKey,
7905
7928
  isSigner