solana-web3-stable 1.0.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.
Files changed (78) hide show
  1. package/LICENSE +20 -0
  2. package/README.md +22 -0
  3. package/lib/index.browser.cjs.js +10564 -0
  4. package/lib/index.browser.cjs.js.map +1 -0
  5. package/lib/index.browser.esm.js +10463 -0
  6. package/lib/index.browser.esm.js.map +1 -0
  7. package/lib/index.cjs.js +11351 -0
  8. package/lib/index.cjs.js.map +1 -0
  9. package/lib/index.d.ts +4025 -0
  10. package/lib/index.esm.js +11246 -0
  11. package/lib/index.esm.js.map +1 -0
  12. package/lib/index.iife.js +26085 -0
  13. package/lib/index.iife.js.map +1 -0
  14. package/lib/index.iife.min.js +20 -0
  15. package/lib/index.iife.min.js.map +1 -0
  16. package/lib/index.native.js +10564 -0
  17. package/lib/index.native.js.map +1 -0
  18. package/package.json +87 -0
  19. package/src/__forks__/browser/fetch-impl.ts +4 -0
  20. package/src/__forks__/react-native/fetch-impl.ts +4 -0
  21. package/src/account-data.ts +39 -0
  22. package/src/account.ts +55 -0
  23. package/src/blockhash.ts +4 -0
  24. package/src/bpf-loader-deprecated.ts +5 -0
  25. package/src/bpf-loader.ts +50 -0
  26. package/src/connection.ts +6961 -0
  27. package/src/epoch-schedule.ts +102 -0
  28. package/src/errors.ts +133 -0
  29. package/src/fee-calculator.ts +18 -0
  30. package/src/fetch-impl.ts +16 -0
  31. package/src/index.ts +24 -0
  32. package/src/instruction.ts +58 -0
  33. package/src/keypair.ts +102 -0
  34. package/src/layout.ts +188 -0
  35. package/src/loader.ts +267 -0
  36. package/src/message/account-keys.ts +79 -0
  37. package/src/message/compiled-keys.ts +165 -0
  38. package/src/message/index.ts +47 -0
  39. package/src/message/legacy.ts +323 -0
  40. package/src/message/v0.ts +513 -0
  41. package/src/message/versioned.ts +36 -0
  42. package/src/nonce-account.ts +82 -0
  43. package/src/programs/address-lookup-table/index.ts +438 -0
  44. package/src/programs/address-lookup-table/state.ts +84 -0
  45. package/src/programs/compute-budget.ts +281 -0
  46. package/src/programs/ed25519.ts +157 -0
  47. package/src/programs/index.ts +7 -0
  48. package/src/programs/secp256k1.ts +228 -0
  49. package/src/programs/stake.ts +952 -0
  50. package/src/programs/system.ts +1048 -0
  51. package/src/programs/vote.ts +586 -0
  52. package/src/publickey.ts +259 -0
  53. package/src/rpc-websocket.ts +75 -0
  54. package/src/sysvar.ts +37 -0
  55. package/src/timing.ts +23 -0
  56. package/src/transaction/constants.ts +12 -0
  57. package/src/transaction/expiry-custom-errors.ts +48 -0
  58. package/src/transaction/index.ts +5 -0
  59. package/src/transaction/legacy.ts +970 -0
  60. package/src/transaction/message.ts +140 -0
  61. package/src/transaction/versioned.ts +127 -0
  62. package/src/utils/assert.ts +8 -0
  63. package/src/utils/bigint.ts +24 -0
  64. package/src/utils/borsh-schema.ts +38 -0
  65. package/src/utils/cluster.ts +35 -0
  66. package/src/utils/ed25519.ts +43 -0
  67. package/src/utils/guarded-array-utils.ts +34 -0
  68. package/src/utils/index.ts +5 -0
  69. package/src/utils/makeWebsocketUrl.ts +26 -0
  70. package/src/utils/promise-timeout.ts +14 -0
  71. package/src/utils/secp256k1.ts +11 -0
  72. package/src/utils/send-and-confirm-raw-transaction.ts +110 -0
  73. package/src/utils/send-and-confirm-transaction.ts +106 -0
  74. package/src/utils/shortvec-encoding.ts +28 -0
  75. package/src/utils/sleep.ts +4 -0
  76. package/src/utils/to-buffer.ts +11 -0
  77. package/src/validator-info.ts +108 -0
  78. package/src/vote-account.ts +236 -0
