clients/js: formatting only change (prettier)

Change-Id: I0b8805e0a577f0ef8757a5468c43fcb1083c0825
This commit is contained in:
Paul Noel 2022-08-18 14:42:11 +00:00 committed by Paul Noel
parent a6f75f1aef
commit 338e6f2648
1 changed files with 331 additions and 234 deletions

View File

@ -4,9 +4,20 @@ import yargs from "yargs";
import { hideBin } from "yargs/helpers";
import { Bech32, fromBech32, toHex } from "@cosmjs/encoding";
import { isTerraChain, assertEVMChain, CONTRACTS, setDefaultWasm } from "@certusone/wormhole-sdk";
import {
isTerraChain,
assertEVMChain,
CONTRACTS,
setDefaultWasm,
} from "@certusone/wormhole-sdk";
import { execute_solana } from "./solana";
import { execute_evm, getImplementation, hijack_evm, query_contract_evm, setStorageAt } from "./evm";
import {
execute_evm,
getImplementation,
hijack_evm,
query_contract_evm,
setStorageAt,
} from "./evm";
import { execute_terra } from "./terra";
import * as vaa from "./vaa";
import { impossible, Payload, serialiseVAA, VAA } from "./vaa";
@ -100,7 +111,10 @@ yargs(hideBin(process.argv))
type: "RegisterChain",
chain: 0,
emitterChain: toChainId(argv["chain"]),
emitterAddress: parseAddress(argv["chain"], argv["contract-address"]),
emitterAddress: parseAddress(
argv["chain"],
argv["contract-address"]
),
};
let v = makeVAA(
GOVERNANCE_CHAIN,
@ -148,7 +162,10 @@ yargs(hideBin(process.argv))
module,
type: "ContractUpgrade",
chain: toChainId(argv["chain"]),
address: parseCodeAddress(argv["chain"], argv["contract-address"]),
address: parseCodeAddress(
argv["chain"],
argv["contract-address"]
),
};
let v = makeVAA(
GOVERNANCE_CHAIN,
@ -179,233 +196,305 @@ yargs(hideBin(process.argv))
async (argv) => {
let buf: Buffer;
try {
buf = Buffer.from(String(argv.vaa), "hex")
buf = Buffer.from(String(argv.vaa), "hex");
if (buf.length == 0) {
throw Error("Couldn't parse VAA as hex")
throw Error("Couldn't parse VAA as hex");
}
} catch (e) {
buf = Buffer.from(String(argv.vaa), "base64")
buf = Buffer.from(String(argv.vaa), "base64");
if (buf.length == 0) {
throw Error("Couldn't parse VAA as base64 or hex")
throw Error("Couldn't parse VAA as base64 or hex");
}
}
const parsed_vaa = vaa.parse(buf);
let parsed_vaa_with_digest = parsed_vaa;
parsed_vaa_with_digest['digest'] = vaa.vaaDigest(parsed_vaa);
parsed_vaa_with_digest["digest"] = vaa.vaaDigest(parsed_vaa);
console.log(parsed_vaa_with_digest);
})
.command("recover <digest> <signature>", "Recover an address from a signature", (yargs) => {
return yargs
.positional("digest", {
describe: "digest",
type: "string"
})
.positional("signature", {
describe: "signature",
type: "string"
});
}, async (argv) => {
console.log(ethers.utils.recoverAddress(hex(argv["digest"]), hex(argv["signature"])))
})
.command("contract <network> <chain> <module>", "Print contract address", (yargs) => {
return yargs
.positional("network", {
describe: "network",
type: "string",
choices: ["mainnet", "testnet", "devnet"],
})
.positional("chain", {
describe: "Chain to query",
type: "string",
choices: Object.keys(CHAINS),
})
.positional("module", {
describe: "Module to query",
type: "string",
choices: ["Core", "NFTBridge", "TokenBridge"],
})
}, async (argv) => {
assertChain(argv["chain"])
assertEVMChain(argv["chain"])
const network = argv.network.toUpperCase();
if (
network !== "MAINNET" &&
network !== "TESTNET" &&
network !== "DEVNET"
) {
throw Error(`Unknown network: ${network}`);
}
let module = argv["module"] as
| "Core"
| "NFTBridge"
| "TokenBridge";
switch (module) {
case "Core":
console.log(CONTRACTS[network][argv["chain"]]["core"])
break;
case "NFTBridge":
console.log(CONTRACTS[network][argv["chain"]]["nft_bridge"])
break;
case "TokenBridge":
console.log(CONTRACTS[network][argv["chain"]]["token_bridge"])
break;
default:
impossible(module)
)
.command(
"recover <digest> <signature>",
"Recover an address from a signature",
(yargs) => {
return yargs
.positional("digest", {
describe: "digest",
type: "string",
})
.positional("signature", {
describe: "signature",
type: "string",
});
},
async (argv) => {
console.log(
ethers.utils.recoverAddress(hex(argv["digest"]), hex(argv["signature"]))
);
}
})
.command("rpc <network> <chain>", "Print RPC address", (yargs) => {
return yargs
.positional("network", {
describe: "network",
type: "string",
choices: ["mainnet", "testnet", "devnet"],
})
.positional("chain", {
describe: "Chain to query",
type: "string",
choices: Object.keys(CHAINS),
})
}, async (argv) => {
assertChain(argv["chain"])
assertEVMChain(argv["chain"])
const network = argv.network.toUpperCase();
if (
network !== "MAINNET" &&
network !== "TESTNET" &&
network !== "DEVNET"
) {
throw Error(`Unknown network: ${network}`);
)
.command(
"contract <network> <chain> <module>",
"Print contract address",
(yargs) => {
return yargs
.positional("network", {
describe: "network",
type: "string",
choices: ["mainnet", "testnet", "devnet"],
})
.positional("chain", {
describe: "Chain to query",
type: "string",
choices: Object.keys(CHAINS),
})
.positional("module", {
describe: "Module to query",
type: "string",
choices: ["Core", "NFTBridge", "TokenBridge"],
});
},
async (argv) => {
assertChain(argv["chain"]);
assertEVMChain(argv["chain"]);
const network = argv.network.toUpperCase();
if (
network !== "MAINNET" &&
network !== "TESTNET" &&
network !== "DEVNET"
) {
throw Error(`Unknown network: ${network}`);
}
let module = argv["module"] as "Core" | "NFTBridge" | "TokenBridge";
switch (module) {
case "Core":
console.log(CONTRACTS[network][argv["chain"]]["core"]);
break;
case "NFTBridge":
console.log(CONTRACTS[network][argv["chain"]]["nft_bridge"]);
break;
case "TokenBridge":
console.log(CONTRACTS[network][argv["chain"]]["token_bridge"]);
break;
default:
impossible(module);
}
}
console.log(NETWORKS[network][argv["chain"]].rpc)
})
)
.command(
"rpc <network> <chain>",
"Print RPC address",
(yargs) => {
return yargs
.positional("network", {
describe: "network",
type: "string",
choices: ["mainnet", "testnet", "devnet"],
})
.positional("chain", {
describe: "Chain to query",
type: "string",
choices: Object.keys(CHAINS),
});
},
async (argv) => {
assertChain(argv["chain"]);
assertEVMChain(argv["chain"]);
const network = argv.network.toUpperCase();
if (
network !== "MAINNET" &&
network !== "TESTNET" &&
network !== "DEVNET"
) {
throw Error(`Unknown network: ${network}`);
}
console.log(NETWORKS[network][argv["chain"]].rpc);
}
)
////////////////////////////////////////////////////////////////////////////////
// Evm utilities
.command("evm", "EVM utilites", (yargs) => {
return yargs
.option("rpc", {
describe: "RPC endpoint",
type: "string",
required: false
})
.command("address-from-secret <secret>", "Compute a 20 byte eth address from a 32 byte private key", (yargs) => {
return yargs
.positional("secret", { type: "string", describe: "Secret key (32 bytes)" })
}, (argv) => {
console.log(ethers.utils.computeAddress(argv["secret"]))
})
.command("storage-update", "Update a storage slot on an EVM fork during testing (anvil or hardhat)", (yargs) => {
return yargs
.option("contract-address", {
alias: "a",
describe: "Contract address",
type: "string",
required: true,
})
.option("storage-slot", {
alias: "k",
describe: "Storage slot to modify",
type: "string",
required: true,
})
.option("value", {
alias: "v",
describe: "Value to write into the slot (32 bytes)",
type: "string",
required: true,
});
}, async (argv) => {
const result = await setStorageAt(argv["rpc"], evm_address(argv["contract-address"]), argv["storage-slot"], ["uint256"], [argv["value"]]);
console.log(result);
})
.command("chains", "Return all EVM chains",
async (_) => {
console.log(Object.values(CHAINS).map(id => toChainName(id)).filter(name => isEVMChain(name)).join(" "))
}
)
.command("info", "Query info about the on-chain state of the contract", (yargs) => {
return yargs
.option("chain", {
alias: "c",
describe: "Chain to query",
type: "string",
choices: Object.keys(CHAINS),
required: true,
})
.option("module", {
alias: "m",
describe: "Module to query",
type: "string",
choices: ["Core", "NFTBridge", "TokenBridge"],
required: true,
})
.option("network", {
alias: "n",
describe: "network",
type: "string",
choices: ["mainnet", "testnet", "devnet"],
required: true,
})
.option("contract-address", {
alias: "a",
describe: "Contract to query (override config)",
type: "string",
required: false,
})
.option("implementation-only", {
alias: "i",
describe: "Only query implementation (faster)",
type: "boolean",
default: false,
required: false,
});
}, async (argv) => {
assertChain(argv["chain"])
assertEVMChain(argv["chain"])
const network = argv.network.toUpperCase();
if (
network !== "MAINNET" &&
network !== "TESTNET" &&
network !== "DEVNET"
) {
throw Error(`Unknown network: ${network}`);
}
let module = argv["module"] as
| "Core"
| "NFTBridge"
| "TokenBridge";
let rpc = argv["rpc"] ?? NETWORKS[network][argv["chain"]].rpc
if (argv["implementation-only"]) {
console.log(await getImplementation(network, argv["chain"], module, argv["contract-address"], rpc))
} else {
console.log(JSON.stringify(await query_contract_evm(network, argv["chain"], module, argv["contract-address"], rpc), null, 2))
}
})
.command("hijack", "Override the guardian set of the core bridge contract during testing (anvil or hardhat)", (yargs) => {
return yargs
.option("core-contract-address", {
alias: "a",
describe: "Core contract address",
type: "string",
default: CONTRACTS.MAINNET.ethereum.core,
})
.option("guardian-address", {
alias: "g",
required: true,
describe: "Guardians' public addresses (CSV)",
type: "string",
})
.option("guardian-set-index", {
alias: "i",
required: false,
describe: "New guardian set index (if unspecified, default to overriding the current index)",
type: "number"
});
}, async (argv) => {
const guardian_addresses = argv["guardian-address"].split(",")
let rpc = argv["rpc"] ?? NETWORKS.DEVNET.ethereum.rpc
await hijack_evm(rpc, argv["core-contract-address"], guardian_addresses, argv["guardian-set-index"])
})
},
.command(
"evm",
"EVM utilites",
(yargs) => {
return yargs
.option("rpc", {
describe: "RPC endpoint",
type: "string",
required: false,
})
.command(
"address-from-secret <secret>",
"Compute a 20 byte eth address from a 32 byte private key",
(yargs) => {
return yargs.positional("secret", {
type: "string",
describe: "Secret key (32 bytes)",
});
},
(argv) => {
console.log(ethers.utils.computeAddress(argv["secret"]));
}
)
.command(
"storage-update",
"Update a storage slot on an EVM fork during testing (anvil or hardhat)",
(yargs) => {
return yargs
.option("contract-address", {
alias: "a",
describe: "Contract address",
type: "string",
required: true,
})
.option("storage-slot", {
alias: "k",
describe: "Storage slot to modify",
type: "string",
required: true,
})
.option("value", {
alias: "v",
describe: "Value to write into the slot (32 bytes)",
type: "string",
required: true,
});
},
async (argv) => {
const result = await setStorageAt(
argv["rpc"],
evm_address(argv["contract-address"]),
argv["storage-slot"],
["uint256"],
[argv["value"]]
);
console.log(result);
}
)
.command("chains", "Return all EVM chains", async (_) => {
console.log(
Object.values(CHAINS)
.map((id) => toChainName(id))
.filter((name) => isEVMChain(name))
.join(" ")
);
})
.command(
"info",
"Query info about the on-chain state of the contract",
(yargs) => {
return yargs
.option("chain", {
alias: "c",
describe: "Chain to query",
type: "string",
choices: Object.keys(CHAINS),
required: true,
})
.option("module", {
alias: "m",
describe: "Module to query",
type: "string",
choices: ["Core", "NFTBridge", "TokenBridge"],
required: true,
})
.option("network", {
alias: "n",
describe: "network",
type: "string",
choices: ["mainnet", "testnet", "devnet"],
required: true,
})
.option("contract-address", {
alias: "a",
describe: "Contract to query (override config)",
type: "string",
required: false,
})
.option("implementation-only", {
alias: "i",
describe: "Only query implementation (faster)",
type: "boolean",
default: false,
required: false,
});
},
async (argv) => {
assertChain(argv["chain"]);
assertEVMChain(argv["chain"]);
const network = argv.network.toUpperCase();
if (
network !== "MAINNET" &&
network !== "TESTNET" &&
network !== "DEVNET"
) {
throw Error(`Unknown network: ${network}`);
}
let module = argv["module"] as "Core" | "NFTBridge" | "TokenBridge";
let rpc = argv["rpc"] ?? NETWORKS[network][argv["chain"]].rpc;
if (argv["implementation-only"]) {
console.log(
await getImplementation(
network,
argv["chain"],
module,
argv["contract-address"],
rpc
)
);
} else {
console.log(
JSON.stringify(
await query_contract_evm(
network,
argv["chain"],
module,
argv["contract-address"],
rpc
),
null,
2
)
);
}
}
)
.command(
"hijack",
"Override the guardian set of the core bridge contract during testing (anvil or hardhat)",
(yargs) => {
return yargs
.option("core-contract-address", {
alias: "a",
describe: "Core contract address",
type: "string",
default: CONTRACTS.MAINNET.ethereum.core,
})
.option("guardian-address", {
alias: "g",
required: true,
describe: "Guardians' public addresses (CSV)",
type: "string",
})
.option("guardian-set-index", {
alias: "i",
required: false,
describe:
"New guardian set index (if unspecified, default to overriding the current index)",
type: "number",
});
},
async (argv) => {
const guardian_addresses = argv["guardian-address"].split(",");
let rpc = argv["rpc"] ?? NETWORKS.DEVNET.ethereum.rpc;
await hijack_evm(
rpc,
argv["core-contract-address"],
guardian_addresses,
argv["guardian-set-index"]
);
}
);
},
(_) => {
yargs.showHelp();
}
@ -445,9 +534,10 @@ yargs(hideBin(process.argv))
.option("rpc", {
describe: "RPC endpoint",
type: "string",
required: false
})
}, async (argv) => {
required: false,
});
},
async (argv) => {
const vaa_hex = String(argv.vaa);
const buf = Buffer.from(vaa_hex, "hex");
const parsed_vaa = vaa.parse(buf);
@ -504,7 +594,14 @@ yargs(hideBin(process.argv))
"This VAA does not specify the target chain, please provide it by hand using the '--chain' flag."
);
} else if (isEVMChain(chain)) {
await execute_evm(parsed_vaa.payload, buf, network, chain, argv["contract-address"], argv["rpc"]);
await execute_evm(
parsed_vaa.payload,
buf,
network,
chain,
argv["contract-address"],
argv["rpc"]
);
} else if (isTerraChain(chain)) {
await execute_terra(parsed_vaa.payload, buf, network, chain);
} else if (chain === "solana") {
@ -530,44 +627,44 @@ yargs(hideBin(process.argv))
).argv;
function hex(x: string): string {
return ethers.utils.hexlify(x, { allowMissingPrefix: true })
return ethers.utils.hexlify(x, { allowMissingPrefix: true });
}
function evm_address(x: string): string {
return hex(x).substring(2).padStart(64, "0")
return hex(x).substring(2).padStart(64, "0");
}
function parseAddress(chain: ChainName, address: string): string {
if (chain === "unset") {
throw Error("Chain unset")
throw Error("Chain unset");
} else if (isEVMChain(chain)) {
return "0x" + evm_address(address)
return "0x" + evm_address(address);
} else if (isTerraChain(chain)) {
return "0x" + toHex(fromBech32(address).data).padStart(64, "0")
return "0x" + toHex(fromBech32(address).data).padStart(64, "0");
} else if (chain === "solana") {
return "0x" + toHex(base58.decode(address)).padStart(64, "0")
return "0x" + toHex(base58.decode(address)).padStart(64, "0");
} else if (chain === "algorand") {
// TODO: is there a better native format for algorand?
return "0x" + evm_address(address)
return "0x" + evm_address(address);
} else if (chain === "near") {
throw Error("NEAR is not supported yet")
throw Error("NEAR is not supported yet");
} else if (chain === "injective") {
throw Error("INJECTIVE is not supported yet");
} else if (chain === "osmosis") {
throw Error("OSMOSIS is not supported yet");
} else if (chain === "sui") {
throw Error("SUI is not supported yet")
throw Error("SUI is not supported yet");
} else if (chain === "aptos") {
throw Error("APTOS is not supported yet")
throw Error("APTOS is not supported yet");
} else {
impossible(chain)
impossible(chain);
}
}
function parseCodeAddress(chain: ChainName, address: string): string {
if (isTerraChain(chain)) {
return "0x" + parseInt(address, 10).toString(16).padStart(64, "0")
return "0x" + parseInt(address, 10).toString(16).padStart(64, "0");
} else {
return parseAddress(chain, address)
return parseAddress(chain, address);
}
}