@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/dist/bitcoin-provider.js +1 -1
- package/package.json +1 -1
- package/src/provider.js +45 -57
package/package.json
CHANGED
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
|
-
|
|
82
|
-
|
|
83
|
-
|
|
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
|
-
|
|
114
|
-
|
|
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": "
|
|
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
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
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
|
-
|
|
130
|
+
if (result) {
|
|
143
131
|
setTimeout(() => {
|
|
144
|
-
startCallback(
|
|
132
|
+
startCallback(data);
|
|
145
133
|
}, 6000);
|
|
146
|
-
}
|
|
147
|
-
}
|
|
134
|
+
}
|
|
135
|
+
});
|
|
148
136
|
}
|
|
149
137
|
|
|
150
138
|
/**
|