@@ -5,19 +5,18 @@ const BITBOXSDK = require('bitbox-sdk/lib/bitbox-sdk').default
55let bfp = require('./lib/bfp');
66let utils = require('./lib/utils');
77let network = require('./lib/network');
8- let bitdb = require('./lib/bitdb');
98
109module.exports = {
1110 bfp: bfp,
1211 utils: utils,
1312 network: network,
14- bitdb: bitdb,
1513 bitbox: BITBOX
1614}
17- },{"./lib/bfp":2,"./lib/bitdb":3,"./lib/ network":4,"./lib/utils":5,"bitbox-sdk/lib/bitbox-sdk":96}],2:[function(require,module,exports){
15+ },{"./lib/bfp":2,"./lib/network":4,"./lib/utils":5,"bitbox-sdk/lib/bitbox-sdk":96}],2:[function(require,module,exports){
1816(function (Buffer){
1917let utils = require('./utils');
20- let bitboxnetwork = require('./network');
18+ let Network = require('./network');
19+ let Bitdb = require('./bitdb');
2120
2221const BITBOXSDK = require('bitbox-sdk/lib/bitbox-sdk').default
2322 , BITBOX = new BITBOXSDK()
@@ -26,9 +25,10 @@ const sleep = ms => new Promise(resolve => setTimeout(resolve, ms))
2625
2726class Bfp {
2827
29- constructor(network = 'mainnet'){
28+ constructor(network = 'mainnet') {
3029 this.networkstring = network;
31- this.network = new bitboxnetwork(network);
30+ this.network = new Network(network);
31+ this.bitdb = new Bitdb(network);
3232 }
3333
3434 static get lokadIdHex() { return "42465000" }
@@ -103,7 +103,7 @@ class Bfp {
103103 if(uploadProgressCallback != null){
104104 uploadProgressCallback(0);
105105 }
106- console.log(transactions[0].toHex());
106+ console.log('transaction: ', transactions[0].toHex());
107107 var bfTxId = await this.network.sendTxWithRetry(transactions[0].toHex());
108108
109109 // progress
@@ -337,7 +337,7 @@ class Bfp {
337337 uploadProgressCallback(0);
338338 }
339339 for (let nId = 0; nId < transactions.length; nId++) {
340- console.log(transactions[nId].toHex());
340+ console.log('transaction: ', transactions[nId].toHex());
341341 var bfTxId = await this.network.sendTxWithRetry(transactions[nId].toHex());
342342 // progress
343343 if(uploadProgressCallback != null){
@@ -361,8 +361,8 @@ class Bfp {
361361 let chunks = [];
362362 let size = 0;
363363
364- let txid = bfpUri.replace('bitcoinfile:', '')
365- txid = txid.replace('bitcoinfiles:', '')
364+ let txid = bfpUri.replace('bitcoinfile:', '');
365+ txid = txid.replace('bitcoinfiles:', '');
366366
367367 let txn = await this.network.getTransactionDetailsWithRetry(txid);
368368
@@ -410,7 +410,7 @@ class Bfp {
410410 if(bfpMsg.sha256 != null){
411411 let fileSha256 = BITBOX.Crypto.sha256(fileBuf);
412412 let res = Buffer.compare(fileSha256, bfpMsg.sha256);
413- if(res == 0){
413+ if(res === 0){
414414 passesHashCheck = true;
415415 }
416416 }
@@ -502,7 +502,7 @@ class Bfp {
502502 chunkData.forEach((item) => script.push(item));
503503 }
504504
505- // console.log(script);
505+ //console.log('script: ', script);
506506 let encodedScript = utils.encodeScript(script);
507507
508508 if (encodedScript.length > 223) {
@@ -850,18 +850,23 @@ class Bfp {
850850
851851module.exports = Bfp;
852852}).call(this,require("buffer").Buffer)
853- },{"./network":4,"./utils":5,"bitbox-sdk/lib/bitbox-sdk":96,"buffer":174}],3:[function(require,module,exports){
853+ },{"./bitdb":3,"./ network":4,"./utils":5,"bitbox-sdk/lib/bitbox-sdk":96,"buffer":174}],3:[function(require,module,exports){
854854(function (Buffer){
855855const axios = require('axios');
856856
857- const bitDbUrl = 'https://bitdb.network/q/';
857+ module.exports = class BfpBitdb {
858+
859+ constructor(network) {
860+ this.bitDbUrl = network === 'mainnet' ? 'https://bitdb.bitcoin.com/q/' : 'https://tbitdb.bitcoin.com/q/';
861+ }
858862
859- module.exports = class BitbdProxy {
863+ async getFileMetadata(txid, apiKey=null) {
860864
861- static async getFileMetadata(txid, apiKey) {
865+ txid = txid.replace('bitcoinfile:', '');
866+ txid = txid.replace('bitcoinfiles:', '');
862867
863- if(!apiKey)
864- throw new Error('Missing BitDB key');
868+ // if(!apiKey)
869+ // throw new Error('Missing BitDB key');
865870
866871 let query = {
867872 "v": 3,
@@ -883,10 +888,11 @@ module.exports = class BitbdProxy {
883888 const data = Buffer.from(json_str).toString('base64');
884889 const response = (await axios({
885890 method: 'GET',
886- url: bitDbUrl + data,
887- headers: {
888- 'key': apiKey,
889- },
891+ url: this.bitDbUrl + data,
892+ headers: null,
893+ // {
894+ // 'key': apiKey,
895+ // },
890896 json: true,
891897 })).data;
892898
@@ -906,7 +912,7 @@ module.exports = class BitbdProxy {
906912 if(list.length === 0){
907913 throw new Error('File not found');
908914 }
909- console.log(list[0]);
915+ console.log('bitdb response: ', list[0]);
910916 return list[0];
911917 }
912918}
@@ -928,11 +934,12 @@ class BfpNetwork {
928934 this.isMonitoringPayment = false;
929935 }
930936
931- async getUtxoWithRetry (address, retries = 40) {
937+ async getLastUtxoWithRetry (address, retries = 40) {
932938 let result;
933939 let count = 0;
934940 while(result == undefined){
935- result = await this.getUtxo(address)
941+ result = await this.getLastUtxo(address)
942+ console.log(result);
936943 count++;
937944 if(count > retries)
938945 throw new Error("BITBOX.Address.utxo endpoint experienced a problem");
@@ -955,13 +962,13 @@ class BfpNetwork {
955962 return result;
956963 }
957964
958- async getUtxo (address, log=true) {
965+ async getLastUtxo (address, log=true) {
959966 // must be a cash or legacy addr
960967 if(!this.BITBOX.Address.isCashAddress(address) && !this.BITBOX.Address.isLegacyAddress(address))
961968 throw new Error("Not an a valid address format, must be cashAddr or Legacy address format.");
962969 let res = await this.BITBOX.Address.utxo(address);
963970 if(log)
964- console.log('getUtxo for ', address, ': ', res);
971+ console.log('getLastUtxo for ', address, ': ', res);
965972 return res[0];
966973 }
967974
@@ -970,7 +977,7 @@ class BfpNetwork {
970977 if(res === "64: too-long-mempool-chain")
971978 throw new Error("Mempool chain too long");
972979 if(log)
973- console.log(res);
980+ console.log('sendTx() res: ', res);
974981 return res;
975982 }
976983
@@ -1003,12 +1010,12 @@ class BfpNetwork {
10031010
10041011 while (true) {
10051012 try {
1006- var utxo = await this.getUtxo (paymentAddress);
1013+ var utxo = await this.getLastUtxo (paymentAddress);
10071014 if (utxo && utxo.satoshis >= fee && utxo.confirmations === 0) {
10081015 break;
10091016 }
10101017 } catch (ex) {
1011- console.log(ex);
1018+ console.log('monitorForPayment() error: ', ex);
10121019 }
10131020
10141021 if(this.stopPayMonitor) {
0 commit comments