0.0.47 • Published 1 year ago

@allstake/bitcoin-sdk v0.0.47

Weekly downloads
-
License
-
Repository
-
Last release
1 year ago

BitHive Bitcoin SDK

BitHive SDK for Bitcoin Staking

Deposit

import {
  validateDepositInTransaction,
  buildUnsignedDepositPsbt,
  CommonUtxo,
  DepositMetadata,
  H256,
  PublicKey,
} from '@bithive/bitcoin-sdk';
import * as bitcoin from 'bitcoinjs-lib';
// Testnet
const network = bitcoin.networks.testnet;

// User public key, address and script
const userPublicKey = PublicKey.fromHex('032b8...8e363');
const userAddress = 'tb1qn...fsk27'; // Native Segwit address
const userScript = bitcoin.address.toOutputScript(userAddress, network);

// Deposit amount
const depositAmount = 2500;
// Fee rate of deposit transaction
const depositFeeRate = 2;

// Following values are just examples, please fetch from BitHive contract
// Chain Signatures public key
const chainSignaturesPublicKey = PublicKey.fromHex(
  '020000000000000000000000000000000000000000000000000000000000000000',
);
// The minimum number of blocks that need to be confirmed before SOLO withdrawal
const soloWithdrawSequenceHeight = 2;
// The earliest block height at which deposits are open
const earliestDepositBlockHeight = 0;

// Deposit metadata
const depositMetadata: DepositMetadata = {
  publicKey: userPublicKey,
  chainSignaturesPublicKey,
  soloWithdrawSequenceHeight,
  earliestDepositBlockHeight,
};

// Common UTXOs owned by user
const utxos: CommonUtxo[] = [
  {
    txHash: H256.fromHex(
      '0000000000000000000000000000000000000000000000000000000000000001',
    ),
    vout: 1,
    value: 1234,
    script: userScript,
  },
  {
    txHash: H256.fromHex(
      '0000000000000000000000000000000000000000000000000000000000000002',
    ),
    vout: 2,
    value: 2345,
    script: userScript,
  },
  {
    txHash: H256.fromHex(
      '0000000000000000000000000000000000000000000000000000000000000003',
    ),
    vout: 3,
    value: 3456,
    script: userScript,
  },
  {
    txHash: H256.fromHex(
      '0000000000000000000000000000000000000000000000000000000000000004',
    ),
    vout: 4,
    value: 4567,
    script: userScript,
  },
];

// 1. Build the deposit PSBT
const { psbt, metadata: psbtMetadata } = await buildUnsignedDepositPsbt({
  utxos,
  amount: depositAmount,
  // strategy: 'Greedy', // Specify `strategy` here if needed
  // feeLimit: 1000, // Specify `feeLimit` here if needed
  feeRate: depositFeeRate,
  changeScript: userScript,
  metadata: depositMetadata,
  network,
});
console.log(psbt, psbtMetadata);

// 2. Sign the deposit PSBT by user
/* Implement here by yourself */

// 3. Finalize the deposit PSBT
psbt.finalizeAllInputs();

// 4. Extract the deposit transaction
const transaction = psbt.extractTransaction();

// 5. Validate the deposit transaction. Don't forget to save the redeem script because it will be used during withdrawals
const { redeemScript } = validateDepositInTransaction({
  transaction,
  amount: depositAmount,
  position: psbtMetadata.position,
  metadata: depositMetadata,
  network,
});

// 6. Broadcast the transaction
/* Implement here by yourself */

Withdrawal

import {
  buildUnsignedWithdrawPsbt,
  finalizeWithdrawPsbt,
  DepositUtxo,
  RedepositMetadata,
  H256,
  PublicKey,
} from '@bithive/bitcoin-sdk';
import * as bitcoin from 'bitcoinjs-lib';
// Testnet
const network = bitcoin.networks.testnet;

// User public key, address and script
const userPublicKey = PublicKey.fromHex('032b8...8e363');
const userAddress = 'tb1qn...fsk27'; // Native Segwit address
const userScript = bitcoin.address.toOutputScript(userAddress, network);

// Withdrawal amount
const withdrawAmount = 1000;
// Fee rate of withdrawal transaction
const withdrawFeeRate = 2;

