Skip to content

Commit

Permalink
feat: modify package.json
Browse files Browse the repository at this point in the history
  • Loading branch information
Polybius93 committed May 29, 2024
1 parent 9edd3df commit 18c9233
Show file tree
Hide file tree
Showing 20 changed files with 408 additions and 165 deletions.
19 changes: 19 additions & 0 deletions .prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export default {
printWidth: 100,
tabWidth: 2,
useTabs: false,
semi: true,
trailingComma: "es5",
singleQuote: true,
arrowParens: "avoid",
plugins: ["@trivago/prettier-plugin-sort-imports"],
importOrder: [
"^react",
"<THIRD_PARTY_MODULES>",
"^@shared/(.*)$",
"^@(app|content-script|inpage|background)/(.*)$",
"^[./]",
],
importOrderSeparation: true,
importOrderSortSpecifiers: true,
};
19 changes: 0 additions & 19 deletions .prettierrc.json

This file was deleted.

9 changes: 4 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"description": "",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"files": ["dist"],
"exports": {
".": "./dist/index.js",
"./utilities": "./dist/utilities/index.js",
Expand All @@ -16,12 +17,11 @@
"build": "tsc",
"start": "node dist/test.js",
"test": "ts-node index.ts",
"lint": "concurrently -g 'yarn lint:eslint' 'yarn lint:prettier' 'yarn run lint:unused-exports' 'yarn run lint:typecheck'",
"lint": "concurrently -g 'yarn lint:eslint' 'yarn lint:prettier' 'yarn run lint:typecheck'",
"lint:eslint": "eslint \"src/**/*.{js,ts}\"",
"lint:eslint:fix": "eslint --fix \"src/**/*.{js,ts}\"",
"lint:prettier": "prettier --check \"src/**/*.{js,ts}\" \"*.{js,json}\"",
"lint:prettier:fix": "prettier --write \"src/**/*.{ts}\" *.js",
"lint:unused-exports": "ts-unused-exports tsconfig.json --ignoreFiles=tests",
"lint:prettier": "prettier --check \"{src,tests}/**/*.ts\" \"*.js\"",
"lint:prettier:fix": "prettier --write \"{src,tests}/**/*.ts\" *.js",
"lint:typecheck": "tsc --noEmit"
},
"keywords": [],
Expand All @@ -39,7 +39,6 @@
"typescript-eslint": "^7.7.0",
"@ledgerhq/hw-transport-node-hid": "^6.28.6",
"@ledgerhq/hw-transport-webusb": "^6.28.6",
"ts-unused-exports": "^10.0.1",
"typecheck": "^0.1.2",
"prettier-eslint": "^16.3.0",
"lint": "^0.8.19",
Expand Down
6 changes: 4 additions & 2 deletions src/constants/ethereum-constants.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/** @format */

import { EthereumNetwork, EthereumNetworkID } from '../models/ethereum-models.js';

