@solana/web3.js 1.0.0 → 1.0.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
@@ -395,6 +395,36 @@ function encodeLength(bytes, len) {
395
395
  }
396
396
  }
397
397
 
398
+ const END_OF_BUFFER_ERROR_MESSAGE = 'Reached end of buffer unexpectedly';
399
+ /**
400
+ * Delegates to `Array#shift`, but throws if the array is zero-length.
401
+ */
402
+
403
+ function guardedShift(byteArray) {
404
+ if (byteArray.length === 0) {
405
+ throw new Error(END_OF_BUFFER_ERROR_MESSAGE);
406
+ }
407
+
408
+ return byteArray.shift();
409
+ }
410
+ /**
411
+ * Delegates to `Array#splice`, but throws if the section being spliced out extends past the end of
412
+ * the array.
413
+ */
414
+
415
+ function guardedSplice(byteArray, ...args) {
416
+ var _args$;
417
+
418
+ const [start] = args;
419
+
420
+ if (args.length === 2 // Implies that `deleteCount` was supplied
421
+ ? start + ((_args$ = args[1]) !== null && _args$ !== void 0 ? _args$ : 0) > byteArray.length : start >= byteArray.length) {
422
+ throw new Error(END_OF_BUFFER_ERROR_MESSAGE);
423
+ }
424
+
425
+ return byteArray.splice(...args);
426
+ }
427
+
398
428
  /**
399
429
  * The message header, identifying signed and read-only account
400
430
  *
@@ -485,32 +515,28 @@ class Message {
485
515
  static from(buffer$1) {
486
516
  // Slice up wire data
487
517
  let byteArray = [...buffer$1];
488
- const numRequiredSignatures = byteArray.shift();
489
- const numReadonlySignedAccounts = byteArray.shift();
490
- const numReadonlyUnsignedAccounts = byteArray.shift();
518
+ const numRequiredSignatures = guardedShift(byteArray);
519
+ const numReadonlySignedAccounts = guardedShift(byteArray);
520
+ const numReadonlyUnsignedAccounts = guardedShift(byteArray);
491
521
  const accountCount = decodeLength(byteArray);
492
522
  let accountKeys = [];
493
523
 
494
524
  for (let i = 0; i < accountCount; i++) {
495
- const account = byteArray.slice(0, PUBKEY_LENGTH);
496
- byteArray = byteArray.slice(PUBKEY_LENGTH);
525
+ const account = guardedSplice(byteArray, 0, PUBKEY_LENGTH);
497
526
  accountKeys.push(bs58__default['default'].encode(buffer.Buffer.from(account)));
498
527
  }
499
528
 
500
- const recentBlockhash = byteArray.slice(0, PUBKEY_LENGTH);
501
- byteArray = byteArray.slice(PUBKEY_LENGTH);
529
+ const recentBlockhash = guardedSplice(byteArray, 0, PUBKEY_LENGTH);
502
530
  const instructionCount = decodeLength(byteArray);
503
531
  let instructions = [];
504
532
 
505
533
  for (let i = 0; i < instructionCount; i++) {
506
- const programIdIndex = byteArray.shift();
534
+ const programIdIndex = guardedShift(byteArray);
507
535
  const accountCount = decodeLength(byteArray);
508
- const accounts = byteArray.slice(0, accountCount);
509
- byteArray = byteArray.slice(accountCount);
536
+ const accounts = guardedSplice(byteArray, 0, accountCount);
510
537
  const dataLength = decodeLength(byteArray);
511
- const dataSlice = byteArray.slice(0, dataLength);
538
+ const dataSlice = guardedSplice(byteArray, 0, dataLength);
512
539
  const data = bs58__default['default'].encode(buffer.Buffer.from(dataSlice));
513
- byteArray = byteArray.slice(dataLength);
514
540
  instructions.push({
515
541
  programIdIndex,
516
542
  accounts,
@@ -1135,8 +1161,7 @@ class Transaction {
1135
1161
  let signatures = [];
1136
1162
 
1137
1163
  for (let i = 0; i < signatureCount; i++) {
1138
- const signature = byteArray.slice(0, SIGNATURE_LENGTH);
1139
- byteArray = byteArray.slice(SIGNATURE_LENGTH);
1164
+ const signature = guardedSplice(byteArray, 0, SIGNATURE_LENGTH);
1140
1165
  signatures.push(bs58__default['default'].encode(buffer.Buffer.from(signature)));
1141
1166
  }
1142
1167
 
@@ -5703,10 +5728,8 @@ class ValidatorInfo {
5703
5728
  const configKeys = [];
5704
5729
 
5705
5730
  for (let i = 0; i < 2; i++) {
5706
- const publicKey = new PublicKey(byteArray.slice(0, PUBKEY_LENGTH));
5707
- byteArray = byteArray.slice(PUBKEY_LENGTH);
5708
- const isSigner = byteArray.slice(0, 1)[0] === 1;
5709
- byteArray = byteArray.slice(1);
5731
+ const publicKey = new PublicKey(guardedSplice(byteArray, 0, PUBKEY_LENGTH));
5732
+ const isSigner = guardedShift(byteArray) === 1;
5710
5733
  configKeys.push({
5711
5734
  publicKey,
5712
5735
  isSigner