UNPKG

@dolomite-exchange/dolomite-margin

Version:

Ethereum Smart Contracts and TypeScript library used for the DolomiteMargin trading protocol

89 lines 4.34 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.WalletLogin = void 0; const web3_1 = __importDefault(require("web3")); const Signer_1 = require("./Signer"); const types_1 = require("../../src/types"); const Helpers_1 = require("../lib/Helpers"); const BytesHelper_1 = require("../lib/BytesHelper"); const SignatureHelper_1 = require("../lib/SignatureHelper"); const EIP712_WALLET_LOGIN_STRUCT = [ { type: 'string', name: 'action' }, { type: 'string', name: 'expiration' }, ]; class WalletLogin extends Signer_1.Signer { constructor(web3, networkId, { domain = 'Dolomite', version = '1.0', } = {}) { super(web3); this.domain = domain; this.networkId = networkId; this.version = version; this.EIP712_WALLET_LOGIN_STRUCT_STRING = 'Dolomite(' + 'string action,' + 'string expiration' + ')'; } async signLogin(expiration, signer, signingMethod) { switch (signingMethod) { case types_1.SigningMethod.Hash: case types_1.SigningMethod.UnsafeHash: case types_1.SigningMethod.Compatibility: { const hash = this.getWalletLoginHash(expiration); const rawSignature = await this.web3.eth.sign(hash, signer); const hashSig = SignatureHelper_1.createTypedSignature(rawSignature, SignatureHelper_1.SIGNATURE_TYPES.DECIMAL); if (signingMethod === types_1.SigningMethod.Hash) { return hashSig; } const unsafeHashSig = SignatureHelper_1.createTypedSignature(rawSignature, SignatureHelper_1.SIGNATURE_TYPES.NO_PREPEND); if (signingMethod === types_1.SigningMethod.UnsafeHash) { return unsafeHashSig; } if (this.walletLoginIsValid(expiration, unsafeHashSig, signer)) { return unsafeHashSig; } return hashSig; } case types_1.SigningMethod.TypedData: case types_1.SigningMethod.MetaMask: case types_1.SigningMethod.MetaMaskLatest: case types_1.SigningMethod.CoinbaseWallet: { const data = { types: { EIP712Domain: SignatureHelper_1.EIP712_DOMAIN_STRUCT_NO_CONTRACT, [this.domain]: EIP712_WALLET_LOGIN_STRUCT, }, domain: this.getDomainData(), primaryType: this.domain, message: { action: 'Login', expiration: expiration.toUTCString(), }, }; return this.ethSignTypedDataInternal(signer, data, signingMethod); } default: throw new Error(`Invalid signing method ${signingMethod}`); } } walletLoginIsValid(expiration, typedSignature, expectedSigner) { const hash = this.getWalletLoginHash(expiration); const signer = SignatureHelper_1.ecRecoverTypedSignature(hash, typedSignature); return BytesHelper_1.addressesAreEqual(signer, expectedSigner) && expiration > new Date(); } getDomainHash() { return web3_1.default.utils.soliditySha3({ t: 'bytes32', v: BytesHelper_1.hashString(SignatureHelper_1.EIP712_DOMAIN_STRING_NO_CONTRACT) }, { t: 'bytes32', v: BytesHelper_1.hashString(this.domain) }, { t: 'bytes32', v: BytesHelper_1.hashString(this.version) }, { t: 'uint256', v: Helpers_1.toString(this.networkId) }); } getWalletLoginHash(expiration) { const structHash = web3_1.default.utils.soliditySha3({ t: 'bytes32', v: BytesHelper_1.hashString(this.EIP712_WALLET_LOGIN_STRUCT_STRING) }, { t: 'bytes32', v: BytesHelper_1.hashString('Login') }, { t: 'bytes32', v: BytesHelper_1.hashString(expiration.toUTCString()) }); return this.getEIP712Hash(structHash); } getDomainData() { return { name: this.domain, version: this.version, chainId: this.networkId, }; } } exports.WalletLogin = WalletLogin; //# sourceMappingURL=WalletLogin.js.map