Skip to content

Commit

Permalink
Merge pull request #28 from babylonlabs-io/cut-v3-release
Browse files Browse the repository at this point in the history
Cut v3 release
  • Loading branch information
jrwbabylonlab authored Sep 18, 2024
2 parents 687585d + b9d2f66 commit e943856
Show file tree
Hide file tree
Showing 24 changed files with 1,529 additions and 348 deletions.
6 changes: 3 additions & 3 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
steps:
- checkout
- node/install:
node-version: '21.7'
node-version: '22.3'
- run:
name: Install requirements
command: npm ci
Expand All @@ -23,7 +23,7 @@ jobs:
steps:
- checkout
- node/install:
node-version: '21.7'
node-version: '22.3'
- run:
name: Install requirements
command: npm ci
Expand Down Expand Up @@ -53,7 +53,7 @@ jobs:
steps:
- checkout
- node/install:
node-version: '21.7'
node-version: '22.3'
- run:
name: Authenticate with NPM registry
command: echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" > .npmrc
Expand Down
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
engine-strict=true
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v22.3.0
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Parameters

Licensor: Babylon Labs Ltd.

Licensed Work: btc-staking-ts
Licensed Work: @babylonlabs-io/btc-staking-ts
The Licensed Work is (c) 2024 Babylon Labs Ltd.

Additional Use Grant:
Expand Down
41 changes: 14 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<p align="center">
<img alt="Babylon Logo" src="https://github.com/user-attachments/assets/b21652b5-847d-48b2-89a7-0f0969a50900" width="100" />
<h3 align="center">btc-staking-ts</h3>
<h3 align="center">@babylonlabs-io/btc-staking-ts</h3>
<p align="center">Babylon Bitcoin Staking Protocol</p>
<p align="center"><strong>TypeScript</strong> library</p>
<p align="center">
Expand All @@ -9,15 +9,10 @@
</p>
<br/>

## Notice

The library is in an experimental version and should not be used for production
purposes and with real funds.

## Installation

```console
npm i btc-staking-ts
npm i @babylonlabs-io/btc-staking-ts
```

## Version Release
Expand Down Expand Up @@ -127,7 +122,7 @@ const network = networks.testnet;
```

### Fee Calculation
The fee calculation in the btc-staking-ts library is based on an estimated size
The fee calculation in the @babylonlabs-io/btc-staking-ts library is based on an estimated size
of the transaction in virtual bytes (vB). This estimation helps in calculating
the appropriate fee to include in the transaction to ensure it is processed by
the Bitcoin network efficiently.
Expand All @@ -152,7 +147,7 @@ First, create an instance of the `StakingScriptData` class
and construct the Bitcoin scipts associated with Bitcoin staking using it.

```ts
import { StakingScriptData } from "btc-staking-ts";
import { StakingScriptData } from "@babylonlabs-io/btc-staking-ts";

