@dynamic-labs/bitcoin 1.1.0-alpha.22 → 1.1.0-alpha.24

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 (43) hide show
  1. package/CHANGELOG.md +15 -0
  2. package/package.json +7 -5
  3. package/src/BitcoinBtcKitConnector.cjs +32 -0
  4. package/src/BitcoinBtcKitConnector.d.ts +2 -1
  5. package/src/BitcoinBtcKitConnector.js +32 -0
  6. package/src/BitcoinSatsConnectConnector.cjs +65 -0
  7. package/src/BitcoinSatsConnectConnector.d.ts +4 -2
  8. package/src/BitcoinSatsConnectConnector.js +66 -1
  9. package/src/BitcoinWalletConnector.cjs +1 -1
  10. package/src/BitcoinWalletConnector.d.ts +2 -1
  11. package/src/BitcoinWalletConnector.js +1 -1
  12. package/src/OkxConnector.cjs +13 -0
  13. package/src/OkxConnector.d.ts +2 -1
  14. package/src/OkxConnector.js +13 -0
  15. package/src/UnisatConnector.cjs +13 -0
  16. package/src/UnisatConnector.d.ts +2 -1
  17. package/src/UnisatConnector.js +13 -0
  18. package/src/types.d.ts +31 -8
  19. package/src/utils/psbt/bitcoinNetworkTypeToNetworks.cjs +13 -0
  20. package/src/utils/psbt/bitcoinNetworkTypeToNetworks.d.ts +3 -0
  21. package/src/utils/psbt/bitcoinNetworkTypeToNetworks.js +9 -0
  22. package/src/utils/psbt/createSignPsbtOptions.cjs +33 -0
  23. package/src/utils/psbt/createSignPsbtOptions.d.ts +3 -0
  24. package/src/utils/psbt/createSignPsbtOptions.js +29 -0
  25. package/src/utils/psbt/extractAddressFromInput.cjs +34 -0
  26. package/src/utils/psbt/extractAddressFromInput.d.ts +6 -0
  27. package/src/utils/psbt/extractAddressFromInput.js +30 -0
  28. package/src/utils/psbt/getSigHashType.cjs +35 -0
  29. package/src/utils/psbt/getSigHashType.d.ts +8 -0
  30. package/src/utils/psbt/getSigHashType.js +31 -0
  31. package/src/utils/psbt/index.d.ts +4 -0
  32. package/src/utils/psbt/validator/index.d.ts +1 -0
  33. package/src/utils/psbt/validator/validateAddress.cjs +18 -0
  34. package/src/utils/psbt/validator/validateAddress.d.ts +2 -0
  35. package/src/utils/psbt/validator/validateAddress.js +14 -0
  36. package/src/utils/psbt/validator/validatePsbt.cjs +40 -0
  37. package/src/utils/psbt/validator/validatePsbt.d.ts +11 -0
  38. package/src/utils/psbt/validator/validatePsbt.js +36 -0
  39. package/src/utils/psbt/validator/validateSigHash.cjs +15 -0
  40. package/src/utils/psbt/validator/validateSigHash.d.ts +2 -0
  41. package/src/utils/psbt/validator/validateSigHash.js +11 -0
  42. package/src/utils/supportsSatsConnect.cjs +1 -0
  43. package/src/utils/supportsSatsConnect.js +1 -0
package/CHANGELOG.md CHANGED
@@ -1,4 +1,19 @@
1
1
 
