@solana/web3.js 1.58.0 → 1.60.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@solana/web3.js",
3
- "version": "1.58.0",
3
+ "version": "1.60.0",
4
4
  "description": "Solana Javascript API",
5
5
  "keywords": [
6
6
  "api",
@@ -69,7 +69,6 @@
69
69
  "buffer": "6.0.1",
70
70
  "fast-stable-stringify": "^1.0.0",
71
71
  "jayson": "^3.4.4",
72
- "js-sha3": "^0.8.0",
73
72
  "node-fetch": "2",
74
73
  "rpc-websockets": "^7.5.0",
75
74
  "superstruct": "^0.14.2"
package/src/connection.ts CHANGED
@@ -1029,6 +1029,8 @@ export type ParsedMessageAccount = {
1029
1029
  signer: boolean;
1030
1030
  /** Indicates if the account is writable for this transaction */
1031
1031
  writable: boolean;
1032
+ /** Indicates if the account key came from the transaction or a lookup table */
1033
+ source?: 'transaction' | 'lookupTable';
1032
1034
  };
1033
1035
 
1034
1036
  /**
@@ -1966,6 +1968,9 @@ const ParsedConfirmedTransactionResult = pick({
1966
1968
  pubkey: PublicKeyFromString,
1967
1969
  signer: boolean(),
1968
1970
  writable: boolean(),
1971
+ source: optional(
1972
+ union([literal('transaction'), literal('lookupTable')]),
1973
+ ),
1969
1974
  }),
1970
1975
  ),
1971
1976
  instructions: array(ParsedOrRawInstruction),
package/src/keypair.ts CHANGED
@@ -88,6 +88,6 @@ export class Keypair {
88
88
  * The raw secret key for this keypair
89
89
  */
90
90
  get secretKey(): Uint8Array {
91
- return this._keypair.secretKey;
91
+ return new Uint8Array(this._keypair.secretKey);
92
92
  }
93
93
  }
package/src/layout.ts CHANGED
@@ -1,6 +1,8 @@
1
1
  import {Buffer} from 'buffer';
2
2
  import * as BufferLayout from '@solana/buffer-layout';
3
3
 
4
+ import {VoteAuthorizeWithSeedArgs} from './programs/vote';
5
+
4
6
  /**
5
7
  * Layout for a public key
6
8
  */
@@ -141,6 +143,23 @@ export const voteInit = (property: string = 'voteInit') => {
141
143
  );
142
144
  };
143
145
 