const stakingScriptData = new StakingScriptData(
stakerPk,
Expand Down Expand Up @@ -191,7 +186,7 @@ transaction and later sign it using a supported wallet's method.
In this instance, we use the `btcWallet.signTransaction()` method.

```ts
import { stakingTransaction } from "btc-staking-ts";
import { stakingTransaction } from "@babylonlabs-io/btc-staking-ts";
import { Psbt, Transaction } from "bitcoinjs-lib";

// stakingTransaction constructs an unsigned BTC Staking transaction
Expand Down Expand Up @@ -225,7 +220,7 @@ the staking transaction timelock expires, subject to an unbonding period.
The unbonding transaction can be created as follows:

```ts
import { unbondingTransaction } from "btc-staking-ts";
import { unbondingTransaction } from "@babylonlabs-io/btc-staking-ts";
import { Psbt, Transaction } from "bitcoinjs-lib";

// Unbonding fee in satoshis. number
Expand Down Expand Up @@ -305,7 +300,7 @@ There are two types of withdrawal

```ts
import { Psbt, Transaction } from "bitcoinjs-lib";
import { withdrawTimelockUnbondedTransaction } from "btc-staking-ts";
import { withdrawTimelockUnbondedTransaction } from "@babylonlabs-io/btc-staking-ts";

// staking transaction. Transaction
const stakingTx: Transaction = undefined;
Expand All @@ -314,7 +309,7 @@ const unsignedWithdrawalPsbt: {psbt: Psbt, fee: number} = withdrawTimelockUnbond
scripts: {
timelockScript,
slashingScript,
unbondingScript,
unbondingScript,
},
stakingTx,
btcWallet.address,
Expand All @@ -329,21 +324,17 @@ const unsignedWithdrawalPsbt: {psbt: Psbt, fee: number} = withdrawTimelockUnbond

```ts
import { Psbt, Transaction } from "bitcoinjs-lib";
import { withdrawEarlyUnbondedTransaction } from "btc-staking-ts";

// unbonding transaction. Transaction
const unbondingTx: Transaction = undefined;
import { withdrawEarlyUnbondedTransaction } from "@babylonlabs-io/btc-staking-ts";

const unsignedWithdrawalPsbt: {psbt: Psbt, fee: number} = withdrawEarlyUnbondedTransaction(
const unsignedWithdrawalPsbt: { psbt: Psbt, fee: number } = withdrawEarlyUnbondedTransaction(
scripts: {
unbondingTimelockScript,
slashingScript,
unbondingTx,
},
unbondingTx,
withdrawalAddress,
network,
feeRate,
stakingOutputIndex,
);

const signedWithdrawalPsbt = await signPsbt(unsignedWithdrawalPsbt.psbt.toHex());
Expand All @@ -366,7 +357,7 @@ These are Babylon parameters and should be collected from the Babylon system.
```ts
// The address to which the slashed funds should go to.
const slashingAddress: string = "";
// The slashing percentage rate.
// The slashing percentage rate. It shall be decimal number between 0-1
const slashingRate: number = 0;
// The required fee for the slashing transaction in satoshis.
const minimumSlashingFee: number = 500;
Expand All @@ -378,7 +369,7 @@ There are two types of slashing transactions:
1. Slashing of the staking transaction when no unbonding has been performed:

```ts
import { slashTimelockUnbondedTransaction } from "btc-staking-ts";
import { slashTimelockUnbondedTransaction } from "@babylonlabs-io/btc-staking-ts";
import { Psbt, Transaction } from "bitcoinjs-lib";

const outputIndex: number = 0;
Expand Down Expand Up @@ -408,22 +399,18 @@ create unsigned unbonding slashing transaction

```ts
import { Psbt, Transaction } from "bitcoinjs-lib";
import { slashEarlyUnbondedTransaction } from "btc-staking-ts";

const outputIndex: number = 0;
import { slashEarlyUnbondedTransaction } from "@babylonlabs-io/btc-staking-ts";

const unsignedUnbondingSlashingPsbt: {psbt: Psbt} = slashEarlyUnbondedTransaction(
scripts: {
slashingScript,
unbondingTimelockScript,
},
unbondingScriptTree,
unbondingTx,
slashingAddress,
slashingRate,
minimumSlashingFee,
network,
outputIndex
);

const signedUnbondingSlashingPsbt = await signPsbt(unsignedUnbondingSlashingPsbt.psbt.toHex());
Expand Down
49 changes: 31 additions & 18 deletions build.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,35 @@
/* eslint-disable no-undef */
import { build } from "esbuild";
import pkg from "./package.json" assert { type: "json" };
const { dependencies } = pkg;
import { readFile } from "fs/promises";
import path from "path";

const shared = {
entryPoints: ["src/index.ts"],
bundle: true,
external: Object.keys(dependencies || {}),
};
async function main() {
const pkgPath = path.resolve("./package.json");
const pkg = JSON.parse(await readFile(pkgPath, "utf8"));
const { dependencies } = pkg;

build({
...shared,
platform: "node", // for CJS
outfile: "dist/index.cjs",
});
const shared = {
entryPoints: ["src/index.ts"],
bundle: true,
external: Object.keys(dependencies || {}),
};

build({
...shared,
platform: "node", // for ESM
outfile: "dist/index.js",
format: "esm",
});
await build({
...shared,
platform: "node", // for CJS
outfile: "dist/index.cjs",
format: "cjs",
});

await build({
...shared,
platform: "node", // for ESM
outfile: "dist/index.js",
format: "esm",
});
}

main().catch((err) => {
console.error(err);
process.exit(1);
});
16 changes: 10 additions & 6 deletions package-lock.json

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

16 changes: 11 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "btc-staking-ts",
"version": "0.2.11",
"description": "Library exposing methods for the creation and consumption of Bitcoin transactions pertaining to Babylon's Bitcoin Staking protocol. Experimental version, should not be used for production purposes or with real funds.",
"name": "@babylonlabs-io/btc-staking-ts",
"version": "0.3.0",
"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",
"typings": "dist/index.d.ts",
Expand All @@ -26,6 +26,9 @@
"babylon",
"btc-staking"
],
"engines": {
"node": "22.3.0"
},
"husky": {
"hooks": {
"pre-commit": "lint-staged"
Expand Down Expand Up @@ -61,7 +64,10 @@
"typescript-eslint": "^7.4.0"
},
"dependencies": {
"@bitcoin-js/tiny-secp256k1-asmjs": "^2.2.3",
"bitcoinjs-lib": "^6.1.5"
"@bitcoin-js/tiny-secp256k1-asmjs": "2.2.3",
"bitcoinjs-lib": "6.1.5"
},
"publishConfig": {
"access": "public"
}
}
6 changes: 6 additions & 0 deletions src/constants/psbt.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// This sequence enables both the locktime field and also replace-by-fee
export const RBF_SEQUENCE = 0xfffffffd;
// This sequence means the transaction is not replaceable
export const NON_RBF_SEQUENCE = 0xffffffff;
// The Transaction version number used across the library(to be set in the psbt)
export const TRANSACTION_VERSION = 2;
Loading

0 comments on commit e943856

Please sign in to comment.