@solana/web3.js 1.39.0 → 1.39.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.cjs.js CHANGED
@@ -2204,6 +2204,36 @@ function encodeLength(bytes, len) {
2204
2204
  }
2205
2205
  }
2206
2206
 
2207
+ const END_OF_BUFFER_ERROR_MESSAGE = 'Reached end of buffer unexpectedly';
2208
+ /**
2209
+ * Delegates to `Array#shift`, but throws if the array is zero-length.
2210
+ */
2211
+
2212
+ function guardedShift(byteArray) {
2213
+ if (byteArray.length === 0) {
2214
+ throw new Error(END_OF_BUFFER_ERROR_MESSAGE);
2215
+ }
2216
+
2217
+ return byteArray.shift();
2218
+ }
2219
+ /**
2220
+ * Delegates to `Array#splice`, but throws if the section being spliced out extends past the end of
2221
+ * the array.
2222
+ */
2223
+
2224
+ function guardedSplice(byteArray, ...args) {
2225
+ var _args$;
2226
+
2227
+ const [start] = args;
2228
+
2229
+ if (args.length === 2 // Implies that `deleteCount` was supplied
2230
+ ? start + ((_args$ = args[1]) !== null && _args$ !== void 0 ? _args$ : 0) > byteArray.length : start >= byteArray.length) {
2231
+ throw new Error(END_OF_BUFFER_ERROR_MESSAGE);
2232
+ }
2233
+
2234
+ return byteArray.splice(...args);
2235
+ }
2236
+
2207
2237
  /**
2208
2238
  * The message header, identifying signed and read-only account
2209
2239
  */
