@solana/web3.js 1.10.0 → 1.10.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
@@ -131,7 +131,7 @@ class PublicKey {
131
131
  let buffer = Buffer.alloc(0);
132
132
  seeds.forEach(function (seed) {
133
133
  if (seed.length > MAX_SEED_LENGTH) {
134
- throw new Error("Max seed length exceeded");
134
+ throw new TypeError("Max seed length exceeded");
135
135
  }
136
136
 
137
137
  buffer = Buffer.concat([buffer, toBuffer(seed)]);
@@ -164,6 +164,10 @@ class PublicKey {
164
164
  const seedsWithNonce = seeds.concat(Buffer.from([nonce]));
165
165
  address = await this.createProgramAddress(seedsWithNonce, programId);
166
166
  } catch (err) {
167
+ if (err instanceof TypeError) {
168
+ throw err;
169
+ }
170
+
167
171
  nonce--;
168
172
  continue;
169
173
  }
@@ -376,6 +380,36 @@ function encodeLength(bytes, len) {
376
380
  }
377
381
  }
378
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
+
379
413
  /**
380
414
  * The message header, identifying signed and read-only account
381
415
  */
@@ -452,32 +486,28 @@ class Message {
452
486
  static from(buffer) {
453
487
  // Slice up wire data
454
488
  let byteArray = [...buffer];
455
- const numRequiredSignatures = byteArray.shift();
456
- const numReadonlySignedAccounts = byteArray.shift();
457
- const numReadonlyUnsignedAccounts = byteArray.shift();
489
+ const numRequiredSignatures = guardedShift(byteArray);
490
+ const numReadonlySignedAccounts = guardedShift(byteArray);
491
+ const numReadonlyUnsignedAccounts = guardedShift(byteArray);
458
492
  const accountCount = decodeLength(byteArray);
459
493
  let accountKeys = [];
460
494
 
461
495
  for (let i = 0; i < accountCount; i++) {
462
- const account = byteArray.slice(0, PUBKEY_LENGTH);
463
- byteArray = byteArray.slice(PUBKEY_LENGTH);
496
+ const account = guardedSplice(byteArray, 0, PUBKEY_LENGTH);
464
497
  accountKeys.push(bs58.encode(Buffer.from(account)));
465
498
  }
466
499
 
467
- const recentBlockhash = byteArray.slice(0, PUBKEY_LENGTH);
468
- byteArray = byteArray.slice(PUBKEY_LENGTH);
500
+ const recentBlockhash = guardedSplice(byteArray, 0, PUBKEY_LENGTH);
469
501
  const instructionCount = decodeLength(byteArray);
470
502
  let instructions = [];
471
503
 
472
504
  for (let i = 0; i < instructionCount; i++) {
473
- const programIdIndex = byteArray.shift();
505
+ const programIdIndex = guardedShift(byteArray);
474
506
  const accountCount = decodeLength(byteArray);
475
- const accounts = byteArray.slice(0, accountCount);
476
- byteArray = byteArray.slice(accountCount);
507
+ const accounts = guardedSplice(byteArray, 0, accountCount);
477
508
  const dataLength = decodeLength(byteArray);
478
- const dataSlice = byteArray.slice(0, dataLength);
509
+ const dataSlice = guardedSplice(byteArray, 0, dataLength);
479
510
  const data = bs58.encode(Buffer.from(dataSlice));
480
- byteArray = byteArray.slice(dataLength);
481
511
  instructions.push({
482
512
  programIdIndex,
483
513
  accounts,
@@ -500,6 +530,10 @@ class Message {
500
530
 
501
531
  }
502
532
 
533
+ /**
534
+ * Transaction signature as base-58 encoded string
535
+ */
536
+
503
537
  /**
504
538
  * Default (empty) signature
505
539
  *
@@ -1083,8 +1117,7 @@ class Transaction {
1083
1117
  let signatures = [];
1084
1118
 
1085
1119
  for (let i = 0; i < signatureCount; i++) {
1086
- const signature = byteArray.slice(0, SIGNATURE_LENGTH);
1087
- byteArray = byteArray.slice(SIGNATURE_LENGTH);
1120
+ const signature = guardedSplice(byteArray, 0, SIGNATURE_LENGTH);
1088
1121
  signatures.push(bs58.encode(Buffer.from(signature)));
1089
1122
  }
1090
1123
 
@@ -6117,10 +6150,8 @@ class ValidatorInfo {
6117
6150
  const configKeys = [];
6118
6151
 
6119
6152
  for (let i = 0; i < 2; i++) {
6120
- const publicKey = new PublicKey(byteArray.slice(0, PUBKEY_LENGTH));
6121
- byteArray = byteArray.slice(PUBKEY_LENGTH);
6122
- const isSigner = byteArray.slice(0, 1)[0] === 1;
6123
- byteArray = byteArray.slice(1);
6153
+ const publicKey = new PublicKey(guardedSplice(byteArray, 0, PUBKEY_LENGTH));
6154
+ const isSigner = guardedShift(byteArray) === 1;
6124
6155
  configKeys.push({
6125
6156
  publicKey,
6126
6157
  isSigner