@solana/web3.js 0.0.0-next → 0.0.0-pr-29130

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.
Files changed (74) hide show
  1. package/README.md +24 -25
  2. package/lib/index.browser.cjs.js +4583 -4238
  3. package/lib/index.browser.cjs.js.map +1 -1
  4. package/lib/index.browser.esm.js +4565 -4238
  5. package/lib/index.browser.esm.js.map +1 -1
  6. package/lib/index.cjs.js +7072 -3604
  7. package/lib/index.cjs.js.map +1 -1
  8. package/lib/index.d.ts +3516 -2420
  9. package/lib/index.esm.js +7046 -3601
  10. package/lib/index.esm.js.map +1 -1
  11. package/lib/index.iife.js +22171 -27053
  12. package/lib/index.iife.js.map +1 -1
  13. package/lib/index.iife.min.js +8 -33
  14. package/lib/index.iife.min.js.map +1 -1
  15. package/lib/index.native.js +10407 -0
  16. package/lib/index.native.js.map +1 -0
  17. package/package.json +36 -36
  18. package/src/__forks__/browser/fetch-impl.ts +4 -0
  19. package/src/__forks__/react-native/fetch-impl.ts +4 -0
  20. package/src/account-data.ts +39 -0
  21. package/src/account.ts +20 -11
  22. package/src/bpf-loader.ts +2 -2
  23. package/src/connection.ts +2303 -635
  24. package/src/epoch-schedule.ts +1 -1
  25. package/src/errors.ts +41 -0
  26. package/src/fee-calculator.ts +2 -0
  27. package/src/fetch-impl.ts +13 -0
  28. package/src/index.ts +3 -10
  29. package/src/keypair.ts +20 -25
  30. package/src/layout.ts +45 -4
  31. package/src/loader.ts +3 -3
  32. package/src/message/account-keys.ts +79 -0
  33. package/src/message/compiled-keys.ts +165 -0
  34. package/src/message/index.ts +47 -0
  35. package/src/{message.ts → message/legacy.ts} +95 -40
  36. package/src/message/v0.ts +496 -0
  37. package/src/message/versioned.ts +36 -0
  38. package/src/nonce-account.ts +8 -4
  39. package/src/programs/address-lookup-table/index.ts +435 -0
  40. package/src/programs/address-lookup-table/state.ts +84 -0
  41. package/src/programs/compute-budget.ts +281 -0
  42. package/src/{ed25519-program.ts → programs/ed25519.ts} +6 -6
  43. package/src/programs/index.ts +7 -0
  44. package/src/{secp256k1-program.ts → programs/secp256k1.ts} +15 -16
  45. package/src/{stake-program.ts → programs/stake.ts} +7 -7
  46. package/src/{system-program.ts → programs/system.ts} +55 -18
  47. package/src/{vote-program.ts → programs/vote.ts} +137 -9
  48. package/src/publickey.ts +37 -79
  49. package/src/transaction/constants.ts +12 -0
  50. package/src/transaction/expiry-custom-errors.ts +48 -0
  51. package/src/transaction/index.ts +5 -0
  52. package/src/{transaction.ts → transaction/legacy.ts} +162 -67
  53. package/src/transaction/message.ts +140 -0
  54. package/src/transaction/versioned.ts +126 -0
  55. package/src/{util → utils}/assert.ts +0 -0
  56. package/src/utils/bigint.ts +43 -0
  57. package/src/{util → utils}/borsh-schema.ts +0 -0
  58. package/src/{util → utils}/cluster.ts +0 -0
  59. package/src/utils/ed25519.ts +46 -0
  60. package/src/utils/index.ts +5 -0
  61. package/src/utils/makeWebsocketUrl.ts +26 -0
  62. package/src/{util → utils}/promise-timeout.ts +0 -0
  63. package/src/utils/secp256k1.ts +18 -0
  64. package/src/utils/send-and-confirm-raw-transaction.ts +105 -0
  65. package/src/utils/send-and-confirm-transaction.ts +98 -0
  66. package/src/{util → utils}/shortvec-encoding.ts +0 -0
  67. package/src/{util → utils}/sleep.ts +0 -0
  68. package/src/{util → utils}/to-buffer.ts +0 -0
  69. package/src/validator-info.ts +4 -6
  70. package/src/vote-account.ts +1 -1
  71. package/src/agent-manager.ts +0 -44
  72. package/src/util/send-and-confirm-raw-transaction.ts +0 -46
  73. package/src/util/send-and-confirm-transaction.ts +0 -50
  74. package/src/util/url.ts +0 -18
