@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.
@@ -2192,6 +2192,36 @@ function encodeLength(bytes, len) {
2192
2192
  }
2193
2193
  }
2194
2194
 
2195
+ const END_OF_BUFFER_ERROR_MESSAGE = 'Reached end of buffer unexpectedly';
2196
+ /**
2197
+ * Delegates to `Array#shift`, but throws if the array is zero-length.
2198
+ */
2199
+
2200
+ function guardedShift(byteArray) {
2201
+ if (byteArray.length === 0) {
2202
+ throw new Error(END_OF_BUFFER_ERROR_MESSAGE);
2203
+ }
2204
+
2205
+ return byteArray.shift();
2206
+ }
2207
+ /**
2208
+ * Delegates to `Array#splice`, but throws if the section being spliced out extends past the end of
2209
+ * the array.
2210
+ */
2211
+
2212
+ function guardedSplice(byteArray, ...args) {
2213
+ var _args$;
2214
+
2215
+ const [start] = args;
2216
+
2217
+ if (args.length === 2 // Implies that `deleteCount` was supplied
2218
+ ? start + ((_args$ = args[1]) !== null && _args$ !== void 0 ? _args$ : 0) > byteArray.length : start >= byteArray.length) {
2219
+ throw new Error(END_OF_BUFFER_ERROR_MESSAGE);
2220
+ }
2221
+
2222
+ return byteArray.splice(...args);
2223
+ }
2224
+
2195
2225
  /**
2196
2226
  * The message header, identifying signed and read-only account
2197
2227
  */
