@solana/web3.js 1.18.0 → 1.18.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
@@ -439,6 +439,36 @@ function encodeLength(bytes, len) {
439
439
  }
440
440
  }
441
441
 
442
+ const END_OF_BUFFER_ERROR_MESSAGE = 'Reached end of buffer unexpectedly';
443
+ /**
444
+ * Delegates to `Array#shift`, but throws if the array is zero-length.
445
+ */
446
+
447
+ function guardedShift(byteArray) {
448
+ if (byteArray.length === 0) {
449
+ throw new Error(END_OF_BUFFER_ERROR_MESSAGE);
450
+ }
451
+
452
+ return byteArray.shift();
453
+ }
454
+ /**
455
+ * Delegates to `Array#splice`, but throws if the section being spliced out extends past the end of
456
+ * the array.
457
+ */
458
+
459
+ function guardedSplice(byteArray, ...args) {
460
+ var _args$;
461
+
462
+ const [start] = args;
463
+
464
+ if (args.length === 2 // Implies that `deleteCount` was supplied
465
+ ? start + ((_args$ = args[1]) !== null && _args$ !== void 0 ? _args$ : 0) > byteArray.length : start >= byteArray.length) {
466
+ throw new Error(END_OF_BUFFER_ERROR_MESSAGE);
467
+ }
468
+
469
+ return byteArray.splice(...args);
470
+ }
471
+
442
472
  /**
443
473
  * The message header, identifying signed and read-only account
444
474
  */
@@ -523,32 +553,28 @@ class Message {
523
553
  static from(buffer) {
524
554
  // Slice up wire data
525
555
  let byteArray = [...buffer];
526
- const numRequiredSignatures = byteArray.shift();
527
- const numReadonlySignedAccounts = byteArray.shift();
528
- const numReadonlyUnsignedAccounts = byteArray.shift();
556
+ const numRequiredSignatures = guardedShift(byteArray);
557
+ const numReadonlySignedAccounts = guardedShift(byteArray);
558
+ const numReadonlyUnsignedAccounts = guardedShift(byteArray);
529
559
  const accountCount = decodeLength(byteArray);
530
560
  let accountKeys = [];
531
561
 
532
562
  for (let i = 0; i < accountCount; i++) {
533
- const account = byteArray.slice(0, PUBKEY_LENGTH);
534
- byteArray = byteArray.slice(PUBKEY_LENGTH);
563
+ const account = guardedSplice(byteArray, 0, PUBKEY_LENGTH);
535
564
  accountKeys.push(bs58.encode(Buffer.from(account)));
536
565
  }
537
566
 
538
- const recentBlockhash = byteArray.slice(0, PUBKEY_LENGTH);
539
- byteArray = byteArray.slice(PUBKEY_LENGTH);
567
+ const recentBlockhash = guardedSplice(byteArray, 0, PUBKEY_LENGTH);
540
568
  const instructionCount = decodeLength(byteArray);
541
569
  let instructions = [];
542
570
 
543
571
  for (let i = 0; i < instructionCount; i++) {
544
- const programIdIndex = byteArray.shift();
572
+ const programIdIndex = guardedShift(byteArray);
545
573
  const accountCount = decodeLength(byteArray);
546
- const accounts = byteArray.slice(0, accountCount);
547
- byteArray = byteArray.slice(accountCount);
574
+ const accounts = guardedSplice(byteArray, 0, accountCount);
548
575
  const dataLength = decodeLength(byteArray);
549
- const dataSlice = byteArray.slice(0, dataLength);
576
+ const dataSlice = guardedSplice(byteArray, 0, dataLength);
550
577
  const data = bs58.encode(Buffer.from(dataSlice));
551
- byteArray = byteArray.slice(dataLength);
552
578
  instructions.push({
553
579
  programIdIndex,
554
580
  accounts,
@@ -571,6 +597,10 @@ class Message {
571
597
 
572
598
  }
573
599
 
600
+ /**
601
+ * Transaction signature as base-58 encoded string
602
+ */
603
+
574
604
  /**
575
605
  * Default (empty) signature
576
606
  *
@@ -1164,8 +1194,7 @@ class Transaction {
1164
1194
  let signatures = [];
1165
1195
 
1166
1196
  for (let i = 0; i < signatureCount; i++) {
1167
- const signature = byteArray.slice(0, SIGNATURE_LENGTH);
1168
- byteArray = byteArray.slice(SIGNATURE_LENGTH);
1197
+ const signature = guardedSplice(byteArray, 0, SIGNATURE_LENGTH);
1169
1198
  signatures.push(bs58.encode(Buffer.from(signature)));
1170
1199
  }
1171
1200
 
@@ -6421,10 +6450,8 @@ class ValidatorInfo {
6421
6450
  const configKeys = [];
6422
6451
 
6423
6452
  for (let i = 0; i < 2; i++) {
6424
- const publicKey = new PublicKey(byteArray.slice(0, PUBKEY_LENGTH));
6425
- byteArray = byteArray.slice(PUBKEY_LENGTH);
6426
- const isSigner = byteArray.slice(0, 1)[0] === 1;
6427
- byteArray = byteArray.slice(1);
6453
+ const publicKey = new PublicKey(guardedSplice(byteArray, 0, PUBKEY_LENGTH));
6454
+ const isSigner = guardedShift(byteArray) === 1;
6428
6455
  configKeys.push({
6429
6456
  publicKey,
6430
6457
  isSigner