UNPKG

dotbit-sdk-allin

Version:

A complete .bit SDK and utilities in TypeScript

87 lines 3.88 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.BitSigner = exports.mmJsonHashAndChainIdHex = void 0; const eth_sig_util_1 = require("@metamask/eth-sig-util"); const const_1 = require("../const"); const DotbitError_1 = require("../errors/DotbitError"); const common_1 = require("../tools/common"); function isMMJson(txs) { return 'mm_json' in txs; } function mmJsonHashAndChainIdHex(typedData, chainId) { const mmHash = eth_sig_util_1.TypedDataUtils.eip712Hash(typedData, eth_sig_util_1.SignTypedDataVersion.V4).toString('hex'); const chainIdHex = chainId.toString(16).padStart(16, '0'); return mmHash + chainIdHex; } exports.mmJsonHashAndChainIdHex = mmJsonHashAndChainIdHex; class BitSigner { signPersonal(data) { return this.signer.signMessage(data); } getAddress() { return this.signer.getAddress(); } getChainId() { return this.signer.getChainId(); } getCoinType() { return __awaiter(this, void 0, void 0, function* () { const chainId = yield this.getChainId(); const coinType = const_1.EvmChainId2CoinType[chainId]; if (!coinType) { throw new DotbitError_1.DotbitError(`Unsupported EVM chainId: ${chainId}`, DotbitError_1.BitErrorCode.UnsupportedEVMChainId); } return coinType; }); } signData(data, isEIP712) { return __awaiter(this, void 0, void 0, function* () { if (isEIP712) { return yield this.signTypedData(data); } else { return yield this.signPersonal(data); } }); } signTxList(txs) { return __awaiter(this, void 0, void 0, function* () { if (isMMJson(txs)) { for (const signItem of txs.sign_list) { if (signItem.sign_msg) { if (signItem.sign_type === const_1.AlgorithmId.eip712) { const mmJson = JSON.parse(JSON.stringify(txs.mm_json)); mmJson.message.digest = signItem.sign_msg; const signDataRes = yield this.signTypedData(mmJson); signItem.sign_msg = signDataRes + mmJsonHashAndChainIdHex(mmJson, mmJson.domain.chainId); } else { signItem.sign_msg = yield this.signData(Buffer.from((0, common_1.remove0x)(signItem.sign_msg), 'hex')); } } } } else { for (const list of txs.list) { for (const signItem of list.sign_list) { if (signItem.sign_msg) { signItem.sign_msg = yield this.signData(Buffer.from((0, common_1.remove0x)(signItem.sign_msg), 'hex')); } } } } return txs; }); } } exports.BitSigner = BitSigner; //# sourceMappingURL=BitSigner.js.map