@solana/web3.js 1.28.0 → 1.28.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.
package/lib/index.esm.js CHANGED
@@ -442,6 +442,36 @@ function encodeLength(bytes, len) {
442
442
  }
443
443
  }
444
444
 
445
+ const END_OF_BUFFER_ERROR_MESSAGE = 'Reached end of buffer unexpectedly';
446
+ /**
447
+ * Delegates to `Array#shift`, but throws if the array is zero-length.
448
+ */
449
+
450
+ function guardedShift(byteArray) {
451
+ if (byteArray.length === 0) {
452
+ throw new Error(END_OF_BUFFER_ERROR_MESSAGE);
453
+ }
454
+
455
+ return byteArray.shift();
456
+ }
457
+ /**
458
+ * Delegates to `Array#splice`, but throws if the section being spliced out extends past the end of
459
+ * the array.
460
+ */
461
+
462
+ function guardedSplice(byteArray, ...args) {
463
+ var _args$;
464
+
465
+ const [start] = args;
466
+
467
+ if (args.length === 2 // Implies that `deleteCount` was supplied
468
+ ? start + ((_args$ = args[1]) !== null && _args$ !== void 0 ? _args$ : 0) > byteArray.length : start >= byteArray.length) {
469
+ throw new Error(END_OF_BUFFER_ERROR_MESSAGE);
470
+ }
471
+
472
+ return byteArray.splice(...args);
473
+ }
474
+
445
475
  /**
446
476
  * The message header, identifying signed and read-only account
447
477
  */
@@ -545,32 +575,28 @@ class Message {
545
575
  static from(buffer) {
546
576
  // Slice up wire data
547
577
  let byteArray = [...buffer];
548
- const numRequiredSignatures = byteArray.shift();
549
- const numReadonlySignedAccounts = byteArray.shift();
550
- const numReadonlyUnsignedAccounts = byteArray.shift();
578
+ const numRequiredSignatures = guardedShift(byteArray);
579
+ const numReadonlySignedAccounts = guardedShift(byteArray);
580
+ const numReadonlyUnsignedAccounts = guardedShift(byteArray);
551
581
  const accountCount = decodeLength(byteArray);
552
582
  let accountKeys = [];
553
583
 
554
584
  for (let i = 0; i < accountCount; i++) {
555
- const account = byteArray.slice(0, PUBKEY_LENGTH);
556
- byteArray = byteArray.slice(PUBKEY_LENGTH);
585
+ const account = guardedSplice(byteArray, 0, PUBKEY_LENGTH);
557
586
  accountKeys.push(bs58.encode(Buffer.from(account)));
558
587
  }
559
588
 
560
- const recentBlockhash = byteArray.slice(0, PUBKEY_LENGTH);
561
- byteArray = byteArray.slice(PUBKEY_LENGTH);
589
+ const recentBlockhash = guardedSplice(byteArray, 0, PUBKEY_LENGTH);
562
590
  const instructionCount = decodeLength(byteArray);
563
591
  let instructions = [];
564
592
 
565
593
  for (let i = 0; i < instructionCount; i++) {
566
- const programIdIndex = byteArray.shift();
594
+ const programIdIndex = guardedShift(byteArray);
567
595
  const accountCount = decodeLength(byteArray);
568
- const accounts = byteArray.slice(0, accountCount);
569
- byteArray = byteArray.slice(accountCount);
596
+ const accounts = guardedSplice(byteArray, 0, accountCount);
570
597
  const dataLength = decodeLength(byteArray);
571
- const dataSlice = byteArray.slice(0, dataLength);
598
+ const dataSlice = guardedSplice(byteArray, 0, dataLength);
572
599
  const data = bs58.encode(Buffer.from(dataSlice));
573
- byteArray = byteArray.slice(dataLength);
574
600
  instructions.push({
575
601
  programIdIndex,
576
602
  accounts,
@@ -599,6 +625,10 @@ function assert (condition, message) {
599
625
  }
600
626
  }
601
627
 
628
+ /**
629
+ * Transaction signature as base-58 encoded string
630
+ */
631
+
602
632
  /**
603
633
  * Default (empty) signature
604
634
  *
@@ -1192,8 +1222,7 @@ class Transaction {
1192
1222
  let signatures = [];
1193
1223
 
1194
1224
  for (let i = 0; i < signatureCount; i++) {
1195
- const signature = byteArray.slice(0, SIGNATURE_LENGTH);
1196
- byteArray = byteArray.slice(SIGNATURE_LENGTH);
1225
+ const signature = guardedSplice(byteArray, 0, SIGNATURE_LENGTH);
1197
1226
  signatures.push(bs58.encode(Buffer.from(signature)));
1198
1227
  }
1199
1228
 
@@ -6727,10 +6756,8 @@ class ValidatorInfo {
6727
6756
  const configKeys = [];
6728
6757
 
6729
6758
  for (let i = 0; i < 2; i++) {
6730
- const publicKey = new PublicKey(byteArray.slice(0, PUBKEY_LENGTH));
6731
- byteArray = byteArray.slice(PUBKEY_LENGTH);
6732
- const isSigner = byteArray.slice(0, 1)[0] === 1;
6733
- byteArray = byteArray.slice(1);
6759
+ const publicKey = new PublicKey(guardedSplice(byteArray, 0, PUBKEY_LENGTH));
6760
+ const isSigner = guardedShift(byteArray) === 1;
6734
6761
  configKeys.push({
6735
6762
  publicKey,
6736
6763
  isSigner