@multiplechain/bitcoin 0.1.10 → 0.1.11

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.10",
4
+ "version": "0.1.11",
5
5
  "description": "Bitcoin JS provider",
6
6
  "scripts": {
7
7
  "serve": "parcel test.html --no-cache --dist-dir test",
package/src/provider.js CHANGED
@@ -51,7 +51,7 @@ class Provider {
51
51
  /**
52
52
  * @param {Object} options
53
53
  */
54
- constructor(options) {
54
+ constructor(options = {}) {
55
55
 
56
56
  this.testnet = options.testnet;
57
57
  this.network = options.testnet ? 'testnet' : 'livenet';
@@ -77,74 +77,62 @@ class Provider {
77
77
  let receiver = options.receiver;
78
78
  let ws = new WebSocket(this.wsUrl);
79
79
 
80
+ let message;
80
81
  if (this.testnet) {
81
- let subscription = {
82
- unsubscribe: () => {
83
- ws.close();
84
- }
85
- }
86
-
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
- let tx = this.Transaction(data.hash);
98
- await tx.getData();
99
- callback(subscription, tx);
100
- } catch (error) {
101
- setTimeout(() => {
102
- startCallback(data);
103
- }, 2500);
104
- }
105
- }
106
-
107
- ws.addEventListener('message', (res) => {
108
- setTimeout(() => {
109
- startCallback(JSON.parse(res.data));
110
- }, 6000);
82
+ message = JSON.stringify({
83
+ event: "unconfirmed-tx",
84
+ address: receiver,
85
+ token: "6d9cba333f234b9498473955497c40d9"
111
86
  });
112
87
  } else {
113
- let subscription = {
114
- unsubscribe: () => {
88
+ message = JSON.stringify({
89
+ "op": "unconfirmed_sub"
90
+ });
91
+ }
92
+
93
+ let subscription = {
94
+ unsubscribe: () => {
95
+ if (!this.testnet) {
115
96
  ws.send(JSON.stringify({
116
- "op": "addr_unsub",
117
- "addr": receiver
97
+ "op": "unconfirmed_unsub"
118
98
  }));
119
- ws.close();
120
99
  }
100
+ ws.close();
121
101
  }
122
-
123
- ws.addEventListener('open', () => {
124
- ws.send(JSON.stringify({
125
- "op": "addr_sub",
126
- "addr": receiver
127
- }));
128
- });
102
+ }
129
103
 
130
- let startCallback = async (data) => {
131
- try {
132
- let tx = this.Transaction(data.x.hash);
133
- await tx.getData();
134
- callback(subscription, tx);
135
- } catch (error) {
136
- setTimeout(() => {
137
- startCallback(data);
138
- }, 2500);
139
- }
104
+ let startCallback = async (data) => {
105
+ try {
106
+ let tx = this.Transaction(data.hash || data.x.hash);
107
+ await tx.getData();
108
+ callback(subscription, tx);
109
+ } catch (error) {
110
+ setTimeout(() => {
111
+ startCallback(data);
112
+ }, 2500);
113
+ }
114
+ }
115
+
116
+ ws.addEventListener('open', () => {
117
+ ws.send(message);
118
+ });
119
+
120
+ ws.addEventListener('message', (res) => {
121
+ let result = true;
122
+ let data = JSON.parse(res.data);
123
+
124
+ if (!this.testnet) {
125
+ result = data.x.out.find(o => {
126
+ return String(o.addr).toLowerCase() == receiver.toLowerCase();
127
+ });
140
128
  }
141
129
 
142
- ws.addEventListener('message', (res) => {
130
+ if (result) {
143
131
  setTimeout(() => {
144
- startCallback(JSON.parse(res.data));
132
+ startCallback(data);
145
133
  }, 6000);
146
- });
147
- }
134
+ }
135
+ });
148
136
  }
149
137
 
150
138
  /**