@chorus-one/signer-cactus-cosmos
Version:
Cactus signer for the Chorus One SDK, used for signing Cosmos transactions
50 lines (49 loc) • 1.86 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.CactusCosmosSigner = void 0;
class CactusCosmosSigner {
signer;
chainId;
walletKey;
constructor(params) {
const { signer, chainId } = params;
this.signer = signer;
this.chainId = chainId;
this.walletKey = undefined;
}
async init() {
this.walletKey = await this.signer.getKey(this.chainId);
}
async getPublicKey(address) {
if (this.walletKey === undefined) {
throw new Error('the signer has not been initialized');
}
if (this.walletKey?.bech32Address !== address) {
throw new Error('address does not match signer address');
}
return Uint8Array.from(Buffer.from(this.walletKey.pubKey, 'hex'));
}
async getAddress() {
if (this.walletKey === undefined) {
throw new Error('the signer has not been initialized');
}
return this.walletKey?.bech32Address;
}
async sign(signerAddress, signerData, _options) {
if (this.walletKey === undefined) {
throw new Error('the signer has not been initialized');
}
const { signDoc } = signerData.data;
const signingResponse = await this.signer.signAmino(signDoc.chain_id, signerAddress, signDoc);
const signature = signingResponse.signature.signature;
const rawSig = Uint8Array.from(Buffer.from(signature, 'base64'));
const sig = {
fullSig: Buffer.from(rawSig).toString('hex'),
r: Buffer.from(rawSig.subarray(0, 32)).toString('hex'),
s: Buffer.from(rawSig.subarray(32, 64)).toString('hex'),
v: undefined
};
return { sig, pk: await this.getPublicKey(signerAddress) };
}
}
exports.CactusCosmosSigner = CactusCosmosSigner;