2
+ ## [1.1.0-alpha.24](https://github.com/dynamic-labs/DynamicAuth/compare/v1.1.0-alpha.23...v1.1.0-alpha.24) (2024-02-02)
3
+
4
+
5
+ ### Bug Fixes
6
+
7
+ * remove isFullyConnected when unable to restore wallet ([#4575](https://github.com/dynamic-labs/DynamicAuth/issues/4575)) ([4504ff0](https://github.com/dynamic-labs/DynamicAuth/commit/4504ff03f594cd9a60055e6f52b7ea8001542bbe))
8
+
9
+ ## [1.1.0-alpha.23](https://github.com/dynamic-labs/DynamicAuth/compare/v1.1.0-alpha.22...v1.1.0-alpha.23) (2024-02-01)
10
+
11
+
12
+ ### Features
13
+
14
+ * bitcoin signPsbt ([3c964de](https://github.com/dynamic-labs/DynamicAuth/commit/3c964dea8a55debaf184c5a94f0f5fabdda3c877))
15
+ * embedded wallet email auth flow ([#4353](https://github.com/dynamic-labs/DynamicAuth/issues/4353)) ([4875da3](https://github.com/dynamic-labs/DynamicAuth/commit/4875da32c47c27facef1b1cdbdc214566bbfd171))
16
+
2
17
  ## [1.1.0-alpha.22](https://github.com/dynamic-labs/DynamicAuth/compare/v1.1.0-alpha.21...v1.1.0-alpha.22) (2024-02-01)
3
18
 
4
19
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dynamic-labs/bitcoin",
3
- "version": "1.1.0-alpha.22",
3
+ "version": "1.1.0-alpha.24",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/dynamic-labs/DynamicAuth.git",
@@ -26,13 +26,15 @@
26
26
  "./package.json": "./package.json"
27
27
  },
28
28
  "dependencies": {
29
- "@dynamic-labs/sdk-api": "0.0.356",
29
+ "@btckit/types": "^0.0.19",
30
+ "@dynamic-labs/sdk-api": "0.0.361",
30
31
  "@wallet-standard/app": "^1.0.1",
31
32
  "@wallet-standard/base": "^1.0.1",
33
+ "bitcoinjs-lib": "^6.1.5",
32
34
  "sats-connect": "^1.1.2",
33
- "@dynamic-labs/utils": "1.1.0-alpha.22",
34
- "@dynamic-labs/wallet-book": "1.1.0-alpha.22",
35
- "@dynamic-labs/wallet-connector-core": "1.1.0-alpha.22",
35
+ "@dynamic-labs/utils": "1.1.0-alpha.24",
36
+ "@dynamic-labs/wallet-book": "1.1.0-alpha.24",
37
+ "@dynamic-labs/wallet-connector-core": "1.1.0-alpha.24",
36
38
  "stream": "0.0.2"
37
39
  },
38
40
  "peerDependencies": {}
@@ -4,8 +4,11 @@ Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var _tslib = require('../_virtual/_tslib.cjs');
6
6
  var satsConnect = require('sats-connect');
7
+ var bitcoinjsLib = require('bitcoinjs-lib');
7
8
  var walletConnectorCore = require('@dynamic-labs/wallet-connector-core');
8
9
  var BitcoinWalletConnector = require('./BitcoinWalletConnector.cjs');
10
+ var validatePsbt = require('./utils/psbt/validator/validatePsbt.cjs');
11
+ var bitcoinNetworkTypeToNetworks = require('./utils/psbt/bitcoinNetworkTypeToNetworks.cjs');
9
12
 
10
13
  class BitcoinBtcKitConnector extends BitcoinWalletConnector.BitcoinWalletConnector {
11
14
  constructor(opts, btcKit) {
@@ -71,6 +74,35 @@ class BitcoinBtcKitConnector extends BitcoinWalletConnector.BitcoinWalletConnect
71
74
  return response.result.txid;
72
75
  });
73
76
  }
77
+ signPsbt(request) {
78
+ var _a, _b;
79
+ return _tslib.__awaiter(this, void 0, void 0, function* () {
80
+ const connectedAddress = yield this.fetchPublicAddress();
81
+ if (!connectedAddress || !this.btcKit) {
82
+ return;
83
+ }
84
+ const network = bitcoinNetworkTypeToNetworks.convertNetworkTypeForPsbt(this.currentNetwork);
85
+ const psbtFromBase64 = bitcoinjsLib.Psbt.fromBase64(request.unsignedPsbtBase64, {
86
+ network,
87
+ });
88
+ if ((_a = request.signature) === null || _a === void 0 ? void 0 : _a.length) {
89
+ validatePsbt.validatePsbt(psbtFromBase64, request.allowedSighash, request.signature);
90
+ }
91
+ const signPsbtRequestParams = {
92
+ allowedSighash: request.allowedSighash,
93
+ hex: psbtFromBase64.toHex(),
94
+ signAtIndex: (_b = request.signature) === null || _b === void 0 ? void 0 : _b.flatMap((sig) => sig.signingIndexes).filter(Number.isInteger),
95
+ };
96
+ const signedPsbtResponse = (yield this.btcKit.request('signPsbt', Object.assign({ broadcast: false, network }, signPsbtRequestParams)));
97
+ if (!signedPsbtResponse ||
98
+ !('result' in signedPsbtResponse) ||
99
+ !('hex' in signedPsbtResponse.result)) {
100
+ throw new Error('signPsbt - failed to sign PSBT');
101
+ }
102
+ const psbtHex = signedPsbtResponse.result.hex;
103
+ return { signedPsbt: bitcoinjsLib.Psbt.fromHex(psbtHex, { network }).toBase64() };
104
+ });
105
+ }
74
106
  }
75
107
 
76
108
  exports.BitcoinBtcKitConnector = BitcoinBtcKitConnector;
@@ -1,7 +1,7 @@
1
1
  import type { BtcKitListenFn, BtcKitRequestFn } from '@btckit/types';
2
2
  import { BitcoinNetworkType } from 'sats-connect';
3
3
  import { BitcoinWalletConnector, BitcoinWalletConnectorOpts } from './BitcoinWalletConnector';
4
- import { BitcoinTransaction } from './types';
4
+ import { BitcoinTransaction, BitcoinSignPsbtRequest, BitcoinSignPsbtResponse } from './types';
5
5
  export type BtcKitProvider = {
6
6
  request: BtcKitRequestFn;
7
7
  listen: BtcKitListenFn;
@@ -13,4 +13,5 @@ export declare abstract class BitcoinBtcKitConnector extends BitcoinWalletConnec
13
13
  fetchPublicAddress(): Promise<string | undefined>;
14
14
  signMessage(messageToSign: string): Promise<string | undefined>;
15
15
  sendBitcoin(transaction: BitcoinTransaction): Promise<string | undefined>;
16
+ signPsbt(request: BitcoinSignPsbtRequest): Promise<BitcoinSignPsbtResponse | undefined>;
16
17
  }
@@ -1,7 +1,10 @@
1
1
  import { __awaiter } from '../_virtual/_tslib.js';
2
2
  import { BitcoinNetworkType } from 'sats-connect';
3
+ import { Psbt } from 'bitcoinjs-lib';
3
4
  import { logger } from '@dynamic-labs/wallet-connector-core';
4
5
  import { BitcoinWalletConnector } from './BitcoinWalletConnector.js';
6
+ import { validatePsbt } from './utils/psbt/validator/validatePsbt.js';
7
+ import { convertNetworkTypeForPsbt } from './utils/psbt/bitcoinNetworkTypeToNetworks.js';
5
8
 
6
9
  class BitcoinBtcKitConnector extends BitcoinWalletConnector {
7
10
  constructor(opts, btcKit) {
@@ -67,6 +70,35 @@ class BitcoinBtcKitConnector extends BitcoinWalletConnector {
67
70
  return response.result.txid;
68
71
  });
69
72
  }
73
+ signPsbt(request) {
74
+ var _a, _b;
75
+ return __awaiter(this, void 0, void 0, function* () {
76
+ const connectedAddress = yield this.fetchPublicAddress();
77
+ if (!connectedAddress || !this.btcKit) {
78
+ return;
79
+ }
80
+ const network = convertNetworkTypeForPsbt(this.currentNetwork);
81
+ const psbtFromBase64 = Psbt.fromBase64(request.unsignedPsbtBase64, {
82
+ network,
83
+ });
84
+ if ((_a = request.signature) === null || _a === void 0 ? void 0 : _a.length) {
85
+ validatePsbt(psbtFromBase64, request.allowedSighash, request.signature);
86
+ }
87
+ const signPsbtRequestParams = {
88
+ allowedSighash: request.allowedSighash,
89
+ hex: psbtFromBase64.toHex(),
90
+ signAtIndex: (_b = request.signature) === null || _b === void 0 ? void 0 : _b.flatMap((sig) => sig.signingIndexes).filter(Number.isInteger),
91
+ };
92
+ const signedPsbtResponse = (yield this.btcKit.request('signPsbt', Object.assign({ broadcast: false, network }, signPsbtRequestParams)));
93
+ if (!signedPsbtResponse ||
94
+ !('result' in signedPsbtResponse) ||
95
+ !('hex' in signedPsbtResponse.result)) {
96
+ throw new Error('signPsbt - failed to sign PSBT');
97
+ }
98
+ const psbtHex = signedPsbtResponse.result.hex;
99
+ return { signedPsbt: Psbt.fromHex(psbtHex, { network }).toBase64() };
100
+ });
101
+ }
70
102
  }
71
103
 
72
104
  export { BitcoinBtcKitConnector };
@@ -4,10 +4,14 @@ Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var _tslib = require('../_virtual/_tslib.cjs');
6
6
  var satsConnect = require('sats-connect');
7
+ var bitcoinjsLib = require('bitcoinjs-lib');
7
8
  var BitcoinWalletConnector = require('./BitcoinWalletConnector.cjs');
8
9
  var _const = require('./const.cjs');
9
10
  require('@dynamic-labs/utils');
10
11
  require('@dynamic-labs/wallet-connector-core');
12
+ var validatePsbt = require('./utils/psbt/validator/validatePsbt.cjs');
13
+ var getSigHashType = require('./utils/psbt/getSigHashType.cjs');
14
+ var bitcoinNetworkTypeToNetworks = require('./utils/psbt/bitcoinNetworkTypeToNetworks.cjs');
11
15
  var supportsSatsConnect = require('./utils/supportsSatsConnect.cjs');
12
16
 
13
17
  class BitcoinSatsConnectConnector extends BitcoinWalletConnector.BitcoinWalletConnector {
@@ -129,6 +133,67 @@ class BitcoinSatsConnectConnector extends BitcoinWalletConnector.BitcoinWalletCo
129
133
  });
130
134
  });
