UNPKG

@tmawallet/sdk

Version:
71 lines 3.18 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TMAWalletSigner = void 0; const ethers_1 = require("ethers"); class TMAWalletSigner extends ethers_1.ethers.AbstractSigner { client; constructor(client, provider) { super(provider); this.client = client; } async getAddress() { const privateKey = await this.client.accessPrivateKey(); const signingKey = new ethers_1.ethers.SigningKey(privateKey); const address = (0, ethers_1.computeAddress)(signingKey.publicKey); return address; } connect(_provider) { return new TMAWalletSigner(this.client, _provider); } async signTransaction(tx) { const privateKey = await this.client.accessPrivateKey(); const signingKey = new ethers_1.ethers.SigningKey(privateKey); const address = (0, ethers_1.computeAddress)(signingKey.publicKey); tx = (0, ethers_1.copyRequest)(tx); // Replace any Addressable or ENS name with an address const { to, from } = await (0, ethers_1.resolveProperties)({ to: tx.to ? (0, ethers_1.resolveAddress)(tx.to, this.provider) : undefined, from: tx.from ? (0, ethers_1.resolveAddress)(tx.from, this.provider) : undefined, }); if (to != null) { tx.to = to; } if (from != null) { tx.from = from; } if (tx.from != null) { (0, ethers_1.assertArgument)((0, ethers_1.getAddress)(tx.from) === address, 'transaction from address mismatch', 'tx.from', tx.from); delete tx.from; } // Build the transaction const btx = ethers_1.Transaction.from(tx); btx.signature = signingKey.sign(btx.unsignedHash); return btx.serialized; } async signMessage(message) { const privateKey = await this.client.accessPrivateKey(); const signingKey = new ethers_1.ethers.SigningKey(privateKey); return signingKey.sign((0, ethers_1.hashMessage)(message)).serialized; } async signTypedData(domain, types, value) { const privateKey = await this.client.accessPrivateKey(); const signingKey = new ethers_1.ethers.SigningKey(privateKey); // Populate any ENS names const populated = await ethers_1.TypedDataEncoder.resolveNames(domain, types, value, async (name) => { // @TODO: this should use resolveName; addresses don't // need a provider (0, ethers_1.assert)(this.provider != null, 'cannot resolve ENS names without a provider', 'UNSUPPORTED_OPERATION', { operation: 'resolveName', info: { name }, }); const address = await this.provider.resolveName(name); (0, ethers_1.assert)(address != null, 'unconfigured ENS name', 'UNCONFIGURED_NAME', { value: name, }); return address; }); return signingKey.sign(ethers_1.TypedDataEncoder.hash(populated.domain, types, populated.value)).serialized; } } exports.TMAWalletSigner = TMAWalletSigner; //# sourceMappingURL=TMAWSigner.js.map