UNPKG

@constellation-labs/key-manager

Version:
45 lines 2.1 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.AwsKmsSigner = void 0; const ethers_1 = require("ethers"); const aws_kms_utils_1 = require("./util/aws-kms-utils"); class AwsKmsSigner extends ethers_1.ethers.Signer { constructor(kmsCredentials, provider) { super(); ethers_1.ethers.utils.defineReadOnly(this, 'provider', provider || null); ethers_1.ethers.utils.defineReadOnly(this, 'kmsCredentials', kmsCredentials); } async getAddress() { if (this.ethereumAddress === undefined) { const key = await (0, aws_kms_utils_1.getPublicKey)(this.kmsCredentials); this.ethereumAddress = (0, aws_kms_utils_1.getEthereumAddress)(key.PublicKey); } return Promise.resolve(this.ethereumAddress); } async _signDigest(digestString) { const digestBuffer = Buffer.from(ethers_1.ethers.utils.arrayify(digestString)); const sig = await (0, aws_kms_utils_1.requestKmsSignature)(digestBuffer, this.kmsCredentials); const ethAddr = await this.getAddress(); const { v } = (0, aws_kms_utils_1.determineCorrectV)(digestBuffer, sig.r, sig.s, ethAddr); return ethers_1.ethers.utils.joinSignature({ v, r: `0x${sig.r.toString('hex')}`, s: `0x${sig.s.toString('hex')}`, }); } async signMessage(message) { return this._signDigest(ethers_1.ethers.utils.hashMessage(message)); } async signTransaction(transaction) { const unsignedTx = await ethers_1.ethers.utils.resolveProperties(transaction); delete unsignedTx.from; const serializedTx = ethers_1.ethers.utils.serializeTransaction(unsignedTx); const transactionSignature = await this._signDigest(ethers_1.ethers.utils.keccak256(serializedTx)); return ethers_1.ethers.utils.serializeTransaction(unsignedTx, transactionSignature); } connect(provider) { return new AwsKmsSigner(this.kmsCredentials, provider); } } exports.AwsKmsSigner = AwsKmsSigner; //# sourceMappingURL=kms.js.map