@solana/web3.js 1.75.0 → 1.77.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.d.ts CHANGED
@@ -1326,6 +1326,8 @@ export class VoteProgram {
1326
1326
  * This is generated from the solana-vote-program VoteState struct as
1327
1327
  * `VoteState::size_of()`:
1328
1328
  * https://docs.rs/solana-vote-program/1.9.5/solana_vote_program/vote_state/struct.VoteState.html#method.size_of
1329
+ *
1330
+ * KEEP IN SYNC WITH `VoteState::size_of()` in https://github.com/solana-labs/solana/blob/a474cb24b9238f5edcc982f65c0b37d4a1046f7e/sdk/program/src/vote/state/mod.rs#L340-L342
1329
1331
  */
1330
1332
  static space: number;
1331
1333
  /**
@@ -2827,7 +2829,14 @@ type GetProgramAccountsConfig = {
2827
2829
  filters?: GetProgramAccountsFilter[];
2828
2830
  /** The minimum slot that the request can be evaluated at */
2829
2831
  minContextSlot?: number;
2832
+ /** wrap the result in an RpcResponse JSON object */
2833
+ withContext?: boolean;
2830
2834
  };
2835
+ type GetProgramAccountsResponse = readonly Readonly<{
2836
+ account: AccountInfo<Buffer>;
2837
+ /** the account Pubkey as base-58 encoded string */
2838
+ pubkey: PublicKey;
2839
+ }>[];
2831
2840
  /**
2832
2841
  * Configuration object for getParsedProgramAccounts
2833
2842
  */
