@dynamic-labs-wallet/btc 1.1.69 → 1.1.71

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/index.cjs CHANGED
@@ -894,4 +894,69 @@ class DynamicBtcWalletClient extends browser.DynamicWalletClient {
894
894
  }
895
895
  }
896
896
 
897
+ const { SIGHASH_ALL, SIGHASH_DEFAULT } = bitcoin__namespace.Transaction;
898
+ const isP2trScript = (script)=>script.length === 34 && script[0] === 0x51 && script[1] === 0x20;
899
+ // Only these types commit to every output, so only they let one approval stand
900
+ // for the whole transaction: SIGHASH_DEFAULT and SIGHASH_ALL for Taproot, SIGHASH_ALL otherwise.
901
+ const committingSighashTypes = (isTaproot)=>isTaproot ? [
902
+ SIGHASH_DEFAULT,
903
+ SIGHASH_ALL
904
+ ] : [
905
+ SIGHASH_ALL
906
+ ];
907
+ /**
908
+ * Describes a PSBT for a signing-quorum intent: its txid, fee, outputs and the
909
+ * sighashes of the inputs the wallet signs, so one approval covers a signature
910
+ * for each of them.
911
+ *
912
+ * The inputs are chosen as `signTransaction` chooses them: the ones that belong
913
+ * to `accountAddress`, narrowed to `signingIndexes` when given. P2WSH co-signing
914
+ * is not part of the intent, so `allowedWitnessScripts` is rejected.
915
+ */ const prepareBtcTransactionSigningOperation = ({ transaction, accountAddress, bitcoinConfig, signingIndexes, allowedSighash, allowedWitnessScripts })=>{
916
+ btcUtils.initEccLib(ecc);
917
+ if (allowedWitnessScripts == null ? void 0 : allowedWitnessScripts.length) throw new Error('BTC signing intent does not support P2WSH co-signing');
918
+ var _bitcoinConfig_network;
919
+ const network = btcUtils.toBitcoinNetwork((_bitcoinConfig_network = bitcoinConfig == null ? void 0 : bitcoinConfig.network) != null ? _bitcoinConfig_network : 'mainnet');
920
+ const psbt = bitcoin__namespace.Psbt.fromBase64(transaction);
921
+ const ownedInputs = psbt.data.inputs.map((input, index)=>({
922
+ input,
923
+ index
924
+ })).filter(({ input })=>btcUtils.doesInputBelongToAddress(input, accountAddress, network));
925
+ const inputsToSign = btcUtils.filterInputsBySigningIndexes(ownedInputs, signingIndexes);
926
+ if (inputsToSign.length === 0) throw new Error('No input of the transaction belongs to the signing account');
927
+ inputsToSign.forEach(({ input, index })=>{
928
+ const isTaproot = Boolean(input.tapInternalKey);
929
+ if (!isTaproot && input.witnessUtxo && isP2trScript(input.witnessUtxo.script)) {
930
+ throw new Error(`Input ${index} is a Taproot input without tapInternalKey`);
931
+ }
932
+ btcUtils.assertSighashAllowed([
933
+ {
934
+ input,
935
+ index
936
+ }
937
+ ], isTaproot, allowedSighash);
938
+ const sighashType = btcUtils.getSigHashType(input, isTaproot);
939
+ if (!committingSighashTypes(isTaproot).includes(sighashType)) {
940
+ throw new Error(`Input ${index} declares sighash type ${sighashType}, which a signing intent does not allow`);
941
+ }
942
+ });
943
+ const { txid, feeSats, outputs, sighashes } = btcUtils.extractPsbtIntentOperation({
944
+ psbtBase64: transaction,
945
+ network,
946
+ inputIndexes: inputsToSign.map(({ index })=>index)
947
+ });
948
+ return {
949
+ operation: {
950
+ kind: 'transaction',
951
+ chainName: 'BTC',
952
+ chainId: btcUtils.getBtcChainId(network),
953
+ txid,
954
+ feeSats,
955
+ outputs,
956
+ sighashes
957
+ }
958
+ };
959
+ };
960
+
897
961
  exports.DynamicBtcWalletClient = DynamicBtcWalletClient;
962
+ exports.prepareBtcTransactionSigningOperation = prepareBtcTransactionSigningOperation;
package/index.esm.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { DynamicWalletClient, BitcoinNetwork, BitcoinAddressType, getMPCChainConfig, ERROR_CREATE_WALLET_ACCOUNT, ERROR_PASSWORD_MISMATCH, ERROR_MULTIPLE_WALLETS_PER_CHAIN, ERROR_SIGN_MESSAGE, ERROR_ACCOUNT_ADDRESS_REQUIRED, ERROR_IMPORT_PRIVATE_KEY } from '@dynamic-labs-wallet/browser';
2
2
  import * as bitcoin from 'bitcoinjs-lib';