@@ -2290,32 +2320,28 @@ class Message {
2290
2320
  static from(buffer$1) {
2291
2321
  // Slice up wire data
2292
2322
  let byteArray = [...buffer$1];
2293
- const numRequiredSignatures = byteArray.shift();
2294
- const numReadonlySignedAccounts = byteArray.shift();
2295
- const numReadonlyUnsignedAccounts = byteArray.shift();
2323
+ const numRequiredSignatures = guardedShift(byteArray);
2324
+ const numReadonlySignedAccounts = guardedShift(byteArray);
2325
+ const numReadonlyUnsignedAccounts = guardedShift(byteArray);
2296
2326
  const accountCount = decodeLength(byteArray);
2297
2327
  let accountKeys = [];
2298
2328
 
2299
2329
  for (let i = 0; i < accountCount; i++) {
2300
- const account = byteArray.slice(0, PUBKEY_LENGTH);
2301
- byteArray = byteArray.slice(PUBKEY_LENGTH);
2330
+ const account = guardedSplice(byteArray, 0, PUBKEY_LENGTH);
2302
2331
  accountKeys.push(bs58__default["default"].encode(buffer.Buffer.from(account)));
2303
2332
  }
2304
2333
 
2305
- const recentBlockhash = byteArray.slice(0, PUBKEY_LENGTH);
2306
- byteArray = byteArray.slice(PUBKEY_LENGTH);
2334
+ const recentBlockhash = guardedSplice(byteArray, 0, PUBKEY_LENGTH);
2307
2335
  const instructionCount = decodeLength(byteArray);
2308
2336
  let instructions = [];
2309
2337
 
2310
2338
  for (let i = 0; i < instructionCount; i++) {
2311
- const programIdIndex = byteArray.shift();
2339
+ const programIdIndex = guardedShift(byteArray);
2312
2340
  const accountCount = decodeLength(byteArray);
2313
- const accounts = byteArray.slice(0, accountCount);
2314
- byteArray = byteArray.slice(accountCount);
2341
+ const accounts = guardedSplice(byteArray, 0, accountCount);
2315
2342
  const dataLength = decodeLength(byteArray);
2316
- const dataSlice = byteArray.slice(0, dataLength);
2343
+ const dataSlice = guardedSplice(byteArray, 0, dataLength);
2317
2344
  const data = bs58__default["default"].encode(buffer.Buffer.from(dataSlice));
2318
- byteArray = byteArray.slice(dataLength);
2319
2345
  instructions.push({
2320
2346
  programIdIndex,
2321
2347
  accounts,
@@ -2344,6 +2370,10 @@ function assert (condition, message) {
2344
2370
  }
2345
2371
  }
2346
2372
 
2373
+ /**
2374
+ * Transaction signature as base-58 encoded string
2375
+ */
2376
+
2347
2377
  /**
2348
2378
  * Default (empty) signature
2349
2379
  *
@@ -2391,6 +2421,26 @@ class TransactionInstruction {
2391
2421
  this.data = opts.data;
2392
2422
  }
2393
2423
  }
2424
+ /**
2425
+ * @internal
2426
+ */
2427
+
2428
+
2429
+ toJSON() {
2430
+ return {
2431
+ keys: this.keys.map(({
2432
+ pubkey,
2433
+ isSigner,
2434
+ isWritable
2435
+ }) => ({
2436
+ pubkey: pubkey.toJSON(),
2437
+ isSigner,
2438
+ isWritable
2439
+ })),
2440
+ programId: this.programId.toJSON(),
2441
+ data: [...this.data]
2442
+ };
2443
+ }
2394
2444
 
2395
2445
  }
2396
2446
  /**
@@ -2430,8 +2480,33 @@ class Transaction {
2430
2480
  this.instructions = [];
2431
2481
  this.recentBlockhash = void 0;
2432
2482
  this.nonceInfo = void 0;
2483
+ this._message = void 0;
2484
+ this._json = void 0;
2433
2485
  opts && Object.assign(this, opts);
2434
2486
  }
2487
+ /**
2488
+ * @internal
2489
+ */
2490
+
2491
+
2492
+ toJSON() {
2493
+ return {
2494
+ recentBlockhash: this.recentBlockhash || null,
2495
+ feePayer: this.feePayer ? this.feePayer.toJSON() : null,
2496
+ nonceInfo: this.nonceInfo ? {
2497
+ nonce: this.nonceInfo.nonce,
2498
+ nonceInstruction: this.nonceInfo.nonceInstruction.toJSON()
2499
+ } : null,
2500
+ instructions: this.instructions.map(instruction => instruction.toJSON()),
2501
+ signatures: this.signatures.map(({
2502
+ publicKey,
2503
+ signature
2504
+ }) => ({
2505
+ publicKey: publicKey.toJSON(),
2506
+ signature: signature ? [...signature] : null
2507
+ }))
2508
+ };
2509
+ }
2435
2510
  /**
2436
2511
  * Add one or more instructions to this Transaction
2437
2512
  */
@@ -2459,6 +2534,14 @@ class Transaction {
2459
2534
 
2460
2535
 
2461
2536
  compileMessage() {
2537
+ if (this._message) {
2538
+ if (JSON.stringify(this.toJSON()) !== JSON.stringify(this._json)) {
2539
+ throw new Error('Transaction mutated after being populated from Message');
2540
+ }
2541
+
2542
+ return this._message;
2543
+ }
2544
+
2462
2545
  const {
2463
2546
  nonceInfo
2464
2547
  } = this;
@@ -2938,8 +3021,7 @@ class Transaction {
2938
3021
  let signatures = [];
2939
3022
 
2940
3023
  for (let i = 0; i < signatureCount; i++) {
2941
- const signature = byteArray.slice(0, SIGNATURE_LENGTH);
2942
- byteArray = byteArray.slice(SIGNATURE_LENGTH);
3024
+ const signature = guardedSplice(byteArray, 0, SIGNATURE_LENGTH);
2943
3025
  signatures.push(bs58__default["default"].encode(buffer.Buffer.from(signature)));
2944
3026
  }
2945
3027
 
@@ -2980,6 +3062,8 @@ class Transaction {
2980
3062
  data: bs58__default["default"].decode(instruction.data)
2981
3063
  }));
2982
3064
  });
3065
+ transaction._message = message;
3066
+ transaction._json = transaction.toJSON();
2983
3067
  return transaction;
2984
3068
  }
2985
3069
 
@@ -7262,7 +7346,9 @@ class Connection {
7262
7346
  });
7263
7347
  transaction.instructions = transactionOrMessage.instructions;
7264
7348
  } else {
7265
- transaction = Transaction.populate(transactionOrMessage);
7349
+ transaction = Transaction.populate(transactionOrMessage); // HACK: this function relies on mutating the populated transaction
7350
+
7351
+ transaction._message = transaction._json = undefined;
7266
7352
  }
7267
7353
 
7268
7354
  if (transaction.nonceInfo && signers) {
@@ -9285,10 +9371,8 @@ class ValidatorInfo {
9285
9371
  const configKeys = [];
9286
9372
 
9287
9373
  for (let i = 0; i < 2; i++) {
9288
- const publicKey = new PublicKey(byteArray.slice(0, PUBKEY_LENGTH));
9289
- byteArray = byteArray.slice(PUBKEY_LENGTH);
9290
- const isSigner = byteArray.slice(0, 1)[0] === 1;
9291
- byteArray = byteArray.slice(1);
9374
+ const publicKey = new PublicKey(guardedSplice(byteArray, 0, PUBKEY_LENGTH));
9375
+ const isSigner = guardedShift(byteArray) === 1;
9292
9376
  configKeys.push({
9293
9377
  publicKey,
9294
9378
  isSigner