@solana/web3.js 1.6.0 → 1.6.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.cjs.js CHANGED
@@ -403,6 +403,36 @@ function encodeLength(bytes, len) {
403
403
  }
404
404
  }
405
405
 
406
+ const END_OF_BUFFER_ERROR_MESSAGE = 'Reached end of buffer unexpectedly';
407
+ /**
408
+ * Delegates to `Array#shift`, but throws if the array is zero-length.
409
+ */
410
+
411
+ function guardedShift(byteArray) {
412
+ if (byteArray.length === 0) {
413
+ throw new Error(END_OF_BUFFER_ERROR_MESSAGE);
414
+ }
415
+
416
+ return byteArray.shift();
417
+ }
418
+ /**
419
+ * Delegates to `Array#splice`, but throws if the section being spliced out extends past the end of
420
+ * the array.
421
+ */
422
+
423
+ function guardedSplice(byteArray, ...args) {
424
+ var _args$;
425
+
426
+ const [start] = args;
427
+
428
+ if (args.length === 2 // Implies that `deleteCount` was supplied
429
+ ? start + ((_args$ = args[1]) !== null && _args$ !== void 0 ? _args$ : 0) > byteArray.length : start >= byteArray.length) {
430
+ throw new Error(END_OF_BUFFER_ERROR_MESSAGE);
431
+ }
432
+
433
+ return byteArray.splice(...args);
434
+ }
435
+
406
436
  /**
407
437
  * The message header, identifying signed and read-only account
408
438
  */
@@ -487,32 +517,28 @@ class Message {
487
517
  static from(buffer$1) {
488
518
  // Slice up wire data
489
519
  let byteArray = [...buffer$1];
490
- const numRequiredSignatures = byteArray.shift();
491
- const numReadonlySignedAccounts = byteArray.shift();
492
- const numReadonlyUnsignedAccounts = byteArray.shift();
520
+ const numRequiredSignatures = guardedShift(byteArray);
521
+ const numReadonlySignedAccounts = guardedShift(byteArray);
522
+ const numReadonlyUnsignedAccounts = guardedShift(byteArray);
493
523
  const accountCount = decodeLength(byteArray);
494
524
  let accountKeys = [];
495
525
 
496
526
  for (let i = 0; i < accountCount; i++) {
497
- const account = byteArray.slice(0, PUBKEY_LENGTH);
498
- byteArray = byteArray.slice(PUBKEY_LENGTH);
527
+ const account = guardedSplice(byteArray, 0, PUBKEY_LENGTH);
499
528
  accountKeys.push(bs58__default['default'].encode(buffer.Buffer.from(account)));
500
529
  }
501
530
 
502
- const recentBlockhash = byteArray.slice(0, PUBKEY_LENGTH);
503
- byteArray = byteArray.slice(PUBKEY_LENGTH);
531
+ const recentBlockhash = guardedSplice(byteArray, 0, PUBKEY_LENGTH);
504
532
  const instructionCount = decodeLength(byteArray);
505
533
  let instructions = [];
506
534
 
507
535
  for (let i = 0; i < instructionCount; i++) {
508
- const programIdIndex = byteArray.shift();
536
+ const programIdIndex = guardedShift(byteArray);
509
537
  const accountCount = decodeLength(byteArray);
510
- const accounts = byteArray.slice(0, accountCount);
511
- byteArray = byteArray.slice(accountCount);
538
+ const accounts = guardedSplice(byteArray, 0, accountCount);
512
539
  const dataLength = decodeLength(byteArray);
513
- const dataSlice = byteArray.slice(0, dataLength);
540
+ const dataSlice = guardedSplice(byteArray, 0, dataLength);
514
541
  const data = bs58__default['default'].encode(buffer.Buffer.from(dataSlice));
515
- byteArray = byteArray.slice(dataLength);
516
542
  instructions.push({
517
543
  programIdIndex,
518
544
  accounts,
@@ -1132,8 +1158,7 @@ class Transaction {
1132
1158
  let signatures = [];
1133
1159
 
1134
1160
  for (let i = 0; i < signatureCount; i++) {
1135
- const signature = byteArray.slice(0, SIGNATURE_LENGTH);
1136
- byteArray = byteArray.slice(SIGNATURE_LENGTH);
1161
+ const signature = guardedSplice(byteArray, 0, SIGNATURE_LENGTH);
1137
1162
  signatures.push(bs58__default['default'].encode(buffer.Buffer.from(signature)));
1138
1163
  }
1139
1164
 
@@ -5946,10 +5971,8 @@ class ValidatorInfo {
5946
5971
  const configKeys = [];
5947
5972
 
5948
5973
  for (let i = 0; i < 2; i++) {
5949
- const publicKey = new PublicKey(byteArray.slice(0, PUBKEY_LENGTH));
5950
- byteArray = byteArray.slice(PUBKEY_LENGTH);
5951
- const isSigner = byteArray.slice(0, 1)[0] === 1;
5952
- byteArray = byteArray.slice(1);
5974
+ const publicKey = new PublicKey(guardedSplice(byteArray, 0, PUBKEY_LENGTH));
5975
+ const isSigner = guardedShift(byteArray) === 1;
5953
5976
  configKeys.push({
5954
5977
  publicKey,
5955
5978
  isSigner