3
3
  import ecc from '@bitcoinerlab/secp256k1';
4
- import { extractPublicKeyHex, normalizePublicKey, publicKeyToBitcoinAddress, resolveBitcoinAddressType, calculateTaprootTweak, encodeBip322Signature, calculateBip322Hash, wifToPrivateKey, getPublicKeyFromPrivateKey, privateKeyToWIF, doesInputBelongToAddress, filterInputsBySigningIndexes, assertSighashAllowed, collectPSBTInputData, getSigHashType, convertSignatureToTaprootBuffer, getSegwitV0ScriptCode, getBitcoinNetwork, convertSignatureToDER, getFeeRates, getUTXOs, selectUTXOs, resolveRpcUrl, rpcFetch, initEccLib } from '@dynamic-labs-wallet/btc-utils';
4
+ import { extractPublicKeyHex, normalizePublicKey, publicKeyToBitcoinAddress, resolveBitcoinAddressType, calculateTaprootTweak, encodeBip322Signature, calculateBip322Hash, wifToPrivateKey, getPublicKeyFromPrivateKey, privateKeyToWIF, doesInputBelongToAddress, filterInputsBySigningIndexes, assertSighashAllowed, collectPSBTInputData, getSigHashType, convertSignatureToTaprootBuffer, getSegwitV0ScriptCode, getBitcoinNetwork, convertSignatureToDER, getFeeRates, getUTXOs, selectUTXOs, resolveRpcUrl, rpcFetch, initEccLib, toBitcoinNetwork, extractPsbtIntentOperation, getBtcChainId } from '@dynamic-labs-wallet/btc-utils';
5
5
 
