Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update ledger signing #12

Merged
merged 4 commits into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
6 changes: 1 addition & 5 deletions src/attestor-handlers/attestor-handler.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
100 changes: 52 additions & 48 deletions src/dlc-handlers/ledger-dlc-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -490,57 +490,61 @@ export class LedgerDLCHandler {
psbt: Psbt,
transactionType: 'funding' | 'deposit' | 'withdraw'
): Promise<Transaction> {
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;
}
}
}
4 changes: 2 additions & 2 deletions src/functions/bitcoin/psbt-functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions src/models/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
Loading