@dynamic-labs/bitcoin 3.0.0-alpha.64 → 3.0.0-alpha.65

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 CHANGED
@@ -1,4 +1,11 @@
1
1
 
2
+ ## [3.0.0-alpha.65](https://github.com/dynamic-labs/DynamicAuth/compare/v3.0.0-alpha.64...v3.0.0-alpha.65) (2024-09-11)
3
+
4
+
5
+ ### Bug Fixes
6
+
7
+ * gracefully handle errors when trying to find solana tx destination ([#6847](https://github.com/dynamic-labs/DynamicAuth/issues/6847)) ([19359ad](https://github.com/dynamic-labs/DynamicAuth/commit/19359ad40b16af0edb822d1fc6643fecdbc89adf))
8
+
2
9
  ## [3.0.0-alpha.64](https://github.com/dynamic-labs/DynamicAuth/compare/v3.0.0-alpha.63...v3.0.0-alpha.64) (2024-09-10)
3
10
 
4
11
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dynamic-labs/bitcoin",
3
- "version": "3.0.0-alpha.64",
3
+ "version": "3.0.0-alpha.65",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/dynamic-labs/dynamic-auth.git",
@@ -32,9 +32,9 @@
32
32
  "@wallet-standard/base": "1.0.1",
33
33
  "bitcoinjs-lib": "6.1.5",
34
34
  "sats-connect": "2.8.0",
35
- "@dynamic-labs/utils": "3.0.0-alpha.64",
36
- "@dynamic-labs/wallet-book": "3.0.0-alpha.64",
37
- "@dynamic-labs/wallet-connector-core": "3.0.0-alpha.64",
35
+ "@dynamic-labs/utils": "3.0.0-alpha.65",
36
+ "@dynamic-labs/wallet-book": "3.0.0-alpha.65",
37
+ "@dynamic-labs/wallet-connector-core": "3.0.0-alpha.65",
38
38
  "stream": "0.0.2"
39
39
  },
40
40
  "peerDependencies": {}
@@ -14,6 +14,7 @@ export declare class BitcoinProviderHelper {
14
14
  walletStandard?: {
15
15
  features: string[];
16
16
  name: string;
17
+ providerId?: string | undefined;
17
18
  } | undefined;
18
19
  walletStandardLocators?: {
19
20
  name: string;
@@ -8,29 +8,46 @@ var bitcoinjsLib = require('bitcoinjs-lib');
8
8
  var satsConnect = require('sats-connect');
9
9
  var utils = require('@dynamic-labs/utils');
10
10
  var walletBook = require('@dynamic-labs/wallet-book');
11
- var _const = require('../../const.cjs');
12
- require('@dynamic-labs/wallet-connector-core');
13
- var BitcoinWalletConnector = require('../BitcoinWalletConnector.cjs');
11
+ var walletConnectorCore = require('@dynamic-labs/wallet-connector-core');
14
12
  var bitcoinNetworkTypeToNetworks = require('../../utils/psbt/bitcoinNetworkTypeToNetworks.cjs');
13
+ var BitcoinWalletConnector = require('../BitcoinWalletConnector.cjs');
15
14
  var createSignPsbtOptions = require('../../utils/psbt/createSignPsbtOptions.cjs');
16
- var supportsSatsConnect = require('../../utils/supportsSatsConnect.cjs');
17
15
 
18
16
  class BitcoinSatsConnectConnector extends BitcoinWalletConnector.BitcoinWalletConnector {
19
17
  constructor(opts) {
20
18
  super(opts);
21
19
  this.currentNetwork = satsConnect.BitcoinNetworkType.Mainnet;
22
20
  }
23
- getAddress() {
21
+ getGenericUserCancelledError() {
22
+ const error = new Error();
23
+ error.code = '-32000'; // error code for user cancelled
24
+ return error;
25
+ }
26
+ getAddresses() {
24
27
  return _tslib.__awaiter(this, void 0, void 0, function* () {
25
28
  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;
29
+ const getAddressResponse = yield satsConnect.request('getAddresses', {
30
+ purposes: [satsConnect.AddressPurpose.Payment, satsConnect.AddressPurpose.Ordinals],
31
+ });
32
+ if (getAddressResponse.status !== 'success') {
33
+ throw getAddressResponse.error;
33
34
  }
35
+ const { addresses } = getAddressResponse.result;
36
+ const ordinalsAccount = addresses === null || addresses === void 0 ? void 0 : addresses.find((address) => address.purpose === satsConnect.AddressPurpose.Ordinals);
37
+ const paymentAccount = addresses === null || addresses === void 0 ? void 0 : addresses.find((address) => address.purpose === satsConnect.AddressPurpose.Payment);
38
+ 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;
39
+ yield this.setConnectedAccountWithAddresses({
40
+ active: true,
41
+ mainAddress,
42
+ ordinalsAddress: ordinalsAccount,
43
+ paymentAddress: paymentAccount,
44
+ });
45
+ return mainAddress;
46
+ });
47
+ }
48
+ getAddress() {
49
+ return _tslib.__awaiter(this, void 0, void 0, function* () {
50
+ var _a;
34
51
  const wallet = walletBook.findWalletBookWallet(this.walletBook, this.key);
35
52
  const inAppBrowserUrl = (_a = wallet === null || wallet === void 0 ? void 0 : wallet.mobile) === null || _a === void 0 ? void 0 : _a.inAppBrowser;
36
53
  if (utils.isMobile() &&
@@ -44,154 +61,87 @@ class BitcoinSatsConnectConnector extends BitcoinWalletConnector.BitcoinWalletCo
44
61
  window.location.href = deepLink;
45
62
  return;
46
63
  }
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
- signMessageWithAddress(messageToSign, address) {
84
- return _tslib.__awaiter(this, void 0, void 0, function* () {
85
- if (!supportsSatsConnect.supportsSatsConnect(this)) {
86
- return;
64
+ try {
65
+ // requesting permissions on initial connect will avoid connection prompt for other requests
66
+ const permissionResponse = yield satsConnect.request('wallet_requestPermissions', undefined);
67
+ if (permissionResponse.status !== 'success') {
68
+ throw permissionResponse.error;
69
+ }
70
+ const address = yield this.getAddresses();
71
+ return address;
72
+ }
73
+ catch (_) {
74
+ const error = this.getGenericUserCancelledError();
75
+ throw error;
87
76
  }
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,
111
- message: messageToSign,
112
- network: {
113
- type: this.currentNetwork,
114
- },
115
- },
116
- });
117
- });
118
77
  });
119
78
  }
120
- signMessage(messageToSign) {
79
+ signMessage(messageToSign, withAddress) {
121
80
  return _tslib.__awaiter(this, void 0, void 0, function* () {
122
- const [walletAddress] = yield this.getConnectedAccounts();
123
- if (!walletAddress) {
124
- return;
81
+ try {
82
+ const response = yield satsConnect.request('signMessage', {
83
+ address: withAddress,
84
+ message: messageToSign,
85
+ });
86
+ if (response.status !== 'success') {
87
+ throw response.error;
88
+ }
89
+ if (this.isHardwareWalletEnabled) {
90
+ return JSON.stringify({
91
+ signedTransaction: {
92
+ data: response.result.signature,
93
+ },
94
+ });
95
+ }
96
+ return response.result.signature;
97
+ }
98
+ catch (_) {
99
+ const error = this.getGenericUserCancelledError();
100
+ throw error;
125
101
  }
126
- return this.signMessageWithAddress(messageToSign, walletAddress);
127
102
  });
128
103
  }
129
104
  sendBitcoin(transaction) {
130
105
  return _tslib.__awaiter(this, void 0, void 0, function* () {
131
- var _a;
132
- const mainAddress = yield this.getAddress();
133
- const senderAddress = (_a = (yield this.getAdditionalAddresses(mainAddress)).find((address) => address.type === 'payment')) === null || _a === void 0 ? void 0 : _a.address;
134
- if (!senderAddress || !supportsSatsConnect.supportsSatsConnect(this)) {
135
- return;
136
- }
137
- return new Promise((resolve, reject) => {
138
- satsConnect.sendBtcTransaction({
139
- getProvider: () => _tslib.__awaiter(this, void 0, void 0, function* () {
140
- var _a, _b;
141
- return (_b = (_a = this.wallet) === null || _a === void 0 ? void 0 : _a.features[_const.SATSCONNECT_FEATURE]) === null || _b === void 0 ? void 0 : _b.provider;
142
- }),
143
- onCancel: () => {
144
- const error = new Error();
145
- error.code = '-32000'; // error code for user cancelled
146
- reject(error);
147
- },
148
- onFinish: (response) => {
149
- resolve(response);
150
- },
151
- payload: {
152
- network: {
153
- type: this.currentNetwork,
106
+ try {
107
+ const response = yield satsConnect.request('sendTransfer', {
108
+ recipients: [
109
+ {
110
+ address: transaction.recipientAddress,
111
+ amount: Number(transaction.amount),
154
112
  },
155
- recipients: [
156
- {
157
- address: transaction.recipientAddress,
158
- amountSats: transaction.amount,
159
- },
160
- ],
161
- senderAddress,
162
- },
113
+ ],
163
114
  });
164
- });
115
+ if (response.status !== 'success') {
116
+ throw response.error;
117
+ }
118
+ return response.result.txid;
119
+ }
120
+ catch (_) {
121
+ const error = this.getGenericUserCancelledError();
122
+ throw error;
123
+ }
165
124
  });
166
125
  }
167
126
  signTransaction(params) {
168
127
  return _tslib.__awaiter(this, void 0, void 0, function* () {
169
- const { message, psbtBase64, broadcast, inputsToSign } = params;
170
- return new Promise((resolve, reject) => {
171
- satsConnect.signTransaction({
172
- getProvider: () => _tslib.__awaiter(this, void 0, void 0, function* () {
173
- var _a, _b;
174
- return (_b = (_a = this.wallet) === null || _a === void 0 ? void 0 : _a.features[_const.SATSCONNECT_FEATURE]) === null || _b === void 0 ? void 0 : _b.provider;
175
- }),
176
- onCancel: () => {
177
- const error = new Error();
178
- error.code = '-32000'; // error code for user cancelled
179
- reject(error);
180
- },
181
- onFinish: (response) => {
182
- resolve(response);
183
- },
184
- payload: {
185
- broadcast,
186
- inputsToSign,
187
- message: message || 'Sign Transaction',
188
- network: {
189
- type: this.currentNetwork,
190
- },
191
- psbtBase64,
192
- },
128
+ const { allowedSignHash, psbtBase64, broadcast, inputsToSign } = params;
129
+ try {
130
+ const response = yield satsConnect.request('signPsbt', {
131
+ allowedSignHash,
132
+ broadcast,
133
+ psbt: psbtBase64,
134
+ signInputs: Object.fromEntries(inputsToSign.map((input) => [input.address, input.signingIndexes])),
193
135
  });
194
- });
136
+ if (response.status !== 'success') {
137
+ throw response.error;
138
+ }
139
+ return { psbtBase64: response.result.psbt, txId: response.result.txid };
140
+ }
141
+ catch (_) {
142
+ const error = this.getGenericUserCancelledError();
143
+ throw error;
144
+ }
195
145
  });
196
146
  }
197
147
  signTransactions(transactions) {
@@ -199,13 +149,8 @@ class BitcoinSatsConnectConnector extends BitcoinWalletConnector.BitcoinWalletCo
199
149
  const { message, psbts, network } = transactions;
200
150
  return new Promise((resolve, reject) => {
201
151
  satsConnect.signMultipleTransactions({
202
- getProvider: () => _tslib.__awaiter(this, void 0, void 0, function* () {
203
- var _a, _b;
204
- return (_b = (_a = this.wallet) === null || _a === void 0 ? void 0 : _a.features[_const.SATSCONNECT_FEATURE]) === null || _b === void 0 ? void 0 : _b.provider;
205
- }),
206
152
  onCancel: () => {
207
- const error = new Error();
208
- error.code = '-32000'; // error code for user cancelled
153
+ const error = this.getGenericUserCancelledError();
209
154
  reject(error);
210
155
  },
211
156
  onFinish: (response) => {
@@ -265,6 +210,16 @@ class BitcoinSatsConnectConnector extends BitcoinWalletConnector.BitcoinWalletCo
265
210
  return (_a = signedPsbts === null || signedPsbts === void 0 ? void 0 : signedPsbts.map((signedPsbts) => signedPsbts.psbtBase64)) !== null && _a !== void 0 ? _a : [];
266
211
  });
267
212
  }
213
+ validateActiveWallet(expectedAddress) {
214
+ return _tslib.__awaiter(this, void 0, void 0, function* () {
215
+ // TODO: this is a workaround to avoid extra connecting prompt for xverse
216
+ // we should revisit it once xverse releases a new version with bug fixes
217
+ walletConnectorCore.logger.debug('validateActiveWallet - skipping validation for xverse', {
218
+ expectedAddress,
219
+ });
220
+ return;
221
+ });
222
+ }
268
223
  }
269
224
 
270
225
  exports.BitcoinSatsConnectConnector = BitcoinSatsConnectConnector;
@@ -4,12 +4,14 @@ import { BitcoinWalletConnector, BitcoinWalletConnectorOpts } from '../BitcoinWa
4
4
  export declare abstract class BitcoinSatsConnectConnector extends BitcoinWalletConnector {
5
5
  currentNetwork: BitcoinNetworkType;
6
6
  constructor(opts: BitcoinWalletConnectorOpts);
7
+ private getGenericUserCancelledError;
8
+ private getAddresses;
7
9
  getAddress(): Promise<string | undefined>;
8
- signMessageWithAddress(messageToSign: string, address: string): Promise<string | undefined>;
9
- signMessage(messageToSign: string): Promise<string | undefined>;
10
+ signMessage(messageToSign: string, withAddress: string): Promise<string | undefined>;
10
11
  sendBitcoin(transaction: BitcoinTransaction): Promise<string | undefined>;
11
12
  signTransaction(params: SatsConnectSignTransactionInput): Promise<SignTransactionResponse | undefined>;
12
13
  signTransactions(transactions: SignMultipleTransactionsPayload): Promise<SignTransactionResponse[] | undefined>;
13
14
  signPsbt(request: BitcoinSignPsbtRequest): Promise<BitcoinSignPsbtResponse | undefined>;
14
15
  signPsbts(requests: BitcoinSignPsbtRequest[]): Promise<string[] | undefined>;
16
+ validateActiveWallet(expectedAddress: string): Promise<void>;
15
17
  }