@dynamic-labs/bitcoin 3.0.0-alpha.9 → 3.0.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.
Files changed (52) hide show
  1. package/CHANGELOG.md +620 -0
  2. package/package.json +7 -6
  3. package/src/BitcoinLocalStorageCache.cjs +0 -16
  4. package/src/BitcoinLocalStorageCache.d.ts +0 -7
  5. package/src/BitcoinLocalStorageCache.js +0 -16
  6. package/src/bitcoinProviderHelper.d.ts +1 -0
  7. package/src/connectors/BitcoinSatsConnectConnector/BitcoinSatsConnectConnector.cjs +170 -141
  8. package/src/connectors/BitcoinSatsConnectConnector/BitcoinSatsConnectConnector.d.ts +8 -3
  9. package/src/connectors/BitcoinSatsConnectConnector/BitcoinSatsConnectConnector.js +171 -142
  10. package/src/connectors/BitcoinSatsConnectLegacyConnector/BitcoinSatsConnectLegacyConnector.cjs +216 -0
  11. package/src/connectors/BitcoinSatsConnectLegacyConnector/BitcoinSatsConnectLegacyConnector.d.ts +11 -0
  12. package/src/connectors/BitcoinSatsConnectLegacyConnector/BitcoinSatsConnectLegacyConnector.js +212 -0
  13. package/src/connectors/BitcoinSatsConnectLegacyConnector/index.d.ts +1 -0
  14. package/src/connectors/BitcoinWalletConnector.cjs +44 -29
  15. package/src/connectors/BitcoinWalletConnector.d.ts +7 -3
  16. package/src/connectors/BitcoinWalletConnector.js +45 -30
  17. package/src/connectors/FallbackBitcoinConnector/FallbackBitcoinConnector.cjs +7 -1
  18. package/src/connectors/FallbackBitcoinConnector/FallbackBitcoinConnector.d.ts +1 -1
  19. package/src/connectors/FallbackBitcoinConnector/FallbackBitcoinConnector.js +7 -1
  20. package/src/connectors/OkxConnector/OkxConnector.cjs +19 -2
  21. package/src/connectors/OkxConnector/OkxConnector.d.ts +1 -1
  22. package/src/connectors/OkxConnector/OkxConnector.js +19 -2
  23. package/src/connectors/PhantomConnector/PhantomConnector.cjs +4 -9
  24. package/src/connectors/PhantomConnector/PhantomConnector.d.ts +1 -2
  25. package/src/connectors/PhantomConnector/PhantomConnector.js +4 -9
  26. package/src/connectors/UnisatConnector/UnisatConnector.cjs +17 -1
  27. package/src/connectors/UnisatConnector/UnisatConnector.d.ts +1 -1
  28. package/src/connectors/UnisatConnector/UnisatConnector.js +17 -1
  29. package/src/connectors/UnknownInjected/UnknownInjected.cjs +7 -1
  30. package/src/connectors/UnknownInjected/UnknownInjected.d.ts +1 -1
  31. package/src/connectors/UnknownInjected/UnknownInjected.js +7 -1
  32. package/src/connectors/index.d.ts +1 -0
  33. package/src/index.cjs +4 -0
  34. package/src/index.d.ts +3 -2
  35. package/src/index.js +2 -0
  36. package/src/types.d.ts +1 -0
  37. package/src/utils/fetchSatsConnectConnectors/fetchSatsConnectConnectors.cjs +15 -3
  38. package/src/utils/fetchSatsConnectConnectors/fetchSatsConnectConnectors.js +15 -3
  39. package/src/utils/psbt/bitcoinNetworkTypeToNetworks.cjs +1 -0
  40. package/src/utils/psbt/bitcoinNetworkTypeToNetworks.d.ts +1 -1
  41. package/src/utils/psbt/bitcoinNetworkTypeToNetworks.js +1 -0
  42. package/src/utils/psbt/createSignPsbtOptions.cjs +22 -1
  43. package/src/utils/psbt/createSignPsbtOptions.d.ts +2 -0
  44. package/src/utils/psbt/createSignPsbtOptions.js +22 -2
  45. package/src/wallet/BitcoinWallet.cjs +69 -0
  46. package/src/wallet/BitcoinWallet.d.ts +36 -0
  47. package/src/wallet/BitcoinWallet.js +65 -0
  48. package/src/wallet/index.d.ts +2 -0
  49. package/src/wallet/isBitcoinWallet/index.d.ts +1 -0
  50. package/src/wallet/isBitcoinWallet/isBitcoinWallet.cjs +8 -0
  51. package/src/wallet/isBitcoinWallet/isBitcoinWallet.d.ts +3 -0
  52. package/src/wallet/isBitcoinWallet/isBitcoinWallet.js +4 -0
