From 918a968703447c772b4b0fc2cc3ea19ea79b700a Mon Sep 17 00:00:00 2001 From: Csongor Kiss Date: Wed, 2 Mar 2022 01:06:27 +0000 Subject: [PATCH] clients: allow passing solana private key Currently the typescript clients generate a fresh wallet and request airdrop into them. This obviously only works on tiltnet and testnet. Here we add an option to allow specifying a private key, which makes the command work in mainnet. --- clients/nft_bridge/main.ts | 36 +++++++++++++++++++++++------------- clients/token_bridge/main.ts | 26 ++++++++++++++++++-------- 2 files changed, 41 insertions(+), 21 deletions(-) diff --git a/clients/nft_bridge/main.ts b/clients/nft_bridge/main.ts index 0482eed8c..9fe725dfb 100644 --- a/clients/nft_bridge/main.ts +++ b/clients/nft_bridge/main.ts @@ -1,18 +1,18 @@ import yargs from "yargs"; -const {hideBin} = require('yargs/helpers') +const { hideBin } = require('yargs/helpers') import * as elliptic from "elliptic"; import * as ethers from "ethers"; import * as web3s from '@solana/web3.js'; -import {PublicKey, TransactionInstruction, AccountMeta, Keypair, Connection} from "@solana/web3.js"; -import {solidityKeccak256} from "ethers/lib/utils"; +import { PublicKey, TransactionInstruction, AccountMeta, Keypair, Connection } from "@solana/web3.js"; +import { base58, solidityKeccak256 } from "ethers/lib/utils"; -import {setDefaultWasm, importCoreWasm, importNftWasm, ixFromRust, BridgeImplementation__factory} from '@certusone/wormhole-sdk' +import { setDefaultWasm, importCoreWasm, importNftWasm, ixFromRust, BridgeImplementation__factory } from '@certusone/wormhole-sdk' setDefaultWasm("node") -const signAndEncodeVM = function ( +const signAndEncodeVM = function( timestamp, nonce, emitterChainId, @@ -40,7 +40,7 @@ const signAndEncodeVM = function ( for (let i in signers) { const ec = new elliptic.ec("secp256k1"); const key = ec.keyFromPrivate(signers[i]); - const signature = key.sign(Buffer.from(hash.substr(2), "hex"), {canonical: true}); + const signature = key.sign(Buffer.from(hash.substr(2), "hex"), { canonical: true }); const packSig = [ ethers.utils.defaultAbiCoder.encode(["uint8"], [i]).substring(2 + (64 - 2)), @@ -135,6 +135,12 @@ yargs(hideBin(process.argv)) description: 'NFT Bridge address', default: "NFTWqJR8YnRVqPDvTJrYuLrQDitTG5AScqbeghi4zSA" }) + .option('key', { + alias: 'k', + type: 'string', + description: 'Private key of the wallet', + required: false + }) }, async (argv: any) => { const bridge = await importCoreWasm() const nft_bridge = await importNftWasm() @@ -143,13 +149,17 @@ yargs(hideBin(process.argv)) let bridge_id = new PublicKey(argv.bridge); let nft_bridge_id = new PublicKey(argv.nft_bridge); - // Generate a new random public key - let from = web3s.Keypair.generate(); - let airdropSignature = await connection.requestAirdrop( - from.publicKey, - web3s.LAMPORTS_PER_SOL, - ); - await connection.confirmTransaction(airdropSignature); + var from: web3s.Keypair; + if (argv.key) { + from = web3s.Keypair.fromSecretKey(base58.decode(argv.key)); + } else { + from = web3s.Keypair.generate(); + let airdropSignature = await connection.requestAirdrop( + from.publicKey, + web3s.LAMPORTS_PER_SOL, + ); + await connection.confirmTransaction(airdropSignature); + } let vaa = Buffer.from(argv.vaa, "hex"); await post_vaa(connection, bridge_id, from, vaa); diff --git a/clients/token_bridge/main.ts b/clients/token_bridge/main.ts index 1d51d491a..7f263d690 100644 --- a/clients/token_bridge/main.ts +++ b/clients/token_bridge/main.ts @@ -10,7 +10,7 @@ import {fromUint8Array} from "js-base64"; import {LCDClient, MnemonicKey} from '@terra-money/terra.js'; import {MsgExecuteContract} from "@terra-money/terra.js"; import {PublicKey, TransactionInstruction, AccountMeta, Keypair, Connection} from "@solana/web3.js"; -import {solidityKeccak256} from "ethers/lib/utils"; +import {base58, solidityKeccak256} from "ethers/lib/utils"; import {setDefaultWasm, importCoreWasm, importTokenWasm, ixFromRust, BridgeImplementation__factory} from '@certusone/wormhole-sdk' setDefaultWasm("node") @@ -255,6 +255,12 @@ yargs(hideBin(process.argv)) description: 'Token Bridge address', default: "B6RHG3mfcckmrYN1UhmJzyS1XX3fZKbkeUcpJe9Sy3FE" }) + .option('key', { + alias: 'k', + type: 'string', + description: 'Private key of the wallet', + required: false + }) }, async (argv: any) => { const bridge = await importCoreWasm() const token_bridge = await importTokenWasm() @@ -263,13 +269,17 @@ yargs(hideBin(process.argv)) let bridge_id = new PublicKey(argv.bridge); let token_bridge_id = new PublicKey(argv.token_bridge); - // Generate a new random public key - let from = web3s.Keypair.generate(); - let airdropSignature = await connection.requestAirdrop( - from.publicKey, - web3s.LAMPORTS_PER_SOL, - ); - await connection.confirmTransaction(airdropSignature); + var from: web3s.Keypair; + if (argv.key) { + from = web3s.Keypair.fromSecretKey(base58.decode(argv.key)); + } else { + from = web3s.Keypair.generate(); + let airdropSignature = await connection.requestAirdrop( + from.publicKey, + web3s.LAMPORTS_PER_SOL, + ); + await connection.confirmTransaction(airdropSignature); + } let vaa = Buffer.from(argv.vaa, "hex"); await post_vaa(connection, bridge_id, from, vaa);