@@ -5,19 +5,18 @@ const BITBOXSDK = require('bitbox-sdk/lib/bitbox-sdk').default
5
5
let bfp = require('./lib/bfp');
6
6
let utils = require('./lib/utils');
7
7
let network = require('./lib/network');
8
- let bitdb = require('./lib/bitdb');
9
8
10
9
module.exports = {
11
10
bfp: bfp,
12
11
utils: utils,
13
12
network: network,
14
- bitdb: bitdb,
15
13
bitbox: BITBOX
16
14
}
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){
18
16
(function (Buffer){
19
17
let utils = require('./utils');
20
- let bitboxnetwork = require('./network');
18
+ let Network = require('./network');
19
+ let Bitdb = require('./bitdb');
21
20
22
21
const BITBOXSDK = require('bitbox-sdk/lib/bitbox-sdk').default
23
22
, BITBOX = new BITBOXSDK()
@@ -26,9 +25,10 @@ const sleep = ms => new Promise(resolve => setTimeout(resolve, ms))
26
25
27
26
class Bfp {
28
27
29
- constructor(network = 'mainnet'){
28
+ constructor(network = 'mainnet') {
30
29
this.networkstring = network;
31
- this.network = new bitboxnetwork(network);
30
+ this.network = new Network(network);
31
+ this.bitdb = new Bitdb(network);
32
32
}
33
33
34
34
static get lokadIdHex() { return "42465000" }
@@ -103,7 +103,7 @@ class Bfp {
103
103
if(uploadProgressCallback != null){
104
104
uploadProgressCallback(0);
105
105
}
106
- console.log(transactions[0].toHex());
106
+ console.log('transaction: ', transactions[0].toHex());
107
107
var bfTxId = await this.network.sendTxWithRetry(transactions[0].toHex());
108
108
109
109
// progress
@@ -337,7 +337,7 @@ class Bfp {
337
337
uploadProgressCallback(0);
338
338
}
339
339
for (let nId = 0; nId < transactions.length; nId++) {
340
- console.log(transactions[nId].toHex());
340
+ console.log('transaction: ', transactions[nId].toHex());
341
341
var bfTxId = await this.network.sendTxWithRetry(transactions[nId].toHex());
342
342
// progress
343
343
if(uploadProgressCallback != null){
@@ -361,8 +361,8 @@ class Bfp {
361
361
let chunks = [];
362
362
let size = 0;
363
363
364
- let txid = bfpUri.replace('bitcoinfile:', '')
365
- txid = txid.replace('bitcoinfiles:', '')
364
+ let txid = bfpUri.replace('bitcoinfile:', '');
365
+ txid = txid.replace('bitcoinfiles:', '');
366
366
367
367
let txn = await this.network.getTransactionDetailsWithRetry(txid);
368
368
@@ -410,7 +410,7 @@ class Bfp {
410
410
if(bfpMsg.sha256 != null){
411
411
let fileSha256 = BITBOX.Crypto.sha256(fileBuf);
412
412
let res = Buffer.compare(fileSha256, bfpMsg.sha256);
413
- if(res == 0){
413
+ if(res === 0){
414
414
passesHashCheck = true;
415
415
}
416
416
}
@@ -502,7 +502,7 @@ class Bfp {
502
502
chunkData.forEach((item) => script.push(item));
503
503
}
504
504
505
- // console.log(script);
505
+ //console.log('script: ', script);
506
506
let encodedScript = utils.encodeScript(script);
507
507
508
508
if (encodedScript.length > 223) {
@@ -850,18 +850,23 @@ class Bfp {
850
850
851
851
module.exports = Bfp;
852
852
}).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){
854
854
(function (Buffer){
855
855
const axios = require('axios');
856
856
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
+ }
858
862
859
- module.exports = class BitbdProxy {
863
+ async getFileMetadata(txid, apiKey=null) {
860
864
861
- static async getFileMetadata(txid, apiKey) {
865
+ txid = txid.replace('bitcoinfile:', '');
866
+ txid = txid.replace('bitcoinfiles:', '');
862
867
863
- if(!apiKey)
864
- throw new Error('Missing BitDB key');
868
+ // if(!apiKey)
869
+ // throw new Error('Missing BitDB key');
865
870
866
871
let query = {
867
872
"v": 3,
@@ -883,10 +888,11 @@ module.exports = class BitbdProxy {
883
888
const data = Buffer.from(json_str).toString('base64');
884
889
const response = (await axios({
885
890
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
+ // },
890
896
json: true,
891
897
})).data;
892
898
@@ -906,7 +912,7 @@ module.exports = class BitbdProxy {
906
912
if(list.length === 0){
907
913
throw new Error('File not found');
908
914
}
909
- console.log(list[0]);
915
+ console.log('bitdb response: ', list[0]);
910
916
return list[0];
911
917
}
912
918
}
@@ -928,11 +934,12 @@ class BfpNetwork {
928
934
this.isMonitoringPayment = false;
929
935
}
930
936
931
- async getUtxoWithRetry (address, retries = 40) {
937
+ async getLastUtxoWithRetry (address, retries = 40) {
932
938
let result;
933
939
let count = 0;
934
940
while(result == undefined){
935
- result = await this.getUtxo(address)
941
+ result = await this.getLastUtxo(address)
942
+ console.log(result);
936
943
count++;
937
944
if(count > retries)
938
945
throw new Error("BITBOX.Address.utxo endpoint experienced a problem");
@@ -955,13 +962,13 @@ class BfpNetwork {
955
962
return result;
956
963
}
957
964
958
- async getUtxo (address, log=true) {
965
+ async getLastUtxo (address, log=true) {
959
966
// must be a cash or legacy addr
960
967
if(!this.BITBOX.Address.isCashAddress(address) && !this.BITBOX.Address.isLegacyAddress(address))
961
968
throw new Error("Not an a valid address format, must be cashAddr or Legacy address format.");
962
969
let res = await this.BITBOX.Address.utxo(address);
963
970
if(log)
964
- console.log('getUtxo for ', address, ': ', res);
971
+ console.log('getLastUtxo for ', address, ': ', res);
965
972
return res[0];
966
973
}
967
974
@@ -970,7 +977,7 @@ class BfpNetwork {
970
977
if(res === "64: too-long-mempool-chain")
971
978
throw new Error("Mempool chain too long");
972
979
if(log)
973
- console.log(res);
980
+ console.log('sendTx() res: ', res);
974
981
return res;
975
982
}
976
983
@@ -1003,12 +1010,12 @@ class BfpNetwork {
1003
1010
1004
1011
while (true) {
1005
1012
try {
1006
- var utxo = await this.getUtxo (paymentAddress);
1013
+ var utxo = await this.getLastUtxo (paymentAddress);
1007
1014
if (utxo && utxo.satoshis >= fee && utxo.confirmations === 0) {
1008
1015
break;
1009
1016
}
1010
1017
} catch (ex) {
1011
- console.log(ex);
1018
+ console.log('monitorForPayment() error: ', ex);
1012
1019
}
1013
1020
1014
1021
if(this.stopPayMonitor) {
0 commit comments