export const ethereumArbitrumSepolia: EthereumNetwork = {
Expand All @@ -16,7 +15,10 @@ export const ethereumArbitrum: EthereumNetwork = {
defaultNodeURL: 'https://arb1.arbitrum.io/rpc',
};

export const supportedEthereumNetworks: EthereumNetwork[] = [ethereumArbitrumSepolia, ethereumArbitrum];
export const supportedEthereumNetworks: EthereumNetwork[] = [
ethereumArbitrumSepolia,
ethereumArbitrum,
];

export const hexChainIDs: { [key in EthereumNetworkID]: string } = {
[EthereumNetworkID.ArbitrumSepolia]: '0x66eee',
Expand Down
96 changes: 73 additions & 23 deletions src/dlc-handlers/ledger-dlc-handler.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/** @format */

import { Transaction } from '@scure/btc-signer';
import { P2Ret, P2TROut, p2wpkh } from '@scure/btc-signer/payment';
import { Network, Psbt } from 'bitcoinjs-lib';
import { bitcoin, regtest, testnet } from 'bitcoinjs-lib/src/networks.js';
import { AppClient, DefaultWalletPolicy, WalletPolicy } from 'ledger-bitcoin';

import {
createBitcoinInputSigningConfiguration,
createTaprootMultisigPayment,
Expand All @@ -14,7 +14,6 @@ import {
getInputByPaymentTypeArray,
getUnspendableKeyCommittedToUUID,
} from '../functions/bitcoin-functions.js';
import { RawVault } from '../models/ethereum-models.js';
import {
addNativeSegwitSignaturesToPSBT,
addTaprootInputSignaturesToPSBT,
Expand All @@ -25,8 +24,9 @@ import {
updateNativeSegwitInputs,
updateTaprootInputs,
} from '../functions/psbt-functions.js';
import { truncateAddress } from '../utilities/index.js';
import { PaymentInformation } from '../models/bitcoin-models.js';
import { RawVault } from '../models/ethereum-models.js';
import { truncateAddress } from '../utilities/index.js';

interface LedgerPolicyInformation {
nativeSegwitWalletPolicy: DefaultWalletPolicy;
Expand Down Expand Up @@ -55,15 +55,22 @@ export class LedgerDLCHandler {
switch (bitcoinNetwork) {
case bitcoin:
this.bitcoinBlockchainAPI = 'https://mempool.space/api';
this.bitcoinBlockchainFeeRecommendationAPI = 'https://mempool.space/api/v1/fees/recommended';
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';
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');
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;
Expand Down Expand Up @@ -155,7 +162,13 @@ export class LedgerDLCHandler {

const nativeSegwitWalletPolicy = new DefaultWalletPolicy('wpkh(@0/**)', nativeSegwitKeyinfo);

const nativeSegwitAddress = await this.ledgerApp.getWalletAddress(nativeSegwitWalletPolicy, null, 0, 0, false);
const nativeSegwitAddress = await this.ledgerApp.getWalletAddress(
nativeSegwitWalletPolicy,
null,
0,
0,
false
);

const nativeSegwitDerivedPublicKey = deriveUnhardenedPublicKey(
nativeSegwitExtendedPublicKey,
Expand All @@ -164,21 +177,32 @@ export class LedgerDLCHandler {
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`);
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 unspendableDerivedPublicKey = deriveUnhardenedPublicKey(
unspendablePublicKey,
this.bitcoinNetwork
);

const attestorDerivedPublicKey = deriveUnhardenedPublicKey(attestorGroupPublicKey, 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 taprootDerivedPublicKey = deriveUnhardenedPublicKey(
taprootExtendedPublicKey,
this.bitcoinNetwork
);

const descriptors =
taprootDerivedPublicKey.toString('hex') < attestorDerivedPublicKey.toString('hex')
Expand All @@ -191,7 +215,9 @@ export class LedgerDLCHandler {
[unspendablePublicKey, ...descriptors]
);

const [, taprootMultisigPolicyHMac] = await this.ledgerApp.registerWallet(taprootMultisigAccountPolicy);
const [, taprootMultisigPolicyHMac] = await this.ledgerApp.registerWallet(
taprootMultisigAccountPolicy
);

const taprootMultisigAddress = await this.ledgerApp.getWalletAddress(
taprootMultisigAccountPolicy,
Expand All @@ -212,7 +238,11 @@ export class LedgerDLCHandler {
throw new Error(`Recreated Multisig Address does not match the Ledger Multisig Address`);
}

this.setPolicyInformation(nativeSegwitWalletPolicy, taprootMultisigAccountPolicy, taprootMultisigPolicyHMac);
this.setPolicyInformation(
nativeSegwitWalletPolicy,
taprootMultisigAccountPolicy,
taprootMultisigPolicyHMac
);
this.setPaymentInformation(
nativeSegwitPayment,
nativeSegwitDerivedPublicKey,
Expand All @@ -224,19 +254,30 @@ export class LedgerDLCHandler {
}
}

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

if (taprootMultisigPayment.address === undefined || nativeSegwitPayment.address === undefined) {
if (
taprootMultisigPayment.address === undefined ||
nativeSegwitPayment.address === undefined
) {
throw new Error('Payment Address is undefined');
}

const feeRate =
customFeeRate ?? BigInt(await getFeeRate(this.bitcoinBlockchainFeeRecommendationAPI, feeRateMultiplier));
customFeeRate ??
BigInt(await getFeeRate(this.bitcoinBlockchainFeeRecommendationAPI, feeRateMultiplier));

const addressBalance = await getBalance(nativeSegwitPayment.address, this.bitcoinBlockchainAPI);
const addressBalance = await getBalance(
nativeSegwitPayment.address,
this.bitcoinBlockchainAPI
);

if (BigInt(addressBalance) < vault.valueLocked.toBigInt()) {
throw new Error('Insufficient Funds');
Expand Down Expand Up @@ -292,14 +333,16 @@ export class LedgerDLCHandler {
customFeeRate?: bigint
): Promise<Psbt> {
try {
const { nativeSegwitPayment, taprootMultisigPayment, taprootDerivedPublicKey } = this.getPaymentInformation();
const { nativeSegwitPayment, taprootMultisigPayment, taprootDerivedPublicKey } =
this.getPaymentInformation();

if (nativeSegwitPayment.address === undefined) {
throw new Error('Could not get Addresses from Payments');
}

const feeRate =
customFeeRate ?? BigInt(await getFeeRate(this.bitcoinBlockchainFeeRecommendationAPI, feeRateMultiplier));
customFeeRate ??
BigInt(await getFeeRate(this.bitcoinBlockchainFeeRecommendationAPI, feeRateMultiplier));

const closingPSBT = createClosingTransaction(
vault.valueLocked.toBigInt(),
Expand Down Expand Up @@ -345,15 +388,22 @@ export class LedgerDLCHandler {

async signPSBT(psbt: Psbt, transactionType: 'funding' | 'closing'): Promise<Transaction> {
try {
const { nativeSegwitWalletPolicy, taprootMultisigWalletPolicy, taprootMultisigWalletPolicyHMac } =
this.getPolicyInformation();
const {
nativeSegwitWalletPolicy,
taprootMultisigWalletPolicy,
taprootMultisigWalletPolicyHMac,
} = this.getPolicyInformation();

let signatures;
let transaction: Transaction;

switch (transactionType) {
case 'funding':
signatures = await this.ledgerApp.signPsbt(psbt.toBase64(), nativeSegwitWalletPolicy, null);
signatures = await this.ledgerApp.signPsbt(
psbt.toBase64(),
nativeSegwitWalletPolicy,
null
);
addNativeSegwitSignaturesToPSBT(psbt, signatures);
transaction = Transaction.fromPSBT(psbt.toBuffer());
transaction.finalize();
Expand Down
Loading

0 comments on commit 18c9233

Please sign in to comment.