@multiplechain/bitcoin 0.1.3 → 0.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "namespace": "multiplechain",
3
3
  "name": "@multiplechain/bitcoin",
4
- "version": "0.1.3",
4
+ "version": "0.1.5",
5
5
  "description": "Bitcoin JS provider",
6
6
  "scripts": {
7
7
  "serve": "parcel test.html --no-cache --dist-dir test",
@@ -31,6 +31,8 @@
31
31
  "dependencies": {
32
32
  "@multiplechain/utils": "^0.1.1",
33
33
  "bignumber.js": "^9.1.1",
34
+ "install": "^0.13.0",
35
+ "npm": "^9.7.2",
34
36
  "web3-utils": "^1.8.1"
35
37
  },
36
38
  "devDependencies": {
@@ -58,6 +58,20 @@ class Transaction {
58
58
  return this.data;
59
59
  }
60
60
 
61
+ /**
62
+ * @param {Object} options
63
+ * @returns {Number}
64
+ */
65
+ async getTransferAmount(options) {
66
+ let data = await this.getData();
67
+ let receiver = options.receiver;
68
+ let index = data.vout.findIndex(object => {
69
+ return object.scriptpubkey_address == receiver;
70
+ });
71
+ data = data.vout[index];
72
+ return utils.toDec(data.value, 8);
73
+ }
74
+
61
75
  /**
62
76
  * @returns {Number}
63
77
  */
package/src/provider.js CHANGED
@@ -1,4 +1,3 @@
1
- const utils = require("@multiplechain/utils");
2
1
  const Coin = require("./entity/coin");
3
2
  const Token = require("./entity/token");
4
3
  const Transaction = require("./entity/transaction");
@@ -25,6 +24,16 @@ class Provider {
25
24
  */
26
25
  network;
27
26
 
27
+ /**
28
+ * @var {Boolean}
29
+ */
30
+ qrPayments = true;
31
+
32
+ /**
33
+ * @var {String}
34
+ */
35
+ wsUrl;
36
+
28
37
  /**
29
38
  * @var {Object}
30
39
  */
@@ -35,16 +44,22 @@ class Provider {
35
44
  */
36
45
  connectedWallet;
37
46
 
38
- constructor(testnet = false) {
39
- this.testnet = testnet;
40
- this.network = testnet ? 'testnet' : 'livenet';
47
+ /**
48
+ * @param {Object} options
49
+ */
50
+ constructor(options) {
51
+
52
+ this.testnet = options.testnet;
53
+ this.network = options.testnet ? 'testnet' : 'livenet';
41
54
 
42
55
  if (!this.testnet) {
43
56
  this.api = "https://blockstream.info/api/";
44
57
  this.explorer = "https://blockstream.info/";
58
+ this.wsUrl = "wss://mempool.space/api/v1/ws";
45
59
  } else {
46
60
  this.api = "https://blockstream.info/testnet/api/";
47
61
  this.explorer = "https://blockstream.info/testnet/";
62
+ this.wsUrl = "wss://mempool.space/testnet/api/v1/ws";
48
63
  }
49
64
 
50
65
  this.detectWallets();
@@ -80,42 +95,36 @@ class Provider {
80
95
  return data;
81
96
  }
82
97
 
83
- /**
84
- * @param {String} receiver
85
- * @returns
86
- */
87
- async getLastTransactionByReceiver(receiver) {
88
-
89
- let apiUrl = this.api + 'address/' + receiver + '/txs';
90
- let data = await fetch(apiUrl).then(response => response.text());
91
- try {
92
- data = JSON.parse(data);
93
- } catch (error) {}
94
-
95
- data = this.errorCheck(data);
96
- if (data.error) {
97
- return data;
98
+ listenTransactions(options, callback) {
99
+ let receiver = options.receiver;
100
+ let ws = new WebSocket(this.wsUrl);
101
+ let subscription = {
102
+ unsubscribe: () => {
103
+ ws.close();
104
+ }
98
105
  }
106
+
107
+ ws.addEventListener('open', () => {
108
+ ws.send(JSON.stringify({ 'track-address': receiver }));
109
+ });
99
110
 
100
- if (data.length == 0) {
101
- return {
102
- hash: null,
103
- amount: 0
111
+ let startCallback = async (data) => {
112
+ try {
113
+ let tx = this.Transaction(data['address-transactions'][0].txid);
114
+ await tx.getData();
115
+ callback(subscription, tx);
116
+ } catch (error) {
117
+ setTimeout(() => {
118
+ startCallback(data);
119
+ }, 2500);
104
120
  }
105
121
  }
106
-
107
- let tx = data[0];
108
-
109
- let index = tx.vout.findIndex(object => {
110
- return object.scriptpubkey_address == receiver;
122
+
123
+ ws.addEventListener('message', (res) => {
124
+ setTimeout(() => {
125
+ startCallback(JSON.parse(res.data));
126
+ }, 6000);
111
127
  });
112
-
113
- data = tx.vout[index];
114
-
115
- return {
116
- hash: tx.txid,
117
- amount: utils.toDec(data.value, 8)
118
- };
119
128
  }
120
129
 
121
130
  /**