131
135
  }
136
+ signTransaction(params) {
137
+ return _tslib.__awaiter(this, void 0, void 0, function* () {
138
+ const { message, psbtBase64, broadcast, inputsToSign } = params;
139
+ return new Promise((resolve, reject) => {
140
+ satsConnect.signTransaction({
141
+ getProvider: () => _tslib.__awaiter(this, void 0, void 0, function* () {
142
+ var _a, _b;
143
+ return (_b = (_a = this.wallet) === null || _a === void 0 ? void 0 : _a.features[_const.SATSCONNECT_FEATURE]) === null || _b === void 0 ? void 0 : _b.provider;
144
+ }),
145
+ onCancel: () => {
146
+ const error = new Error();
147
+ error.code = '-32000'; // error code for user cancelled
148
+ reject(error);
149
+ },
150
+ onFinish: (response) => {
151
+ resolve(response);
152
+ },
153
+ payload: {
154
+ broadcast,
155
+ inputsToSign,
156
+ message: message || 'Sign Transaction',
157
+ network: {
158
+ type: this.currentNetwork,
159
+ },
160
+ psbtBase64,
161
+ },
162
+ });
163
+ });
164
+ });
165
+ }
166
+ signPsbt(request) {
167
+ var _a, _b, _c, _d;
168
+ return _tslib.__awaiter(this, void 0, void 0, function* () {
169
+ if (!((_a = request.allowedSighash) === null || _a === void 0 ? void 0 : _a.length))
170
+ throw new Error('allowedSighash cannot be an empty array');
171
+ const network = bitcoinNetworkTypeToNetworks.convertNetworkTypeForPsbt(this.currentNetwork);
172
+ const psbtFromBase64 = bitcoinjsLib.Psbt.fromBase64(request.unsignedPsbtBase64, {
173
+ network,
174
+ });
175
+ if ((_b = request.signature) === null || _b === void 0 ? void 0 : _b.length) {
176
+ validatePsbt.validatePsbt(psbtFromBase64, request.allowedSighash, request.signature);
177
+ }
178
+ const inputsToSign = ((_d = (_c = request.signature) === null || _c === void 0 ? void 0 : _c.map((sig) => {
179
+ var _a;
180
+ return ((_a = sig.signingIndexes) !== null && _a !== void 0 ? _a : []).map((inputIndex) => ({
181
+ address: sig.address,
182
+ sigHash: getSigHashType.getSigHashType(psbtFromBase64.data.inputs[inputIndex]),
183
+ signingIndexes: [inputIndex],
184
+ }));
185
+ })) !== null && _d !== void 0 ? _d : []).flat();
186
+ const signedPsbt = yield this.signTransaction({
187
+ broadcast: false,
188
+ inputsToSign,
189
+ psbtBase64: request.unsignedPsbtBase64,
190
+ });
191
+ if (!signedPsbt) {
192
+ throw new Error('Failed to sign transaction with sats-connect');
193
+ }
194
+ return { signedPsbt: signedPsbt.psbtBase64 };
195
+ });
196
+ }
132
197
  }
133
198
 
134
199
  exports.BitcoinSatsConnectConnector = BitcoinSatsConnectConnector;
@@ -1,10 +1,12 @@
1
- import { BitcoinNetworkType } from 'sats-connect';
1
+ import { BitcoinNetworkType, SignTransactionResponse } from 'sats-connect';
2
2
  import { BitcoinWalletConnector, BitcoinWalletConnectorOpts } from './BitcoinWalletConnector';
3
- import { BitcoinTransaction } from './types';
3
+ import { BitcoinTransaction, SatsConnectSignTransactionInput, BitcoinSignPsbtRequest, BitcoinSignPsbtResponse } from './types';
4
4
  export declare abstract class BitcoinSatsConnectConnector extends BitcoinWalletConnector {
5
5
  currentNetwork: BitcoinNetworkType;
6
6
  constructor(opts: BitcoinWalletConnectorOpts);
7
7
  fetchPublicAddress(): Promise<string | undefined>;
8
8
  signMessage(messageToSign: string): Promise<string | undefined>;
9
9
  sendBitcoin(transaction: BitcoinTransaction): Promise<string | undefined>;
10
+ signTransaction(params: SatsConnectSignTransactionInput): Promise<SignTransactionResponse | undefined>;
11
+ signPsbt(request: BitcoinSignPsbtRequest): Promise<BitcoinSignPsbtResponse | undefined>;
10
12
  }
@@ -1,9 +1,13 @@
1
1
  import { __awaiter } from '../_virtual/_tslib.js';
2
- import { BitcoinNetworkType, getAddress, AddressPurpose, signMessage, sendBtcTransaction } from 'sats-connect';
2
+ import { BitcoinNetworkType, getAddress, AddressPurpose, signMessage, sendBtcTransaction, signTransaction } from 'sats-connect';
3
+ import { Psbt } from 'bitcoinjs-lib';
3
4
  import { BitcoinWalletConnector } from './BitcoinWalletConnector.js';
4
5
  import { SATSCONNECT_FEATURE } from './const.js';
5
6
  import '@dynamic-labs/utils';
6
7
  import '@dynamic-labs/wallet-connector-core';
8
+ import { validatePsbt } from './utils/psbt/validator/validatePsbt.js';
9
+ import { getSigHashType } from './utils/psbt/getSigHashType.js';
10
+ import { convertNetworkTypeForPsbt } from './utils/psbt/bitcoinNetworkTypeToNetworks.js';
7
11
  import { supportsSatsConnect } from './utils/supportsSatsConnect.js';
8
12
 
9
13
  class BitcoinSatsConnectConnector extends BitcoinWalletConnector {
@@ -125,6 +129,67 @@ class BitcoinSatsConnectConnector extends BitcoinWalletConnector {
125
129
  });
126
130
  });
127
131
  }
