@multiplechain/bitcoin 0.1.18 → 0.4.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/README.md +7 -1
- package/dist/assets/Coin.d.ts +38 -0
- package/dist/assets/index.d.ts +1 -0
- package/dist/browser/Wallet.d.ts +81 -0
- package/dist/browser/adapters/Leather.d.ts +16 -0
- package/dist/browser/adapters/UniSat.d.ts +19 -0
- package/dist/browser/adapters/Xverse.d.ts +6 -0
- package/dist/browser/adapters/icons.d.ts +6 -0
- package/dist/browser/adapters/index.d.ts +3 -0
- package/dist/browser/index.d.ts +11 -0
- package/dist/index.cjs +83 -0
- package/dist/index.cjs.map +7 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.es.js +43583 -0
- package/dist/index.es.js.map +1 -0
- package/dist/index.umd.js +133 -0
- package/dist/index.umd.js.map +1 -0
- package/dist/models/CoinTransaction.d.ts +24 -0
- package/dist/models/Transaction.d.ts +106 -0
- package/dist/models/index.d.ts +2 -0
- package/dist/services/Provider.d.ts +80 -0
- package/dist/services/TransactionListener.d.ts +120 -0
- package/dist/services/TransactionSigner.d.ts +50 -0
- package/dist/services/index.d.ts +2 -0
- package/dist/utils.d.ts +3 -0
- package/package.json +85 -50
- package/LICENSE +0 -21
- package/dist/bitcoin-provider.js +0 -2
- package/dist/bitcoin-provider.js.LICENSE.txt +0 -30
- package/src/adapters/leather.js +0 -75
- package/src/adapters/trustwallet.js +0 -32
- package/src/adapters/unisat.js +0 -42
- package/src/adapters/xverse.js +0 -86
- package/src/entity/coin.js +0 -74
- package/src/entity/token.js +0 -5
- package/src/entity/transaction.js +0 -183
- package/src/get-adapter.js +0 -14
- package/src/provider.js +0 -221
- package/src/utils.js +0 -26
- package/src/wallet.js +0 -180
package/src/provider.js
DELETED
|
@@ -1,221 +0,0 @@
|
|
|
1
|
-
const Coin = require("./entity/coin");
|
|
2
|
-
const Token = require("./entity/token");
|
|
3
|
-
const Transaction = require("./entity/transaction");
|
|
4
|
-
|
|
5
|
-
if (typeof window === 'undefined') {
|
|
6
|
-
WebSocket = require('ws');
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
class Provider {
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* @var {String}
|
|
13
|
-
*/
|
|
14
|
-
api;
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* @var {String}
|
|
18
|
-
*/
|
|
19
|
-
explorer;
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* @var {Boolean}
|
|
23
|
-
*/
|
|
24
|
-
testnet;
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* @var {String}
|
|
28
|
-
*/
|
|
29
|
-
network;
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* @var {Boolean}
|
|
33
|
-
*/
|
|
34
|
-
qrPayments = true;
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* @var {String}
|
|
38
|
-
*/
|
|
39
|
-
wsUrl;
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* @var {Object}
|
|
43
|
-
*/
|
|
44
|
-
supportedWallets;
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* @var {Object}
|
|
48
|
-
*/
|
|
49
|
-
connectedWallet;
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* @var {String}
|
|
53
|
-
*/
|
|
54
|
-
blockcypherToken;
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* @param {Object} options
|
|
58
|
-
*/
|
|
59
|
-
constructor(options = {}) {
|
|
60
|
-
|
|
61
|
-
this.testnet = options.testnet;
|
|
62
|
-
this.network = options.testnet ? 'testnet' : 'mainnet';
|
|
63
|
-
this.blockcypherToken = options.blockcypherToken;
|
|
64
|
-
|
|
65
|
-
if (this.testnet) {
|
|
66
|
-
this.api = "https://blockstream.info/testnet/api/";
|
|
67
|
-
this.explorer = "https://blockstream.info/testnet/";
|
|
68
|
-
let token = this.blockcypherToken || "6d9cba333f234b9498473955497c40d9";
|
|
69
|
-
this.wsUrl = "wss://socket.blockcypher.com/v1/btc/test3?token=" + token;
|
|
70
|
-
} else {
|
|
71
|
-
this.api = "https://blockstream.info/api/";
|
|
72
|
-
this.explorer = "https://blockstream.info/";
|
|
73
|
-
if (this.blockcypherToken) {
|
|
74
|
-
this.wsUrl = "wss://socket.blockcypher.com/v1/btc/main?token=" + this.blockcypherToken;
|
|
75
|
-
} else {
|
|
76
|
-
this.wsUrl = "wss://ws.blockchain.info/inv";
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
getWalletOpenLink(address, amount) {
|
|
82
|
-
return 'bitcoin' + ':' + String(address).toUpperCase() + '?amount=' + amount;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
listenTransactions(options, callback) {
|
|
86
|
-
let receiver = options.receiver;
|
|
87
|
-
let ws = new WebSocket(this.wsUrl);
|
|
88
|
-
|
|
89
|
-
let message;
|
|
90
|
-
if (this.testnet || this.blockcypherToken) {
|
|
91
|
-
message = JSON.stringify({
|
|
92
|
-
event: "unconfirmed-tx",
|
|
93
|
-
address: receiver,
|
|
94
|
-
token: this.blockcypherToken || "6d9cba333f234b9498473955497c40d9"
|
|
95
|
-
});
|
|
96
|
-
} else {
|
|
97
|
-
message = JSON.stringify({
|
|
98
|
-
"op": "unconfirmed_sub"
|
|
99
|
-
});
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
let subscription = {
|
|
103
|
-
unsubscribe: () => {
|
|
104
|
-
if (!this.testnet && !this.blockcypherToken) {
|
|
105
|
-
ws.send(JSON.stringify({
|
|
106
|
-
"op": "unconfirmed_unsub"
|
|
107
|
-
}));
|
|
108
|
-
}
|
|
109
|
-
ws.close();
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
let startCallback = async (data) => {
|
|
114
|
-
try {
|
|
115
|
-
let tx = this.Transaction(data.hash || data.x.hash);
|
|
116
|
-
await tx.getData();
|
|
117
|
-
callback(subscription, tx);
|
|
118
|
-
} catch (error) {
|
|
119
|
-
setTimeout(() => {
|
|
120
|
-
startCallback(data);
|
|
121
|
-
}, 2500);
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
ws.addEventListener('open', () => {
|
|
126
|
-
ws.send(message);
|
|
127
|
-
});
|
|
128
|
-
|
|
129
|
-
ws.addEventListener('message', (res) => {
|
|
130
|
-
let result = true;
|
|
131
|
-
let data = JSON.parse(res.data);
|
|
132
|
-
|
|
133
|
-
if (!this.testnet && !this.blockcypherToken) {
|
|
134
|
-
result = data.x.out.find(o => {
|
|
135
|
-
return String(o.addr).toLowerCase() == receiver.toLowerCase();
|
|
136
|
-
});
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
if (result) {
|
|
140
|
-
setTimeout(() => {
|
|
141
|
-
startCallback(data);
|
|
142
|
-
}, 6000);
|
|
143
|
-
}
|
|
144
|
-
});
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
/**
|
|
148
|
-
* @param {Wallet} wallet
|
|
149
|
-
*/
|
|
150
|
-
setConnectedWallet(wallet) {
|
|
151
|
-
this.connectedWallet = wallet;
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
/**
|
|
155
|
-
* @param {String} adapter
|
|
156
|
-
* @returns {Promise}
|
|
157
|
-
*/
|
|
158
|
-
connectWallet(adapter) {
|
|
159
|
-
return new Promise(async (resolve, reject) => {
|
|
160
|
-
let detectedWallets = this.getDetectedWallets();
|
|
161
|
-
if (detectedWallets[adapter]) {
|
|
162
|
-
let wallet = detectedWallets[adapter];
|
|
163
|
-
wallet.connect()
|
|
164
|
-
.then(() => {
|
|
165
|
-
resolve(wallet);
|
|
166
|
-
})
|
|
167
|
-
.catch(error => {
|
|
168
|
-
reject(error);
|
|
169
|
-
});
|
|
170
|
-
} else {
|
|
171
|
-
reject('wallet-not-found');
|
|
172
|
-
}
|
|
173
|
-
});
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
/**
|
|
177
|
-
* @param {Array|null} filter
|
|
178
|
-
* @returns {Array}
|
|
179
|
-
*/
|
|
180
|
-
getSupportedWallets(filter) {
|
|
181
|
-
if (!this.supportedWallets) {
|
|
182
|
-
const Wallet = require('./wallet');
|
|
183
|
-
|
|
184
|
-
this.supportedWallets = {
|
|
185
|
-
unisat: new Wallet('unisat', this),
|
|
186
|
-
xverse: new Wallet('xverse', this),
|
|
187
|
-
leather: new Wallet('leather', this),
|
|
188
|
-
trustwallet: new Wallet('trustwallet', this),
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
return Object.fromEntries(Object.entries(this.supportedWallets).filter(([key]) => {
|
|
193
|
-
return !filter ? true : filter.includes(key);
|
|
194
|
-
}));
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
/**
|
|
198
|
-
* @param {Array|null} filter
|
|
199
|
-
* @returns {Array}
|
|
200
|
-
*/
|
|
201
|
-
getDetectedWallets(filter) {
|
|
202
|
-
let detectedWallets = this.getSupportedWallets(filter);
|
|
203
|
-
return Object.fromEntries(Object.entries(detectedWallets).filter(([key, value]) => {
|
|
204
|
-
return value.isDetected() == undefined ? true : value.isDetected()
|
|
205
|
-
}));
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
Coin() {
|
|
209
|
-
return new Coin(this);
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
Token(address) {
|
|
213
|
-
return new Token(address, this);
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
Transaction(hash) {
|
|
217
|
-
return new Transaction(hash, this);
|
|
218
|
-
}
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
module.exports = Provider;
|
package/src/utils.js
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
const BigNumber = require('bignumber.js');
|
|
2
|
-
const utils = require('@multiplechain/utils');
|
|
3
|
-
|
|
4
|
-
module.exports = Object.assign(utils, {
|
|
5
|
-
toSatoshi(amount) {
|
|
6
|
-
let decimals = 8;
|
|
7
|
-
let length = '1' + '0'.repeat(decimals);
|
|
8
|
-
let value = new BigNumber(amount.toString(10), 10).times(length);
|
|
9
|
-
return parseInt(value.toString(10));
|
|
10
|
-
},
|
|
11
|
-
toBitcoin(amount) {
|
|
12
|
-
return parseFloat(amount.toString(10) / 100000000);
|
|
13
|
-
},
|
|
14
|
-
rejectMessage(error, reject) {
|
|
15
|
-
|
|
16
|
-
if (typeof error == 'object') {
|
|
17
|
-
if (error.code == 4001 || error.message == 'User rejected the request.') {
|
|
18
|
-
return reject('request-rejected');
|
|
19
|
-
} else if (String(error).includes('is not valid JSON')) {
|
|
20
|
-
return reject('not-accepted-chain');
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
return reject(error);
|
|
25
|
-
}
|
|
26
|
-
})
|
package/src/wallet.js
DELETED
|
@@ -1,180 +0,0 @@
|
|
|
1
|
-
const getAdapter = require('./get-adapter');
|
|
2
|
-
const utils = require('./utils');
|
|
3
|
-
|
|
4
|
-
class Wallet {
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* @var {Object}
|
|
8
|
-
*/
|
|
9
|
-
adapter;
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* @var {Object}
|
|
13
|
-
*/
|
|
14
|
-
wallet;
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* @var {Object}
|
|
18
|
-
*/
|
|
19
|
-
provider;
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* @var {String}
|
|
23
|
-
*/
|
|
24
|
-
connectedAccount;
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* @param {String} adapter
|
|
28
|
-
* @param {Object} provider
|
|
29
|
-
*/
|
|
30
|
-
constructor(adapter, provider) {
|
|
31
|
-
this.provider = provider;
|
|
32
|
-
this.setAdapter(adapter);
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* @param {String} adapter
|
|
37
|
-
*/
|
|
38
|
-
setAdapter(adapter) {
|
|
39
|
-
this.adapter = getAdapter(adapter, this.provider);
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* @returns {String}
|
|
44
|
-
*/
|
|
45
|
-
getKey() {
|
|
46
|
-
return this.adapter.key;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* @returns {String}
|
|
51
|
-
*/
|
|
52
|
-
getName() {
|
|
53
|
-
return this.adapter.name;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* @returns {String}
|
|
58
|
-
*/
|
|
59
|
-
getSupports() {
|
|
60
|
-
return this.adapter.supports;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* @returns {String}
|
|
65
|
-
*/
|
|
66
|
-
getDeepLink() {
|
|
67
|
-
return this.adapter.deepLink;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* @returns {String}
|
|
72
|
-
*/
|
|
73
|
-
getDownloadLink() {
|
|
74
|
-
return this.adapter.download;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
* @returns {Boolean}
|
|
79
|
-
*/
|
|
80
|
-
isDetected() {
|
|
81
|
-
return this.adapter.isDetected();
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
/**
|
|
85
|
-
* @returns {String}
|
|
86
|
-
*/
|
|
87
|
-
connect() {
|
|
88
|
-
return new Promise((resolve, reject) => {
|
|
89
|
-
this.adapter.connect()
|
|
90
|
-
.then(async (wallet) => {
|
|
91
|
-
this.wallet = wallet;
|
|
92
|
-
this.connectedAccount = await wallet.getAddress();
|
|
93
|
-
this.provider.setConnectedWallet(this);
|
|
94
|
-
resolve(this.connectedAccount);
|
|
95
|
-
})
|
|
96
|
-
.catch((error) => {
|
|
97
|
-
utils.rejectMessage(error, reject);
|
|
98
|
-
});
|
|
99
|
-
});
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
/**
|
|
103
|
-
* @returns {Array}
|
|
104
|
-
*/
|
|
105
|
-
getAccounts() {
|
|
106
|
-
return this.wallet.getAccounts();
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
/**
|
|
110
|
-
* @returns {String}
|
|
111
|
-
*/
|
|
112
|
-
getPublicKey() {
|
|
113
|
-
return this.wallet.getPublicKey();
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
/**
|
|
118
|
-
* @param {String} to
|
|
119
|
-
* @param {Integer} amount
|
|
120
|
-
* @return {Transaction|Object}
|
|
121
|
-
* @throws {Error}
|
|
122
|
-
*/
|
|
123
|
-
coinTransfer(to, amount) {
|
|
124
|
-
return new Promise(async (resolve, reject) => {
|
|
125
|
-
try {
|
|
126
|
-
let coin = this.provider.Coin();
|
|
127
|
-
if (parseFloat(amount) > await coin.getBalance(this.connectedAccount)) {
|
|
128
|
-
return reject('insufficient-balance');
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
amount = utils.toSatoshi(amount);
|
|
132
|
-
|
|
133
|
-
this.wallet.sendBitcoin(to, amount)
|
|
134
|
-
.then((transactionId) => {
|
|
135
|
-
resolve(this.provider.Transaction(transactionId));
|
|
136
|
-
})
|
|
137
|
-
.catch((error) => {
|
|
138
|
-
utils.rejectMessage(error, reject);
|
|
139
|
-
});
|
|
140
|
-
} catch (error) {
|
|
141
|
-
utils.rejectMessage(error, reject);
|
|
142
|
-
}
|
|
143
|
-
});
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
// TODO: implement
|
|
147
|
-
tokenTransfer(to, amount, tokenAddress) {
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
/**
|
|
151
|
-
* @param {String} to
|
|
152
|
-
* @param {Integer} amount
|
|
153
|
-
* @param {String|null} tokenAddress
|
|
154
|
-
* @return {Transaction|Object}
|
|
155
|
-
* @throws {Error}
|
|
156
|
-
*/
|
|
157
|
-
transfer(to, amount, tokenAddress = null) {
|
|
158
|
-
if (tokenAddress) {
|
|
159
|
-
return this.tokenTransfer(to, amount, tokenAddress);
|
|
160
|
-
} else {
|
|
161
|
-
return this.coinTransfer(to, amount);
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
// Events
|
|
166
|
-
accountsChanged(callback) {
|
|
167
|
-
this.wallet.on('accountsChanged', (accounts) => {
|
|
168
|
-
callback(accounts);
|
|
169
|
-
});
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
networkChanged(callback) {
|
|
173
|
-
this.wallet.on('networkChanged', (network) => {
|
|
174
|
-
callback(network);
|
|
175
|
-
});
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
module.exports = Wallet;
|