@multiplechain/bitcoin 0.1.0

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 MultipleChain
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1 @@
1
+ Preparing
package/package.json ADDED
@@ -0,0 +1,45 @@
1
+ {
2
+ "namespace": "multiplechain",
3
+ "name": "@multiplechain/bitcoin",
4
+ "version": "0.1.0",
5
+ "description": "Bitcoin JS provider",
6
+ "scripts": {
7
+ "serve": "parcel test.html --no-cache --dist-dir test",
8
+ "build": "webpack"
9
+ },
10
+ "files": [
11
+ "src"
12
+ ],
13
+ "main": "src/provider.js",
14
+ "types": "index.d.ts",
15
+ "typings": "index.d.ts",
16
+ "repository": {
17
+ "type": "git",
18
+ "url": "git+https://github.com/MultipleChain/bitcoin.git"
19
+ },
20
+ "keywords": [
21
+ "Blockchain",
22
+ "Bitcoin",
23
+ "Cryptocurrency"
24
+ ],
25
+ "author": "MultipleChain",
26
+ "license": "MIT",
27
+ "bugs": {
28
+ "url": "https://github.com/MultipleChain/bitcoin/issues"
29
+ },
30
+ "homepage": "https://github.com/MultipleChain/bitcoin",
31
+ "dependencies": {
32
+ "@multiplechain/utils": "^0.1.1",
33
+ "bignumber.js": "^9.1.1",
34
+ "web3-utils": "^1.8.1"
35
+ },
36
+ "devDependencies": {
37
+ "assert": "^2.0.0",
38
+ "buffer": "^5.7.1",
39
+ "events": "^3.3.0",
40
+ "process": "^0.11.10",
41
+ "stream-browserify": "^3.0.0",
42
+ "webpack": "^5.76.0",
43
+ "webpack-cli": "^5.0.1"
44
+ }
45
+ }
@@ -0,0 +1,75 @@
1
+ const Transaction = require("./transaction");
2
+ const utils = require("@multiplechain/utils");
3
+
4
+ class Provider {
5
+
6
+ api;
7
+
8
+ explorer;
9
+
10
+ testnet;
11
+
12
+ constructor(testnet = false) {
13
+ this.testnet = testnet;
14
+
15
+ if (!this.testnet) {
16
+ this.api = "https://blockchain.info/";
17
+ this.explorer = "https://www.blockchain.com/explorer/";
18
+ } else {
19
+ this.api = "https://blockstream.info/testnet/api/";
20
+ this.explorer = "https://blockstream.info/testnet/";
21
+ }
22
+ }
23
+
24
+ getWalletOpenLink(address, amount) {
25
+ return 'bitcoin' + ':' + String(address).toUpperCase() + '?amount=' + amount;
26
+ }
27
+
28
+ async getAddressLastTransaction(receiver) {
29
+
30
+ let apiUrl;
31
+ if (this.testnet) {
32
+ apiUrl = this.api + 'address/' + receiver + '/txs';
33
+ } else {
34
+ apiUrl = this.api + 'rawaddr/' + receiver;
35
+ }
36
+
37
+ let data = await fetch(apiUrl).then(res => res.json());
38
+
39
+ if (data.txs) {
40
+
41
+ let tx = data.txs[0];
42
+
43
+ let index = tx.out.findIndex(object => {
44
+ return object.addr == receiver;
45
+ });
46
+
47
+ data = tx.out[index];
48
+
49
+ return {
50
+ hash: tx.hash,
51
+ amount: utils.toDec(data.value, 8)
52
+ }
53
+ } else {
54
+
55
+ let tx = data[0];
56
+
57
+ let index = tx.vout.findIndex(object => {
58
+ return object.scriptpubkey_address == receiver;
59
+ });
60
+
61
+ data = tx.vout[index];
62
+
63
+ return {
64
+ hash: tx.txid,
65
+ amount: utils.toDec(data.value, 8)
66
+ };
67
+ }
68
+ }
69
+
70
+ Transaction(hash) {
71
+ return new Transaction(hash, this);
72
+ }
73
+ }
74
+
75
+ module.exports = Provider;
@@ -0,0 +1,226 @@
1
+ const utils = require('@multiplechain/utils');
2
+
3
+ class Transaction {
4
+
5
+ /**
6
+ * @var {Object}
7
+ */
8
+ provider;
9
+
10
+ /**
11
+ * @var {String}
12
+ */
13
+ hash;
14
+
15
+ /**
16
+ * @var {Object}
17
+ */
18
+ data;
19
+
20
+ /**
21
+ * @var {Number}
22
+ */
23
+ timer;
24
+
25
+ /**
26
+ * @param {String} hash
27
+ */
28
+ constructor(hash, provider) {
29
+ this.provider = provider;
30
+ this.hash = hash;
31
+ }
32
+
33
+ /**
34
+ * @returns {String}
35
+ */
36
+ getHash() {
37
+ return this.hash;
38
+ }
39
+
40
+ /**
41
+ * @param {Number} timer
42
+ */
43
+ setTimer(timer) {
44
+ this.timer = timer;
45
+ }
46
+
47
+ /**
48
+ * @returns {Object}
49
+ */
50
+ async getData() {
51
+ try {
52
+
53
+ let txApi;
54
+
55
+ if (this.provider.testnet) {
56
+ txApi = this.provider.api + 'tx/' + this.hash;
57
+ } else {
58
+ txApi = this.provider.api + 'rawtx/' + this.hash;
59
+ }
60
+
61
+ this.data = await fetch(txApi).then(res => res.json());
62
+ } catch (error) {
63
+ throw new Error('There was a problem retrieving transaction data!');
64
+ }
65
+
66
+ return this.data;
67
+ }
68
+
69
+ /**
70
+ * @returns {Number}
71
+ */
72
+ async getConfirmations() {
73
+ try {
74
+
75
+ let blockApi;
76
+ if (this.provider.testnet) {
77
+ blockApi = this.provider.api + 'blocks/tip/height';
78
+ } else {
79
+ blockApi = this.provider.api + 'latestblock';
80
+ }
81
+
82
+ if (!this.data) await this.getData();
83
+ let latestBlock = await fetch(blockApi).then(res => res.json());
84
+
85
+ if (typeof latestBlock == 'object') {
86
+ latestBlock = latestBlock.height;
87
+ }
88
+
89
+ let blockHeight;
90
+ if (this.provider.testnet) {
91
+ blockHeight = this.data.status.block_height;
92
+ } else {
93
+ blockHeight = this.data.block_height;
94
+ }
95
+
96
+ return ((latestBlock - blockHeight) + 1);
97
+ } catch (error) {}
98
+ }
99
+
100
+ /**
101
+ * @param {Number} confirmations
102
+ * @returns {Number}
103
+ */
104
+ confirmTransaction(confirmations = 2) {
105
+ return new Promise((resolve, reject) => {
106
+ try {
107
+ this.intervalConfirm = setInterval(async () => {
108
+ const trxConfirmations = await this.getConfirmations();
109
+
110
+ if (trxConfirmations >= confirmations) {
111
+ clearInterval(this.intervalConfirm);
112
+ return resolve(trxConfirmations);
113
+ }
114
+ }, (this.timer*1000));
115
+ } catch (error) {
116
+ reject(error);
117
+ }
118
+ });
119
+ }
120
+
121
+ /**
122
+ * @returns {Boolean}
123
+ */
124
+ validateTransaction() {
125
+ return new Promise((resolve, reject) => {
126
+ this.intervalValidate = setInterval(async () => {
127
+ try {
128
+
129
+ await this.getData();
130
+
131
+ let result = null;
132
+
133
+ if (this.data == null) {
134
+ result = false;
135
+ } else {
136
+ if (this.provider.testnet) {
137
+ if (this.data.status.block_height) {
138
+ result = true;
139
+ }
140
+ } else {
141
+ if (this.data.block_height) {
142
+ result = true;
143
+ }
144
+ }
145
+ }
146
+
147
+ if (typeof result == 'boolean') {
148
+ clearInterval(this.intervalValidate);
149
+ if (result == true) {
150
+ resolve(true);
151
+ } else {
152
+ reject(false);
153
+ }
154
+ }
155
+
156
+ } catch (error) {
157
+ if (error.message == 'There was a problem retrieving transaction data!') {
158
+ this.validateTransaction();
159
+ } else {
160
+ clearInterval(this.intervalValidate);
161
+ reject(error);
162
+ }
163
+ }
164
+ }, (this.timer*1000));
165
+ });
166
+ }
167
+
168
+ /**
169
+ * @param {String} receiver
170
+ * @param {Number} amount
171
+ * @returns {Boolean}
172
+ */
173
+ async verifyTransferWithData(receiver, amount) {
174
+
175
+ if (await this.validateTransaction()) {
176
+
177
+ let data;
178
+ if (this.provider.testnet) {
179
+
180
+ let index = this.data.vout.findIndex(object => {
181
+ return object.scriptpubkey_address == receiver;
182
+ });
183
+
184
+ data = this.data.vout[index];
185
+
186
+ data = {
187
+ receiver: data.scriptpubkey_address,
188
+ amount: utils.toDec(data.value, 8)
189
+ };
190
+ } else {
191
+
192
+ let index = this.data.out.findIndex(object => {
193
+ return object.addr == receiver;
194
+ });
195
+
196
+ data = this.data.out[index];
197
+
198
+ data = {
199
+ receiver: data.addr,
200
+ amount: utils.toDec(data.value, 8)
201
+ };
202
+ }
203
+
204
+ if (data.receiver == receiver && data.amount == amount) {
205
+ return true;
206
+ } else {
207
+ return false;
208
+ }
209
+ } else {
210
+ return false;
211
+ }
212
+ }
213
+
214
+ /**
215
+ * @returns {String}
216
+ */
217
+ getUrl() {
218
+ if (this.provider.testnet) {
219
+ return this.provider.explorer + 'tx/' + this.hash;
220
+ } else {
221
+ return this.provider.explorer + 'transactions/btc/' + this.hash;
222
+ }
223
+ }
224
+ }
225
+
226
+ module.exports = Transaction;