UNPKG

@urbanisierung/flethly

Version:

easily sell digital assets with ether

154 lines 5.26 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Web3Controller = void 0; const web3_1 = __importDefault(require("web3")); const signedMessage_constant_1 = require("../constants/signedMessage.constant"); const network_controller_1 = require("./network.controller"); const sigUtil = require('eth-sig-util'); const web3object = 'web3'; const ethObject = 'ethereum'; class Web3Controller { constructor() { } async signMessage() { this.initCheck(); const accounts = await this.web3.eth.getAccounts(); const wallet = accounts[0]; const blockTimestamp = await this.getBlockTimestamp(); const signedMessage = { domain: signedMessage_constant_1.SIGNED_MESSAGE_DOMAIN, version: signedMessage_constant_1.SIGNED_MESSAGE_VERSION, timestamp: { type: 'string', name: 'Timestamp', value: `${blockTimestamp}`, }, wallet: { type: 'string', name: 'Wallet', value: wallet, }, }; const signatureResult = await new Promise((resolve, reject) => { ; this.web3.currentProvider.sendAsync({ method: 'eth_signTypedData', params: [Web3Controller.getMessageArray(signedMessage), wallet], wallet, }, (error, result) => { if (error) { reject(error); } if (result.error) { reject(result.error); } resolve(result.result); }); }); const signature = { message: signedMessage, wallet, signature: signatureResult, }; return signature; } static verifySignature(signature) { const recovered = sigUtil.recoverTypedSignatureLegacy({ data: Web3Controller.getMessageArray(signature.message), sig: signature.signature, }); const signatureCorrect = recovered === signature.wallet.toLowerCase(); const expired = Date.now() - Number(signature.message.timestamp.value) > signedMessage_constant_1.TTL; return signatureCorrect && !expired; } async generateAuthHeader(signature) { let verification = false; let sig = signature; if (sig) { try { verification = Web3Controller.verifySignature(signature); } catch (error) { // } } if (!verification || !sig) { sig = await this.signMessage(); } if (!sig) { throw new Error(`Signature unavailable`); } return Web3Controller.encodeHeader(sig); } async getBlockTimestamp() { this.initCheck(); const blockNumber = await this.web3.eth.getBlockNumber(); const block = await this.web3.eth.getBlock(blockNumber); return Number(block.timestamp) * 1000; } async getNetwork() { this.initCheck(); const networkId = await this.web3.eth.net.getId(); const networkData = network_controller_1.NetworkController.getNetworkDataById(networkId); return networkData; } async initBrowser() { if (typeof window !== 'undefined') { if (window[ethObject]) { this.web3 = new web3_1.default(window[ethObject]); try { await window[ethObject].enable(); } catch (error) { return false; } } else { const provider = window[web3object].currentProvider; if (!provider) { return false; } this.web3 = new web3_1.default(provider); } return true; } return false; } initServer(httpProvider) { try { const provider = new web3_1.default.providers.HttpProvider(httpProvider); if (!provider) { return false; } this.web3 = new web3_1.default(provider); } catch (_error) { return false; } return true; } static getMessageArray(message) { return [message.domain, message.version, message.timestamp, message.wallet]; } initCheck() { if (!this.web3) { throw new Error(`web3 not initialized`); } } static encodeHeader(signature) { const data = JSON.stringify(signature); const buffer = Buffer.from(data, 'utf-8'); const base64 = buffer.toString('base64'); return base64; } static decodeHeader(base64) { const buffer = Buffer.from(base64, 'base64'); const decoded = buffer.toString('utf-8'); const signature = JSON.parse(decoded); return signature; } } exports.Web3Controller = Web3Controller; //# sourceMappingURL=web3.controller.js.map