6
6
  function _extends() {
7
7
  _extends = Object.assign || function assign(target) {
@@ -873,4 +873,68 @@ class DynamicBtcWalletClient extends DynamicWalletClient {
873
873
  }
874
874
  }
875
875
 
876
- export { DynamicBtcWalletClient };
876
+ const { SIGHASH_ALL, SIGHASH_DEFAULT } = bitcoin.Transaction;
877
+ const isP2trScript = (script)=>script.length === 34 && script[0] === 0x51 && script[1] === 0x20;
878
+ // Only these types commit to every output, so only they let one approval stand
879
+ // for the whole transaction: SIGHASH_DEFAULT and SIGHASH_ALL for Taproot, SIGHASH_ALL otherwise.
880
+ const committingSighashTypes = (isTaproot)=>isTaproot ? [
881
+ SIGHASH_DEFAULT,
882
+ SIGHASH_ALL
883
+ ] : [
884
+ SIGHASH_ALL
885
+ ];
886
+ /**
887
+ * Describes a PSBT for a signing-quorum intent: its txid, fee, outputs and the
888
+ * sighashes of the inputs the wallet signs, so one approval covers a signature
889
+ * for each of them.
890
+ *
891
+ * The inputs are chosen as `signTransaction` chooses them: the ones that belong
892
+ * to `accountAddress`, narrowed to `signingIndexes` when given. P2WSH co-signing
893
+ * is not part of the intent, so `allowedWitnessScripts` is rejected.
894
+ */ const prepareBtcTransactionSigningOperation = ({ transaction, accountAddress, bitcoinConfig, signingIndexes, allowedSighash, allowedWitnessScripts })=>{
895
+ initEccLib(ecc);
896
+ if (allowedWitnessScripts == null ? void 0 : allowedWitnessScripts.length) throw new Error('BTC signing intent does not support P2WSH co-signing');
897
+ var _bitcoinConfig_network;
898
+ const network = toBitcoinNetwork((_bitcoinConfig_network = bitcoinConfig == null ? void 0 : bitcoinConfig.network) != null ? _bitcoinConfig_network : 'mainnet');
899
+ const psbt = bitcoin.Psbt.fromBase64(transaction);
900
+ const ownedInputs = psbt.data.inputs.map((input, index)=>({
901
+ input,
902
+ index
903
+ })).filter(({ input })=>doesInputBelongToAddress(input, accountAddress, network));
904
+ const inputsToSign = filterInputsBySigningIndexes(ownedInputs, signingIndexes);
905
+ if (inputsToSign.length === 0) throw new Error('No input of the transaction belongs to the signing account');
906
+ inputsToSign.forEach(({ input, index })=>{
907
+ const isTaproot = Boolean(input.tapInternalKey);
908
+ if (!isTaproot && input.witnessUtxo && isP2trScript(input.witnessUtxo.script)) {
909
+ throw new Error(`Input ${index} is a Taproot input without tapInternalKey`);
910
+ }
911
+ assertSighashAllowed([
912
+ {
913
+ input,
914
+ index
915
+ }
916
+ ], isTaproot, allowedSighash);
917
+ const sighashType = getSigHashType(input, isTaproot);
918
+ if (!committingSighashTypes(isTaproot).includes(sighashType)) {
919
+ throw new Error(`Input ${index} declares sighash type ${sighashType}, which a signing intent does not allow`);
920
+ }
921
+ });
922
+ const { txid, feeSats, outputs, sighashes } = extractPsbtIntentOperation({
923
+ psbtBase64: transaction,
924
+ network,
925
+ inputIndexes: inputsToSign.map(({ index })=>index)
926
+ });
927
+ return {
928
+ operation: {
929
+ kind: 'transaction',
930
+ chainName: 'BTC',
931
+ chainId: getBtcChainId(network),
932
+ txid,
933
+ feeSats,
934
+ outputs,
935
+ sighashes
936
+ }
937
+ };
938
+ };
939
+
940
+ export { DynamicBtcWalletClient, prepareBtcTransactionSigningOperation };
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@dynamic-labs-wallet/btc",
3
- "version": "1.1.69",
3
+ "version": "1.1.71",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "sideEffects": false,
7
7
  "dependencies": {
8
8
  "@dynamic-labs/sdk-api-core": "0.167.0",
9
- "@dynamic-labs-wallet/browser": "1.1.69",
10
- "@dynamic-labs-wallet/btc-utils": "1.1.69",
9
+ "@dynamic-labs-wallet/browser": "1.1.71",
10
+ "@dynamic-labs-wallet/btc-utils": "1.1.71",
11
11
  "@bitcoinerlab/secp256k1": "^1.2.0",
12
12
  "bitcoinjs-lib": "^7.0.2"
13
13
  },
package/src/index.d.ts CHANGED
@@ -1,2 +1,3 @@
1
1
  export * from './client/client.js';
2
+ export { prepareBtcTransactionSigningOperation } from './signingIntent.js';
2
3
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../packages/src/index.ts"],"names":[],"mappings":"AAAA,cAAc,oBAAoB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../packages/src/index.ts"],"names":[],"mappings":"AAAA,cAAc,oBAAoB,CAAC;AACnC,OAAO,EAAE,qCAAqC,EAAE,MAAM,oBAAoB,CAAC"}
@@ -0,0 +1,19 @@
1
+ import type { BitcoinConfig, PreparedBtcTransactionSigningIntent } from '@dynamic-labs-wallet/browser';
2
+ /**
3
+ * Describes a PSBT for a signing-quorum intent: its txid, fee, outputs and the
4
+ * sighashes of the inputs the wallet signs, so one approval covers a signature
5
+ * for each of them.
6
+ *
7
+ * The inputs are chosen as `signTransaction` chooses them: the ones that belong
8
+ * to `accountAddress`, narrowed to `signingIndexes` when given. P2WSH co-signing
9
+ * is not part of the intent, so `allowedWitnessScripts` is rejected.
10
+ */
11
+ export declare const prepareBtcTransactionSigningOperation: ({ transaction, accountAddress, bitcoinConfig, signingIndexes, allowedSighash, allowedWitnessScripts, }: {
12
+ transaction: string;
13
+ accountAddress: string;
14
+ bitcoinConfig?: BitcoinConfig;
15
+ signingIndexes?: number[];
16
+ allowedSighash?: number[];
17
+ allowedWitnessScripts?: string[];
18
+ }) => PreparedBtcTransactionSigningIntent;
19
+ //# sourceMappingURL=signingIntent.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"signingIntent.d.ts","sourceRoot":"","sources":["../../packages/src/signingIntent.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,mCAAmC,EAAE,MAAM,8BAA8B,CAAC;AAuBvG;;;;;;;;GAQG;AACH,eAAO,MAAM,qCAAqC,2GAO/C;IACD,WAAW,EAAE,MAAM,CAAC;IACpB,cAAc,EAAE,MAAM,CAAC;IACvB,aAAa,CAAC,EAAE,aAAa,CAAC;IAC9B,cAAc,CAAC,EAAE,MAAM,EAAE,CAAC;IAC1B,cAAc,CAAC,EAAE,MAAM,EAAE,CAAC;IAC1B,qBAAqB,CAAC,EAAE,MAAM,EAAE,CAAC;CAClC,KAAG,mCA8CH,CAAC"}