@dynamic-labs/bitcoin 2.1.0-alpha.8 → 2.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/CHANGELOG.md +264 -0
- package/package.json +6 -6
- package/src/bitcoinProviderHelper.cjs +9 -0
- package/src/bitcoinProviderHelper.d.ts +2 -0
- package/src/bitcoinProviderHelper.js +9 -0
- package/src/{BitcoinBtcKitConnector.cjs → connectors/BitcoinBtcKitConnector/BitcoinBtcKitConnector.cjs} +13 -8
- package/src/{BitcoinBtcKitConnector.d.ts → connectors/BitcoinBtcKitConnector/BitcoinBtcKitConnector.d.ts} +2 -2
- package/src/{BitcoinBtcKitConnector.js → connectors/BitcoinBtcKitConnector/BitcoinBtcKitConnector.js} +13 -8
- package/src/connectors/BitcoinBtcKitConnector/index.d.ts +2 -0
- package/src/{BitcoinSatsConnectConnector.cjs → connectors/BitcoinSatsConnectConnector/BitcoinSatsConnectConnector.cjs} +17 -12
- package/src/{BitcoinSatsConnectConnector.d.ts → connectors/BitcoinSatsConnectConnector/BitcoinSatsConnectConnector.d.ts} +3 -2
- package/src/{BitcoinSatsConnectConnector.js → connectors/BitcoinSatsConnectConnector/BitcoinSatsConnectConnector.js} +17 -12
- package/src/connectors/BitcoinSatsConnectConnector/index.d.ts +1 -0
- package/src/{BitcoinWalletConnector.cjs → connectors/BitcoinWalletConnector.cjs} +55 -16
- package/src/{BitcoinWalletConnector.d.ts → connectors/BitcoinWalletConnector.d.ts} +5 -3
- package/src/{BitcoinWalletConnector.js → connectors/BitcoinWalletConnector.js} +55 -16
- package/src/{OkxConnector.cjs → connectors/OkxConnector/OkxConnector.cjs} +24 -5
- package/src/{OkxConnector.d.ts → connectors/OkxConnector/OkxConnector.d.ts} +3 -2
- package/src/{OkxConnector.js → connectors/OkxConnector/OkxConnector.js} +24 -5
- package/src/connectors/OkxConnector/index.d.ts +1 -0
- package/src/connectors/PhantomConnector/PhantomConnector.cjs +147 -0
- package/src/connectors/PhantomConnector/PhantomConnector.d.ts +12 -0
- package/src/connectors/PhantomConnector/PhantomConnector.js +143 -0
- package/src/connectors/PhantomConnector/index.d.ts +1 -0
- package/src/{UnisatConnector.cjs → connectors/UnisatConnector/UnisatConnector.cjs} +8 -4
- package/src/{UnisatConnector.d.ts → connectors/UnisatConnector/UnisatConnector.d.ts} +2 -2
- package/src/{UnisatConnector.js → connectors/UnisatConnector/UnisatConnector.js} +8 -4
- package/src/connectors/UnisatConnector/index.d.ts +1 -0
- package/src/connectors/UnknownInjected/UnknownInjected.cjs +37 -0
- package/src/connectors/UnknownInjected/UnknownInjected.d.ts +11 -0
- package/src/connectors/UnknownInjected/UnknownInjected.js +33 -0
- package/src/connectors/UnknownInjected/index.d.ts +1 -0
- package/src/connectors/index.d.ts +7 -0
- package/src/index.cjs +12 -3
- package/src/index.d.ts +3 -3
- package/src/index.js +12 -3
- package/src/types.d.ts +74 -2
- package/src/utils/base64.cjs +10 -0
- package/src/utils/base64.d.ts +2 -0
- package/src/utils/base64.js +5 -0
- package/src/utils/fetchBtcKitConnectors/fetchBtcKitConnectors.cjs +2 -4
- package/src/utils/fetchBtcKitConnectors/fetchBtcKitConnectors.js +2 -4
- package/src/utils/fetchSatsConnectConnectors/fetchSatsConnectConnectors.cjs +9 -1
- package/src/utils/fetchSatsConnectConnectors/fetchSatsConnectConnectors.js +9 -1
- package/src/utils/psbt/createSignPsbtOptions.cjs +8 -6
- package/src/utils/psbt/createSignPsbtOptions.js +8 -6
- package/src/utils/psbt/index.d.ts +1 -0
- package/src/utils/psbt/sighashNumberToString.cjs +27 -0
- package/src/utils/psbt/sighashNumberToString.d.ts +2 -0
- package/src/utils/psbt/sighashNumberToString.js +23 -0
- package/src/utils/satoshisToBtc/index.d.ts +1 -0
- package/src/utils/satoshisToBtc/satoshisToBtc.cjs +8 -0
- package/src/utils/satoshisToBtc/satoshisToBtc.d.ts +1 -0
- package/src/utils/satoshisToBtc/satoshisToBtc.js +4 -0
- package/src/utils/supportsSatsConnect.d.ts +1 -1
|
@@ -3,15 +3,16 @@
|
|
|
3
3
|
|
|
4
4
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
5
5
|
|
|
6
|
-
var _tslib = require('
|
|
6
|
+
var _tslib = require('../../_virtual/_tslib.cjs');
|
|
7
7
|
var walletConnectorCore = require('@dynamic-labs/wallet-connector-core');
|
|
8
8
|
var walletBook = require('@dynamic-labs/wallet-book');
|
|
9
9
|
var utils = require('@dynamic-labs/utils');
|
|
10
10
|
var sdkApiCore = require('@dynamic-labs/sdk-api-core');
|
|
11
|
-
var
|
|
12
|
-
var
|
|
13
|
-
var getMempoolApiUrl = require('
|
|
14
|
-
var _const = require('
|
|
11
|
+
var BitcoinLocalStorageCache = require('../BitcoinLocalStorageCache.cjs');
|
|
12
|
+
var bitcoinProviderHelper = require('../bitcoinProviderHelper.cjs');
|
|
13
|
+
var getMempoolApiUrl = require('../utils/getMempoolApiUrl.cjs');
|
|
14
|
+
var _const = require('../const.cjs');
|
|
15
|
+
var satoshisToBtc = require('../utils/satoshisToBtc/satoshisToBtc.cjs');
|
|
15
16
|
|
|
16
17
|
class BitcoinWalletConnector extends walletConnectorCore.WalletConnectorBase {
|
|
17
18
|
constructor(opts) {
|
|
@@ -27,6 +28,9 @@ class BitcoinWalletConnector extends walletConnectorCore.WalletConnectorBase {
|
|
|
27
28
|
this.overrideKey = (_a = opts.overrideKey) !== null && _a !== void 0 ? _a : this.key;
|
|
28
29
|
this.bitcoinProviderHelper = new bitcoinProviderHelper.BitcoinProviderHelper(opts.walletData || walletBook.getWalletBookWallet(this.walletBook, this.key));
|
|
29
30
|
this.wallet = this.bitcoinProviderHelper.findWallet();
|
|
31
|
+
if (this.wallet) {
|
|
32
|
+
this.walletMethods = this.bitcoinProviderHelper.getWalletMethods(this.wallet);
|
|
33
|
+
}
|
|
30
34
|
this.cache = new BitcoinLocalStorageCache.BitcoinLocalStorageCache(this.overrideKey);
|
|
31
35
|
}
|
|
32
36
|
canConnectWithHardwareWallet() {
|
|
@@ -76,12 +80,14 @@ class BitcoinWalletConnector extends walletConnectorCore.WalletConnectorBase {
|
|
|
76
80
|
return undefined;
|
|
77
81
|
}
|
|
78
82
|
const addressInfo = yield response.json();
|
|
79
|
-
if (!(addressInfo === null || addressInfo === void 0 ? void 0 : addressInfo.chain_stats)) {
|
|
83
|
+
if (!(addressInfo === null || addressInfo === void 0 ? void 0 : addressInfo.chain_stats) || !(addressInfo === null || addressInfo === void 0 ? void 0 : addressInfo.mempool_stats)) {
|
|
80
84
|
return undefined;
|
|
81
85
|
}
|
|
82
|
-
const
|
|
83
|
-
Number(addressInfo.chain_stats.spent_txo_sum)
|
|
84
|
-
|
|
86
|
+
const confirmedBalanceInSats = Number(addressInfo.chain_stats.funded_txo_sum) -
|
|
87
|
+
Number(addressInfo.chain_stats.spent_txo_sum);
|
|
88
|
+
const unconfirmedBalanceInSats = Number(addressInfo.mempool_stats.funded_txo_sum) -
|
|
89
|
+
Number(addressInfo.mempool_stats.spent_txo_sum);
|
|
90
|
+
const balance = satoshisToBtc.satoshisToBtc(confirmedBalanceInSats + unconfirmedBalanceInSats);
|
|
85
91
|
yield this.cache.setLastBalance(balance.toString());
|
|
86
92
|
return balance.toString();
|
|
87
93
|
});
|
|
@@ -104,14 +110,31 @@ class BitcoinWalletConnector extends walletConnectorCore.WalletConnectorBase {
|
|
|
104
110
|
}
|
|
105
111
|
getConnectedAccounts() {
|
|
106
112
|
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
113
|
+
// some wallets like xverse don't support fetching connected accounts
|
|
114
|
+
// without prompting for a connection
|
|
115
|
+
// to avoid this behavior, we cache the connected accounts
|
|
107
116
|
if (!this.canFetchConnectedAccounts) {
|
|
108
117
|
return this.getConnectedAccountsFromCache();
|
|
109
118
|
}
|
|
110
|
-
|
|
111
|
-
|
|
119
|
+
// if we decide that is ok to prompt for a connection when fetching connected accounts
|
|
120
|
+
// we shouldn't prompt every time we call this method (which is a lot of times)
|
|
121
|
+
// so we just store in a promise and return the same promise every time
|
|
122
|
+
if (!this.getAddressPromise) {
|
|
123
|
+
this.getAddressPromise = this.getAddress();
|
|
124
|
+
}
|
|
125
|
+
let connectedAccount;
|
|
126
|
+
try {
|
|
127
|
+
connectedAccount = yield this.getAddressPromise;
|
|
128
|
+
}
|
|
129
|
+
catch (error) {
|
|
130
|
+
walletConnectorCore.logger.error(`${this.key} getConnectedAccounts - error fetching connected account`);
|
|
131
|
+
//don't throw error just return empty array after clearing the promise
|
|
132
|
+
}
|
|
133
|
+
this.getAddressPromise = undefined;
|
|
134
|
+
if (!connectedAccount) {
|
|
112
135
|
return [];
|
|
113
136
|
}
|
|
114
|
-
return [
|
|
137
|
+
return [connectedAccount];
|
|
115
138
|
});
|
|
116
139
|
}
|
|
117
140
|
getAdditionalAddresses(mainAddress) {
|
|
@@ -162,8 +185,8 @@ class BitcoinWalletConnector extends walletConnectorCore.WalletConnectorBase {
|
|
|
162
185
|
var _a;
|
|
163
186
|
return (_a = this.bitcoinProviderHelper) === null || _a === void 0 ? void 0 : _a.getProvider();
|
|
164
187
|
}
|
|
165
|
-
setConnectedAccountWithAddresses(
|
|
166
|
-
return _tslib.__awaiter(this,
|
|
188
|
+
setConnectedAccountWithAddresses(_a) {
|
|
189
|
+
return _tslib.__awaiter(this, arguments, void 0, function* ({ mainAddress, ordinalsAddress, paymentAddress, active, }) {
|
|
167
190
|
if (!mainAddress) {
|
|
168
191
|
return;
|
|
169
192
|
}
|
|
@@ -194,7 +217,23 @@ class BitcoinWalletConnector extends walletConnectorCore.WalletConnectorBase {
|
|
|
194
217
|
return;
|
|
195
218
|
}
|
|
196
219
|
const { handleAccountChange, handleChainChange, handleDisconnect } = walletConnectorCore.eventListenerHandlers(this);
|
|
197
|
-
|
|
220
|
+
const handleBitcoinAccountChange = (accounts) => _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
221
|
+
let connectedAccounts = accounts;
|
|
222
|
+
// if accounts is an array of objects, we need to parse them to return only addresses
|
|
223
|
+
// since ordinals is the main address we use, we should return it as the first address
|
|
224
|
+
if (typeof accounts[0] === 'object') {
|
|
225
|
+
connectedAccounts = accounts
|
|
226
|
+
.sort((account) => (account.purpose === 'ordinals' ? -1 : 1))
|
|
227
|
+
.map((account) => account.address);
|
|
228
|
+
}
|
|
229
|
+
const currentConnectedAccounts = yield this.getConnectedAccountsFromCache();
|
|
230
|
+
// don't do anything if the connected accounts haven't changed
|
|
231
|
+
if (currentConnectedAccounts[0] === connectedAccounts[0]) {
|
|
232
|
+
return;
|
|
233
|
+
}
|
|
234
|
+
handleAccountChange(connectedAccounts);
|
|
235
|
+
});
|
|
236
|
+
provider.on('accountsChanged', handleBitcoinAccountChange);
|
|
198
237
|
provider.on('networkChanged', handleChainChange);
|
|
199
238
|
provider.on('disconnect', handleDisconnect);
|
|
200
239
|
const tearDownEventListeners = () => {
|
|
@@ -202,7 +241,7 @@ class BitcoinWalletConnector extends walletConnectorCore.WalletConnectorBase {
|
|
|
202
241
|
if (!(provider === null || provider === void 0 ? void 0 : provider.removeListener)) {
|
|
203
242
|
return;
|
|
204
243
|
}
|
|
205
|
-
provider.removeListener('accountsChanged',
|
|
244
|
+
provider.removeListener('accountsChanged', handleBitcoinAccountChange);
|
|
206
245
|
provider.removeListener('networkChanged', handleChainChange);
|
|
207
246
|
provider.removeListener('disconnect', handleDisconnect);
|
|
208
247
|
};
|
|
@@ -5,8 +5,8 @@ import type { Wallet } from '@wallet-standard/base';
|
|
|
5
5
|
import { Chain, IBitcoinWalletConnector, WalletConnectorBase } from '@dynamic-labs/wallet-connector-core';
|
|
6
6
|
import { WalletBookSchema, WalletSchema } from '@dynamic-labs/wallet-book';
|
|
7
7
|
import { JwtVerifiedCredential, WalletAdditionalAddress } from '@dynamic-labs/sdk-api-core';
|
|
8
|
-
import { IBitcoinSessionCache } from '
|
|
9
|
-
import {
|
|
8
|
+
import { IBitcoinSessionCache } from '../BitcoinLocalStorageCache';
|
|
9
|
+
import { BitcoinTransaction, BitcoinSignPsbtRequest, BitcoinSignPsbtResponse, BitcoinWalletStandardMethods, ConnectedAccountWithAddressesProps } from '../types';
|
|
10
10
|
export type BitcoinWalletConnectorOpts = {
|
|
11
11
|
walletBook: WalletBookSchema;
|
|
12
12
|
walletData: WalletSchema;
|
|
@@ -16,8 +16,10 @@ export declare abstract class BitcoinWalletConnector extends WalletConnectorBase
|
|
|
16
16
|
cache: IBitcoinSessionCache;
|
|
17
17
|
connectedChain: Chain;
|
|
18
18
|
supportedChains: Chain[];
|
|
19
|
+
private getAddressPromise;
|
|
19
20
|
private bitcoinProviderHelper;
|
|
20
21
|
wallet: Wallet | undefined;
|
|
22
|
+
walletMethods: BitcoinWalletStandardMethods | undefined;
|
|
21
23
|
canFetchConnectedAccounts: boolean;
|
|
22
24
|
isHardwareWalletEnabled: boolean;
|
|
23
25
|
verifiedCredentials: JwtVerifiedCredential[];
|
|
@@ -35,7 +37,7 @@ export declare abstract class BitcoinWalletConnector extends WalletConnectorBase
|
|
|
35
37
|
abstract sendBitcoin(transaction: BitcoinTransaction): Promise<string | undefined>;
|
|
36
38
|
getProvider<T>(): T & EventEmitter;
|
|
37
39
|
abstract signPsbt(request: BitcoinSignPsbtRequest): Promise<BitcoinSignPsbtResponse | undefined>;
|
|
38
|
-
setConnectedAccountWithAddresses(mainAddress
|
|
40
|
+
setConnectedAccountWithAddresses({ mainAddress, ordinalsAddress, paymentAddress, active, }: ConnectedAccountWithAddressesProps): Promise<void>;
|
|
39
41
|
setupEventListeners(): void;
|
|
40
42
|
setVerifiedCredentials(verifiedCredentials: JwtVerifiedCredential[]): void;
|
|
41
43
|
isLedgerAddress(address: string): boolean;
|
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
'use client'
|
|
2
|
-
import { __awaiter } from '
|
|
2
|
+
import { __awaiter } from '../../_virtual/_tslib.js';
|
|
3
3
|
import { WalletConnectorBase, eventListenerHandlers, logger } from '@dynamic-labs/wallet-connector-core';
|
|
4
4
|
import { getWalletBookWallet, findWalletBookWallet } from '@dynamic-labs/wallet-book';
|
|
5
5
|
import { isLedgerAddressViaVerifiedCredentials, DynamicError } from '@dynamic-labs/utils';
|
|
6
6
|
import { WalletAddressType } from '@dynamic-labs/sdk-api-core';
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import { getMempoolApiUrl } from '
|
|
10
|
-
import { HTTP_STATUS_TOO_MANY_REQUESTS, HTTP_STATUS_NOT_FOUND } from '
|
|
7
|
+
import { BitcoinLocalStorageCache } from '../BitcoinLocalStorageCache.js';
|
|
8
|
+
import { BitcoinProviderHelper } from '../bitcoinProviderHelper.js';
|
|
9
|
+
import { getMempoolApiUrl } from '../utils/getMempoolApiUrl.js';
|
|
10
|
+
import { HTTP_STATUS_TOO_MANY_REQUESTS, HTTP_STATUS_NOT_FOUND } from '../const.js';
|
|
11
|
+
import { satoshisToBtc } from '../utils/satoshisToBtc/satoshisToBtc.js';
|
|
11
12
|
|
|
12
13
|
class BitcoinWalletConnector extends WalletConnectorBase {
|
|
13
14
|
constructor(opts) {
|
|
@@ -23,6 +24,9 @@ class BitcoinWalletConnector extends WalletConnectorBase {
|
|
|
23
24
|
this.overrideKey = (_a = opts.overrideKey) !== null && _a !== void 0 ? _a : this.key;
|
|
24
25
|
this.bitcoinProviderHelper = new BitcoinProviderHelper(opts.walletData || getWalletBookWallet(this.walletBook, this.key));
|
|
25
26
|
this.wallet = this.bitcoinProviderHelper.findWallet();
|
|
27
|
+
if (this.wallet) {
|
|
28
|
+
this.walletMethods = this.bitcoinProviderHelper.getWalletMethods(this.wallet);
|
|
29
|
+
}
|
|
26
30
|
this.cache = new BitcoinLocalStorageCache(this.overrideKey);
|
|
27
31
|
}
|
|
28
32
|
canConnectWithHardwareWallet() {
|
|
@@ -72,12 +76,14 @@ class BitcoinWalletConnector extends WalletConnectorBase {
|
|
|
72
76
|
return undefined;
|
|
73
77
|
}
|
|
74
78
|
const addressInfo = yield response.json();
|
|
75
|
-
if (!(addressInfo === null || addressInfo === void 0 ? void 0 : addressInfo.chain_stats)) {
|
|
79
|
+
if (!(addressInfo === null || addressInfo === void 0 ? void 0 : addressInfo.chain_stats) || !(addressInfo === null || addressInfo === void 0 ? void 0 : addressInfo.mempool_stats)) {
|
|
76
80
|
return undefined;
|
|
77
81
|
}
|
|
78
|
-
const
|
|
79
|
-
Number(addressInfo.chain_stats.spent_txo_sum)
|
|
80
|
-
|
|
82
|
+
const confirmedBalanceInSats = Number(addressInfo.chain_stats.funded_txo_sum) -
|
|
83
|
+
Number(addressInfo.chain_stats.spent_txo_sum);
|
|
84
|
+
const unconfirmedBalanceInSats = Number(addressInfo.mempool_stats.funded_txo_sum) -
|
|
85
|
+
Number(addressInfo.mempool_stats.spent_txo_sum);
|
|
86
|
+
const balance = satoshisToBtc(confirmedBalanceInSats + unconfirmedBalanceInSats);
|
|
81
87
|
yield this.cache.setLastBalance(balance.toString());
|
|
82
88
|
return balance.toString();
|
|
83
89
|
});
|
|
@@ -100,14 +106,31 @@ class BitcoinWalletConnector extends WalletConnectorBase {
|
|
|
100
106
|
}
|
|
101
107
|
getConnectedAccounts() {
|
|
102
108
|
return __awaiter(this, void 0, void 0, function* () {
|
|
109
|
+
// some wallets like xverse don't support fetching connected accounts
|
|
110
|
+
// without prompting for a connection
|
|
111
|
+
// to avoid this behavior, we cache the connected accounts
|
|
103
112
|
if (!this.canFetchConnectedAccounts) {
|
|
104
113
|
return this.getConnectedAccountsFromCache();
|
|
105
114
|
}
|
|
106
|
-
|
|
107
|
-
|
|
115
|
+
// if we decide that is ok to prompt for a connection when fetching connected accounts
|
|
116
|
+
// we shouldn't prompt every time we call this method (which is a lot of times)
|
|
117
|
+
// so we just store in a promise and return the same promise every time
|
|
118
|
+
if (!this.getAddressPromise) {
|
|
119
|
+
this.getAddressPromise = this.getAddress();
|
|
120
|
+
}
|
|
121
|
+
let connectedAccount;
|
|
122
|
+
try {
|
|
123
|
+
connectedAccount = yield this.getAddressPromise;
|
|
124
|
+
}
|
|
125
|
+
catch (error) {
|
|
126
|
+
logger.error(`${this.key} getConnectedAccounts - error fetching connected account`);
|
|
127
|
+
//don't throw error just return empty array after clearing the promise
|
|
128
|
+
}
|
|
129
|
+
this.getAddressPromise = undefined;
|
|
130
|
+
if (!connectedAccount) {
|
|
108
131
|
return [];
|
|
109
132
|
}
|
|
110
|
-
return [
|
|
133
|
+
return [connectedAccount];
|
|
111
134
|
});
|
|
112
135
|
}
|
|
113
136
|
getAdditionalAddresses(mainAddress) {
|
|
@@ -158,8 +181,8 @@ class BitcoinWalletConnector extends WalletConnectorBase {
|
|
|
158
181
|
var _a;
|
|
159
182
|
return (_a = this.bitcoinProviderHelper) === null || _a === void 0 ? void 0 : _a.getProvider();
|
|
160
183
|
}
|
|
161
|
-
setConnectedAccountWithAddresses(
|
|
162
|
-
return __awaiter(this,
|
|
184
|
+
setConnectedAccountWithAddresses(_a) {
|
|
185
|
+
return __awaiter(this, arguments, void 0, function* ({ mainAddress, ordinalsAddress, paymentAddress, active, }) {
|
|
163
186
|
if (!mainAddress) {
|
|
164
187
|
return;
|
|
165
188
|
}
|
|
@@ -190,7 +213,23 @@ class BitcoinWalletConnector extends WalletConnectorBase {
|
|
|
190
213
|
return;
|
|
191
214
|
}
|
|
192
215
|
const { handleAccountChange, handleChainChange, handleDisconnect } = eventListenerHandlers(this);
|
|
193
|
-
|
|
216
|
+
const handleBitcoinAccountChange = (accounts) => __awaiter(this, void 0, void 0, function* () {
|
|
217
|
+
let connectedAccounts = accounts;
|
|
218
|
+
// if accounts is an array of objects, we need to parse them to return only addresses
|
|
219
|
+
// since ordinals is the main address we use, we should return it as the first address
|
|
220
|
+
if (typeof accounts[0] === 'object') {
|
|
221
|
+
connectedAccounts = accounts
|
|
222
|
+
.sort((account) => (account.purpose === 'ordinals' ? -1 : 1))
|
|
223
|
+
.map((account) => account.address);
|
|
224
|
+
}
|
|
225
|
+
const currentConnectedAccounts = yield this.getConnectedAccountsFromCache();
|
|
226
|
+
// don't do anything if the connected accounts haven't changed
|
|
227
|
+
if (currentConnectedAccounts[0] === connectedAccounts[0]) {
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
230
|
+
handleAccountChange(connectedAccounts);
|
|
231
|
+
});
|
|
232
|
+
provider.on('accountsChanged', handleBitcoinAccountChange);
|
|
194
233
|
provider.on('networkChanged', handleChainChange);
|
|
195
234
|
provider.on('disconnect', handleDisconnect);
|
|
196
235
|
const tearDownEventListeners = () => {
|
|
@@ -198,7 +237,7 @@ class BitcoinWalletConnector extends WalletConnectorBase {
|
|
|
198
237
|
if (!(provider === null || provider === void 0 ? void 0 : provider.removeListener)) {
|
|
199
238
|
return;
|
|
200
239
|
}
|
|
201
|
-
provider.removeListener('accountsChanged',
|
|
240
|
+
provider.removeListener('accountsChanged', handleBitcoinAccountChange);
|
|
202
241
|
provider.removeListener('networkChanged', handleChainChange);
|
|
203
242
|
provider.removeListener('disconnect', handleDisconnect);
|
|
204
243
|
};
|
|
@@ -3,19 +3,34 @@
|
|
|
3
3
|
|
|
4
4
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
5
5
|
|
|
6
|
-
var _tslib = require('
|
|
6
|
+
var _tslib = require('../../../_virtual/_tslib.cjs');
|
|
7
7
|
var bitcoinjsLib = require('bitcoinjs-lib');
|
|
8
|
-
var
|
|
9
|
-
var
|
|
8
|
+
var utils = require('@dynamic-labs/utils');
|
|
9
|
+
var BitcoinWalletConnector = require('../BitcoinWalletConnector.cjs');
|
|
10
|
+
var createSignPsbtOptions = require('../../utils/psbt/createSignPsbtOptions.cjs');
|
|
10
11
|
|
|
11
12
|
class OkxConnector extends BitcoinWalletConnector.BitcoinWalletConnector {
|
|
12
13
|
constructor(opts) {
|
|
13
14
|
super(Object.assign(Object.assign({}, opts), { overrideKey: 'okxwalletbtc' }));
|
|
14
|
-
this.name = 'OKX Wallet
|
|
15
|
+
this.name = 'OKX Wallet';
|
|
15
16
|
this.canFetchConnectedAccounts = true;
|
|
16
17
|
}
|
|
18
|
+
get walletBookWallet() {
|
|
19
|
+
return this.walletBook.wallets[this.key];
|
|
20
|
+
}
|
|
17
21
|
getAddress() {
|
|
18
22
|
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
23
|
+
if (!this.isInstalledOnBrowser() &&
|
|
24
|
+
utils.isMobile() &&
|
|
25
|
+
this.walletBookWallet.mobile &&
|
|
26
|
+
this.walletBookWallet.mobile.inAppBrowser) {
|
|
27
|
+
const inAppBrowserCompiledTemplate = utils.template(this.walletBookWallet.mobile.inAppBrowser);
|
|
28
|
+
const deepLink = inAppBrowserCompiledTemplate({
|
|
29
|
+
encodedDappURI: encodeURIComponent(window.location.toString()),
|
|
30
|
+
});
|
|
31
|
+
window.location.assign(deepLink);
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
19
34
|
const provider = this.getProvider();
|
|
20
35
|
if (!provider) {
|
|
21
36
|
return;
|
|
@@ -25,7 +40,11 @@ class OkxConnector extends BitcoinWalletConnector.BitcoinWalletConnector {
|
|
|
25
40
|
address,
|
|
26
41
|
publicKey,
|
|
27
42
|
};
|
|
28
|
-
yield this.setConnectedAccountWithAddresses(
|
|
43
|
+
yield this.setConnectedAccountWithAddresses({
|
|
44
|
+
active: true,
|
|
45
|
+
mainAddress: address,
|
|
46
|
+
ordinalsAddress: bitcoinAddress,
|
|
47
|
+
});
|
|
29
48
|
return address;
|
|
30
49
|
});
|
|
31
50
|
}
|
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import { BitcoinWalletConnector, BitcoinWalletConnectorOpts } from '
|
|
2
|
-
import { BitcoinTransaction, BitcoinSignPsbtRequest, BitcoinSignPsbtResponse } from '
|
|
1
|
+
import { BitcoinWalletConnector, BitcoinWalletConnectorOpts } from '../BitcoinWalletConnector';
|
|
2
|
+
import { BitcoinTransaction, BitcoinSignPsbtRequest, BitcoinSignPsbtResponse } from '../../types';
|
|
3
3
|
export declare class OkxConnector extends BitcoinWalletConnector {
|
|
4
4
|
name: string;
|
|
5
5
|
canFetchConnectedAccounts: boolean;
|
|
6
6
|
constructor(opts: BitcoinWalletConnectorOpts);
|
|
7
|
+
private get walletBookWallet();
|
|
7
8
|
getAddress(): Promise<string | undefined>;
|
|
8
9
|
signMessage(messageToSign: string): Promise<string | undefined>;
|
|
9
10
|
sendBitcoin(transaction: BitcoinTransaction): Promise<string | undefined>;
|
|
@@ -1,17 +1,32 @@
|
|
|
1
1
|
'use client'
|
|
2
|
-
import { __awaiter } from '
|
|
2
|
+
import { __awaiter } from '../../../_virtual/_tslib.js';
|
|
3
3
|
import { Psbt } from 'bitcoinjs-lib';
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
4
|
+
import { isMobile, template } from '@dynamic-labs/utils';
|
|
5
|
+
import { BitcoinWalletConnector } from '../BitcoinWalletConnector.js';
|
|
6
|
+
import { createPsbtOptions } from '../../utils/psbt/createSignPsbtOptions.js';
|
|
6
7
|
|
|
7
8
|
class OkxConnector extends BitcoinWalletConnector {
|
|
8
9
|
constructor(opts) {
|
|
9
10
|
super(Object.assign(Object.assign({}, opts), { overrideKey: 'okxwalletbtc' }));
|
|
10
|
-
this.name = 'OKX Wallet
|
|
11
|
+
this.name = 'OKX Wallet';
|
|
11
12
|
this.canFetchConnectedAccounts = true;
|
|
12
13
|
}
|
|
14
|
+
get walletBookWallet() {
|
|
15
|
+
return this.walletBook.wallets[this.key];
|
|
16
|
+
}
|
|
13
17
|
getAddress() {
|
|
14
18
|
return __awaiter(this, void 0, void 0, function* () {
|
|
19
|
+
if (!this.isInstalledOnBrowser() &&
|
|
20
|
+
isMobile() &&
|
|
21
|
+
this.walletBookWallet.mobile &&
|
|
22
|
+
this.walletBookWallet.mobile.inAppBrowser) {
|
|
23
|
+
const inAppBrowserCompiledTemplate = template(this.walletBookWallet.mobile.inAppBrowser);
|
|
24
|
+
const deepLink = inAppBrowserCompiledTemplate({
|
|
25
|
+
encodedDappURI: encodeURIComponent(window.location.toString()),
|
|
26
|
+
});
|
|
27
|
+
window.location.assign(deepLink);
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
15
30
|
const provider = this.getProvider();
|
|
16
31
|
if (!provider) {
|
|
17
32
|
return;
|
|
@@ -21,7 +36,11 @@ class OkxConnector extends BitcoinWalletConnector {
|
|
|
21
36
|
address,
|
|
22
37
|
publicKey,
|
|
23
38
|
};
|
|
24
|
-
yield this.setConnectedAccountWithAddresses(
|
|
39
|
+
yield this.setConnectedAccountWithAddresses({
|
|
40
|
+
active: true,
|
|
41
|
+
mainAddress: address,
|
|
42
|
+
ordinalsAddress: bitcoinAddress,
|
|
43
|
+
});
|
|
25
44
|
return address;
|
|
26
45
|
});
|
|
27
46
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { OkxConnector } from './OkxConnector';
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
5
|
+
|
|
6
|
+
var _tslib = require('../../../_virtual/_tslib.cjs');
|
|
7
|
+
var bitcoinjsLib = require('bitcoinjs-lib');
|
|
8
|
+
var walletConnectorCore = require('@dynamic-labs/wallet-connector-core');
|
|
9
|
+
var utils = require('@dynamic-labs/utils');
|
|
10
|
+
var BitcoinWalletConnector = require('../BitcoinWalletConnector.cjs');
|
|
11
|
+
var createSignPsbtOptions = require('../../utils/psbt/createSignPsbtOptions.cjs');
|
|
12
|
+
require('sats-connect');
|
|
13
|
+
var sighashNumberToString = require('../../utils/psbt/sighashNumberToString.cjs');
|
|
14
|
+
var base64 = require('../../utils/base64.cjs');
|
|
15
|
+
|
|
16
|
+
class PhantomConnector extends BitcoinWalletConnector.BitcoinWalletConnector {
|
|
17
|
+
constructor(opts) {
|
|
18
|
+
super(Object.assign(Object.assign({}, opts), { overrideKey: 'phantombtc' }));
|
|
19
|
+
this.name = 'Phantom';
|
|
20
|
+
this.canFetchConnectedAccounts = true;
|
|
21
|
+
}
|
|
22
|
+
connectWithInstalledExtension() {
|
|
23
|
+
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
24
|
+
var _a, _b;
|
|
25
|
+
let paymentAccount, ordinalsAccount;
|
|
26
|
+
if ((_a = this.walletMethods) === null || _a === void 0 ? void 0 : _a.connect) {
|
|
27
|
+
const { accounts } = yield this.walletMethods.connect();
|
|
28
|
+
const parsedAccounts = accounts.map((account) => ({
|
|
29
|
+
address: account.address,
|
|
30
|
+
publicKey: Buffer.from(account.publicKey).toString('base64'),
|
|
31
|
+
}));
|
|
32
|
+
[paymentAccount, ordinalsAccount] = parsedAccounts;
|
|
33
|
+
// in case it only returns one account, we will use it as both payment and ordinals
|
|
34
|
+
if (!ordinalsAccount) {
|
|
35
|
+
ordinalsAccount = paymentAccount;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
const provider = this.getProvider();
|
|
40
|
+
if (!provider) {
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
const accounts = yield provider.requestAccounts();
|
|
44
|
+
ordinalsAccount = accounts === null || accounts === void 0 ? void 0 : accounts.find((address) => address.purpose === 'ordinals');
|
|
45
|
+
paymentAccount = accounts === null || accounts === void 0 ? void 0 : accounts.find((address) => address.purpose === 'payment');
|
|
46
|
+
}
|
|
47
|
+
const mainAddress = (_b = ordinalsAccount === null || ordinalsAccount === void 0 ? void 0 : ordinalsAccount.address) !== null && _b !== void 0 ? _b : paymentAccount === null || paymentAccount === void 0 ? void 0 : paymentAccount.address;
|
|
48
|
+
yield this.setConnectedAccountWithAddresses({
|
|
49
|
+
active: true,
|
|
50
|
+
mainAddress,
|
|
51
|
+
ordinalsAddress: ordinalsAccount,
|
|
52
|
+
paymentAddress: paymentAccount,
|
|
53
|
+
});
|
|
54
|
+
return mainAddress;
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
getAddress() {
|
|
58
|
+
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
59
|
+
if (this.isInstalledOnBrowser()) {
|
|
60
|
+
return this.connectWithInstalledExtension();
|
|
61
|
+
}
|
|
62
|
+
if (utils.isMobile()) {
|
|
63
|
+
utils.handleMobileWalletRedirect({
|
|
64
|
+
nativeLink: 'phantom://browse',
|
|
65
|
+
universalLink: 'https://phantom.app/ul/browse',
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
return undefined;
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
signMessage(messageToSign) {
|
|
72
|
+
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
73
|
+
var _a;
|
|
74
|
+
const [walletAddress] = yield this.getConnectedAccounts();
|
|
75
|
+
if (!walletAddress) {
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
if ((_a = this.walletMethods) === null || _a === void 0 ? void 0 : _a.signMessage) {
|
|
79
|
+
const [result] = yield this.walletMethods.signMessage({
|
|
80
|
+
// we need to sign with the ordinals account
|
|
81
|
+
account: { address: walletAddress },
|
|
82
|
+
message: new TextEncoder().encode(messageToSign),
|
|
83
|
+
});
|
|
84
|
+
return Buffer.from(result.signature).toString('base64');
|
|
85
|
+
}
|
|
86
|
+
else {
|
|
87
|
+
const provider = this.getProvider();
|
|
88
|
+
if (!provider) {
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
const result = yield provider.signMessage(walletAddress, new TextEncoder().encode(messageToSign));
|
|
92
|
+
return Buffer.from(result.signature).toString('base64');
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
sendBitcoin(transaction) {
|
|
97
|
+
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
98
|
+
walletConnectorCore.logger.debug('sendBitcoin - function not implemented', transaction);
|
|
99
|
+
return undefined;
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
signPsbt(request) {
|
|
103
|
+
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
104
|
+
var _a, _b, _c, _d, _e;
|
|
105
|
+
const [walletAddress] = yield this.getConnectedAccounts();
|
|
106
|
+
if (!walletAddress) {
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
const psbtFromBase64 = bitcoinjsLib.Psbt.fromBase64(request.unsignedPsbtBase64);
|
|
110
|
+
const psbtOptions = createSignPsbtOptions.createPsbtOptions(psbtFromBase64, request);
|
|
111
|
+
if ((_a = this.walletMethods) === null || _a === void 0 ? void 0 : _a.signTransaction) {
|
|
112
|
+
const [signedPsbtHex] = yield this.walletMethods.signTransaction({
|
|
113
|
+
inputsToSign: (_c = (_b = psbtOptions.toSignInputs) === null || _b === void 0 ? void 0 : _b.map((input) => ({
|
|
114
|
+
account: { address: input.address },
|
|
115
|
+
sigHash: input.sighashTypes &&
|
|
116
|
+
sighashNumberToString.sighashNumberToString(input.sighashTypes[0]),
|
|
117
|
+
signingIndexes: [input.index],
|
|
118
|
+
}))) !== null && _c !== void 0 ? _c : [],
|
|
119
|
+
psbt: base64.decodeBase64(request.unsignedPsbtBase64),
|
|
120
|
+
});
|
|
121
|
+
return {
|
|
122
|
+
signedPsbt: Buffer.from(signedPsbtHex.signedPsbt).toString('base64'),
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
else {
|
|
126
|
+
const provider = this.getProvider();
|
|
127
|
+
if (!provider) {
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
const inputsToSign = (_e = (_d = request.signature) === null || _d === void 0 ? void 0 : _d.map((sig) => {
|
|
131
|
+
var _a;
|
|
132
|
+
return ({
|
|
133
|
+
address: sig.address,
|
|
134
|
+
sigHash: request.allowedSighash[0],
|
|
135
|
+
signingIndexes: (_a = sig.signingIndexes) !== null && _a !== void 0 ? _a : [],
|
|
136
|
+
});
|
|
137
|
+
})) !== null && _e !== void 0 ? _e : [];
|
|
138
|
+
const signedPsbt = yield provider.signPSBT(base64.decodeBase64(request.unsignedPsbtBase64), {
|
|
139
|
+
inputsToSign,
|
|
140
|
+
});
|
|
141
|
+
return { signedPsbt: base64.encodeBase64(Uint8Array.from(signedPsbt)) };
|
|
142
|
+
}
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
exports.PhantomConnector = PhantomConnector;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { BitcoinTransaction, BitcoinSignPsbtRequest, BitcoinSignPsbtResponse } from '../../types';
|
|
2
|
+
import { BitcoinWalletConnector, BitcoinWalletConnectorOpts } from '../BitcoinWalletConnector';
|
|
3
|
+
export declare class PhantomConnector extends BitcoinWalletConnector {
|
|
4
|
+
name: string;
|
|
5
|
+
canFetchConnectedAccounts: boolean;
|
|
6
|
+
constructor(opts: BitcoinWalletConnectorOpts);
|
|
7
|
+
private connectWithInstalledExtension;
|
|
8
|
+
getAddress(): Promise<string | undefined>;
|
|
9
|
+
signMessage(messageToSign: string): Promise<string | undefined>;
|
|
10
|
+
sendBitcoin(transaction: BitcoinTransaction): Promise<string | undefined>;
|
|
11
|
+
signPsbt(request: BitcoinSignPsbtRequest): Promise<BitcoinSignPsbtResponse | undefined>;
|
|
12
|
+
}
|