132
+ signTransaction(params) {
133
+ return __awaiter(this, void 0, void 0, function* () {
134
+ const { message, psbtBase64, broadcast, inputsToSign } = params;
135
+ return new Promise((resolve, reject) => {
136
+ signTransaction({
137
+ getProvider: () => __awaiter(this, void 0, void 0, function* () {
138
+ var _a, _b;
139
+ return (_b = (_a = this.wallet) === null || _a === void 0 ? void 0 : _a.features[SATSCONNECT_FEATURE]) === null || _b === void 0 ? void 0 : _b.provider;
140
+ }),
141
+ onCancel: () => {
142
+ const error = new Error();
143
+ error.code = '-32000'; // error code for user cancelled
144
+ reject(error);
145
+ },
146
+ onFinish: (response) => {
147
+ resolve(response);
148
+ },
149
+ payload: {
150
+ broadcast,
151
+ inputsToSign,
152
+ message: message || 'Sign Transaction',
153
+ network: {
154
+ type: this.currentNetwork,
155
+ },
156
+ psbtBase64,
157
+ },
158
+ });
159
+ });
160
+ });
161
+ }
162
+ signPsbt(request) {
163
+ var _a, _b, _c, _d;
164
+ return __awaiter(this, void 0, void 0, function* () {
165
+ if (!((_a = request.allowedSighash) === null || _a === void 0 ? void 0 : _a.length))
166
+ throw new Error('allowedSighash cannot be an empty array');
167
+ const network = convertNetworkTypeForPsbt(this.currentNetwork);
168
+ const psbtFromBase64 = Psbt.fromBase64(request.unsignedPsbtBase64, {
169
+ network,
170
+ });
171
+ if ((_b = request.signature) === null || _b === void 0 ? void 0 : _b.length) {
172
+ validatePsbt(psbtFromBase64, request.allowedSighash, request.signature);
173
+ }
174
+ const inputsToSign = ((_d = (_c = request.signature) === null || _c === void 0 ? void 0 : _c.map((sig) => {
175
+ var _a;
176
+ return ((_a = sig.signingIndexes) !== null && _a !== void 0 ? _a : []).map((inputIndex) => ({
177
+ address: sig.address,
178
+ sigHash: getSigHashType(psbtFromBase64.data.inputs[inputIndex]),
179
+ signingIndexes: [inputIndex],
180
+ }));
181
+ })) !== null && _d !== void 0 ? _d : []).flat();
182
+ const signedPsbt = yield this.signTransaction({
183
+ broadcast: false,
184
+ inputsToSign,
185
+ psbtBase64: request.unsignedPsbtBase64,
186
+ });
187
+ if (!signedPsbt) {
188
+ throw new Error('Failed to sign transaction with sats-connect');
189
+ }
190
+ return { signedPsbt: signedPsbt.psbtBase64 };
191
+ });
192
+ }
128
193
  }
129
194
 
130
195
  export { BitcoinSatsConnectConnector };
@@ -9,8 +9,8 @@ var utils = require('@dynamic-labs/utils');
9
9
  var sdkApi = require('@dynamic-labs/sdk-api');
10
10
  var bitcoinProviderHelper = require('./bitcoinProviderHelper.cjs');
11
11
  var BitcoinLocalStorageCache = require('./BitcoinLocalStorageCache.cjs');
12
- var _const = require('./const.cjs');
13
12
  var getMempoolApiUrl = require('./utils/getMempoolApiUrl.cjs');
13
+ var _const = require('./const.cjs');
14
14
 
