Skip to content

Commit

Permalink
chore: lint utils/index.ts file
Browse files Browse the repository at this point in the history
  • Loading branch information
iamcrazycoder committed Aug 3, 2023
1 parent c11f46a commit 950ed7c
Showing 1 changed file with 33 additions and 29 deletions.
62 changes: 33 additions & 29 deletions packages/sdk/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import * as ecc from "@bitcoinerlab/secp256k1";
import { BIP32Interface } from "bip32";
import * as bitcoin from "bitcoinjs-lib";
import ECPairFactory from "ecpair";
import * as ecc from "@bitcoinerlab/secp256k1"
import { BIP32Interface } from "bip32"
import * as bitcoin from "bitcoinjs-lib"
import ECPairFactory from "ecpair"

import { AddressFormats, AddressTypes } from "../addresses/formats"
import { Network } from "../config/types"
import { CalculateTxFeeOptions, CalculateTxWeightOptions } from "./types"

export function getNetwork(value: Network) {
if (value === "mainnet") {
return bitcoin.networks["bitcoin"];
return bitcoin.networks["bitcoin"]
}

return bitcoin.networks[value];
return bitcoin.networks[value]
}

export function createTransaction(
Expand All @@ -19,21 +21,21 @@ export function createTransaction(
network: Network | bitcoin.Network,
paymentOptions?: bitcoin.Payment
) {
bitcoin.initEccLib(ecc);
const networkObj = typeof network === "string" ? getNetwork(network) : network;
bitcoin.initEccLib(ecc)
const networkObj = typeof network === "string" ? getNetwork(network) : network

if (type === "p2tr") {
return bitcoin.payments.p2tr({ internalPubkey: key, network: networkObj, ...paymentOptions });
return bitcoin.payments.p2tr({ internalPubkey: key, network: networkObj, ...paymentOptions })
}

if (type === "p2sh") {
return bitcoin.payments.p2sh({
redeem: bitcoin.payments.p2wpkh({ pubkey: key, network: networkObj }),
network: networkObj
});
})
}

return bitcoin.payments[type]({ pubkey: key, network: networkObj });
return bitcoin.payments[type]({ pubkey: key, network: networkObj })
}

export function getDerivationPath(formatType: AddressFormats, account = 0, addressIndex = 0) {
Expand All @@ -42,8 +44,8 @@ export function getDerivationPath(formatType: AddressFormats, account = 0, addre
"nested-segwit": `m/49'/0'/${account}'/0/${addressIndex}`,
segwit: `m/84'/0'/${account}'/0/${addressIndex}`,
taproot: `m/86'/0'/${account}'/0/${addressIndex}`
};
return pathFormat[formatType];
}
return pathFormat[formatType]
}

export function hdNodeToChild(
Expand All @@ -52,9 +54,9 @@ export function hdNodeToChild(
addressIndex = 0,
account = 0
) {
const fullDerivationPath = getDerivationPath(formatType, account, addressIndex);
const fullDerivationPath = getDerivationPath(formatType, account, addressIndex)

return node.derivePath(fullDerivationPath);
return node.derivePath(fullDerivationPath)
}

export function calculateTxFeeWithRate(
Expand All @@ -63,43 +65,45 @@ export function calculateTxFeeWithRate(
feeRate = 10,
hasChangeOutput: 0 | 1 = 1
): number {
const baseTxSize = 10;
const inSize = 180;
const outSize = 34;
const baseTxSize = 10
const inSize = 180
const outSize = 34

const txSize = baseTxSize + inputsLength * inSize + outputsLength * outSize + hasChangeOutput * outSize;
const fee = txSize * feeRate;
return fee;
const txSize = baseTxSize + inputsLength * inSize + outputsLength * outSize + hasChangeOutput * outSize
const fee = txSize * feeRate
return fee
}

export function toXOnly(pubkey: Buffer): Buffer {
return pubkey.subarray(1, 33);
return pubkey.subarray(1, 33)
}

export function tweakSigner(signer: bitcoin.Signer, opts: any = {}): bitcoin.Signer {
const ECPair = ECPairFactory(ecc);
const ECPair = ECPairFactory(ecc)

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
let privateKey: Uint8Array | undefined = signer.privateKey!;
let privateKey: Uint8Array | undefined = signer.privateKey!
if (!privateKey) {
throw new Error("Private key is required for tweaking signer!");
throw new Error("Private key is required for tweaking signer!")
}
if (signer.publicKey[0] === 3) {
privateKey = ecc.privateNegate(privateKey);
privateKey = ecc.privateNegate(privateKey)
}

const tweakedPrivateKey = ecc.privateAdd(privateKey, tapTweakHash(toXOnly(signer.publicKey), opts.tweakHash));
const tweakedPrivateKey = ecc.privateAdd(privateKey, tapTweakHash(toXOnly(signer.publicKey), opts.tweakHash))
if (!tweakedPrivateKey) {
throw new Error("Invalid tweaked private key!");
throw new Error("Invalid tweaked private key!")
}

return ECPair.fromPrivateKey(Buffer.from(tweakedPrivateKey), {
network: opts.network
});
})
}

export function tapTweakHash(pubKey: Buffer, h: Buffer | undefined): Buffer {
return bitcoin.crypto.taggedHash("TapTweak", Buffer.concat(h ? [pubKey, h] : [pubKey]))
}

export function calculateTxFee({
totalInputs,
Expand Down

0 comments on commit 950ed7c

Please sign in to comment.