Skip to content

Commit

Permalink
Partially implemented Blockbook support
Browse files Browse the repository at this point in the history
  • Loading branch information
Lesmiscore committed Jan 14, 2019
1 parent 25b60ba commit a4b635d
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 6 deletions.
4 changes: 4 additions & 0 deletions js/currencyList.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,10 @@ const defaultCoins = [{
url: "https://insight.electrum-mona.org/insight-api-monacoin",
explorer: "https://insight.electrum-mona.org/insight",
type:"insight"
}, {
url: "https://blockbook.electrum-mona.org/api",
explorer: "https://blockbook.electrum-mona.org",
type:"blockbook"
}],
network: {
messagePrefix: '\x19Monacoin Signed Message:\n',
Expand Down
105 changes: 99 additions & 6 deletions js/explorers/blockbook.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const axios = require('axios');
const bitcoin = require('bitcoinjs-lib');
const BigNumber = require('bignumber.js');
const qs = require('qs');

module.exports = class BlockbookExplorer {
Expand All @@ -8,27 +10,118 @@ module.exports = class BlockbookExplorer {
}

pushTx(hex) {
return
return axios({
url: this.apiEndpoint + "/sendtx",
data: hex,
method: "POST"
}).then(res => ({
txid: res.data.result
}));
}

getTxs(from, to, addrs) {
return
throw new Error('Missing implementation: getTxs');
}

getTx(txId) {
return
return axios({
url: this.apiEndpoint + "/tx/" + txId,
method: "GET"
}).then(res => res.data).then(data => {
// diff <(wget -qO- https://blockbook.electrum-mona.org/api/tx/ae3fda4b6ec6bb87c9df003f558e4391a00320de2350d8cb0954c9bdb5a85cd5 | jq -S) <(wget -qO- https://mona.chainsight.info/api/tx/ae3fda4b6ec6bb87c9df003f558e4391a00320de2350d8cb0954c9bdb5a85cd5 | jq -S) -u
const tx = bitcoin.Transaction.fromHex(data.hex);
// add sizes
data.size = tx.byteLength();
data.vsize = tx.virtualSize();
// copy time
data.time = data.blocktime;
// convert valueOut, valueIn, fees to decimal
data.valueOut = +data.valueOut;
data.valueIn = +data.valueIn;
data.fees = +data.fees;
for (let vin of data.vin) {
// choose address
vin.addr = vin.addresses[0];
// add asm
vin.scriptSig.asm = bitcoin.script.toASM(Buffer.from(vin.scriptSig.hex, 'hex'));
// convert value to satoshis/watanabes
data.valueSat = +new BigNumber(data.value).times(100000000);
// and to decimal
data.value = +data.value;
}
for (let vout of data.vout) {
// add asm
vout.scriptPubKey.asm = bitcoin.script.toASM(Buffer.from(vout.scriptPubKey.hex, 'hex'));
}
});
}

getBlocks() {
return
const blockAmount = 4;
return axios({
url: this.apiEndpoint + "/",
method: "GET"
}).then(res => res.data.blockbook.bestHeight).then(highest => {
const hashPromise = [];
for (let i = 0; i < blockAmount; i++) {
hashPromise.push(
axios({
url: this.apiEndpoint + "/block-index/" + (highest - i),
method: "GET"
}).then(res => res.data.blockHash)
);
}
return Promise.all(hashPromise);
}).then(hashes => {
const blockPromise = [];
for (let i = 0; i < blockAmount; i++) {
blockPromise.push(
axios({
url: this.apiEndpoint + "/block/" + hashes[i],
method: "GET"
}).then(res => res.data)
);
}
// that's ok: all required keys set in this step
return Promise.all(blockPromise);
});
}

getUtxos(addressList) {
return
const eachUtxoPromise = [];
for (let addr of addressList) {
const addr_ = addr;
eachUtxoPromise.push(
axios({
url: this.apiEndpoint + "/utxo/" + addr,
method: "GET"
}).then(res => res.data).then(data => {
// add address
data.address = addr_;
// have amount in float
data.amount = +new BigNumber(data.amount).dividedBy(100000000);
})
);
}
return Promise.all(eachUtxoPromise).then(arrays =>
Array.prototype.concat.apply([], arrays)
);
}

getAddressProp(propName, address, noTxList) {
return
return axios({
url: this.apiEndpoint + "/address/" + address,
method: "GET"
}).then(res => res.data).then(data => {
if (propName) {
return data[propName];
} else {
if (noTxList) {
delete data.transactions;
}
return data;
}
});
}

explorerUrls(opt) {
Expand Down

0 comments on commit a4b635d

Please sign in to comment.