@solana/web3.js 1.12.0 → 1.12.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.
@@ -1321,6 +1321,36 @@ function encodeLength(bytes, len) {
1321
1321
  }
1322
1322
  }
1323
1323
 
1324
+ const END_OF_BUFFER_ERROR_MESSAGE = 'Reached end of buffer unexpectedly';
1325
+ /**
1326
+ * Delegates to `Array#shift`, but throws if the array is zero-length.
1327
+ */
1328
+
1329
+ function guardedShift(byteArray) {
1330
+ if (byteArray.length === 0) {
1331
+ throw new Error(END_OF_BUFFER_ERROR_MESSAGE);
1332
+ }
1333
+
1334
+ return byteArray.shift();
1335
+ }
1336
+ /**
1337
+ * Delegates to `Array#splice`, but throws if the section being spliced out extends past the end of
1338
+ * the array.
1339
+ */
1340
+
1341
+ function guardedSplice(byteArray, ...args) {
1342
+ var _args$;
1343
+
1344
+ const [start] = args;
1345
+
1346
+ if (args.length === 2 // Implies that `deleteCount` was supplied
1347
+ ? start + ((_args$ = args[1]) !== null && _args$ !== void 0 ? _args$ : 0) > byteArray.length : start >= byteArray.length) {
1348
+ throw new Error(END_OF_BUFFER_ERROR_MESSAGE);
1349
+ }
1350
+
1351
+ return byteArray.splice(...args);
1352
+ }
1353
+
1324
1354
  /**
1325
1355
  * The message header, identifying signed and read-only account
1326
1356
  */
@@ -1397,32 +1427,28 @@ class Message {
1397
1427
  static from(buffer) {
1398
1428
  // Slice up wire data
1399
1429
  let byteArray = [...buffer];
1400
- const numRequiredSignatures = byteArray.shift();
1401
- const numReadonlySignedAccounts = byteArray.shift();
1402
- const numReadonlyUnsignedAccounts = byteArray.shift();
1430
+ const numRequiredSignatures = guardedShift(byteArray);
1431
+ const numReadonlySignedAccounts = guardedShift(byteArray);
1432
+ const numReadonlyUnsignedAccounts = guardedShift(byteArray);
1403
1433
  const accountCount = decodeLength(byteArray);
1404
1434
  let accountKeys = [];
1405
1435
 
1406
1436
  for (let i = 0; i < accountCount; i++) {
1407
- const account = byteArray.slice(0, PUBKEY_LENGTH);
1408
- byteArray = byteArray.slice(PUBKEY_LENGTH);
1437
+ const account = guardedSplice(byteArray, 0, PUBKEY_LENGTH);
1409
1438
  accountKeys.push(bs58.encode(Buffer.from(account)));
1410
1439
  }
1411
1440
 
1412
- const recentBlockhash = byteArray.slice(0, PUBKEY_LENGTH);
1413
- byteArray = byteArray.slice(PUBKEY_LENGTH);
1441
+ const recentBlockhash = guardedSplice(byteArray, 0, PUBKEY_LENGTH);
1414
1442
  const instructionCount = decodeLength(byteArray);
1415
1443
  let instructions = [];
1416
1444
 
1417
1445
  for (let i = 0; i < instructionCount; i++) {
1418
- const programIdIndex = byteArray.shift();
1446
+ const programIdIndex = guardedShift(byteArray);
1419
1447
  const accountCount = decodeLength(byteArray);
1420
- const accounts = byteArray.slice(0, accountCount);
1421
- byteArray = byteArray.slice(accountCount);
1448
+ const accounts = guardedSplice(byteArray, 0, accountCount);
1422
1449
  const dataLength = decodeLength(byteArray);
1423
- const dataSlice = byteArray.slice(0, dataLength);
1450
+ const dataSlice = guardedSplice(byteArray, 0, dataLength);
1424
1451
  const data = bs58.encode(Buffer.from(dataSlice));
1425
- byteArray = byteArray.slice(dataLength);
1426
1452
  instructions.push({
1427
1453
  programIdIndex,
1428
1454
  accounts,
@@ -1445,6 +1471,10 @@ class Message {
1445
1471
 
1446
1472
  }
1447
1473
 
1474
+ /**
1475
+ * Transaction signature as base-58 encoded string
1476
+ */
1477
+
1448
1478
  /**
1449
1479
  * Default (empty) signature
1450
1480
  *
@@ -2028,8 +2058,7 @@ class Transaction {
2028
2058
  let signatures = [];
2029
2059
 
2030
2060
  for (let i = 0; i < signatureCount; i++) {
2031
- const signature = byteArray.slice(0, SIGNATURE_LENGTH);
2032
- byteArray = byteArray.slice(SIGNATURE_LENGTH);
2061
+ const signature = guardedSplice(byteArray, 0, SIGNATURE_LENGTH);
2033
2062
  signatures.push(bs58.encode(Buffer.from(signature)));
2034
2063
  }
2035
2064
 
@@ -8112,10 +8141,8 @@ class ValidatorInfo {
8112
8141
  const configKeys = [];
8113
8142
 
8114
8143
  for (let i = 0; i < 2; i++) {
8115
- const publicKey = new PublicKey(byteArray.slice(0, PUBKEY_LENGTH));
8116
- byteArray = byteArray.slice(PUBKEY_LENGTH);
8117
- const isSigner = byteArray.slice(0, 1)[0] === 1;
8118
- byteArray = byteArray.slice(1);
8144
+ const publicKey = new PublicKey(guardedSplice(byteArray, 0, PUBKEY_LENGTH));
8145
+ const isSigner = guardedShift(byteArray) === 1;
8119
8146
  configKeys.push({
8120
8147
  publicKey,
8121
8148
  isSigner