@multiplechain/bitcoin 0.1.2 → 0.1.4

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.2",
4
+ "version": "0.1.4",
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
  */
@@ -42,9 +51,11 @@ class Provider {
42
51
  if (!this.testnet) {
43
52
  this.api = "https://blockstream.info/api/";
44
53
  this.explorer = "https://blockstream.info/";
54
+ this.wsUrl = "wss://mempool.space/api/v1/ws";
45
55
  } else {
46
56
  this.api = "https://blockstream.info/testnet/api/";
47
57
  this.explorer = "https://blockstream.info/testnet/";
58
+ this.wsUrl = "wss://mempool.space/testnet/api/v1/ws";
48
59
  }
49
60
 
50
61
  this.detectWallets();
@@ -80,42 +91,36 @@ class Provider {
80
91
  return data;
81
92
  }
82
93
 
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;
94
+ listenTransactions(options, callback) {
95
+ let receiver = options.receiver;
96
+ let ws = new WebSocket(this.wsUrl);
97
+ let subscription = {
98
+ unsubscribe: () => {
99
+ ws.close();
100
+ }
98
101
  }
102
+
103
+ ws.addEventListener('open', () => {
104
+ ws.send(JSON.stringify({ 'track-address': receiver }));
105
+ });
99
106
 
100
- if (data.length == 0) {
101
- return {
102
- hash: null,
103
- amount: 0
107
+ let startCallback = async (data) => {
108
+ try {
109
+ let tx = this.Transaction(data['address-transactions'][0].txid);
110
+ await tx.getData();
111
+ callback(subscription, tx);
112
+ } catch (error) {
113
+ setTimeout(() => {
114
+ startCallback(data);
115
+ }, 2500);
104
116
  }
105
117
  }
106
-
107
- let tx = data[0];
108
-
109
- let index = tx.vout.findIndex(object => {
110
- return object.scriptpubkey_address == receiver;
118
+
119
+ ws.addEventListener('message', (res) => {
120
+ setTimeout(() => {
121
+ startCallback(JSON.parse(res.data));
122
+ }, 6000);
111
123
  });
112
-
113
- data = tx.vout[index];
114
-
115
- return {
116
- hash: tx.txid,
117
- amount: utils.toDec(data.value, 8)
118
- };
119
124
  }
120
125
 
121
126
  /**
@@ -161,7 +166,7 @@ class Provider {
161
166
  if (typeof window != 'undefined') {
162
167
  const Wallet = require('./wallet');
163
168
 
164
- if (typeof window.unisat !== 'undefined') {
169
+ if (typeof window.unisat !== 'undefined' && unisat.requestAccounts) {
165
170
  this.detectedWallets['unisat'] = new Wallet('unisat', this);
166
171
  }
167
172
  }