@multiplechain/bitcoin 0.1.7 → 0.1.9

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.7",
4
+ "version": "0.1.9",
5
5
  "description": "Bitcoin JS provider",
6
6
  "scripts": {
7
7
  "serve": "parcel test.html --no-cache --dist-dir test",
@@ -34,7 +34,8 @@
34
34
  "bignumber.js": "^9.1.1",
35
35
  "install": "^0.13.0",
36
36
  "npm": "^9.7.2",
37
- "web3-utils": "^1.8.1"
37
+ "web3-utils": "^1.8.1",
38
+ "ws": "^8.13.0"
38
39
  },
39
40
  "devDependencies": {
40
41
  "assert": "^2.0.0",
package/src/provider.js CHANGED
@@ -2,6 +2,10 @@ const Coin = require("./entity/coin");
2
2
  const Token = require("./entity/token");
3
3
  const Transaction = require("./entity/transaction");
4
4
 
5
+ if (typeof window === 'undefined') {
6
+ WebSocket = require('ws');
7
+ }
8
+
5
9
  class Provider {
6
10
 
7
11
  /**
@@ -52,14 +56,14 @@ class Provider {
52
56
  this.testnet = options.testnet;
53
57
  this.network = options.testnet ? 'testnet' : 'livenet';
54
58
 
55
- if (!this.testnet) {
56
- this.api = "https://blockstream.info/api/";
57
- this.explorer = "https://blockstream.info/";
58
- this.wsUrl = "wss://mempool.space/api/v1/ws";
59
- } else {
59
+ if (this.testnet) {
60
60
  this.api = "https://blockstream.info/testnet/api/";
61
61
  this.explorer = "https://blockstream.info/testnet/";
62
- this.wsUrl = "wss://mempool.space/testnet/api/v1/ws";
62
+ this.wsUrl = "wss://socket.blockcypher.com/v1/btc/test3?token=6d9cba333f234b9498473955497c40d9";
63
+ } else {
64
+ this.api = "https://blockstream.info/api/";
65
+ this.explorer = "https://blockstream.info/";
66
+ this.wsUrl = "wss://ws.blockchain.info/inv";
63
67
  }
64
68
 
65
69
  this.detectWallets();
@@ -72,33 +76,77 @@ class Provider {
72
76
  listenTransactions(options, callback) {
73
77
  let receiver = options.receiver;
74
78
  let ws = new WebSocket(this.wsUrl);
75
- let subscription = {
76
- unsubscribe: () => {
77
- ws.close();
79
+
80
+ if (this.testnet) {
81
+ let subscription = {
82
+ unsubscribe: () => {
83
+ ws.close();
84
+ }
78
85
  }
79
- }
80
86
 
81
- ws.addEventListener('open', () => {
82
- ws.send(JSON.stringify({ 'track-address': receiver }));
83
- });
84
-
85
- let startCallback = async (data) => {
86
- try {
87
- let tx = this.Transaction(data['address-transactions'][0].txid);
88
- await tx.getData();
89
- callback(subscription, tx);
90
- } catch (error) {
87
+ ws.addEventListener('open', () => {
88
+ ws.send(JSON.stringify({
89
+ event: "unconfirmed-tx",
90
+ address: receiver,
91
+ token: "6d9cba333f234b9498473955497c40d9"
92
+ }));
93
+ });
94
+
95
+ let startCallback = async (data) => {
96
+ try {
97
+ console.log(data);
98
+ let tx = this.Transaction(data.hash);
99
+ await tx.getData();
100
+ callback(subscription, tx);
101
+ } catch (error) {
102
+ setTimeout(() => {
103
+ startCallback(data);
104
+ }, 2500);
105
+ }
106
+ }
107
+
108
+ ws.addEventListener('message', (res) => {
109
+ console.log(res);
91
110
  setTimeout(() => {
92
- startCallback(data);
93
- }, 2500);
111
+ startCallback(JSON.parse(res.data));
112
+ }, 6000);
113
+ });
114
+ } else {
115
+ let subscription = {
116
+ unsubscribe: () => {
117
+ ws.send(JSON.stringify({
118
+ "op": "addr_unsub",
119
+ "addr": receiver
120
+ }));
121
+ ws.close();
122
+ }
94
123
  }
124
+
125
+ ws.addEventListener('open', () => {
126
+ ws.send(JSON.stringify({
127
+ "op": "addr_sub",
128
+ "addr": receiver
129
+ }));
130
+ });
131
+
132
+ let startCallback = async (data) => {
133
+ try {
134
+ let tx = this.Transaction(data.x.hash);
135
+ await tx.getData();
136
+ callback(subscription, tx);
137
+ } catch (error) {
138
+ setTimeout(() => {
139
+ startCallback(data);
140
+ }, 2500);
141
+ }
142
+ }
143
+
144
+ ws.addEventListener('message', (res) => {
145
+ setTimeout(() => {
146
+ startCallback(JSON.parse(res.data));
147
+ }, 6000);
148
+ });
95
149
  }
96
-
97
- ws.addEventListener('message', (res) => {
98
- setTimeout(() => {
99
- startCallback(JSON.parse(res.data));
100
- }, 6000);
101
- });
102
150
  }
103
151
 
104
152
  /**