@solana/web3.js 1.27.0 → 1.27.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
  */
@@ -526,32 +556,28 @@ class Message {
526
556
  static from(buffer) {
527
557
  // Slice up wire data
528
558
  let byteArray = [...buffer];
529
- const numRequiredSignatures = byteArray.shift();
530
- const numReadonlySignedAccounts = byteArray.shift();
531
- const numReadonlyUnsignedAccounts = byteArray.shift();
559
+ const numRequiredSignatures = guardedShift(byteArray);
560
+ const numReadonlySignedAccounts = guardedShift(byteArray);
561
+ const numReadonlyUnsignedAccounts = guardedShift(byteArray);
532
562
  const accountCount = decodeLength(byteArray);
533
563
  let accountKeys = [];
534
564
 
535
565
  for (let i = 0; i < accountCount; i++) {
536
- const account = byteArray.slice(0, PUBKEY_LENGTH);
537
- byteArray = byteArray.slice(PUBKEY_LENGTH);
566
+ const account = guardedSplice(byteArray, 0, PUBKEY_LENGTH);
538
567
  accountKeys.push(bs58.encode(Buffer.from(account)));
539
568
  }
540
569
 
541
- const recentBlockhash = byteArray.slice(0, PUBKEY_LENGTH);
542
- byteArray = byteArray.slice(PUBKEY_LENGTH);
570
+ const recentBlockhash = guardedSplice(byteArray, 0, PUBKEY_LENGTH);
543
571
  const instructionCount = decodeLength(byteArray);
544
572
  let instructions = [];
545
573
 
546
574
  for (let i = 0; i < instructionCount; i++) {
547
- const programIdIndex = byteArray.shift();
575
+ const programIdIndex = guardedShift(byteArray);
548
576
  const accountCount = decodeLength(byteArray);
549
- const accounts = byteArray.slice(0, accountCount);
550
- byteArray = byteArray.slice(accountCount);
577
+ const accounts = guardedSplice(byteArray, 0, accountCount);
551
578
  const dataLength = decodeLength(byteArray);
552
- const dataSlice = byteArray.slice(0, dataLength);
579
+ const dataSlice = guardedSplice(byteArray, 0, dataLength);
553
580
  const data = bs58.encode(Buffer.from(dataSlice));
554
- byteArray = byteArray.slice(dataLength);
555
581
  instructions.push({
556
582
  programIdIndex,
557
583
  accounts,
@@ -580,6 +606,10 @@ function assert (condition, message) {
580
606
  }
581
607
  }
582
608
 
609
+ /**
610
+ * Transaction signature as base-58 encoded string
611
+ */
612
+
583
613
  /**
584
614
  * Default (empty) signature
585
615
  *
@@ -1173,8 +1203,7 @@ class Transaction {
1173
1203
  let signatures = [];
1174
1204
 
1175
1205
  for (let i = 0; i < signatureCount; i++) {
1176
- const signature = byteArray.slice(0, SIGNATURE_LENGTH);
1177
- byteArray = byteArray.slice(SIGNATURE_LENGTH);
1206
+ const signature = guardedSplice(byteArray, 0, SIGNATURE_LENGTH);
1178
1207
  signatures.push(bs58.encode(Buffer.from(signature)));
1179
1208
  }
1180
1209
 
@@ -6677,10 +6706,8 @@ class ValidatorInfo {
6677
6706
  const configKeys = [];
6678
6707
 
6679
6708
  for (let i = 0; i < 2; i++) {
6680
- const publicKey = new PublicKey(byteArray.slice(0, PUBKEY_LENGTH));
6681
- byteArray = byteArray.slice(PUBKEY_LENGTH);
6682
- const isSigner = byteArray.slice(0, 1)[0] === 1;
6683
- byteArray = byteArray.slice(1);
6709
+ const publicKey = new PublicKey(guardedSplice(byteArray, 0, PUBKEY_LENGTH));
6710
+ const isSigner = guardedShift(byteArray) === 1;
6684
6711
  configKeys.push({
6685
6712
  publicKey,
6686
6713
  isSigner