@solana/web3.js 1.11.0 → 1.11.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
@@ -380,6 +380,36 @@ function encodeLength(bytes, len) {
380
380
  }
381
381
  }
382
382
 
383
+ const END_OF_BUFFER_ERROR_MESSAGE = 'Reached end of buffer unexpectedly';
384
+ /**
385
+ * Delegates to `Array#shift`, but throws if the array is zero-length.
386
+ */
387
+
388
+ function guardedShift(byteArray) {
389
+ if (byteArray.length === 0) {
390
+ throw new Error(END_OF_BUFFER_ERROR_MESSAGE);
391
+ }
392
+
393
+ return byteArray.shift();
394
+ }
395
+ /**
396
+ * Delegates to `Array#splice`, but throws if the section being spliced out extends past the end of
397
+ * the array.
398
+ */
399
+
400
+ function guardedSplice(byteArray, ...args) {
401
+ var _args$;
402
+
403
+ const [start] = args;
404
+
405
+ if (args.length === 2 // Implies that `deleteCount` was supplied
406
+ ? start + ((_args$ = args[1]) !== null && _args$ !== void 0 ? _args$ : 0) > byteArray.length : start >= byteArray.length) {
407
+ throw new Error(END_OF_BUFFER_ERROR_MESSAGE);
408
+ }
409
+
410
+ return byteArray.splice(...args);
411
+ }
412
+
383
413
  /**
384
414
  * The message header, identifying signed and read-only account
385
415
  */
@@ -456,32 +486,28 @@ class Message {
456
486
  static from(buffer) {
457
487
  // Slice up wire data
458
488
  let byteArray = [...buffer];
459
- const numRequiredSignatures = byteArray.shift();
460
- const numReadonlySignedAccounts = byteArray.shift();
461
- const numReadonlyUnsignedAccounts = byteArray.shift();
489
+ const numRequiredSignatures = guardedShift(byteArray);
490
+ const numReadonlySignedAccounts = guardedShift(byteArray);
491
+ const numReadonlyUnsignedAccounts = guardedShift(byteArray);
462
492
  const accountCount = decodeLength(byteArray);
463
493
  let accountKeys = [];
464
494
 
465
495
  for (let i = 0; i < accountCount; i++) {
466
- const account = byteArray.slice(0, PUBKEY_LENGTH);
467
- byteArray = byteArray.slice(PUBKEY_LENGTH);
496
+ const account = guardedSplice(byteArray, 0, PUBKEY_LENGTH);
468
497
  accountKeys.push(bs58.encode(Buffer.from(account)));
469
498
  }
470
499
 
471
- const recentBlockhash = byteArray.slice(0, PUBKEY_LENGTH);
472
- byteArray = byteArray.slice(PUBKEY_LENGTH);
500
+ const recentBlockhash = guardedSplice(byteArray, 0, PUBKEY_LENGTH);
473
501
  const instructionCount = decodeLength(byteArray);
474
502
  let instructions = [];
475
503
 
476
504
  for (let i = 0; i < instructionCount; i++) {
477
- const programIdIndex = byteArray.shift();
505
+ const programIdIndex = guardedShift(byteArray);
478
506
  const accountCount = decodeLength(byteArray);
479
- const accounts = byteArray.slice(0, accountCount);
480
- byteArray = byteArray.slice(accountCount);
507
+ const accounts = guardedSplice(byteArray, 0, accountCount);
481
508
  const dataLength = decodeLength(byteArray);
482
- const dataSlice = byteArray.slice(0, dataLength);
509
+ const dataSlice = guardedSplice(byteArray, 0, dataLength);
483
510
  const data = bs58.encode(Buffer.from(dataSlice));
484
- byteArray = byteArray.slice(dataLength);
485
511
  instructions.push({
486
512
  programIdIndex,
487
513
  accounts,
@@ -504,6 +530,10 @@ class Message {
504
530
 
505
531
  }
506
532
 
533
+ /**
534
+ * Transaction signature as base-58 encoded string
535
+ */
536
+
507
537
  /**
508
538
  * Default (empty) signature
509
539
  *
@@ -1087,8 +1117,7 @@ class Transaction {
1087
1117
  let signatures = [];
1088
1118
 
1089
1119
  for (let i = 0; i < signatureCount; i++) {
1090
- const signature = byteArray.slice(0, SIGNATURE_LENGTH);
1091
- byteArray = byteArray.slice(SIGNATURE_LENGTH);
1120
+ const signature = guardedSplice(byteArray, 0, SIGNATURE_LENGTH);
1092
1121
  signatures.push(bs58.encode(Buffer.from(signature)));
1093
1122
  }
1094
1123
 
@@ -6121,10 +6150,8 @@ class ValidatorInfo {
6121
6150
  const configKeys = [];
6122
6151
 
6123
6152
  for (let i = 0; i < 2; i++) {
6124
- const publicKey = new PublicKey(byteArray.slice(0, PUBKEY_LENGTH));
6125
- byteArray = byteArray.slice(PUBKEY_LENGTH);
6126
- const isSigner = byteArray.slice(0, 1)[0] === 1;
6127
- byteArray = byteArray.slice(1);
6153
+ const publicKey = new PublicKey(guardedSplice(byteArray, 0, PUBKEY_LENGTH));
6154
+ const isSigner = guardedShift(byteArray) === 1;
6128
6155
  configKeys.push({
6129
6156
  publicKey,
6130
6157
  isSigner