@solana/web3.js 1.9.1 → 1.9.2

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
@@ -374,6 +374,36 @@ function encodeLength(bytes, len) {
374
374
  }
375
375
  }
376
376
 
377
+ const END_OF_BUFFER_ERROR_MESSAGE = 'Reached end of buffer unexpectedly';
378
+ /**
379
+ * Delegates to `Array#shift`, but throws if the array is zero-length.
380
+ */
381
+
382
+ function guardedShift(byteArray) {
383
+ if (byteArray.length === 0) {
384
+ throw new Error(END_OF_BUFFER_ERROR_MESSAGE);
385
+ }
386
+
387
+ return byteArray.shift();
388
+ }
389
+ /**
390
+ * Delegates to `Array#splice`, but throws if the section being spliced out extends past the end of
391
+ * the array.
392
+ */
393
+
394
+ function guardedSplice(byteArray, ...args) {
395
+ var _args$;
396
+
397
+ const [start] = args;
398
+
399
+ if (args.length === 2 // Implies that `deleteCount` was supplied
400
+ ? start + ((_args$ = args[1]) !== null && _args$ !== void 0 ? _args$ : 0) > byteArray.length : start >= byteArray.length) {
401
+ throw new Error(END_OF_BUFFER_ERROR_MESSAGE);
402
+ }
403
+
404
+ return byteArray.splice(...args);
405
+ }
406
+
377
407
  /**
378
408
  * The message header, identifying signed and read-only account
379
409
  */
@@ -450,32 +480,28 @@ class Message {
450
480
  static from(buffer) {
451
481
  // Slice up wire data
452
482
  let byteArray = [...buffer];
453
- const numRequiredSignatures = byteArray.shift();
454
- const numReadonlySignedAccounts = byteArray.shift();
455
- const numReadonlyUnsignedAccounts = byteArray.shift();
483
+ const numRequiredSignatures = guardedShift(byteArray);
484
+ const numReadonlySignedAccounts = guardedShift(byteArray);
485
+ const numReadonlyUnsignedAccounts = guardedShift(byteArray);
456
486
  const accountCount = decodeLength(byteArray);
457
487
  let accountKeys = [];
458
488
 
459
489
  for (let i = 0; i < accountCount; i++) {
460
- const account = byteArray.slice(0, PUBKEY_LENGTH);
461
- byteArray = byteArray.slice(PUBKEY_LENGTH);
490
+ const account = guardedSplice(byteArray, 0, PUBKEY_LENGTH);
462
491
  accountKeys.push(bs58.encode(Buffer.from(account)));
463
492
  }
464
493
 
465
- const recentBlockhash = byteArray.slice(0, PUBKEY_LENGTH);
466
- byteArray = byteArray.slice(PUBKEY_LENGTH);
494
+ const recentBlockhash = guardedSplice(byteArray, 0, PUBKEY_LENGTH);
467
495
  const instructionCount = decodeLength(byteArray);
468
496
  let instructions = [];
469
497
 
470
498
  for (let i = 0; i < instructionCount; i++) {
471
- const programIdIndex = byteArray.shift();
499
+ const programIdIndex = guardedShift(byteArray);
472
500
  const accountCount = decodeLength(byteArray);
473
- const accounts = byteArray.slice(0, accountCount);
474
- byteArray = byteArray.slice(accountCount);
501
+ const accounts = guardedSplice(byteArray, 0, accountCount);
475
502
  const dataLength = decodeLength(byteArray);
476
- const dataSlice = byteArray.slice(0, dataLength);
503
+ const dataSlice = guardedSplice(byteArray, 0, dataLength);
477
504
  const data = bs58.encode(Buffer.from(dataSlice));
478
- byteArray = byteArray.slice(dataLength);
479
505
  instructions.push({
480
506
  programIdIndex,
481
507
  accounts,
@@ -1085,8 +1111,7 @@ class Transaction {
1085
1111
  let signatures = [];
1086
1112
 
1087
1113
  for (let i = 0; i < signatureCount; i++) {
1088
- const signature = byteArray.slice(0, SIGNATURE_LENGTH);
1089
- byteArray = byteArray.slice(SIGNATURE_LENGTH);
1114
+ const signature = guardedSplice(byteArray, 0, SIGNATURE_LENGTH);
1090
1115
  signatures.push(bs58.encode(Buffer.from(signature)));
1091
1116
  }
1092
1117
 
@@ -6035,10 +6060,8 @@ class ValidatorInfo {
6035
6060
  const configKeys = [];
6036
6061
 
6037
6062
  for (let i = 0; i < 2; i++) {
6038
- const publicKey = new PublicKey(byteArray.slice(0, PUBKEY_LENGTH));
6039
- byteArray = byteArray.slice(PUBKEY_LENGTH);
6040
- const isSigner = byteArray.slice(0, 1)[0] === 1;
6041
- byteArray = byteArray.slice(1);
6063
+ const publicKey = new PublicKey(guardedSplice(byteArray, 0, PUBKEY_LENGTH));
6064
+ const isSigner = guardedShift(byteArray) === 1;
6042
6065
  configKeys.push({
6043
6066
  publicKey,
6044
6067
  isSigner