@@ -0,0 +1,106 @@
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
+ import {SendTransactionError} from '../errors';
7
+
8
+ /**
9
+ * Sign, send and confirm a transaction.
10
+ *
11
+ * If `commitment` option is not specified, defaults to 'max' commitment.
12
+ *
13
+ * @param {Connection} connection
14
+ * @param {Transaction} transaction
15
+ * @param {Array<Signer>} signers
16
+ * @param {ConfirmOptions} [options]
17
+ * @returns {Promise<TransactionSignature>}
18
+ */
19
+ export async function sendAndConfirmTransaction(
20
+ connection: Connection,
21
+ transaction: Transaction,
22
+ signers: Array<Signer>,
23
+ options?: ConfirmOptions &
24
+ Readonly<{
25
+ // A signal that, when aborted, cancels any outstanding transaction confirmation operations
26
+ abortSignal?: AbortSignal;
27
+ }>,
28
+ ): Promise<TransactionSignature> {
29
+ const sendOptions = options && {
30
+ skipPreflight: options.skipPreflight,
31
+ preflightCommitment: options.preflightCommitment || options.commitment,
32
+ maxRetries: options.maxRetries,
33
+ minContextSlot: options.minContextSlot,
34
+ };
35
+
36
+ const signature = await connection.sendTransaction(
37
+ transaction,
38
+ signers,
39
+ sendOptions,
40
+ );
41
+
42
+ let status: SignatureResult;
43
+ if (
44
+ transaction.recentBlockhash != null &&
45
+ transaction.lastValidBlockHeight != null
46
+ ) {
47
+ status = (
48
+ await connection.confirmTransaction(
49
+ {
50
+ abortSignal: options?.abortSignal,
51
+ signature: signature,
52
+ blockhash: transaction.recentBlockhash,
53
+ lastValidBlockHeight: transaction.lastValidBlockHeight,
54
+ },
55
+ options && options.commitment,
56
+ )
57
+ ).value;
58
+ } else if (
59
+ transaction.minNonceContextSlot != null &&
60
+ transaction.nonceInfo != null
61
+ ) {
62
+ const {nonceInstruction} = transaction.nonceInfo;
63
+ const nonceAccountPubkey = nonceInstruction.keys[0].pubkey;
64
+ status = (
65
+ await connection.confirmTransaction(
66
+ {
67
+ abortSignal: options?.abortSignal,
68
+ minContextSlot: transaction.minNonceContextSlot,
69
+ nonceAccountPubkey,
70
+ nonceValue: transaction.nonceInfo.nonce,
71
+ signature,
72
+ },
73
+ options && options.commitment,
74
+ )
75
+ ).value;
76
+ } else {
77
+ if (options?.abortSignal != null) {
78
+ console.warn(
79
+ 'sendAndConfirmTransaction(): A transaction with a deprecated confirmation strategy was ' +
80
+ 'supplied along with an `abortSignal`. Only transactions having `lastValidBlockHeight` ' +
81
+ 'or a combination of `nonceInfo` and `minNonceContextSlot` are abortable.',
82
+ );
83
+ }
84
+ status = (
85
+ await connection.confirmTransaction(
86
+ signature,
87
+ options && options.commitment,
88
+ )
89
+ ).value;
90
+ }
91
+
92
+ if (status.err) {
93
+ if (signature != null) {
94
+ throw new SendTransactionError({
95
+ action: 'send',
96
+ signature: signature,
97
+ transactionMessage: `Status: (${JSON.stringify(status)})`,
98
+ });
99
+ }
100
+ throw new Error(
101
+ `Transaction ${signature} failed (${JSON.stringify(status)})`,
102
+ );
103
+ }
104
+
105
+ return signature;
106
+ }
@@ -0,0 +1,28 @@
1
+ export function decodeLength(bytes: Array<number>): number {
2
+ let len = 0;
3
+ let size = 0;
4
+ for (;;) {
5
+ let elem = bytes.shift() as number;
6
+ len |= (elem & 0x7f) << (size * 7);
7
+ size += 1;
8
+ if ((elem & 0x80) === 0) {
9
+ break;
10
+ }
11
+ }
12
+ return len;
13
+ }
14
+
15
+ export function encodeLength(bytes: Array<number>, len: number) {
16
+ let rem_len = len;
17
+ for (;;) {
18
+ let elem = rem_len & 0x7f;
19
+ rem_len >>= 7;
20
+ if (rem_len == 0) {
21
+ bytes.push(elem);
22
+ break;
23
+ } else {
24
+ elem |= 0x80;
25
+ bytes.push(elem);
26
+ }
27
+ }
28
+ }
@@ -0,0 +1,4 @@
1
+ // zzz
2
+ export function sleep(ms: number): Promise<void> {
3
+ return new Promise(resolve => setTimeout(resolve, ms));
4
+ }
@@ -0,0 +1,11 @@
1
+ import {Buffer} from 'buffer';
2
+
3
+ export const toBuffer = (arr: Buffer | Uint8Array | Array<number>): Buffer => {
4
+ if (Buffer.isBuffer(arr)) {
5
+ return arr;
6
+ } else if (arr instanceof Uint8Array) {
7
+ return Buffer.from(arr.buffer, arr.byteOffset, arr.byteLength);
8
+ } else {
9
+ return Buffer.from(arr);
10
+ }
11
+ };
@@ -0,0 +1,108 @@
1
+ import {Buffer} from 'buffer';
2
+ import {
3
+ assert as assertType,
4
+ optional,
5
+ string,
6
+ type as pick,
7
+ } from 'superstruct';
8
+
9
+ import * as Layout from './layout';
10
+ import * as shortvec from './utils/shortvec-encoding';
11
+ import {PublicKey, PUBLIC_KEY_LENGTH} from './publickey';
12
+ import {guardedShift, guardedSplice} from './utils/guarded-array-utils';
13
+
14
+ export const VALIDATOR_INFO_KEY = new PublicKey(
15
+ 'Va1idator1nfo111111111111111111111111111111',
16
+ );
17
+
18
+ /**
19
+ * @internal
20
+ */
21
+ type ConfigKey = {
22
+ publicKey: PublicKey;
23
+ isSigner: boolean;
24
+ };
25
+
26
+ /**
27
+ * Info used to identity validators.
28
+ */
29
+ export type Info = {
30
+ /** validator name */
31
+ name: string;
32
+ /** optional, validator website */
33
+ website?: string;
34
+ /** optional, extra information the validator chose to share */
35
+ details?: string;
36
+ /** optional, validator logo URL */
37
+ iconUrl?: string;
38
+ /** optional, used to identify validators on keybase.io */
39
+ keybaseUsername?: string;
40
+ };
41
+
42
+ const InfoString = pick({
43
+ name: string(),
44
+ website: optional(string()),
45
+ details: optional(string()),
46
+ iconUrl: optional(string()),
47
+ keybaseUsername: optional(string()),
48
+ });
49
+
50
+ /**
51
+ * ValidatorInfo class
52
+ */
53
+ export class ValidatorInfo {
54
+ /**
55
+ * validator public key
56
+ */
57
+ key: PublicKey;
58
+ /**
59
+ * validator information
60
+ */
61
+ info: Info;
62
+
63
+ /**
64
+ * Construct a valid ValidatorInfo
65
+ *
66
+ * @param key validator public key
67
+ * @param info validator information
68
+ */
69
+ constructor(key: PublicKey, info: Info) {
70
+ this.key = key;
71
+ this.info = info;
72
+ }
73
+
74
+ /**
75
+ * Deserialize ValidatorInfo from the config account data. Exactly two config
76
+ * keys are required in the data.
77
+ *
78
+ * @param buffer config account data
79
+ * @return null if info was not found
80
+ */
81
+ static fromConfigData(
82
+ buffer: Buffer | Uint8Array | Array<number>,
83
+ ): ValidatorInfo | null {
84
+ let byteArray = [...buffer];
85
+ const configKeyCount = shortvec.decodeLength(byteArray);
86
+ if (configKeyCount !== 2) return null;
87
+
88
+ const configKeys: Array<ConfigKey> = [];
89
+ for (let i = 0; i < 2; i++) {
90
+ const publicKey = new PublicKey(
91
+ guardedSplice(byteArray, 0, PUBLIC_KEY_LENGTH),
92
+ );
93
+ const isSigner = guardedShift(byteArray) === 1;
94
+ configKeys.push({publicKey, isSigner});
95
+ }
96
+
97
+ if (configKeys[0].publicKey.equals(VALIDATOR_INFO_KEY)) {
98
+ if (configKeys[1].isSigner) {
99
+ const rawInfo: any = Layout.rustString().decode(Buffer.from(byteArray));
100
+ const info = JSON.parse(rawInfo as string);
101
+ assertType(info, InfoString);
102
+ return new ValidatorInfo(configKeys[1].publicKey, info);
103
+ }
104
+ }
105
+
106
+ return null;
107
+ }
108
+ }
@@ -0,0 +1,236 @@
1
+ import * as BufferLayout from '@solana/buffer-layout';
2
+ import type {Buffer} from 'buffer';
3
+
4
+ import * as Layout from './layout';
5
+ import {PublicKey} from './publickey';
6
+ import {toBuffer} from './utils/to-buffer';
7
+
8
+ export const VOTE_PROGRAM_ID = new PublicKey(
9
+ 'Vote111111111111111111111111111111111111111',
10
+ );
11
+
12
+ export type Lockout = {
13
+ slot: number;
14
+ confirmationCount: number;
15
+ };
16
+
17
+ /**
18
+ * History of how many credits earned by the end of each epoch
19
+ */
20
+ export type EpochCredits = Readonly<{
21
+ epoch: number;
22
+ credits: number;
23
+ prevCredits: number;
24
+ }>;
25
+
26
+ export type AuthorizedVoter = Readonly<{
27
+ epoch: number;
28
+ authorizedVoter: PublicKey;
29
+ }>;
30
+
31
+ type AuthorizedVoterRaw = Readonly<{
32
+ authorizedVoter: Uint8Array;
33
+ epoch: number;
34
+ }>;
35
+
36
+ type PriorVoters = Readonly<{
37
+ buf: PriorVoterRaw[];
38
+ idx: number;
39
+ isEmpty: number;
40
+ }>;
41
+
42
+ export type PriorVoter = Readonly<{
43
+ authorizedPubkey: PublicKey;
44
+ epochOfLastAuthorizedSwitch: number;
45
+ targetEpoch: number;
46
+ }>;
47
+
48
+ type PriorVoterRaw = Readonly<{
49
+ authorizedPubkey: Uint8Array;
50
+ epochOfLastAuthorizedSwitch: number;
51
+ targetEpoch: number;
52
+ }>;
53
+
54
+ export type BlockTimestamp = Readonly<{
55
+ slot: number;
56
+ timestamp: number;
57
+ }>;
58
+
59
+ type VoteAccountData = Readonly<{
60
+ authorizedVoters: AuthorizedVoterRaw[];
61
+ authorizedWithdrawer: Uint8Array;
62
+ commission: number;
63
+ epochCredits: EpochCredits[];
64
+ lastTimestamp: BlockTimestamp;
65
+ nodePubkey: Uint8Array;
66
+ priorVoters: PriorVoters;
67
+ rootSlot: number;
68
+ rootSlotValid: number;
69
+ votes: Lockout[];
70
+ }>;
71
+
72
+ /**
73
+ * See https://github.com/solana-labs/solana/blob/8a12ed029cfa38d4a45400916c2463fb82bbec8c/programs/vote_api/src/vote_state.rs#L68-L88
74
+ *
75
+ * @internal
76
+ */
77
+ const VoteAccountLayout = BufferLayout.struct<VoteAccountData>([
78
+ Layout.publicKey('nodePubkey'),
79
+ Layout.publicKey('authorizedWithdrawer'),
80
+ BufferLayout.u8('commission'),
81
+ BufferLayout.nu64(), // votes.length
82
+ BufferLayout.seq<Lockout>(
83
+ BufferLayout.struct([
84
+ BufferLayout.nu64('slot'),
85
+ BufferLayout.u32('confirmationCount'),
86
+ ]),
87
+ BufferLayout.offset(BufferLayout.u32(), -8),
88
+ 'votes',
89
+ ),
90
+ BufferLayout.u8('rootSlotValid'),
91
+ BufferLayout.nu64('rootSlot'),
92
+ BufferLayout.nu64(), // authorizedVoters.length
93
+ BufferLayout.seq<AuthorizedVoterRaw>(
94
+ BufferLayout.struct([
95
+ BufferLayout.nu64('epoch'),
96
+ Layout.publicKey('authorizedVoter'),
97
+ ]),
98
+ BufferLayout.offset(BufferLayout.u32(), -8),
99
+ 'authorizedVoters',
100
+ ),
101
+ BufferLayout.struct<PriorVoters>(
102
+ [
103
+ BufferLayout.seq(
104
+ BufferLayout.struct([
105
+ Layout.publicKey('authorizedPubkey'),
106
+ BufferLayout.nu64('epochOfLastAuthorizedSwitch'),
107
+ BufferLayout.nu64('targetEpoch'),
108
+ ]),
109
+ 32,
110
+ 'buf',
111
+ ),
112
+ BufferLayout.nu64('idx'),
113
+ BufferLayout.u8('isEmpty'),
114
+ ],
115
+ 'priorVoters',
116
+ ),
117
+ BufferLayout.nu64(), // epochCredits.length
118
+ BufferLayout.seq<EpochCredits>(
119
+ BufferLayout.struct([
120
+ BufferLayout.nu64('epoch'),
121
+ BufferLayout.nu64('credits'),
122
+ BufferLayout.nu64('prevCredits'),
123
+ ]),
124
+ BufferLayout.offset(BufferLayout.u32(), -8),
125
+ 'epochCredits',
126
+ ),
127
+ BufferLayout.struct<BlockTimestamp>(
128
+ [BufferLayout.nu64('slot'), BufferLayout.nu64('timestamp')],
129
+ 'lastTimestamp',
130
+ ),
131
+ ]);
132
+
133
+ type VoteAccountArgs = {
134
+ nodePubkey: PublicKey;
135
+ authorizedWithdrawer: PublicKey;
136
+ commission: number;
137
+ rootSlot: number | null;
138
+ votes: Lockout[];
139
+ authorizedVoters: AuthorizedVoter[];
140
+ priorVoters: PriorVoter[];
141
+ epochCredits: EpochCredits[];
142
+ lastTimestamp: BlockTimestamp;
143
+ };
144
+
145
+ /**
146
+ * VoteAccount class
147
+ */
148
+ export class VoteAccount {
149
+ nodePubkey: PublicKey;
150
+ authorizedWithdrawer: PublicKey;
151
+ commission: number;
152
+ rootSlot: number | null;
153
+ votes: Lockout[];
154
+ authorizedVoters: AuthorizedVoter[];
155
+ priorVoters: PriorVoter[];
156
+ epochCredits: EpochCredits[];
157
+ lastTimestamp: BlockTimestamp;
158
+
159
+ /**
160
+ * @internal
161
+ */
162
+ constructor(args: VoteAccountArgs) {
163
+ this.nodePubkey = args.nodePubkey;
164
+ this.authorizedWithdrawer = args.authorizedWithdrawer;
165
+ this.commission = args.commission;
166
+ this.rootSlot = args.rootSlot;
167
+ this.votes = args.votes;
168
+ this.authorizedVoters = args.authorizedVoters;
169
+ this.priorVoters = args.priorVoters;
170
+ this.epochCredits = args.epochCredits;
171
+ this.lastTimestamp = args.lastTimestamp;
172
+ }
173
+
174
+ /**
175
+ * Deserialize VoteAccount from the account data.
176
+ *
177
+ * @param buffer account data
178
+ * @return VoteAccount
179
+ */
180
+ static fromAccountData(
181
+ buffer: Buffer | Uint8Array | Array<number>,
182
+ ): VoteAccount {
183
+ const versionOffset = 4;
184
+ const va = VoteAccountLayout.decode(toBuffer(buffer), versionOffset);
185
+
186
+ let rootSlot: number | null = va.rootSlot;
187
+ if (!va.rootSlotValid) {
188
+ rootSlot = null;
189
+ }
190
+
191
+ return new VoteAccount({
192
+ nodePubkey: new PublicKey(va.nodePubkey),
193
+ authorizedWithdrawer: new PublicKey(va.authorizedWithdrawer),
194
+ commission: va.commission,
195
+ votes: va.votes,
196
+ rootSlot,
197
+ authorizedVoters: va.authorizedVoters.map(parseAuthorizedVoter),
198
+ priorVoters: getPriorVoters(va.priorVoters),
199
+ epochCredits: va.epochCredits,
200
+ lastTimestamp: va.lastTimestamp,
201
+ });
202
+ }
203
+ }
204
+
205
+ function parseAuthorizedVoter({
206
+ authorizedVoter,
207
+ epoch,
208
+ }: AuthorizedVoterRaw): AuthorizedVoter {
209
+ return {
210
+ epoch,
211
+ authorizedVoter: new PublicKey(authorizedVoter),
212
+ };
213
+ }
214
+
215
+ function parsePriorVoters({
216
+ authorizedPubkey,
217
+ epochOfLastAuthorizedSwitch,
218
+ targetEpoch,
219
+ }: PriorVoterRaw): PriorVoter {
220
+ return {
221
+ authorizedPubkey: new PublicKey(authorizedPubkey),
222
+ epochOfLastAuthorizedSwitch,
223
+ targetEpoch,
224
+ };
225
+ }
226
+
227
+ function getPriorVoters({buf, idx, isEmpty}: PriorVoters): PriorVoter[] {
228
+ if (isEmpty) {
229
+ return [];
230
+ }
231
+
232
+ return [
233
+ ...buf.slice(idx + 1).map(parsePriorVoters),
234
+ ...buf.slice(0, idx).map(parsePriorVoters),
235
+ ];
236
+ }