@@ -2302,32 +2332,28 @@ class Message {
2302
2332
  static from(buffer$1) {
2303
2333
  // Slice up wire data
2304
2334
  let byteArray = [...buffer$1];
2305
- const numRequiredSignatures = byteArray.shift();
2306
- const numReadonlySignedAccounts = byteArray.shift();
2307
- const numReadonlyUnsignedAccounts = byteArray.shift();
2335
+ const numRequiredSignatures = guardedShift(byteArray);
2336
+ const numReadonlySignedAccounts = guardedShift(byteArray);
2337
+ const numReadonlyUnsignedAccounts = guardedShift(byteArray);
2308
2338
  const accountCount = decodeLength(byteArray);
2309
2339
  let accountKeys = [];
2310
2340
 
2311
2341
  for (let i = 0; i < accountCount; i++) {
2312
- const account = byteArray.slice(0, PUBKEY_LENGTH);
2313
- byteArray = byteArray.slice(PUBKEY_LENGTH);
2342
+ const account = guardedSplice(byteArray, 0, PUBKEY_LENGTH);
2314
2343
  accountKeys.push(bs58__default["default"].encode(buffer.Buffer.from(account)));
2315
2344
  }
2316
2345
 
2317
- const recentBlockhash = byteArray.slice(0, PUBKEY_LENGTH);
2318
- byteArray = byteArray.slice(PUBKEY_LENGTH);
2346
+ const recentBlockhash = guardedSplice(byteArray, 0, PUBKEY_LENGTH);
2319
2347
  const instructionCount = decodeLength(byteArray);
2320
2348
  let instructions = [];
2321
2349
 
2322
2350
  for (let i = 0; i < instructionCount; i++) {
2323
- const programIdIndex = byteArray.shift();
2351
+ const programIdIndex = guardedShift(byteArray);
2324
2352
  const accountCount = decodeLength(byteArray);
2325
- const accounts = byteArray.slice(0, accountCount);
2326
- byteArray = byteArray.slice(accountCount);
2353
+ const accounts = guardedSplice(byteArray, 0, accountCount);
2327
2354
  const dataLength = decodeLength(byteArray);
2328
- const dataSlice = byteArray.slice(0, dataLength);
2355
+ const dataSlice = guardedSplice(byteArray, 0, dataLength);
2329
2356
  const data = bs58__default["default"].encode(buffer.Buffer.from(dataSlice));
2330
- byteArray = byteArray.slice(dataLength);
2331
2357
  instructions.push({
2332
2358
  programIdIndex,
2333
2359
  accounts,
@@ -2356,6 +2382,10 @@ function assert (condition, message) {
2356
2382
  }
2357
2383
  }
2358
2384
 
2385
+ /**
2386
+ * Transaction signature as base-58 encoded string
2387
+ */
2388
+
2359
2389
  /**
2360
2390
  * Default (empty) signature
2361
2391
  *
@@ -2403,6 +2433,26 @@ class TransactionInstruction {
2403
2433
  this.data = opts.data;
2404
2434
  }
2405
2435
  }
2436
+ /**
2437
+ * @internal
2438
+ */
2439
+
2440
+
2441
+ toJSON() {
2442
+ return {
2443
+ keys: this.keys.map(({
2444
+ pubkey,
2445
+ isSigner,
2446
+ isWritable
2447
+ }) => ({
2448
+ pubkey: pubkey.toJSON(),
2449
+ isSigner,
2450
+ isWritable
2451
+ })),
2452
+ programId: this.programId.toJSON(),
2453
+ data: [...this.data]
2454
+ };
2455
+ }
2406
2456
 
2407
2457
  }
2408
2458
  /**
@@ -2442,8 +2492,33 @@ class Transaction {
2442
2492
  this.instructions = [];
2443
2493
  this.recentBlockhash = void 0;
2444
2494
  this.nonceInfo = void 0;
2495
+ this._message = void 0;
2496
+ this._json = void 0;
2445
2497
  opts && Object.assign(this, opts);
2446
2498
  }
2499
+ /**
2500
+ * @internal
2501
+ */
2502
+
2503
+
2504
+ toJSON() {
2505
+ return {
2506
+ recentBlockhash: this.recentBlockhash || null,
2507
+ feePayer: this.feePayer ? this.feePayer.toJSON() : null,
2508
+ nonceInfo: this.nonceInfo ? {
2509
+ nonce: this.nonceInfo.nonce,
2510
+ nonceInstruction: this.nonceInfo.nonceInstruction.toJSON()
2511
+ } : null,
2512
+ instructions: this.instructions.map(instruction => instruction.toJSON()),
2513
+ signatures: this.signatures.map(({
2514
+ publicKey,
2515
+ signature
2516
+ }) => ({
2517
+ publicKey: publicKey.toJSON(),
2518
+ signature: signature ? [...signature] : null
2519
+ }))
2520
+ };
2521
+ }
2447
2522
  /**
2448
2523
  * Add one or more instructions to this Transaction
2449
2524
  */
@@ -2471,6 +2546,14 @@ class Transaction {
2471
2546
 
2472
2547
 
2473
2548
  compileMessage() {
2549
+ if (this._message) {
2550
+ if (JSON.stringify(this.toJSON()) !== JSON.stringify(this._json)) {
2551
+ throw new Error('Transaction mutated after being populated from Message');
2552
+ }
2553
+
2554
+ return this._message;
2555
+ }
2556
+
2474
2557
  const {
2475
2558
  nonceInfo
2476
2559
  } = this;
@@ -2950,8 +3033,7 @@ class Transaction {
2950
3033
  let signatures = [];
2951
3034
 
2952
3035
  for (let i = 0; i < signatureCount; i++) {
2953
- const signature = byteArray.slice(0, SIGNATURE_LENGTH);
2954
- byteArray = byteArray.slice(SIGNATURE_LENGTH);
3036
+ const signature = guardedSplice(byteArray, 0, SIGNATURE_LENGTH);
2955
3037
  signatures.push(bs58__default["default"].encode(buffer.Buffer.from(signature)));
2956
3038
  }
2957
3039
 
@@ -2992,6 +3074,8 @@ class Transaction {
2992
3074
  data: bs58__default["default"].decode(instruction.data)
2993
3075
  }));
2994
3076
  });
3077
+ transaction._message = message;
3078
+ transaction._json = transaction.toJSON();
2995
3079
  return transaction;
2996
3080
  }
2997
3081
 
@@ -6768,7 +6852,9 @@ class Connection {
6768
6852
  });
6769
6853
  transaction.instructions = transactionOrMessage.instructions;
6770
6854
  } else {
6771
- transaction = Transaction.populate(transactionOrMessage);
6855
+ transaction = Transaction.populate(transactionOrMessage); // HACK: this function relies on mutating the populated transaction
6856
+
6857
+ transaction._message = transaction._json = undefined;
6772
6858
  }
6773
6859
 
6774
6860
  if (transaction.nonceInfo && signers) {
@@ -8791,10 +8877,8 @@ class ValidatorInfo {
8791
8877
  const configKeys = [];
8792
8878
 
8793
8879
  for (let i = 0; i < 2; i++) {
8794
- const publicKey = new PublicKey(byteArray.slice(0, PUBKEY_LENGTH));
8795
- byteArray = byteArray.slice(PUBKEY_LENGTH);
8796
- const isSigner = byteArray.slice(0, 1)[0] === 1;
8797
- byteArray = byteArray.slice(1);
8880
+ const publicKey = new PublicKey(guardedSplice(byteArray, 0, PUBKEY_LENGTH));
8881
+ const isSigner = guardedShift(byteArray) === 1;
8798
8882
  configKeys.push({
8799
8883
  publicKey,
8800
8884
  isSigner