@@ -0,0 +1,46 @@
1
+ import {sha512} from '@noble/hashes/sha512';
2
+ import * as ed25519 from '@noble/ed25519';
3
+
4
+ /**
5
+ * A 64 byte secret key, the first 32 bytes of which is the
6
+ * private scalar and the last 32 bytes is the public key.
7
+ * Read more: https://blog.mozilla.org/warner/2011/11/29/ed25519-keys/
8
+ */
9
+ type Ed25519SecretKey = Uint8Array;
10
+
11
+ /**
12
+ * Ed25519 Keypair
13
+ */
14
+ export interface Ed25519Keypair {
15
+ publicKey: Uint8Array;
16
+ secretKey: Ed25519SecretKey;
17
+ }
18
+
19
+ ed25519.utils.sha512Sync = (...m) => sha512(ed25519.utils.concatBytes(...m));
20
+
21
+ export const generatePrivateKey = ed25519.utils.randomPrivateKey;
22
+ export const generateKeypair = (): Ed25519Keypair => {
23
+ const privateScalar = ed25519.utils.randomPrivateKey();
24
+ const publicKey = getPublicKey(privateScalar);
25
+ const secretKey = new Uint8Array(64);
26
+ secretKey.set(privateScalar);
27
+ secretKey.set(publicKey, 32);
28
+ return {
29
+ publicKey,
30
+ secretKey,
31
+ };
32
+ };
33
+ export const getPublicKey = ed25519.sync.getPublicKey;
34
+ export function isOnCurve(publicKey: Uint8Array): boolean {
35
+ try {
36
+ ed25519.Point.fromHex(publicKey, true /* strict */);
37
+ return true;
38
+ } catch {
39
+ return false;
40
+ }
41
+ }
42
+ export const sign = (
43
+ message: Parameters<typeof ed25519.sync.sign>[0],
44
+ secretKey: Ed25519SecretKey,
45
+ ) => ed25519.sync.sign(message, secretKey.slice(0, 32));
46
+ export const verify = ed25519.sync.verify;
@@ -0,0 +1,5 @@
1
+ export * from './borsh-schema';
2
+ export * from './cluster';
3
+ export type {Ed25519Keypair} from './ed25519';
4
+ export * from './send-and-confirm-raw-transaction';
5
+ export * from './send-and-confirm-transaction';
@@ -0,0 +1,26 @@
1
+ const URL_RE = /^[^:]+:\/\/([^:[]+|\[[^\]]+\])(:\d+)?(.*)/i;
2
+
3
+ export function makeWebsocketUrl(endpoint: string) {
4
+ const matches = endpoint.match(URL_RE);
5
+ if (matches == null) {
6
+ throw TypeError(`Failed to validate endpoint URL \`${endpoint}\``);
7
+ }
8
+ const [
9
+ _, // eslint-disable-line @typescript-eslint/no-unused-vars
10
+ hostish,
11
+ portWithColon,
12
+ rest,
13
+ ] = matches;
14
+ const protocol = endpoint.startsWith('https:') ? 'wss:' : 'ws:';
15
+ const startPort =
16
+ portWithColon == null ? null : parseInt(portWithColon.slice(1), 10);
17
+ const websocketPort =
18
+ // Only shift the port by +1 as a convention for ws(s) only if given endpoint
19
+ // is explictly specifying the endpoint port (HTTP-based RPC), assuming
20
+ // we're directly trying to connect to solana-validator's ws listening port.
21
+ // When the endpoint omits the port, we're connecting to the protocol
22
+ // default ports: http(80) or https(443) and it's assumed we're behind a reverse
23
+ // proxy which manages WebSocket upgrade and backend port redirection.
24
+ startPort == null ? '' : `:${startPort + 1}`;
25
+ return `${protocol}//${hostish}${websocketPort}${rest}`;
26
+ }
File without changes
@@ -0,0 +1,18 @@
1
+ import {hmac} from '@noble/hashes/hmac';
2
+ import {sha256} from '@noble/hashes/sha256';
3
+ import * as secp256k1 from '@noble/secp256k1';
4
+
5
+ // Supply a synchronous hashing algorithm to make this
6
+ // library interoperable with the synchronous APIs in web3.js.
7
+ secp256k1.utils.hmacSha256Sync = (key: Uint8Array, ...msgs: Uint8Array[]) => {
8
+ const h = hmac.create(sha256, key);
9
+ msgs.forEach(msg => h.update(msg));
10
+ return h.digest();
11
+ };
12
+
13
+ export const ecdsaSign = (
14
+ msgHash: Parameters<typeof secp256k1.signSync>[0],
15
+ privKey: Parameters<typeof secp256k1.signSync>[1],
16
+ ) => secp256k1.signSync(msgHash, privKey, {der: false, recovered: true});
17
+ export const isValidPrivateKey = secp256k1.utils.isValidPrivateKey;
18
+ export const publicKeyCreate = secp256k1.getPublicKey;
@@ -0,0 +1,105 @@
1
+ import type {Buffer} from 'buffer';
2
+
3
+ import {
4
+ BlockheightBasedTransactionConfirmationStrategy,
5
+ Connection,
6
+ DurableNonceTransactionConfirmationStrategy,
7
+ } from '../connection';
8
+ import type {TransactionSignature} from '../transaction';
9
+ import type {ConfirmOptions} from '../connection';
10
+
11
+ /**
12
+ * Send and confirm a raw transaction
13
+ *
14
+ * If `commitment` option is not specified, defaults to 'max' commitment.
15
+ *
16
+ * @param {Connection} connection
17
+ * @param {Buffer} rawTransaction
18
+ * @param {BlockheightBasedTransactionConfirmationStrategy} confirmationStrategy
19
+ * @param {ConfirmOptions} [options]
20
+ * @returns {Promise<TransactionSignature>}
21
+ */
22
+ export async function sendAndConfirmRawTransaction(
23
+ connection: Connection,
24
+ rawTransaction: Buffer,
25
+ confirmationStrategy: BlockheightBasedTransactionConfirmationStrategy,
26
+ options?: ConfirmOptions,
27
+ ): Promise<TransactionSignature>;
28
+
29
+ /**
30
+ * @deprecated Calling `sendAndConfirmRawTransaction()` without a `confirmationStrategy`
31
+ * is no longer supported and will be removed in a future version.
32
+ */
33
+ // eslint-disable-next-line no-redeclare
34
+ export async function sendAndConfirmRawTransaction(
35
+ connection: Connection,
36
+ rawTransaction: Buffer,
37
+ options?: ConfirmOptions,
38
+ ): Promise<TransactionSignature>;
39
+
40
+ // eslint-disable-next-line no-redeclare
41
+ export async function sendAndConfirmRawTransaction(
42
+ connection: Connection,
43
+ rawTransaction: Buffer,
44
+ confirmationStrategyOrConfirmOptions:
45
+ | BlockheightBasedTransactionConfirmationStrategy
46
+ | DurableNonceTransactionConfirmationStrategy
47
+ | ConfirmOptions
48
+ | undefined,
49
+ maybeConfirmOptions?: ConfirmOptions,
50
+ ): Promise<TransactionSignature> {
51
+ let confirmationStrategy:
52
+ | BlockheightBasedTransactionConfirmationStrategy
53
+ | DurableNonceTransactionConfirmationStrategy
54
+ | undefined;
55
+ let options: ConfirmOptions | undefined;
56
+ if (
57
+ confirmationStrategyOrConfirmOptions &&
58
+ Object.prototype.hasOwnProperty.call(
59
+ confirmationStrategyOrConfirmOptions,
60
+ 'lastValidBlockHeight',
61
+ )
62
+ ) {
63
+ confirmationStrategy =
64
+ confirmationStrategyOrConfirmOptions as BlockheightBasedTransactionConfirmationStrategy;
65
+ options = maybeConfirmOptions;
66
+ } else if (
67
+ confirmationStrategyOrConfirmOptions &&
68
+ Object.prototype.hasOwnProperty.call(
69
+ confirmationStrategyOrConfirmOptions,
70
+ 'nonceValue',
71
+ )
72
+ ) {
73
+ confirmationStrategy =
74
+ confirmationStrategyOrConfirmOptions as DurableNonceTransactionConfirmationStrategy;
75
+ options = maybeConfirmOptions;
76
+ } else {
77
+ options = confirmationStrategyOrConfirmOptions as
78
+ | ConfirmOptions
79
+ | undefined;
80
+ }
81
+ const sendOptions = options && {
82
+ skipPreflight: options.skipPreflight,
83
+ preflightCommitment: options.preflightCommitment || options.commitment,
84
+ minContextSlot: options.minContextSlot,
85
+ };
86
+
87
+ const signature = await connection.sendRawTransaction(
88
+ rawTransaction,
89
+ sendOptions,
90
+ );
91
+
92
+ const commitment = options && options.commitment;
93
+ const confirmationPromise = confirmationStrategy
94
+ ? connection.confirmTransaction(confirmationStrategy, commitment)
95
+ : connection.confirmTransaction(signature, commitment);
96
+ const status = (await confirmationPromise).value;
97
+
98
+ if (status.err) {
99
+ throw new Error(
100
+ `Raw transaction ${signature} failed (${JSON.stringify(status)})`,
101
+ );
102
+ }
103
+
104
+ return signature;
105
+ }
@@ -0,0 +1,98 @@
1
+ import {Connection, SignatureResult} from '../connection';
2
+ import {Transaction} from '../transaction';
3
+ import type {ConfirmOptions} from '../connection';
4
+ import type {Signer} from '../keypair';
5
+ import type {TransactionSignature} from '../transaction';
6
+
7
+ /**
8
+ * Sign, send and confirm a transaction.
9
+ *
10
+ * If `commitment` option is not specified, defaults to 'max' commitment.
11
+ *
12
+ * @param {Connection} connection
13
+ * @param {Transaction} transaction
14
+ * @param {Array<Signer>} signers
15
+ * @param {ConfirmOptions} [options]
16
+ * @returns {Promise<TransactionSignature>}
17
+ */
18
+ export async function sendAndConfirmTransaction(
19
+ connection: Connection,
20
+ transaction: Transaction,
21
+ signers: Array<Signer>,
22
+ options?: ConfirmOptions &
23
+ Readonly<{
24
+ // A signal that, when aborted, cancels any outstanding transaction confirmation operations
25
+ abortSignal?: AbortSignal;
26
+ }>,
27
+ ): Promise<TransactionSignature> {
28
+ const sendOptions = options && {
29
+ skipPreflight: options.skipPreflight,
30
+ preflightCommitment: options.preflightCommitment || options.commitment,
31
+ maxRetries: options.maxRetries,
32
+ minContextSlot: options.minContextSlot,
33
+ };
34
+
35
+ const signature = await connection.sendTransaction(
36
+ transaction,
37
+ signers,
38
+ sendOptions,
39
+ );
40
+
41
+ let status: SignatureResult;
42
+ if (
43
+ transaction.recentBlockhash != null &&
44
+ transaction.lastValidBlockHeight != null
45
+ ) {
46
+ status = (
47
+ await connection.confirmTransaction(
48
+ {
49
+ abortSignal: options?.abortSignal,
50
+ signature: signature,
51
+ blockhash: transaction.recentBlockhash,
52
+ lastValidBlockHeight: transaction.lastValidBlockHeight,
53
+ },
54
+ options && options.commitment,
55
+ )
56
+ ).value;
57
+ } else if (
58
+ transaction.minNonceContextSlot != null &&
59
+ transaction.nonceInfo != null
60
+ ) {
61
+ const {nonceInstruction} = transaction.nonceInfo;
62
+ const nonceAccountPubkey = nonceInstruction.keys[0].pubkey;
63
+ status = (
64
+ await connection.confirmTransaction(
65
+ {
66
+ abortSignal: options?.abortSignal,
67
+ minContextSlot: transaction.minNonceContextSlot,
68
+ nonceAccountPubkey,
69
+ nonceValue: transaction.nonceInfo.nonce,
70
+ signature,
71
+ },
72
+ options && options.commitment,
73
+ )
74
+ ).value;
75
+ } else {
76
+ if (options?.abortSignal != null) {
77
+ console.warn(
78
+ 'sendAndConfirmTransaction(): A transaction with a deprecated confirmation strategy was ' +
79
+ 'supplied along with an `abortSignal`. Only transactions having `lastValidBlockHeight` ' +
80
+ 'or a combination of `nonceInfo` and `minNonceContextSlot` are abortable.',
81
+ );
82
+ }
83
+ status = (
84
+ await connection.confirmTransaction(
85
+ signature,
86
+ options && options.commitment,
87
+ )
88
+ ).value;
89
+ }
90
+
91
+ if (status.err) {
92
+ throw new Error(
93
+ `Transaction ${signature} failed (${JSON.stringify(status)})`,
94
+ );
95
+ }
96
+
97
+ return signature;
98
+ }
File without changes
File without changes
File without changes
@@ -7,8 +7,8 @@ import {
7
7
  } from 'superstruct';
