@dynamic-labs/bitcoin 3.0.0-alpha.3 → 3.0.0-alpha.30
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/CHANGELOG.md +303 -0
- package/package.json +5 -5
- package/src/BitcoinLocalStorageCache.cjs +0 -16
- package/src/BitcoinLocalStorageCache.d.ts +0 -7
- package/src/BitcoinLocalStorageCache.js +0 -16
- package/src/bitcoinProviderHelper.d.ts +4 -0
- package/src/connectors/BitcoinSatsConnectConnector/BitcoinSatsConnectConnector.cjs +14 -11
- package/src/connectors/BitcoinSatsConnectConnector/BitcoinSatsConnectConnector.d.ts +0 -1
- package/src/connectors/BitcoinSatsConnectConnector/BitcoinSatsConnectConnector.js +14 -11
- package/src/connectors/BitcoinWalletConnector.cjs +42 -50
- package/src/connectors/BitcoinWalletConnector.d.ts +4 -1
- package/src/connectors/BitcoinWalletConnector.js +43 -51
- package/src/connectors/FallbackBitcoinConnector/FallbackBitcoinConnector.cjs +37 -0
- package/src/connectors/FallbackBitcoinConnector/FallbackBitcoinConnector.d.ts +11 -0
- package/src/connectors/FallbackBitcoinConnector/FallbackBitcoinConnector.js +33 -0
- package/src/connectors/FallbackBitcoinConnector/index.d.ts +1 -0
- package/src/connectors/OkxConnector/OkxConnector.cjs +2 -2
- package/src/connectors/OkxConnector/OkxConnector.d.ts +0 -1
- package/src/connectors/OkxConnector/OkxConnector.js +2 -2
- package/src/connectors/PhantomConnector/PhantomConnector.cjs +1 -2
- package/src/connectors/PhantomConnector/PhantomConnector.d.ts +0 -1
- package/src/connectors/PhantomConnector/PhantomConnector.js +1 -2
- package/src/connectors/UnisatConnector/UnisatConnector.cjs +9 -1
- package/src/connectors/UnisatConnector/UnisatConnector.d.ts +1 -1
- package/src/connectors/UnisatConnector/UnisatConnector.js +9 -1
- package/src/connectors/UnknownInjected/UnknownInjected.cjs +0 -1
- package/src/connectors/UnknownInjected/UnknownInjected.d.ts +0 -1
- package/src/connectors/UnknownInjected/UnknownInjected.js +0 -1
- package/src/connectors/index.d.ts +1 -0
- package/src/index.cjs +3 -1
- package/src/index.js +3 -1
- package/src/utils/fetchSatsConnectConnectors/fetchSatsConnectConnectors.cjs +1 -1
- package/src/utils/fetchSatsConnectConnectors/fetchSatsConnectConnectors.js +1 -1
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
5
5
|
|
|
6
6
|
var _tslib = require('../../_virtual/_tslib.cjs');
|
|
7
|
+
var satsConnect = require('sats-connect');
|
|
7
8
|
var walletConnectorCore = require('@dynamic-labs/wallet-connector-core');
|
|
8
9
|
var walletBook = require('@dynamic-labs/wallet-book');
|
|
9
10
|
var utils = require('@dynamic-labs/utils');
|
|
@@ -26,12 +27,20 @@ class BitcoinWalletConnector extends walletConnectorCore.WalletConnectorBase {
|
|
|
26
27
|
this.verifiedCredentials = [];
|
|
27
28
|
// this is the key from the wallet book entry so that we don't purely rely on the normalized name
|
|
28
29
|
this.overrideKey = (_a = opts.overrideKey) !== null && _a !== void 0 ? _a : this.key;
|
|
29
|
-
|
|
30
|
+
const walletBookWallet = opts.walletData || walletBook.getWalletBookWallet(this.walletBook, this.key);
|
|
31
|
+
this.bitcoinProviderHelper = new bitcoinProviderHelper.BitcoinProviderHelper(walletBookWallet);
|
|
30
32
|
this.wallet = this.bitcoinProviderHelper.findWallet();
|
|
31
33
|
if (this.wallet) {
|
|
32
34
|
this.walletMethods = this.bitcoinProviderHelper.getWalletMethods(this.wallet);
|
|
33
35
|
}
|
|
34
36
|
this.cache = new BitcoinLocalStorageCache.BitcoinLocalStorageCache(this.overrideKey);
|
|
37
|
+
this.canFetchConnectedAccounts = walletBook.isWalletMethodSupported(walletBookWallet, 'getConnectedAccounts', 'browserExtension');
|
|
38
|
+
}
|
|
39
|
+
isSameAccountChangeRequest(to) {
|
|
40
|
+
return this.lastAccountChange === to;
|
|
41
|
+
}
|
|
42
|
+
setLastAccountChangeRequest(to) {
|
|
43
|
+
this.lastAccountChange = to;
|
|
35
44
|
}
|
|
36
45
|
clearConnectedAccounts() {
|
|
37
46
|
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
@@ -53,31 +62,17 @@ class BitcoinWalletConnector extends walletConnectorCore.WalletConnectorBase {
|
|
|
53
62
|
}
|
|
54
63
|
endSession() {
|
|
55
64
|
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
56
|
-
yield this.cache.
|
|
65
|
+
yield this.cache.clearConnectedAcccounts();
|
|
57
66
|
});
|
|
58
67
|
}
|
|
59
|
-
getBalance() {
|
|
68
|
+
getBalance(address) {
|
|
60
69
|
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
61
|
-
|
|
62
|
-
const
|
|
63
|
-
if (!connectedAddress) {
|
|
64
|
-
throw new utils.DynamicError('getBalance - No connected address found!');
|
|
65
|
-
}
|
|
66
|
-
const additionalAddresses = yield this.getAdditionalAddresses(connectedAddress);
|
|
67
|
-
const ordinalsAdditionalAddress = (_a = additionalAddresses.find((address) => address.type === sdkApiCore.WalletAddressType.Ordinals)) === null || _a === void 0 ? void 0 : _a.address;
|
|
68
|
-
const paymentAdditionalAddress = (_b = additionalAddresses.find((address) => address.type === sdkApiCore.WalletAddressType.Payment)) === null || _b === void 0 ? void 0 : _b.address;
|
|
69
|
-
// Some BTC wallet connectors only have 1 address type. when this is the case, it would always be `ordinals`
|
|
70
|
-
// if a BTC walletConnector does NOT have a payment address, just use the ordinal address
|
|
71
|
-
const paymentAddress = paymentAdditionalAddress !== null && paymentAdditionalAddress !== void 0 ? paymentAdditionalAddress : ordinalsAdditionalAddress;
|
|
72
|
-
if (!paymentAddress) {
|
|
73
|
-
throw new utils.DynamicError('getBalance - No payment address found!');
|
|
74
|
-
}
|
|
75
|
-
const API_URL = getMempoolApiUrl.getMempoolApiUrl(paymentAddress);
|
|
76
|
-
const response = yield fetch(`${API_URL}/address/${paymentAddress}`);
|
|
70
|
+
const API_URL = getMempoolApiUrl.getMempoolApiUrl(address);
|
|
71
|
+
const response = yield fetch(`${API_URL}/address/${address}`);
|
|
77
72
|
if (!response.ok) {
|
|
78
73
|
// if the request fails due to rate limits, return cached value
|
|
79
74
|
if (response.status === _const.HTTP_STATUS_TOO_MANY_REQUESTS) {
|
|
80
|
-
return
|
|
75
|
+
return '0';
|
|
81
76
|
}
|
|
82
77
|
// new accounts not yet indexed will return a 404
|
|
83
78
|
if (response.status === _const.HTTP_STATUS_NOT_FOUND) {
|
|
@@ -94,7 +89,6 @@ class BitcoinWalletConnector extends walletConnectorCore.WalletConnectorBase {
|
|
|
94
89
|
const unconfirmedBalanceInSats = Number(addressInfo.mempool_stats.funded_txo_sum) -
|
|
95
90
|
Number(addressInfo.mempool_stats.spent_txo_sum);
|
|
96
91
|
const balance = satoshisToBtc.satoshisToBtc(confirmedBalanceInSats + unconfirmedBalanceInSats);
|
|
97
|
-
yield this.cache.setLastBalance(balance.toString());
|
|
98
92
|
return balance.toString();
|
|
99
93
|
});
|
|
100
94
|
}
|
|
@@ -116,31 +110,7 @@ class BitcoinWalletConnector extends walletConnectorCore.WalletConnectorBase {
|
|
|
116
110
|
}
|
|
117
111
|
getConnectedAccounts() {
|
|
118
112
|
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
119
|
-
|
|
120
|
-
// without prompting for a connection
|
|
121
|
-
// to avoid this behavior, we cache the connected accounts
|
|
122
|
-
if (!this.canFetchConnectedAccounts) {
|
|
123
|
-
return this.getConnectedAccountsFromCache();
|
|
124
|
-
}
|
|
125
|
-
// if we decide that is ok to prompt for a connection when fetching connected accounts
|
|
126
|
-
// we shouldn't prompt every time we call this method (which is a lot of times)
|
|
127
|
-
// so we just store in a promise and return the same promise every time
|
|
128
|
-
if (!this.getAddressPromise) {
|
|
129
|
-
this.getAddressPromise = this.getAddress();
|
|
130
|
-
}
|
|
131
|
-
let connectedAccount;
|
|
132
|
-
try {
|
|
133
|
-
connectedAccount = yield this.getAddressPromise;
|
|
134
|
-
}
|
|
135
|
-
catch (error) {
|
|
136
|
-
walletConnectorCore.logger.error(`${this.key} getConnectedAccounts - error fetching connected account`);
|
|
137
|
-
//don't throw error just return empty array after clearing the promise
|
|
138
|
-
}
|
|
139
|
-
this.getAddressPromise = undefined;
|
|
140
|
-
if (!connectedAccount) {
|
|
141
|
-
return [];
|
|
142
|
-
}
|
|
143
|
-
return [connectedAccount];
|
|
113
|
+
return this.getConnectedAccountsFromCache();
|
|
144
114
|
});
|
|
145
115
|
}
|
|
146
116
|
getAdditionalAddresses(mainAddress) {
|
|
@@ -223,25 +193,47 @@ class BitcoinWalletConnector extends walletConnectorCore.WalletConnectorBase {
|
|
|
223
193
|
return;
|
|
224
194
|
}
|
|
225
195
|
const { handleAccountChange, handleChainChange, handleDisconnect } = walletConnectorCore.eventListenerHandlers(this);
|
|
196
|
+
const handleBitcoinDisconnect = (error) => _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
197
|
+
this.cache.clearConnectedAcccounts();
|
|
198
|
+
handleDisconnect(error);
|
|
199
|
+
});
|
|
226
200
|
const handleBitcoinAccountChange = (accounts) => _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
201
|
+
if (accounts.length === 0) {
|
|
202
|
+
this.cache.clearConnectedAcccounts();
|
|
203
|
+
}
|
|
227
204
|
let connectedAccounts = accounts;
|
|
205
|
+
let ordinalsAccount, paymentAccount;
|
|
228
206
|
// if accounts is an array of objects, we need to parse them to return only addresses
|
|
229
207
|
// since ordinals is the main address we use, we should return it as the first address
|
|
230
208
|
if (typeof accounts[0] === 'object') {
|
|
231
209
|
connectedAccounts = accounts
|
|
232
|
-
.sort((account) =>
|
|
210
|
+
.sort((account) => account.purpose === satsConnect.AddressPurpose.Ordinals ? -1 : 1)
|
|
233
211
|
.map((account) => account.address);
|
|
212
|
+
[ordinalsAccount, paymentAccount] = connectedAccounts;
|
|
234
213
|
}
|
|
235
214
|
const currentConnectedAccounts = yield this.getConnectedAccountsFromCache();
|
|
236
215
|
// don't do anything if the connected accounts haven't changed
|
|
237
|
-
if
|
|
216
|
+
// or if the account change request is the same as previous request
|
|
217
|
+
if (currentConnectedAccounts[0] === connectedAccounts[0] ||
|
|
218
|
+
this.isSameAccountChangeRequest(connectedAccounts[0])) {
|
|
238
219
|
return;
|
|
239
220
|
}
|
|
221
|
+
// set the last account change request with the from and to addresses
|
|
222
|
+
// to ensure that the requests are not duplicated
|
|
223
|
+
this.setLastAccountChangeRequest(connectedAccounts[0]);
|
|
224
|
+
if (ordinalsAccount || paymentAccount) {
|
|
225
|
+
this.setConnectedAccountWithAddresses({
|
|
226
|
+
active: true,
|
|
227
|
+
mainAddress: ordinalsAccount !== null && ordinalsAccount !== void 0 ? ordinalsAccount : paymentAccount,
|
|
228
|
+
ordinalsAddress: ordinalsAccount,
|
|
229
|
+
paymentAddress: paymentAccount,
|
|
230
|
+
});
|
|
231
|
+
}
|
|
240
232
|
handleAccountChange(connectedAccounts);
|
|
241
233
|
});
|
|
242
234
|
provider.on('accountsChanged', handleBitcoinAccountChange);
|
|
243
235
|
provider.on('networkChanged', handleChainChange);
|
|
244
|
-
provider.on('disconnect',
|
|
236
|
+
provider.on('disconnect', handleBitcoinDisconnect);
|
|
245
237
|
const tearDownEventListeners = () => {
|
|
246
238
|
const provider = this.getProvider();
|
|
247
239
|
if (!(provider === null || provider === void 0 ? void 0 : provider.removeListener)) {
|
|
@@ -249,7 +241,7 @@ class BitcoinWalletConnector extends walletConnectorCore.WalletConnectorBase {
|
|
|
249
241
|
}
|
|
250
242
|
provider.removeListener('accountsChanged', handleBitcoinAccountChange);
|
|
251
243
|
provider.removeListener('networkChanged', handleChainChange);
|
|
252
|
-
provider.removeListener('disconnect',
|
|
244
|
+
provider.removeListener('disconnect', handleBitcoinDisconnect);
|
|
253
245
|
};
|
|
254
246
|
this.teardownEventListeners = tearDownEventListeners;
|
|
255
247
|
}
|
|
@@ -23,13 +23,16 @@ export declare abstract class BitcoinWalletConnector extends WalletConnectorBase
|
|
|
23
23
|
canFetchConnectedAccounts: boolean;
|
|
24
24
|
isHardwareWalletEnabled: boolean;
|
|
25
25
|
verifiedCredentials: JwtVerifiedCredential[];
|
|
26
|
+
private lastAccountChange;
|
|
26
27
|
constructor(opts: BitcoinWalletConnectorOpts);
|
|
28
|
+
private isSameAccountChangeRequest;
|
|
29
|
+
private setLastAccountChangeRequest;
|
|
27
30
|
clearConnectedAccounts(): Promise<void>;
|
|
28
31
|
canConnectWithHardwareWallet(): boolean;
|
|
29
32
|
isInstalledOnBrowser(): boolean;
|
|
30
33
|
getDeepLink(): string | undefined;
|
|
31
34
|
endSession(): Promise<void>;
|
|
32
|
-
getBalance(): Promise<string | undefined>;
|
|
35
|
+
getBalance(address: string): Promise<string | undefined>;
|
|
33
36
|
getConnectedAccountsFromCache(): Promise<string[]>;
|
|
34
37
|
getConnectedAccounts(): Promise<string[]>;
|
|
35
38
|
getAdditionalAddresses(mainAddress?: string): Promise<WalletAdditionalAddress[]>;
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
'use client'
|
|
2
2
|
import { __awaiter } from '../../_virtual/_tslib.js';
|
|
3
|
+
import { AddressPurpose } from 'sats-connect';
|
|
3
4
|
import { WalletConnectorBase, eventListenerHandlers, logger } from '@dynamic-labs/wallet-connector-core';
|
|
4
|
-
import { getWalletBookWallet, findWalletBookWallet } from '@dynamic-labs/wallet-book';
|
|
5
|
+
import { getWalletBookWallet, isWalletMethodSupported, findWalletBookWallet } from '@dynamic-labs/wallet-book';
|
|
5
6
|
import { isLedgerAddressViaVerifiedCredentials, DynamicError } from '@dynamic-labs/utils';
|
|
6
7
|
import { WalletAddressType } from '@dynamic-labs/sdk-api-core';
|
|
7
8
|
import { BitcoinLocalStorageCache } from '../BitcoinLocalStorageCache.js';
|
|
@@ -22,12 +23,20 @@ class BitcoinWalletConnector extends WalletConnectorBase {
|
|
|
22
23
|
this.verifiedCredentials = [];
|
|
23
24
|
// this is the key from the wallet book entry so that we don't purely rely on the normalized name
|
|
24
25
|
this.overrideKey = (_a = opts.overrideKey) !== null && _a !== void 0 ? _a : this.key;
|
|
25
|
-
|
|
26
|
+
const walletBookWallet = opts.walletData || getWalletBookWallet(this.walletBook, this.key);
|
|
27
|
+
this.bitcoinProviderHelper = new BitcoinProviderHelper(walletBookWallet);
|
|
26
28
|
this.wallet = this.bitcoinProviderHelper.findWallet();
|
|
27
29
|
if (this.wallet) {
|
|
28
30
|
this.walletMethods = this.bitcoinProviderHelper.getWalletMethods(this.wallet);
|
|
29
31
|
}
|
|
30
32
|
this.cache = new BitcoinLocalStorageCache(this.overrideKey);
|
|
33
|
+
this.canFetchConnectedAccounts = isWalletMethodSupported(walletBookWallet, 'getConnectedAccounts', 'browserExtension');
|
|
34
|
+
}
|
|
35
|
+
isSameAccountChangeRequest(to) {
|
|
36
|
+
return this.lastAccountChange === to;
|
|
37
|
+
}
|
|
38
|
+
setLastAccountChangeRequest(to) {
|
|
39
|
+
this.lastAccountChange = to;
|
|
31
40
|
}
|
|
32
41
|
clearConnectedAccounts() {
|
|
33
42
|
return __awaiter(this, void 0, void 0, function* () {
|
|
@@ -49,31 +58,17 @@ class BitcoinWalletConnector extends WalletConnectorBase {
|
|
|
49
58
|
}
|
|
50
59
|
endSession() {
|
|
51
60
|
return __awaiter(this, void 0, void 0, function* () {
|
|
52
|
-
yield this.cache.
|
|
61
|
+
yield this.cache.clearConnectedAcccounts();
|
|
53
62
|
});
|
|
54
63
|
}
|
|
55
|
-
getBalance() {
|
|
64
|
+
getBalance(address) {
|
|
56
65
|
return __awaiter(this, void 0, void 0, function* () {
|
|
57
|
-
|
|
58
|
-
const
|
|
59
|
-
if (!connectedAddress) {
|
|
60
|
-
throw new DynamicError('getBalance - No connected address found!');
|
|
61
|
-
}
|
|
62
|
-
const additionalAddresses = yield this.getAdditionalAddresses(connectedAddress);
|
|
63
|
-
const ordinalsAdditionalAddress = (_a = additionalAddresses.find((address) => address.type === WalletAddressType.Ordinals)) === null || _a === void 0 ? void 0 : _a.address;
|
|
64
|
-
const paymentAdditionalAddress = (_b = additionalAddresses.find((address) => address.type === WalletAddressType.Payment)) === null || _b === void 0 ? void 0 : _b.address;
|
|
65
|
-
// Some BTC wallet connectors only have 1 address type. when this is the case, it would always be `ordinals`
|
|
66
|
-
// if a BTC walletConnector does NOT have a payment address, just use the ordinal address
|
|
67
|
-
const paymentAddress = paymentAdditionalAddress !== null && paymentAdditionalAddress !== void 0 ? paymentAdditionalAddress : ordinalsAdditionalAddress;
|
|
68
|
-
if (!paymentAddress) {
|
|
69
|
-
throw new DynamicError('getBalance - No payment address found!');
|
|
70
|
-
}
|
|
71
|
-
const API_URL = getMempoolApiUrl(paymentAddress);
|
|
72
|
-
const response = yield fetch(`${API_URL}/address/${paymentAddress}`);
|
|
66
|
+
const API_URL = getMempoolApiUrl(address);
|
|
67
|
+
const response = yield fetch(`${API_URL}/address/${address}`);
|
|
73
68
|
if (!response.ok) {
|
|
74
69
|
// if the request fails due to rate limits, return cached value
|
|
75
70
|
if (response.status === HTTP_STATUS_TOO_MANY_REQUESTS) {
|
|
76
|
-
return
|
|
71
|
+
return '0';
|
|
77
72
|
}
|
|
78
73
|
// new accounts not yet indexed will return a 404
|
|
79
74
|
if (response.status === HTTP_STATUS_NOT_FOUND) {
|
|
@@ -90,7 +85,6 @@ class BitcoinWalletConnector extends WalletConnectorBase {
|
|
|
90
85
|
const unconfirmedBalanceInSats = Number(addressInfo.mempool_stats.funded_txo_sum) -
|
|
91
86
|
Number(addressInfo.mempool_stats.spent_txo_sum);
|
|
92
87
|
const balance = satoshisToBtc(confirmedBalanceInSats + unconfirmedBalanceInSats);
|
|
93
|
-
yield this.cache.setLastBalance(balance.toString());
|
|
94
88
|
return balance.toString();
|
|
95
89
|
});
|
|
96
90
|
}
|
|
@@ -112,31 +106,7 @@ class BitcoinWalletConnector extends WalletConnectorBase {
|
|
|
112
106
|
}
|
|
113
107
|
getConnectedAccounts() {
|
|
114
108
|
return __awaiter(this, void 0, void 0, function* () {
|
|
115
|
-
|
|
116
|
-
// without prompting for a connection
|
|
117
|
-
// to avoid this behavior, we cache the connected accounts
|
|
118
|
-
if (!this.canFetchConnectedAccounts) {
|
|
119
|
-
return this.getConnectedAccountsFromCache();
|
|
120
|
-
}
|
|
121
|
-
// if we decide that is ok to prompt for a connection when fetching connected accounts
|
|
122
|
-
// we shouldn't prompt every time we call this method (which is a lot of times)
|
|
123
|
-
// so we just store in a promise and return the same promise every time
|
|
124
|
-
if (!this.getAddressPromise) {
|
|
125
|
-
this.getAddressPromise = this.getAddress();
|
|
126
|
-
}
|
|
127
|
-
let connectedAccount;
|
|
128
|
-
try {
|
|
129
|
-
connectedAccount = yield this.getAddressPromise;
|
|
130
|
-
}
|
|
131
|
-
catch (error) {
|
|
132
|
-
logger.error(`${this.key} getConnectedAccounts - error fetching connected account`);
|
|
133
|
-
//don't throw error just return empty array after clearing the promise
|
|
134
|
-
}
|
|
135
|
-
this.getAddressPromise = undefined;
|
|
136
|
-
if (!connectedAccount) {
|
|
137
|
-
return [];
|
|
138
|
-
}
|
|
139
|
-
return [connectedAccount];
|
|
109
|
+
return this.getConnectedAccountsFromCache();
|
|
140
110
|
});
|
|
141
111
|
}
|
|
142
112
|
getAdditionalAddresses(mainAddress) {
|
|
@@ -219,25 +189,47 @@ class BitcoinWalletConnector extends WalletConnectorBase {
|
|
|
219
189
|
return;
|
|
220
190
|
}
|
|
221
191
|
const { handleAccountChange, handleChainChange, handleDisconnect } = eventListenerHandlers(this);
|
|
192
|
+
const handleBitcoinDisconnect = (error) => __awaiter(this, void 0, void 0, function* () {
|
|
193
|
+
this.cache.clearConnectedAcccounts();
|
|
194
|
+
handleDisconnect(error);
|
|
195
|
+
});
|
|
222
196
|
const handleBitcoinAccountChange = (accounts) => __awaiter(this, void 0, void 0, function* () {
|
|
197
|
+
if (accounts.length === 0) {
|
|
198
|
+
this.cache.clearConnectedAcccounts();
|
|
199
|
+
}
|
|
223
200
|
let connectedAccounts = accounts;
|
|
201
|
+
let ordinalsAccount, paymentAccount;
|
|
224
202
|
// if accounts is an array of objects, we need to parse them to return only addresses
|
|
225
203
|
// since ordinals is the main address we use, we should return it as the first address
|
|
226
204
|
if (typeof accounts[0] === 'object') {
|
|
227
205
|
connectedAccounts = accounts
|
|
228
|
-
.sort((account) =>
|
|
206
|
+
.sort((account) => account.purpose === AddressPurpose.Ordinals ? -1 : 1)
|
|
229
207
|
.map((account) => account.address);
|
|
208
|
+
[ordinalsAccount, paymentAccount] = connectedAccounts;
|
|
230
209
|
}
|
|
231
210
|
const currentConnectedAccounts = yield this.getConnectedAccountsFromCache();
|
|
232
211
|
// don't do anything if the connected accounts haven't changed
|
|
233
|
-
if
|
|
212
|
+
// or if the account change request is the same as previous request
|
|
213
|
+
if (currentConnectedAccounts[0] === connectedAccounts[0] ||
|
|
214
|
+
this.isSameAccountChangeRequest(connectedAccounts[0])) {
|
|
234
215
|
return;
|
|
235
216
|
}
|
|
217
|
+
// set the last account change request with the from and to addresses
|
|
218
|
+
// to ensure that the requests are not duplicated
|
|
219
|
+
this.setLastAccountChangeRequest(connectedAccounts[0]);
|
|
220
|
+
if (ordinalsAccount || paymentAccount) {
|
|
221
|
+
this.setConnectedAccountWithAddresses({
|
|
222
|
+
active: true,
|
|
223
|
+
mainAddress: ordinalsAccount !== null && ordinalsAccount !== void 0 ? ordinalsAccount : paymentAccount,
|
|
224
|
+
ordinalsAddress: ordinalsAccount,
|
|
225
|
+
paymentAddress: paymentAccount,
|
|
226
|
+
});
|
|
227
|
+
}
|
|
236
228
|
handleAccountChange(connectedAccounts);
|
|
237
229
|
});
|
|
238
230
|
provider.on('accountsChanged', handleBitcoinAccountChange);
|
|
239
231
|
provider.on('networkChanged', handleChainChange);
|
|
240
|
-
provider.on('disconnect',
|
|
232
|
+
provider.on('disconnect', handleBitcoinDisconnect);
|
|
241
233
|
const tearDownEventListeners = () => {
|
|
242
234
|
const provider = this.getProvider();
|
|
243
235
|
if (!(provider === null || provider === void 0 ? void 0 : provider.removeListener)) {
|
|
@@ -245,7 +237,7 @@ class BitcoinWalletConnector extends WalletConnectorBase {
|
|
|
245
237
|
}
|
|
246
238
|
provider.removeListener('accountsChanged', handleBitcoinAccountChange);
|
|
247
239
|
provider.removeListener('networkChanged', handleChainChange);
|
|
248
|
-
provider.removeListener('disconnect',
|
|
240
|
+
provider.removeListener('disconnect', handleBitcoinDisconnect);
|
|
249
241
|
};
|
|
250
242
|
this.teardownEventListeners = tearDownEventListeners;
|
|
251
243
|
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
5
|
+
|
|
6
|
+
var _tslib = require('../../../_virtual/_tslib.cjs');
|
|
7
|
+
var BitcoinWalletConnector = require('../BitcoinWalletConnector.cjs');
|
|
8
|
+
|
|
9
|
+
class FallbackBitcoinConnector extends BitcoinWalletConnector.BitcoinWalletConnector {
|
|
10
|
+
constructor(opts) {
|
|
11
|
+
super(Object.assign(Object.assign({}, opts), { overrideKey: 'fallbackconnector' }));
|
|
12
|
+
this.name = 'Fallback Connector';
|
|
13
|
+
this.overrideKey = 'fallbackconnector';
|
|
14
|
+
this.isAvailable = false;
|
|
15
|
+
}
|
|
16
|
+
getAddress() {
|
|
17
|
+
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
18
|
+
return;
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
signPsbt(
|
|
22
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
23
|
+
_request) {
|
|
24
|
+
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
25
|
+
return;
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
sendBitcoin(
|
|
29
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
30
|
+
_transaction) {
|
|
31
|
+
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
32
|
+
return;
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
exports.FallbackBitcoinConnector = FallbackBitcoinConnector;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { BitcoinSignPsbtRequest, BitcoinSignPsbtResponse, BitcoinTransaction } from '../../types';
|
|
2
|
+
import { BitcoinWalletConnector, BitcoinWalletConnectorOpts } from '../BitcoinWalletConnector';
|
|
3
|
+
export declare class FallbackBitcoinConnector extends BitcoinWalletConnector {
|
|
4
|
+
name: string;
|
|
5
|
+
overrideKey: string;
|
|
6
|
+
isAvailable: boolean;
|
|
7
|
+
constructor(opts: BitcoinWalletConnectorOpts);
|
|
8
|
+
getAddress(): Promise<string | undefined>;
|
|
9
|
+
signPsbt(_request: BitcoinSignPsbtRequest): Promise<BitcoinSignPsbtResponse | undefined>;
|
|
10
|
+
sendBitcoin(_transaction: BitcoinTransaction): Promise<string | undefined>;
|
|
11
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
import { __awaiter } from '../../../_virtual/_tslib.js';
|
|
3
|
+
import { BitcoinWalletConnector } from '../BitcoinWalletConnector.js';
|
|
4
|
+
|
|
5
|
+
class FallbackBitcoinConnector extends BitcoinWalletConnector {
|
|
6
|
+
constructor(opts) {
|
|
7
|
+
super(Object.assign(Object.assign({}, opts), { overrideKey: 'fallbackconnector' }));
|
|
8
|
+
this.name = 'Fallback Connector';
|
|
9
|
+
this.overrideKey = 'fallbackconnector';
|
|
10
|
+
this.isAvailable = false;
|
|
11
|
+
}
|
|
12
|
+
getAddress() {
|
|
13
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
14
|
+
return;
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
signPsbt(
|
|
18
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
19
|
+
_request) {
|
|
20
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
21
|
+
return;
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
sendBitcoin(
|
|
25
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
26
|
+
_transaction) {
|
|
27
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
28
|
+
return;
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export { FallbackBitcoinConnector };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { FallbackBitcoinConnector } from './FallbackBitcoinConnector';
|
|
@@ -13,7 +13,6 @@ class OkxConnector extends BitcoinWalletConnector.BitcoinWalletConnector {
|
|
|
13
13
|
constructor(opts) {
|
|
14
14
|
super(Object.assign(Object.assign({}, opts), { overrideKey: 'okxwalletbtc' }));
|
|
15
15
|
this.name = 'OKX Wallet';
|
|
16
|
-
this.canFetchConnectedAccounts = true;
|
|
17
16
|
}
|
|
18
17
|
get walletBookWallet() {
|
|
19
18
|
return this.walletBook.wallets[this.key];
|
|
@@ -23,7 +22,8 @@ class OkxConnector extends BitcoinWalletConnector.BitcoinWalletConnector {
|
|
|
23
22
|
if (!this.isInstalledOnBrowser() &&
|
|
24
23
|
utils.isMobile() &&
|
|
25
24
|
this.walletBookWallet.mobile &&
|
|
26
|
-
this.walletBookWallet.mobile.inAppBrowser
|
|
25
|
+
this.walletBookWallet.mobile.inAppBrowser &&
|
|
26
|
+
this.mobileExperience === 'in-app-browser') {
|
|
27
27
|
const inAppBrowserCompiledTemplate = utils.template(this.walletBookWallet.mobile.inAppBrowser);
|
|
28
28
|
const deepLink = inAppBrowserCompiledTemplate({
|
|
29
29
|
encodedDappURI: encodeURIComponent(window.location.toString()),
|
|
@@ -2,7 +2,6 @@ import { BitcoinWalletConnector, BitcoinWalletConnectorOpts } from '../BitcoinWa
|
|
|
2
2
|
import { BitcoinTransaction, BitcoinSignPsbtRequest, BitcoinSignPsbtResponse } from '../../types';
|
|
3
3
|
export declare class OkxConnector extends BitcoinWalletConnector {
|
|
4
4
|
name: string;
|
|
5
|
-
canFetchConnectedAccounts: boolean;
|
|
6
5
|
constructor(opts: BitcoinWalletConnectorOpts);
|
|
7
6
|
private get walletBookWallet();
|
|
8
7
|
getAddress(): Promise<string | undefined>;
|
|
@@ -9,7 +9,6 @@ class OkxConnector extends BitcoinWalletConnector {
|
|
|
9
9
|
constructor(opts) {
|
|
10
10
|
super(Object.assign(Object.assign({}, opts), { overrideKey: 'okxwalletbtc' }));
|
|
11
11
|
this.name = 'OKX Wallet';
|
|
12
|
-
this.canFetchConnectedAccounts = true;
|
|
13
12
|
}
|
|
14
13
|
get walletBookWallet() {
|
|
15
14
|
return this.walletBook.wallets[this.key];
|
|
@@ -19,7 +18,8 @@ class OkxConnector extends BitcoinWalletConnector {
|
|
|
19
18
|
if (!this.isInstalledOnBrowser() &&
|
|
20
19
|
isMobile() &&
|
|
21
20
|
this.walletBookWallet.mobile &&
|
|
22
|
-
this.walletBookWallet.mobile.inAppBrowser
|
|
21
|
+
this.walletBookWallet.mobile.inAppBrowser &&
|
|
22
|
+
this.mobileExperience === 'in-app-browser') {
|
|
23
23
|
const inAppBrowserCompiledTemplate = template(this.walletBookWallet.mobile.inAppBrowser);
|
|
24
24
|
const deepLink = inAppBrowserCompiledTemplate({
|
|
25
25
|
encodedDappURI: encodeURIComponent(window.location.toString()),
|
|
@@ -17,7 +17,6 @@ class PhantomConnector extends BitcoinWalletConnector.BitcoinWalletConnector {
|
|
|
17
17
|
constructor(opts) {
|
|
18
18
|
super(Object.assign(Object.assign({}, opts), { overrideKey: 'phantombtc' }));
|
|
19
19
|
this.name = 'Phantom';
|
|
20
|
-
this.canFetchConnectedAccounts = true;
|
|
21
20
|
}
|
|
22
21
|
connectWithInstalledExtension() {
|
|
23
22
|
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
@@ -27,7 +26,7 @@ class PhantomConnector extends BitcoinWalletConnector.BitcoinWalletConnector {
|
|
|
27
26
|
const { accounts } = yield this.walletMethods.connect();
|
|
28
27
|
const parsedAccounts = accounts.map((account) => ({
|
|
29
28
|
address: account.address,
|
|
30
|
-
publicKey: Buffer.from(account.publicKey).toString('
|
|
29
|
+
publicKey: Buffer.from(account.publicKey).toString('hex'),
|
|
31
30
|
}));
|
|
32
31
|
[paymentAccount, ordinalsAccount] = parsedAccounts;
|
|
33
32
|
// in case it only returns one account, we will use it as both payment and ordinals
|
|
@@ -2,7 +2,6 @@ import { BitcoinTransaction, BitcoinSignPsbtRequest, BitcoinSignPsbtResponse } f
|
|
|
2
2
|
import { BitcoinWalletConnector, BitcoinWalletConnectorOpts } from '../BitcoinWalletConnector';
|
|
3
3
|
export declare class PhantomConnector extends BitcoinWalletConnector {
|
|
4
4
|
name: string;
|
|
5
|
-
canFetchConnectedAccounts: boolean;
|
|
6
5
|
constructor(opts: BitcoinWalletConnectorOpts);
|
|
7
6
|
private connectWithInstalledExtension;
|
|
8
7
|
getAddress(): Promise<string | undefined>;
|
|
@@ -13,7 +13,6 @@ class PhantomConnector extends BitcoinWalletConnector {
|
|
|
13
13
|
constructor(opts) {
|
|
14
14
|
super(Object.assign(Object.assign({}, opts), { overrideKey: 'phantombtc' }));
|
|
15
15
|
this.name = 'Phantom';
|
|
16
|
-
this.canFetchConnectedAccounts = true;
|
|
17
16
|
}
|
|
18
17
|
connectWithInstalledExtension() {
|
|
19
18
|
return __awaiter(this, void 0, void 0, function* () {
|
|
@@ -23,7 +22,7 @@ class PhantomConnector extends BitcoinWalletConnector {
|
|
|
23
22
|
const { accounts } = yield this.walletMethods.connect();
|
|
24
23
|
const parsedAccounts = accounts.map((account) => ({
|
|
25
24
|
address: account.address,
|
|
26
|
-
publicKey: Buffer.from(account.publicKey).toString('
|
|
25
|
+
publicKey: Buffer.from(account.publicKey).toString('hex'),
|
|
27
26
|
}));
|
|
28
27
|
[paymentAccount, ordinalsAccount] = parsedAccounts;
|
|
29
28
|
// in case it only returns one account, we will use it as both payment and ordinals
|
|
@@ -13,7 +13,6 @@ class UnisatConnector extends BitcoinWalletConnector.BitcoinWalletConnector {
|
|
|
13
13
|
super(Object.assign(Object.assign({}, opts), { overrideKey: 'unisat' }));
|
|
14
14
|
this.name = 'UniSat';
|
|
15
15
|
this.overrideKey = 'unisat';
|
|
16
|
-
this.canFetchConnectedAccounts = true;
|
|
17
16
|
}
|
|
18
17
|
getAddress() {
|
|
19
18
|
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
@@ -65,6 +64,15 @@ class UnisatConnector extends BitcoinWalletConnector.BitcoinWalletConnector {
|
|
|
65
64
|
return { signedPsbt: bitcoinjsLib.Psbt.fromHex(signedPsbtHex).toBase64() };
|
|
66
65
|
});
|
|
67
66
|
}
|
|
67
|
+
getConnectedAccounts() {
|
|
68
|
+
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
69
|
+
const provider = this.getProvider();
|
|
70
|
+
if (!provider) {
|
|
71
|
+
return [];
|
|
72
|
+
}
|
|
73
|
+
return provider.getAccounts();
|
|
74
|
+
});
|
|
75
|
+
}
|
|
68
76
|
}
|
|
69
77
|
|
|
70
78
|
exports.UnisatConnector = UnisatConnector;
|
|
@@ -3,10 +3,10 @@ import { BitcoinTransaction, BitcoinSignPsbtRequest, BitcoinSignPsbtResponse } f
|
|
|
3
3
|
export declare class UnisatConnector extends BitcoinWalletConnector {
|
|
4
4
|
name: string;
|
|
5
5
|
overrideKey: string;
|
|
6
|
-
canFetchConnectedAccounts: boolean;
|
|
7
6
|
constructor(opts: BitcoinWalletConnectorOpts);
|
|
8
7
|
getAddress(): Promise<string | undefined>;
|
|
9
8
|
signMessage(messageToSign: string): Promise<string | undefined>;
|
|
10
9
|
sendBitcoin(transaction: BitcoinTransaction): Promise<string | undefined>;
|
|
11
10
|
signPsbt(request: BitcoinSignPsbtRequest): Promise<BitcoinSignPsbtResponse | undefined>;
|
|
11
|
+
getConnectedAccounts(): Promise<string[]>;
|
|
12
12
|
}
|
|
@@ -9,7 +9,6 @@ class UnisatConnector extends BitcoinWalletConnector {
|
|
|
9
9
|
super(Object.assign(Object.assign({}, opts), { overrideKey: 'unisat' }));
|
|
10
10
|
this.name = 'UniSat';
|
|
11
11
|
this.overrideKey = 'unisat';
|
|
12
|
-
this.canFetchConnectedAccounts = true;
|
|
13
12
|
}
|
|
14
13
|
getAddress() {
|
|
15
14
|
return __awaiter(this, void 0, void 0, function* () {
|
|
@@ -61,6 +60,15 @@ class UnisatConnector extends BitcoinWalletConnector {
|
|
|
61
60
|
return { signedPsbt: Psbt.fromHex(signedPsbtHex).toBase64() };
|
|
62
61
|
});
|
|
63
62
|
}
|
|
63
|
+
getConnectedAccounts() {
|
|
64
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
65
|
+
const provider = this.getProvider();
|
|
66
|
+
if (!provider) {
|
|
67
|
+
return [];
|
|
68
|
+
}
|
|
69
|
+
return provider.getAccounts();
|
|
70
|
+
});
|
|
71
|
+
}
|
|
64
72
|
}
|
|
65
73
|
|
|
66
74
|
export { UnisatConnector };
|
|
@@ -11,7 +11,6 @@ class UnknownInjectedConnector extends BitcoinWalletConnector.BitcoinWalletConne
|
|
|
11
11
|
super(Object.assign(Object.assign({}, opts), { overrideKey: 'unknown' }));
|
|
12
12
|
this.name = 'Unknown';
|
|
13
13
|
this.overrideKey = 'unknown';
|
|
14
|
-
this.canFetchConnectedAccounts = false;
|
|
15
14
|
}
|
|
16
15
|
getAddress() {
|
|
17
16
|
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
@@ -3,7 +3,6 @@ import { BitcoinWalletConnector, BitcoinWalletConnectorOpts } from '../BitcoinWa
|
|
|
3
3
|
export declare class UnknownInjectedConnector extends BitcoinWalletConnector {
|
|
4
4
|
name: string;
|
|
5
5
|
overrideKey: string;
|
|
6
|
-
canFetchConnectedAccounts: boolean;
|
|
7
6
|
constructor(opts: BitcoinWalletConnectorOpts);
|
|
8
7
|
getAddress(): Promise<string | undefined>;
|
|
9
8
|
signPsbt(_request: BitcoinSignPsbtRequest): Promise<BitcoinSignPsbtResponse | undefined>;
|
|
@@ -7,7 +7,6 @@ class UnknownInjectedConnector extends BitcoinWalletConnector {
|
|
|
7
7
|
super(Object.assign(Object.assign({}, opts), { overrideKey: 'unknown' }));
|
|
8
8
|
this.name = 'Unknown';
|
|
9
9
|
this.overrideKey = 'unknown';
|
|
10
|
-
this.canFetchConnectedAccounts = false;
|
|
11
10
|
}
|
|
12
11
|
getAddress() {
|
|
13
12
|
return __awaiter(this, void 0, void 0, function* () {
|