@solana/web3.js 1.19.0 → 1.19.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.
@@ -1329,6 +1329,36 @@ function encodeLength(bytes, len) {
1329
1329
  }
1330
1330
  }
1331
1331
 
1332
+ const END_OF_BUFFER_ERROR_MESSAGE = 'Reached end of buffer unexpectedly';
1333
+ /**
1334
+ * Delegates to `Array#shift`, but throws if the array is zero-length.
1335
+ */
1336
+
1337
+ function guardedShift(byteArray) {
1338
+ if (byteArray.length === 0) {
1339
+ throw new Error(END_OF_BUFFER_ERROR_MESSAGE);
1340
+ }
1341
+
1342
+ return byteArray.shift();
1343
+ }
1344
+ /**
1345
+ * Delegates to `Array#splice`, but throws if the section being spliced out extends past the end of
1346
+ * the array.
1347
+ */
1348
+
1349
+ function guardedSplice(byteArray, ...args) {
1350
+ var _args$;
1351
+
1352
+ const [start] = args;
1353
+
1354
+ if (args.length === 2 // Implies that `deleteCount` was supplied
1355
+ ? start + ((_args$ = args[1]) !== null && _args$ !== void 0 ? _args$ : 0) > byteArray.length : start >= byteArray.length) {
1356
+ throw new Error(END_OF_BUFFER_ERROR_MESSAGE);
1357
+ }
1358
+
1359
+ return byteArray.splice(...args);
1360
+ }
1361
+
1332
1362
  /**
1333
1363
  * The message header, identifying signed and read-only account
1334
1364
  */
@@ -1413,32 +1443,28 @@ class Message {
1413
1443
  static from(buffer) {
1414
1444
  // Slice up wire data
1415
1445
  let byteArray = [...buffer];
1416
- const numRequiredSignatures = byteArray.shift();
1417
- const numReadonlySignedAccounts = byteArray.shift();
1418
- const numReadonlyUnsignedAccounts = byteArray.shift();
1446
+ const numRequiredSignatures = guardedShift(byteArray);
1447
+ const numReadonlySignedAccounts = guardedShift(byteArray);
1448
+ const numReadonlyUnsignedAccounts = guardedShift(byteArray);
1419
1449
  const accountCount = decodeLength(byteArray);
1420
1450
  let accountKeys = [];
1421
1451
 
1422
1452
  for (let i = 0; i < accountCount; i++) {
1423
- const account = byteArray.slice(0, PUBKEY_LENGTH);
1424
- byteArray = byteArray.slice(PUBKEY_LENGTH);
1453
+ const account = guardedSplice(byteArray, 0, PUBKEY_LENGTH);
1425
1454
  accountKeys.push(bs58.encode(Buffer.from(account)));
1426
1455
  }
1427
1456
 
1428
- const recentBlockhash = byteArray.slice(0, PUBKEY_LENGTH);
1429
- byteArray = byteArray.slice(PUBKEY_LENGTH);
1457
+ const recentBlockhash = guardedSplice(byteArray, 0, PUBKEY_LENGTH);
1430
1458
  const instructionCount = decodeLength(byteArray);
1431
1459
  let instructions = [];
1432
1460
 
1433
1461
  for (let i = 0; i < instructionCount; i++) {
1434
- const programIdIndex = byteArray.shift();
1462
+ const programIdIndex = guardedShift(byteArray);
1435
1463
  const accountCount = decodeLength(byteArray);
1436
- const accounts = byteArray.slice(0, accountCount);
1437
- byteArray = byteArray.slice(accountCount);
1464
+ const accounts = guardedSplice(byteArray, 0, accountCount);
1438
1465
  const dataLength = decodeLength(byteArray);
1439
- const dataSlice = byteArray.slice(0, dataLength);
1466
+ const dataSlice = guardedSplice(byteArray, 0, dataLength);
1440
1467
  const data = bs58.encode(Buffer.from(dataSlice));
1441
- byteArray = byteArray.slice(dataLength);
1442
1468
  instructions.push({
1443
1469
  programIdIndex,
1444
1470
  accounts,
@@ -1461,6 +1487,10 @@ class Message {
1461
1487
 
1462
1488
  }
1463
1489
 
1490
+ /**
1491
+ * Transaction signature as base-58 encoded string
1492
+ */
1493
+
1464
1494
  /**
1465
1495
  * Default (empty) signature
1466
1496
  *
@@ -2054,8 +2084,7 @@ class Transaction {
2054
2084
  let signatures = [];
2055
2085
 
2056
2086
  for (let i = 0; i < signatureCount; i++) {
2057
- const signature = byteArray.slice(0, SIGNATURE_LENGTH);
2058
- byteArray = byteArray.slice(SIGNATURE_LENGTH);
2087
+ const signature = guardedSplice(byteArray, 0, SIGNATURE_LENGTH);
2059
2088
  signatures.push(bs58.encode(Buffer.from(signature)));
2060
2089
  }
2061
2090
 
@@ -8390,10 +8419,8 @@ class ValidatorInfo {
8390
8419
  const configKeys = [];
8391
8420
 
8392
8421
  for (let i = 0; i < 2; i++) {
8393
- const publicKey = new PublicKey(byteArray.slice(0, PUBKEY_LENGTH));
8394
- byteArray = byteArray.slice(PUBKEY_LENGTH);
8395
- const isSigner = byteArray.slice(0, 1)[0] === 1;
8396
- byteArray = byteArray.slice(1);
8422
+ const publicKey = new PublicKey(guardedSplice(byteArray, 0, PUBKEY_LENGTH));
8423
+ const isSigner = guardedShift(byteArray) === 1;
8397
8424
  configKeys.push({
8398
8425
  publicKey,
8399
8426
  isSigner