Skip to content

Commit

Permalink
feat: remove DLCHandler createPayment() function export (#3)
Browse files Browse the repository at this point in the history
* feat: add deployment fetching by network name

* feat: modify handler functions
  • Loading branch information
Polybius93 authored Jun 3, 2024
1 parent c118167 commit 0be128b
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 40 deletions.
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.1",
"version": "1.0.2",
"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
35 changes: 23 additions & 12 deletions src/dlc-handlers/ledger-dlc-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
updateNativeSegwitInputs,
updateTaprootInputs,
} from '../functions/bitcoin/psbt-functions.js';
import { PaymentInformation } from '../models/bitcoin-models.js';
import { ExtendedPaymentInformation } from '../models/bitcoin-models.js';
import { RawVault } from '../models/ethereum-models.js';
import { truncateAddress } from '../utilities/index.js';

Expand All @@ -38,7 +38,7 @@ export class LedgerDLCHandler {
private masterFingerprint: string;
private walletAccountIndex: number;
private policyInformation: LedgerPolicyInformation | undefined;
private paymentInformation: PaymentInformation | undefined;
public payment: ExtendedPaymentInformation | undefined;
private bitcoinNetwork: Network;
private bitcoinBlockchainAPI: string;
private bitcoinBlockchainFeeRecommendationAPI: string;
Expand Down Expand Up @@ -94,13 +94,13 @@ export class LedgerDLCHandler {
taprootMultisigWalletPolicyHMac,
};
}
private setPaymentInformation(
private setPayment(
nativeSegwitPayment: P2Ret,
nativeSegwitDerivedPublicKey: Buffer,
taprootMultisigPayment: P2TROut,
taprootDerivedPublicKey: Buffer
): void {
this.paymentInformation = {
this.payment = {
nativeSegwitPayment,
nativeSegwitDerivedPublicKey,
taprootMultisigPayment,
Expand All @@ -115,15 +115,15 @@ export class LedgerDLCHandler {
return this.policyInformation;
}

private getPaymentInformation(): PaymentInformation {
if (!this.paymentInformation) {
private getPayment(): ExtendedPaymentInformation {
if (!this.payment) {
throw new Error('Payment Information not set');
}
return this.paymentInformation;
return this.payment;
}

getVaultRelatedAddress(paymentType: 'p2wpkh' | 'p2tr'): string {
const payment = this.getPaymentInformation();
const payment = this.getPayment();

if (payment === undefined) {
throw new Error('Payment objects have not been set');
Expand All @@ -149,7 +149,10 @@ export class LedgerDLCHandler {
}
}

async createPayment(vaultUUID: string, attestorGroupPublicKey: string): Promise<void> {
private async createPayment(
vaultUUID: string,
attestorGroupPublicKey: string
): Promise<ExtendedPaymentInformation> {
try {
const networkIndex = this.bitcoinNetwork === bitcoin ? 0 : 1;

Expand Down Expand Up @@ -242,25 +245,33 @@ export class LedgerDLCHandler {
taprootMultisigAccountPolicy,
taprootMultisigPolicyHMac
);
this.setPaymentInformation(
this.setPayment(
nativeSegwitPayment,
nativeSegwitDerivedPublicKey,
taprootMultisigPayment,
taprootDerivedPublicKey
);

return {
nativeSegwitPayment,
nativeSegwitDerivedPublicKey,
taprootMultisigPayment,
taprootDerivedPublicKey,
};
} catch (error: any) {
throw new Error(`Error creating required wallet information: ${error}`);
}
}

async createFundingPSBT(
vault: RawVault,
attestorGroupPublicKey: string,
feeRateMultiplier?: number,
customFeeRate?: bigint
): Promise<Psbt> {
try {
const { nativeSegwitPayment, nativeSegwitDerivedPublicKey, taprootMultisigPayment } =
this.getPaymentInformation();
await this.createPayment(vault.uuid, attestorGroupPublicKey);

if (
taprootMultisigPayment.address === undefined ||
Expand Down Expand Up @@ -333,7 +344,7 @@ export class LedgerDLCHandler {
): Promise<Psbt> {
try {
const { nativeSegwitPayment, taprootMultisigPayment, taprootDerivedPublicKey } =
this.getPaymentInformation();
this.getPayment();

if (nativeSegwitPayment.address === undefined) {
throw new Error('Could not get Addresses from Payments');
Expand Down
8 changes: 4 additions & 4 deletions src/dlc-handlers/private-key-dlc-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
createClosingTransaction,
createFundingTransaction,
} from '../functions/bitcoin/psbt-functions.js';
import { RequiredPayment } from '../models/bitcoin-models.js';
import { PaymentInformation } from '../models/bitcoin-models.js';
import { RawVault } from '../models/ethereum-models.js';

interface RequiredKeyPair {
Expand All @@ -27,7 +27,7 @@ interface RequiredKeyPair {

export class PrivateKeyDLCHandler {
private derivedKeyPair: RequiredKeyPair;
public payment: RequiredPayment | undefined;
public payment: PaymentInformation | undefined;
private bitcoinNetwork: Network;
private bitcoinBlockchainAPI: string;
private bitcoinBlockchainFeeRecommendationAPI: string;
Expand Down Expand Up @@ -132,7 +132,7 @@ export class PrivateKeyDLCHandler {
return privateKey;
}

handlePayment(vaultUUID: string, attestorGroupPublicKey: string): RequiredPayment {
private createPayments(vaultUUID: string, attestorGroupPublicKey: string): PaymentInformation {
try {
const unspendablePublicKey = getUnspendableKeyCommittedToUUID(vaultUUID, this.bitcoinNetwork);
const unspendableDerivedPublicKey = deriveUnhardenedPublicKey(
Expand Down Expand Up @@ -174,7 +174,7 @@ export class PrivateKeyDLCHandler {
feeRateMultiplier?: number,
customFeeRate?: bigint
): Promise<Transaction> {
const { nativeSegwitPayment, taprootMultisigPayment } = this.handlePayment(
const { nativeSegwitPayment, taprootMultisigPayment } = this.createPayments(
vault.uuid,
attestorGroupPublicKey
);
Expand Down
38 changes: 25 additions & 13 deletions src/dlc-handlers/software-wallet-dlc-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ import {
createClosingTransaction,
createFundingTransaction,
} from '../functions/bitcoin/psbt-functions.js';
import { RequiredPayment } from '../models/bitcoin-models.js';
import { PaymentInformation } from '../models/bitcoin-models.js';
import { RawVault } from '../models/ethereum-models.js';

export class SoftwareWalletDLCHandler {
private nativeSegwitDerivedPublicKey: string;
private taprootDerivedPublicKey: string;
public paymentInformation: RequiredPayment | undefined;
public payment: PaymentInformation | undefined;
private bitcoinNetwork: Network;
private bitcoinBlockchainAPI: string;
private bitcoinBlockchainFeeRecommendationAPI: string;
Expand Down Expand Up @@ -63,22 +63,22 @@ export class SoftwareWalletDLCHandler {
this.taprootDerivedPublicKey = taprootDerivedPublicKey;
}

private setPaymentInformation(nativeSegwitPayment: P2Ret, taprootMultisigPayment: P2TROut): void {
this.paymentInformation = {
private setPayment(nativeSegwitPayment: P2Ret, taprootMultisigPayment: P2TROut): void {
this.payment = {
nativeSegwitPayment,
taprootMultisigPayment,
};
}

private getPaymentInformation(): RequiredPayment {
if (!this.paymentInformation) {
private getPayment(): PaymentInformation {
if (!this.payment) {
throw new Error('Payment Information not set');
}
return this.paymentInformation;
return this.payment;
}

etVaultRelatedAddress(paymentType: 'p2wpkh' | 'p2tr'): string {
const payment = this.getPaymentInformation();
getVaultRelatedAddress(paymentType: 'p2wpkh' | 'p2tr'): string {
const payment = this.getPayment();

if (payment === undefined) {
throw new Error('Payment objects have not been set');
Expand All @@ -104,7 +104,10 @@ export class SoftwareWalletDLCHandler {
}
}

async createPayment(vaultUUID: string, attestorGroupPublicKey: string): Promise<void> {
private async createPayments(
vaultUUID: string,
attestorGroupPublicKey: string
): Promise<PaymentInformation> {
try {
const nativeSegwitPayment = p2wpkh(
Buffer.from(this.nativeSegwitDerivedPublicKey, 'hex'),
Expand All @@ -129,19 +132,28 @@ export class SoftwareWalletDLCHandler {
this.bitcoinNetwork
);

this.setPaymentInformation(nativeSegwitPayment, taprootMultisigPayment);
this.setPayment(nativeSegwitPayment, taprootMultisigPayment);

return {
nativeSegwitPayment,
taprootMultisigPayment,
};
} catch (error: any) {
throw new Error(`Error creating required wallet information: ${error}`);
}
}

async createFundingPSBT(
vault: RawVault,
attestorGroupPublicKey: string,
feeRateMultiplier?: number,
customFeeRate?: bigint
): Promise<Transaction> {
try {
const { nativeSegwitPayment, taprootMultisigPayment } = this.getPaymentInformation();
const { nativeSegwitPayment, taprootMultisigPayment } = await this.createPayments(
vault.uuid,
attestorGroupPublicKey
);

if (
taprootMultisigPayment.address === undefined ||
Expand Down Expand Up @@ -186,7 +198,7 @@ export class SoftwareWalletDLCHandler {
customFeeRate?: bigint
): Promise<Transaction> {
try {
const { nativeSegwitPayment, taprootMultisigPayment } = this.getPaymentInformation();
const { nativeSegwitPayment, taprootMultisigPayment } = this.getPayment();

if (
taprootMultisigPayment.address === undefined ||
Expand Down
7 changes: 1 addition & 6 deletions src/example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,8 @@ async function runFlowWithLedger() {
// Fetch Attestor Group Public Key from the Smart Contract
const attestorGroupPublicKey = await ethereumHandler.getAttestorGroupPublicKey();

// Create Native Segwit Payment from the User's Native Segwit Extended Public Key,
// and Taproot Multisig Payment from the User's Taproot Extended Public Key, the Attestor Group Public Key, and the Unspendable Public Key committed to the Vault UUID,
// which will later be used for the Funding and Closing Transaction
await dlcHandler.createPayment(vaultUUID, attestorGroupPublicKey);

// Create Funding Transaction
const fundingPSBT = await dlcHandler.createFundingPSBT(vault, 2);
const fundingPSBT = await dlcHandler.createFundingPSBT(vault, attestorGroupPublicKey, 2);

// Sign Funding Transaction
const fundingTransaction = await dlcHandler.signPSBT(fundingPSBT, 'funding');
Expand Down
6 changes: 2 additions & 4 deletions src/models/bitcoin-models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,13 @@ export interface FeeRates {
minimumFee: number;
}

export interface RequiredPayment {
export interface PaymentInformation {
nativeSegwitPayment: P2Ret;
taprootMultisigPayment: P2TROut;
}

export interface PaymentInformation {
nativeSegwitPayment: P2Ret;
export interface ExtendedPaymentInformation extends PaymentInformation {
nativeSegwitDerivedPublicKey: Buffer;
taprootMultisigPayment: P2TROut;
taprootDerivedPublicKey: Buffer;
}

Expand Down

0 comments on commit 0be128b

Please sign in to comment.