Skip to content

Commit

Permalink
fix: remove event emitter replace with signing step (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
jrwbabylonlab authored Feb 9, 2025
1 parent ceacde5 commit de1d1cf
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 90 deletions.
4 changes: 2 additions & 2 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
@@ -1,6 +1,6 @@
{
"name": "@babylonlabs-io/btc-staking-ts",
"version": "0.4.0-canary.12",
"version": "0.4.0-canary.13",
"description": "Library exposing methods for the creation and consumption of Bitcoin transactions pertaining to Babylon's Bitcoin Staking protocol.",
"module": "dist/index.js",
"main": "dist/index.cjs",
Expand Down
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@ export * from "./utils/btc";
export * from "./utils/utxo/findInputUTXO";
export * from "./utils/utxo/getPsbtInputFields";
export * from "./utils/utxo/getScriptType";
export {
getBabylonParamByBtcHeight,
getBabylonParamByVersion,
} from "./utils/staking/param";
export * from "./staking/manager";
115 changes: 28 additions & 87 deletions src/staking/manager.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { networks, Psbt, Transaction } from "bitcoinjs-lib";
import { EventEmitter } from "events";
import { StakingParams, VersionedStakingParams } from "../types/params";
import { TransactionResult, UTXO } from "../types";
import { StakerInfo, Staking } from ".";
Expand All @@ -25,27 +24,25 @@ import { StakingErrorCode } from "../error";

export interface BtcProvider {
// Sign a PSBT
signPsbt(psbtHex: string): Promise<string>;
signPsbt(signingStep: SigningStep, psbtHex: string): Promise<string>;
// Sign a message using the ECDSA type
// This is optional and only required if you would like to use the
// `createProofOfPossession` function
signMessage?: (message: string, type: "ecdsa") => Promise<string>;
signMessage?: (signingStep: SigningStep, message: string, type: "ecdsa") => Promise<string>;
}

export interface BabylonProvider {
signTransaction: <T extends object>(msg: {
typeUrl: string;
value: T;
}) => Promise<Uint8Array>
}

// Event types for the BabylonBtcStakingManager
export enum StakingEventType {
SIGNING = "signing",
signTransaction: <T extends object>(
signingStep: SigningStep,
msg: {
typeUrl: string;
value: T;
}
) => Promise<Uint8Array>
}

// Event types for the Signing event
export enum SigningType {
export enum SigningStep {
STAKING_SLASHING = "staking-slashing",
UNBONDING_SLASHING = "unbonding-slashing",
PROOF_OF_POSSESSION = "proof-of-possession",
Expand All @@ -57,10 +54,6 @@ export enum SigningType {
WITHDRAW_SLASHING = "withdraw-slashing",
}

interface StakingEventMap {
[StakingEventType.SIGNING]: SigningType;
}

interface StakingInputs {
finalityProviderPkNoCoordHex: string;
stakingAmountSat: number;
Expand All @@ -82,7 +75,7 @@ interface InclusionProof {
blockHashHex: string;
}

export class BabylonBtcStakingManager extends EventEmitter {
export class BabylonBtcStakingManager {
private stakingParams: VersionedStakingParams[];
private btcProvider: BtcProvider;
private network: networks.Network;
Expand All @@ -94,7 +87,6 @@ export class BabylonBtcStakingManager extends EventEmitter {
btcProvider: BtcProvider,
babylonProvider: BabylonProvider,
) {
super(); // Initialize EventEmitter
this.network = network;
this.btcProvider = btcProvider;
this.babylonProvider = babylonProvider;
Expand Down Expand Up @@ -170,12 +162,11 @@ export class BabylonBtcStakingManager extends EventEmitter {
stakerBtcInfo,
params,
);
this.emit(
StakingEventType.SIGNING,
SigningType.CREATE_BTC_DELEGATION_MSG,
);
return {
signedBabylonTx: await this.babylonProvider.signTransaction(msg),
signedBabylonTx: await this.babylonProvider.signTransaction(
SigningStep.CREATE_BTC_DELEGATION_MSG,
msg,
),
stakingTx: transaction,
};
}
Expand Down Expand Up @@ -241,12 +232,11 @@ export class BabylonBtcStakingManager extends EventEmitter {
params,
this.getInclusionProof(inclusionProof),
);
this.emit(
StakingEventType.SIGNING,
SigningType.CREATE_BTC_DELEGATION_MSG,
);
return {
signedBabylonTx: await this.babylonProvider.signTransaction(delegationMsg),
signedBabylonTx: await this.babylonProvider.signTransaction(
SigningStep.CREATE_BTC_DELEGATION_MSG,
delegationMsg,
),
};
}

Expand Down Expand Up @@ -333,11 +323,8 @@ export class BabylonBtcStakingManager extends EventEmitter {
inputUTXOs,
);

this.emit(
StakingEventType.SIGNING,
SigningType.STAKING,
);
const signedStakingPsbtHex = await this.btcProvider.signPsbt(
SigningStep.STAKING,
stakingPsbt.toHex()
);
return Psbt.fromHex(signedStakingPsbtHex).extractTransaction();
Expand Down Expand Up @@ -382,11 +369,8 @@ export class BabylonBtcStakingManager extends EventEmitter {
} = staking.createUnbondingTransaction(stakingTx);

const psbt = staking.toUnbondingPsbt(unbondingTx, stakingTx);
this.emit(
StakingEventType.SIGNING,
SigningType.UNBONDING,
);
const signedUnbondingPsbtHex = await this.btcProvider.signPsbt(
SigningStep.UNBONDING,
psbt.toHex(),
);
const signedUnbondingTx = Psbt.fromHex(
Expand Down Expand Up @@ -506,11 +490,8 @@ export class BabylonBtcStakingManager extends EventEmitter {
feeRate,
);

this.emit(
StakingEventType.SIGNING,
SigningType.WITHDRAW_EARLY_UNBONDED,
);
const signedWithdrawalPsbtHex = await this.btcProvider.signPsbt(
SigningStep.WITHDRAW_EARLY_UNBONDED,
unbondingPsbt.toHex()
);
return {
Expand Down Expand Up @@ -556,11 +537,8 @@ export class BabylonBtcStakingManager extends EventEmitter {
feeRate,
);

this.emit(
StakingEventType.SIGNING,
SigningType.WITHDRAW_STAKING_EXPIRED,
);
const signedWithdrawalPsbtHex = await this.btcProvider.signPsbt(
SigningStep.WITHDRAW_STAKING_EXPIRED,
psbt.toHex()
);
return {
Expand Down Expand Up @@ -606,11 +584,8 @@ export class BabylonBtcStakingManager extends EventEmitter {
feeRate,
);

this.emit(
StakingEventType.SIGNING,
SigningType.WITHDRAW_SLASHING,
);
const signedSlashingPsbtHex = await this.btcProvider.signPsbt(
SigningStep.WITHDRAW_SLASHING,
psbt.toHex()
);
return {
Expand All @@ -632,11 +607,9 @@ export class BabylonBtcStakingManager extends EventEmitter {
}
// Create Proof of Possession
const bech32AddressHex = uint8ArrayToHex(fromBech32(bech32Address).data);
this.emit(
StakingEventType.SIGNING,
SigningType.PROOF_OF_POSSESSION,
);

const signedBabylonAddress = await this.btcProvider.signMessage(
SigningStep.PROOF_OF_POSSESSION,
bech32AddressHex,
"ecdsa",
);
Expand All @@ -647,32 +620,6 @@ export class BabylonBtcStakingManager extends EventEmitter {
};
}

/**
* Adds an event listener for a specific event type.
* @param event - The event type to listen for.
* @param listener - The listener function to be called when the event occurs.
* @returns The current event emitter instance.
*/
on<K extends keyof StakingEventMap>(
event: K,
listener: (message: StakingEventMap[K]) => void
): this {
return super.on(event, listener);
}

/**
* Removes an event listener for a specific event type.
* @param event - The event type to stop listening for.
* @param listener - The listener function to remove.
* @returns The current event emitter instance.
*/
off<K extends keyof StakingEventMap>(
event: K,
listener: (message: StakingEventMap[K]) => void
): this {
return super.off(event, listener);
}

/**
* Creates the unbonding, slashing, and unbonding slashing transactions and
* PSBTs.
Expand Down Expand Up @@ -733,11 +680,8 @@ export class BabylonBtcStakingManager extends EventEmitter {
);

// Sign the slashing PSBT
this.emit(
StakingEventType.SIGNING,
SigningType.STAKING_SLASHING,
);
const signedSlashingPsbtHex = await this.btcProvider.signPsbt(
SigningStep.STAKING_SLASHING,
slashingPsbt.toHex(),
);
const signedSlashingTx = Psbt.fromHex(
Expand All @@ -751,11 +695,8 @@ export class BabylonBtcStakingManager extends EventEmitter {
}

// Sign the unbonding slashing PSBT
this.emit(
StakingEventType.SIGNING,
SigningType.UNBONDING_SLASHING,
);
const signedUnbondingSlashingPsbtHex = await this.btcProvider.signPsbt(
SigningStep.UNBONDING_SLASHING,
unbondingSlashingPsbt.toHex(),
);
const signedUnbondingSlashingTx = Psbt.fromHex(
Expand Down

0 comments on commit de1d1cf

Please sign in to comment.