8
8
 
9
9
  import * as Layout from './layout';
10
- import * as shortvec from './util/shortvec-encoding';
11
- import {PublicKey} from './publickey';
10
+ import * as shortvec from './utils/shortvec-encoding';
11
+ import {PublicKey, PUBLIC_KEY_LENGTH} from './publickey';
12
12
 
13
13
  export const VALIDATOR_INFO_KEY = new PublicKey(
14
14
  'Va1idator1nfo111111111111111111111111111111',
@@ -77,16 +77,14 @@ export class ValidatorInfo {
77
77
  static fromConfigData(
78
78
  buffer: Buffer | Uint8Array | Array<number>,
79
79
  ): ValidatorInfo | null {
80
- const PUBKEY_LENGTH = 32;
81
-
82
80
  let byteArray = [...buffer];
83
81
  const configKeyCount = shortvec.decodeLength(byteArray);
84
82
  if (configKeyCount !== 2) return null;
85
83
 
86
84
  const configKeys: Array<ConfigKey> = [];
87
85
  for (let i = 0; i < 2; i++) {
88
- const publicKey = new PublicKey(byteArray.slice(0, PUBKEY_LENGTH));
89
- byteArray = byteArray.slice(PUBKEY_LENGTH);
86
+ const publicKey = new PublicKey(byteArray.slice(0, PUBLIC_KEY_LENGTH));
87
+ byteArray = byteArray.slice(PUBLIC_KEY_LENGTH);
90
88
  const isSigner = byteArray.slice(0, 1)[0] === 1;
91
89
  byteArray = byteArray.slice(1);
92
90
  configKeys.push({publicKey, isSigner});
@@ -3,7 +3,7 @@ import type {Buffer} from 'buffer';
3
3
 
4
4
  import * as Layout from './layout';
5
5
  import {PublicKey} from './publickey';
6
- import {toBuffer} from './util/to-buffer';
6
+ import {toBuffer} from './utils/to-buffer';
7
7
 
8
8
  export const VOTE_PROGRAM_ID = new PublicKey(
9
9
  'Vote111111111111111111111111111111111111111',
@@ -1,44 +0,0 @@
1
- import http from 'http';
2
- import https from 'https';
3
-
4
- export const DESTROY_TIMEOUT_MS = 5000;
5
-
6
- export class AgentManager {
7
- _agent: http.Agent | https.Agent;
8
- _activeRequests = 0;
9
- _destroyTimeout: ReturnType<typeof setTimeout> | null = null;
10
- _useHttps: boolean;
11
-
12
- static _newAgent(useHttps: boolean): http.Agent | https.Agent {
13
- const options = {keepAlive: true, maxSockets: 25};
14
- if (useHttps) {
15
- return new https.Agent(options);
16
- } else {
17
- return new http.Agent(options);
18
- }
19
- }
20
-
21
- constructor(useHttps?: boolean) {
22
- this._useHttps = useHttps === true;
23
- this._agent = AgentManager._newAgent(this._useHttps);
24
- }
25
-
26
- requestStart(): http.Agent | https.Agent {
27
- this._activeRequests++;
28
- if (this._destroyTimeout !== null) {
29
- clearTimeout(this._destroyTimeout);
30
- this._destroyTimeout = null;
31
- }
32
- return this._agent;
33
- }
34
-
35
- requestEnd() {
36
- this._activeRequests--;
37
- if (this._activeRequests === 0 && this._destroyTimeout === null) {
38
- this._destroyTimeout = setTimeout(() => {
39
- this._agent.destroy();
40
- this._agent = AgentManager._newAgent(this._useHttps);
41
- }, DESTROY_TIMEOUT_MS);
42
- }
43
- }
44
- }
@@ -1,46 +0,0 @@
1
- import type {Buffer} from 'buffer';
2
-
3
- import {Connection} from '../connection';
4
- import type {TransactionSignature} from '../transaction';
5
- import type {ConfirmOptions} from '../connection';
6
-
7
- /**
8
- * Send and confirm a raw transaction
9
- *
10
- * If `commitment` option is not specified, defaults to 'max' commitment.
11
- *
12
- * @param {Connection} connection
13
- * @param {Buffer} rawTransaction
14
- * @param {ConfirmOptions} [options]
15
- * @returns {Promise<TransactionSignature>}
16
- */
17
- export async function sendAndConfirmRawTransaction(
18
- connection: Connection,
19
- rawTransaction: Buffer,
20
- options?: ConfirmOptions,
21
- ): Promise<TransactionSignature> {
22
- const sendOptions = options && {
23
- skipPreflight: options.skipPreflight,
24
- preflightCommitment: options.preflightCommitment || options.commitment,
25
- };
26
-
27
- const signature = await connection.sendRawTransaction(
28
- rawTransaction,
29
- sendOptions,
30
- );
31
-
32
- const status = (
33
- await connection.confirmTransaction(
34
- signature,
35
- options && options.commitment,
36
- )
37
- ).value;
38
-
39
- if (status.err) {
40
- throw new Error(
41
- `Raw transaction ${signature} failed (${JSON.stringify(status)})`,
42
- );
43
- }
44
-
45
- return signature;
46
- }
@@ -1,50 +0,0 @@
1
- import {Connection} from '../connection';
2
- import {Transaction} from '../transaction';
3
- import type {ConfirmOptions} from '../connection';
4
- import type {Signer} from '../keypair';
5
- import type {TransactionSignature} from '../transaction';
6
-
7
- /**
8
- * Sign, send and confirm a transaction.
9
- *
10
- * If `commitment` option is not specified, defaults to 'max' commitment.
11
- *
12
- * @param {Connection} connection
13
- * @param {Transaction} transaction
14
- * @param {Array<Signer>} signers
15
- * @param {ConfirmOptions} [options]
16
- * @returns {Promise<TransactionSignature>}
17
- */
18
- export async function sendAndConfirmTransaction(
19
- connection: Connection,
20
- transaction: Transaction,
21
- signers: Array<Signer>,
22
- options?: ConfirmOptions,
23
- ): Promise<TransactionSignature> {
24
- const sendOptions = options && {
25
- skipPreflight: options.skipPreflight,
26
- preflightCommitment: options.preflightCommitment || options.commitment,
27
- maxRetries: options.maxRetries,
28
- };
29
-
30
- const signature = await connection.sendTransaction(
31
- transaction,
32
- signers,
33
- sendOptions,
34
- );
35
-
36
- const status = (
37
- await connection.confirmTransaction(
38
- signature,
39
- options && options.commitment,
40
- )
41
- ).value;
42
-
43
- if (status.err) {
44
- throw new Error(
45
- `Transaction ${signature} failed (${JSON.stringify(status)})`,
46
- );
47
- }
48
-
49
- return signature;
50
- }
package/src/util/url.ts DELETED
@@ -1,18 +0,0 @@
1
- export function makeWebsocketUrl(endpoint: string) {
2
- let url = new URL(endpoint);
3
- const useHttps = url.protocol === 'https:';
4
-
5
- url.protocol = useHttps ? 'wss:' : 'ws:';
6
- url.host = '';
7
-
8
- // Only shift the port by +1 as a convention for ws(s) only if given endpoint
9
- // is explictly specifying the endpoint port (HTTP-based RPC), assuming
10
- // we're directly trying to connect to solana-validator's ws listening port.
11
- // When the endpoint omits the port, we're connecting to the protocol
12
- // default ports: http(80) or https(443) and it's assumed we're behind a reverse
13
- // proxy which manages WebSocket upgrade and backend port redirection.
14
- if (url.port !== '') {
15
- url.port = String(Number(url.port) + 1);
16
- }
17
- return url.toString();
18
- }