Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add flag for signer method #19

Merged
merged 1 commit into from
Nov 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions src/base.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { Command, Flags } from "@oclif/core";
import { CommandError } from "@oclif/core/lib/interfaces";
import { SignerType, SignerTypes } from "./helpers";
import { NetworkName, NetworkNames } from "./networks";

type TxError = { error: { reason: string } };

export abstract class BlockchainCommand extends Command {
static globalFlags = {
network: Flags.enum<NetworkName>({
description: "The network to use",
description: "The network to use.",
options: NetworkNames,
default: "testnet",
}),
Expand All @@ -19,11 +20,11 @@ export abstract class TransactionCommand extends BlockchainCommand {
...super.globalFlags,
address: Flags.string({
description: "The account address to use.",
exclusive: ["ledger"],
}),
ledger: Flags.boolean({
description: "Use Ledger wallet to sign transactions",
exclusive: ["address"],
signer: Flags.enum<SignerType>({
description: "The method for signing transactions.",
options: SignerTypes,
default: "keystore",
}),
};

Expand Down
2 changes: 1 addition & 1 deletion src/commands/project/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export default class ProjectContent extends TransactionCommand {
this.error("URL and SHA must be specified");
}

const signer = await getSigner(flags.network, flags.address, flags.ledger);
const signer = await getSigner(flags.network, flags.address, flags.signer);
const projects = await getContract(flags.network, "projects", signer);
const projectId = normalizeHex(args.ID);
const bundleSha = normalizeHex(args.SHA);
Expand Down
2 changes: 1 addition & 1 deletion src/commands/project/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export default class ProjectCreate extends TransactionCommand {
this.error("URL and SHA must be specified together");
}

const signer = await getSigner(flags.network, flags.address, flags.ledger);
const signer = await getSigner(flags.network, flags.address, flags.signer);
const projects = await getContract(flags.network, "projects", signer);
const address = await signer.getAddress();
const bundleSha = normalizeHex(args.SHA);
Expand Down
2 changes: 1 addition & 1 deletion src/commands/project/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default class ProjectDelete extends TransactionCommand {

public async run(): Promise<void> {
const { args, flags } = await this.parse(ProjectDelete);
const signer = await getSigner(flags.network, flags.address, flags.ledger);
const signer = await getSigner(flags.network, flags.address, flags.signer);
const projects = await getContract(flags.network, "projects", signer);
const projectId = normalizeHex(args.ID);
CliUx.ux.action.start("- Submitting transaction");
Expand Down
23 changes: 15 additions & 8 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import { listWallets, loadWallet } from "./keystore";
import { LedgerSigner } from "./ledger";
import { ContractName, Contracts, NetworkName, Networks } from "./networks";

export type SignerType = "keystore" | "ledger";
export const SignerTypes: SignerType[] = ["keystore", "ledger"];

const Chains: Record<number, string> = {
0: "mainnet",
5: "goerli",
Expand Down Expand Up @@ -46,15 +49,19 @@ export async function getProvider(network: NetworkName): Promise<Provider> {
return provider;
}

export async function getSigner(network: NetworkName, address: string | undefined, ledger: boolean): Promise<Signer> {
export async function getSigner(
network: NetworkName,
address: string | undefined,
signer: SignerType
): Promise<Signer> {
const url = Networks[network].url;
const provider = new ethers.providers.JsonRpcProvider(url);

let signer: Signer;
if (ledger) {
let wallet: Signer;
if (signer === "ledger") {
console.log("Make sure the Ledger wallet is unlocked and the Ethereum application is open");
signer = new LedgerSigner(provider);
const address = await signer.getAddress();
wallet = new LedgerSigner(provider);
const address = await wallet.getAddress();
console.log("Using Ledger wallet. Wallet address: ", address);
} else {
if (!address) {
Expand Down Expand Up @@ -88,11 +95,11 @@ export async function getSigner(network: NetworkName, address: string | undefine
password = res.password as string;
}

signer = await loadWallet(address, password);
signer = signer.connect(provider);
wallet = await loadWallet(address, password);
wallet = wallet.connect(provider);
}

return signer;
return wallet;
}

export async function getContract(
Expand Down