@multiplechain/bitcoin 0.1.9 → 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.9",
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,76 +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
- 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);
110
- setTimeout(() => {
111
- startCallback(JSON.parse(res.data));
112
- }, 6000);
82
+ message = JSON.stringify({
83
+ event: "unconfirmed-tx",
84
+ address: receiver,
85
+ token: "6d9cba333f234b9498473955497c40d9"
113
86
  });
114
87
  } else {
115
- let subscription = {
116
- unsubscribe: () => {
88
+ message = JSON.stringify({
89
+ "op": "unconfirmed_sub"
90
+ });
91
+ }
92
+
93
+ let subscription = {
94
+ unsubscribe: () => {
95
+ if (!this.testnet) {
117
96
  ws.send(JSON.stringify({
118
- "op": "addr_unsub",
119
- "addr": receiver
97
+ "op": "unconfirmed_unsub"
120
98
  }));
121
- ws.close();
122
99
  }
100
+ ws.close();
123
101
  }
124
-
125
- ws.addEventListener('open', () => {
126
- ws.send(JSON.stringify({
127
- "op": "addr_sub",
128
- "addr": receiver
129
- }));
130
- });
102
+ }
131
103
 
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
- }
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
+ });
142
128
  }
143
129
 
144
- ws.addEventListener('message', (res) => {
130
+ if (result) {
145
131
  setTimeout(() => {
146
- startCallback(JSON.parse(res.data));
132
+ startCallback(data);
147
133
  }, 6000);
148
- });
149
- }
134
+ }
135
+ });
150
136
  }
151
137
 
152
138
  /**