@hyperlane-xyz/sdk
Version:
The official SDK for the Hyperlane Network
39 lines • 1.91 kB
JavaScript
import { DirectSecp256k1Wallet } from '@cosmjs/proto-signing';
import { GasPrice, SigningStargateClient } from '@cosmjs/stargate';
import { assert, strip0x } from '@hyperlane-xyz/utils';
export class CosmosNativeMultiProtocolSignerAdapter {
chainName;
accountAddress;
signer;
constructor(chainName, accountAddress, signer) {
this.chainName = chainName;
this.accountAddress = accountAddress;
this.signer = signer;
}
static async init(chainName, privateKey, multiProtocolProvider) {
const { bech32Prefix, rpcUrls, gasPrice } = multiProtocolProvider.getChainMetadata(chainName);
const [rpc] = rpcUrls;
assert(bech32Prefix, 'prefix is required for cosmos chains');
assert(rpc, 'rpc is required for configuring cosmos chains');
assert(gasPrice, 'gas price is required for cosmos chains');
const wallet = await DirectSecp256k1Wallet.fromKey(Buffer.from(strip0x(privateKey), 'hex'), bech32Prefix);
const [account] = await wallet.getAccounts();
assert(account, 'account not found for cosmos chain');
const signer = await SigningStargateClient.connectWithSigner(rpc.http, wallet, {
gasPrice: GasPrice.fromString(`${gasPrice.amount}${gasPrice.denom}`),
});
return new CosmosNativeMultiProtocolSignerAdapter(chainName, account.address, signer);
}
async address() {
return this.accountAddress;
}
async sendAndConfirmTransaction(tx, _options) {
await this.signer.simulate(this.accountAddress, [tx.transaction], undefined);
const res = await this.signer.signAndBroadcast(this.accountAddress, [tx.transaction], 'auto');
if (res.code !== 0) {
throw new Error(`Transaction ${res.transactionHash} failed on chain ${this.chainName}`);
}
return res.transactionHash;
}
}
//# sourceMappingURL=cosmjs.js.map