@solana/web3.js 1.24.2 → 1.28.0

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
@@ -10,7 +10,7 @@ var bs58 = require('bs58');
10
10
  var cryptoHash = require('crypto-hash');
11
11
  var borsh = require('borsh');
12
12
  var BufferLayout = require('@solana/buffer-layout');
13
- var fetch = require('node-fetch');
13
+ var fetch = require('cross-fetch');
14
14
  var superstruct = require('superstruct');
15
15
  var rpcWebsockets = require('rpc-websockets');
16
16
  var RpcClient = require('jayson/lib/client/browser');
@@ -106,6 +106,9 @@ const SOLANA_SCHEMA = new Map();
106
106
  */
107
107
 
108
108
  const MAX_SEED_LENGTH = 32;
109
+ /**
110
+ * Value to be converted into public key
111
+ */
109
112
 
110
113
  function isPublicKeyData(value) {
111
114
  return value._bn !== undefined;
@@ -495,16 +498,35 @@ class Message {
495
498
 
496
499
  _defineProperty__default['default'](this, "instructions", void 0);
497
500
 
501
+ _defineProperty__default['default'](this, "indexToProgramIds", new Map());
502
+
498
503
  this.header = args.header;
499
504
  this.accountKeys = args.accountKeys.map(account => new PublicKey(account));
500
505
  this.recentBlockhash = args.recentBlockhash;
501
506
  this.instructions = args.instructions;
507
+ this.instructions.forEach(ix => this.indexToProgramIds.set(ix.programIdIndex, this.accountKeys[ix.programIdIndex]));
508
+ }
509
+
510
+ isAccountSigner(index) {
511
+ return index < this.header.numRequiredSignatures;
502
512
  }
503
513
 
504
514
  isAccountWritable(index) {
505
515
  return index < this.header.numRequiredSignatures - this.header.numReadonlySignedAccounts || index >= this.header.numRequiredSignatures && index < this.accountKeys.length - this.header.numReadonlyUnsignedAccounts;
506
516
  }
507
517
 
518
+ isProgramId(index) {
519
+ return this.indexToProgramIds.has(index);
520
+ }
521
+
522
+ programIds() {
523
+ return [...this.indexToProgramIds.values()];
524
+ }
525
+
526
+ nonProgramIds() {
527
+ return this.accountKeys.filter((_, index) => !this.isProgramId(index));
528
+ }
529
+
508
530
  serialize() {
509
531
  const numKeys = this.accountKeys.length;
510
532
  let keyCount = [];
@@ -1219,7 +1241,7 @@ class Transaction {
1219
1241
  */
1220
1242
 
1221
1243
 
1222
- static populate(message, signatures) {
1244
+ static populate(message, signatures = []) {
1223
1245
  const transaction = new Transaction();
1224
1246
  transaction.recentBlockhash = message.recentBlockhash;
1225
1247
 
@@ -1239,7 +1261,7 @@ class Transaction {
1239
1261
  const pubkey = message.accountKeys[account];
1240
1262
  return {
1241
1263
  pubkey,
1242
- isSigner: transaction.signatures.some(keyObj => keyObj.publicKey.toString() === pubkey.toString()),
1264
+ isSigner: transaction.signatures.some(keyObj => keyObj.publicKey.toString() === pubkey.toString()) || message.isAccountSigner(account),
1243
1265
  isWritable: message.isAccountWritable(account)
1244
1266
  };
1245
1267
  });
@@ -2671,7 +2693,15 @@ const VersionResult = superstruct.type({
2671
2693
  });
2672
2694
  const SimulatedTransactionResponseStruct = jsonRpcResultAndContext(superstruct.type({
2673
2695
  err: superstruct.nullable(superstruct.union([superstruct.type({}), superstruct.string()])),
2674
- logs: superstruct.nullable(superstruct.array(superstruct.string()))
2696
+ logs: superstruct.nullable(superstruct.array(superstruct.string())),
2697
+ accounts: superstruct.optional(superstruct.nullable(superstruct.array(superstruct.type({
2698
+ executable: superstruct.boolean(),
2699
+ owner: superstruct.string(),
2700
+ lamports: superstruct.number(),
2701
+ data: superstruct.array(superstruct.string()),
2702
+ rentEpoch: superstruct.optional(superstruct.number())
2703
+ })))),
2704
+ unitsConsumed: superstruct.optional(superstruct.number())
2675
2705
  }));
2676
2706
 
2677
2707
  function createRpcClient(url, useHttps, httpHeaders, fetchMiddleware, disableRetryOnRateLimit) {
@@ -3403,6 +3433,8 @@ class Connection {
3403
3433
 
3404
3434
  /** @internal */
3405
3435
 
3436
+ /** @internal */
3437
+
3406
3438
  /**
3407
3439
  * Establish a JSON RPC connection
3408
3440
  *
@@ -3412,6 +3444,8 @@ class Connection {
3412
3444
  constructor(endpoint, commitmentOrConfig) {
3413
3445
  _defineProperty__default['default'](this, "_commitment", void 0);
3414
3446
 
3447
+ _defineProperty__default['default'](this, "_confirmTransactionInitialTimeout", void 0);
3448
+
3415
3449
  _defineProperty__default['default'](this, "_rpcEndpoint", void 0);
3416
3450
 
3417
3451
  _defineProperty__default['default'](this, "_rpcWsEndpoint", void 0);
@@ -3480,6 +3514,7 @@ class Connection {
3480
3514
  this._commitment = commitmentOrConfig;
3481
3515
  } else if (commitmentOrConfig) {
3482
3516
  this._commitment = commitmentOrConfig.commitment;
3517
+ this._confirmTransactionInitialTimeout = commitmentOrConfig.confirmTransactionInitialTimeout;
3483
3518
  wsEndpoint = commitmentOrConfig.wsEndpoint;
3484
3519
  httpHeaders = commitmentOrConfig.httpHeaders;
3485
3520
  fetchMiddleware = commitmentOrConfig.fetchMiddleware;
@@ -3939,7 +3974,7 @@ class Connection {
3939
3974
  reject(err);
3940
3975
  }
3941
3976
  });
3942
- let timeoutMs = 60 * 1000;
3977
+ let timeoutMs = this._confirmTransactionInitialTimeout || 60 * 1000;
3943
3978
 
3944
3979
  switch (subscriptionCommitment) {
3945
3980
  case 'processed':
@@ -3948,7 +3983,7 @@ class Connection {
3948
3983
  case 'confirmed':
3949
3984
  case 'singleGossip':
3950
3985
  {
3951
- timeoutMs = 30 * 1000;
3986
+ timeoutMs = this._confirmTransactionInitialTimeout || 30 * 1000;
3952
3987
  break;
3953
3988
  }
3954
3989
  }
@@ -4320,6 +4355,21 @@ class Connection {
4320
4355
 
4321
4356
  return res.result;
4322
4357
  }
4358
+ /**
4359
+ * Fetch the genesis hash
4360
+ */
4361
+
4362
+
4363
+ async getGenesisHash() {
4364
+ const unsafeRes = await this._rpcRequest('getGenesisHash', []);
4365
+ const res = superstruct.create(unsafeRes, jsonRpcResult(superstruct.string()));
4366
+
4367
+ if ('error' in res) {
4368
+ throw new Error('failed to get genesis hash: ' + res.error.message);
4369
+ }
4370
+
4371
+ return res.result;
4372
+ }
4323
4373
  /**
4324
4374
  * Fetch a processed block from the cluster.
4325
4375
  */
@@ -4720,7 +4770,15 @@ class Connection {
4720
4770
  */
4721
4771
 
4722
4772
 
4723
- async simulateTransaction(transaction, signers) {
4773
+ async simulateTransaction(transactionOrMessage, signers, includeAccounts) {
4774
+ let transaction;
4775
+
4776
+ if (transactionOrMessage instanceof Transaction) {
4777
+ transaction = transactionOrMessage;
4778
+ } else {
4779
+ transaction = Transaction.populate(transactionOrMessage);
4780
+ }
4781
+
4724
4782
  if (transaction.nonceInfo && signers) {
4725
4783
  transaction.sign(...signers);
4726
4784
  } else {
@@ -4753,7 +4811,9 @@ class Connection {
4753
4811
  }
4754
4812
  }
4755
4813
 
4756
- const signData = transaction.serializeMessage();
4814
+ const message = transaction._compile();
4815
+
4816
+ const signData = message.serialize();
4757
4817
 
4758
4818
  const wireTransaction = transaction._serialize(signData);
4759
4819
 
@@ -4763,6 +4823,14 @@ class Connection {
4763
4823
  commitment: this.commitment
4764
4824
  };
4765
4825
 
4826
+ if (includeAccounts) {
4827
+ const addresses = (Array.isArray(includeAccounts) ? includeAccounts : message.nonProgramIds()).map(key => key.toBase58());
4828
+ config['accounts'] = {
4829
+ encoding: 'base64',
4830
+ addresses
4831
+ };
4832
+ }
4833
+
4766
4834
  if (signers) {
4767
4835
  config.sigVerify = true;
4768
4836
  }
@@ -5643,6 +5711,96 @@ class Keypair {
5643
5711
 
5644
5712
  }
5645
5713
 
5714
+ const PRIVATE_KEY_BYTES$1 = 64;
5715
+ const PUBLIC_KEY_BYTES$1 = 32;
5716
+ const SIGNATURE_BYTES = 64;
5717
+ /**
5718
+ * Params for creating an ed25519 instruction using a public key
5719
+ */
5720
+
5721
+ const ED25519_INSTRUCTION_LAYOUT = BufferLayout__namespace.struct([BufferLayout__namespace.u8('numSignatures'), BufferLayout__namespace.u8('padding'), BufferLayout__namespace.u16('signatureOffset'), BufferLayout__namespace.u16('signatureInstructionIndex'), BufferLayout__namespace.u16('publicKeyOffset'), BufferLayout__namespace.u16('publicKeyInstructionIndex'), BufferLayout__namespace.u16('messageDataOffset'), BufferLayout__namespace.u16('messageDataSize'), BufferLayout__namespace.u16('messageInstructionIndex')]);
5722
+ class Ed25519Program {
5723
+ /**
5724
+ * @internal
5725
+ */
5726
+ constructor() {}
5727
+ /**
5728
+ * Public key that identifies the ed25519 program
5729
+ */
5730
+
5731
+
5732
+ /**
5733
+ * Create an ed25519 instruction with a public key and signature. The
5734
+ * public key must be a buffer that is 32 bytes long, and the signature
5735
+ * must be a buffer of 64 bytes.
5736
+ */
5737
+ static createInstructionWithPublicKey(params) {
5738
+ const {
5739
+ publicKey,
5740
+ message,
5741
+ signature,
5742
+ instructionIndex
5743
+ } = params;
5744
+ assert(publicKey.length === PUBLIC_KEY_BYTES$1, `Public Key must be ${PUBLIC_KEY_BYTES$1} bytes but received ${publicKey.length} bytes`);
5745
+ assert(signature.length === SIGNATURE_BYTES, `Signature must be ${SIGNATURE_BYTES} bytes but received ${signature.length} bytes`);
5746
+ const publicKeyOffset = ED25519_INSTRUCTION_LAYOUT.span;
5747
+ const signatureOffset = publicKeyOffset + publicKey.length;
5748
+ const messageDataOffset = signatureOffset + signature.length;
5749
+ const numSignatures = 1;
5750
+ const instructionData = buffer.Buffer.alloc(messageDataOffset + message.length);
5751
+ ED25519_INSTRUCTION_LAYOUT.encode({
5752
+ numSignatures,
5753
+ padding: 0,
5754
+ signatureOffset,
5755
+ signatureInstructionIndex: instructionIndex,
5756
+ publicKeyOffset,
5757
+ publicKeyInstructionIndex: instructionIndex,
5758
+ messageDataOffset,
5759
+ messageDataSize: message.length,
5760
+ messageInstructionIndex: instructionIndex
5761
+ }, instructionData);
5762
+ instructionData.fill(publicKey, publicKeyOffset);
5763
+ instructionData.fill(signature, signatureOffset);
5764
+ instructionData.fill(message, messageDataOffset);
5765
+ return new TransactionInstruction({
5766
+ keys: [],
5767
+ programId: Ed25519Program.programId,
5768
+ data: instructionData
5769
+ });
5770
+ }
5771
+ /**
5772
+ * Create an ed25519 instruction with a private key. The private key
5773
+ * must be a buffer that is 64 bytes long.
5774
+ */
5775
+
5776
+
5777
+ static createInstructionWithPrivateKey(params) {
5778
+ const {
5779
+ privateKey,
5780
+ message,
5781
+ instructionIndex
5782
+ } = params;
5783
+ assert(privateKey.length === PRIVATE_KEY_BYTES$1, `Private key must be ${PRIVATE_KEY_BYTES$1} bytes but received ${privateKey.length} bytes`);
5784
+
5785
+ try {
5786
+ const keypair = Keypair.fromSecretKey(privateKey);
5787
+ const publicKey = keypair.publicKey.toBytes();
5788
+ const signature = nacl__default['default'].sign.detached(message, keypair.secretKey);
5789
+ return this.createInstructionWithPublicKey({
5790
+ publicKey,
5791
+ message,
5792
+ signature,
5793
+ instructionIndex
5794
+ });
5795
+ } catch (error) {
5796
+ throw new Error(`Error creating instruction; ${error}`);
5797
+ }
5798
+ }
5799
+
5800
+ }
5801
+
5802
+ _defineProperty__default['default'](Ed25519Program, "programId", new PublicKey('Ed25519SigVerify111111111111111111111111111'));
5803
+
5646
5804
  /**
5647
5805
  * Address of the stake config account which configures the rate
5648
5806
  * of stake warmup and cooldown as well as the slashing penalty.
@@ -6782,6 +6940,7 @@ exports.BPF_LOADER_DEPRECATED_PROGRAM_ID = BPF_LOADER_DEPRECATED_PROGRAM_ID;
6782
6940
  exports.BPF_LOADER_PROGRAM_ID = BPF_LOADER_PROGRAM_ID;
6783
6941
  exports.BpfLoader = BpfLoader;
6784
6942
  exports.Connection = Connection;
6943
+ exports.Ed25519Program = Ed25519Program;
6785
6944
  exports.Enum = Enum;
6786
6945
  exports.EpochSchedule = EpochSchedule;
6787
6946
  exports.FeeCalculatorLayout = FeeCalculatorLayout;