diff --git a/.gitignore b/.gitignore index 8a79922..2b3f55c 100644 --- a/.gitignore +++ b/.gitignore @@ -93,7 +93,7 @@ out # Nuxt.js build / generate output .nuxt -# dist +dist # Gatsby files .cache/ diff --git a/dist/attestor-handlers/attestor-handler.d.ts b/dist/attestor-handlers/attestor-handler.d.ts deleted file mode 100644 index e3ea4b0..0000000 --- a/dist/attestor-handlers/attestor-handler.d.ts +++ /dev/null @@ -1,7 +0,0 @@ -export declare class AttestorHandler { - private attestorRootURLs; - private ethereumChainID; - constructor(attestorRootURLs: string[], ethereumChainID: 'evm-arbitrum' | 'evm-arbsepolia' | 'evm-localhost'); - createPSBTEvent(vaultUUID: string, fundingTransactionPsbt: string, mintAddress: string, alicePubkey: string): Promise; - submitWithdrawRequest(vaultUUID: string, withdrawPSBT: string): Promise; -} diff --git a/dist/attestor-handlers/attestor-handler.js b/dist/attestor-handlers/attestor-handler.js deleted file mode 100644 index 959f29a..0000000 --- a/dist/attestor-handlers/attestor-handler.js +++ /dev/null @@ -1,50 +0,0 @@ -import { AttestorError } from '../models/errors.js'; -export class AttestorHandler { - attestorRootURLs; - ethereumChainID; - constructor(attestorRootURLs, ethereumChainID) { - this.attestorRootURLs = attestorRootURLs; - this.ethereumChainID = ethereumChainID; - } - async createPSBTEvent(vaultUUID, fundingTransactionPsbt, mintAddress, alicePubkey) { - const createPSBTEndpoints = this.attestorRootURLs.map(url => `${url}/app/create-psbt-event`); - const body = JSON.stringify({ - uuid: vaultUUID, - funding_transaction_psbt: fundingTransactionPsbt, - mint_address: mintAddress, - chain: this.ethereumChainID, - alice_pubkey: alicePubkey, - }); - const requests = createPSBTEndpoints.map(async (url) => fetch(url, { - method: 'POST', - headers: { 'Content-Type': 'application/json', 'Access-Control-Allow-Origin': '*' }, - body, - }) - .then(response => (response.ok ? true : response.statusText)) - .catch(error => error.message)); - const responses = await Promise.all(requests); - const failedResponses = responses.filter(response => response !== true); - if (failedResponses.length === createPSBTEndpoints.length) { - throw new AttestorError(`Error sending Funding and Closing Transaction to Attestors: ${failedResponses.join(', ')}`); - } - } - async submitWithdrawRequest(vaultUUID, withdrawPSBT) { - const withdrawEndpoints = this.attestorRootURLs.map(url => `${url}/app/withdraw`); - const body = JSON.stringify({ - uuid: vaultUUID, - wd_psbt: withdrawPSBT, - }); - const requests = withdrawEndpoints.map(async (url) => fetch(url, { - method: 'POST', - headers: { 'Content-Type': 'application/json', 'Access-Control-Allow-Origin': '*' }, - body, - }) - .then(response => (response.ok ? true : response.statusText)) - .catch(error => error.message)); - const responses = await Promise.all(requests); - const failedResponses = responses.filter(response => response !== true); - if (failedResponses.length === withdrawEndpoints.length) { - throw new AttestorError(`Error sending Withdraw Transaction to Attestors: ${failedResponses.join(', ')}`); - } - } -} diff --git a/dist/constants/ethereum-constants.d.ts b/dist/constants/ethereum-constants.d.ts deleted file mode 100644 index 5c0d156..0000000 --- a/dist/constants/ethereum-constants.d.ts +++ /dev/null @@ -1,33 +0,0 @@ -import { DLCEthereumContractName, EthereumNetwork, EthereumNetworkID } from '../models/ethereum-models.js'; -export declare const ethereumArbitrumSepolia: EthereumNetwork; -export declare const ethereumArbitrum: EthereumNetwork; -export declare const supportedEthereumNetworks: EthereumNetwork[]; -export declare const hexChainIDs: { - [key in EthereumNetworkID]: string; -}; -export declare const addNetworkParams: { - "421614": { - chainId: string; - rpcUrls: string[]; - chainName: string; - nativeCurrency: { - name: string; - symbol: string; - decimals: number; - }; - blockExplorerUrls: string[]; - }[]; - "42161": { - chainId: string; - rpcUrls: string[]; - chainName: string; - nativeCurrency: { - name: string; - symbol: string; - decimals: number; - }; - blockExplorerUrls: string[]; - }[]; -}; -export declare const GITHUB_SOLIDITY_URL = "https://raw.githubusercontent.com/DLC-link/dlc-solidity"; -export declare const dlcContractNames: DLCEthereumContractName[]; diff --git a/dist/constants/ethereum-constants.js b/dist/constants/ethereum-constants.js deleted file mode 100644 index c19592f..0000000 --- a/dist/constants/ethereum-constants.js +++ /dev/null @@ -1,51 +0,0 @@ -import { EthereumNetworkID, } from '../models/ethereum-models.js'; -export const ethereumArbitrumSepolia = { - name: 'ArbSepolia', - displayName: 'Arbitrum Sepolia', - id: EthereumNetworkID.ArbitrumSepolia, - defaultNodeURL: 'https://sepolia-rollup.arbitrum.io/rpc', -}; -export const ethereumArbitrum = { - name: 'Arbitrum', - displayName: 'Arbitrum', - id: EthereumNetworkID.Arbitrum, - defaultNodeURL: 'https://arb1.arbitrum.io/rpc', -}; -export const supportedEthereumNetworks = [ - ethereumArbitrumSepolia, - ethereumArbitrum, -]; -export const hexChainIDs = { - [EthereumNetworkID.ArbitrumSepolia]: '0x66eee', - [EthereumNetworkID.Arbitrum]: '0xa4b1', -}; -export const addNetworkParams = { - [EthereumNetworkID.ArbitrumSepolia]: [ - { - chainId: '0x66eee', - rpcUrls: ['https://sepolia-rollup.arbitrum.io/rpc', 'https://arb-sepolia.infura.io/v3/'], - chainName: 'Arbitrum Sepolia Testnet', - nativeCurrency: { - name: 'ETH', - symbol: 'ETH', - decimals: 18, - }, - blockExplorerUrls: ['https://sepolia.arbiscan.io/'], - }, - ], - [EthereumNetworkID.Arbitrum]: [ - { - chainId: '42161', - rpcUrls: ['https://arb1.arbitrum.io/rpc', 'https://arbitrum-mainnet.infura.io'], - chainName: 'Arbitrum One', - nativeCurrency: { - name: 'ETH', - symbol: 'ETH', - decimals: 18, - }, - blockExplorerUrls: ['https://arbiscan.io/'], - }, - ], -}; -export const GITHUB_SOLIDITY_URL = 'https://raw.githubusercontent.com/DLC-link/dlc-solidity'; -export const dlcContractNames = ['DLCManager', 'DLCBTC']; diff --git a/dist/constants/index.d.ts b/dist/constants/index.d.ts deleted file mode 100644 index e8f4e81..0000000 --- a/dist/constants/index.d.ts +++ /dev/null @@ -1,3 +0,0 @@ -export * from './ethereum-constants.js'; -export * from './ledger-constants.js'; -export { bitcoin, testnet, regtest } from 'bitcoinjs-lib/src/networks.js'; diff --git a/dist/constants/index.js b/dist/constants/index.js deleted file mode 100644 index e8f4e81..0000000 --- a/dist/constants/index.js +++ /dev/null @@ -1,3 +0,0 @@ -export * from './ethereum-constants.js'; -export * from './ledger-constants.js'; -export { bitcoin, testnet, regtest } from 'bitcoinjs-lib/src/networks.js'; diff --git a/dist/constants/ledger-constants.d.ts b/dist/constants/ledger-constants.d.ts deleted file mode 100644 index 4d570b1..0000000 --- a/dist/constants/ledger-constants.d.ts +++ /dev/null @@ -1,5 +0,0 @@ -export declare const LEDGER_APPS_MAP: { - readonly BITCOIN_MAINNET: "Bitcoin"; - readonly BITCOIN_TESTNET: "Bitcoin Test"; - readonly MAIN_MENU: "BOLOS"; -}; diff --git a/dist/constants/ledger-constants.js b/dist/constants/ledger-constants.js deleted file mode 100644 index 70b9b64..0000000 --- a/dist/constants/ledger-constants.js +++ /dev/null @@ -1,5 +0,0 @@ -export const LEDGER_APPS_MAP = { - BITCOIN_MAINNET: 'Bitcoin', - BITCOIN_TESTNET: 'Bitcoin Test', - MAIN_MENU: 'BOLOS', -}; diff --git a/dist/dlc-handlers/ledger-dlc-handler.d.ts b/dist/dlc-handlers/ledger-dlc-handler.d.ts deleted file mode 100644 index 594c706..0000000 --- a/dist/dlc-handlers/ledger-dlc-handler.d.ts +++ /dev/null @@ -1,27 +0,0 @@ -import { Transaction } from '@scure/btc-signer'; -import { Network, Psbt } from 'bitcoinjs-lib'; -import { AppClient } from 'ledger-bitcoin'; -import { ExtendedPaymentInformation } from '../models/bitcoin-models.js'; -import { RawVault } from '../models/ethereum-models.js'; -export declare class LedgerDLCHandler { - private ledgerApp; - private masterFingerprint; - private walletAccountIndex; - private policyInformation; - payment: ExtendedPaymentInformation | undefined; - private bitcoinNetwork; - private bitcoinBlockchainAPI; - private bitcoinBlockchainFeeRecommendationAPI; - constructor(ledgerApp: AppClient, masterFingerprint: string, walletAccountIndex: number, bitcoinNetwork: Network, bitcoinBlockchainAPI?: string, bitcoinBlockchainFeeRecommendationAPI?: string); - private setPolicyInformation; - private setPayment; - private getPolicyInformation; - private getPayment; - getTaprootDerivedPublicKey(): string; - getVaultRelatedAddress(paymentType: 'p2wpkh' | 'p2tr'): string; - private createPayment; - createFundingPSBT(vault: RawVault, bitcoinAmount: bigint, attestorGroupPublicKey: string, feeRateMultiplier?: number, customFeeRate?: bigint): Promise; - createWithdrawalPSBT(vault: RawVault, withdrawAmount: bigint, attestorGroupPublicKey: string, fundingTransactionID: string, feeRateMultiplier?: number, customFeeRate?: bigint): Promise; - createDepositPSBT(depositAmount: bigint, vault: RawVault, attestorGroupPublicKey: string, fundingTransactionID: string, feeRateMultiplier?: number, customFeeRate?: bigint): Promise; - signPSBT(psbt: Psbt, transactionType: 'funding' | 'deposit' | 'closing'): Promise; -} diff --git a/dist/dlc-handlers/ledger-dlc-handler.js b/dist/dlc-handlers/ledger-dlc-handler.js deleted file mode 100644 index 9553628..0000000 --- a/dist/dlc-handlers/ledger-dlc-handler.js +++ /dev/null @@ -1,242 +0,0 @@ -import { bytesToHex } from '@noble/hashes/utils'; -import { Transaction } from '@scure/btc-signer'; -import { p2wpkh } from '@scure/btc-signer/payment'; -import { Psbt } from 'bitcoinjs-lib'; -import { bitcoin, regtest, testnet } from 'bitcoinjs-lib/src/networks.js'; -import { DefaultWalletPolicy, WalletPolicy } from 'ledger-bitcoin'; -import { createBitcoinInputSigningConfiguration, createTaprootMultisigPayment, deriveUnhardenedPublicKey, getBalance, getFeeRate, getInputByPaymentTypeArray, getUnspendableKeyCommittedToUUID, } from '../functions/bitcoin/bitcoin-functions.js'; -import { addNativeSegwitSignaturesToPSBT, addTaprootInputSignaturesToPSBT, createDepositTransaction, createFundingTransaction, createWithdrawalTransaction, getNativeSegwitInputsToSign, getTaprootInputsToSign, updateNativeSegwitInputs, updateTaprootInputs, } from '../functions/bitcoin/psbt-functions.js'; -import { truncateAddress } from '../utilities/index.js'; -export class LedgerDLCHandler { - ledgerApp; - masterFingerprint; - walletAccountIndex; - policyInformation; - payment; - bitcoinNetwork; - bitcoinBlockchainAPI; - bitcoinBlockchainFeeRecommendationAPI; - constructor(ledgerApp, masterFingerprint, walletAccountIndex, bitcoinNetwork, bitcoinBlockchainAPI, bitcoinBlockchainFeeRecommendationAPI) { - switch (bitcoinNetwork) { - case bitcoin: - this.bitcoinBlockchainAPI = 'https://mempool.space/api'; - this.bitcoinBlockchainFeeRecommendationAPI = - 'https://mempool.space/api/v1/fees/recommended'; - break; - case testnet: - this.bitcoinBlockchainAPI = 'https://mempool.space/testnet/api'; - this.bitcoinBlockchainFeeRecommendationAPI = - 'https://mempool.space/testnet/api/v1/fees/recommended'; - break; - case regtest: - if (bitcoinBlockchainAPI === undefined || - bitcoinBlockchainFeeRecommendationAPI === undefined) { - throw new Error('Regtest requires a Bitcoin Blockchain API and a Bitcoin Blockchain Fee Recommendation API'); - } - this.bitcoinBlockchainAPI = bitcoinBlockchainAPI; - this.bitcoinBlockchainFeeRecommendationAPI = bitcoinBlockchainFeeRecommendationAPI; - break; - default: - throw new Error('Invalid Bitcoin Network'); - } - this.ledgerApp = ledgerApp; - this.masterFingerprint = masterFingerprint; - this.walletAccountIndex = walletAccountIndex; - this.bitcoinNetwork = bitcoinNetwork; - } - setPolicyInformation(nativeSegwitWalletPolicy, taprootMultisigWalletPolicy, taprootMultisigWalletPolicyHMac) { - this.policyInformation = { - nativeSegwitWalletPolicy, - taprootMultisigWalletPolicy, - taprootMultisigWalletPolicyHMac, - }; - } - setPayment(nativeSegwitPayment, nativeSegwitDerivedPublicKey, taprootMultisigPayment, taprootDerivedPublicKey) { - this.payment = { - nativeSegwitPayment, - nativeSegwitDerivedPublicKey, - taprootMultisigPayment, - taprootDerivedPublicKey, - }; - } - getPolicyInformation() { - if (!this.policyInformation) { - throw new Error('Policy Information not set'); - } - return this.policyInformation; - } - getPayment() { - if (!this.payment) { - throw new Error('Payment Information not set'); - } - return this.payment; - } - getTaprootDerivedPublicKey() { - return bytesToHex(this.getPayment().taprootDerivedPublicKey); - } - getVaultRelatedAddress(paymentType) { - const payment = this.getPayment(); - if (payment === undefined) { - throw new Error('Payment objects have not been set'); - } - let address; - switch (paymentType) { - case 'p2wpkh': - if (!payment.nativeSegwitPayment.address) { - throw new Error('Native Segwit Payment Address is undefined'); - } - address = payment.nativeSegwitPayment.address; - return address; - case 'p2tr': - if (!payment.taprootMultisigPayment.address) { - throw new Error('Taproot Multisig Payment Address is undefined'); - } - address = payment.taprootMultisigPayment.address; - return address; - default: - throw new Error('Invalid Payment Type'); - } - } - async createPayment(vaultUUID, attestorGroupPublicKey) { - try { - const networkIndex = this.bitcoinNetwork === bitcoin ? 0 : 1; - const nativeSegwitExtendedPublicKey = await this.ledgerApp.getExtendedPubkey(`m/84'/${networkIndex}'/${this.walletAccountIndex}'`); - const nativeSegwitKeyinfo = `[${this.masterFingerprint}/84'/${networkIndex}'/${this.walletAccountIndex}']${nativeSegwitExtendedPublicKey}`; - const nativeSegwitWalletPolicy = new DefaultWalletPolicy('wpkh(@0/**)', nativeSegwitKeyinfo); - const nativeSegwitAddress = await this.ledgerApp.getWalletAddress(nativeSegwitWalletPolicy, null, 0, 0, false); - const nativeSegwitDerivedPublicKey = deriveUnhardenedPublicKey(nativeSegwitExtendedPublicKey, this.bitcoinNetwork); - const nativeSegwitPayment = p2wpkh(nativeSegwitDerivedPublicKey, this.bitcoinNetwork); - if (nativeSegwitPayment.address !== nativeSegwitAddress) { - throw new Error(`[Ledger] Recreated Native Segwit Address does not match the Ledger Native Segwit Address`); - } - const unspendablePublicKey = getUnspendableKeyCommittedToUUID(vaultUUID, this.bitcoinNetwork); - const unspendableDerivedPublicKey = deriveUnhardenedPublicKey(unspendablePublicKey, this.bitcoinNetwork); - const attestorDerivedPublicKey = deriveUnhardenedPublicKey(attestorGroupPublicKey, this.bitcoinNetwork); - const taprootExtendedPublicKey = await this.ledgerApp.getExtendedPubkey(`m/86'/${networkIndex}'/${this.walletAccountIndex}'`); - const ledgerTaprootKeyInfo = `[${this.masterFingerprint}/86'/${networkIndex}'/${this.walletAccountIndex}']${taprootExtendedPublicKey}`; - const taprootDerivedPublicKey = deriveUnhardenedPublicKey(taprootExtendedPublicKey, this.bitcoinNetwork); - const descriptors = taprootDerivedPublicKey.toString('hex') < attestorDerivedPublicKey.toString('hex') - ? [ledgerTaprootKeyInfo, attestorGroupPublicKey] - : [attestorGroupPublicKey, ledgerTaprootKeyInfo]; - const taprootMultisigAccountPolicy = new WalletPolicy(`Taproot Multisig Wallet for Vault: ${truncateAddress(vaultUUID)}`, `tr(@0/**,and_v(v:pk(@1/**),pk(@2/**)))`, [unspendablePublicKey, ...descriptors]); - const [, taprootMultisigPolicyHMac] = await this.ledgerApp.registerWallet(taprootMultisigAccountPolicy); - const taprootMultisigAddress = await this.ledgerApp.getWalletAddress(taprootMultisigAccountPolicy, taprootMultisigPolicyHMac, 0, 0, false); - const taprootMultisigPayment = createTaprootMultisigPayment(unspendableDerivedPublicKey, attestorDerivedPublicKey, taprootDerivedPublicKey, this.bitcoinNetwork); - if (taprootMultisigAddress !== taprootMultisigPayment.address) { - throw new Error(`Recreated Multisig Address does not match the Ledger Multisig Address`); - } - this.setPolicyInformation(nativeSegwitWalletPolicy, taprootMultisigAccountPolicy, taprootMultisigPolicyHMac); - this.setPayment(nativeSegwitPayment, nativeSegwitDerivedPublicKey, taprootMultisigPayment, taprootDerivedPublicKey); - return { - nativeSegwitPayment, - nativeSegwitDerivedPublicKey, - taprootMultisigPayment, - taprootDerivedPublicKey, - }; - } - catch (error) { - throw new Error(`Error creating required wallet information: ${error}`); - } - } - async createFundingPSBT(vault, bitcoinAmount, attestorGroupPublicKey, feeRateMultiplier, customFeeRate) { - try { - const { nativeSegwitPayment, nativeSegwitDerivedPublicKey, taprootMultisigPayment } = await this.createPayment(vault.uuid, attestorGroupPublicKey); - if (taprootMultisigPayment.address === undefined || - nativeSegwitPayment.address === undefined) { - throw new Error('Payment Address is undefined'); - } - const feeRate = customFeeRate ?? - BigInt(await getFeeRate(this.bitcoinBlockchainFeeRecommendationAPI, feeRateMultiplier)); - const addressBalance = await getBalance(nativeSegwitPayment.address, this.bitcoinBlockchainAPI); - if (BigInt(addressBalance) < vault.valueLocked.toBigInt()) { - throw new Error('Insufficient Funds'); - } - const fundingTransaction = await createFundingTransaction(bitcoinAmount, this.bitcoinNetwork, taprootMultisigPayment.address, nativeSegwitPayment, feeRate, vault.btcFeeRecipient, vault.btcMintFeeBasisPoints.toBigInt(), this.bitcoinBlockchainAPI); - const signingConfiguration = createBitcoinInputSigningConfiguration(fundingTransaction, this.walletAccountIndex, this.bitcoinNetwork); - const formattedFundingPSBT = Psbt.fromBuffer(Buffer.from(fundingTransaction.toPSBT()), { - network: this.bitcoinNetwork, - }); - const inputByPaymentTypeArray = getInputByPaymentTypeArray(signingConfiguration, formattedFundingPSBT.toBuffer(), this.bitcoinNetwork); - const nativeSegwitInputsToSign = getNativeSegwitInputsToSign(inputByPaymentTypeArray); - await updateNativeSegwitInputs(nativeSegwitInputsToSign, nativeSegwitDerivedPublicKey, this.masterFingerprint, formattedFundingPSBT, this.bitcoinBlockchainAPI); - return formattedFundingPSBT; - } - catch (error) { - throw new Error(`Error creating Funding PSBT: ${error}`); - } - } - async createWithdrawalPSBT(vault, withdrawAmount, attestorGroupPublicKey, fundingTransactionID, feeRateMultiplier, customFeeRate) { - try { - const { nativeSegwitPayment, taprootDerivedPublicKey, taprootMultisigPayment } = await this.createPayment(vault.uuid, attestorGroupPublicKey); - if (taprootMultisigPayment.address === undefined || - nativeSegwitPayment.address === undefined) { - throw new Error('Payment Address is undefined'); - } - const feeRate = customFeeRate ?? - BigInt(await getFeeRate(this.bitcoinBlockchainFeeRecommendationAPI, feeRateMultiplier)); - const withdrawalTransaction = await createWithdrawalTransaction(this.bitcoinBlockchainAPI, withdrawAmount, this.bitcoinNetwork, fundingTransactionID, taprootMultisigPayment, nativeSegwitPayment.address, feeRate, vault.btcFeeRecipient, vault.btcRedeemFeeBasisPoints.toBigInt()); - const withdrawalTransactionSigningConfiguration = createBitcoinInputSigningConfiguration(withdrawalTransaction, this.walletAccountIndex, this.bitcoinNetwork); - const formattedWithdrawalPSBT = Psbt.fromBuffer(Buffer.from(withdrawalTransaction.toPSBT()), { - network: this.bitcoinNetwork, - }); - const withdrawalInputByPaymentTypeArray = getInputByPaymentTypeArray(withdrawalTransactionSigningConfiguration, formattedWithdrawalPSBT.toBuffer(), this.bitcoinNetwork); - const taprootInputsToSign = getTaprootInputsToSign(withdrawalInputByPaymentTypeArray); - await updateTaprootInputs(taprootInputsToSign, taprootDerivedPublicKey, this.masterFingerprint, formattedWithdrawalPSBT); - return formattedWithdrawalPSBT; - } - catch (error) { - throw new Error(`Error creating Withdrawal PSBT: ${error}`); - } - } - async createDepositPSBT(depositAmount, vault, attestorGroupPublicKey, fundingTransactionID, feeRateMultiplier, customFeeRate) { - const { nativeSegwitPayment, taprootDerivedPublicKey, nativeSegwitDerivedPublicKey, taprootMultisigPayment, } = await this.createPayment(vault.uuid, attestorGroupPublicKey); - if (taprootMultisigPayment.address === undefined || nativeSegwitPayment.address === undefined) { - throw new Error('Payment Address is undefined'); - } - const feeRate = customFeeRate ?? - BigInt(await getFeeRate(this.bitcoinBlockchainFeeRecommendationAPI, feeRateMultiplier)); - const depositTransaction = await createDepositTransaction(this.bitcoinBlockchainAPI, this.bitcoinNetwork, depositAmount, fundingTransactionID, taprootMultisigPayment, nativeSegwitPayment, feeRate, vault.btcFeeRecipient, vault.btcMintFeeBasisPoints.toBigInt()); - const depositTransactionSigningConfiguration = createBitcoinInputSigningConfiguration(depositTransaction, this.walletAccountIndex, this.bitcoinNetwork); - const formattedDepositPSBT = Psbt.fromBuffer(Buffer.from(depositTransaction.toPSBT()), { - network: this.bitcoinNetwork, - }); - const withdrawalInputByPaymentTypeArray = getInputByPaymentTypeArray(depositTransactionSigningConfiguration, formattedDepositPSBT.toBuffer(), this.bitcoinNetwork); - const taprootInputsToSign = getTaprootInputsToSign(withdrawalInputByPaymentTypeArray); - const nativeSegwitInputsToSign = getNativeSegwitInputsToSign(withdrawalInputByPaymentTypeArray); - await updateTaprootInputs(taprootInputsToSign, taprootDerivedPublicKey, this.masterFingerprint, formattedDepositPSBT); - await updateNativeSegwitInputs(nativeSegwitInputsToSign, nativeSegwitDerivedPublicKey, this.masterFingerprint, formattedDepositPSBT, this.bitcoinBlockchainAPI); - return formattedDepositPSBT; - } - async signPSBT(psbt, transactionType) { - try { - const { nativeSegwitWalletPolicy, taprootMultisigWalletPolicy, taprootMultisigWalletPolicyHMac, } = this.getPolicyInformation(); - let signatures; - let transaction; - switch (transactionType) { - case 'funding': - signatures = await this.ledgerApp.signPsbt(psbt.toBase64(), nativeSegwitWalletPolicy, null); - addNativeSegwitSignaturesToPSBT(psbt, signatures); - transaction = Transaction.fromPSBT(psbt.toBuffer()); - transaction.finalize(); - return transaction; - case 'deposit': - signatures = await this.ledgerApp.signPsbt(psbt.toBase64(), taprootMultisigWalletPolicy, taprootMultisigWalletPolicyHMac); - addTaprootInputSignaturesToPSBT(psbt, signatures); - signatures = await this.ledgerApp.signPsbt(psbt.toBase64(), nativeSegwitWalletPolicy, null); - addNativeSegwitSignaturesToPSBT(psbt, signatures); - transaction = Transaction.fromPSBT(psbt.toBuffer()); - return transaction; - case 'closing': - signatures = await this.ledgerApp.signPsbt(psbt.toBase64(), taprootMultisigWalletPolicy, taprootMultisigWalletPolicyHMac); - addTaprootInputSignaturesToPSBT(psbt, signatures); - transaction = Transaction.fromPSBT(psbt.toBuffer()); - return transaction; - default: - throw new Error('Invalid Transaction Type'); - } - } - catch (error) { - throw new Error(`Error signing PSBT: ${error}`); - } - } -} diff --git a/dist/dlc-handlers/private-key-dlc-handler.d.ts b/dist/dlc-handlers/private-key-dlc-handler.d.ts deleted file mode 100644 index 01c8334..0000000 --- a/dist/dlc-handlers/private-key-dlc-handler.d.ts +++ /dev/null @@ -1,22 +0,0 @@ -import { Transaction } from '@scure/btc-signer'; -import { Network } from 'bitcoinjs-lib'; -import { PaymentInformation } from '../models/bitcoin-models.js'; -import { RawVault } from '../models/ethereum-models.js'; -export declare class PrivateKeyDLCHandler { - private derivedKeyPair; - payment: PaymentInformation | undefined; - private bitcoinNetwork; - private bitcoinBlockchainAPI; - private bitcoinBlockchainFeeRecommendationAPI; - constructor(bitcoinWalletPrivateKey: string, walletAccountIndex: number, bitcoinNetwork: Network, bitcoinBlockchainAPI?: string, bitcoinBlockchainFeeRecommendationAPI?: string); - private setPayment; - private getPayment; - getTaprootDerivedPublicKey(): string; - getVaultRelatedAddress(paymentType: 'p2wpkh' | 'p2tr'): string; - private getPrivateKey; - private createPayments; - createFundingPSBT(vault: RawVault, bitcoinAmount: bigint, attestorGroupPublicKey: string, feeRateMultiplier?: number, customFeeRate?: bigint): Promise; - createWithdrawalPSBT(vault: RawVault, withdrawAmount: bigint, attestorGroupPublicKey: string, fundingTransactionID: string, feeRateMultiplier?: number, customFeeRate?: bigint): Promise; - signPSBT(psbt: Transaction, transactionType: 'funding' | 'deposit' | 'withdraw'): Transaction; - createDepositPSBT(depositAmount: bigint, vault: RawVault, attestorGroupPublicKey: string, fundingTransactionID: string, feeRateMultiplier?: number, customFeeRate?: bigint): Promise; -} diff --git a/dist/dlc-handlers/private-key-dlc-handler.js b/dist/dlc-handlers/private-key-dlc-handler.js deleted file mode 100644 index d85ec6e..0000000 --- a/dist/dlc-handlers/private-key-dlc-handler.js +++ /dev/null @@ -1,176 +0,0 @@ -import { bytesToHex } from '@noble/hashes/utils'; -import { p2wpkh } from '@scure/btc-signer'; -import { bitcoin, regtest, testnet } from 'bitcoinjs-lib/src/networks.js'; -import { createTaprootMultisigPayment, deriveUnhardenedKeyPairFromRootPrivateKey, deriveUnhardenedPublicKey, finalizeUserInputs, getBalance, getFeeRate, getUnspendableKeyCommittedToUUID, } from '../functions/bitcoin/bitcoin-functions.js'; -import { createDepositTransaction, createFundingTransaction, createWithdrawalTransaction, } from '../functions/bitcoin/psbt-functions.js'; -export class PrivateKeyDLCHandler { - derivedKeyPair; - payment; - bitcoinNetwork; - bitcoinBlockchainAPI; - bitcoinBlockchainFeeRecommendationAPI; - constructor(bitcoinWalletPrivateKey, walletAccountIndex, bitcoinNetwork, bitcoinBlockchainAPI, bitcoinBlockchainFeeRecommendationAPI) { - switch (bitcoinNetwork) { - case bitcoin: - this.bitcoinBlockchainAPI = 'https://mempool.space/api'; - this.bitcoinBlockchainFeeRecommendationAPI = - 'https://mempool.space/api/v1/fees/recommended'; - break; - case testnet: - this.bitcoinBlockchainAPI = 'https://mempool.space/testnet/api'; - this.bitcoinBlockchainFeeRecommendationAPI = - 'https://mempool.space/testnet/api/v1/fees/recommended'; - break; - case regtest: - if (bitcoinBlockchainAPI === undefined || - bitcoinBlockchainFeeRecommendationAPI === undefined) { - throw new Error('Regtest requires a Bitcoin Blockchain API and a Bitcoin Blockchain Fee Recommendation API'); - } - this.bitcoinBlockchainAPI = bitcoinBlockchainAPI; - this.bitcoinBlockchainFeeRecommendationAPI = bitcoinBlockchainFeeRecommendationAPI; - break; - default: - throw new Error('Invalid Bitcoin Network'); - } - this.bitcoinNetwork = bitcoinNetwork; - const nativeSegwitDerivedKeyPair = deriveUnhardenedKeyPairFromRootPrivateKey(bitcoinWalletPrivateKey, bitcoinNetwork, 'p2wpkh', walletAccountIndex); - const taprootDerivedKeyPair = deriveUnhardenedKeyPairFromRootPrivateKey(bitcoinWalletPrivateKey, bitcoinNetwork, 'p2tr', walletAccountIndex); - this.derivedKeyPair = { - taprootDerivedKeyPair, - nativeSegwitDerivedKeyPair, - }; - } - setPayment(nativeSegwitPayment, taprootMultisigPayment) { - this.payment = { - nativeSegwitPayment, - taprootMultisigPayment, - }; - } - getPayment() { - if (!this.payment) { - throw new Error('Payment Information not set'); - } - return this.payment; - } - getTaprootDerivedPublicKey() { - return bytesToHex(this.derivedKeyPair.taprootDerivedKeyPair.publicKey); - } - getVaultRelatedAddress(paymentType) { - const payment = this.payment; - if (payment === undefined) { - throw new Error('Payment objects have not been set'); - } - let address; - switch (paymentType) { - case 'p2wpkh': - if (!payment.nativeSegwitPayment.address) { - throw new Error('Native Segwit Payment Address is undefined'); - } - address = payment.nativeSegwitPayment.address; - return address; - case 'p2tr': - if (!payment.taprootMultisigPayment.address) { - throw new Error('Taproot Multisig Payment Address is undefined'); - } - address = payment.taprootMultisigPayment.address; - return address; - default: - throw new Error('Invalid Payment Type'); - } - } - getPrivateKey(paymentType) { - const privateKey = paymentType === 'p2wpkh' - ? this.derivedKeyPair.nativeSegwitDerivedKeyPair.privateKey - : this.derivedKeyPair.taprootDerivedKeyPair.privateKey; - if (!privateKey) { - throw new Error('Private Key is Undefined'); - } - return privateKey; - } - createPayments(vaultUUID, attestorGroupPublicKey) { - try { - const unspendablePublicKey = getUnspendableKeyCommittedToUUID(vaultUUID, this.bitcoinNetwork); - const unspendableDerivedPublicKey = deriveUnhardenedPublicKey(unspendablePublicKey, this.bitcoinNetwork); - const attestorDerivedPublicKey = deriveUnhardenedPublicKey(attestorGroupPublicKey, this.bitcoinNetwork); - const nativeSegwitPayment = p2wpkh(this.derivedKeyPair.nativeSegwitDerivedKeyPair.publicKey, this.bitcoinNetwork); - const taprootMultisigPayment = createTaprootMultisigPayment(unspendableDerivedPublicKey, attestorDerivedPublicKey, this.derivedKeyPair.taprootDerivedKeyPair.publicKey, this.bitcoinNetwork); - this.setPayment(nativeSegwitPayment, taprootMultisigPayment); - return { - nativeSegwitPayment, - taprootMultisigPayment, - }; - } - catch (error) { - throw new Error(`Error creating required Payment objects: ${error}`); - } - } - async createFundingPSBT(vault, bitcoinAmount, attestorGroupPublicKey, feeRateMultiplier, customFeeRate) { - const { nativeSegwitPayment, taprootMultisigPayment } = this.createPayments(vault.uuid, attestorGroupPublicKey); - if (nativeSegwitPayment.address === undefined || taprootMultisigPayment.address === undefined) { - throw new Error('Could not get Addresses from Payments'); - } - const addressBalance = await getBalance(nativeSegwitPayment.address, this.bitcoinBlockchainAPI); - if (BigInt(addressBalance) < vault.valueLocked.toBigInt()) { - throw new Error('Insufficient Funds'); - } - const feeRate = customFeeRate ?? - BigInt(await getFeeRate(this.bitcoinBlockchainFeeRecommendationAPI, feeRateMultiplier)); - const fundingTransaction = await createFundingTransaction(bitcoinAmount, this.bitcoinNetwork, taprootMultisigPayment.address, nativeSegwitPayment, feeRate, vault.btcFeeRecipient, vault.btcMintFeeBasisPoints.toBigInt(), this.bitcoinBlockchainAPI); - return fundingTransaction; - } - async createWithdrawalPSBT(vault, withdrawAmount, attestorGroupPublicKey, fundingTransactionID, feeRateMultiplier, customFeeRate) { - try { - const { nativeSegwitPayment, taprootMultisigPayment } = this.createPayments(vault.uuid, attestorGroupPublicKey); - if (taprootMultisigPayment.address === undefined || - nativeSegwitPayment.address === undefined) { - throw new Error('Payment Address is undefined'); - } - const feeRate = customFeeRate ?? - BigInt(await getFeeRate(this.bitcoinBlockchainFeeRecommendationAPI, feeRateMultiplier)); - const withdrawalTransaction = await createWithdrawalTransaction(this.bitcoinBlockchainAPI, withdrawAmount, this.bitcoinNetwork, fundingTransactionID, taprootMultisigPayment, nativeSegwitPayment.address, feeRate, vault.btcFeeRecipient, vault.btcRedeemFeeBasisPoints.toBigInt()); - return withdrawalTransaction; - } - catch (error) { - throw new Error(`Error creating Withdrawal PSBT: ${error}`); - } - } - signPSBT(psbt, transactionType) { - switch (transactionType) { - case 'funding': - psbt.sign(this.getPrivateKey('p2wpkh')); - psbt.finalize(); - break; - case 'deposit': - try { - psbt.sign(this.getPrivateKey('p2tr')); - } - catch (error) { - // this can happen if there are no tr inputs to sign - } - try { - psbt.sign(this.getPrivateKey('p2wpkh')); - } - catch (error) { - // this can happen if there are no p2wpkh inputs to sign - } - finalizeUserInputs(psbt, this.getPayment().nativeSegwitPayment); - break; - case 'withdraw': - psbt.sign(this.getPrivateKey('p2tr')); - break; - default: - throw new Error('Invalid Transaction Type'); - } - return psbt; - } - async createDepositPSBT(depositAmount, vault, attestorGroupPublicKey, fundingTransactionID, feeRateMultiplier, customFeeRate) { - const { nativeSegwitPayment, taprootMultisigPayment } = this.createPayments(vault.uuid, attestorGroupPublicKey); - if (taprootMultisigPayment.address === undefined || nativeSegwitPayment.address === undefined) { - throw new Error('Payment Address is undefined'); - } - const feeRate = customFeeRate ?? - BigInt(await getFeeRate(this.bitcoinBlockchainFeeRecommendationAPI, feeRateMultiplier)); - const depositTransaction = await createDepositTransaction(this.bitcoinBlockchainAPI, this.bitcoinNetwork, depositAmount, fundingTransactionID, taprootMultisigPayment, nativeSegwitPayment, feeRate, vault.btcFeeRecipient, vault.btcMintFeeBasisPoints.toBigInt()); - return depositTransaction; - } -} diff --git a/dist/dlc-handlers/software-wallet-dlc-handler.d.ts b/dist/dlc-handlers/software-wallet-dlc-handler.d.ts deleted file mode 100644 index 8279a12..0000000 --- a/dist/dlc-handlers/software-wallet-dlc-handler.d.ts +++ /dev/null @@ -1,21 +0,0 @@ -import { Transaction } from '@scure/btc-signer'; -import { Network } from 'bitcoinjs-lib'; -import { PaymentInformation } from '../models/bitcoin-models.js'; -import { RawVault } from '../models/ethereum-models.js'; -export declare class SoftwareWalletDLCHandler { - private nativeSegwitDerivedPublicKey; - private taprootDerivedPublicKey; - payment: PaymentInformation | undefined; - private bitcoinNetwork; - private bitcoinBlockchainAPI; - private bitcoinBlockchainFeeRecommendationAPI; - constructor(nativeSegwitDerivedPublicKey: string, taprootDerivedPublicKey: string, bitcoinNetwork: Network, bitcoinBlockchainAPI?: string, bitcoinBlockchainFeeRecommendationAPI?: string); - private setPayment; - private getPayment; - getTaprootDerivedPublicKey(): string; - getVaultRelatedAddress(paymentType: 'p2wpkh' | 'p2tr'): string; - private createPayments; - createFundingPSBT(vault: RawVault, bitcoinAmount: bigint, attestorGroupPublicKey: string, feeRateMultiplier?: number, customFeeRate?: bigint): Promise; - createWithdrawalPSBT(vault: RawVault, withdrawAmount: bigint, attestorGroupPublicKey: string, fundingTransactionID: string, feeRateMultiplier?: number, customFeeRate?: bigint): Promise; - createDepositPSBT(depositAmount: bigint, vault: RawVault, attestorGroupPublicKey: string, fundingTransactionID: string, feeRateMultiplier?: number, customFeeRate?: bigint): Promise; -} diff --git a/dist/dlc-handlers/software-wallet-dlc-handler.js b/dist/dlc-handlers/software-wallet-dlc-handler.js deleted file mode 100644 index 50988c4..0000000 --- a/dist/dlc-handlers/software-wallet-dlc-handler.js +++ /dev/null @@ -1,140 +0,0 @@ -import { p2wpkh } from '@scure/btc-signer'; -import { bitcoin, regtest, testnet } from 'bitcoinjs-lib/src/networks.js'; -import { createTaprootMultisigPayment, deriveUnhardenedPublicKey, getBalance, getFeeRate, getUnspendableKeyCommittedToUUID, } from '../functions/bitcoin/bitcoin-functions.js'; -import { createDepositTransaction, createFundingTransaction, createWithdrawalTransaction, } from '../functions/bitcoin/psbt-functions.js'; -export class SoftwareWalletDLCHandler { - nativeSegwitDerivedPublicKey; - taprootDerivedPublicKey; - payment; - bitcoinNetwork; - bitcoinBlockchainAPI; - bitcoinBlockchainFeeRecommendationAPI; - constructor(nativeSegwitDerivedPublicKey, taprootDerivedPublicKey, bitcoinNetwork, bitcoinBlockchainAPI, bitcoinBlockchainFeeRecommendationAPI) { - switch (bitcoinNetwork) { - case bitcoin: - this.bitcoinBlockchainAPI = 'https://mempool.space/api'; - this.bitcoinBlockchainFeeRecommendationAPI = - 'https://mempool.space/api/v1/fees/recommended'; - break; - case testnet: - this.bitcoinBlockchainAPI = 'https://mempool.space/testnet/api'; - this.bitcoinBlockchainFeeRecommendationAPI = - 'https://mempool.space/testnet/api/v1/fees/recommended'; - break; - case regtest: - if (bitcoinBlockchainAPI === undefined || - bitcoinBlockchainFeeRecommendationAPI === undefined) { - throw new Error('Regtest requires a Bitcoin Blockchain API and a Bitcoin Blockchain Fee Recommendation API'); - } - this.bitcoinBlockchainAPI = bitcoinBlockchainAPI; - this.bitcoinBlockchainFeeRecommendationAPI = bitcoinBlockchainFeeRecommendationAPI; - break; - default: - throw new Error('Invalid Bitcoin Network'); - } - this.bitcoinNetwork = bitcoinNetwork; - this.nativeSegwitDerivedPublicKey = nativeSegwitDerivedPublicKey; - this.taprootDerivedPublicKey = taprootDerivedPublicKey; - } - setPayment(nativeSegwitPayment, taprootMultisigPayment) { - this.payment = { - nativeSegwitPayment, - taprootMultisigPayment, - }; - } - getPayment() { - if (!this.payment) { - throw new Error('Payment Information not set'); - } - return this.payment; - } - getTaprootDerivedPublicKey() { - return this.taprootDerivedPublicKey; - } - getVaultRelatedAddress(paymentType) { - const payment = this.getPayment(); - if (payment === undefined) { - throw new Error('Payment objects have not been set'); - } - let address; - switch (paymentType) { - case 'p2wpkh': - if (!payment.nativeSegwitPayment.address) { - throw new Error('Native Segwit Payment Address is undefined'); - } - address = payment.nativeSegwitPayment.address; - return address; - case 'p2tr': - if (!payment.taprootMultisigPayment.address) { - throw new Error('Taproot Multisig Payment Address is undefined'); - } - address = payment.taprootMultisigPayment.address; - return address; - default: - throw new Error('Invalid Payment Type'); - } - } - async createPayments(vaultUUID, attestorGroupPublicKey) { - try { - const nativeSegwitPayment = p2wpkh(Buffer.from(this.nativeSegwitDerivedPublicKey, 'hex'), this.bitcoinNetwork); - const unspendablePublicKey = getUnspendableKeyCommittedToUUID(vaultUUID, this.bitcoinNetwork); - const unspendableDerivedPublicKey = deriveUnhardenedPublicKey(unspendablePublicKey, this.bitcoinNetwork); - const attestorDerivedPublicKey = deriveUnhardenedPublicKey(attestorGroupPublicKey, this.bitcoinNetwork); - const taprootMultisigPayment = createTaprootMultisigPayment(unspendableDerivedPublicKey, attestorDerivedPublicKey, Buffer.from(this.taprootDerivedPublicKey, 'hex'), this.bitcoinNetwork); - this.setPayment(nativeSegwitPayment, taprootMultisigPayment); - return { - nativeSegwitPayment, - taprootMultisigPayment, - }; - } - catch (error) { - throw new Error(`Error creating required wallet information: ${error}`); - } - } - async createFundingPSBT(vault, bitcoinAmount, attestorGroupPublicKey, feeRateMultiplier, customFeeRate) { - try { - const { nativeSegwitPayment, taprootMultisigPayment } = await this.createPayments(vault.uuid, attestorGroupPublicKey); - if (taprootMultisigPayment.address === undefined || - nativeSegwitPayment.address === undefined) { - throw new Error('Payment Address is undefined'); - } - const feeRate = customFeeRate ?? - BigInt(await getFeeRate(this.bitcoinBlockchainFeeRecommendationAPI, feeRateMultiplier)); - const addressBalance = await getBalance(nativeSegwitPayment.address, this.bitcoinBlockchainAPI); - if (BigInt(addressBalance) < vault.valueLocked.toBigInt()) { - throw new Error('Insufficient Funds'); - } - const fundingTransaction = await createFundingTransaction(bitcoinAmount, this.bitcoinNetwork, taprootMultisigPayment.address, nativeSegwitPayment, feeRate, vault.btcFeeRecipient, vault.btcMintFeeBasisPoints.toBigInt(), this.bitcoinBlockchainAPI); - return fundingTransaction; - } - catch (error) { - throw new Error(`Error creating Funding PSBT: ${error}`); - } - } - async createWithdrawalPSBT(vault, withdrawAmount, attestorGroupPublicKey, fundingTransactionID, feeRateMultiplier, customFeeRate) { - try { - const { nativeSegwitPayment, taprootMultisigPayment } = await this.createPayments(vault.uuid, attestorGroupPublicKey); - if (taprootMultisigPayment.address === undefined || - nativeSegwitPayment.address === undefined) { - throw new Error('Payment Address is undefined'); - } - const feeRate = customFeeRate ?? - BigInt(await getFeeRate(this.bitcoinBlockchainFeeRecommendationAPI, feeRateMultiplier)); - const withdrawalTransaction = await createWithdrawalTransaction(this.bitcoinBlockchainAPI, withdrawAmount, this.bitcoinNetwork, fundingTransactionID, taprootMultisigPayment, nativeSegwitPayment.address, feeRate, vault.btcFeeRecipient, vault.btcRedeemFeeBasisPoints.toBigInt()); - return withdrawalTransaction; - } - catch (error) { - throw new Error(`Error creating Withdrawal PSBT: ${error}`); - } - } - async createDepositPSBT(depositAmount, vault, attestorGroupPublicKey, fundingTransactionID, feeRateMultiplier, customFeeRate) { - const { nativeSegwitPayment, taprootMultisigPayment } = await this.createPayments(vault.uuid, attestorGroupPublicKey); - if (taprootMultisigPayment.address === undefined || nativeSegwitPayment.address === undefined) { - throw new Error('Payment Address is undefined'); - } - const feeRate = customFeeRate ?? - BigInt(await getFeeRate(this.bitcoinBlockchainFeeRecommendationAPI, feeRateMultiplier)); - const depositTransaction = await createDepositTransaction(this.bitcoinBlockchainAPI, this.bitcoinNetwork, depositAmount, fundingTransactionID, taprootMultisigPayment, nativeSegwitPayment, feeRate, vault.btcFeeRecipient, vault.btcMintFeeBasisPoints.toBigInt()); - return depositTransaction; - } -} diff --git a/dist/functions/bitcoin/bitcoin-functions.d.ts b/dist/functions/bitcoin/bitcoin-functions.d.ts deleted file mode 100644 index ba48ee7..0000000 --- a/dist/functions/bitcoin/bitcoin-functions.d.ts +++ /dev/null @@ -1,89 +0,0 @@ -/// -import { Transaction } from '@scure/btc-signer'; -import { P2Ret, P2TROut } from '@scure/btc-signer/payment'; -import { BIP32Interface } from 'bip32'; -import { Network } from 'bitcoinjs-lib'; -import { BitcoinInputSigningConfig, BitcoinTransaction, BitcoinTransactionVectorOutput, PaymentTypes } from '../../models/bitcoin-models.js'; -export declare function getFeeAmount(bitcoinAmount: number, feeBasisPoints: number): number; -/** - * Derives the Public Key at the Unhardened Path (0/0) from a given Extended Public Key. - * @param extendedPublicKey - The base58-encoded Extended Public Key. - * @param bitcoinNetwork - The Bitcoin Network to use. - * @returns The Public Key derived at the Unhardened Path. - */ -export declare function deriveUnhardenedPublicKey(extendedPublicKey: string, bitcoinNetwork: Network): Buffer; -/** - * Derives the Account Key Pair from the Root Private Key. - * @param rootPrivateKey - The Root Private Key. - * @param bitcoinNetwork - The Bitcoin Network to use. - * @param paymentType - The Payment Type to use. - * @param accountIndex - The Account Index to use. - * @returns The Account Key Pair. - */ -export declare function deriveUnhardenedKeyPairFromRootPrivateKey(rootPrivateKey: string, bitcoinNetwork: Network, paymentType: 'p2tr' | 'p2wpkh', accountIndex: number): BIP32Interface; -export declare function getXOnlyPublicKey(publicKey: Buffer): Buffer; -/** - * Creates a Taproot Multisig Payment. - * @param unspendableDerivedPublicKey - The Unspendable Derived Public Key. - * @param attestorDerivedPublicKey - The Attestor Derived Public Key. - * @param userDerivedPublicKey - The User Derived Public Key. - * @param bitcoinNetwork - The Bitcoin Network to use. - * @returns The Taproot Multisig Payment. - */ -export declare function createTaprootMultisigPayment(unspendableDerivedPublicKey: Buffer, publicKeyA: Buffer, publicKeyB: Buffer, bitcoinNetwork: Network): P2TROut; -/** - * Fetches the fee rate from the bitcoin blockchain API. - * - * @returns A promise that resolves to the hour fee rate. - */ -export declare function getFeeRate(bitcoinBlockchainAPIFeeURL: string, feeRateMultiplier?: number): Promise; -/** - * Gets the UTXOs of the User's Native Segwit Address. - * - * @param bitcoinNativeSegwitTransaction - The User's Native Segwit Payment Transaction. - * @returns A Promise that resolves to the UTXOs of the User's Native Segwit Address. - */ -export declare function getUTXOs(bitcoinNativeSegwitTransaction: P2Ret | P2TROut, bitcoinBlockchainAPIURL: string): Promise; -/** - * Gets the Balance of the User's Bitcoin Address. - * - * @param bitcoinAddress - The User's Bitcoin Address. - * @returns A Promise that resolves to the Balance of the User's Bitcoin Address. - */ -export declare function getBalance(bitcoinAddress: string, bitcoinBlockchainAPIURL: string): Promise; -/** - * Gets the Fee Recipient's Address from the Rcipient's Public Key. - * @param feePublicKey - The Fee Recipient's Public Key. - * @param bitcoinNetwork - The Bitcoin Network to use. - * @returns The Fee Recipient's Address. - */ -export declare function getFeeRecipientAddressFromPublicKey(feePublicKey: string, bitcoinNetwork: Network): string; -/** - * Creates an Unspendable Key Committed to the Vault UUID. - * @param vaultUUID - The UUID of the Vault. - * @param bitcoinNetwork - The Bitcoin Network to use. - * @returns The Unspendable Key Committed to the Vault UUID. - */ -export declare function getUnspendableKeyCommittedToUUID(vaultUUID: string, bitcoinNetwork: Network): string; -/** - * Creates the Bitcoin Input Signing Configuration. - * @param psbt - The PSBT from which to create the Configuration. - * @param bitcoinNetwork - The Bitcoin Network to use. - * @returns The Bitcoin Input Signing Configuration. - */ -export declare function createBitcoinInputSigningConfiguration(transaction: Transaction, walletAccountIndex: number, bitcoinNetwork: Network): BitcoinInputSigningConfig[]; -/** - * Returns the Bitcoin Input Signing Configuration and Payment Type Array for the given PSBT. - * @param signingConfiguration - The Bitcoin Input Signing Configuration. - * @param psbt - The PSBT. - * @param bitcoinNetwork - The Bitcoin Network to use. - */ -export declare function getInputByPaymentTypeArray(signingConfiguration: BitcoinInputSigningConfig[], psbt: Buffer, bitcoinNetwork: Network): [BitcoinInputSigningConfig, PaymentTypes][]; -export declare function getValueMatchingInputFromTransaction(bitcoinTransaction: BitcoinTransaction, bitcoinValue: number): BitcoinTransactionVectorOutput; -export declare function validateScript(script: Uint8Array, outputScript: Uint8Array): boolean; -export declare function finalizeUserInputs(transaction: Transaction, userPayment: P2TROut | P2Ret): Transaction; -/** - * Converts an ECDSA Public Key to a Schnorr Public Key. - * @param publicKey - The ECDSA Public Key. - */ -export declare function ecdsaPublicKeyToSchnorr(publicKey: Buffer): Buffer; diff --git a/dist/functions/bitcoin/bitcoin-functions.js b/dist/functions/bitcoin/bitcoin-functions.js deleted file mode 100644 index c41536f..0000000 --- a/dist/functions/bitcoin/bitcoin-functions.js +++ /dev/null @@ -1,334 +0,0 @@ -import { Address, OutScript, Transaction, p2ms, p2pk, p2tr, p2tr_ns, p2wpkh, } from '@scure/btc-signer'; -import { BIP32Factory } from 'bip32'; -import { bitcoin, regtest, testnet } from 'bitcoinjs-lib/src/networks.js'; -import { Decimal } from 'decimal.js'; -import * as ellipticCurveCryptography from 'tiny-secp256k1'; -import { createRangeFromLength, isDefined, isUndefined } from '../../utilities/index.js'; -const TAPROOT_UNSPENDABLE_KEY_HEX = '0250929b74c1a04954b78b4b6035e97a5e078a5a0f28ec96d547bfee9ace803ac0'; -const ECDSA_PUBLIC_KEY_LENGTH = 33; -const bip32 = BIP32Factory(ellipticCurveCryptography); -export function getFeeAmount(bitcoinAmount, feeBasisPoints) { - const feePercentage = new Decimal(feeBasisPoints).dividedBy(100); - return new Decimal(bitcoinAmount).times(feePercentage.dividedBy(100)).toNumber(); -} -/** - * Derives the Public Key at the Unhardened Path (0/0) from a given Extended Public Key. - * @param extendedPublicKey - The base58-encoded Extended Public Key. - * @param bitcoinNetwork - The Bitcoin Network to use. - * @returns The Public Key derived at the Unhardened Path. - */ -export function deriveUnhardenedPublicKey(extendedPublicKey, bitcoinNetwork) { - return bip32.fromBase58(extendedPublicKey, bitcoinNetwork).derivePath('0/0').publicKey; -} -/** - * Derives the Account Key Pair from the Root Private Key. - * @param rootPrivateKey - The Root Private Key. - * @param bitcoinNetwork - The Bitcoin Network to use. - * @param paymentType - The Payment Type to use. - * @param accountIndex - The Account Index to use. - * @returns The Account Key Pair. - */ -export function deriveUnhardenedKeyPairFromRootPrivateKey(rootPrivateKey, bitcoinNetwork, paymentType, accountIndex) { - switch (bitcoinNetwork) { - case bitcoin: - switch (paymentType) { - case 'p2wpkh': - return bip32 - .fromBase58(rootPrivateKey, bitcoinNetwork) - .derivePath(`m/84'/0'/${accountIndex}'/0/0`); - case 'p2tr': - return bip32 - .fromBase58(rootPrivateKey, bitcoinNetwork) - .derivePath(`m/86'/0'/${accountIndex}'/0/0`); - default: - throw new Error('Unsupported Payment Type'); - } - case testnet: - case regtest: - switch (paymentType) { - case 'p2wpkh': - return bip32 - .fromBase58(rootPrivateKey, bitcoinNetwork) - .derivePath(`m/84'/1'/${accountIndex}'/0/0`); - case 'p2tr': - return bip32 - .fromBase58(rootPrivateKey, bitcoinNetwork) - .derivePath(`m/86'/1'/${accountIndex}'/0/0`); - default: - throw new Error('Unsupported Payment Type'); - } - default: - throw new Error('Unsupported Bitcoin Network'); - } -} -export function getXOnlyPublicKey(publicKey) { - return publicKey.length === 32 ? publicKey : publicKey.subarray(1); -} -/** - * Creates a Taproot Multisig Payment. - * @param unspendableDerivedPublicKey - The Unspendable Derived Public Key. - * @param attestorDerivedPublicKey - The Attestor Derived Public Key. - * @param userDerivedPublicKey - The User Derived Public Key. - * @param bitcoinNetwork - The Bitcoin Network to use. - * @returns The Taproot Multisig Payment. - */ -export function createTaprootMultisigPayment(unspendableDerivedPublicKey, publicKeyA, publicKeyB, bitcoinNetwork) { - const unspendableDerivedPublicKeyFormatted = getXOnlyPublicKey(unspendableDerivedPublicKey); - const publicKeys = [getXOnlyPublicKey(publicKeyA), getXOnlyPublicKey(publicKeyB)]; - const sortedArray = publicKeys.sort((a, b) => (a.toString('hex') > b.toString('hex') ? 1 : -1)); - const taprootMultiLeafWallet = p2tr_ns(2, sortedArray); - return p2tr(unspendableDerivedPublicKeyFormatted, taprootMultiLeafWallet, bitcoinNetwork); -} -/** - * Evaluates the fee rate from the bitcoin blockchain API. - * - * @returns The fee rate. - */ -function checkFeeRate(feeRate) { - if (!feeRate || feeRate < 2) { - return 2; - } - return feeRate; -} -/** - * Fetches the fee rate from the bitcoin blockchain API. - * - * @returns A promise that resolves to the hour fee rate. - */ -export async function getFeeRate(bitcoinBlockchainAPIFeeURL, feeRateMultiplier) { - const response = await fetch(bitcoinBlockchainAPIFeeURL); - if (!response.ok) { - throw new Error(`Bitcoin Blockchain Fee Rate Response was not OK: ${response.statusText}`); - } - let feeRates; - try { - feeRates = await response.json(); - } - catch (error) { - throw new Error(`Error parsing Bitcoin Blockchain Fee Rate Response JSON: ${error}`); - } - const feeRate = checkFeeRate(feeRates.fastestFee); - const multipliedFeeRate = feeRate * (feeRateMultiplier ?? 1); - return multipliedFeeRate; -} -/** - * Gets the UTXOs of the User's Native Segwit Address. - * - * @param bitcoinNativeSegwitTransaction - The User's Native Segwit Payment Transaction. - * @returns A Promise that resolves to the UTXOs of the User's Native Segwit Address. - */ -export async function getUTXOs(bitcoinNativeSegwitTransaction, bitcoinBlockchainAPIURL) { - const utxoEndpoint = `${bitcoinBlockchainAPIURL}/address/${bitcoinNativeSegwitTransaction.address}/utxo`; - const utxoResponse = await fetch(utxoEndpoint); - if (!utxoResponse.ok) { - throw new Error(`Error getting UTXOs: ${utxoResponse.statusText}`); - } - const userUTXOs = await utxoResponse.json(); - const modifiedUTXOs = await Promise.all(userUTXOs.map(async (utxo) => { - return { - ...bitcoinNativeSegwitTransaction, - txid: utxo.txid, - index: utxo.vout, - value: utxo.value, - witnessUtxo: { - script: bitcoinNativeSegwitTransaction.script, - amount: BigInt(utxo.value), - }, - redeemScript: bitcoinNativeSegwitTransaction.redeemScript, - }; - })); - return modifiedUTXOs; -} -/** - * Gets the Balance of the User's Bitcoin Address. - * - * @param bitcoinAddress - The User's Bitcoin Address. - * @returns A Promise that resolves to the Balance of the User's Bitcoin Address. - */ -export async function getBalance(bitcoinAddress, bitcoinBlockchainAPIURL) { - const utxoResponse = await fetch(`${bitcoinBlockchainAPIURL}/address/${bitcoinAddress}/utxo`); - if (!utxoResponse.ok) { - throw new Error(`Error getting UTXOs: ${utxoResponse.statusText}`); - } - const userUTXOs = await utxoResponse.json(); - const balanceInSats = userUTXOs.reduce((total, utxo) => total + utxo.value, 0); - return balanceInSats; -} -/** - * Gets the Fee Recipient's Address from the Rcipient's Public Key. - * @param feePublicKey - The Fee Recipient's Public Key. - * @param bitcoinNetwork - The Bitcoin Network to use. - * @returns The Fee Recipient's Address. - */ -export function getFeeRecipientAddressFromPublicKey(feePublicKey, bitcoinNetwork) { - const feePublicKeyBuffer = Buffer.from(feePublicKey, 'hex'); - const { address } = p2wpkh(feePublicKeyBuffer, bitcoinNetwork); - if (!address) - throw new Error('Could not create Fee Address from Public Key'); - return address; -} -/** - * Creates an Unspendable Key Committed to the Vault UUID. - * @param vaultUUID - The UUID of the Vault. - * @param bitcoinNetwork - The Bitcoin Network to use. - * @returns The Unspendable Key Committed to the Vault UUID. - */ -export function getUnspendableKeyCommittedToUUID(vaultUUID, bitcoinNetwork) { - const publicKeyBuffer = Buffer.from(TAPROOT_UNSPENDABLE_KEY_HEX, 'hex'); - const chainCodeBuffer = Buffer.from(vaultUUID.slice(2), 'hex'); - const unspendablePublicKey = bip32 - .fromPublicKey(publicKeyBuffer, chainCodeBuffer, bitcoinNetwork) - .toBase58(); - return unspendablePublicKey; -} -/** - * Returns the Payment Type of the Input. - * @param index - A number that refers to the position of the output that will be utilized as an input. - * @param input - The Input. - * @param bitcoinNetwork - The Bitcoin Network to use. - */ -function getInputPaymentType(index, input, bitcoinNetwork) { - const bitcoinAddress = getBitcoinInputAddress(index, input, bitcoinNetwork); - if (bitcoinAddress === '') - throw new Error('Bitcoin Address is empty'); - if (bitcoinAddress.startsWith('bc1p') || - bitcoinAddress.startsWith('tb1p') || - bitcoinAddress.startsWith('bcrt1p')) - return 'p2tr'; - if (bitcoinAddress.startsWith('bc1q') || - bitcoinAddress.startsWith('tb1q') || - bitcoinAddress.startsWith('bcrt1q')) - return 'p2wpkh'; - throw new Error('Unable to infer payment type from BitcoinAddress'); -} -/** - * Returns the Bitcoin Address of the Input. - * @param index - A number that refers to the position of the output that will be utilized as an input. - * @param input - The Input. - * @param bitcoinNetwork - The Bitcoin Network to use. - */ -function getBitcoinInputAddress(index, input, bitcoinNetwork) { - if (isDefined(input.witnessUtxo)) - return getAddressFromOutScript(input.witnessUtxo.script, bitcoinNetwork); - if (isDefined(input.nonWitnessUtxo)) - return getAddressFromOutScript(input.nonWitnessUtxo.outputs[index]?.script, bitcoinNetwork); - return ''; -} -/** - * Returns the Bitcoin Address from the Output Script. - * @param script - The Output Script. - * @param bitcoinNetwork - The Bitcoin Network to use. - */ -function getAddressFromOutScript(script, bitcoinNetwork) { - const outputScript = OutScript.decode(script); - switch (outputScript.type) { - case 'pkh': - case 'sh': - case 'wpkh': - case 'wsh': - return Address(bitcoinNetwork).encode({ - type: outputScript.type, - hash: outputScript.hash, - }); - case 'tr': - return Address(bitcoinNetwork).encode({ - type: outputScript.type, - pubkey: outputScript.pubkey, - }); - case 'ms': - return p2ms(outputScript.m, outputScript.pubkeys).address ?? ''; - case 'pk': - return p2pk(outputScript.pubkey, bitcoinNetwork).address ?? ''; - case 'tr_ms': - case 'tr_ns': - throw new Error('Unsupported Script Type'); - case 'unknown': - throw new Error('Unknown Script Type'); - default: - throw new Error('Unsupported Script Type'); - } -} -/** - * Creates the Bitcoin Input Signing Configuration. - * @param psbt - The PSBT from which to create the Configuration. - * @param bitcoinNetwork - The Bitcoin Network to use. - * @returns The Bitcoin Input Signing Configuration. - */ -export function createBitcoinInputSigningConfiguration(transaction, walletAccountIndex, bitcoinNetwork) { - const networkIndex = bitcoinNetwork === bitcoin ? 0 : 1; - const nativeSegwitDerivationPath = `m/84'/${networkIndex}'/${walletAccountIndex}'/0/0`; - const taprootDerivationPath = `m/86'/${networkIndex}'/${walletAccountIndex}'/0/0`; - const indexesToSign = createRangeFromLength(transaction.inputsLength); - return indexesToSign.map(inputIndex => { - const input = transaction.getInput(inputIndex); - if (isUndefined(input.index)) - throw new Error('Input must have an index for payment type'); - const paymentType = getInputPaymentType(input.index, input, bitcoinNetwork); - switch (paymentType) { - case 'p2wpkh': - return { - index: inputIndex, - derivationPath: nativeSegwitDerivationPath, - }; - case 'p2tr': - return { - index: inputIndex, - derivationPath: taprootDerivationPath, - }; - default: - throw new Error('Unsupported Payment Type'); - } - }); -} -/** - * Returns the Bitcoin Input Signing Configuration and Payment Type Array for the given PSBT. - * @param signingConfiguration - The Bitcoin Input Signing Configuration. - * @param psbt - The PSBT. - * @param bitcoinNetwork - The Bitcoin Network to use. - */ -export function getInputByPaymentTypeArray(signingConfiguration, psbt, bitcoinNetwork) { - const transaction = Transaction.fromPSBT(psbt); - return signingConfiguration.map(config => { - const inputIndex = transaction.getInput(config.index).index; - if (isUndefined(inputIndex)) - throw new Error('Input must have an index for payment type'); - return [ - config, - getInputPaymentType(inputIndex, transaction.getInput(config.index), bitcoinNetwork), - ]; - }); -} -export function getValueMatchingInputFromTransaction(bitcoinTransaction, bitcoinValue) { - const valueMatchingTransactionInput = bitcoinTransaction.vout.find(output => output.value === bitcoinValue); - if (!valueMatchingTransactionInput) { - throw new Error('Could not find Value matching Input in Transaction'); - } - return valueMatchingTransactionInput; -} -export function validateScript(script, outputScript) { - return (outputScript.length === script.length && - outputScript.every((value, index) => value === script[index])); -} -export function finalizeUserInputs(transaction, userPayment) { - const userPaymentScript = userPayment.script; - createRangeFromLength(transaction.inputsLength).forEach(index => { - const inputScript = transaction.getInput(index).witnessUtxo?.script; - if (!inputScript) { - throw new Error('Could not get Input Script'); - } - if (inputScript.length === userPaymentScript.length && - inputScript.every((value, index) => value === userPaymentScript[index])) { - transaction.finalizeIdx(index); - } - }); - return transaction; -} -/** - * Converts an ECDSA Public Key to a Schnorr Public Key. - * @param publicKey - The ECDSA Public Key. - */ -export function ecdsaPublicKeyToSchnorr(publicKey) { - if (publicKey.byteLength !== ECDSA_PUBLIC_KEY_LENGTH) - throw new Error('Invalid Public Key Length'); - return publicKey.subarray(1); -} diff --git a/dist/functions/bitcoin/bitcoin-request-functions.d.ts b/dist/functions/bitcoin/bitcoin-request-functions.d.ts deleted file mode 100644 index 20ba82b..0000000 --- a/dist/functions/bitcoin/bitcoin-request-functions.d.ts +++ /dev/null @@ -1,40 +0,0 @@ -import { BitcoinTransaction } from '../../models/bitcoin-models.js'; -/** - * Fetches the Bitcoin Transaction from the Bitcoin Network. - * - * @param txID - The Transaction ID of the Bitcoin Transaction. - * @param bitcoinBlockchainAPI - The URL of the Bitcoin Blockchain API. - * @returns A Promise that resolves to the Bitcoin Transaction. - */ -export declare function fetchBitcoinTransaction(txID: string, bitcoinBlockchainAPI: string): Promise; -/** - * Broadcasts the Transaction to the Bitcoin Network. - * - * @param transaction - The Transaction to broadcast. - * @param bitcoinBlockchainAPI - The URL of the Bitcoin Blockchain API. - * @returns A Promise that resolves to the Response from the Broadcast Request. - */ -export declare function broadcastTransaction(transaction: string, bitcoinBlockchainAPI: string): Promise; -/** - * Fetches the Current Block Height of the Bitcoin Network. - * - * @param bitcoinBlockchainAPI - The URL of the Bitcoin Blockchain API. - * @returns A Promise that resolves to the Current Block Height of the Bitcoin Network. - */ -export declare function fetchBitcoinBlockchainBlockHeight(bitcoinBlockchainAPI: string): Promise; -/** - * Checks if the Bitcoin Transaction has the required number of Confirmations. - * - * @param bitcoinTransaction - The Bitcoin Transaction to check. - * @param bitcoinBlockHeight - The Current Block Height of the Bitcoin Network. - * @returns A Promise that resolves to a Boolean indicating if the Transaction has the required number of Confirmations. - */ -export declare function checkBitcoinTransactionConfirmations(bitcoinTransaction: BitcoinTransaction, bitcoinBlockHeight: number): Promise; -/** - * Return the Balance of the User's Bitcoin Address in Satoshis. - * - * @param bitcoinAddress - The User's Bitcoin Address. - * @param bitcoinBlockchainAPIURL - The URL of the Bitcoin Blockchain API. - * @returns A Promise that resolves to the Balance of the User's Bitcoin Address. - */ -export declare function getBalance(bitcoinAddress: string, bitcoinBlockchainAPIURL: string): Promise; diff --git a/dist/functions/bitcoin/bitcoin-request-functions.js b/dist/functions/bitcoin/bitcoin-request-functions.js deleted file mode 100644 index 887f98b..0000000 --- a/dist/functions/bitcoin/bitcoin-request-functions.js +++ /dev/null @@ -1,98 +0,0 @@ -/** - * Fetches the Bitcoin Transaction from the Bitcoin Network. - * - * @param txID - The Transaction ID of the Bitcoin Transaction. - * @param bitcoinBlockchainAPI - The URL of the Bitcoin Blockchain API. - * @returns A Promise that resolves to the Bitcoin Transaction. - */ -export async function fetchBitcoinTransaction(txID, bitcoinBlockchainAPI) { - try { - const bitcoinBlockchainAPITransactionEndpoint = `${bitcoinBlockchainAPI}/tx/${txID}`; - const response = await fetch(bitcoinBlockchainAPITransactionEndpoint); - if (!response.ok) - throw new Error(`Bitcoin Network Transaction Response was not OK: ${response.statusText}`); - return await response.json(); - } - catch (error) { - throw new Error(`Error fetching Bitcoin Transaction: ${error}`); - } -} -/** - * Broadcasts the Transaction to the Bitcoin Network. - * - * @param transaction - The Transaction to broadcast. - * @param bitcoinBlockchainAPI - The URL of the Bitcoin Blockchain API. - * @returns A Promise that resolves to the Response from the Broadcast Request. - */ -export async function broadcastTransaction(transaction, bitcoinBlockchainAPI) { - try { - const response = await fetch(`${bitcoinBlockchainAPI}/tx`, { - method: 'POST', - body: transaction, - }); - if (!response.ok) { - throw new Error(`Error while broadcasting Bitcoin Transaction: ${await response.text()}`); - } - const transactionID = await response.text(); - return transactionID; - } - catch (error) { - throw new Error(`Error broadcasting Transaction: ${error}`); - } -} -/** - * Fetches the Current Block Height of the Bitcoin Network. - * - * @param bitcoinBlockchainAPI - The URL of the Bitcoin Blockchain API. - * @returns A Promise that resolves to the Current Block Height of the Bitcoin Network. - */ -export async function fetchBitcoinBlockchainBlockHeight(bitcoinBlockchainAPI) { - try { - const bitcoinBlockchainBlockHeightURL = `${bitcoinBlockchainAPI}/blocks/tip/height`; - const response = await fetch(bitcoinBlockchainBlockHeightURL); - if (!response.ok) - throw new Error(`Bitcoin Network Block Height Network Response was not OK: ${response.statusText}`); - return await response.json(); - } - catch (error) { - throw new Error(`Error fetching Bitcoin Blockchain Block Height: ${error}`); - } -} -/** - * Checks if the Bitcoin Transaction has the required number of Confirmations. - * - * @param bitcoinTransaction - The Bitcoin Transaction to check. - * @param bitcoinBlockHeight - The Current Block Height of the Bitcoin Network. - * @returns A Promise that resolves to a Boolean indicating if the Transaction has the required number of Confirmations. - */ -export async function checkBitcoinTransactionConfirmations(bitcoinTransaction, bitcoinBlockHeight) { - try { - if (!bitcoinTransaction.status.block_height) { - throw new Error('Funding Transaction has no Block Height.'); - } - const confirmations = bitcoinBlockHeight - (bitcoinTransaction.status.block_height + 1); - if (confirmations >= 6) { - return true; - } - return false; - } - catch (error) { - throw new Error(`Error checking Bitcoin Transaction Confirmations: ${error}`); - } -} -/** - * Return the Balance of the User's Bitcoin Address in Satoshis. - * - * @param bitcoinAddress - The User's Bitcoin Address. - * @param bitcoinBlockchainAPIURL - The URL of the Bitcoin Blockchain API. - * @returns A Promise that resolves to the Balance of the User's Bitcoin Address. - */ -export async function getBalance(bitcoinAddress, bitcoinBlockchainAPIURL) { - const utxoResponse = await fetch(`${bitcoinBlockchainAPIURL}/address/${bitcoinAddress}/utxo`); - if (!utxoResponse.ok) { - throw new Error(`Error getting UTXOs: ${utxoResponse.statusText}`); - } - const userUTXOs = await utxoResponse.json(); - const balanceInSats = userUTXOs.reduce((total, utxo) => total + utxo.value, 0); - return balanceInSats; -} diff --git a/dist/functions/bitcoin/index.d.ts b/dist/functions/bitcoin/index.d.ts deleted file mode 100644 index b1b48d5..0000000 --- a/dist/functions/bitcoin/index.d.ts +++ /dev/null @@ -1,4 +0,0 @@ -import { finalizeUserInputs, getFeeAmount } from '../bitcoin/bitcoin-functions.js'; -import { broadcastTransaction, fetchBitcoinBlockchainBlockHeight, fetchBitcoinTransaction, getBalance } from '../bitcoin/bitcoin-request-functions.js'; -import { createFundingTransaction, createWithdrawalTransaction } from '../bitcoin/psbt-functions.js'; -export { createFundingTransaction, createWithdrawalTransaction, broadcastTransaction, fetchBitcoinBlockchainBlockHeight, fetchBitcoinTransaction, finalizeUserInputs, getFeeAmount, getBalance, }; diff --git a/dist/functions/bitcoin/index.js b/dist/functions/bitcoin/index.js deleted file mode 100644 index ad3e388..0000000 --- a/dist/functions/bitcoin/index.js +++ /dev/null @@ -1,4 +0,0 @@ -import { finalizeUserInputs, getFeeAmount } from '../bitcoin/bitcoin-functions.js'; -import { broadcastTransaction, fetchBitcoinBlockchainBlockHeight, fetchBitcoinTransaction, getBalance, } from '../bitcoin/bitcoin-request-functions.js'; -import { createFundingTransaction, createWithdrawalTransaction, } from '../bitcoin/psbt-functions.js'; -export { createFundingTransaction, createWithdrawalTransaction, broadcastTransaction, fetchBitcoinBlockchainBlockHeight, fetchBitcoinTransaction, finalizeUserInputs, getFeeAmount, getBalance, }; diff --git a/dist/functions/bitcoin/psbt-functions.d.ts b/dist/functions/bitcoin/psbt-functions.d.ts deleted file mode 100644 index bbb7ac7..0000000 --- a/dist/functions/bitcoin/psbt-functions.d.ts +++ /dev/null @@ -1,105 +0,0 @@ -/// -import { Transaction } from '@scure/btc-signer'; -import { P2Ret, P2TROut } from '@scure/btc-signer/payment'; -import { Network, Psbt } from 'bitcoinjs-lib'; -import { PartialSignature } from 'ledger-bitcoin/build/main/lib/appClient.js'; -import { BitcoinInputSigningConfig, PaymentTypes } from '../../models/bitcoin-models.js'; -/** - * Creates the initial Funding Transaction to fund the Multisig Transaction. - * - * @param bitcoinAmount - The amount of Bitcoin to fund the Transaction with. - * @param bitcoinNetwork - The Bitcoin Network to use. - * @param multisigAddress - The Multisig Address. - * @param bitcoinNativeSegwitTransaction - The user's Native Segwit Payment Transaction. - * @param feeRate - The Fee Rate to use for the Transaction. - * @param feePublicKey - The Fee Recipient's Public Key. - * @param feeBasisPoints - The Fee Basis Points. - * @returns The Funding Transaction. - */ -export declare function createFundingTransaction(bitcoinAmount: bigint, bitcoinNetwork: Network, multisigAddress: string, bitcoinNativeSegwitTransaction: P2Ret, feeRate: bigint, feePublicKey: string, feeBasisPoints: bigint, bitcoinBlockchainAPIURL: string): Promise; -/** - * Creates a Deposit Transaction. - * Uses the existing Vault's Funding Transaction ID and additional UTXOs to create the Deposit Transaction. - * The specified amount of Bitcoin is sent to the Vault's Multisig Address. - * The remaining amount is sent back to the user's address. - * - * @param bitcoinBlockchainURL - The Bitcoin Blockchain URL. - * @param bitcoinNetwork - The Bitcoin Network to use. - * @param depositAmount - The Amount of Bitcoin to deposit. - * @param vaultTransactionID - The ID of the Vault Funding Transaction. - * @param multisigPayment - The Taproot Multisig Payment Transaction. - * @param depositPayment - The User's Native Segwit or Taproot Payment Transaction which will be used to fund the Deposit Transaction. - * @param feeRate - The Fee Rate to use for the Transaction. - * @param feePublicKey - The Fee Recipient's Public Key. - * @param feeBasisPoints - The Fee Basis Points. - * @returns The Deposit Transaction. - */ -export declare function createDepositTransaction(bitcoinBlockchainURL: string, bitcoinNetwork: Network, depositAmount: bigint, vaultTransactionID: string, multisigPayment: P2TROut, depositPayment: P2TROut | P2Ret, feeRate: bigint, feePublicKey: string, feeBasisPoints: bigint): Promise; -/** - * Creates a Withdrawal Transaction. - * Uses the Funding Transaction's ID to create the Withdrawal Transaction. - * The specified amount of Bitcoin is sent to the User's Native Segwit Address. - * The remaining amount is sent back to the Multisig Transaction. - * - * @param bitcoinBlockchainURL - The Bitcoin Blockchain URL. - * @param bitcoinAmount - The Amount of Bitcoin to withdraw. - * @param bitcoinNetwork - The Bitcoin Network to use. - * @param fundingTransactionID - The ID of the Funding Transaction. - * @param multisigTransaction - The Multisig Transaction. - * @param userNativeSegwitAddress - The User's Native Segwit Address. - * @param feeRate - The Fee Rate to use for the Transaction. - * @param feePublicKey - The Fee Recipient's Public Key. - * @param feeBasisPoints - The Fee Basis Points. - * @returns The Closing Transaction. - */ -export declare function createWithdrawalTransaction(bitcoinBlockchainURL: string, bitcoinAmount: bigint, bitcoinNetwork: Network, fundingTransactionID: string, multisigTransaction: P2TROut, userNativeSegwitAddress: string, feeRate: bigint, feePublicKey: string, feeBasisPoints: bigint): Promise; -/** - * This function updates the PSBT with the necessary information to sign the inputs - * that correspond to the given input signing configuration. - * @param inputByPaymentType - An array of tuples containing the BitcoinInputSigningConfig - * and the payment type of the input. - * @param nativeSegwitPublicKey - The public key corresponding to the native segwit inputs. - * @param masterFingerprint - The master fingerprint of the wallet. - * @param psbt - The PSBT to update. - * @returns The updated PSBT. - * @throws An error if there is an issue adding the UTXO Ledger props or the BIP32 derivation. - */ -export declare function updateNativeSegwitInputs(inputsToUpdate: BitcoinInputSigningConfig[] | undefined, nativeSegwitPublicKey: Buffer, masterFingerprint: string, psbt: Psbt, bitcoinBlockchainAPIURL: string): Promise; -/** - * This function returns the Native Segwit Inputs to sign from the given input signing configuration. - * @param inputByPaymentType - An array of tuples containing the BitcoinInputSigningConfig - * and the payment type of the input. - * @returns An array of BitcoinInputSigningConfig objects. - */ -export declare function getNativeSegwitInputsToSign(inputByPaymentType: [BitcoinInputSigningConfig, PaymentTypes][]): BitcoinInputSigningConfig[]; -/** - * This function updates the PSBT with the necessary information to sign the inputs - * that correspond to the given input signing configuration. - * @param inputsToUpdate - An array of BitcoinInputSigningConfig objects. - * @param taprootPublicKey - The public key corresponding to the taproot inputs. - * @param masterFingerprint - The master fingerprint of the wallet. - * @param psbt - The PSBT to update. - * @returns The updated PSBT. - */ -export declare function updateTaprootInputs(inputsToUpdate: BitcoinInputSigningConfig[] | undefined, taprootPublicKey: Buffer, masterFingerprint: string, psbt: Psbt): Promise; -/** - * This function returns the Taproot Inputs to sign from the given input signing configuration. - * @param inputByPaymentType - An array of tuples containing the BitcoinInputSigningConfig - * and the payment type of the input. - * @returns An array of BitcoinInputSigningConfig objects. - */ -export declare function getTaprootInputsToSign(inputByPaymentType: [BitcoinInputSigningConfig, PaymentTypes][]): BitcoinInputSigningConfig[]; -/** - * This function updates the PSBT with the received Native Segwit Signatures. - * @param psbt - The PSBT to update. - * @param signatures - An array of tuples containing the index of the input and the PartialSignature. - * @returns The updated PSBT. - */ -export declare function addNativeSegwitSignaturesToPSBT(psbt: Psbt, signatures: [number, PartialSignature][]): void; -/** - * This function updates the PSBT with the received Taproot Signatures. - * @param psbt - The PSBT to update. - * @param signatures - An array of tuples containing the index of the input and the PartialSignature. - * @returns The updated PSBT. - */ -export declare function addTaprootInputSignaturesToPSBT(psbt: Psbt, signatures: [number, PartialSignature][]): void; diff --git a/dist/functions/bitcoin/psbt-functions.js b/dist/functions/bitcoin/psbt-functions.js deleted file mode 100644 index ce58af9..0000000 --- a/dist/functions/bitcoin/psbt-functions.js +++ /dev/null @@ -1,356 +0,0 @@ -import { hexToBytes } from '@noble/hashes/utils'; -import { selectUTXO } from '@scure/btc-signer'; -import { reverseBytes } from '../../utilities/index.js'; -import { ecdsaPublicKeyToSchnorr, getFeeAmount, getFeeRecipientAddressFromPublicKey, getUTXOs, } from '../bitcoin/bitcoin-functions.js'; -import { fetchBitcoinTransaction } from './bitcoin-request-functions.js'; -/** - * Creates the initial Funding Transaction to fund the Multisig Transaction. - * - * @param bitcoinAmount - The amount of Bitcoin to fund the Transaction with. - * @param bitcoinNetwork - The Bitcoin Network to use. - * @param multisigAddress - The Multisig Address. - * @param bitcoinNativeSegwitTransaction - The user's Native Segwit Payment Transaction. - * @param feeRate - The Fee Rate to use for the Transaction. - * @param feePublicKey - The Fee Recipient's Public Key. - * @param feeBasisPoints - The Fee Basis Points. - * @returns The Funding Transaction. - */ -export async function createFundingTransaction(bitcoinAmount, bitcoinNetwork, multisigAddress, bitcoinNativeSegwitTransaction, feeRate, feePublicKey, feeBasisPoints, bitcoinBlockchainAPIURL) { - const feeAddress = getFeeRecipientAddressFromPublicKey(feePublicKey, bitcoinNetwork); - const feeAmount = getFeeAmount(Number(bitcoinAmount), Number(feeBasisPoints)); - const userUTXOs = await getUTXOs(bitcoinNativeSegwitTransaction, bitcoinBlockchainAPIURL); - const psbtOutputs = [ - { address: multisigAddress, amount: bitcoinAmount }, - { - address: feeAddress, - amount: BigInt(feeAmount), - }, - ]; - const selected = selectUTXO(userUTXOs, psbtOutputs, 'default', { - changeAddress: bitcoinNativeSegwitTransaction.address, - feePerByte: feeRate, - bip69: false, - createTx: true, - network: bitcoinNetwork, - }); - if (!selected) { - throw new Error('Failed to select Inputs for the Funding Transaction. Ensure sufficient funds are available.'); - } - const fundingTX = selected.tx; - if (!fundingTX) - throw new Error('Could not create Funding Transaction'); - fundingTX.updateInput(0, { - sequence: 0xfffffff0, - }); - return fundingTX; -} -/** - * Creates a Deposit Transaction. - * Uses the existing Vault's Funding Transaction ID and additional UTXOs to create the Deposit Transaction. - * The specified amount of Bitcoin is sent to the Vault's Multisig Address. - * The remaining amount is sent back to the user's address. - * - * @param bitcoinBlockchainURL - The Bitcoin Blockchain URL. - * @param bitcoinNetwork - The Bitcoin Network to use. - * @param depositAmount - The Amount of Bitcoin to deposit. - * @param vaultTransactionID - The ID of the Vault Funding Transaction. - * @param multisigPayment - The Taproot Multisig Payment Transaction. - * @param depositPayment - The User's Native Segwit or Taproot Payment Transaction which will be used to fund the Deposit Transaction. - * @param feeRate - The Fee Rate to use for the Transaction. - * @param feePublicKey - The Fee Recipient's Public Key. - * @param feeBasisPoints - The Fee Basis Points. - * @returns The Deposit Transaction. - */ -export async function createDepositTransaction(bitcoinBlockchainURL, bitcoinNetwork, depositAmount, vaultTransactionID, multisigPayment, depositPayment, feeRate, feePublicKey, feeBasisPoints) { - const multisigPaymentAddress = multisigPayment.address; - if (!multisigPaymentAddress) { - throw new Error('Multisig Payment is missing Address'); - } - const depositPaymentAddress = depositPayment.address; - if (!depositPaymentAddress) { - throw new Error('Deposit Payment is missing Address'); - } - const feeAddress = getFeeRecipientAddressFromPublicKey(feePublicKey, bitcoinNetwork); - const feeAmount = getFeeAmount(Number(depositAmount), Number(feeBasisPoints)); - const vaultTransaction = await fetchBitcoinTransaction(vaultTransactionID, bitcoinBlockchainURL); - const vaultTransactionOutputIndex = vaultTransaction.vout.findIndex(output => output.scriptpubkey_address === multisigPaymentAddress); - if (vaultTransactionOutputIndex === -1) { - throw new Error('Could not find Vault Transaction Output Index'); - } - const vaultTransactionOutputValue = BigInt(vaultTransaction.vout[vaultTransactionOutputIndex].value); - const userUTXOs = await getUTXOs(depositPayment, bitcoinBlockchainURL); - const additionalDepositOutputs = [ - { - address: feeAddress, - amount: BigInt(feeAmount), - }, - { - address: multisigPaymentAddress, - amount: BigInt(depositAmount), - }, - ]; - const additionalDepositSelected = selectUTXO(userUTXOs, additionalDepositOutputs, 'default', { - changeAddress: depositPaymentAddress, - feePerByte: feeRate, - bip69: false, - createTx: false, - network: bitcoinNetwork, - }); - if (!additionalDepositSelected) { - throw new Error('Failed to select Inputs for the Additional Deposit Transaction. Ensure sufficient funds are available.'); - } - const vaultInput = { - txid: hexToBytes(vaultTransactionID), - index: vaultTransactionOutputIndex, - witnessUtxo: { - amount: BigInt(vaultTransactionOutputValue), - script: multisigPayment.script, - }, - ...multisigPayment, - }; - const depositInputPromises = additionalDepositSelected.inputs - .map(async (input) => { - const txID = input.txid; - if (!txID) { - throw new Error('Could not get Transaction ID from Input'); - } - return userUTXOs.find((utxo) => utxo.txid === txID && utxo.index === input.index); - }) - .filter(utxo => utxo !== undefined); - const depositInputs = await Promise.all(depositInputPromises); - depositInputs.push(vaultInput); - const depositOutputs = [ - { - address: feeAddress, - amount: BigInt(feeAmount), - }, - { - address: multisigPaymentAddress, - amount: BigInt(depositAmount) + BigInt(vaultTransactionOutputValue), - }, - ]; - const depositSelected = selectUTXO(depositInputs, depositOutputs, 'default', { - changeAddress: depositPaymentAddress, - feePerByte: feeRate, - bip69: false, - createTx: true, - network: bitcoinNetwork, - }); - if (!depositSelected) { - throw new Error('Failed to select Inputs for the Deposit Transaction. Ensure sufficient funds are available.'); - } - const depositTransaction = depositSelected.tx; - if (!depositTransaction) - throw new Error('Could not create Deposit Transaction'); - depositTransaction.updateInput(0, { - sequence: 0xfffffff0, - }); - return depositTransaction; -} -/** - * Creates a Withdrawal Transaction. - * Uses the Funding Transaction's ID to create the Withdrawal Transaction. - * The specified amount of Bitcoin is sent to the User's Native Segwit Address. - * The remaining amount is sent back to the Multisig Transaction. - * - * @param bitcoinBlockchainURL - The Bitcoin Blockchain URL. - * @param bitcoinAmount - The Amount of Bitcoin to withdraw. - * @param bitcoinNetwork - The Bitcoin Network to use. - * @param fundingTransactionID - The ID of the Funding Transaction. - * @param multisigTransaction - The Multisig Transaction. - * @param userNativeSegwitAddress - The User's Native Segwit Address. - * @param feeRate - The Fee Rate to use for the Transaction. - * @param feePublicKey - The Fee Recipient's Public Key. - * @param feeBasisPoints - The Fee Basis Points. - * @returns The Closing Transaction. - */ -export async function createWithdrawalTransaction(bitcoinBlockchainURL, bitcoinAmount, bitcoinNetwork, fundingTransactionID, multisigTransaction, userNativeSegwitAddress, feeRate, feePublicKey, feeBasisPoints) { - const multisigTransactionAddress = multisigTransaction.address; - if (!multisigTransactionAddress) { - throw new Error('Multisig Transaction is missing Address'); - } - const fundingTransaction = await fetchBitcoinTransaction(fundingTransactionID, bitcoinBlockchainURL); - const fundingTransactionOutputIndex = fundingTransaction.vout.findIndex(output => output.scriptpubkey_address === multisigTransactionAddress); - if (fundingTransactionOutputIndex === -1) { - throw new Error('Could not find Funding Transaction Output Index'); - } - const fundingTransactionOutputValue = BigInt(fundingTransaction.vout[fundingTransactionOutputIndex].value); - if (fundingTransactionOutputValue < bitcoinAmount) { - throw new Error('Insufficient Funds'); - } - const remainingAmount = BigInt(fundingTransaction.vout[fundingTransactionOutputIndex].value) - BigInt(bitcoinAmount); - const feeAddress = getFeeRecipientAddressFromPublicKey(feePublicKey, bitcoinNetwork); - const feeAmount = getFeeAmount(Number(bitcoinAmount), Number(feeBasisPoints)); - const inputs = [ - { - txid: hexToBytes(fundingTransactionID), - index: fundingTransactionOutputIndex, - witnessUtxo: { - amount: BigInt(fundingTransaction.vout[fundingTransactionOutputIndex].value), - script: multisigTransaction.script, - }, - ...multisigTransaction, - }, - ]; - const outputs = [ - { - address: feeAddress, - amount: BigInt(feeAmount), - }, - ]; - if (remainingAmount > 0) { - outputs.push({ - address: multisigTransactionAddress, - amount: remainingAmount, - }); - } - const selected = selectUTXO(inputs, outputs, 'default', { - changeAddress: userNativeSegwitAddress, - feePerByte: feeRate, - bip69: false, - createTx: true, - network: bitcoinNetwork, - }); - if (!selected) { - throw new Error('Failed to select Inputs for the Withdrawal Transaction. Ensure sufficient funds are available.'); - } - const withdrawTX = selected.tx; - if (!withdrawTX) - throw new Error('Could not create Withdrawal Transaction'); - withdrawTX.updateInput(0, { - sequence: 0xfffffff0, - }); - return withdrawTX; -} -/** - * This function updates the PSBT with the necessary information to sign the inputs - * that correspond to the given input signing configuration. - * @param inputByPaymentType - An array of tuples containing the BitcoinInputSigningConfig - * and the payment type of the input. - * @param nativeSegwitPublicKey - The public key corresponding to the native segwit inputs. - * @param masterFingerprint - The master fingerprint of the wallet. - * @param psbt - The PSBT to update. - * @returns The updated PSBT. - * @throws An error if there is an issue adding the UTXO Ledger props or the BIP32 derivation. - */ -export async function updateNativeSegwitInputs(inputsToUpdate = [], nativeSegwitPublicKey, masterFingerprint, psbt, bitcoinBlockchainAPIURL) { - try { - await addNativeSegwitUTXOLedgerProps(psbt, inputsToUpdate, bitcoinBlockchainAPIURL); - } - catch (e) { - // Intentionally Ignored - } - await addNativeSegwitBip32Derivation(psbt, masterFingerprint, nativeSegwitPublicKey, inputsToUpdate); - return psbt; -} -/** - * This function returns the Native Segwit Inputs to sign from the given input signing configuration. - * @param inputByPaymentType - An array of tuples containing the BitcoinInputSigningConfig - * and the payment type of the input. - * @returns An array of BitcoinInputSigningConfig objects. - */ -export function getNativeSegwitInputsToSign(inputByPaymentType) { - return (inputByPaymentType - // eslint-disable-next-line @typescript-eslint/no-unused-vars - .filter(([_, paymentType]) => paymentType === 'p2wpkh') - .map(([index]) => index)); -} -/** - * This function updates the PSBT with the necessary information to sign the inputs - * that correspond to the given input signing configuration. - * @param inputsToUpdate - An array of BitcoinInputSigningConfig objects. - * @param taprootPublicKey - The public key corresponding to the taproot inputs. - * @param masterFingerprint - The master fingerprint of the wallet. - * @param psbt - The PSBT to update. - * @returns The updated PSBT. - */ -export async function updateTaprootInputs(inputsToUpdate = [], taprootPublicKey, masterFingerprint, psbt) { - inputsToUpdate.forEach(({ index, derivationPath }) => { - psbt.updateInput(index, { - tapBip32Derivation: [ - { - masterFingerprint: Buffer.from(masterFingerprint, 'hex'), - pubkey: ecdsaPublicKeyToSchnorr(taprootPublicKey), - path: derivationPath, - leafHashes: [], - }, - ], - }); - }); - return psbt; -} -/** - * This function returns the Taproot Inputs to sign from the given input signing configuration. - * @param inputByPaymentType - An array of tuples containing the BitcoinInputSigningConfig - * and the payment type of the input. - * @returns An array of BitcoinInputSigningConfig objects. - */ -export function getTaprootInputsToSign(inputByPaymentType) { - return (inputByPaymentType - // eslint-disable-next-line @typescript-eslint/no-unused-vars - .filter(([_, paymentType]) => paymentType === 'p2tr') - .map(([index]) => index)); -} -/** - * This function updates the PSBT with the transaction hexes of the inputs that correspond to the given input signing configuration. - * @param inputsToUpdate - An array of BitcoinInputSigningConfig objects. - * @param psbt - The PSBT to update. - * @returns The updated PSBT. - */ -async function addNativeSegwitUTXOLedgerProps(psbt, inputSigningConfiguration, bitcoinBlockchainAPIURL) { - const inputTransactionHexes = await Promise.all(psbt.txInputs.map(async (input) => (await fetch(`${bitcoinBlockchainAPIURL}/tx/${reverseBytes(input.hash).toString('hex')}/hex`)).text())); - inputSigningConfiguration.forEach(({ index }) => { - psbt.updateInput(index, { - nonWitnessUtxo: Buffer.from(inputTransactionHexes[index], 'hex'), - }); - }); - return psbt; -} -/** - * This function updates the PSBT inputs with the BIP32 derivation information. - * @param psbt - The PSBT to update. - * @param masterFingerPrint - The Master Fingerprint of the Wallet. - * @param nativeSegwitPublicKey - The Public Key corresponding to the Native Segwit Inputs. - * @param inputSigningConfiguration - An array of BitcoinInputSigningConfig objects. - * @returns The updated PSBT. - */ -async function addNativeSegwitBip32Derivation(psbt, masterFingerPrint, nativeSegwitPublicKey, inputSigningConfiguration) { - inputSigningConfiguration.forEach(({ index, derivationPath }) => { - psbt.updateInput(index, { - bip32Derivation: [ - { - masterFingerprint: Buffer.from(masterFingerPrint, 'hex'), - pubkey: nativeSegwitPublicKey, - path: derivationPath, - }, - ], - }); - }); - return psbt; -} -/** - * This function updates the PSBT with the received Native Segwit Signatures. - * @param psbt - The PSBT to update. - * @param signatures - An array of tuples containing the index of the input and the PartialSignature. - * @returns The updated PSBT. - */ -export function addNativeSegwitSignaturesToPSBT(psbt, signatures) { - signatures.forEach(([index, signature]) => psbt.updateInput(index, { partialSig: [signature] })); -} -/** - * This function updates the PSBT with the received Taproot Signatures. - * @param psbt - The PSBT to update. - * @param signatures - An array of tuples containing the index of the input and the PartialSignature. - * @returns The updated PSBT. - */ -export function addTaprootInputSignaturesToPSBT(psbt, signatures) { - signatures.forEach(([index, signature]) => psbt.updateInput(index, { - tapScriptSig: [ - { - signature: signature.signature, - pubkey: signature.pubkey, - leafHash: signature.tapleafHash, - }, - ], - })); -} diff --git a/dist/functions/ethereum/ethereum-functions.d.ts b/dist/functions/ethereum/ethereum-functions.d.ts deleted file mode 100644 index dd55253..0000000 --- a/dist/functions/ethereum/ethereum-functions.d.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { Contract, Wallet, providers } from 'ethers'; -import { DLCEthereumContracts, EthereumDeploymentPlan, EthereumNetwork, RawVault, SupportedNetwork } from '../../models/ethereum-models.js'; -export declare function fetchEthereumDeploymentPlan(contractName: string, ethereumNetwork: EthereumNetwork, deploymentBranch: string, deploymentPlanRootURL: string): Promise; -export declare function fetchEthereumDeploymentPlansByNetwork(network: SupportedNetwork): Promise; -export declare function getProvider(rpcEndpoint: string): providers.JsonRpcProvider | providers.WebSocketProvider; -export declare function getEthereumontract(ethereumDeploymentPlans: EthereumDeploymentPlan[], contractName: string, signerOrProvider: Wallet | providers.JsonRpcSigner | providers.JsonRpcProvider): Contract; -export declare function getEthereumContracts(ethereumDeploymentPlans: EthereumDeploymentPlan[], signer: Wallet | providers.JsonRpcSigner): DLCEthereumContracts; -export declare function getReadOnlyEthereumContracts(ethereumDeploymentPlans: EthereumDeploymentPlan[], readOnlyProvider: providers.JsonRpcProvider): { - protocolContract: Contract; - dlcManagerContract: Contract; - dlcBTCContract: Contract; -}; -export declare function getLockedBTCBalance(userVaults: RawVault[]): Promise; diff --git a/dist/functions/ethereum/ethereum-functions.js b/dist/functions/ethereum/ethereum-functions.js deleted file mode 100644 index f4660db..0000000 --- a/dist/functions/ethereum/ethereum-functions.js +++ /dev/null @@ -1,90 +0,0 @@ -import { Contract, providers } from 'ethers'; -import { GITHUB_SOLIDITY_URL, dlcContractNames, ethereumArbitrum, ethereumArbitrumSepolia, } from '../../constants/ethereum-constants.js'; -import { EthereumError } from '../../models/errors.js'; -import { VaultState, } from '../../models/ethereum-models.js'; -export async function fetchEthereumDeploymentPlan(contractName, ethereumNetwork, deploymentBranch, deploymentPlanRootURL) { - const deploymentPlanURL = `${deploymentPlanRootURL}/${deploymentBranch}/deploymentFiles/${ethereumNetwork.name.toLowerCase()}/${contractName}.json`; - try { - const response = await fetch(deploymentPlanURL); - if (!response.ok) { - throw new Error(`Failed to fetch deployment. Received a non-OK response. Status: ${response.status} ${response.statusText}`); - } - const contractData = await response.json(); - return contractData; - } - catch (error) { - throw new EthereumError(`Could not fetch deployment info for ${contractName}`); - } -} -export async function fetchEthereumDeploymentPlansByNetwork(network) { - try { - let ethereumNetwork; - let deploymentBranch; - switch (network) { - case 'arbitrum': - ethereumNetwork = ethereumArbitrum; - deploymentBranch = 'dev'; - break; - case 'arbitrum-sepolia-testnet': - ethereumNetwork = ethereumArbitrumSepolia; - deploymentBranch = 'testnet-rolling'; - break; - case 'arbitrum-sepolia-devnet': - ethereumNetwork = ethereumArbitrumSepolia; - deploymentBranch = 'dev'; - break; - default: - throw new Error('Unsupported Network'); - } - return Promise.all(dlcContractNames.map(async (contractName) => { - return await fetchEthereumDeploymentPlan(contractName, ethereumNetwork, deploymentBranch, GITHUB_SOLIDITY_URL); - })); - } - catch (error) { - throw new EthereumError(`Could not fetch Ethereum Deployment Plans: ${error}`); - } -} -export function getProvider(rpcEndpoint) { - if (rpcEndpoint.startsWith('http') || rpcEndpoint.startsWith('https')) { - return new providers.JsonRpcProvider(rpcEndpoint); - } - else if (rpcEndpoint.startsWith('ws') || rpcEndpoint.startsWith('wss')) { - return new providers.WebSocketProvider(rpcEndpoint); - } - else { - throw new Error('Invalid RPC endpoint. It should start with either http, https, ws, or wss.'); - } -} -export function getEthereumontract(ethereumDeploymentPlans, contractName, signerOrProvider) { - try { - const contractDeploymentPlan = ethereumDeploymentPlans.find(plan => plan.contract.name === contractName); - if (!contractDeploymentPlan) { - throw new Error(`${contractName} Contract not found in Deployment Plans`); - } - return new Contract(contractDeploymentPlan.contract.address, contractDeploymentPlan.contract.abi, signerOrProvider); - } - catch (error) { - throw new EthereumError(`Could not find ${contractName} Contract in Deployment Plans: ${error}`); - } -} -export function getEthereumContracts(ethereumDeploymentPlans, signer) { - const dlcManagerContract = getEthereumontract(ethereumDeploymentPlans, 'DLCManager', signer); - const dlcBTCContract = getEthereumontract(ethereumDeploymentPlans, 'DLCBTC', signer); - return { dlcManagerContract, dlcBTCContract }; -} -export function getReadOnlyEthereumContracts(ethereumDeploymentPlans, readOnlyProvider) { - const protocolContract = getEthereumontract(ethereumDeploymentPlans, 'TokenManager', readOnlyProvider); - const dlcManagerContract = getEthereumontract(ethereumDeploymentPlans, 'DLCManager', readOnlyProvider); - const dlcBTCContract = getEthereumontract(ethereumDeploymentPlans, 'DLCBTC', readOnlyProvider); - return { protocolContract, dlcManagerContract, dlcBTCContract }; -} -export async function getLockedBTCBalance(userVaults) { - try { - const fundedVaults = userVaults.filter(vault => vault.status === VaultState.FUNDED); - const totalCollateral = fundedVaults.reduce((sum, vault) => sum + vault.valueLocked.toNumber(), 0); - return totalCollateral; - } - catch (error) { - throw new EthereumError(`Could not fetch locked BTC balance: ${error}`); - } -} diff --git a/dist/functions/ethereum/index.d.ts b/dist/functions/ethereum/index.d.ts deleted file mode 100644 index af8032f..0000000 --- a/dist/functions/ethereum/index.d.ts +++ /dev/null @@ -1,2 +0,0 @@ -import { fetchEthereumDeploymentPlan, fetchEthereumDeploymentPlansByNetwork } from '../ethereum/ethereum-functions.js'; -export { fetchEthereumDeploymentPlan, fetchEthereumDeploymentPlansByNetwork }; diff --git a/dist/functions/ethereum/index.js b/dist/functions/ethereum/index.js deleted file mode 100644 index ea5e45a..0000000 --- a/dist/functions/ethereum/index.js +++ /dev/null @@ -1,2 +0,0 @@ -import { fetchEthereumDeploymentPlan, fetchEthereumDeploymentPlansByNetwork, } from '../ethereum/ethereum-functions.js'; -export { fetchEthereumDeploymentPlan, fetchEthereumDeploymentPlansByNetwork }; diff --git a/dist/index.d.ts b/dist/index.d.ts deleted file mode 100644 index d7a6231..0000000 --- a/dist/index.d.ts +++ /dev/null @@ -1,8 +0,0 @@ -import { AttestorHandler } from './attestor-handlers/attestor-handler.js'; -import { LedgerDLCHandler } from './dlc-handlers/ledger-dlc-handler.js'; -import { PrivateKeyDLCHandler } from './dlc-handlers/private-key-dlc-handler.js'; -import { SoftwareWalletDLCHandler } from './dlc-handlers/software-wallet-dlc-handler.js'; -import { EthereumHandler } from './network-handlers/ethereum-handler.js'; -import { ReadOnlyEthereumHandler } from './network-handlers/read-only-ethereum-handler.js'; -import { ProofOfReserveHandler } from './proof-of-reserve-handlers/proof-of-reserve-handler.js'; -export { PrivateKeyDLCHandler, LedgerDLCHandler, SoftwareWalletDLCHandler, EthereumHandler, ReadOnlyEthereumHandler, AttestorHandler, ProofOfReserveHandler, }; diff --git a/dist/index.js b/dist/index.js deleted file mode 100644 index d7a6231..0000000 --- a/dist/index.js +++ /dev/null @@ -1,8 +0,0 @@ -import { AttestorHandler } from './attestor-handlers/attestor-handler.js'; -import { LedgerDLCHandler } from './dlc-handlers/ledger-dlc-handler.js'; -import { PrivateKeyDLCHandler } from './dlc-handlers/private-key-dlc-handler.js'; -import { SoftwareWalletDLCHandler } from './dlc-handlers/software-wallet-dlc-handler.js'; -import { EthereumHandler } from './network-handlers/ethereum-handler.js'; -import { ReadOnlyEthereumHandler } from './network-handlers/read-only-ethereum-handler.js'; -import { ProofOfReserveHandler } from './proof-of-reserve-handlers/proof-of-reserve-handler.js'; -export { PrivateKeyDLCHandler, LedgerDLCHandler, SoftwareWalletDLCHandler, EthereumHandler, ReadOnlyEthereumHandler, AttestorHandler, ProofOfReserveHandler, }; diff --git a/dist/models/bitcoin-models.d.ts b/dist/models/bitcoin-models.d.ts deleted file mode 100644 index 9ea3005..0000000 --- a/dist/models/bitcoin-models.d.ts +++ /dev/null @@ -1,90 +0,0 @@ -/// -import { P2Ret, P2TROut } from '@scure/btc-signer/payment'; -export interface UTXO { - txid: string; - vout: number; - status: BitcoinTransactionStatus; - value: number; -} -export interface BitcoinInputSigningConfig { - derivationPath: string; - index: number; -} -export interface FeeRates { - fastestFee: number; - halfHourFee: number; - hourFee: number; - economyFee: number; - minimumFee: number; -} -export interface PaymentInformation { - nativeSegwitPayment: P2Ret; - taprootMultisigPayment: P2TROut; -} -export interface ExtendedPaymentInformation extends PaymentInformation { - nativeSegwitDerivedPublicKey: Buffer; - taprootDerivedPublicKey: Buffer; -} -interface BitcoinTransactionIssuance { - asset_id: string; - is_reissuance: boolean; - asset_blinding_nonce: number; - asset_entropy: number; - contract_hash: string; - assetamount?: number; - assetamountcommitment?: number; - tokenamount?: number; - tokenamountcommitment?: number; -} -interface BitcoinTransactionPegOut { - genesis_hash: string; - scriptpubkey: string; - scriptpubkey_asm: string; - scriptpubkey_address: string; -} -interface BitcoinTransactionStatus { - confirmed: boolean; - block_height?: number | null; - block_hash?: string | null; - block_time?: number | null; -} -export interface BitcoinTransactionVectorOutput { - scriptpubkey: string; - scriptpubkey_asm: string; - scriptpubkey_type: string; - scriptpubkey_address: string; - value: number; - valuecommitment?: number; - asset?: string; - assetcommitment?: number; - pegout?: BitcoinTransactionPegOut | null; -} -interface BitcoinTransactionVectorInput { - inner_redeemscript_asm?: string; - inner_witnessscript_asm?: string; - is_coinbase: boolean; - is_pegin?: boolean; - issuance?: BitcoinTransactionIssuance | null; - prevout: BitcoinTransactionVectorOutput; - scriptsig: string; - scriptsig_asm: string; - sequence: number; - txid: string; - vout: number; - witness: string[]; -} -export interface BitcoinTransaction { - fee: number; - locktime: number; - size: number; - status: BitcoinTransactionStatus; - tx_type?: string; - txid: string; - version: number; - vin: BitcoinTransactionVectorInput[]; - vout: BitcoinTransactionVectorOutput[]; - weight: number; -} -export type PaymentTypes = 'p2pkh' | 'p2sh' | 'p2wpkh-p2sh' | 'p2wpkh' | 'p2tr'; -export type BitcoinNetworkName = 'Mainnet' | 'Testnet' | 'Regtest'; -export {}; diff --git a/dist/models/bitcoin-models.js b/dist/models/bitcoin-models.js deleted file mode 100644 index cb0ff5c..0000000 --- a/dist/models/bitcoin-models.js +++ /dev/null @@ -1 +0,0 @@ -export {}; diff --git a/dist/models/errors.d.ts b/dist/models/errors.d.ts deleted file mode 100644 index b53ee00..0000000 --- a/dist/models/errors.d.ts +++ /dev/null @@ -1,12 +0,0 @@ -export declare class EthereumError extends Error { - constructor(message: string); -} -export declare class BitcoinError extends Error { - constructor(message: string); -} -export declare class AttestorError extends Error { - constructor(message: string); -} -export declare class LedgerError extends Error { - constructor(message: string); -} diff --git a/dist/models/errors.js b/dist/models/errors.js deleted file mode 100644 index 2f351b9..0000000 --- a/dist/models/errors.js +++ /dev/null @@ -1,24 +0,0 @@ -export class EthereumError extends Error { - constructor(message) { - super(message); - this.name = 'EthereumError'; - } -} -export class BitcoinError extends Error { - constructor(message) { - super(message); - this.name = 'BitcoinError'; - } -} -export class AttestorError extends Error { - constructor(message) { - super(message); - this.name = 'AttestorError'; - } -} -export class LedgerError extends Error { - constructor(message) { - super(message); - this.name = 'LedgerError'; - } -} diff --git a/dist/models/ethereum-models.d.ts b/dist/models/ethereum-models.d.ts deleted file mode 100644 index 00d3210..0000000 --- a/dist/models/ethereum-models.d.ts +++ /dev/null @@ -1,55 +0,0 @@ -import { BigNumber, Contract } from 'ethers'; -export interface EthereumNetwork { - id: EthereumNetworkID; - name: string; - displayName: string; - defaultNodeURL: string; -} -export declare enum EthereumNetworkID { - ArbitrumSepolia = "421614", - Arbitrum = "42161" -} -export declare enum VaultState { - READY = 0, - FUNDED = 1, - CLOSING = 2, - CLOSED = 3, - PENDING = 4, - FUNDING = 5 -} -export interface RawVault { - uuid: string; - protocolContract: string; - timestamp: BigNumber; - valueLocked: BigNumber; - valueMinted: BigNumber; - creator: string; - status: number; - fundingTxId: string; - closingTxId: string; - wdTxId: string; - btcFeeRecipient: string; - btcMintFeeBasisPoints: BigNumber; - btcRedeemFeeBasisPoints: BigNumber; - taprootPubKey: string; -} -interface EthereumContract { - name: string; - address: string; - signerAddress: string; - abi: string[]; -} -export interface EthereumDeploymentPlan { - network: string; - updatedAt: string; - gitSHA: string; - contract: EthereumContract; -} -export interface DLCEthereumContracts { - dlcManagerContract: Contract; - dlcBTCContract: Contract; -} -export type SupportedNetwork = 'arbitrum' | 'arbitrum-sepolia-testnet' | 'arbitrum-sepolia-devnet'; -export type DLCEthereumContractName = 'DLCManager' | 'DLCBTC'; -export type DLCSolidityBranchName = 'dev' | 'testnet-rolling'; -export {}; diff --git a/dist/models/ethereum-models.js b/dist/models/ethereum-models.js deleted file mode 100644 index c32e816..0000000 --- a/dist/models/ethereum-models.js +++ /dev/null @@ -1,14 +0,0 @@ -export var EthereumNetworkID; -(function (EthereumNetworkID) { - EthereumNetworkID["ArbitrumSepolia"] = "421614"; - EthereumNetworkID["Arbitrum"] = "42161"; -})(EthereumNetworkID || (EthereumNetworkID = {})); -export var VaultState; -(function (VaultState) { - VaultState[VaultState["READY"] = 0] = "READY"; - VaultState[VaultState["FUNDED"] = 1] = "FUNDED"; - VaultState[VaultState["CLOSING"] = 2] = "CLOSING"; - VaultState[VaultState["CLOSED"] = 3] = "CLOSED"; - VaultState[VaultState["PENDING"] = 4] = "PENDING"; - VaultState[VaultState["FUNDING"] = 5] = "FUNDING"; -})(VaultState || (VaultState = {})); diff --git a/dist/models/index.d.ts b/dist/models/index.d.ts deleted file mode 100644 index d928923..0000000 --- a/dist/models/index.d.ts +++ /dev/null @@ -1,5 +0,0 @@ -export { Network } from 'bitcoinjs-lib/src/networks.js'; -export { Transaction } from '@scure/btc-signer'; -export * from '../models/bitcoin-models.js'; -export * from '../models/errors.js'; -export * from '../models/ethereum-models.js'; diff --git a/dist/models/index.js b/dist/models/index.js deleted file mode 100644 index 0cfad8d..0000000 --- a/dist/models/index.js +++ /dev/null @@ -1,4 +0,0 @@ -export { Transaction } from '@scure/btc-signer'; -export * from '../models/bitcoin-models.js'; -export * from '../models/errors.js'; -export * from '../models/ethereum-models.js'; diff --git a/dist/network-handlers/ethereum-handler.d.ts b/dist/network-handlers/ethereum-handler.d.ts deleted file mode 100644 index d845dcc..0000000 --- a/dist/network-handlers/ethereum-handler.d.ts +++ /dev/null @@ -1,19 +0,0 @@ -import { Event, providers } from 'ethers'; -import { DLCEthereumContractName, DLCEthereumContracts, EthereumDeploymentPlan, RawVault } from '../models/ethereum-models.js'; -export declare class EthereumHandler { - private ethereumContracts; - private constructor(); - static fromPrivateKey(ethereumDeploymentPlans: EthereumDeploymentPlan[], ethereumPrivateKey: string, rpcEndpoint: string): EthereumHandler; - static fromSigner(ethereumDeploymentPlans: EthereumDeploymentPlan[], signer: providers.JsonRpcSigner): EthereumHandler; - getContracts(): DLCEthereumContracts; - getAllVaults(): Promise; - getRawVault(vaultUUID: string): Promise; - setupVault(): Promise; - withdraw(vaultUUID: string, amount: bigint): Promise; - closeVault(vaultUUID: string): Promise; - getDLCBTCBalance(): Promise; - getAttestorGroupPublicKey(): Promise; - getContractTransferEvents(contractName: DLCEthereumContractName): Promise; - getContractTotalSupply(): Promise; - getContractFundedVaults(amount?: number): Promise; -} diff --git a/dist/network-handlers/ethereum-handler.js b/dist/network-handlers/ethereum-handler.js deleted file mode 100644 index 86e9852..0000000 --- a/dist/network-handlers/ethereum-handler.js +++ /dev/null @@ -1,130 +0,0 @@ -import { Wallet } from 'ethers'; -import { getEthereumContracts, getProvider } from '../functions/ethereum/ethereum-functions.js'; -import { EthereumError } from '../models/errors.js'; -import { VaultState, } from '../models/ethereum-models.js'; -export class EthereumHandler { - ethereumContracts; - constructor(ethereumContracts) { - this.ethereumContracts = ethereumContracts; - } - static fromPrivateKey(ethereumDeploymentPlans, ethereumPrivateKey, rpcEndpoint) { - const provider = getProvider(rpcEndpoint); - const signer = new Wallet(ethereumPrivateKey, provider); - const ethereumContracts = getEthereumContracts(ethereumDeploymentPlans, signer); - return new EthereumHandler(ethereumContracts); - } - static fromSigner(ethereumDeploymentPlans, signer) { - const ethereumContracts = getEthereumContracts(ethereumDeploymentPlans, signer); - return new EthereumHandler(ethereumContracts); - } - getContracts() { - return this.ethereumContracts; - } - async getAllVaults() { - try { - const userAddress = await this.ethereumContracts.dlcManagerContract.signer.getAddress(); - return await this.ethereumContracts.dlcManagerContract.getAllVaultsForAddress(userAddress); - } - catch (error) { - throw new EthereumError(`Could not fetch Vaults: ${error}`); - } - } - async getRawVault(vaultUUID) { - const vault = await this.ethereumContracts.dlcManagerContract.getVault(vaultUUID); - if (!vault) - throw new Error('Vault not found'); - return vault; - } - async setupVault() { - try { - await this.ethereumContracts.dlcManagerContract.callStatic.setupVault(); - const transaction = await this.ethereumContracts.dlcManagerContract.setupVault(); - return await transaction.wait(); - } - catch (error) { - throw new EthereumError(`Could not setup Vault: ${error}`); - } - } - async withdraw(vaultUUID, amount) { - try { - await this.ethereumContracts.dlcManagerContract.callStatic.withdraw(vaultUUID, amount); - const transaction = await this.ethereumContracts.dlcManagerContract.withdraw(vaultUUID, amount); - return await transaction.wait(); - } - catch (error) { - throw new EthereumError(`Unable to perform withdraw: ${error}`); - } - } - async closeVault(vaultUUID) { - try { - await this.ethereumContracts.dlcManagerContract.callStatic.closeVault(vaultUUID); - const transaction = await this.ethereumContracts.dlcManagerContract.closeVault(vaultUUID); - return await transaction.wait(); - } - catch (error) { - throw new EthereumError(`Could not close Vault: ${error}`); - } - } - async getDLCBTCBalance() { - try { - const userAddress = await this.ethereumContracts.dlcManagerContract.signer.getAddress(); - const balance = await this.ethereumContracts.dlcBTCContract.balanceOf(userAddress); - return balance.toNumber(); - } - catch (error) { - throw new EthereumError(`Could not fetch dlcBTC balance: ${error}`); - } - } - async getAttestorGroupPublicKey() { - try { - const attestorGroupPubKey = await this.ethereumContracts.dlcManagerContract.attestorGroupPubKey(); - if (!attestorGroupPubKey) - throw new Error('Could not get Attestor Group Public Key'); - return attestorGroupPubKey; - } - catch (error) { - throw new EthereumError(`Could not fetch Attestor Public Key: ${error}`); - } - } - async getContractTransferEvents(contractName) { - try { - switch (contractName) { - case 'DLCBTC': - return await this.ethereumContracts.dlcBTCContract.queryFilter(this.ethereumContracts.dlcBTCContract.filters.Transfer()); - case 'DLCManager': - return await this.ethereumContracts.dlcManagerContract.queryFilter(this.ethereumContracts.dlcManagerContract.filters.Transfer()); - default: - throw new Error('Invalid Contract Name'); - } - } - catch (error) { - throw new EthereumError(`Could not fetch Transfer Events: ${error}`); - } - } - async getContractTotalSupply() { - try { - return await this.ethereumContracts.dlcBTCContract.totalSupply().toNumber(); - } - catch (error) { - throw new EthereumError(`Could not fetch Total Supply: ${error}`); - } - } - async getContractFundedVaults(amount = 50) { - try { - let totalFetched = 0; - const fundedVaults = []; - let shouldContinue = true; - while (shouldContinue) { - const fetchedVaults = await this.ethereumContracts.dlcManagerContract.getAllDLCs(totalFetched, totalFetched + amount); - const filteredVaults = fetchedVaults.filter(vault => vault.status === VaultState.FUNDED); - fundedVaults.push(...filteredVaults); - totalFetched += amount; - shouldContinue = fetchedVaults.length === amount; - } - return fundedVaults; - } - catch (error) { - throw new EthereumError(`Could not fetch Funded Vaults: ${error instanceof Error ? error.message : error}`); - } - } -} diff --git a/dist/network-handlers/read-only-ethereum-handler.d.ts b/dist/network-handlers/read-only-ethereum-handler.d.ts deleted file mode 100644 index 5ad64af..0000000 --- a/dist/network-handlers/read-only-ethereum-handler.d.ts +++ /dev/null @@ -1,12 +0,0 @@ -import { Event } from 'ethers'; -import { DLCEthereumContractName, DLCEthereumContracts, EthereumDeploymentPlan, RawVault } from '../models/ethereum-models.js'; -export declare class ReadOnlyEthereumHandler { - private ethereumContracts; - constructor(ethereumDeploymentPlans: EthereumDeploymentPlan[], rpcEndpoint: string); - getContracts(): DLCEthereumContracts; - getRawVault(vaultUUID: string): Promise; - getAttestorGroupPublicKey(): Promise; - getContractTransferEvents(contractName: DLCEthereumContractName): Promise; - getContractTotalSupply(): Promise; - getContractFundedVaults(amount?: number): Promise; -} diff --git a/dist/network-handlers/read-only-ethereum-handler.js b/dist/network-handlers/read-only-ethereum-handler.js deleted file mode 100644 index dedb8d5..0000000 --- a/dist/network-handlers/read-only-ethereum-handler.js +++ /dev/null @@ -1,71 +0,0 @@ -import { getProvider, getReadOnlyEthereumContracts, } from '../functions/ethereum/ethereum-functions.js'; -import { EthereumError } from '../models/errors.js'; -import { VaultState, } from '../models/ethereum-models.js'; -export class ReadOnlyEthereumHandler { - ethereumContracts; - constructor(ethereumDeploymentPlans, rpcEndpoint) { - this.ethereumContracts = getReadOnlyEthereumContracts(ethereumDeploymentPlans, getProvider(rpcEndpoint)); - } - getContracts() { - return this.ethereumContracts; - } - async getRawVault(vaultUUID) { - const vault = await this.ethereumContracts.dlcManagerContract.getVault(vaultUUID); - if (!vault) - throw new Error('Vault not found'); - return vault; - } - async getAttestorGroupPublicKey() { - try { - const attestorGroupPubKey = await this.ethereumContracts.dlcManagerContract.attestorGroupPubKey(); - if (!attestorGroupPubKey) - throw new Error('Could not get Attestor Group Public Key'); - return attestorGroupPubKey; - } - catch (error) { - throw new EthereumError(`Could not fetch Attestor Public Key: ${error}`); - } - } - async getContractTransferEvents(contractName) { - try { - switch (contractName) { - case 'DLCBTC': - return await this.ethereumContracts.dlcBTCContract.queryFilter(this.ethereumContracts.dlcBTCContract.filters.Transfer()); - case 'DLCManager': - return await this.ethereumContracts.dlcManagerContract.queryFilter(this.ethereumContracts.dlcManagerContract.filters.Transfer()); - default: - throw new Error('Invalid Contract Name'); - } - } - catch (error) { - throw new EthereumError(`Could not fetch Transfer Events: ${error}`); - } - } - async getContractTotalSupply() { - try { - const totalSupply = await this.ethereumContracts.dlcBTCContract.totalSupply(); - return totalSupply.toNumber(); - } - catch (error) { - throw new EthereumError(`Could not fetch Total Supply: ${error}`); - } - } - async getContractFundedVaults(amount = 50) { - try { - let totalFetched = 0; - const fundedVaults = []; - let shouldContinue = true; - while (shouldContinue) { - const fetchedVaults = await this.ethereumContracts.dlcManagerContract.getAllDLCs(totalFetched, totalFetched + amount); - const filteredVaults = fetchedVaults.filter(vault => vault.status === VaultState.FUNDED); - fundedVaults.push(...filteredVaults); - totalFetched += amount; - shouldContinue = fetchedVaults.length === amount; - } - return fundedVaults; - } - catch (error) { - throw new EthereumError(`Could not fetch Funded Vaults: ${error instanceof Error ? error.message : error}`); - } - } -} diff --git a/dist/proof-of-reserve-handlers/proof-of-reserve-handler.d.ts b/dist/proof-of-reserve-handlers/proof-of-reserve-handler.d.ts deleted file mode 100644 index 9bf3e01..0000000 --- a/dist/proof-of-reserve-handlers/proof-of-reserve-handler.d.ts +++ /dev/null @@ -1,11 +0,0 @@ -/// -import { Network } from 'bitcoinjs-lib'; -import { RawVault } from '../models/ethereum-models.js'; -export declare class ProofOfReserveHandler { - private bitcoinBlockchainAPI; - private bitcoinNetwork; - private attestorGroupPublicKey; - constructor(bitcoinBlockchainAPI: string, bitcoinNetwork: Network, attestorGroupPublicKey: string); - verifyVaultDeposit(vault: RawVault, attestorGroupPublicKey: Buffer, bitcoinBlockchainBlockHeight: number): Promise; - calculateProofOfReserve(vaults: RawVault[]): Promise; -} diff --git a/dist/proof-of-reserve-handlers/proof-of-reserve-handler.js b/dist/proof-of-reserve-handlers/proof-of-reserve-handler.js deleted file mode 100644 index 88bba75..0000000 --- a/dist/proof-of-reserve-handlers/proof-of-reserve-handler.js +++ /dev/null @@ -1,40 +0,0 @@ -import { hex } from '@scure/base'; -import { createTaprootMultisigPayment, deriveUnhardenedPublicKey, getUnspendableKeyCommittedToUUID, getValueMatchingInputFromTransaction, validateScript, } from '../functions/bitcoin/bitcoin-functions.js'; -import { checkBitcoinTransactionConfirmations, fetchBitcoinBlockchainBlockHeight, fetchBitcoinTransaction, } from '../functions/bitcoin/bitcoin-request-functions.js'; -export class ProofOfReserveHandler { - bitcoinBlockchainAPI; - bitcoinNetwork; - attestorGroupPublicKey; - constructor(bitcoinBlockchainAPI, bitcoinNetwork, attestorGroupPublicKey) { - this.bitcoinBlockchainAPI = bitcoinBlockchainAPI; - this.bitcoinNetwork = bitcoinNetwork; - this.attestorGroupPublicKey = attestorGroupPublicKey; - } - async verifyVaultDeposit(vault, attestorGroupPublicKey, bitcoinBlockchainBlockHeight) { - try { - const fundingTransaction = await fetchBitcoinTransaction(vault.fundingTxId, this.bitcoinBlockchainAPI); - const isFundingTransactionConfirmed = await checkBitcoinTransactionConfirmations(fundingTransaction, bitcoinBlockchainBlockHeight); - if (!isFundingTransactionConfirmed) { - return false; - } - const closingTransactionInput = getValueMatchingInputFromTransaction(fundingTransaction, vault.valueLocked.toNumber()); - const unspendableKeyCommittedToUUID = deriveUnhardenedPublicKey(getUnspendableKeyCommittedToUUID(vault.uuid, this.bitcoinNetwork), this.bitcoinNetwork); - const taprootMultisigPayment = createTaprootMultisigPayment(unspendableKeyCommittedToUUID, attestorGroupPublicKey, Buffer.from(vault.taprootPubKey, 'hex'), this.bitcoinNetwork); - return validateScript(taprootMultisigPayment.script, hex.decode(closingTransactionInput.scriptpubkey)); - } - catch (error) { - console.error(`Error verifying Vault Deposit: ${error}`); - return false; - } - } - async calculateProofOfReserve(vaults) { - const bitcoinBlockchainBlockHeight = await fetchBitcoinBlockchainBlockHeight(this.bitcoinBlockchainAPI); - const derivedAttestorGroupPublicKey = deriveUnhardenedPublicKey(this.attestorGroupPublicKey, this.bitcoinNetwork); - const verifiedDeposits = await Promise.all(vaults.map(async (vault) => { - return (await this.verifyVaultDeposit(vault, derivedAttestorGroupPublicKey, bitcoinBlockchainBlockHeight)) === true - ? vault.valueLocked.toNumber() - : 0; - })); - return verifiedDeposits.reduce((a, b) => a + b, 0); - } -} diff --git a/dist/utilities/index.d.ts b/dist/utilities/index.d.ts deleted file mode 100644 index 3e94718..0000000 --- a/dist/utilities/index.d.ts +++ /dev/null @@ -1,11 +0,0 @@ -/// -export declare function shiftValue(value: number): number; -export declare function unshiftValue(value: number): number; -export declare function customShiftValue(value: number, shift: number, unshift: boolean): number; -export declare function truncateAddress(address: string): string; -export declare function createRangeFromLength(length: number): number[]; -export declare function isUndefined(value: unknown): value is undefined; -export declare function isDefined(argument: T | undefined): argument is T; -export declare function delay(ms: number): Promise; -export declare function reverseBytes(bytes: Buffer): Buffer; -export declare function reverseBytes(bytes: Uint8Array): Uint8Array; diff --git a/dist/utilities/index.js b/dist/utilities/index.js deleted file mode 100644 index a63c57c..0000000 --- a/dist/utilities/index.js +++ /dev/null @@ -1,44 +0,0 @@ -import { Decimal } from 'decimal.js'; -export function shiftValue(value) { - const decimalPoweredShift = new Decimal(10 ** 8); - const decimalValue = new Decimal(Number(value)); - const decimalShiftedValue = decimalValue.mul(decimalPoweredShift).toNumber(); - return decimalShiftedValue; -} -export function unshiftValue(value) { - const decimalPoweredShift = new Decimal(10 ** 8); - const decimalValue = new Decimal(Number(value)); - const decimalShiftedValue = decimalValue.div(decimalPoweredShift).toNumber(); - return decimalShiftedValue; -} -export function customShiftValue(value, shift, unshift) { - const decimalPoweredShift = new Decimal(10 ** shift); - const decimalValue = new Decimal(Number(value)); - const decimalShiftedValue = unshift - ? decimalValue.div(decimalPoweredShift).toNumber() - : decimalValue.mul(decimalPoweredShift).toNumber(); - return decimalShiftedValue; -} -export function truncateAddress(address) { - const truncationLength = 4; - const prefix = address.substring(0, truncationLength); - const suffix = address.substring(address.length - truncationLength); - return `${prefix}...${suffix}`; -} -export function createRangeFromLength(length) { - return [...Array(length).keys()]; -} -export function isUndefined(value) { - return typeof value === 'undefined'; -} -export function isDefined(argument) { - return !isUndefined(argument); -} -export async function delay(ms) { - return new Promise(resolve => setTimeout(resolve, ms)); -} -export function reverseBytes(bytes) { - if (Buffer.isBuffer(bytes)) - return Buffer.from(bytes).reverse(); - return new Uint8Array(bytes.slice().reverse()); -} diff --git a/package.json b/package.json index 74bc191..6f96bc0 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "type": "module", "name": "dlc-btc-lib", - "version": "1.0.11", + "version": "1.0.13", "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/functions/bitcoin/psbt-functions.ts b/src/functions/bitcoin/psbt-functions.ts index dde34ad..1243e95 100644 --- a/src/functions/bitcoin/psbt-functions.ts +++ b/src/functions/bitcoin/psbt-functions.ts @@ -240,7 +240,7 @@ export async function createWithdrawalTransaction( bitcoinNetwork: Network, fundingTransactionID: string, multisigTransaction: P2TROut, - userNativeSegwitAddress: string, + withdrawAddress: string, feeRate: bigint, feePublicKey: string, feeBasisPoints: bigint @@ -304,7 +304,7 @@ export async function createWithdrawalTransaction( } const selected = selectUTXO(inputs, outputs, 'default', { - changeAddress: userNativeSegwitAddress, + changeAddress: withdrawAddress, feePerByte: feeRate, bip69: false, createTx: true,