@@ -1,39 +1,57 @@
1
1
  'use client'
2
2
  import { __awaiter } from '../../../_virtual/_tslib.js';
3
3
  import { Psbt } from 'bitcoinjs-lib';
4
- import { BitcoinNetworkType, getAddress, AddressPurpose, signMessage, sendBtcTransaction, signTransaction } from 'sats-connect';
4
+ import { BitcoinNetworkType, request, AddressPurpose, signMultipleTransactions } from 'sats-connect';
5
5
  import { isMobile, template } from '@dynamic-labs/utils';
6
6
  import { findWalletBookWallet } from '@dynamic-labs/wallet-book';
7
- import { SATSCONNECT_FEATURE } from '../../const.js';
8
- import '@dynamic-labs/wallet-connector-core';
9
- import { BitcoinWalletConnector } from '../BitcoinWalletConnector.js';
10
- import { validatePsbt } from '../../utils/psbt/validator/validatePsbt.js';
11
- import { getSigHashType } from '../../utils/psbt/getSigHashType.js';
7
+ import { logger } from '@dynamic-labs/wallet-connector-core';
12
8
  import { convertNetworkTypeForPsbt } from '../../utils/psbt/bitcoinNetworkTypeToNetworks.js';
13
- import { supportsSatsConnect } from '../../utils/supportsSatsConnect.js';
9
+ import { BitcoinWalletConnector } from '../BitcoinWalletConnector.js';
10
+ import { createSignPsbtOptionsForSatsConnect } from '../../utils/psbt/createSignPsbtOptions.js';
11
+ import { SATSCONNECT_FEATURE } from '../../const.js';
14
12
 