@@ -3128,12 +3137,9 @@ export class Connection {
3128
3137
  /**
3129
3138
  * Fetch all the token accounts owned by the specified account
3130
3139
  *
3131
- * @return {Promise<RpcResponseAndContext<Array<{pubkey: PublicKey, account: AccountInfo<Buffer>}>>>}
3140
+ * @return {Promise<RpcResponseAndContext<GetProgramAccountsResponse>}
3132
3141
  */
3133
- getTokenAccountsByOwner(ownerAddress: PublicKey, filter: TokenAccountsFilter, commitmentOrConfig?: Commitment | GetTokenAccountsByOwnerConfig): Promise<RpcResponseAndContext<Array<{
3134
- pubkey: PublicKey;
3135
- account: AccountInfo<Buffer>;
3136
- }>>>;
3142
+ getTokenAccountsByOwner(ownerAddress: PublicKey, filter: TokenAccountsFilter, commitmentOrConfig?: Commitment | GetTokenAccountsByOwnerConfig): Promise<RpcResponseAndContext<GetProgramAccountsResponse>>;
3137
3143
  /**
3138
3144
  * Fetch parsed token accounts owned by the specified account
3139
3145
  *
@@ -3185,10 +3191,10 @@ export class Connection {
3185
3191
  *
3186
3192
  * @return {Promise<Array<{pubkey: PublicKey, account: AccountInfo<Buffer>}>>}
3187
3193
  */
3188
- getProgramAccounts(programId: PublicKey, configOrCommitment?: GetProgramAccountsConfig | Commitment): Promise<Array<{
3189
- pubkey: PublicKey;
3190
- account: AccountInfo<Buffer>;
3191
- }>>;
3194
+ getProgramAccounts(programId: PublicKey, configOrCommitment?: GetProgramAccountsConfig & Readonly<{
3195
+ withContext: true;
3196
+ }>): Promise<RpcResponseAndContext<GetProgramAccountsResponse>>;
3197
+ getProgramAccounts(programId: PublicKey, configOrCommitment?: GetProgramAccountsConfig | Commitment): Promise<GetProgramAccountsResponse>;
3192
3198
  /**
3193
3199
  * Fetch and parse all the accounts owned by the specified program id
3194
3200
  *
package/lib/index.esm.js CHANGED
@@ -1,6 +1,5 @@
1
1
  import { Buffer } from 'buffer';
2
- import { sha512 } from '@noble/hashes/sha512';
3
- import * as ed25519 from '@noble/ed25519';
2
+ import { ed25519 } from '@noble/curves/ed25519';
4
3
  import BN from 'bn.js';
5
4
  import bs58 from 'bs58';
6
5
  import { sha256 } from '@noble/hashes/sha256';
@@ -20,8 +19,7 @@ import * as nodeFetch from 'node-fetch';
20
19
  import RpcWebSocketCommonClient from 'rpc-websockets/dist/lib/client';
21
20
  import WebsocketFactory from 'rpc-websockets/dist/lib/client/websocket';
22
21
  import { keccak_256 } from '@noble/hashes/sha3';
23
- import { hmac } from '@noble/hashes/hmac';
24
- import * as secp256k1 from '@noble/secp256k1';
22
+ import { secp256k1 } from '@noble/curves/secp256k1';
25
23
 
26
24
  /**
27
25
  * A 64 byte secret key, the first 32 bytes of which is the
@@ -29,7 +27,6 @@ import * as secp256k1 from '@noble/secp256k1';
29
27
  * Read more: https://blog.mozilla.org/warner/2011/11/29/ed25519-keys/
30
28
  */
31
29
 
32
- ed25519.utils.sha512Sync = (...m) => sha512(ed25519.utils.concatBytes(...m));
33
30
  const generatePrivateKey = ed25519.utils.randomPrivateKey;
34
31
  const generateKeypair = () => {
35
32
  const privateScalar = ed25519.utils.randomPrivateKey();
@@ -42,17 +39,17 @@ const generateKeypair = () => {
42
39
  secretKey
43
40
  };
44
41
  };
45
- const getPublicKey = ed25519.sync.getPublicKey;
42
+ const getPublicKey = ed25519.getPublicKey;
46
43
  function isOnCurve(publicKey) {
47
44
  try {
48
- ed25519.Point.fromHex(publicKey, true /* strict */);
45
+ ed25519.ExtendedPoint.fromHex(publicKey);
49
46
  return true;
50
47
  } catch {
51
48
  return false;
52
49
  }
53
50
  }
54
- const sign = (message, secretKey) => ed25519.sync.sign(message, secretKey.slice(0, 32));
55
- const verify = ed25519.sync.verify;
51
+ const sign = (message, secretKey) => ed25519.sign(message, secretKey.slice(0, 32));
52
+ const verify = ed25519.verify;
56
53
 
57
54
  const toBuffer = arr => {
58
55
  if (Buffer.isBuffer(arr)) {
@@ -609,8 +606,8 @@ class CompiledKeys {
609
606
  getOrInsertDefault(ix.programId).isInvoked = true;
610
607
  for (const accountMeta of ix.keys) {
611
608
  const keyMeta = getOrInsertDefault(accountMeta.pubkey);
612
- keyMeta.isSigner || (keyMeta.isSigner = accountMeta.isSigner);
613
- keyMeta.isWritable || (keyMeta.isWritable = accountMeta.isWritable);
609
+ keyMeta.isSigner ||= accountMeta.isSigner;
610
+ keyMeta.isWritable ||= accountMeta.isWritable;
614
611
  }
615
612
  }
616
613
  return new CompiledKeys(payer, keyMetaMap);
@@ -1948,6 +1945,29 @@ class VersionedTransaction {
1948
1945
  }
1949
1946
  }
1950
1947
 
1948
+ // TODO: These constants should be removed in favor of reading them out of a
1949
+ // Syscall account
1950
+
1951
+ /**
1952
+ * @internal
1953
+ */
1954
+ const NUM_TICKS_PER_SECOND = 160;
1955
+
1956
+ /**
1957
+ * @internal
1958
+ */
1959
+ const DEFAULT_TICKS_PER_SLOT = 64;
1960
+
1961
+ /**
1962
+ * @internal
1963
+ */
1964
+ const NUM_SLOTS_PER_SECOND = NUM_TICKS_PER_SECOND / DEFAULT_TICKS_PER_SLOT;
1965
+
1966
+ /**
1967
+ * @internal
1968
+ */
1969
+ const MS_PER_SLOT = 1000 / NUM_SLOTS_PER_SECOND;
1970
+
1951
1971
  const SYSVAR_CLOCK_PUBKEY = new PublicKey('SysvarC1ock11111111111111111111111111111111');
1952
1972
  const SYSVAR_EPOCH_SCHEDULE_PUBKEY = new PublicKey('SysvarEpochSchedu1e111111111111111111111111');
1953
1973
  const SYSVAR_INSTRUCTIONS_PUBKEY = new PublicKey('Sysvar1nstructions1111111111111111111111111');
@@ -2962,9 +2982,37 @@ class Loader {
2962
2982
  programId,
2963
2983
  data
2964
2984
  });
2965
- await sendAndConfirmTransaction(connection, transaction, [payer, program], {
2966
- commitment: 'confirmed'
2985
+ const deployCommitment = 'processed';
2986
+ const finalizeSignature = await connection.sendTransaction(transaction, [payer, program], {
2987
+ preflightCommitment: deployCommitment
2967
2988
  });
2989
+ const {
2990
+ context,
2991
+ value
2992
+ } = await connection.confirmTransaction({
2993
+ signature: finalizeSignature,
2994
+ lastValidBlockHeight: transaction.lastValidBlockHeight,
2995
+ blockhash: transaction.recentBlockhash
2996
+ }, deployCommitment);
2997
+ if (value.err) {
2998
+ throw new Error(`Transaction ${finalizeSignature} failed (${JSON.stringify(value)})`);
2999
+ }
3000
+ // We prevent programs from being usable until the slot after their deployment.
3001
+ // See https://github.com/solana-labs/solana/pull/29654
3002
+ while (true // eslint-disable-line no-constant-condition
3003
+ ) {
3004
+ try {
3005
+ const currentSlot = await connection.getSlot({
3006
+ commitment: deployCommitment
3007
+ });
3008
+ if (currentSlot > context.slot) {
3009
+ break;
3010
+ }
3011
+ } catch {
3012
+ /* empty */
3013
+ }
3014
+ await new Promise(resolve => setTimeout(resolve, Math.round(MS_PER_SLOT / 2)));
3015
+ }
2968
3016
  }
2969
3017
 
2970
3018
  // success
@@ -5737,29 +5785,6 @@ class RpcWebSocketClient extends RpcWebSocketCommonClient {
5737
5785
  }
5738
5786
  }
5739
5787
 
5740
- // TODO: These constants should be removed in favor of reading them out of a
5741
- // Syscall account
5742
-
5743
- /**
5744
- * @internal
5745
- */
5746
- const NUM_TICKS_PER_SECOND = 160;
5747
-
5748
- /**
5749
- * @internal
5750
- */
5751
- const DEFAULT_TICKS_PER_SLOT = 64;
5752
-
5753
- /**
5754
- * @internal
5755
- */
5756
- const NUM_SLOTS_PER_SECOND = NUM_TICKS_PER_SECOND / DEFAULT_TICKS_PER_SLOT;
5757
-
5758
- /**
5759
- * @internal
5760
- */
5761
- const MS_PER_SLOT = 1000 / NUM_SLOTS_PER_SECOND;
5762
-
5763
5788
  /**
5764
5789
  * @internal
5765
5790
  */
@@ -7214,7 +7239,7 @@ class Connection {
7214
7239
  /**
7215
7240
  * Fetch all the token accounts owned by the specified account
7216
7241
  *
7217
- * @return {Promise<RpcResponseAndContext<Array<{pubkey: PublicKey, account: AccountInfo<Buffer>}>>>}
7242
+ * @return {Promise<RpcResponseAndContext<GetProgramAccountsResponse>}
7218
7243
  */
7219
7244
  async getTokenAccountsByOwner(ownerAddress, filter, commitmentOrConfig) {
7220
7245
  const {
@@ -7411,6 +7436,8 @@ class Connection {
7411
7436
  *
7412
7437
  * @return {Promise<Array<{pubkey: PublicKey, account: AccountInfo<Buffer>}>>}
7413
7438
  */
7439
+
7440
+ // eslint-disable-next-line no-dupe-class-members
7414
7441
  async getProgramAccounts(programId, configOrCommitment) {
7415
7442
  const {
7416
7443
  commitment,
@@ -7422,7 +7449,8 @@ class Connection {
7422
7449
  } = config || {};
7423
7450
  const args = this._buildArgs([programId.toBase58()], commitment, encoding || 'base64', configWithoutEncoding);
7424
7451
  const unsafeRes = await this._rpcRequest('getProgramAccounts', args);
7425
- const res = create(unsafeRes, jsonRpcResult(array(KeyedAccountInfoResult)));
7452
+ const baseSchema = array(KeyedAccountInfoResult);
7453
+ const res = configWithoutEncoding.withContext === true ? create(unsafeRes, jsonRpcResultAndContext(baseSchema)) : create(unsafeRes, jsonRpcResult(baseSchema));
7426
7454
  if ('error' in res) {
7427
7455
  throw new SolanaJSONRPCError(res.error, `failed to get accounts owned by program ${programId.toBase58()}`);
7428
7456
  }
@@ -9154,12 +9182,11 @@ class Connection {
9154
9182
  * @internal
9155
9183
  */
9156
9184
  _onSubscriptionStateChange(clientSubscriptionId, callback) {
9157
- var _this$_subscriptionSt;
9158
9185
  const hash = this._subscriptionHashByClientSubscriptionId[clientSubscriptionId];
9159
9186
  if (hash == null) {
9160
9187
  return () => {};
9161
9188
  }
9162
- const stateChangeCallbacks = (_this$_subscriptionSt = this._subscriptionStateChangeCallbacksByHash)[hash] || (_this$_subscriptionSt[hash] = new Set());
9189
+ const stateChangeCallbacks = this._subscriptionStateChangeCallbacksByHash[hash] ||= new Set();
9163
9190
  stateChangeCallbacks.add(callback);
9164
9191
  return () => {
9165
9192
  stateChangeCallbacks.delete(callback);
@@ -10387,17 +10414,10 @@ class Ed25519Program {
10387
10414
  }
10388
10415
  Ed25519Program.programId = new PublicKey('Ed25519SigVerify111111111111111111111111111');
10389
10416
 
10390
- // Supply a synchronous hashing algorithm to make this
10391
- // library interoperable with the synchronous APIs in web3.js.
10392
- secp256k1.utils.hmacSha256Sync = (key, ...msgs) => {
10393
- const h = hmac.create(sha256, key);
10394
- msgs.forEach(msg => h.update(msg));
10395
- return h.digest();
10417
+ const ecdsaSign = (msgHash, privKey) => {
10418
+ const signature = secp256k1.sign(msgHash, privKey);
10419
+ return [signature.toCompactRawBytes(), signature.recovery];
10396
10420
  };
10397
- const ecdsaSign = (msgHash, privKey) => secp256k1.signSync(msgHash, privKey, {
10398
- der: false,
10399
- recovered: true
10400
- });
10401
10421
  secp256k1.utils.isValidPrivateKey;
10402
10422
  const publicKeyCreate = secp256k1.getPublicKey;
10403
10423
 
@@ -11667,7 +11687,7 @@ class VoteProgram {
11667
11687
  }
11668
11688
  }
11669
11689
  VoteProgram.programId = new PublicKey('Vote111111111111111111111111111111111111111');
11670
- VoteProgram.space = 3731;
11690
+ VoteProgram.space = process.env.TEST_LIVE ? 3762 : 3731;
11671
11691
 
11672
11692
  const VALIDATOR_INFO_KEY = new PublicKey('Va1idator1nfo111111111111111111111111111111');
11673
11693