@solana/web3.js 1.13.0 → 1.13.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
@@ -431,6 +431,36 @@ function encodeLength(bytes, len) {
431
431
  }
432
432
  }
433
433
 
434
+ const END_OF_BUFFER_ERROR_MESSAGE = 'Reached end of buffer unexpectedly';
435
+ /**
436
+ * Delegates to `Array#shift`, but throws if the array is zero-length.
437
+ */
438
+
439
+ function guardedShift(byteArray) {
440
+ if (byteArray.length === 0) {
441
+ throw new Error(END_OF_BUFFER_ERROR_MESSAGE);
442
+ }
443
+
444
+ return byteArray.shift();
445
+ }
446
+ /**
447
+ * Delegates to `Array#splice`, but throws if the section being spliced out extends past the end of
448
+ * the array.
449
+ */
450
+
451
+ function guardedSplice(byteArray, ...args) {
452
+ var _args$;
453
+
454
+ const [start] = args;
455
+
456
+ if (args.length === 2 // Implies that `deleteCount` was supplied
457
+ ? start + ((_args$ = args[1]) !== null && _args$ !== void 0 ? _args$ : 0) > byteArray.length : start >= byteArray.length) {
458
+ throw new Error(END_OF_BUFFER_ERROR_MESSAGE);
459
+ }
460
+
461
+ return byteArray.splice(...args);
462
+ }
463
+
434
464
  /**
435
465
  * The message header, identifying signed and read-only account
436
466
  */
@@ -507,32 +537,28 @@ class Message {
507
537
  static from(buffer) {
508
538
  // Slice up wire data
509
539
  let byteArray = [...buffer];
510
- const numRequiredSignatures = byteArray.shift();
511
- const numReadonlySignedAccounts = byteArray.shift();
512
- const numReadonlyUnsignedAccounts = byteArray.shift();
540
+ const numRequiredSignatures = guardedShift(byteArray);
541
+ const numReadonlySignedAccounts = guardedShift(byteArray);
542
+ const numReadonlyUnsignedAccounts = guardedShift(byteArray);
513
543
  const accountCount = decodeLength(byteArray);
514
544
  let accountKeys = [];
515
545
 
516
546
  for (let i = 0; i < accountCount; i++) {
517
- const account = byteArray.slice(0, PUBKEY_LENGTH);
518
- byteArray = byteArray.slice(PUBKEY_LENGTH);
547
+ const account = guardedSplice(byteArray, 0, PUBKEY_LENGTH);
519
548
  accountKeys.push(bs58.encode(Buffer.from(account)));
520
549
  }
521
550
 
522
- const recentBlockhash = byteArray.slice(0, PUBKEY_LENGTH);
523
- byteArray = byteArray.slice(PUBKEY_LENGTH);
551
+ const recentBlockhash = guardedSplice(byteArray, 0, PUBKEY_LENGTH);
524
552
  const instructionCount = decodeLength(byteArray);
525
553
  let instructions = [];
526
554
 
527
555
  for (let i = 0; i < instructionCount; i++) {
528
- const programIdIndex = byteArray.shift();
556
+ const programIdIndex = guardedShift(byteArray);
529
557
  const accountCount = decodeLength(byteArray);
530
- const accounts = byteArray.slice(0, accountCount);
531
- byteArray = byteArray.slice(accountCount);
558
+ const accounts = guardedSplice(byteArray, 0, accountCount);
532
559
  const dataLength = decodeLength(byteArray);
533
- const dataSlice = byteArray.slice(0, dataLength);
560
+ const dataSlice = guardedSplice(byteArray, 0, dataLength);
534
561
  const data = bs58.encode(Buffer.from(dataSlice));
535
- byteArray = byteArray.slice(dataLength);
536
562
  instructions.push({
537
563
  programIdIndex,
538
564
  accounts,
@@ -555,6 +581,10 @@ class Message {
555
581
 
556
582
  }
557
583
 
584
+ /**
585
+ * Transaction signature as base-58 encoded string
586
+ */
587
+
558
588
  /**
559
589
  * Default (empty) signature
560
590
  *
@@ -1138,8 +1168,7 @@ class Transaction {
1138
1168
  let signatures = [];
1139
1169
 
1140
1170
  for (let i = 0; i < signatureCount; i++) {
1141
- const signature = byteArray.slice(0, SIGNATURE_LENGTH);
1142
- byteArray = byteArray.slice(SIGNATURE_LENGTH);
1171
+ const signature = guardedSplice(byteArray, 0, SIGNATURE_LENGTH);
1143
1172
  signatures.push(bs58.encode(Buffer.from(signature)));
1144
1173
  }
1145
1174
 
@@ -6175,10 +6204,8 @@ class ValidatorInfo {
6175
6204
  const configKeys = [];
6176
6205
 
6177
6206
  for (let i = 0; i < 2; i++) {
6178
- const publicKey = new PublicKey(byteArray.slice(0, PUBKEY_LENGTH));
6179
- byteArray = byteArray.slice(PUBKEY_LENGTH);
6180
- const isSigner = byteArray.slice(0, 1)[0] === 1;
6181
- byteArray = byteArray.slice(1);
6207
+ const publicKey = new PublicKey(guardedSplice(byteArray, 0, PUBKEY_LENGTH));
6208
+ const isSigner = guardedShift(byteArray) === 1;
6182
6209
  configKeys.push({
6183
6210
  publicKey,
6184
6211
  isSigner