@berrywallet/core
Version:
Berrywallet main Core for work with common cryptocurrencies like Bitcoin, Ethereum, Dash, Litecoin
78 lines (77 loc) • 3.36 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
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) : new P(function (resolve) { resolve(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 });
const lodash_1 = require("lodash");
const axios_1 = __importDefault(require("axios"));
const api_1 = require("../api");
const blockcypher_1 = require("../limmiters/blockcypher");
const network_client_1 = require("./network-client");
class BlockcypherBIPNetworkClient extends network_client_1.NetworkClient {
constructor(coin, options) {
super(coin, options);
this.client = axios_1.default.create({
baseURL: this.getApiUrl(),
timeout: 10000,
});
}
getTx(txid) {
return blockcypher_1.wrapLimiterMethod(() => __awaiter(this, void 0, void 0, function* () {
try {
const response = yield this.client.get('/txs/' + txid);
const tx = response.data;
return api_1.Blockcypher.toWalletTx(tx, this.coin);
}
catch (error) {
if (error.response.status == 404) {
return null;
}
throw error;
}
}));
}
getBlock(blockHash) {
return blockcypher_1.wrapLimiterMethod(() => __awaiter(this, void 0, void 0, function* () {
const response = yield this.client.get('/blocks/' + blockHash);
const block = response.data;
return {
hash: block.hash,
height: block.depth,
time: new Date(block.received_time).getTime(),
txids: block.txids,
original: block,
};
}));
}
broadCastTransaction(transaction) {
return blockcypher_1.wrapLimiterMethod(() => __awaiter(this, void 0, void 0, function* () {
const response = yield this.client.post('/txs/push', {
tx: transaction.toBuffer().toString('hex'),
});
const tx = response.data;
return tx.hash;
}));
}
getAddressTxs(address) {
return blockcypher_1.wrapLimiterMethod(() => __awaiter(this, void 0, void 0, function* () {
const response = yield this.client.get('/addrs/' + address + '/full');
const addressData = response.data;
const txList = [];
const extractTxCallback = (tx) => {
txList.push(api_1.Blockcypher.toWalletTx(tx, this.coin));
};
lodash_1.forEach(lodash_1.orderBy(addressData.txs, 'block_height', 'asc'), extractTxCallback);
return txList;
}));
}
}
exports.BlockcypherBIPNetworkClient = BlockcypherBIPNetworkClient;