15
15
  class BitcoinWalletConnector extends walletConnectorCore.WalletConnectorBase {
16
16
  constructor(opts) {
@@ -5,7 +5,7 @@ import { Chain, IBitcoinWalletConnector, WalletConnectorBase } from '@dynamic-la
5
5
  import { WalletBookSchema, WalletSchema } from '@dynamic-labs/wallet-book';
6
6
  import { WalletAdditionalAddress } from '@dynamic-labs/sdk-api';
7
7
  import { IBitcoinSessionCache } from './BitcoinLocalStorageCache';
8
- import { BitcoinAddress, BitcoinTransaction } from './types';
8
+ import { BitcoinAddress, BitcoinTransaction, BitcoinSignPsbtRequest, BitcoinSignPsbtResponse } from './types';
9
9
  export type BitcoinWalletConnectorOpts = {
10
10
  walletBook: WalletBookSchema;
11
11
  walletData: WalletSchema;
@@ -30,6 +30,7 @@ export declare abstract class BitcoinWalletConnector extends WalletConnectorBase
30
30
  sendRawTransaction(rawTransaction: string): Promise<string | undefined>;
31
31
  abstract sendBitcoin(transaction: BitcoinTransaction): Promise<string | undefined>;
32
32
  getProvider<T>(): T & EventEmitter;
33
+ abstract signPsbt(request: BitcoinSignPsbtRequest): Promise<BitcoinSignPsbtResponse | undefined>;
33
34
  setConnectedAccountWithAddresses(mainAddress?: string, ordinalsAddress?: BitcoinAddress, paymentAddress?: BitcoinAddress, active?: boolean): Promise<void>;
34
35
  setupEventListeners(): void;
35
36
  }
@@ -5,8 +5,8 @@ import { DynamicError } from '@dynamic-labs/utils';
5
5
  import { WalletAddressType } from '@dynamic-labs/sdk-api';
6
6
  import { BitcoinProviderHelper } from './bitcoinProviderHelper.js';
7
7
  import { BitcoinLocalStorageCache } from './BitcoinLocalStorageCache.js';
8
- import { HTTP_STATUS_TOO_MANY_REQUESTS, HTTP_STATUS_NOT_FOUND } from './const.js';
9
8
  import { getMempoolApiUrl } from './utils/getMempoolApiUrl.js';
9
+ import { HTTP_STATUS_TOO_MANY_REQUESTS, HTTP_STATUS_NOT_FOUND } from './const.js';
10
10
 
11
11
  class BitcoinWalletConnector extends WalletConnectorBase {
12
12
  constructor(opts) {
@@ -3,7 +3,9 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var _tslib = require('../_virtual/_tslib.cjs');
6
+ var bitcoinjsLib = require('bitcoinjs-lib');
6
7
  var BitcoinWalletConnector = require('./BitcoinWalletConnector.cjs');
8
+ var createSignPsbtOptions = require('./utils/psbt/createSignPsbtOptions.cjs');
7
9
 
8
10
  class OkxConnector extends BitcoinWalletConnector.BitcoinWalletConnector {
9
11
  constructor(opts) {
@@ -45,6 +47,17 @@ class OkxConnector extends BitcoinWalletConnector.BitcoinWalletConnector {
45
47
  return provider.sendBitcoin(transaction.recipientAddress, transaction.amount);
46
48
  });
47
49
  }
50
+ signPsbt(request) {
51
+ return _tslib.__awaiter(this, void 0, void 0, function* () {
52
+ const provider = this.getProvider();
53
+ if (!provider) {
54
+ return;
55
+ }
56
+ const psbtFromBase64 = bitcoinjsLib.Psbt.fromBase64(request.unsignedPsbtBase64);
57
+ const signedPsbtHex = yield provider.signPsbt(psbtFromBase64.toHex(), createSignPsbtOptions.createPsbtOptions(psbtFromBase64, request));
58
+ return { signedPsbt: bitcoinjsLib.Psbt.fromHex(signedPsbtHex).toBase64() };
59
+ });
60
+ }
48
61
  }
49
62
 
50
63
  exports.OkxConnector = OkxConnector;
@@ -1,5 +1,5 @@
1
1
  import { BitcoinWalletConnector, BitcoinWalletConnectorOpts } from './BitcoinWalletConnector';
2
- import { BitcoinTransaction } from './types';
2
+ import { BitcoinTransaction, BitcoinSignPsbtRequest, BitcoinSignPsbtResponse } from './types';
3
3
  export declare class OkxConnector extends BitcoinWalletConnector {
4
4
  name: string;
5
5
  canFetchConnectedAccounts: boolean;
@@ -7,4 +7,5 @@ export declare class OkxConnector extends BitcoinWalletConnector {
7
7
  fetchPublicAddress(): Promise<string | undefined>;
8
8
  signMessage(messageToSign: string): Promise<string | undefined>;
9
9
  sendBitcoin(transaction: BitcoinTransaction): Promise<string | undefined>;
10
+ signPsbt(request: BitcoinSignPsbtRequest): Promise<BitcoinSignPsbtResponse | undefined>;
10
11
  }
@@ -1,5 +1,7 @@
1
1
  import { __awaiter } from '../_virtual/_tslib.js';
2
+ import { Psbt } from 'bitcoinjs-lib';
2
3
  import { BitcoinWalletConnector } from './BitcoinWalletConnector.js';
4
+ import { createPsbtOptions } from './utils/psbt/createSignPsbtOptions.js';
3
5
 
4
6
  class OkxConnector extends BitcoinWalletConnector {
5
7
  constructor(opts) {
@@ -41,6 +43,17 @@ class OkxConnector extends BitcoinWalletConnector {
41
43
  return provider.sendBitcoin(transaction.recipientAddress, transaction.amount);
42
44
  });
43
45
  }
46
+ signPsbt(request) {
47
+ return __awaiter(this, void 0, void 0, function* () {
48
+ const provider = this.getProvider();
49
+ if (!provider) {
50
+ return;
51
+ }
52
+ const psbtFromBase64 = Psbt.fromBase64(request.unsignedPsbtBase64);
53
+ const signedPsbtHex = yield provider.signPsbt(psbtFromBase64.toHex(), createPsbtOptions(psbtFromBase64, request));
54
+ return { signedPsbt: Psbt.fromHex(signedPsbtHex).toBase64() };
55
+ });
56
+ }
44
57
  }
45
58
 
46
59
  export { OkxConnector };
@@ -3,7 +3,9 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var _tslib = require('../_virtual/_tslib.cjs');
6
+ var bitcoinjsLib = require('bitcoinjs-lib');
6
7
  var BitcoinWalletConnector = require('./BitcoinWalletConnector.cjs');
8
+ var createSignPsbtOptions = require('./utils/psbt/createSignPsbtOptions.cjs');
7
9
 
8
10
  class UnisatConnector extends BitcoinWalletConnector.BitcoinWalletConnector {
9
11
  constructor(opts) {
@@ -46,6 +48,17 @@ class UnisatConnector extends BitcoinWalletConnector.BitcoinWalletConnector {
46
48
  return provider.sendBitcoin(transaction.recipientAddress, transaction.amount);
47
49
  });
48
50
  }
51
+ signPsbt(request) {
52
+ return _tslib.__awaiter(this, void 0, void 0, function* () {
53
+ const provider = this.getProvider();
54
+ if (!provider) {
55
+ return;
56
+ }
57
+ const psbtFromBase64 = bitcoinjsLib.Psbt.fromBase64(request.unsignedPsbtBase64);
58
+ const signedPsbtHex = yield provider.signPsbt(psbtFromBase64.toHex(), createSignPsbtOptions.createPsbtOptions(psbtFromBase64, request));
59
+ return { signedPsbt: bitcoinjsLib.Psbt.fromHex(signedPsbtHex).toBase64() };
60
+ });
61
+ }
49
62
  }
50
63
 
51
64
  exports.UnisatConnector = UnisatConnector;
@@ -1,5 +1,5 @@
1
1
  import { BitcoinWalletConnector, BitcoinWalletConnectorOpts } from './BitcoinWalletConnector';
2
- import { BitcoinTransaction } from './types';
2
+ import { BitcoinTransaction, BitcoinSignPsbtRequest, BitcoinSignPsbtResponse } from './types';
3
3
  export declare class UnisatConnector extends BitcoinWalletConnector {
4
4
  name: string;
5
5
  canFetchConnectedAccounts: boolean;
@@ -7,4 +7,5 @@ export declare class UnisatConnector extends BitcoinWalletConnector {
7
7
  fetchPublicAddress(): Promise<string | undefined>;
8
8
  signMessage(messageToSign: string): Promise<string | undefined>;
9
9
  sendBitcoin(transaction: BitcoinTransaction): Promise<string | undefined>;
10
+ signPsbt(request: BitcoinSignPsbtRequest): Promise<BitcoinSignPsbtResponse | undefined>;
10
11
  }
@@ -1,5 +1,7 @@
1
1
  import { __awaiter } from '../_virtual/_tslib.js';
2
+ import { Psbt } from 'bitcoinjs-lib';
2
3
  import { BitcoinWalletConnector } from './BitcoinWalletConnector.js';
4
+ import { createPsbtOptions } from './utils/psbt/createSignPsbtOptions.js';
3
5
 
4
6
  class UnisatConnector extends BitcoinWalletConnector {
5
7
  constructor(opts) {
@@ -42,6 +44,17 @@ class UnisatConnector extends BitcoinWalletConnector {
42
44
  return provider.sendBitcoin(transaction.recipientAddress, transaction.amount);
43
45
  });
44
46
  }
47
+ signPsbt(request) {
48
+ return __awaiter(this, void 0, void 0, function* () {
49
+ const provider = this.getProvider();
50
+ if (!provider) {
51
+ return;
52
+ }
53
+ const psbtFromBase64 = Psbt.fromBase64(request.unsignedPsbtBase64);
54
+ const signedPsbtHex = yield provider.signPsbt(psbtFromBase64.toHex(), createPsbtOptions(psbtFromBase64, request));
55
+ return { signedPsbt: Psbt.fromHex(signedPsbtHex).toBase64() };
56
+ });
57
+ }
45
58
  }
46
59
 
47
60
  export { UnisatConnector };
package/src/types.d.ts CHANGED
@@ -1,4 +1,5 @@
1
- import { BitcoinProvider } from 'sats-connect';
1
+ import { BitcoinProvider, InputToSign } from 'sats-connect';
2
+ import { SignPsbtResponse as BtcKitSignPsbtResponse } from '@btckit/types';
2
3
  import type { WalletAdditionalAddress } from '@dynamic-labs/sdk-api';
3
4
  import { SATSCONNECT_FEATURE } from './const';
4
5
  export type ErrorWithCode = Error & {
@@ -21,14 +22,36 @@ export type BitcoinTransaction = {
21
22
  amount: bigint;
22
23
  recipientAddress: string;
23
24
  };
24
- export type SignPbstOptions = {
25
+ export type SignPsbtOptions = {
25
26
  autoFinalized: boolean;
26
- toSignInputs: SignPbstInput[];
27
+ toSignInputs?: {
28
+ index: number;
29
+ address: string;
30
+ sighashTypes?: number[];
31
+ disableAddressValidation?: boolean;
32
+ }[];
27
33
  };
28
- export type SignPbstInput = {
29
- index: number;
34
+ export type BitcoinSignPsbtRequest = {
35
+ allowedSighash: number[];
36
+ unsignedPsbtBase64: string;
37
+ signature?: BitcoinSignPsbtRequestSignature[];
38
+ };
39
+ export type BitcoinSignPsbtRequestSignature = {
30
40
  address: string;
31
- publicKey: string;
32
- sighashTypes?: number[];
33
- disableTweakSigner?: boolean;
41
+ signingIndexes: number[] | undefined;
42
+ disableAddressValidation?: boolean;
43
+ };
44
+ export type SatsConnectSignTransactionInput = {
45
+ message?: string;
46
+ psbtBase64: string;
47
+ inputsToSign: InputToSign[];
48
+ broadcast?: boolean;
49
+ };
50
+ export type BtcKitSignPsbtResponseExtended = BtcKitSignPsbtResponse & {
51
+ result: {
52
+ hex: string;
53
+ };
54
+ };
55
+ export type BitcoinSignPsbtResponse = {
56
+ signedPsbt: string;
34
57
  };
@@ -0,0 +1,13 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var satsConnect = require('sats-connect');
6
+ var bitcoinjsLib = require('bitcoinjs-lib');
7
+
8
+ const convertNetworkTypeForPsbt = (network) => ({
9
+ [satsConnect.BitcoinNetworkType.Mainnet]: bitcoinjsLib.networks.bitcoin,
10
+ [satsConnect.BitcoinNetworkType.Testnet]: bitcoinjsLib.networks.testnet,
11
+ }[network]);
12
+
13
+ exports.convertNetworkTypeForPsbt = convertNetworkTypeForPsbt;
@@ -0,0 +1,3 @@
1
+ import { BitcoinNetworkType } from 'sats-connect';
2
+ import { networks } from 'bitcoinjs-lib';
3
+ export declare const convertNetworkTypeForPsbt: (network: BitcoinNetworkType) => networks.Network;
@@ -0,0 +1,9 @@
1
+ import { BitcoinNetworkType } from 'sats-connect';
2
+ import { networks } from 'bitcoinjs-lib';
3
+
4
+ const convertNetworkTypeForPsbt = (network) => ({
5
+ [BitcoinNetworkType.Mainnet]: networks.bitcoin,
6
+ [BitcoinNetworkType.Testnet]: networks.testnet,
7
+ }[network]);
8
+
9
+ export { convertNetworkTypeForPsbt };
@@ -0,0 +1,33 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var validatePsbt = require('./validator/validatePsbt.cjs');
6
+ require('bitcoinjs-lib');
7
+ require('@dynamic-labs/wallet-connector-core');
8
+ require('sats-connect');
9
+
10
+ const createPsbtOptions = (psbt, request) => {
11
+ var _a;
12
+ const psbtSignOptions = {
13
+ autoFinalized: false,
14
+ };
15
+ if (request.signature) {
16
+ validatePsbt.validatePsbt(psbt, request.allowedSighash, request.signature);
17
+ const toSignInputs = [];
18
+ for (const signature of request.signature) {
19
+ if ((_a = signature.signingIndexes) === null || _a === void 0 ? void 0 : _a.length) {
20
+ toSignInputs.push({
21
+ address: signature.address,
22
+ disableAddressValidation: signature.disableAddressValidation,
23
+ index: signature.signingIndexes[0],
24
+ sighashTypes: request.allowedSighash,
25
+ });
26
+ }
27
+ }
28
+ psbtSignOptions.toSignInputs = toSignInputs;
29
+ }
30
+ return psbtSignOptions;
31
+ };
32
+
33
+ exports.createPsbtOptions = createPsbtOptions;
@@ -0,0 +1,3 @@
1
+ import { Psbt } from 'bitcoinjs-lib';
2
+ import { BitcoinSignPsbtRequest, SignPsbtOptions } from '../../types';
3
+ export declare const createPsbtOptions: (psbt: Psbt, request: BitcoinSignPsbtRequest) => SignPsbtOptions;
@@ -0,0 +1,29 @@
1
+ import { validatePsbt } from './validator/validatePsbt.js';
2
+ import 'bitcoinjs-lib';
3
+ import '@dynamic-labs/wallet-connector-core';
4
+ import 'sats-connect';
5
+
6
+ const createPsbtOptions = (psbt, request) => {
7
+ var _a;
8
+ const psbtSignOptions = {
9
+ autoFinalized: false,
10
+ };
11
+ if (request.signature) {
12
+ validatePsbt(psbt, request.allowedSighash, request.signature);
13
+ const toSignInputs = [];
14
+ for (const signature of request.signature) {
15
+ if ((_a = signature.signingIndexes) === null || _a === void 0 ? void 0 : _a.length) {
16
+ toSignInputs.push({
17
+ address: signature.address,
18
+ disableAddressValidation: signature.disableAddressValidation,
19
+ index: signature.signingIndexes[0],
20
+ sighashTypes: request.allowedSighash,
21
+ });
22
+ }
23
+ }
24
+ psbtSignOptions.toSignInputs = toSignInputs;
25
+ }
26
+ return psbtSignOptions;
27
+ };
28
+
29
+ export { createPsbtOptions };
@@ -0,0 +1,34 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var bitcoinjsLib = require('bitcoinjs-lib');
6
+
7
+ /**
8
+ * This method extracts the address from the input data of the psbt.
9
+ * The address is stored as script (Buffer) in either the witnessUtxo or nonWitnessUtxo as an output
10
+ */
11
+ const extractAddressFromInput = (psbt, index, input) => {
12
+ var _a;
13
+ let script;
14
+ if ((_a = input.witnessUtxo) === null || _a === void 0 ? void 0 : _a.script) {
15
+ // eslint-disable-next-line prefer-destructuring
16
+ script = input.witnessUtxo.script;
17
+ }
18
+ else if (input.nonWitnessUtxo) {
19
+ const nonWitnessTxn = bitcoinjsLib.Transaction.fromBuffer(input.nonWitnessUtxo);
20
+ const txOut = nonWitnessTxn.outs[psbt.txInputs[index].index];
21
+ // eslint-disable-next-line prefer-destructuring
22
+ script = txOut.script;
23
+ }
24
+ if (!script)
25
+ return;
26
+ try {
27
+ return bitcoinjsLib.address.fromOutputScript(script);
28
+ }
29
+ catch (e) {
30
+ throw new Error('address not found in output script');
31
+ }
32
+ };
33
+
34
+ exports.extractAddressFromInput = extractAddressFromInput;
@@ -0,0 +1,6 @@
1
+ import { Psbt } from 'bitcoinjs-lib';
2
+ /**
3
+ * This method extracts the address from the input data of the psbt.
4
+ * The address is stored as script (Buffer) in either the witnessUtxo or nonWitnessUtxo as an output
5
+ */
6
+ export declare const extractAddressFromInput: (psbt: Psbt, index: number, input: Psbt['data']['inputs'][0]) => string | undefined;
@@ -0,0 +1,30 @@
1
+ import { Transaction, address } from 'bitcoinjs-lib';
2
+
3
+ /**
4
+ * This method extracts the address from the input data of the psbt.
5
+ * The address is stored as script (Buffer) in either the witnessUtxo or nonWitnessUtxo as an output
6
+ */
7
+ const extractAddressFromInput = (psbt, index, input) => {
8
+ var _a;
9
+ let script;
10
+ if ((_a = input.witnessUtxo) === null || _a === void 0 ? void 0 : _a.script) {
11
+ // eslint-disable-next-line prefer-destructuring
12
+ script = input.witnessUtxo.script;
13
+ }
14
+ else if (input.nonWitnessUtxo) {
15
+ const nonWitnessTxn = Transaction.fromBuffer(input.nonWitnessUtxo);
16
+ const txOut = nonWitnessTxn.outs[psbt.txInputs[index].index];
17
+ // eslint-disable-next-line prefer-destructuring
18
+ script = txOut.script;
19
+ }
20
+ if (!script)
21
+ return;
22
+ try {
23
+ return address.fromOutputScript(script);
24
+ }
25
+ catch (e) {
26
+ throw new Error('address not found in output script');
27
+ }
28
+ };
29
+
30
+ export { extractAddressFromInput };
@@ -0,0 +1,35 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var bitcoinjsLib = require('bitcoinjs-lib');
6
+ var walletConnectorCore = require('@dynamic-labs/wallet-connector-core');
7
+
8
+ /**
9
+ * This method will return the signature hash type for the current input.
10
+ * If there is a sighashType, it will return that
11
+ * If there is a witnessUtxo AND it is a taproot address, then it will return SIGHASH_DEFAULT
12
+ * Otherwise, it will return SIGHASH_ALL
13
+ */
14
+ const getSigHashType = (input) => {
15
+ var _a;
16
+ if (input === null || input === void 0 ? void 0 : input.sighashType)
17
+ return input.sighashType;
18
+ let isTaprootAddress = false;
19
+ if ((_a = input.witnessUtxo) === null || _a === void 0 ? void 0 : _a.script) {
20
+ try {
21
+ bitcoinjsLib.payments.p2tr({
22
+ output: input.witnessUtxo.script,
23
+ });
24
+ isTaprootAddress = true;
25
+ }
26
+ catch (_) {
27
+ walletConnectorCore.logger.debug('address is not taproot');
28
+ }
29
+ }
30
+ return isTaprootAddress
31
+ ? bitcoinjsLib.Transaction.SIGHASH_DEFAULT
32
+ : bitcoinjsLib.Transaction.SIGHASH_ALL;
33
+ };
34
+
35
+ exports.getSigHashType = getSigHashType;
@@ -0,0 +1,8 @@
1
+ import { Psbt } from 'bitcoinjs-lib';
2
+ /**
3
+ * This method will return the signature hash type for the current input.
4
+ * If there is a sighashType, it will return that
5
+ * If there is a witnessUtxo AND it is a taproot address, then it will return SIGHASH_DEFAULT
6
+ * Otherwise, it will return SIGHASH_ALL
7
+ */
8
+ export declare const getSigHashType: (input: Psbt['data']['inputs'][0]) => number;
@@ -0,0 +1,31 @@
1
+ import { payments, Transaction } from 'bitcoinjs-lib';
2
+ import { logger } from '@dynamic-labs/wallet-connector-core';
3
+
4
+ /**
5
+ * This method will return the signature hash type for the current input.
6
+ * If there is a sighashType, it will return that
7
+ * If there is a witnessUtxo AND it is a taproot address, then it will return SIGHASH_DEFAULT
8
+ * Otherwise, it will return SIGHASH_ALL
9
+ */
10
+ const getSigHashType = (input) => {
11
+ var _a;
12
+ if (input === null || input === void 0 ? void 0 : input.sighashType)
13
+ return input.sighashType;
14
+ let isTaprootAddress = false;
15
+ if ((_a = input.witnessUtxo) === null || _a === void 0 ? void 0 : _a.script) {
16
+ try {
17
+ payments.p2tr({
18
+ output: input.witnessUtxo.script,
19
+ });
20
+ isTaprootAddress = true;
21
+ }
22
+ catch (_) {
23
+ logger.debug('address is not taproot');
24
+ }
25
+ }
26
+ return isTaprootAddress
27
+ ? Transaction.SIGHASH_DEFAULT
28
+ : Transaction.SIGHASH_ALL;
29
+ };
30
+
31
+ export { getSigHashType };
@@ -0,0 +1,4 @@
1
+ export { validatePsbt } from './validator/validatePsbt';
2
+ export { getSigHashType } from './getSigHashType';
3
+ export { extractAddressFromInput } from './extractAddressFromInput';
4
+ export { convertNetworkTypeForPsbt } from './bitcoinNetworkTypeToNetworks';
@@ -0,0 +1 @@
1
+ export { validatePsbt } from './validatePsbt';
@@ -0,0 +1,18 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var utils = require('@dynamic-labs/utils');
6
+ var extractAddressFromInput = require('../extractAddressFromInput.cjs');
7
+
8
+ const validateAddress = (psbt, signingAddress, index, inputAtIndex) => {
9
+ // we need to extract the address from the input at the current signing index
10
+ // to be able to compare it to the address provided in the request for the current index
11
+ const extractedAddress = extractAddressFromInput.extractAddressFromInput(psbt, index, inputAtIndex);
12
+ if (!extractedAddress)
13
+ throw new utils.DynamicError(`validateAddress - input not found for the given address: ${signingAddress}`);
14
+ if (extractedAddress !== signingAddress)
15
+ throw new utils.DynamicError('validateAddress - address mismatch with index');
16
+ };
17
+
18
+ exports.validateAddress = validateAddress;
@@ -0,0 +1,2 @@
1
+ import { Psbt } from 'bitcoinjs-lib';
2
+ export declare const validateAddress: (psbt: Psbt, signingAddress: string, index: number, inputAtIndex: Psbt['data']['inputs'][0]) => void;
@@ -0,0 +1,14 @@
1
+ import { DynamicError } from '@dynamic-labs/utils';
2
+ import { extractAddressFromInput } from '../extractAddressFromInput.js';
3
+
4
+ const validateAddress = (psbt, signingAddress, index, inputAtIndex) => {
5
+ // we need to extract the address from the input at the current signing index
6
+ // to be able to compare it to the address provided in the request for the current index
7
+ const extractedAddress = extractAddressFromInput(psbt, index, inputAtIndex);
8
+ if (!extractedAddress)
9
+ throw new DynamicError(`validateAddress - input not found for the given address: ${signingAddress}`);
10
+ if (extractedAddress !== signingAddress)
11
+ throw new DynamicError('validateAddress - address mismatch with index');
12
+ };
13
+
14
+ export { validateAddress };
@@ -0,0 +1,40 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var validateAddress = require('./validateAddress.cjs');
6
+ var validateSigHash = require('./validateSigHash.cjs');
7
+
8
+ /**
9
+ * This method will validate the psbt against the signature data provided in the request
10
+ * It checks 3 things:
11
+ * - That the signing index exists in the input
12
+ * - That the address provided in the request matches the address in the input,
13
+ * unless the disableAddressValidation flag is set
14
+ * - That the sigHashType of the input is a member of the allowedSigHashTypes array
15
+ */
16
+ const validatePsbt = (psbt, allowedSigHashTypes, signatureData) => {
17
+ if (!(signatureData === null || signatureData === void 0 ? void 0 : signatureData.length))
18
+ return;
19
+ for (const input of signatureData) {
20
+ const { address: signingAddress, signingIndexes,
21
+ // request from ME in cases of multi-sig. Steven wanted this defaulted to true
22
+ disableAddressValidation = true, } = input;
23
+ if (!(signingIndexes === null || signingIndexes === void 0 ? void 0 : signingIndexes.length))
24
+ return;
25
+ // address is a required input
26
+ if (!signingAddress)
27
+ throw new Error('validatePsbt - address not provided');
28
+ for (const index of signingIndexes) {
29
+ const selectedInput = psbt.data.inputs[index];
30
+ if (!selectedInput)
31
+ throw new Error('validatePsbt - requested input not found');
32
+ if (!disableAddressValidation) {
33
+ validateAddress.validateAddress(psbt, signingAddress, index, selectedInput);
34
+ }
35
+ validateSigHash.validateSigHash(allowedSigHashTypes, selectedInput);
36
+ }
37
+ }
38
+ };
39
+
40
+ exports.validatePsbt = validatePsbt;
@@ -0,0 +1,11 @@
1
+ import { Psbt } from 'bitcoinjs-lib';
2
+ import { BitcoinSignPsbtRequestSignature } from '../../../types';
3
+ /**
4
+ * This method will validate the psbt against the signature data provided in the request
5
+ * It checks 3 things:
6
+ * - That the signing index exists in the input
7
+ * - That the address provided in the request matches the address in the input,
8
+ * unless the disableAddressValidation flag is set
9
+ * - That the sigHashType of the input is a member of the allowedSigHashTypes array
10
+ */
11
+ export declare const validatePsbt: (psbt: Psbt, allowedSigHashTypes: number[], signatureData?: BitcoinSignPsbtRequestSignature[]) => void;
@@ -0,0 +1,36 @@
1
+ import { validateAddress } from './validateAddress.js';
2
+ import { validateSigHash } from './validateSigHash.js';
3
+
4
+ /**
5
+ * This method will validate the psbt against the signature data provided in the request
6
+ * It checks 3 things:
7
+ * - That the signing index exists in the input
8
+ * - That the address provided in the request matches the address in the input,
9
+ * unless the disableAddressValidation flag is set
10
+ * - That the sigHashType of the input is a member of the allowedSigHashTypes array
11
+ */
12
+ const validatePsbt = (psbt, allowedSigHashTypes, signatureData) => {
13
+ if (!(signatureData === null || signatureData === void 0 ? void 0 : signatureData.length))
14
+ return;
15
+ for (const input of signatureData) {
16
+ const { address: signingAddress, signingIndexes,
17
+ // request from ME in cases of multi-sig. Steven wanted this defaulted to true
18
+ disableAddressValidation = true, } = input;
19
+ if (!(signingIndexes === null || signingIndexes === void 0 ? void 0 : signingIndexes.length))
20
+ return;
21
+ // address is a required input
22
+ if (!signingAddress)
23
+ throw new Error('validatePsbt - address not provided');
24
+ for (const index of signingIndexes) {
25
+ const selectedInput = psbt.data.inputs[index];
26
+ if (!selectedInput)
27
+ throw new Error('validatePsbt - requested input not found');
28
+ if (!disableAddressValidation) {
29
+ validateAddress(psbt, signingAddress, index, selectedInput);
30
+ }
31
+ validateSigHash(allowedSigHashTypes, selectedInput);
32
+ }
33
+ }
34
+ };
35
+
36
+ export { validatePsbt };
@@ -0,0 +1,15 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var getSigHashType = require('../getSigHashType.cjs');
6
+
7
+ const validateSigHash = (allowedSigHashTypes, selectedInput) => {
8
+ const sigHashType = getSigHashType.getSigHashType(selectedInput);
9
+ // if the request has allowedSigHashTypes, then we need to make sure that the sigHashType
10
+ // is a member of that array before continuing
11
+ if ((allowedSigHashTypes === null || allowedSigHashTypes === void 0 ? void 0 : allowedSigHashTypes.length) && !allowedSigHashTypes.includes(sigHashType))
12
+ throw new Error(`validatePsbt - sigHashType ${sigHashType} not in allowed list`);
13
+ };
14
+
15
+ exports.validateSigHash = validateSigHash;
@@ -0,0 +1,2 @@
1
+ import { Psbt } from 'bitcoinjs-lib';
2
+ export declare const validateSigHash: (allowedSigHashTypes: number[], selectedInput: Psbt['data']['inputs'][0]) => void;
@@ -0,0 +1,11 @@
1
+ import { getSigHashType } from '../getSigHashType.js';
2
+
3
+ const validateSigHash = (allowedSigHashTypes, selectedInput) => {
4
+ const sigHashType = getSigHashType(selectedInput);
5
+ // if the request has allowedSigHashTypes, then we need to make sure that the sigHashType
6
+ // is a member of that array before continuing
7
+ if ((allowedSigHashTypes === null || allowedSigHashTypes === void 0 ? void 0 : allowedSigHashTypes.length) && !allowedSigHashTypes.includes(sigHashType))
8
+ throw new Error(`validatePsbt - sigHashType ${sigHashType} not in allowed list`);
9
+ };
10
+
11
+ export { validateSigHash };
@@ -6,6 +6,7 @@ var _const = require('../const.cjs');
6
6
  require('@dynamic-labs/utils');
7
7
  require('../../_virtual/_tslib.cjs');
8
8
  require('sats-connect');
9
+ require('bitcoinjs-lib');
9
10
  require('@dynamic-labs/wallet-connector-core');
10
11
  require('@dynamic-labs/wallet-book');
11
12
  require('@dynamic-labs/sdk-api');
@@ -2,6 +2,7 @@ import { XVERSE_KEY } from '../const.js';
2
2
  import '@dynamic-labs/utils';
3
3
  import '../../_virtual/_tslib.js';
4
4
  import 'sats-connect';
5
+ import 'bitcoinjs-lib';
5
6
  import '@dynamic-labs/wallet-connector-core';
6
7
  import '@dynamic-labs/wallet-book';
7
8
  import '@dynamic-labs/sdk-api';