Skip to content

Commit

Permalink
fix Bitcoin & Liquid types
Browse files Browse the repository at this point in the history
  • Loading branch information
michael1011 committed Jan 4, 2024
1 parent eacabc6 commit f378a8b
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 37 deletions.
44 changes: 36 additions & 8 deletions src/compat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ import zkp, {
} from "@vulpemventures/secp256k1-zkp";
import { Transaction, address, networks } from "bitcoinjs-lib";
import {
ClaimDetails,
RefundDetails,
TransactionOutput,
constructClaimTransaction,
constructRefundTransaction,
targetFee,
} from "boltz-core";
import {
LiquidClaimDetails,
LiquidRefundDetails,
init,
constructClaimTransaction as lcCT,
Expand All @@ -33,9 +35,11 @@ import { network } from "./config";
import { LBTC } from "./consts";

type LiquidTransactionOutputWithKey = LiquidTransactionOutput & {
blindingPrivateKey: Buffer;
blindingPrivateKey?: Buffer;
};

type DecodedAddress = { script: Buffer; blindingKey?: Buffer };

export let secp: {
ecdh: Ecdh;
ecc: Ecc;
Expand Down Expand Up @@ -64,10 +68,7 @@ const getAddress = (asset: string): typeof address | typeof LiquidAddress => {
}
};

const decodeAddress = (
asset: string,
addr: string,
): { script: Buffer; blindingKey?: Buffer } => {
const decodeAddress = (asset: string, addr: string): DecodedAddress => {
const address = getAddress(asset);

// We always do this to validate the network
Expand Down Expand Up @@ -110,13 +111,38 @@ const getTransaction = (asset: string) => {
};

const getConstructClaimTransaction = (asset: string) => {
return asset === LBTC ? lcCT : constructClaimTransaction;
return (
utxos: ClaimDetails[] | LiquidClaimDetails[],
destinationScript: Buffer,
fee: number,
isRbf?: boolean,
assetHash?: string,
blindingKey?: Buffer,
) => {
if (asset === LBTC) {
return lcCT(
utxos as LiquidClaimDetails[],
destinationScript,
fee,
isRbf,
assetHash,
blindingKey,
);
} else {
return constructClaimTransaction(
utxos as ClaimDetails[],
destinationScript,
fee,
isRbf,
);
}
};
};

const getConstructRefundTransaction = (asset: string) => {
const fn = asset === LBTC ? lcRT : constructRefundTransaction;
return (
refundDetails: RefundDetails | LiquidRefundDetails,
refundDetails: RefundDetails[] | LiquidRefundDetails[],
outputScript: Buffer,
timeoutBlockHeight: number,
feePerVbyte: number,
Expand All @@ -126,7 +152,7 @@ const getConstructRefundTransaction = (asset: string) => {
) =>
targetFee(feePerVbyte, (fee) =>
fn(
refundDetails as any,
refundDetails as any[],
outputScript,
timeoutBlockHeight,
fee,
Expand Down Expand Up @@ -164,7 +190,9 @@ export {
getNetwork,
decodeAddress,
getTransaction,
DecodedAddress,
getOutputAmount,
getConstructClaimTransaction,
getConstructRefundTransaction,
LiquidTransactionOutputWithKey,
};
55 changes: 30 additions & 25 deletions src/helper.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { detectSwap } from "boltz-core";
import { ClaimDetails, RefundDetails, detectSwap } from "boltz-core";
import {
LiquidClaimDetails,
LiquidRefundDetails,
} from "boltz-core/dist/lib/liquid";
import { Buffer } from "buffer";
import log from "loglevel";

import {
DecodedAddress,
decodeAddress,
getAddress,
getConstructClaimTransaction,
Expand Down Expand Up @@ -46,21 +51,21 @@ export const isIos = !!navigator.userAgent.match(/iphone|ipad/gi) || false;
export const isMobile =
isIos || !!navigator.userAgent.match(/android|blackberry/gi) || false;

const parseBlindingKey = (swap: any) => {
const parseBlindingKey = (swap: { blindingKey: string | undefined }) => {
return swap.blindingKey ? Buffer.from(swap.blindingKey, "hex") : undefined;
};

export const cropString = (str: any) => {
export const cropString = (str: string) => {
if (str.length < 40) {
return str;
}
return str.substring(0, 19) + "..." + str.substring(str.length - 19);
};

export const checkReferralId = () => {
const ref_param = new URLSearchParams(window.location.search).get("ref");
if (ref_param && ref_param !== "") {
setRef(ref_param);
const refParam = new URLSearchParams(window.location.search).get("ref");
if (refParam && refParam !== "") {
setRef(refParam);
window.history.replaceState(
{},
document.title,
Expand All @@ -69,7 +74,7 @@ export const checkReferralId = () => {
}
};

export const startInterval = (cb: any, interval: number) => {
export const startInterval = (cb: () => any, interval: number) => {
cb();
return setInterval(cb, interval);
};
Expand Down Expand Up @@ -110,8 +115,8 @@ export const getApiUrl = (asset: string) => {

export const fetcher = (
url: string,
cb: any,
params = null,
cb: (value: any) => void,
params: any | undefined = null,
errorCb = errorHandler,
) => {
let opts = {};
Expand Down Expand Up @@ -184,13 +189,12 @@ export const setSwapStatusAndClaim = (data: any, activeSwap: any) => {
};

export async function refund(swap: any, t: any) {
let output: any;
setRefundTx("");

log.debug("starting to refund swap", swap);
setRefundTx("");

const asset_name = swap.asset;

let output: DecodedAddress;
try {
output = decodeAddress(asset_name, refundAddress());
} catch (e) {
Expand Down Expand Up @@ -238,17 +242,16 @@ export async function refund(swap: any, t: any) {
Buffer.from(swap.privateKey, "hex"),
);
log.debug("privkey", private_key);
// TODO: @michael1011 whats up here?
const refundTransaction = constructRefundTransaction(
// @ts-ignore
[
{
...swapOutput,
value: 0,
txHash: tx.getHash(),
redeemScript: script,
keys: private_key,
blindingPrivateKey: parseBlindingKey(swap),
},
} as RefundDetails & LiquidRefundDetails,
],
output.script,
txToRefund.timeoutBlockHeight,
Expand Down Expand Up @@ -320,7 +323,7 @@ export async function refund(swap: any, t: any) {
return true;
}

export async function getfeeestimation(swap: any) {
export async function getfeeestimation(swap: any): Promise<number> {
return new Promise((resolve) => {
fetcher("/getfeeestimation", (data: any) => {
log.debug("getfeeestimation: ", data);
Expand All @@ -330,27 +333,29 @@ export async function getfeeestimation(swap: any) {
});
}

const createAdjustedClaim = (
const createAdjustedClaim = <
T extends
| (ClaimDetails & { blindingPrivateKey?: Buffer })
| LiquidClaimDetails,
>(
swap: any,
claimDetails: any,
destination: any,
assetHash: any,
blindingKey: any,
claimDetails: T[],
destination: Buffer,
assetHash?: string,
blindingKey?: Buffer,
) => {
const inputSum = claimDetails.reduce(
(total: any, input: any) => total + getOutputAmount(swap.asset, input),
(total: number, input: T) => total + getOutputAmount(swap.asset, input),
0,
);
const feeBudget = Math.floor(inputSum - swap.receiveAmount);

const constructClaimTransaction = getConstructClaimTransaction(swap.asset);
return constructClaimTransaction(
claimDetails,
claimDetails as ClaimDetails[] | LiquidClaimDetails[],
destination,
feeBudget,
true,
// TODO: help me
// @ts-ignore
assetHash,
blindingKey,
);
Expand Down
9 changes: 5 additions & 4 deletions src/signals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ import { pairs } from "./config";
import { LN, sideSend } from "./consts";
import { isMobile } from "./helper";

type SwapStatusTransaction = {
hex?: string;
id?: string;
};

const defaultSelection = Object.keys(pairs)[0].split("/")[0];

// ui
Expand Down Expand Up @@ -41,10 +46,6 @@ export const [swap, setSwap] = createSignal(null, {
equals: () => false,
});
export const [swapStatus, setSwapStatus] = createSignal(null);
interface SwapStatusTransaction {
hex?: string;
id?: string;
}
export const [swapStatusTransaction, setSwapStatusTransaction] =
createSignal<SwapStatusTransaction>({});
export const [failureReason, setFailureReason] = createSignal("");
Expand Down

0 comments on commit f378a8b

Please sign in to comment.