@solana/web3.js 1.78.0 → 2.0.0-development

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 (99) hide show
  1. package/README.md +56 -112
  2. package/dist/index.browser.cjs +158 -0
  3. package/dist/index.browser.cjs.map +1 -0
  4. package/dist/index.browser.js +137 -0
  5. package/dist/index.browser.js.map +1 -0
  6. package/dist/index.development.js +1044 -0
  7. package/dist/index.development.js.map +1 -0
  8. package/dist/index.native.js +139 -0
  9. package/dist/index.native.js.map +1 -0
  10. package/dist/index.node.cjs +158 -0
  11. package/dist/index.node.cjs.map +1 -0
  12. package/dist/index.node.js +139 -0
  13. package/dist/index.node.js.map +1 -0
  14. package/dist/index.production.min.js +25 -0
  15. package/dist/types/index.d.ts +5 -0
  16. package/dist/types/rpc-default-config.d.ts +3 -0
  17. package/dist/types/rpc-integer-overflow-error.d.ts +8 -0
  18. package/dist/types/rpc-request-coalescer.d.ts +5 -0
  19. package/dist/types/rpc-request-deduplication.d.ts +2 -0
  20. package/dist/types/rpc-transport.d.ts +4 -0
  21. package/dist/types/rpc.d.ts +5 -0
  22. package/package.json +86 -111
  23. package/lib/index.browser.cjs.js +0 -10248
  24. package/lib/index.browser.cjs.js.map +0 -1
  25. package/lib/index.browser.esm.js +0 -10145
  26. package/lib/index.browser.esm.js.map +0 -1
  27. package/lib/index.cjs.js +0 -12748
  28. package/lib/index.cjs.js.map +0 -1
  29. package/lib/index.d.ts +0 -3904
  30. package/lib/index.esm.js +0 -12638
  31. package/lib/index.esm.js.map +0 -1
  32. package/lib/index.iife.js +0 -26757
  33. package/lib/index.iife.js.map +0 -1
  34. package/lib/index.iife.min.js +0 -20
  35. package/lib/index.iife.min.js.map +0 -1
  36. package/lib/index.native.js +0 -10248
  37. package/lib/index.native.js.map +0 -1
  38. package/src/__forks__/browser/fetch-impl.ts +0 -4
  39. package/src/__forks__/browser/rpc-websocket-factory.ts +0 -1
  40. package/src/__forks__/react-native/fetch-impl.ts +0 -4
  41. package/src/__forks__/react-native/rpc-websocket-factory.ts +0 -1
  42. package/src/account-data.ts +0 -39
  43. package/src/account.ts +0 -55
  44. package/src/blockhash.ts +0 -4
  45. package/src/bpf-loader-deprecated.ts +0 -5
  46. package/src/bpf-loader.ts +0 -45
  47. package/src/connection.ts +0 -6796
  48. package/src/epoch-schedule.ts +0 -102
  49. package/src/errors.ts +0 -50
  50. package/src/fee-calculator.ts +0 -18
  51. package/src/fetch-impl.ts +0 -13
  52. package/src/index.ts +0 -24
  53. package/src/instruction.ts +0 -58
  54. package/src/keypair.ts +0 -93
  55. package/src/layout.ts +0 -188
  56. package/src/loader.ts +0 -267
  57. package/src/message/account-keys.ts +0 -79
  58. package/src/message/compiled-keys.ts +0 -165
  59. package/src/message/index.ts +0 -47
  60. package/src/message/legacy.ts +0 -326
  61. package/src/message/v0.ts +0 -496
  62. package/src/message/versioned.ts +0 -36
  63. package/src/nonce-account.ts +0 -82
  64. package/src/programs/address-lookup-table/index.ts +0 -435
  65. package/src/programs/address-lookup-table/state.ts +0 -84
  66. package/src/programs/compute-budget.ts +0 -281
  67. package/src/programs/ed25519.ts +0 -157
  68. package/src/programs/index.ts +0 -7
  69. package/src/programs/secp256k1.ts +0 -228
  70. package/src/programs/stake.ts +0 -935
  71. package/src/programs/system.ts +0 -1048
  72. package/src/programs/vote.ts +0 -543
  73. package/src/publickey.ts +0 -259
  74. package/src/rpc-websocket-factory.ts +0 -4
  75. package/src/rpc-websocket.ts +0 -79
  76. package/src/sysvar.ts +0 -37
  77. package/src/timing.ts +0 -23
  78. package/src/transaction/constants.ts +0 -12
  79. package/src/transaction/expiry-custom-errors.ts +0 -48
  80. package/src/transaction/index.ts +0 -5
  81. package/src/transaction/legacy.ts +0 -918
  82. package/src/transaction/message.ts +0 -140
  83. package/src/transaction/versioned.ts +0 -126
  84. package/src/utils/assert.ts +0 -8
  85. package/src/utils/bigint.ts +0 -43
  86. package/src/utils/borsh-schema.ts +0 -38
  87. package/src/utils/cluster.ts +0 -31
  88. package/src/utils/ed25519.ts +0 -43
  89. package/src/utils/index.ts +0 -5
  90. package/src/utils/makeWebsocketUrl.ts +0 -26
  91. package/src/utils/promise-timeout.ts +0 -14
  92. package/src/utils/secp256k1.ts +0 -11
  93. package/src/utils/send-and-confirm-raw-transaction.ts +0 -102
  94. package/src/utils/send-and-confirm-transaction.ts +0 -98
  95. package/src/utils/shortvec-encoding.ts +0 -28
  96. package/src/utils/sleep.ts +0 -4
  97. package/src/utils/to-buffer.ts +0 -11
  98. package/src/validator-info.ts +0 -104
  99. package/src/vote-account.ts +0 -236
