@dolomite-exchange/dolomite-margin
Version:
Ethereum Smart Contracts and TypeScript library used for the DolomiteMargin trading protocol
67 lines • 2.63 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Signer = void 0;
const web3_1 = __importDefault(require("web3"));
const es6_promisify_1 = require("es6-promisify");
const BytesHelper_1 = require("../lib/BytesHelper");
const SignatureHelper_1 = require("../lib/SignatureHelper");
const types_1 = require("../../src/types");
class Signer {
// ============ Constructor ============
constructor(web3) {
this.web3 = web3;
}
// ============ Functions ============
/**
* Returns a signable EIP712 Hash of a struct
*/
getEIP712Hash(structHash) {
return web3_1.default.utils.soliditySha3({ t: 'bytes2', v: '0x1901' }, { t: 'bytes32', v: this.getDomainHash() }, { t: 'bytes32', v: structHash });
}
async ethSignTypedDataInternal(signer, data, signingMethod) {
let sendMethod;
let rpcMethod;
let rpcData;
switch (signingMethod) {
case types_1.SigningMethod.TypedData:
sendMethod = 'send';
rpcMethod = 'eth_signTypedData';
rpcData = data;
break;
case types_1.SigningMethod.MetaMask:
sendMethod = 'sendAsync';
rpcMethod = 'eth_signTypedData_v3';
rpcData = JSON.stringify(data);
break;
case types_1.SigningMethod.MetaMaskLatest:
sendMethod = 'sendAsync';
rpcMethod = 'eth_signTypedData_v4';
rpcData = JSON.stringify(data);
break;
case types_1.SigningMethod.CoinbaseWallet:
sendMethod = 'sendAsync';
rpcMethod = 'eth_signTypedData';
rpcData = data;
break;
default:
throw new Error(`Invalid signing method ${signingMethod}`);
}
const provider = this.web3.currentProvider;
const sendAsync = es6_promisify_1.promisify(provider[sendMethod]).bind(provider);
const response = await sendAsync({
method: rpcMethod,
params: [signer, rpcData],
jsonrpc: '2.0',
id: new Date().getTime(),
});
if (response.error) {
throw new Error(response.error.message);
}
return `0x${BytesHelper_1.stripHexPrefix(response.result)}0${SignatureHelper_1.SIGNATURE_TYPES.NO_PREPEND}`;
}
}
exports.Signer = Signer;
//# sourceMappingURL=Signer.js.map