Skip to content

Commit

Permalink
feat: transition phase-1 delegation to 2 with inclusion proof (#337)
Browse files Browse the repository at this point in the history
  • Loading branch information
jrwbabylonlab authored Nov 13, 2024
1 parent 5c646ca commit 1936db2
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 18 deletions.
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"node": "22.3.0"
},
"dependencies": {
"@babylonlabs-io/babylon-proto-ts": "0.0.3-canary.2",
"@babylonlabs-io/babylon-proto-ts": "0.0.3-canary.3",
"@babylonlabs-io/btc-staking-ts": "0.4.0-canary.2",
"@bitcoin-js/tiny-secp256k1-asmjs": "2.2.3",
"@bitcoinerlab/secp256k1": "^1.1.1",
Expand Down
4 changes: 2 additions & 2 deletions src/app/components/Delegations/Delegation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import { IoIosWarning } from "react-icons/io";
import { Tooltip } from "react-tooltip";

import {
SigningStep,
type SigningStep,
useTransactionService,
} from "@/app/hooks/services/useTransactionService";
import { useHealthCheck } from "@/app/hooks/useHealthCheck";
import {
Delegation as DelegationInterface,
type Delegation as DelegationInterface,
DelegationState,
} from "@/app/types/delegations";
import { shouldDisplayPoints } from "@/config";
Expand Down
40 changes: 35 additions & 5 deletions src/app/hooks/services/useTransactionService.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { btcstakingtx } from "@babylonlabs-io/babylon-proto-ts";
import { InclusionProof } from "@babylonlabs-io/babylon-proto-ts/dist/generated/babylon/btcstaking/v1/btcstaking";
import {
btccheckpoint,
btcstaking,
btcstakingtx,
} from "@babylonlabs-io/babylon-proto-ts";
import {
BTCSigType,
ProofOfPossessionBTC,
Expand All @@ -19,6 +22,7 @@ import {
extractSchnorrSignaturesFromTransaction,
uint8ArrayToHex,
} from "@/utils/delegations";
import { getTxMerkleProof, MerkleProof } from "@/utils/mempool_api";

export interface BtcStakingInputs {
finalityProviderPkNoCoordHex: string;
Expand All @@ -43,7 +47,7 @@ export enum SigningStep {
}

export const useTransactionService = () => {
const { availableUTXOs: inputUTXOs } = useAppState();
const { availableUTXOs: inputUTXOs, params } = useAppState();
const {
connected: cosmosConnected,
bech32Address,
Expand All @@ -58,7 +62,6 @@ export const useTransactionService = () => {
network: btcNetwork,
} = useBTCWallet();

const { params } = useAppState();
const latestParam = params?.bbnStakingParams?.latestParam;
const genesisParam = params?.bbnStakingParams?.genesisParam;

Expand Down Expand Up @@ -194,6 +197,9 @@ export const useTransactionService = () => {
stakingInput.stakingTimeBlocks,
);

// Get the merkle proof
const inclusionProof = await getInclusionProof(stakingTx);
// Create delegation message
const delegationMsg = await createBtcDelegationMsg(
stakingInstance,
stakingInput,
Expand All @@ -206,6 +212,7 @@ export const useTransactionService = () => {
signMessage,
signingCallback,
},
inclusionProof,
);
await sendBbnTx(signingStargateClient!, bech32Address, delegationMsg);
await signingCallback(SigningStep.SEND_BBN);
Expand Down Expand Up @@ -268,7 +275,7 @@ const createBtcDelegationMsg = async (
},
param: BbnStakingParamsVersion,
btcSigningFuncs: BtcSigningFuncs,
inclusionProof?: InclusionProof,
inclusionProof?: btcstaking.InclusionProof,
) => {
const cleanedStakingTx = clearTxSignatures(stakingTx);

Expand Down Expand Up @@ -388,3 +395,26 @@ const checkWalletConnection = (
)
throw new Error("Wallet not connected");
};

const getInclusionProof = async (
stakingTx: Transaction,
): Promise<btcstaking.InclusionProof> => {
// Get the merkle proof
let txMerkleProof: MerkleProof;
try {
txMerkleProof = await getTxMerkleProof(stakingTx.getId());
} catch (err) {
throw new Error("Failed to get the merkle proof", { cause: err });
}

const hash = Uint8Array.from(Buffer.from(stakingTx.getId(), "hex"));
const inclusionProofKey: btccheckpoint.TransactionKey =
btccheckpoint.TransactionKey.fromPartial({
index: txMerkleProof.pos,
hash,
});
return btcstaking.InclusionProof.fromPartial({
key: inclusionProofKey,
proof: Uint8Array.from(Buffer.from(txMerkleProof.proofHex, "hex")),
});
};
25 changes: 19 additions & 6 deletions src/utils/mempool_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { Fees, UTXO } from "./wallet/btc_wallet_provider";

const { mempoolApiUrl } = getNetworkConfig();

interface MerkleProof {
export interface MerkleProof {
blockHeight: number;
merkle: string[];
proofHex: string;
pos: number;
}

Expand Down Expand Up @@ -235,10 +235,23 @@ export async function getTxMerkleProof(txId: string): Promise<MerkleProof> {
const err = await response.text();
throw new ServerError(err, response.status);
}
const data = await response.json();
const data: {
block_height: number;
merkle: string[];
pos: number;
} = await response.json();

const { block_height, merkle, pos } = data;
if (!block_height || !merkle.length || !pos) {
throw new Error("Invalid transaction merkle proof result returned");
}
const proofHex = merkle.reduce((acc: string, m: string) => {
return acc + m;
}, "");

return {
blockHeight: data.block_height,
merkle: data.merkle,
pos: data.pos,
blockHeight: block_height,
proofHex,
pos,
};
}

0 comments on commit 1936db2

Please sign in to comment.