@@ -1,281 +0,0 @@
1
- import * as BufferLayout from '@solana/buffer-layout';
2
-
3
- import {
4
- encodeData,
5
- decodeData,
6
- InstructionType,
7
- IInstructionInputData,
8
- } from '../instruction';
9
- import {PublicKey} from '../publickey';
10
- import {TransactionInstruction} from '../transaction';
11
- import {u64} from '../utils/bigint';
12
-
13
- /**
14
- * Compute Budget Instruction class
15
- */
16
- export class ComputeBudgetInstruction {
17
- /**
18
- * @internal
19
- */
20
- constructor() {}
21
-
22
- /**
23
- * Decode a compute budget instruction and retrieve the instruction type.
24
- */
25
- static decodeInstructionType(
26
- instruction: TransactionInstruction,
27
- ): ComputeBudgetInstructionType {
28
- this.checkProgramId(instruction.programId);
29
-
30
- const instructionTypeLayout = BufferLayout.u8('instruction');
31
- const typeIndex = instructionTypeLayout.decode(instruction.data);
32
-
33
- let type: ComputeBudgetInstructionType | undefined;
34
- for (const [ixType, layout] of Object.entries(
35
- COMPUTE_BUDGET_INSTRUCTION_LAYOUTS,
36
- )) {
37
- if (layout.index == typeIndex) {
38
- type = ixType as ComputeBudgetInstructionType;
39
- break;
40
- }
41
- }
42
-
43
- if (!type) {
44
- throw new Error(
45
- 'Instruction type incorrect; not a ComputeBudgetInstruction',
46
- );
47
- }
48
-
49
- return type;
50
- }
51
-
52
- /**
53
- * Decode request units compute budget instruction and retrieve the instruction params.
54
- */
55
- static decodeRequestUnits(
56
- instruction: TransactionInstruction,
57
- ): RequestUnitsParams {
58
- this.checkProgramId(instruction.programId);
59
- const {units, additionalFee} = decodeData(
60
- COMPUTE_BUDGET_INSTRUCTION_LAYOUTS.RequestUnits,
61
- instruction.data,
62
- );
63
- return {units, additionalFee};
64
- }
65
-
66
- /**
67
- * Decode request heap frame compute budget instruction and retrieve the instruction params.
68
- */
69
- static decodeRequestHeapFrame(
70
- instruction: TransactionInstruction,
71
- ): RequestHeapFrameParams {
72
- this.checkProgramId(instruction.programId);
73
- const {bytes} = decodeData(
74
- COMPUTE_BUDGET_INSTRUCTION_LAYOUTS.RequestHeapFrame,
75
- instruction.data,
76
- );
77
- return {bytes};
78
- }
79
-
80
- /**
81
- * Decode set compute unit limit compute budget instruction and retrieve the instruction params.
82
- */
83
- static decodeSetComputeUnitLimit(
84
- instruction: TransactionInstruction,
85
- ): SetComputeUnitLimitParams {
86
- this.checkProgramId(instruction.programId);
87
- const {units} = decodeData(
88
- COMPUTE_BUDGET_INSTRUCTION_LAYOUTS.SetComputeUnitLimit,
89
- instruction.data,
90
- );
91
- return {units};
92
- }
93
-
94
- /**
95
- * Decode set compute unit price compute budget instruction and retrieve the instruction params.
96
- */
97
- static decodeSetComputeUnitPrice(
98
- instruction: TransactionInstruction,
99
- ): SetComputeUnitPriceParams {
100
- this.checkProgramId(instruction.programId);
101
- const {microLamports} = decodeData(
102
- COMPUTE_BUDGET_INSTRUCTION_LAYOUTS.SetComputeUnitPrice,
103
- instruction.data,
104
- );
105
- return {microLamports};
106
- }
107
-
108
- /**
109
- * @internal
110
- */
111
- static checkProgramId(programId: PublicKey) {
112
- if (!programId.equals(ComputeBudgetProgram.programId)) {
113
- throw new Error(
114
- 'invalid instruction; programId is not ComputeBudgetProgram',
115
- );
116
- }
117
- }
118
- }
119
-
120
- /**
121
- * An enumeration of valid ComputeBudgetInstructionType's
122
- */
123
- export type ComputeBudgetInstructionType =
124
- // FIXME
125
- // It would be preferable for this type to be `keyof ComputeBudgetInstructionInputData`
126
- // but Typedoc does not transpile `keyof` expressions.
127
- // See https://github.com/TypeStrong/typedoc/issues/1894
128
- | 'RequestUnits'
129
- | 'RequestHeapFrame'
130
- | 'SetComputeUnitLimit'
131
- | 'SetComputeUnitPrice';
132
-
133
- type ComputeBudgetInstructionInputData = {
134
- RequestUnits: IInstructionInputData & Readonly<RequestUnitsParams>;
135
- RequestHeapFrame: IInstructionInputData & Readonly<RequestHeapFrameParams>;
136
- SetComputeUnitLimit: IInstructionInputData &
137
- Readonly<SetComputeUnitLimitParams>;
138
- SetComputeUnitPrice: IInstructionInputData &
139
- Readonly<SetComputeUnitPriceParams>;
140
- };
141
-
142
- /**
143
- * Request units instruction params
144
- */
145
- export interface RequestUnitsParams {
146
- /** Units to request for transaction-wide compute */
147
- units: number;
148
- /** Prioritization fee lamports */
149
- additionalFee: number;
150
- }
151
-
152
- /**
153
- * Request heap frame instruction params
154
- */
155
- export type RequestHeapFrameParams = {
156
- /** Requested transaction-wide program heap size in bytes. Must be multiple of 1024. Applies to each program, including CPIs. */
157
- bytes: number;
158
- };
159
-
160
- /**
161
- * Set compute unit limit instruction params
162
- */
163
- export interface SetComputeUnitLimitParams {
164
- /** Transaction-wide compute unit limit */
165
- units: number;
166
- }
167
-
168
- /**
169
- * Set compute unit price instruction params
170
- */
171
- export interface SetComputeUnitPriceParams {
172
- /** Transaction compute unit price used for prioritization fees */
173
- microLamports: number | bigint;
174
- }
175
-
176
- /**
177
- * An enumeration of valid ComputeBudget InstructionType's
178
- * @internal
179
- */
180
- export const COMPUTE_BUDGET_INSTRUCTION_LAYOUTS = Object.freeze<{
181
- [Instruction in ComputeBudgetInstructionType]: InstructionType<
182
- ComputeBudgetInstructionInputData[Instruction]
183
- >;
184
- }>({
185
- RequestUnits: {
186
- index: 0,
187
- layout: BufferLayout.struct<
188
- ComputeBudgetInstructionInputData['RequestUnits']
189
- >([
190
- BufferLayout.u8('instruction'),
191
- BufferLayout.u32('units'),
192
- BufferLayout.u32('additionalFee'),
193
- ]),
194
- },
195
- RequestHeapFrame: {
196
- index: 1,
197
- layout: BufferLayout.struct<
198
- ComputeBudgetInstructionInputData['RequestHeapFrame']
199
- >([BufferLayout.u8('instruction'), BufferLayout.u32('bytes')]),
200
- },
201
- SetComputeUnitLimit: {
202
- index: 2,
203
- layout: BufferLayout.struct<
204
- ComputeBudgetInstructionInputData['SetComputeUnitLimit']
205
- >([BufferLayout.u8('instruction'), BufferLayout.u32('units')]),
206
- },
207
- SetComputeUnitPrice: {
208
- index: 3,
209
- layout: BufferLayout.struct<
210
- ComputeBudgetInstructionInputData['SetComputeUnitPrice']
211
- >([BufferLayout.u8('instruction'), u64('microLamports')]),
212
- },
213
- });
214
-
215
- /**
216
- * Factory class for transaction instructions to interact with the Compute Budget program
217
- */
218
- export class ComputeBudgetProgram {
219
- /**
220
- * @internal
221
- */
222
- constructor() {}
223
-
224
- /**
225
- * Public key that identifies the Compute Budget program
226
- */
227
- static programId: PublicKey = new PublicKey(
228
- 'ComputeBudget111111111111111111111111111111',
229
- );
230
-
231
- /**
232
- * @deprecated Instead, call {@link setComputeUnitLimit} and/or {@link setComputeUnitPrice}
233
- */
234
- static requestUnits(params: RequestUnitsParams): TransactionInstruction {
235
- const type = COMPUTE_BUDGET_INSTRUCTION_LAYOUTS.RequestUnits;
236
- const data = encodeData(type, params);
237
- return new TransactionInstruction({
238
- keys: [],
239
- programId: this.programId,
240
- data,
241
- });
242
- }
243
-
244
- static requestHeapFrame(
245
- params: RequestHeapFrameParams,
246
- ): TransactionInstruction {
247
- const type = COMPUTE_BUDGET_INSTRUCTION_LAYOUTS.RequestHeapFrame;
248
- const data = encodeData(type, params);
249
- return new TransactionInstruction({
250
- keys: [],
251
- programId: this.programId,
252
- data,
253
- });
254
- }
255
-
256
- static setComputeUnitLimit(
257
- params: SetComputeUnitLimitParams,
258
- ): TransactionInstruction {
259
- const type = COMPUTE_BUDGET_INSTRUCTION_LAYOUTS.SetComputeUnitLimit;
260
- const data = encodeData(type, params);
261
- return new TransactionInstruction({
262
- keys: [],
263
- programId: this.programId,
264
- data,
265
- });
266
- }
267
-
268
- static setComputeUnitPrice(
269
- params: SetComputeUnitPriceParams,
270
- ): TransactionInstruction {
271
- const type = COMPUTE_BUDGET_INSTRUCTION_LAYOUTS.SetComputeUnitPrice;
272
- const data = encodeData(type, {
273
- microLamports: BigInt(params.microLamports),
274
- });
275
- return new TransactionInstruction({
276
- keys: [],
277
- programId: this.programId,
278
- data,
279
- });
280
- }
281
- }
@@ -1,157 +0,0 @@
1
- import {Buffer} from 'buffer';
2
- import * as BufferLayout from '@solana/buffer-layout';
3
-
4
- import {Keypair} from '../keypair';
5
- import {PublicKey} from '../publickey';
6
- import {TransactionInstruction} from '../transaction';
7
- import assert from '../utils/assert';
8
- import {sign} from '../utils/ed25519';
9
-
10
- const PRIVATE_KEY_BYTES = 64;
11
- const PUBLIC_KEY_BYTES = 32;
12
- const SIGNATURE_BYTES = 64;
13
-
14
- /**
15
- * Params for creating an ed25519 instruction using a public key
16
- */
17
- export type CreateEd25519InstructionWithPublicKeyParams = {
18
- publicKey: Uint8Array;
19
- message: Uint8Array;
20
- signature: Uint8Array;
21
- instructionIndex?: number;
22
- };
23
-
24
- /**
25
- * Params for creating an ed25519 instruction using a private key
26
- */
27
- export type CreateEd25519InstructionWithPrivateKeyParams = {
28
- privateKey: Uint8Array;
29
- message: Uint8Array;
30
- instructionIndex?: number;
31
- };
32
-
33
- const ED25519_INSTRUCTION_LAYOUT = BufferLayout.struct<
34
- Readonly<{
35
- messageDataOffset: number;
36
- messageDataSize: number;
37
- messageInstructionIndex: number;
38
- numSignatures: number;
39
- padding: number;
40
- publicKeyInstructionIndex: number;
41
- publicKeyOffset: number;
42
- signatureInstructionIndex: number;
43
- signatureOffset: number;
44
- }>
45
- >([
46
- BufferLayout.u8('numSignatures'),
47
- BufferLayout.u8('padding'),
48
- BufferLayout.u16('signatureOffset'),
49
- BufferLayout.u16('signatureInstructionIndex'),
50
- BufferLayout.u16('publicKeyOffset'),
51
- BufferLayout.u16('publicKeyInstructionIndex'),
52
- BufferLayout.u16('messageDataOffset'),
53
- BufferLayout.u16('messageDataSize'),
54
- BufferLayout.u16('messageInstructionIndex'),
55
- ]);
56
-
57
- export class Ed25519Program {
58
- /**
59
- * @internal
60
- */
61
- constructor() {}
62
-
63
- /**
64
- * Public key that identifies the ed25519 program
65
- */
66
- static programId: PublicKey = new PublicKey(
67
- 'Ed25519SigVerify111111111111111111111111111',
68
- );
69
-
70
- /**
71
- * Create an ed25519 instruction with a public key and signature. The
72
- * public key must be a buffer that is 32 bytes long, and the signature
73
- * must be a buffer of 64 bytes.
74
- */
75
- static createInstructionWithPublicKey(
76
- params: CreateEd25519InstructionWithPublicKeyParams,
77
- ): TransactionInstruction {
78
- const {publicKey, message, signature, instructionIndex} = params;
79
-
80
- assert(
81
- publicKey.length === PUBLIC_KEY_BYTES,
82
- `Public Key must be ${PUBLIC_KEY_BYTES} bytes but received ${publicKey.length} bytes`,
83
- );
84
-
85
- assert(
86
- signature.length === SIGNATURE_BYTES,
87
- `Signature must be ${SIGNATURE_BYTES} bytes but received ${signature.length} bytes`,
88
- );
89
-
90
- const publicKeyOffset = ED25519_INSTRUCTION_LAYOUT.span;
91
- const signatureOffset = publicKeyOffset + publicKey.length;
92
- const messageDataOffset = signatureOffset + signature.length;
93
- const numSignatures = 1;
94
-
95
- const instructionData = Buffer.alloc(messageDataOffset + message.length);
96
-
97
- const index =
98
- instructionIndex == null
99
- ? 0xffff // An index of `u16::MAX` makes it default to the current instruction.
100
- : instructionIndex;
101
-
102
- ED25519_INSTRUCTION_LAYOUT.encode(
103
- {
104
- numSignatures,
105
- padding: 0,
106
- signatureOffset,
107
- signatureInstructionIndex: index,
108
- publicKeyOffset,
109
- publicKeyInstructionIndex: index,
110
- messageDataOffset,
111
- messageDataSize: message.length,
112
- messageInstructionIndex: index,
113
- },
114
- instructionData,
115
- );
116
-
117
- instructionData.fill(publicKey, publicKeyOffset);
118
- instructionData.fill(signature, signatureOffset);
119
- instructionData.fill(message, messageDataOffset);
120
-
121
- return new TransactionInstruction({
122
- keys: [],
123
- programId: Ed25519Program.programId,
124
- data: instructionData,
125
- });
126
- }
127
-
128
- /**
129
- * Create an ed25519 instruction with a private key. The private key
130
- * must be a buffer that is 64 bytes long.
131
- */
132
- static createInstructionWithPrivateKey(
133
- params: CreateEd25519InstructionWithPrivateKeyParams,
134
- ): TransactionInstruction {
135
- const {privateKey, message, instructionIndex} = params;
136
-
137
- assert(
138
- privateKey.length === PRIVATE_KEY_BYTES,
139
- `Private key must be ${PRIVATE_KEY_BYTES} bytes but received ${privateKey.length} bytes`,
140
- );
141
-
142
- try {
143
- const keypair = Keypair.fromSecretKey(privateKey);
144
- const publicKey = keypair.publicKey.toBytes();
145
- const signature = sign(message, keypair.secretKey);
146
-
147
- return this.createInstructionWithPublicKey({
148
- publicKey,
149
- message,
150
- signature,
151
- instructionIndex,
152
- });
153
- } catch (error) {
154
- throw new Error(`Error creating instruction; ${error}`);
155
- }
156
- }
157
- }
@@ -1,7 +0,0 @@
1
- export * from './address-lookup-table';
2
- export * from './compute-budget';
3
- export * from './ed25519';
4
- export * from './secp256k1';
5
- export * from './stake';
6
- export * from './system';
7
- export * from './vote';
@@ -1,228 +0,0 @@
1
- import {Buffer} from 'buffer';
2
- import * as BufferLayout from '@solana/buffer-layout';
3
- import {keccak_256} from '@noble/hashes/sha3';
4
-
5
- import {PublicKey} from '../publickey';
6
- import {TransactionInstruction} from '../transaction';
7
- import assert from '../utils/assert';
8
- import {publicKeyCreate, ecdsaSign} from '../utils/secp256k1';
9
- import {toBuffer} from '../utils/to-buffer';
10
-
11
- const PRIVATE_KEY_BYTES = 32;
12
- const ETHEREUM_ADDRESS_BYTES = 20;
13
- const PUBLIC_KEY_BYTES = 64;
14
- const SIGNATURE_OFFSETS_SERIALIZED_SIZE = 11;
15
-
16
- /**
17
- * Params for creating an secp256k1 instruction using a public key
18
- */
19
- export type CreateSecp256k1InstructionWithPublicKeyParams = {
20
- publicKey: Buffer | Uint8Array | Array<number>;
21
- message: Buffer | Uint8Array | Array<number>;
22
- signature: Buffer | Uint8Array | Array<number>;
23
- recoveryId: number;
24
- instructionIndex?: number;
25
- };
26
-
27
- /**
28
- * Params for creating an secp256k1 instruction using an Ethereum address
29
- */
30
- export type CreateSecp256k1InstructionWithEthAddressParams = {
31
- ethAddress: Buffer | Uint8Array | Array<number> | string;
32
- message: Buffer | Uint8Array | Array<number>;
33
- signature: Buffer | Uint8Array | Array<number>;
34
- recoveryId: number;
35
- instructionIndex?: number;
36
- };
37
-
38
- /**
39
- * Params for creating an secp256k1 instruction using a private key
40
- */
41
- export type CreateSecp256k1InstructionWithPrivateKeyParams = {
42
- privateKey: Buffer | Uint8Array | Array<number>;
43
- message: Buffer | Uint8Array | Array<number>;
44
- instructionIndex?: number;
45
- };
46
-
47
- const SECP256K1_INSTRUCTION_LAYOUT = BufferLayout.struct<
48
- Readonly<{
49
- ethAddress: Uint8Array;
50
- ethAddressInstructionIndex: number;
51
- ethAddressOffset: number;
52
- messageDataOffset: number;
53
- messageDataSize: number;
54
- messageInstructionIndex: number;
55
- numSignatures: number;
56
- recoveryId: number;
57
- signature: Uint8Array;
58
- signatureInstructionIndex: number;
59
- signatureOffset: number;
60
- }>
61
- >([
62
- BufferLayout.u8('numSignatures'),
63
- BufferLayout.u16('signatureOffset'),
64
- BufferLayout.u8('signatureInstructionIndex'),
65
- BufferLayout.u16('ethAddressOffset'),
66
- BufferLayout.u8('ethAddressInstructionIndex'),
67
- BufferLayout.u16('messageDataOffset'),
68
- BufferLayout.u16('messageDataSize'),
69
- BufferLayout.u8('messageInstructionIndex'),
70
- BufferLayout.blob(20, 'ethAddress'),
71
- BufferLayout.blob(64, 'signature'),
72
- BufferLayout.u8('recoveryId'),
73
- ]);
74
-
75
- export class Secp256k1Program {
76
- /**
77
- * @internal
78
- */
79
- constructor() {}
80
-
81
- /**
82
- * Public key that identifies the secp256k1 program
83
- */
84
- static programId: PublicKey = new PublicKey(
85
- 'KeccakSecp256k11111111111111111111111111111',
86
- );
87
-
88
- /**
89
- * Construct an Ethereum address from a secp256k1 public key buffer.
90
- * @param {Buffer} publicKey a 64 byte secp256k1 public key buffer
91
- */
92
- static publicKeyToEthAddress(
93
- publicKey: Buffer | Uint8Array | Array<number>,
94
- ): Buffer {
95
- assert(
96
- publicKey.length === PUBLIC_KEY_BYTES,
97
- `Public key must be ${PUBLIC_KEY_BYTES} bytes but received ${publicKey.length} bytes`,
98
- );
99
-
100
- try {
101
- return Buffer.from(keccak_256(toBuffer(publicKey))).slice(
102
- -ETHEREUM_ADDRESS_BYTES,
103
- );
104
- } catch (error) {
105
- throw new Error(`Error constructing Ethereum address: ${error}`);
106
- }
107
- }
108
-
109
- /**
110
- * Create an secp256k1 instruction with a public key. The public key
111
- * must be a buffer that is 64 bytes long.
112
- */
113
- static createInstructionWithPublicKey(
114
- params: CreateSecp256k1InstructionWithPublicKeyParams,
115
- ): TransactionInstruction {
116
- const {publicKey, message, signature, recoveryId, instructionIndex} =
117
- params;
118
- return Secp256k1Program.createInstructionWithEthAddress({
119
- ethAddress: Secp256k1Program.publicKeyToEthAddress(publicKey),
120
- message,
121
- signature,
122
- recoveryId,
123
- instructionIndex,
124
- });
125
- }
126
-
127
- /**
128
- * Create an secp256k1 instruction with an Ethereum address. The address
129
- * must be a hex string or a buffer that is 20 bytes long.
130
- */
131
- static createInstructionWithEthAddress(
132
- params: CreateSecp256k1InstructionWithEthAddressParams,
133
- ): TransactionInstruction {
134
- const {
135
- ethAddress: rawAddress,
136
- message,
137
- signature,
138
- recoveryId,
139
- instructionIndex = 0,
140
- } = params;
141
-
142
- let ethAddress;
143
- if (typeof rawAddress === 'string') {
144
- if (rawAddress.startsWith('0x')) {
145
- ethAddress = Buffer.from(rawAddress.substr(2), 'hex');
146
- } else {
147
- ethAddress = Buffer.from(rawAddress, 'hex');
148
- }
149
- } else {
150
- ethAddress = rawAddress;
151
- }
152
-
153
- assert(
154
- ethAddress.length === ETHEREUM_ADDRESS_BYTES,
155
- `Address must be ${ETHEREUM_ADDRESS_BYTES} bytes but received ${ethAddress.length} bytes`,
156
- );
157
-
158
- const dataStart = 1 + SIGNATURE_OFFSETS_SERIALIZED_SIZE;
159
- const ethAddressOffset = dataStart;
160
- const signatureOffset = dataStart + ethAddress.length;
161
- const messageDataOffset = signatureOffset + signature.length + 1;
162
- const numSignatures = 1;
163
-
164
- const instructionData = Buffer.alloc(
165
- SECP256K1_INSTRUCTION_LAYOUT.span + message.length,
166
- );
167
-
168
- SECP256K1_INSTRUCTION_LAYOUT.encode(
169
- {
170
- numSignatures,
171
- signatureOffset,
172
- signatureInstructionIndex: instructionIndex,
173
- ethAddressOffset,
174
- ethAddressInstructionIndex: instructionIndex,
175
- messageDataOffset,
176
- messageDataSize: message.length,
177
- messageInstructionIndex: instructionIndex,
178
- signature: toBuffer(signature),
179
- ethAddress: toBuffer(ethAddress),
180
- recoveryId,
181
- },
182
- instructionData,
183
- );
184
-
185
- instructionData.fill(toBuffer(message), SECP256K1_INSTRUCTION_LAYOUT.span);
186
-
187
- return new TransactionInstruction({
188
- keys: [],
189
- programId: Secp256k1Program.programId,
190
- data: instructionData,
191
- });
192
- }
193
-
194
- /**
195
- * Create an secp256k1 instruction with a private key. The private key
196
- * must be a buffer that is 32 bytes long.
197
- */
198
- static createInstructionWithPrivateKey(
199
- params: CreateSecp256k1InstructionWithPrivateKeyParams,
200
- ): TransactionInstruction {
201
- const {privateKey: pkey, message, instructionIndex} = params;
202
-
203
- assert(
204
- pkey.length === PRIVATE_KEY_BYTES,
205
- `Private key must be ${PRIVATE_KEY_BYTES} bytes but received ${pkey.length} bytes`,
206
- );
207
-
208
- try {
209
- const privateKey = toBuffer(pkey);
210
- const publicKey = publicKeyCreate(
211
- privateKey,
212
- false /* isCompressed */,
213
- ).slice(1); // throw away leading byte
214
- const messageHash = Buffer.from(keccak_256(toBuffer(message)));
215
- const [signature, recoveryId] = ecdsaSign(messageHash, privateKey);
216
-
217
- return this.createInstructionWithPublicKey({
218
- publicKey,
219
- message,
220
- signature,
221
- recoveryId,
222
- instructionIndex,
223
- });
224
- } catch (error) {
225
- throw new Error(`Error creating instruction; ${error}`);
226
- }
227
- }
228
- }