15
13
  class BitcoinSatsConnectConnector extends BitcoinWalletConnector {
16
14
  constructor(opts) {
17
15
  super(opts);
16
+ this.isLegacy = false;
18
17
  this.currentNetwork = BitcoinNetworkType.Mainnet;
19
- // satsconnect wallets don't support fetching connected accounts without prompting
20
- // for a connection, so we handle getConnectedAccounts differently.
21
- this.canFetchConnectedAccounts = false;
22
18
  }
23
- getAddress() {
19
+ getGenericUserCancelledError() {
20
+ const error = new Error();
21
+ error.code = '-32000'; // error code for user cancelled
22
+ return error;
23
+ }
24
+ getAddresses() {
24
25
  return __awaiter(this, void 0, void 0, function* () {
25
26
  var _a;
26
- // xverse doesn't support wallet standard, so we won't have a wallet object,
27
- // but it's already the default provider for sats-connect, so it's ok
28
- // for getProvider in getAddress to return undefined
29
- // if we're not using xverse, we need to check if there is a wallet and
30
- // that it has the satsconnect feature to return the correct provider to use
31
- if (!supportsSatsConnect(this)) {
32
- return;
27
+ const getAddressResponse = yield request('getAddresses', {
28
+ purposes: [AddressPurpose.Payment, AddressPurpose.Ordinals],
29
+ });
30
+ if (getAddressResponse.status !== 'success') {
31
+ throw getAddressResponse.error;
33
32
  }
33
+ const { addresses } = getAddressResponse.result;
34
+ const ordinalsAccount = addresses === null || addresses === void 0 ? void 0 : addresses.find((address) => address.purpose === AddressPurpose.Ordinals);
35
+ const paymentAccount = addresses === null || addresses === void 0 ? void 0 : addresses.find((address) => address.purpose === AddressPurpose.Payment);
36
+ const mainAddress = (_a = ordinalsAccount === null || ordinalsAccount === void 0 ? void 0 : ordinalsAccount.address) !== null && _a !== void 0 ? _a : paymentAccount === null || paymentAccount === void 0 ? void 0 : paymentAccount.address;
37
+ yield this.setConnectedAccountWithAddresses({
38
+ active: true,
39
+ mainAddress,
40
+ ordinalsAddress: ordinalsAccount,
41
+ paymentAddress: paymentAccount,
42
+ });
43
+ return mainAddress;
44
+ });
45
+ }
46
+ getAddress() {
47
+ return __awaiter(this, void 0, void 0, function* () {
48
+ var _a;
34
49
  const wallet = findWalletBookWallet(this.walletBook, this.key);
35
50
  const inAppBrowserUrl = (_a = wallet === null || wallet === void 0 ? void 0 : wallet.mobile) === null || _a === void 0 ? void 0 : _a.inAppBrowser;
36
- if (isMobile() && !this.isInstalledOnBrowser() && inAppBrowserUrl) {
51
+ if (isMobile() &&
52
+ !this.isInstalledOnBrowser() &&
53
+ inAppBrowserUrl &&
54
+ this.mobileExperience === 'in-app-browser') {
37
55
  const inAppBrowserTemplate = template(inAppBrowserUrl);
38
56
  const deepLink = inAppBrowserTemplate({
39
57
  encodedDappURI: encodeURIComponent(window.location.toString()),
@@ -41,143 +59,123 @@ class BitcoinSatsConnectConnector extends BitcoinWalletConnector {
41
59
  window.location.href = deepLink;
42
60
  return;
43
61
  }
44
- return new Promise((resolve, reject) => {
45
- getAddress({
46
- getProvider: () => __awaiter(this, void 0, void 0, function* () {
47
- var _a, _b;
48
- return (_b = (_a = this.wallet) === null || _a === void 0 ? void 0 : _a.features[SATSCONNECT_FEATURE]) === null || _b === void 0 ? void 0 : _b.provider;
49
- }),
50
- onCancel: () => {
51
- const error = new Error();
52
- error.code = '-32000'; // error code for user cancelled
53
- reject(error);
54
- },
55
- onFinish: (response) => __awaiter(this, void 0, void 0, function* () {
56
- var _c;
57
- const { addresses } = response;
58
- const ordinalsAccount = addresses === null || addresses === void 0 ? void 0 : addresses.find((address) => address.purpose === AddressPurpose.Ordinals);
59
- const paymentAccount = addresses === null || addresses === void 0 ? void 0 : addresses.find((address) => address.purpose === AddressPurpose.Payment);
60
- const mainAddress = (_c = ordinalsAccount === null || ordinalsAccount === void 0 ? void 0 : ordinalsAccount.address) !== null && _c !== void 0 ? _c : paymentAccount === null || paymentAccount === void 0 ? void 0 : paymentAccount.address;
61
- yield this.setConnectedAccountWithAddresses({
62
- active: true,
63
- mainAddress,
64
- ordinalsAddress: ordinalsAccount,
65
- paymentAddress: paymentAccount,
66
- });
67
- resolve(mainAddress);
68
- }),
69
- payload: {
70
- message: 'Address for receiving Ordinals and payments',
71
- network: {
72
- type: this.currentNetwork,
73
- },
74
- purposes: [AddressPurpose.Ordinals, AddressPurpose.Payment],
75
- },
76
- });
77
- });
62
+ try {
63
+ // requesting permissions on initial connect will avoid connection prompt for other requests
64
+ const permissionResponse = yield request('wallet_requestPermissions', undefined);
65
+ if (permissionResponse.status !== 'success') {
66
+ throw permissionResponse.error;
67
+ }
68
+ const address = yield this.getAddresses();
69
+ return address;
70
+ }
71
+ catch (_) {
72
+ const error = this.getGenericUserCancelledError();
73
+ throw error;
74
+ }
78
75
  });
79
76
  }
80
- signMessage(messageToSign) {
77
+ signMessage(messageToSign, withAddress) {
81
78
  return __awaiter(this, void 0, void 0, function* () {
82
- const [walletAddress] = yield this.getConnectedAccounts();
83
- if (!walletAddress || !supportsSatsConnect(this)) {
84
- return;
85
- }
86
- return new Promise((resolve, reject) => {
87
- signMessage({
88
- getProvider: () => __awaiter(this, void 0, void 0, function* () {
89
- var _a, _b;
90
- return (_b = (_a = this.wallet) === null || _a === void 0 ? void 0 : _a.features[SATSCONNECT_FEATURE]) === null || _b === void 0 ? void 0 : _b.provider;
91
- }),
92
- onCancel: () => {
93
- const error = new Error();
94
- error.code = '-32000'; // error code for user cancelled
95
- reject(error);
96
- },
97
- onFinish: (response) => __awaiter(this, void 0, void 0, function* () {
98
- if (this.isHardwareWalletEnabled) {
99
- return resolve(JSON.stringify({
100
- signedTransaction: {
101
- data: response,
102
- },
103
- }));
104
- }
105
- resolve(response);
106
- }),
107
- payload: {
108
- address: walletAddress,
109
- message: messageToSign,
110
- network: {
111
- type: this.currentNetwork,
112
- },
113
- },
79
+ try {
80
+ const response = yield request('signMessage', {
81
+ address: withAddress,
82
+ message: messageToSign,
114
83
  });
115
- });
84
+ if (response.status !== 'success') {
85
+ throw response.error;
86
+ }
87
+ if (this.isHardwareWalletEnabled) {
88
+ return JSON.stringify({
89
+ signedTransaction: {
90
+ data: response.result.signature,
91
+ },
92
+ });
93
+ }
94
+ return response.result.signature;
95
+ }
96
+ catch (_) {
97
+ const error = this.getGenericUserCancelledError();
98
+ throw error;
99
+ }
116
100
  });
117
101
  }
118
102
  sendBitcoin(transaction) {
119
103
  return __awaiter(this, void 0, void 0, function* () {
120
- var _a;
121
- const mainAddress = yield this.getAddress();
122
- const senderAddress = (_a = (yield this.getAdditionalAddresses(mainAddress)).find((address) => address.type === 'payment')) === null || _a === void 0 ? void 0 : _a.address;
123
- if (!senderAddress || !supportsSatsConnect(this)) {
124
- return;
125
- }
126
- return new Promise((resolve, reject) => {
127
- sendBtcTransaction({
128
- getProvider: () => __awaiter(this, void 0, void 0, function* () {
129
- var _a, _b;
130
- return (_b = (_a = this.wallet) === null || _a === void 0 ? void 0 : _a.features[SATSCONNECT_FEATURE]) === null || _b === void 0 ? void 0 : _b.provider;
131
- }),
132
- onCancel: () => {
133
- const error = new Error();
134
- error.code = '-32000'; // error code for user cancelled
135
- reject(error);
136
- },
137
- onFinish: (response) => {
138
- resolve(response);
139
- },
140
- payload: {
141
- network: {
142
- type: this.currentNetwork,
104
+ try {
105
+ const response = yield request('sendTransfer', {
106
+ recipients: [
107
+ {
108
+ address: transaction.recipientAddress,
109
+ amount: Number(transaction.amount),
143
110
  },
144
- recipients: [
145
- {
146
- address: transaction.recipientAddress,
147
- amountSats: transaction.amount,
148
- },
149
- ],
150
- senderAddress,
151
- },
111
+ ],
152
112
  });
153
- });
113
+ if (response.status !== 'success') {
114
+ throw response.error;
115
+ }
116
+ return response.result.txid;
117
+ }
118
+ catch (_) {
119
+ const error = this.getGenericUserCancelledError();
120
+ throw error;
121
+ }
154
122
  });
155
123
  }
156
124
  signTransaction(params) {
157
125
  return __awaiter(this, void 0, void 0, function* () {
158
- const { message, psbtBase64, broadcast, inputsToSign } = params;
126
+ const { allowedSignHash, psbtBase64, broadcast, inputsToSign } = params;
127
+ const signPbstPayload = {
128
+ allowedSignHash,
129
+ broadcast,
130
+ psbt: psbtBase64,
131
+ // we have to combine signing indexes for like addresses
132
+ signInputs: inputsToSign.reduce((accum, curr) => {
133
+ if (!accum[curr.address]) {
134
+ accum[curr.address] = [];
135
+ }
136
+ accum[curr.address].push(...curr.signingIndexes);
137
+ return accum;
138
+ }, {}),
139
+ };
140
+ logger.logVerboseTroubleshootingMessage('[BitcoinSatsConnectConnector] signTransaction', {
141
+ signPbstPayload,
142
+ });
143
+ try {
144
+ const response = yield request('signPsbt', signPbstPayload);
145
+ if (response.status !== 'success') {
146
+ throw response.error;
147
+ }
148
+ return { psbtBase64: response.result.psbt, txId: response.result.txid };
149
+ }
150
+ catch (_) {
151
+ const error = this.getGenericUserCancelledError();
152
+ throw error;
153
+ }
154
+ });
155
+ }
156
+ signTransactions(transactions) {
157
+ return __awaiter(this, void 0, void 0, function* () {
158
+ const { message, psbts, network } = transactions;
159
+ logger.logVerboseTroubleshootingMessage('[BitcoinSatsConnectConnector] signTransactions', {
160
+ transactions,
161
+ });
159
162
  return new Promise((resolve, reject) => {
160
- signTransaction({
163
+ signMultipleTransactions({
161
164
  getProvider: () => __awaiter(this, void 0, void 0, function* () {
162
165
  var _a, _b;
163
166
  return (_b = (_a = this.wallet) === null || _a === void 0 ? void 0 : _a.features[SATSCONNECT_FEATURE]) === null || _b === void 0 ? void 0 : _b.provider;
164
167
  }),
165
168
  onCancel: () => {
166
- const error = new Error();
167
- error.code = '-32000'; // error code for user cancelled
169
+ const error = this.getGenericUserCancelledError();
168
170
  reject(error);
169
171
  },
170
172
  onFinish: (response) => {
171
173
  resolve(response);
172
174
  },
173
175
  payload: {
174
- broadcast,
175
- inputsToSign,
176
176
  message: message || 'Sign Transaction',
177
- network: {
178
- type: this.currentNetwork,
179
- },
180
- psbtBase64,
177
+ network,
178
+ psbts,
181
179
  },
182
180
  });
183
181
  });
@@ -185,27 +183,22 @@ class BitcoinSatsConnectConnector extends BitcoinWalletConnector {
185
183
  }
186
184
  signPsbt(request) {
187
185
  return __awaiter(this, void 0, void 0, function* () {
188
- var _a, _b, _c, _d;
186
+ var _a;
189
187
  if (!((_a = request.allowedSighash) === null || _a === void 0 ? void 0 : _a.length))
190
188
  throw new Error('allowedSighash cannot be an empty array');
191
189
  const network = convertNetworkTypeForPsbt(this.currentNetwork);
192
190
  const psbtFromBase64 = Psbt.fromBase64(request.unsignedPsbtBase64, {
193
191
  network,
194
192
  });
195
- if ((_b = request.signature) === null || _b === void 0 ? void 0 : _b.length) {
196
- validatePsbt(psbtFromBase64, request.allowedSighash, request.signature);
197
- }
198
- const inputsToSign = ((_d = (_c = request.signature) === null || _c === void 0 ? void 0 : _c.map((sig) => {
199
- var _a;
200
- return ((_a = sig.signingIndexes) !== null && _a !== void 0 ? _a : []).map((inputIndex) => ({
201
- address: sig.address,
202
- sigHash: getSigHashType(psbtFromBase64.data.inputs[inputIndex]),
203
- signingIndexes: [inputIndex],
204
- }));
205
- })) !== null && _d !== void 0 ? _d : []).flat();
193
+ logger.logVerboseTroubleshootingMessage('signPsbt', {
194
+ isLegacy: this.isLegacy,
195
+ psbtFromBase64,
196
+ request,
197
+ });
206
198
  const signedPsbt = yield this.signTransaction({
199
+ allowedSignHash: request.allowedSighash[0],
207
200
  broadcast: false,
208
- inputsToSign,
201
+ inputsToSign: createSignPsbtOptionsForSatsConnect(psbtFromBase64, request, this.isLegacy),
209
202
  psbtBase64: request.unsignedPsbtBase64,
210
203
  });
211
204
  if (!signedPsbt) {
@@ -214,6 +207,42 @@ class BitcoinSatsConnectConnector extends BitcoinWalletConnector {
214
207
  return { signedPsbt: signedPsbt.psbtBase64 };
215
208
  });
216
209
  }
210
+ signPsbts(requests) {
211
+ return __awaiter(this, void 0, void 0, function* () {
212
+ var _a;
213
+ const network = convertNetworkTypeForPsbt(this.currentNetwork);
214
+ const signMultipleTransactionPayload = {
215
+ message: 'Sign Transaction',
216
+ network: {
217
+ type: this.currentNetwork,
218
+ },
219
+ psbts: [],
220
+ };
221
+ for (const request of requests) {
222
+ const psbtFromBase64 = Psbt.fromBase64(request.unsignedPsbtBase64, {
223
+ network,
224
+ });
225
+ const inputsToSign = createSignPsbtOptionsForSatsConnect(psbtFromBase64, request, this.isLegacy);
226
+ signMultipleTransactionPayload.psbts.push({
227
+ inputsToSign,
228
+ psbtBase64: request.unsignedPsbtBase64,
229
+ });
230
+ }
231
+ logger.logVerboseTroubleshootingMessage('signPsbts', signMultipleTransactionPayload);
232
+ const signedPsbts = yield this.signTransactions(signMultipleTransactionPayload);
233
+ return (_a = signedPsbts === null || signedPsbts === void 0 ? void 0 : signedPsbts.map((signedPsbts) => signedPsbts.psbtBase64)) !== null && _a !== void 0 ? _a : [];
234
+ });
235
+ }
236
+ validateActiveWallet(expectedAddress) {
237
+ return __awaiter(this, void 0, void 0, function* () {
238
+ // TODO: this is a workaround to avoid extra connecting prompt for xverse
239
+ // we should revisit it once xverse releases a new version with bug fixes
240
+ logger.debug('validateActiveWallet - skipping validation for xverse', {
241
+ expectedAddress,
242
+ });
243
+ return;
244
+ });
245
+ }
217
246
  }
218
247
 
219
248
  export { BitcoinSatsConnectConnector };
@@ -0,0 +1,216 @@
1
+ 'use client'
2
+ 'use strict';
3
+
4
+ Object.defineProperty(exports, '__esModule', { value: true });
5
+
6
+ var _tslib = require('../../../_virtual/_tslib.cjs');
7
+ var satsConnect = require('sats-connect');
8
+ var utils = require('@dynamic-labs/utils');
9
+ var walletBook = require('@dynamic-labs/wallet-book');
10
+ var _const = require('../../const.cjs');
11
+ require('bitcoinjs-lib');
12
+ require('@dynamic-labs/wallet-connector-core');
13
+ require('@dynamic-labs/sdk-api-core');
14
+ require('@wallet-standard/app');
15
+ var BitcoinSatsConnectConnector = require('../BitcoinSatsConnectConnector/BitcoinSatsConnectConnector.cjs');
16
+ var supportsSatsConnect = require('../../utils/supportsSatsConnect.cjs');
17
+
18
+ class BitcoinSatsConnectLegacyConnector extends BitcoinSatsConnectConnector.BitcoinSatsConnectConnector {
19
+ constructor() {
20
+ super(...arguments);
21
+ this.isLegacy = true;
22
+ }
23
+ getAddress() {
24
+ return _tslib.__awaiter(this, void 0, void 0, function* () {
25
+ var _a;
26
+ // xverse doesn't support wallet standard, so we won't have a wallet object,
27
+ // but it's already the default provider for sats-connect, so it's ok
28
+ // for getProvider in getAddress to return undefined
29
+ // if we're not using xverse, we need to check if there is a wallet and
30
+ // that it has the satsconnect feature to return the correct provider to use
31
+ if (!supportsSatsConnect.supportsSatsConnect(this)) {
32
+ return;
33
+ }
34
+ const wallet = walletBook.findWalletBookWallet(this.walletBook, this.key);
35
+ const inAppBrowserUrl = (_a = wallet === null || wallet === void 0 ? void 0 : wallet.mobile) === null || _a === void 0 ? void 0 : _a.inAppBrowser;
36
+ if (utils.isMobile() &&
37
+ !this.isInstalledOnBrowser() &&
38
+ inAppBrowserUrl &&
39
+ this.mobileExperience === 'in-app-browser') {
40
+ const inAppBrowserTemplate = utils.template(inAppBrowserUrl);
41
+ const deepLink = inAppBrowserTemplate({
42
+ encodedDappURI: encodeURIComponent(window.location.toString()),
43
+ });
44
+ window.location.href = deepLink;
45
+ return;
46
+ }
47
+ return new Promise((resolve, reject) => {
48
+ satsConnect.getAddress({
49
+ getProvider: () => _tslib.__awaiter(this, void 0, void 0, function* () {
50
+ var _a, _b;
51
+ return (_b = (_a = this.wallet) === null || _a === void 0 ? void 0 : _a.features[_const.SATSCONNECT_FEATURE]) === null || _b === void 0 ? void 0 : _b.provider;
52
+ }),
53
+ onCancel: () => {
54
+ const error = new Error();
55
+ error.code = '-32000'; // error code for user cancelled
56
+ reject(error);
57
+ },
58
+ onFinish: (response) => _tslib.__awaiter(this, void 0, void 0, function* () {
59
+ var _c;
60
+ const { addresses } = response;
61
+ const ordinalsAccount = addresses === null || addresses === void 0 ? void 0 : addresses.find((address) => address.purpose === satsConnect.AddressPurpose.Ordinals);
62
+ const paymentAccount = addresses === null || addresses === void 0 ? void 0 : addresses.find((address) => address.purpose === satsConnect.AddressPurpose.Payment);
63
+ const mainAddress = (_c = ordinalsAccount === null || ordinalsAccount === void 0 ? void 0 : ordinalsAccount.address) !== null && _c !== void 0 ? _c : paymentAccount === null || paymentAccount === void 0 ? void 0 : paymentAccount.address;
64
+ yield this.setConnectedAccountWithAddresses({
65
+ active: true,
66
+ mainAddress,
67
+ ordinalsAddress: ordinalsAccount,
68
+ paymentAddress: paymentAccount,
69
+ });
70
+ resolve(mainAddress);
71
+ }),
72
+ payload: {
73
+ message: 'Address for receiving Ordinals and payments',
74
+ network: {
75
+ type: this.currentNetwork,
76
+ },
77
+ purposes: [satsConnect.AddressPurpose.Ordinals, satsConnect.AddressPurpose.Payment],
78
+ },
79
+ });
80
+ });
81
+ });
82
+ }
83
+ signMessage(messageToSign, withAddress) {
84
+ return _tslib.__awaiter(this, void 0, void 0, function* () {
85
+ if (!supportsSatsConnect.supportsSatsConnect(this)) {
86
+ return;
87
+ }
88
+ return new Promise((resolve, reject) => {
89
+ satsConnect.signMessage({
90
+ getProvider: () => _tslib.__awaiter(this, void 0, void 0, function* () {
91
+ var _a, _b;
92
+ return (_b = (_a = this.wallet) === null || _a === void 0 ? void 0 : _a.features[_const.SATSCONNECT_FEATURE]) === null || _b === void 0 ? void 0 : _b.provider;
93
+ }),
94
+ onCancel: () => {
95
+ const error = new Error();
96
+ error.code = '-32000'; // error code for user cancelled
97
+ reject(error);
98
+ },
99
+ onFinish: (response) => _tslib.__awaiter(this, void 0, void 0, function* () {
100
+ if (this.isHardwareWalletEnabled) {
101
+ return resolve(JSON.stringify({
102
+ signedTransaction: {
103
+ data: response,
104
+ },
105
+ }));
106
+ }
107
+ resolve(response);
108
+ }),
109
+ payload: {
110
+ address: withAddress,
111
+ message: messageToSign,
112
+ network: {
113
+ type: this.currentNetwork,
114
+ },
115
+ },
116
+ });
117
+ });
118
+ });
119
+ }
120
+ sendBitcoin(transaction) {
121
+ return _tslib.__awaiter(this, void 0, void 0, function* () {
122
+ var _a;
123
+ const mainAddress = yield this.getAddress();
124
+ const senderAddress = (_a = (yield this.getAdditionalAddresses(mainAddress)).find((address) => address.type === 'payment')) === null || _a === void 0 ? void 0 : _a.address;
125
+ if (!senderAddress || !supportsSatsConnect.supportsSatsConnect(this)) {
126
+ return;
127
+ }
128
+ return new Promise((resolve, reject) => {
129
+ satsConnect.sendBtcTransaction({
130
+ getProvider: () => _tslib.__awaiter(this, void 0, void 0, function* () {
131
+ var _a, _b;
132
+ return (_b = (_a = this.wallet) === null || _a === void 0 ? void 0 : _a.features[_const.SATSCONNECT_FEATURE]) === null || _b === void 0 ? void 0 : _b.provider;
133
+ }),
134
+ onCancel: () => {
135
+ const error = new Error();
136
+ error.code = '-32000'; // error code for user cancelled
137
+ reject(error);
138
+ },
139
+ onFinish: (response) => {
140
+ resolve(response);
141
+ },
142
+ payload: {
143
+ network: {
144
+ type: this.currentNetwork,
145
+ },
146
+ recipients: [
147
+ {
148
+ address: transaction.recipientAddress,
149
+ amountSats: transaction.amount,
150
+ },
151
+ ],
152
+ senderAddress,
153
+ },
154
+ });
155
+ });
156
+ });
157
+ }
158
+ signTransaction(params) {
159
+ return _tslib.__awaiter(this, void 0, void 0, function* () {
160
+ const { message, psbtBase64, broadcast, inputsToSign } = params;
161
+ return new Promise((resolve, reject) => {
162
+ satsConnect.signTransaction({
163
+ getProvider: () => _tslib.__awaiter(this, void 0, void 0, function* () {
164
+ var _a, _b;
165
+ return (_b = (_a = this.wallet) === null || _a === void 0 ? void 0 : _a.features[_const.SATSCONNECT_FEATURE]) === null || _b === void 0 ? void 0 : _b.provider;
166
+ }),
167
+ onCancel: () => {
168
+ const error = new Error();
169
+ error.code = '-32000'; // error code for user cancelled
170
+ reject(error);
171
+ },
172
+ onFinish: (response) => {
173
+ resolve(response);
174
+ },
175
+ payload: {
176
+ broadcast,
177
+ inputsToSign,
178
+ message: message || 'Sign Transaction',
179
+ network: {
180
+ type: this.currentNetwork,
181
+ },
182
+ psbtBase64,
183
+ },
184
+ });
185
+ });
186
+ });
187
+ }
188
+ signTransactions(transactions) {
189
+ return _tslib.__awaiter(this, void 0, void 0, function* () {
190
+ const { message, psbts, network } = transactions;
191
+ return new Promise((resolve, reject) => {
192
+ satsConnect.signMultipleTransactions({
193
+ getProvider: () => _tslib.__awaiter(this, void 0, void 0, function* () {
194
+ var _a, _b;
195
+ return (_b = (_a = this.wallet) === null || _a === void 0 ? void 0 : _a.features[_const.SATSCONNECT_FEATURE]) === null || _b === void 0 ? void 0 : _b.provider;
196
+ }),
197
+ onCancel: () => {
198
+ const error = new Error();
199
+ error.code = '-32000'; // error code for user cancelled
200
+ reject(error);
201
+ },
202
+ onFinish: (response) => {
203
+ resolve(response);
204
+ },
205
+ payload: {
206
+ message: message || 'Sign Transaction',
207
+ network,
208
+ psbts,
209
+ },
210
+ });
211
+ });
212
+ });
213
+ }
214
+ }
215
+
216
+ exports.BitcoinSatsConnectLegacyConnector = BitcoinSatsConnectLegacyConnector;
@@ -0,0 +1,11 @@
1
+ import { SignMultipleTransactionsPayload, SignTransactionResponse } from 'sats-connect';
2
+ import { BitcoinTransaction, SatsConnectSignTransactionInput } from '../../types';
3
+ import { BitcoinSatsConnectConnector } from '../BitcoinSatsConnectConnector';
4
+ export declare abstract class BitcoinSatsConnectLegacyConnector extends BitcoinSatsConnectConnector {
5
+ isLegacy: boolean;
6
+ getAddress(): Promise<string | undefined>;
7
+ signMessage(messageToSign: string, withAddress: string): Promise<string | undefined>;
8
+ sendBitcoin(transaction: BitcoinTransaction): Promise<string | undefined>;
9
+ signTransaction(params: SatsConnectSignTransactionInput): Promise<SignTransactionResponse | undefined>;
10
+ signTransactions(transactions: SignMultipleTransactionsPayload): Promise<SignTransactionResponse[] | undefined>;
11
+ }