Skip to content

Commit d1fcb33

Browse files
committed
Using bitdb.bitcoin.com endpoint for mainnet and testnet.* Bitdb class now requires instantiation to set network.* Bitdb methods can be accessed as a property of Bfp object* Unit tests updated to include bitdb response tests, including bitdb testnet* Fixed mistake in README file
1 parent 1637e1c commit d1fcb33

File tree

10 files changed

+129
-89
lines changed

10 files changed

+129
-89
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2018 Simple Ledger Protocol
3+
Copyright (c) 2018 Simple Ledger, Inc.
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ Other tools using the Bitcoin Files Protocol include:
1919

2020
# Example File Download
2121
```javascript
22-
const bfp = require('bitcoinfiles').bfp;
22+
const Bfp = require('bitcoinfiles').bfp;
23+
const bfp = new Bfp();
2324

2425
// 1 - download file using URI
2526
let result;
@@ -70,7 +71,7 @@ let config = {
7071
chunkData: null // chunk not needed for cost estimate stage
7172
};
7273
let uploadCost = Bfp.calculateFileUploadCost(fileSize, config);
73-
console.log(uploadCost);
74+
console.log('upload cost: ', uploadCost);
7475

7576
// 3 - create a funding transaction
7677
let fundingAddress = 'bitcoincash:qqgvrkm0xpmwqgyhfm65qxv70tjtwma6lgk07ffv9u'
@@ -80,7 +81,7 @@ let fundingWif = 'KzcuA9xnDRrb9cPh29N7EQbBhQQLMWtcrDwKbEMoahmwBNACNRfa'
8081
let fundingUtxo;
8182

8283
(async function(){
83-
let txo = await network.getUtxoWithRetry(fundingAddress);
84+
let txo = await network.getLastUtxoWithRetry(fundingAddress);
8485

8586
console.log('got funding Utxo.')
8687
})();
@@ -91,7 +92,7 @@ let fundingUtxo;
9192
let fileId;
9293
(async function(){
9394
fileId = await bfp.uploadFile(fundingUtxo, fundingAddress, fundingWif, someFileBuffer, fileName, fileExt);
94-
console.log(fileId);
95+
console.log('fileId: ', fileId);
9596
})();
9697

9798
// wait for upload to complete resolve... Done.
@@ -107,7 +108,7 @@ const bfp = require('bitcoinfiles');
107108
let metadata;
108109
(async function(){
109110
metadata = await bfp.bitdb.getFileMetadata("dc76c5bd116fd61713c5b454b393212e33a1b2a8c926dcc40261f955d59b8e90","qrg3fvfue463rc5genp2kyrj4mg6g2lpxst0y4wamw");
110-
console.log(metadata);
111+
console.log('metadata: ', metadata);
111112
})
112113

113114
// metadata :

dist/bitcoinfiles.js

Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,18 @@ const BITBOXSDK = require('bitbox-sdk/lib/bitbox-sdk').default
55
let bfp = require('./lib/bfp');
66
let utils = require('./lib/utils');
77
let network = require('./lib/network');
8-
let bitdb = require('./lib/bitdb');
98

109
module.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){
1917
let utils = require('./utils');
20-
let bitboxnetwork = require('./network');
18+
let Network = require('./network');
19+
let Bitdb = require('./bitdb');
2120

2221
const 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

2726
class 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

851851
module.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){
855855
const 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) {

dist/bitcoinfiles.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

index.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@ const BITBOXSDK = require('bitbox-sdk/lib/bitbox-sdk').default
44
let bfp = require('./lib/bfp');
55
let utils = require('./lib/utils');
66
let network = require('./lib/network');
7-
let bitdb = require('./lib/bitdb');
87

98
module.exports = {
109
bfp: bfp,
1110
utils: utils,
1211
network: network,
13-
bitdb: bitdb,
1412
bitbox: BITBOX
1513
}

lib/bfp.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
let utils = require('./utils');
2-
let bitboxnetwork = require('./network');
2+
let Network = require('./network');
3+
let Bitdb = require('./bitdb');
34

45
const BITBOXSDK = require('bitbox-sdk/lib/bitbox-sdk').default
56
, BITBOX = new BITBOXSDK()
@@ -8,9 +9,10 @@ const sleep = ms => new Promise(resolve => setTimeout(resolve, ms))
89

910
class Bfp {
1011

11-
constructor(network = 'mainnet'){
12+
constructor(network = 'mainnet') {
1213
this.networkstring = network;
13-
this.network = new bitboxnetwork(network);
14+
this.network = new Network(network);
15+
this.bitdb = new Bitdb(network);
1416
}
1517

1618
static get lokadIdHex() { return "42465000" }
@@ -85,7 +87,7 @@ class Bfp {
8587
if(uploadProgressCallback != null){
8688
uploadProgressCallback(0);
8789
}
88-
console.log(transactions[0].toHex());
90+
console.log('transaction: ', transactions[0].toHex());
8991
var bfTxId = await this.network.sendTxWithRetry(transactions[0].toHex());
9092

9193
// progress
@@ -319,7 +321,7 @@ class Bfp {
319321
uploadProgressCallback(0);
320322
}
321323
for (let nId = 0; nId < transactions.length; nId++) {
322-
console.log(transactions[nId].toHex());
324+
console.log('transaction: ', transactions[nId].toHex());
323325
var bfTxId = await this.network.sendTxWithRetry(transactions[nId].toHex());
324326
// progress
325327
if(uploadProgressCallback != null){
@@ -343,8 +345,8 @@ class Bfp {
343345
let chunks = [];
344346
let size = 0;
345347

346-
let txid = bfpUri.replace('bitcoinfile:', '')
347-
txid = txid.replace('bitcoinfiles:', '')
348+
let txid = bfpUri.replace('bitcoinfile:', '');
349+
txid = txid.replace('bitcoinfiles:', '');
348350

349351
let txn = await this.network.getTransactionDetailsWithRetry(txid);
350352

@@ -392,7 +394,7 @@ class Bfp {
392394
if(bfpMsg.sha256 != null){
393395
let fileSha256 = BITBOX.Crypto.sha256(fileBuf);
394396
let res = Buffer.compare(fileSha256, bfpMsg.sha256);
395-
if(res == 0){
397+
if(res === 0){
396398
passesHashCheck = true;
397399
}
398400
}
@@ -484,7 +486,7 @@ class Bfp {
484486
chunkData.forEach((item) => script.push(item));
485487
}
486488

487-
// console.log(script);
489+
//console.log('script: ', script);
488490
let encodedScript = utils.encodeScript(script);
489491

490492
if (encodedScript.length > 223) {

lib/bitdb.js

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
const axios = require('axios');
22

3-
const bitDbUrl = 'https://bitdb.network/q/';
3+
module.exports = class BfpBitdb {
44

5-
module.exports = class BitbdProxy {
5+
constructor(network) {
6+
this.bitDbUrl = network === 'mainnet' ? 'https://bitdb.bitcoin.com/q/' : 'https://tbitdb.bitcoin.com/q/';
7+
}
8+
9+
async getFileMetadata(txid, apiKey=null) {
610

7-
static async getFileMetadata(txid, apiKey) {
11+
txid = txid.replace('bitcoinfile:', '');
12+
txid = txid.replace('bitcoinfiles:', '');
813

9-
if(!apiKey)
10-
throw new Error('Missing BitDB key');
14+
// if(!apiKey)
15+
// throw new Error('Missing BitDB key');
1116

1217
let query = {
1318
"v": 3,
@@ -29,10 +34,11 @@ module.exports = class BitbdProxy {
2934
const data = Buffer.from(json_str).toString('base64');
3035
const response = (await axios({
3136
method: 'GET',
32-
url: bitDbUrl + data,
33-
headers: {
34-
'key': apiKey,
35-
},
37+
url: this.bitDbUrl + data,
38+
headers: null,
39+
// {
40+
// 'key': apiKey,
41+
// },
3642
json: true,
3743
})).data;
3844

@@ -52,7 +58,7 @@ module.exports = class BitbdProxy {
5258
if(list.length === 0){
5359
throw new Error('File not found');
5460
}
55-
console.log(list[0]);
61+
console.log('bitdb response: ', list[0]);
5662
return list[0];
5763
}
5864
}

0 commit comments

Comments
 (0)