Skip to content

Commit 89cc35d

Browse files
jayantkJayant Krishnamurthy
andauthored
Run prettier (#318)
Co-authored-by: Jayant Krishnamurthy <jkrishnamurthy@jumptrading.com>
1 parent 863a983 commit 89cc35d

File tree

2 files changed

+139
-75
lines changed

2 files changed

+139
-75
lines changed

third_party/pyth/multisig-wh-message-builder/src/index.ts

Lines changed: 65 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ import {
1111
Keypair,
1212
LAMPORTS_PER_SOL,
1313
PublicKey,
14-
SystemProgram, TransactionInstruction,
14+
SystemProgram,
15+
TransactionInstruction,
1516
} from "@solana/web3.js";
1617
import Squads from "@sqds/mesh";
1718
import bs58 from "bs58";
@@ -47,7 +48,13 @@ program
4748
)
4849
.option("-p, --payload <hex-string>", "payload to sign", "0xdeadbeef")
4950
.action(async (options) => {
50-
const squad = await getSquadsClient(options.cluster, options.ledger, options.ledgerDerivationAccount, options.ledgerDerivationChange, options.wallet);
51+
const squad = await getSquadsClient(
52+
options.cluster,
53+
options.ledger,
54+
options.ledgerDerivationAccount,
55+
options.ledgerDerivationChange,
56+
options.wallet
57+
);
5158
await createWormholeMsgMultisigTx(
5259
options.cluster,
5360
squad,
@@ -59,7 +66,9 @@ program
5966

6067
program
6168
.command("set-is-active")
62-
.description("Create a new multisig transaction to set the attester is-active flag")
69+
.description(
70+
"Create a new multisig transaction to set the attester is-active flag"
71+
)
6372
.option("-c, --cluster <network>", "solana cluster to use", "devnet")
6473
.requiredOption("-v, --vault-address <address>", "multisig vault address")
6574
.option("-l, --ledger", "use ledger")
@@ -77,17 +86,38 @@ program
7786
"keys/key.json"
7887
)
7988
.option("-a, --attester <program id>")
80-
.option("-i, --is-active <true/false>", "set the isActive field to this value", true)
89+
.option(
90+
"-i, --is-active <true/false>",
91+
"set the isActive field to this value",
92+
true
93+
)
8194
.action(async (options) => {
82-
const squad = await getSquadsClient(options.cluster, options.ledger, options.ledgerDerivationAccount, options.ledgerDerivationChange, options.wallet);
95+
const squad = await getSquadsClient(
96+
options.cluster,
97+
options.ledger,
98+
options.ledgerDerivationAccount,
99+
options.ledgerDerivationChange,
100+
options.wallet
101+
);
83102
const msAccount = await squad.getMultisig(options.vaultAddress);
84103
const vaultAuthority = squad.getAuthorityPDA(
85104
msAccount.publicKey,
86105
msAccount.authorityIndex
87106
);
88107
const attesterProgramId = new PublicKey(options.attester);
89-
const txKey = await createTx(squad, options.ledger, new PublicKey(options.vaultAddress));
90-
const instructions = [await setIsActiveIx(vaultAuthority, vaultAuthority, attesterProgramId, options.active)];
108+
const txKey = await createTx(
109+
squad,
110+
options.ledger,
111+
new PublicKey(options.vaultAddress)
112+
);
113+
const instructions = [
114+
await setIsActiveIx(
115+
vaultAuthority,
116+
vaultAuthority,
117+
attesterProgramId,
118+
options.active
119+
),
120+
];
91121
await addInstructionsToTx(squad, options.ledger, txKey, instructions);
92122
});
93123

@@ -146,11 +176,12 @@ const solanaClusterMappingToWormholeNetwork: Record<Cluster, WormholeNetwork> =
146176
"mainnet-beta": "MAINNET",
147177
};
148178

149-
async function getSquadsClient(cluster: Cluster,
150-
ledger: boolean,
151-
ledgerDerivationAccount: number | undefined,
152-
ledgerDerivationChange: number | undefined,
153-
walletPath: string,
179+
async function getSquadsClient(
180+
cluster: Cluster,
181+
ledger: boolean,
182+
ledgerDerivationAccount: number | undefined,
183+
ledgerDerivationChange: number | undefined,
184+
walletPath: string
154185
) {
155186
let wallet: LedgerNodeWallet | NodeWallet;
156187
if (ledger) {
@@ -176,7 +207,7 @@ async function getSquadsClient(cluster: Cluster,
176207
async function createTx(
177208
squad: Squads,
178209
ledger: boolean,
179-
vault: PublicKey,
210+
vault: PublicKey
180211
): Promise<PublicKey> {
181212
const msAccount = await squad.getMultisig(vault);
182213

@@ -201,7 +232,9 @@ async function addInstructionsToTx(
201232
instructions: TransactionInstruction[]
202233
) {
203234
for (let i = 0; i < instructions.length; i++) {
204-
console.log(`Adding instruction ${i}/${instructions.length} to transaction...`);
235+
console.log(
236+
`Adding instruction ${i}/${instructions.length} to transaction...`
237+
);
205238
if (ledger) {
206239
console.log("Please approve the transaction on your ledger device...");
207240
}
@@ -221,33 +254,36 @@ async function setIsActiveIx(
221254
attesterProgramId: PublicKey,
222255
isActive: boolean
223256
): Promise<TransactionInstruction> {
224-
const configKey = PublicKey.createProgramAddressSync([Buffer.from("pyth2wormhole-config-v3")], attesterProgramId)
257+
const configKey = PublicKey.createProgramAddressSync(
258+
[Buffer.from("pyth2wormhole-config-v3")],
259+
attesterProgramId
260+
);
225261
const config: AccountMeta = {
226262
pubkey: configKey,
227263
isSigner: false,
228264
isWritable: true,
229-
}
265+
};
230266

231267
const opsOwner: AccountMeta = {
232268
pubkey: opsOwnerKey,
233269
isSigner: true,
234270
isWritable: true,
235-
}
271+
};
236272
const payer: AccountMeta = {
237273
pubkey: payerKey,
238274
isSigner: true,
239275
isWritable: true,
240-
}
276+
};
241277

242-
const isActiveInt = (isActive === true) ? 1 : 0;
278+
const isActiveInt = isActive === true ? 1 : 0;
243279
// first byte is the isActive instruction, second byte is true/false
244-
const data = new Buffer([4, isActiveInt])
280+
const data = new Buffer([4, isActiveInt]);
245281

246282
return {
247283
keys: [config, opsOwner, payer],
248284
programId: attesterProgramId,
249285
data: data,
250-
}
286+
};
251287
}
252288

253289
async function getWormholeMessageIx(
@@ -286,7 +322,7 @@ async function getWormholeMessageIx(
286322
emitter.toBase58(),
287323
message.toBase58(),
288324
0,
289-
Uint8Array.from(Buffer.from(payload, 'hex')),
325+
Uint8Array.from(Buffer.from(payload, "hex")),
290326
"CONFIRMED"
291327
)
292328
),
@@ -312,9 +348,12 @@ async function createWormholeMsgMultisigTx(
312348

313349
const message = Keypair.generate();
314350

315-
fs.mkdirSync('keys', {recursive: true});
351+
fs.mkdirSync("keys", { recursive: true });
316352
// save message to Uint8 array keypair file called mesage.json
317-
fs.writeFileSync(`keys/message-${txKey.toBase58()}.json`, `[${message.secretKey.toString()}]`);
353+
fs.writeFileSync(
354+
`keys/message-${txKey.toBase58()}.json`,
355+
`[${message.secretKey.toString()}]`
356+
);
318357
console.log(`Message Address: ${message.publicKey.toBase58()}`);
319358

320359
console.log("Creating wormhole instructions...");
@@ -431,7 +470,6 @@ async function executeMultisigTx(
431470
);
432471
await new Promise((resolve) => setTimeout(resolve, 10000));
433472

434-
435473
const txDetails = await squad.connection.getParsedTransaction(
436474
signature,
437475
"confirmed"
@@ -460,10 +498,10 @@ async function executeMultisigTx(
460498
const { vaaBytes } = await response.json();
461499
console.log(`VAA (Base64): ${vaaBytes}`);
462500
const parsedVaa = await parse(vaaBytes);
463-
console.log(`VAA (Hex): ${Buffer.from(vaaBytes).toString('hex')}`)
501+
console.log(`VAA (Hex): ${Buffer.from(vaaBytes).toString("hex")}`);
464502
console.log(`Emitter chain: ${parsedVaa.emitter_chain}`);
465503
console.log(`Nonce: ${parsedVaa.nonce}`);
466-
console.log(`Payload: ${Buffer.from(parsedVaa.payload).toString('hex')}`);
504+
console.log(`Payload: ${Buffer.from(parsedVaa.payload).toString("hex")}`);
467505
}
468506

469507
async function parse(data: string) {
Lines changed: 74 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
1-
import type { default as Transport } from '@ledgerhq/hw-transport';
2-
import { StatusCodes, TransportStatusError } from '@ledgerhq/hw-transport';
3-
import type { Transaction } from '@solana/web3.js';
4-
import { PublicKey } from '@solana/web3.js';
1+
import type { default as Transport } from "@ledgerhq/hw-transport";
2+
import { StatusCodes, TransportStatusError } from "@ledgerhq/hw-transport";
3+
import type { Transaction } from "@solana/web3.js";
4+
import { PublicKey } from "@solana/web3.js";
55

66
export function getDerivationPath(account?: number, change?: number): Buffer {
7-
const length = account !== undefined ? (change === undefined ? 3 : 4) : 2;
8-
const derivationPath = Buffer.alloc(1 + length * 4);
9-
10-
let offset = derivationPath.writeUInt8(length, 0);
11-
offset = derivationPath.writeUInt32BE(harden(44), offset); // Using BIP44
12-
offset = derivationPath.writeUInt32BE(harden(501), offset); // Solana's BIP44 path
13-
14-
if (account !== undefined) {
15-
offset = derivationPath.writeUInt32BE(harden(account), offset);
16-
if (change !== undefined) {
17-
derivationPath.writeUInt32BE(harden(change), offset);
18-
}
7+
const length = account !== undefined ? (change === undefined ? 3 : 4) : 2;
8+
const derivationPath = Buffer.alloc(1 + length * 4);
9+
10+
let offset = derivationPath.writeUInt8(length, 0);
11+
offset = derivationPath.writeUInt32BE(harden(44), offset); // Using BIP44
12+
offset = derivationPath.writeUInt32BE(harden(501), offset); // Solana's BIP44 path
13+
14+
if (account !== undefined) {
15+
offset = derivationPath.writeUInt32BE(harden(account), offset);
16+
if (change !== undefined) {
17+
derivationPath.writeUInt32BE(harden(change), offset);
1918
}
19+
}
2020

21-
return derivationPath;
21+
return derivationPath;
2222
}
2323

2424
const BIP32_HARDENED_BIT = (1 << 31) >>> 0;
2525

2626
function harden(n: number): number {
27-
return (n | BIP32_HARDENED_BIT) >>> 0;
27+
return (n | BIP32_HARDENED_BIT) >>> 0;
2828
}
2929

3030
const INS_GET_PUBKEY = 0x05;
@@ -41,44 +41,70 @@ const MAX_PAYLOAD = 255;
4141
const LEDGER_CLA = 0xe0;
4242

4343
/** @internal */
44-
export async function getPublicKey(transport: Transport, derivationPath: Buffer): Promise<PublicKey> {
45-
const bytes = await send(transport, INS_GET_PUBKEY, P1_NON_CONFIRM, derivationPath);
46-
return new PublicKey(bytes);
44+
export async function getPublicKey(
45+
transport: Transport,
46+
derivationPath: Buffer
47+
): Promise<PublicKey> {
48+
const bytes = await send(
49+
transport,
50+
INS_GET_PUBKEY,
51+
P1_NON_CONFIRM,
52+
derivationPath
53+
);
54+
return new PublicKey(bytes);
4755
}
4856

4957
/** @internal */
5058
export async function signTransaction(
51-
transport: Transport,
52-
transaction: Transaction,
53-
derivationPath: Buffer
59+
transport: Transport,
60+
transaction: Transaction,
61+
derivationPath: Buffer
5462
): Promise<Buffer> {
55-
const paths = Buffer.alloc(1);
56-
paths.writeUInt8(1, 0);
63+
const paths = Buffer.alloc(1);
64+
paths.writeUInt8(1, 0);
5765

58-
const message = transaction.serializeMessage();
59-
const data = Buffer.concat([paths, derivationPath, message]);
66+
const message = transaction.serializeMessage();
67+
const data = Buffer.concat([paths, derivationPath, message]);
6068

61-
return await send(transport, INS_SIGN_MESSAGE, P1_CONFIRM, data);
69+
return await send(transport, INS_SIGN_MESSAGE, P1_CONFIRM, data);
6270
}
6371

64-
async function send(transport: Transport, instruction: number, p1: number, data: Buffer): Promise<Buffer> {
65-
let p2 = 0;
66-
let offset = 0;
67-
68-
if (data.length > MAX_PAYLOAD) {
69-
while (data.length - offset > MAX_PAYLOAD) {
70-
const buffer = data.subarray(offset, offset + MAX_PAYLOAD);
71-
const response = await transport.send(LEDGER_CLA, instruction, p1, p2 | P2_MORE, buffer);
72-
// @ts-ignore -- TransportStatusError is a constructor Function, not a Class
73-
if (response.length !== 2) throw new TransportStatusError(StatusCodes.INCORRECT_DATA);
74-
75-
p2 |= P2_EXTEND;
76-
offset += MAX_PAYLOAD;
77-
}
72+
async function send(
73+
transport: Transport,
74+
instruction: number,
75+
p1: number,
76+
data: Buffer
77+
): Promise<Buffer> {
78+
let p2 = 0;
79+
let offset = 0;
80+
81+
if (data.length > MAX_PAYLOAD) {
82+
while (data.length - offset > MAX_PAYLOAD) {
83+
const buffer = data.subarray(offset, offset + MAX_PAYLOAD);
84+
const response = await transport.send(
85+
LEDGER_CLA,
86+
instruction,
87+
p1,
88+
p2 | P2_MORE,
89+
buffer
90+
);
91+
// @ts-ignore -- TransportStatusError is a constructor Function, not a Class
92+
if (response.length !== 2)
93+
throw new TransportStatusError(StatusCodes.INCORRECT_DATA);
94+
95+
p2 |= P2_EXTEND;
96+
offset += MAX_PAYLOAD;
7897
}
79-
80-
const buffer = data.subarray(offset);
81-
const response = await transport.send(LEDGER_CLA, instruction, p1, p2, buffer);
82-
83-
return response.subarray(0, response.length - 2);
98+
}
99+
100+
const buffer = data.subarray(offset);
101+
const response = await transport.send(
102+
LEDGER_CLA,
103+
instruction,
104+
p1,
105+
p2,
106+
buffer
107+
);
108+
109+
return response.subarray(0, response.length - 2);
84110
}

0 commit comments

Comments
 (0)