// Following values are just examples, please fetch from BitHive contract
// Chain Signatures public key
const chainSignaturesPublicKey = PublicKey.fromHex(
  '020000000000000000000000000000000000000000000000000000000000000000',
);
// The minimum number of blocks that need to be confirmed before SOLO withdrawal
const soloWithdrawSequenceHeight = 2;
// The earliest block height at which deposits are open
const earliestDepositBlockHeight = 0;

// Redeposit metadata
const redepositMetadata: RedepositMetadata = {
  publicKey: userPublicKey,
  chainSignaturesPublicKey,
  soloWithdrawSequenceHeight,
  earliestDepositBlockHeight,
};

// Redeem script
const redeemScript = Buffer.from('6352b...2ae68', 'hex');

// Deposit payment and script
const depositPayment = bitcoin.payments.p2wsh({
  redeem: {
    output: redeemScript,
    network,
  },
  network,
});
const depositScript = depositPayment.output!;

// Deposit UTXOs owned by user
const utxos: DepositUtxo[] = [
  {
    txHash: H256.fromHex(
      '0000000000000000000000000000000000000000000000000000000000000000',
    ),
    vout: 0,
    value: 2500,
    script: depositScript,
    redeemScript,
    // soloWithdrawSequenceHeight, // Required by SOLO withdrawal
  },
];

// 1. Build the withdrawal PSBT
const { psbt, metadata: psbtMetadata } = await buildUnsignedWithdrawPsbt({
  utxos,
  amount: withdrawAmount,
  // strategy: 'Greedy', // Specify `strategy` here if needed
  recipientScript: userScript,
  // feeLimit: 1500, // Specify `feeLimit` here if needed
  feeRate: withdrawFeeRate,
  redepositMetadata,
  network,
});
console.log(psbt, psbtMetadata);

// 2. Sign the withdrawal PSBT by user
/* Implement here by yourself */

// 3. Sign the withdrawal PSBT by Chain Signatures. Ignore this if it is a SOLO withdrawal
/* Implement here by yourself */

// 4. Finalize the withdrawal PSBT
finalizeWithdrawPsbt({
  psbt,
  inputs: psbtMetadata.utxos.map((_utxo, index) => ({
    vin: index,
    // User partial signature
    partialSignature: {
      publicKey: userPublicKey,
      // Optional: Specify manually if PSBT doesn't contain signature
    },
    // Chain partial signature. Ignore this if it is a SOLO withdrawal
    chainPartialSignature: {
      publicKey: chainSignaturesPublicKey,
      // Optional: Specify manually if PSBT doesn't contain signature
    },
  })),
  network,
});

// 5. Extract the withdrawal transaction
const transaction = psbt.extractTransaction();

// 6. Broadcast the withdrawal transaction
/* Implement here by yourself */
0.0.40

1 year ago

0.0.41

1 year ago

0.0.42

1 year ago

0.0.43

1 year ago

0.0.44

1 year ago

0.0.45

1 year ago

0.0.46

1 year ago

0.0.47

1 year ago

0.0.40-alpha.1

1 year ago

0.0.40-alpha.2

1 year ago

0.0.37

1 year ago

0.0.38

1 year ago

0.0.39

1 year ago

0.0.30

1 year ago

0.0.31

1 year ago

0.0.32

1 year ago

0.0.33

1 year ago

0.0.34

1 year ago

0.0.35

1 year ago

0.0.36

1 year ago

0.0.26

1 year ago

0.0.27

1 year ago

0.0.28

1 year ago

0.0.29

1 year ago

0.0.20

1 year ago

0.0.21

1 year ago

0.0.22

1 year ago

0.0.23

1 year ago

0.0.24

1 year ago

0.0.25

1 year ago

0.0.15

1 year ago

0.0.16

1 year ago

0.0.17

1 year ago

0.0.18

1 year ago

0.0.19

1 year ago

0.0.12

1 year ago

0.0.13

1 year ago

0.0.14

1 year ago

0.0.11

1 year ago

0.0.10

1 year ago

0.0.9

1 year ago

0.0.8

1 year ago

0.0.7

1 year ago

0.0.6

1 year ago

0.0.5

1 year ago

0.0.4

1 year ago

0.0.3

1 year ago

0.0.2

1 year ago

0.0.1

1 year ago