UNPKG

@beraji/wallet-sdk

Version:

Beraji: Distributed Secret Sharing.

108 lines 4.52 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()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.BWeb3Tx = void 0; const axios_1 = __importDefault(require("axios")); const utils_1 = require("../utils"); const constants_1 = require("./constants"); const RPCS = { local: 'http://127.0.0.1:9001', test: 'https://dev-web3-tx.onrender.com', prod: 'https://api.web3-tx.beraji.com', }; class BWeb3Tx { constructor(params) { this.token = params.token; this.net = params.net || 'prod'; } static connection(params) { if (!params.net) params.net = 'prod'; const client = axios_1.default.create({ baseURL: RPCS[params.net], headers: params.token ? { Authorization: `Bearer ${params.token}` } : {}, }); client.interceptors.response.use((response) => response, (error) => { var _a; const err = ((_a = error === null || error === void 0 ? void 0 : error.response) === null || _a === void 0 ? void 0 : _a.data) || error.message; return Promise.reject(err); }); return client; } get con() { return BWeb3Tx.connection({ token: this.token, net: this.net }); } ethSendTransaction(payload) { return __awaiter(this, void 0, void 0, function* () { const tx = yield this.con.post('transactions', payload); const result = window.confirm('Do you want to open telegram?'); if (result) { window.open(constants_1.TELE_APPS[this.net], '_blank'); } else { console.log('User clicked Cancel'); } const approved = yield this.waitTransactionApproved(tx.data._id); if (approved.data.status === constants_1.TxStatus.Rejected) throw new Error('User rejected the transaction!'); return tx.data; }); } signMessage(message) { return __awaiter(this, void 0, void 0, function* () { const msg = yield this.con.post('message', { message }); const result = window.confirm('Do you want to open telegram?'); if (result) { window.open(constants_1.TELE_APPS[this.net], '_blank'); } else { console.log('User clicked Cancel'); } const approved = yield this.waitMessageApproved(msg.data._id); if (approved.data.status !== constants_1.MsgStatus.Signed) throw new Error('User rejected the message!'); return approved; }); } waitMessageApproved(msgId) { return __awaiter(this, void 0, void 0, function* () { const msg = (0, utils_1.waitTo)({ fn: () => this.con.get(`message/${msgId}`), condition: (result) => result.data.status != constants_1.MsgStatus.Init, timeout: 5 * 60 * 1000, intervalTime: 5000, }); return msg; }); } waitTransactionApproved(msgId) { return __awaiter(this, void 0, void 0, function* () { const msg = (0, utils_1.waitTo)({ fn: () => this.con.get(`transactions/${msgId}`), condition: (result) => result.data.status != constants_1.TxStatus.Init, timeout: 5 * 60 * 1000, intervalTime: 5000, }); return msg; }); } getChain() { return __awaiter(this, void 0, void 0, function* () { const chain = yield this.con.get('chain'); return chain.data; }); } } exports.BWeb3Tx = BWeb3Tx; //# sourceMappingURL=bweb3tx.js.map