diff --git a/package.json b/package.json index 3e40666..46a7d9a 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "type": "module", "name": "dlc-btc-lib", - "version": "1.0.14", + "version": "1.0.16", "description": "This library provides a comprehensive set of interfaces and functions for minting dlcBTC tokens on supported blockchains.", "main": "dist/index.js", "types": "dist/index.d.ts", diff --git a/src/attestor-handlers/attestor-handler.ts b/src/attestor-handlers/attestor-handler.ts index 2017897..e29bd32 100644 --- a/src/attestor-handlers/attestor-handler.ts +++ b/src/attestor-handlers/attestor-handler.ts @@ -1,8 +1,4 @@ -import { - FundingTXAttestorInfo, - WithdrawDepositTXAttestorInfo, -} from 'src/models/attestor.models.js'; - +import { FundingTXAttestorInfo, WithdrawDepositTXAttestorInfo } from '../models/attestor.models.js'; import { AttestorError } from '../models/errors.js'; export class AttestorHandler { diff --git a/src/dlc-handlers/ledger-dlc-handler.ts b/src/dlc-handlers/ledger-dlc-handler.ts index 4199b87..ec87d33 100644 --- a/src/dlc-handlers/ledger-dlc-handler.ts +++ b/src/dlc-handlers/ledger-dlc-handler.ts @@ -490,57 +490,61 @@ export class LedgerDLCHandler { psbt: Psbt, transactionType: 'funding' | 'deposit' | 'withdraw' ): Promise { - try { - const { fundingWalletPolicy, multisigWalletPolicy, multisigWalletPolicyHMac } = - this.getPolicyInformation(); - - let signatures; - let transaction: Transaction; - - switch (transactionType) { - case 'funding': - signatures = await this.ledgerApp.signPsbt(psbt.toBase64(), fundingWalletPolicy, null); - switch (this.fundingPaymentType) { - case 'wpkh': - addNativeSegwitSignaturesToPSBT(psbt, signatures); - break; - case 'tr': - addTaprootInputSignaturesToPSBT('funding', psbt, signatures); - break; - default: - throw new Error('Invalid Funding Payment Type'); - } - transaction = Transaction.fromPSBT(psbt.toBuffer()); - transaction.finalize(); - return transaction; - case 'deposit': - signatures = await this.ledgerApp.signPsbt( - psbt.toBase64(), - multisigWalletPolicy, - multisigWalletPolicyHMac - ); - addTaprootInputSignaturesToPSBT('funding', psbt, signatures); - - signatures = await this.ledgerApp.signPsbt(psbt.toBase64(), fundingWalletPolicy, null); - + const { fundingWalletPolicy, multisigWalletPolicy, multisigWalletPolicyHMac } = + this.getPolicyInformation(); + if (transactionType === 'funding') { + const signatures = await this.ledgerApp.signPsbt(psbt.toBase64(), fundingWalletPolicy, null); + switch (this.fundingPaymentType) { + case 'wpkh': addNativeSegwitSignaturesToPSBT(psbt, signatures); - - transaction = Transaction.fromPSBT(psbt.toBuffer()); - return transaction; - case 'withdraw': - signatures = await this.ledgerApp.signPsbt( - psbt.toBase64(), - multisigWalletPolicy, - multisigWalletPolicyHMac - ); - addTaprootInputSignaturesToPSBT('withdraw', psbt, signatures); - transaction = Transaction.fromPSBT(psbt.toBuffer()); - return transaction; + break; + case 'tr': + addTaprootInputSignaturesToPSBT('funding', psbt, signatures); + break; default: - throw new Error('Invalid Transaction Type'); + throw new Error('Invalid Funding Payment Type'); } - } catch (error: any) { - throw new Error(`Error signing PSBT: ${error}`); + const fundingTransaction = Transaction.fromPSBT(psbt.toBuffer()); + fundingTransaction.finalize(); + return fundingTransaction; + } else if (transactionType === 'deposit') { + const multisigSignatures = await this.ledgerApp.signPsbt( + psbt.toBase64(), + multisigWalletPolicy, + multisigWalletPolicyHMac + ); + addTaprootInputSignaturesToPSBT('depositWithdraw', psbt, multisigSignatures); + const userSignatures = await this.ledgerApp.signPsbt( + psbt.toBase64(), + fundingWalletPolicy, + null + ); + switch (this.fundingPaymentType) { + case 'wpkh': + addNativeSegwitSignaturesToPSBT(psbt, userSignatures); + break; + case 'tr': + addTaprootInputSignaturesToPSBT('funding', psbt, userSignatures); + break; + default: + throw new Error('Invalid Funding Payment Type'); + } + const userInputIndices = userSignatures.map(signature => signature[0]); + + const depositTransaction = Transaction.fromPSBT(psbt.toBuffer()); + userInputIndices.forEach(index => { + depositTransaction.finalizeIdx(index); + }); + return depositTransaction; + } else { + const multisigSignatures = await this.ledgerApp.signPsbt( + psbt.toBase64(), + multisigWalletPolicy, + multisigWalletPolicyHMac + ); + addTaprootInputSignaturesToPSBT('depositWithdraw', psbt, multisigSignatures); + const withdrawTransaction = Transaction.fromPSBT(psbt.toBuffer()); + return withdrawTransaction; } } } diff --git a/src/functions/bitcoin/psbt-functions.ts b/src/functions/bitcoin/psbt-functions.ts index 6abb4b9..5c21a2d 100644 --- a/src/functions/bitcoin/psbt-functions.ts +++ b/src/functions/bitcoin/psbt-functions.ts @@ -207,7 +207,7 @@ export async function createDepositTransaction( }, ]; - const depositSelected = selectUTXO(depositInputs, depositOutputs, 'default', { + const depositSelected = selectUTXO(depositInputs, depositOutputs, 'all', { changeAddress: depositAddress, feePerByte: feeRate, bip69: false, @@ -525,7 +525,7 @@ export function addNativeSegwitSignaturesToPSBT( * @returns The updated PSBT. */ export function addTaprootInputSignaturesToPSBT( - psbtType: 'funding' | 'withdraw', + psbtType: 'funding' | 'depositWithdraw', psbt: Psbt, signatures: [number, PartialSignature][] ): void { diff --git a/src/models/index.ts b/src/models/index.ts index 0985f9b..40e320b 100644 --- a/src/models/index.ts +++ b/src/models/index.ts @@ -1,5 +1,6 @@ export { Network } from 'bitcoinjs-lib/src/networks.js'; export { Transaction } from '@scure/btc-signer'; +export { TransactionInputUpdate } from '@scure/btc-signer/psbt'; export * from '../models/bitcoin-models.js'; export * from '../models/errors.js'; export * from '../models/ethereum-models.js';