Client/js: Add command to generate emitter addrs (#2839)

* Client/js: Add command to generate emitter addrs

Change-Id: Icb06990ca2004df3e01223a63e2142a77b10c47b

* Move emitters.ts into src dir

Change-Id: I2ca652db72ca650d8740633ceb9297000a4ece2c
This commit is contained in:
bruce-riley 2023-05-05 11:51:44 -05:00 committed by GitHub
parent a5092db982
commit b4a5bc8aef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 96 additions and 18 deletions

View File

@ -24,6 +24,8 @@ Commands:
base64 format)
worm recover <digest> <signature> Recover an address from a signature
worm info contract <network> <chain> <module> Print contract address
worm info convert-to-emitter <chain> <address> Prints address in emitter address form based
on chain encoding
worm info rpc <network> <chain> Print RPC address
worm info chain-id <chain> Print the wormhole chain ID integer
associated with the specified chain name

View File

@ -2,10 +2,10 @@ import yargs from "yargs";
import {
CHAINS,
assertChain,
isCosmWasmChain,
} from "@certusone/wormhole-sdk/lib/cjs/utils/consts";
import { impossible } from "../vaa";
import { CONTRACTS } from "../consts";
import { getEmitterAddress } from "../emitter";
exports.command = "contract <network> <chain> <module>";
exports.desc = "Print contract address";
@ -57,19 +57,7 @@ exports.handler = async (argv) => {
impossible(module);
}
if (argv["emitter"]) {
const emitter = require("@certusone/wormhole-sdk/lib/cjs/bridge/getEmitterAddress");
if (chain === "solana" || chain === "pythnet") {
// TODO: Create an isSolanaChain()
addr = await emitter.getEmitterAddressSolana(addr);
} else if (isCosmWasmChain(chain)) {
addr = await emitter.getEmitterAddressTerra(addr);
} else if (chain === "algorand") {
addr = emitter.getEmitterAddressAlgorand(BigInt(addr));
} else if (chain === "near") {
addr = emitter.getEmitterAddressNear(addr);
} else {
addr = emitter.getEmitterAddressEth(addr);
}
addr = await getEmitterAddress(chain, addr);
}
console.log(addr);
};

View File

@ -0,0 +1,26 @@
import yargs from "yargs";
import {
CHAINS,
assertChain,
} from "@certusone/wormhole-sdk/lib/cjs/utils/consts";
import { getEmitterAddress } from "../emitter";
exports.command = "convert-to-emitter <chain> <address-to-convert>";
exports.desc = "Print address in emitter address format";
exports.builder = (y: typeof yargs) => {
return y
.positional("chain", {
describe: "Chain to query",
type: "string",
choices: Object.keys(CHAINS),
})
.positional("address-to-convert", {
describe: "Address to be converted to emitter address format",
type: "string",
});
};
exports.handler = async (argv) => {
assertChain(argv["chain"]);
let chain = argv["chain"];
console.log(await getEmitterAddress(chain, argv["address-to-convert"]));
};

View File

@ -1,11 +1,12 @@
import yargs from "yargs";
exports.command = "info";
exports.desc = "Contract, chain and rpc information utilities";
exports.desc = "Contract, chain, rpc and address information utilities";
exports.builder = (y: typeof yargs) => {
// Imports modules logic from root commands, more info here -> https://github.com/yargs/yargs/blob/main/docs/advanced.md#providing-a-command-module
return y
.command(require('./chainId'))
.command(require('./rpc'))
.command(require('./contractAddress'))
.command(require("./chainId"))
.command(require("./rpc"))
.command(require("./contractAddress"))
.command(require("./convert-to-emitter"));
};

61
clients/js/src/emitter.ts Normal file
View File

@ -0,0 +1,61 @@
import {
ChainId,
ChainName,
isCosmWasmChain,
} from "@certusone/wormhole-sdk/lib/cjs/utils/consts";
export async function getEmitterAddress(
chain: ChainId | ChainName,
addr: string
) {
const emitter = require("@certusone/wormhole-sdk/lib/cjs/bridge/getEmitterAddress");
if (chain === "solana" || chain === "pythnet") {
// TODO: Create an isSolanaChain()
addr = emitter.getEmitterAddressSolana(addr);
} else if (isCosmWasmChain(chain)) {
addr = await emitter.getEmitterAddressTerra(addr);
} else if (chain === "algorand") {
addr = emitter.getEmitterAddressAlgorand(BigInt(addr));
} else if (chain === "near") {
addr = emitter.getEmitterAddressNear(addr);
} else if (chain === "aptos") {
// TODO: There should be something in the SDK to do this.
if (
addr ===
"0x576410486a2da45eee6c949c995670112ddf2fbeedab20350d506328eefc9d4f"
) {
// Mainnet / Testnet TokenBridge
addr = "0000000000000000000000000000000000000000000000000000000000000001";
} else if (
// Mainnet NFTBridge
addr ===
"0x1bdffae984043833ed7fe223f7af7a3f8902d04129b14f801823e64827da7130"
) {
addr = "0000000000000000000000000000000000000000000000000000000000000005";
} else {
throw Error(`Unsupported Aptos address: ${addr}`);
}
} else if (chain === "sui") {
// TODO: There should be something in the SDK to do this.
if (
addr ===
"0xc57508ee0d4595e5a8728974a4a93a787d38f339757230d441e895422c07aba9"
) {
// Mainnet TokenBridge
addr = "ccceeb29348f71bdd22ffef43a2a19c1f5b5e17c5cca5411529120182672ade5";
} else if (
addr ===
"0x32422cb2f929b6a4e3f81b4791ea11ac2af896b310f3d9442aa1fe924ce0bab4"
) {
// Testnet TokenBridge
addr =
"0xb22cd218bb63da447ac2704c1cc72727df6b5e981ee17a22176fd7b84c114610";
} else {
throw Error(`Unsupported Sui address: ${addr}`);
}
} else {
addr = emitter.getEmitterAddressEth(addr);
}
return addr;
}