146
+ /**
147
+ * Layout for a VoteAuthorizeWithSeedArgs object
148
+ */
149
+ export const voteAuthorizeWithSeedArgs = (
150
+ property: string = 'voteAuthorizeWithSeedArgs',
151
+ ) => {
152
+ return BufferLayout.struct<VoteAuthorizeWithSeedArgs>(
153
+ [
154
+ BufferLayout.u32('voteAuthorizationType'),
155
+ publicKey('currentAuthorityDerivedKeyOwnerPubkey'),
156
+ rustString('currentAuthorityDerivedKeySeed'),
157
+ publicKey('newAuthorized'),
158
+ ],
159
+ property,
160
+ );
161
+ };
162
+
144
163
  export function getAlloc(type: any, fields: any): number {
145
164
  const getItemAlloc = (item: any): number => {
146
165
  if (item.span >= 0) {
@@ -152,6 +171,9 @@ export function getAlloc(type: any, fields: any): number {
152
171
  if (Array.isArray(field)) {
153
172
  return field.length * getItemAlloc(item.elementLayout);
154
173
  }
174
+ } else if ('fields' in item) {
175
+ // This is a `Structure` whose size needs to be recursively measured.
176
+ return getAlloc({layout: item}, fields[item.property]);
155
177
  }
156
178
  // Couldn't determine allocated size of layout
157
179
  return 0;
@@ -1,6 +1,6 @@
1
1
  import {Buffer} from 'buffer';
2
2
  import * as BufferLayout from '@solana/buffer-layout';
3
- import sha3 from 'js-sha3';
3
+ import {keccak_256} from '@noble/hashes/sha3';
4
4
 
5
5
  import {PublicKey} from '../publickey';
6
6
  import {TransactionInstruction} from '../transaction';
@@ -98,9 +98,9 @@ export class Secp256k1Program {
98
98
  );
99
99
 
100
100
  try {
101
- return Buffer.from(
102
- sha3.keccak_256.update(toBuffer(publicKey)).digest(),
103
- ).slice(-ETHEREUM_ADDRESS_BYTES);
101
+ return Buffer.from(keccak_256(toBuffer(publicKey))).slice(
102
+ -ETHEREUM_ADDRESS_BYTES,
103
+ );
104
104
  } catch (error) {
105
105
  throw new Error(`Error constructing Ethereum address: ${error}`);
106
106
  }
@@ -211,9 +211,7 @@ export class Secp256k1Program {
211
211
  privateKey,
212
212
  false /* isCompressed */,
213
213
  ).slice(1); // throw away leading byte
214
- const messageHash = Buffer.from(
215
- sha3.keccak_256.update(toBuffer(message)).digest(),
216
- );
214
+ const messageHash = Buffer.from(keccak_256(toBuffer(message)));
217
215
  const [signature, recoveryId] = ecdsaSign(messageHash, privateKey);
218
216
 
219
217
  return this.createInstructionWithPublicKey({
@@ -65,6 +65,18 @@ export type AuthorizeVoteParams = {
65
65
  voteAuthorizationType: VoteAuthorizationType;
66
66
  };
67
67
 
68
+ /**
69
+ * AuthorizeWithSeed instruction params
70
+ */
71
+ export type AuthorizeVoteWithSeedParams = {
72
+ currentAuthorityDerivedKeyBasePubkey: PublicKey;
73
+ currentAuthorityDerivedKeyOwnerPubkey: PublicKey;
74
+ currentAuthorityDerivedKeySeed: string;
75
+ newAuthorizedPubkey: PublicKey;
76
+ voteAuthorizationType: VoteAuthorizationType;
77
+ votePubkey: PublicKey;
78
+ };
79
+
68
80
  /**
69
81
  * Withdraw from vote account transaction params
70
82
  */
@@ -160,6 +172,41 @@ export class VoteInstruction {
160
172
  };
161
173
  }
162
174
 
175
+ /**
176
+ * Decode an authorize instruction and retrieve the instruction params.
177
+ */
178
+ static decodeAuthorizeWithSeed(
179
+ instruction: TransactionInstruction,
180
+ ): AuthorizeVoteWithSeedParams {
181
+ this.checkProgramId(instruction.programId);
182
+ this.checkKeyLength(instruction.keys, 3);
183
+
184
+ const {
185
+ voteAuthorizeWithSeedArgs: {
186
+ currentAuthorityDerivedKeyOwnerPubkey,
187
+ currentAuthorityDerivedKeySeed,
188
+ newAuthorized,
189
+ voteAuthorizationType,
190
+ },
191
+ } = decodeData(
192
+ VOTE_INSTRUCTION_LAYOUTS.AuthorizeWithSeed,
193
+ instruction.data,
194
+ );
195
+
196
+ return {
197
+ currentAuthorityDerivedKeyBasePubkey: instruction.keys[2].pubkey,
198
+ currentAuthorityDerivedKeyOwnerPubkey: new PublicKey(
199
+ currentAuthorityDerivedKeyOwnerPubkey,
200
+ ),
201
+ currentAuthorityDerivedKeySeed: currentAuthorityDerivedKeySeed,
202
+ newAuthorizedPubkey: new PublicKey(newAuthorized),
203
+ voteAuthorizationType: {
204
+ index: voteAuthorizationType,
205
+ },
206
+ votePubkey: instruction.keys[0].pubkey,
207
+ };
208
+ }
209
+
163
210
  /**
164
211
  * Decode a withdraw instruction and retrieve the instruction params.
165
212
  */
@@ -211,13 +258,23 @@ export type VoteInstructionType =
211
258
  // It would be preferable for this type to be `keyof VoteInstructionInputData`
212
259
  // but Typedoc does not transpile `keyof` expressions.
213
260
  // See https://github.com/TypeStrong/typedoc/issues/1894
214
- 'Authorize' | 'InitializeAccount' | 'Withdraw';
215
-
261
+ 'Authorize' | 'AuthorizeWithSeed' | 'InitializeAccount' | 'Withdraw';
262
+
263
+ /** @internal */
264
+ export type VoteAuthorizeWithSeedArgs = Readonly<{
265
+ currentAuthorityDerivedKeyOwnerPubkey: Uint8Array;
266
+ currentAuthorityDerivedKeySeed: string;
267
+ newAuthorized: Uint8Array;
268
+ voteAuthorizationType: number;
269
+ }>;
216
270
  type VoteInstructionInputData = {
217
271
  Authorize: IInstructionInputData & {
218
272
  newAuthorized: Uint8Array;
219
273
  voteAuthorizationType: number;
220
274
  };
275
+ AuthorizeWithSeed: IInstructionInputData & {
276
+ voteAuthorizeWithSeedArgs: VoteAuthorizeWithSeedArgs;
277
+ };
221
278
  InitializeAccount: IInstructionInputData & {
222
279
  voteInit: Readonly<{
223
280
  authorizedVoter: Uint8Array;
@@ -258,6 +315,13 @@ const VOTE_INSTRUCTION_LAYOUTS = Object.freeze<{
258
315
  BufferLayout.ns64('lamports'),
259
316
  ]),
260
317
  },
318
+ AuthorizeWithSeed: {
319
+ index: 10,
320
+ layout: BufferLayout.struct<VoteInstructionInputData['AuthorizeWithSeed']>([
321
+ BufferLayout.u32('instruction'),
322
+ Layout.voteAuthorizeWithSeedArgs(),
323
+ ]),
324
+ },
261
325
  });
262
326
 
263
327
  /**
@@ -390,6 +454,49 @@ export class VoteProgram {
390
454
  });
391
455
  }
392
456
 
457
+ /**
458
+ * Generate a transaction that authorizes a new Voter or Withdrawer on the Vote account
459
+ * where the current Voter or Withdrawer authority is a derived key.
460
+ */
461
+ static authorizeWithSeed(params: AuthorizeVoteWithSeedParams): Transaction {
462
+ const {
463
+ currentAuthorityDerivedKeyBasePubkey,
464
+ currentAuthorityDerivedKeyOwnerPubkey,
465
+ currentAuthorityDerivedKeySeed,
466
+ newAuthorizedPubkey,
467
+ voteAuthorizationType,
468
+ votePubkey,
469
+ } = params;
470
+
471
+ const type = VOTE_INSTRUCTION_LAYOUTS.AuthorizeWithSeed;
472
+ const data = encodeData(type, {
473
+ voteAuthorizeWithSeedArgs: {
474
+ currentAuthorityDerivedKeyOwnerPubkey: toBuffer(
475
+ currentAuthorityDerivedKeyOwnerPubkey.toBuffer(),
476
+ ),
477
+ currentAuthorityDerivedKeySeed: currentAuthorityDerivedKeySeed,
478
+ newAuthorized: toBuffer(newAuthorizedPubkey.toBuffer()),
479
+ voteAuthorizationType: voteAuthorizationType.index,
480
+ },
481
+ });
482
+
483
+ const keys = [
484
+ {pubkey: votePubkey, isSigner: false, isWritable: true},
485
+ {pubkey: SYSVAR_CLOCK_PUBKEY, isSigner: false, isWritable: false},
486
+ {
487
+ pubkey: currentAuthorityDerivedKeyBasePubkey,
488
+ isSigner: true,
489
+ isWritable: false,
490
+ },
491
+ ];
492
+
493
+ return new Transaction().add({
494
+ keys,
495
+ programId: this.programId,
496
+ data,
497
+ });
498
+ }
499
+
393
500
  /**
394
501
  * Generate a transaction to withdraw from a Vote account.
395
502
  */
@@ -1,16 +1,14 @@
1
- import {
2
- AccountKeysFromLookups,
3
- MessageAccountKeys,
4
- } from '../message/account-keys';
1
+ import {AccountKeysFromLookups} from '../message/account-keys';
5
2
  import assert from '../utils/assert';
6
3
  import {toBuffer} from '../utils/to-buffer';
7
4
  import {Blockhash} from '../blockhash';
8
5
  import {Message, MessageV0, VersionedMessage} from '../message';
6
+ import {PublicKey} from '../publickey';
9
7
  import {AddressLookupTableAccount} from '../programs';
10
8
  import {AccountMeta, TransactionInstruction} from './legacy';
11
9
 
12
10
  export type TransactionMessageArgs = {
13
- accountKeys: MessageAccountKeys;
11
+ payerKey: PublicKey;
14
12
  instructions: Array<TransactionInstruction>;
15
13
  recentBlockhash: Blockhash;
16
14
  };
@@ -24,12 +22,12 @@ export type DecompileArgs =
24
22
  };
25
23
 
26
24
  export class TransactionMessage {
27
- accountKeys: MessageAccountKeys;
25
+ payerKey: PublicKey;
28
26
  instructions: Array<TransactionInstruction>;
29
27
  recentBlockhash: Blockhash;
30
28
 
31
29
  constructor(args: TransactionMessageArgs) {
32
- this.accountKeys = args.accountKeys;
30
+ this.payerKey = args.payerKey;
33
31
  this.instructions = args.instructions;
34
32
  this.recentBlockhash = args.recentBlockhash;
35
33
  }
@@ -55,6 +53,13 @@ export class TransactionMessage {
55
53
  assert(numWritableUnsignedAccounts >= 0, 'Message header is invalid');
56
54
 
57
55
  const accountKeys = message.getAccountKeys(args);
56
+ const payerKey = accountKeys.get(0);
57
+ if (payerKey === undefined) {
58
+ throw new Error(
59
+ 'Failed to decompile message because no account keys were found',
60
+ );
61
+ }
62
+
58
63
  const instructions: TransactionInstruction[] = [];
59
64
  for (const compiledIx of compiledInstructions) {
60
65
  const keys: AccountMeta[] = [];
@@ -106,22 +111,15 @@ export class TransactionMessage {
106
111
  }
107
112
 
108
113
  return new TransactionMessage({
109
- accountKeys,
114
+ payerKey,
110
115
  instructions,
111
116
  recentBlockhash,
112
117
  });
113
118
  }
114
119
 
115
120
  compileToLegacyMessage(): Message {
116
- const payerKey = this.accountKeys.get(0);
117
- if (payerKey === undefined) {
118
- throw new Error(
119
- 'Failed to compile message because no account keys were found',
120
- );
121
- }
122
-
123
121
  return Message.compile({
124
- payerKey,
122
+ payerKey: this.payerKey,
125
123
  recentBlockhash: this.recentBlockhash,
126
124
  instructions: this.instructions,
127
125
  });
@@ -130,15 +128,8 @@ export class TransactionMessage {
130
128
  compileToV0Message(
131
129
  addressLookupTableAccounts?: AddressLookupTableAccount[],
132
130
  ): MessageV0 {
133
- const payerKey = this.accountKeys.get(0);
134
- if (payerKey === undefined) {
135
- throw new Error(
136
- 'Failed to compile message because no account keys were found',
137
- );
138
- }
139
-
140
131
  return MessageV0.compile({
141
- payerKey,
132
+ payerKey: this.payerKey,
142
133
  recentBlockhash: this.recentBlockhash,
143
134
  instructions: this.instructions,
144
135
  addressLookupTableAccounts,