Babylon
Learn how to add Babylon staking support using Everstake Wallet SDK.
Getting Started
Step. 1: Installing the Library
$ npm install @everstake/wallet-sdk$ yarn add @everstake/wallet-sdkLearn how to add Babylon staking support using Everstake Wallet SDK.
$ npm install @everstake/wallet-sdk$ yarn add @everstake/wallet-sdkWas this helpful?
// or you can also use
import * as Babylon from '@everstake/wallet-sdk/babylon';
// import needed class
import { Babylon } from '@everstake/wallet-sdk/babylon';// import module
const { Babylon } = require("@everstake/wallet-sdk");
// or you can also use
const { Babylon } = require("@everstake/wallet-sdk/babylon");// import
const { CreateToken } = require("@everstake/wallet-sdk");
// Project name
const name = 'Everstake Wallet SDK';
// wallet | Dapp | Other
const type = 'SDK';
// Create Token - return token ID
const token = await CreateToken(name, type);
console.log(token); // 3155f389-d943-4966-8e18-f159c2a6ef66const { Babylon, signetNetwork } = require("@everstake/wallet-sdk/babylon");
// public key from KeyPair. Buffer type
const publicKey = Buffer.from(keyPair.publicKey)
const babylon = new Babylon(signetNetwork, publicKey, 'YOUR_TOKEN');const {payments, initEccLib, networks} = require('bitcoinjs-lib');
const ecc = require('tiny-secp256k1'); // For elliptic curve operations
const {ECPairFactory} = require('ecpair')
const ECPair = ECPairFactory(ecc);
const bs58 = require('bs58');
// Taproot requires tiny-secp256k1 for ECC operations
bitcoin.initEccLib(ecc);
const privateKey = 'XXXXXXXXXXXXqdUAXbpr92hF7M28kTsXXXXXXXXXXXXX'
const keyPair = ECPair.fromWIF(privateKey, networks.testnet)
// Taproot address generation (using single public key)
const { address } = bitcoin.payments.p2tr({
internalPubkey: keyPair.publicKey.slice(1, 33), // Remove prefix byte
network: signetNetwork,
});
console.log('address:', address);
console.log('public key:', bs58.encode(keyPair.publicKey));// decode PSBT that have been got from API response
const resultFromAPI = "012.....0123"
const decodedTx = Psbt.fromHex(resultFromAPI)
// sign tx
const signedTx = decodedTx.signInput(0, keyPair);
signedTx.finalizeAllInputs()
// send to network
const tx = Psbt.fromHex(signedTx.toHex()).extractTransaction();
fetch('https://mempool.space/signet/api/tx', {
method: 'POST',
headers: { 'Content-Type': 'text/plain' },
body: tx.toHex()
})
.then(response => response.text())
.then(txid => {
console.log('Broadcasted Transaction ID:', txid);
})
.catch(error => {
console.error('Failed to broadcast transaction:', error);
});const { Babylon, signetNetwork } = require("@everstake/wallet-sdk/babylon");
const {crypto, Psbt} = require('bitcoinjs-lib');
// Token ID.
const token = process.env.TOKEN; // 3155f389-d943-4966-8e18-f159c2a6ef66
// create instance of Babylon class
const babylon = new Babylon(signetNetwork, Buffer.from(keyPair.publicKey), token);
// amount of stake (sats)
const amount = 50000;
// get unsigned stake tx
const unsignedStakeTx = await babylon.stake(amount);
// sign tx
const tweakedChildNode = keyPair.tweak(
crypto.taggedHash('TapTweak', keyPair.publicKey.slice(1, 33)),
);
const signedTx = unsignedStakeTx.psbt.signInput(0, tweakedChildNode);
signedTx.finalizeAllInputs()
// extract and sent to network
const stakingTx = Psbt.fromHex(signedTx.toHex()).extractTransaction();
fetch('https://mempool.space/signet/api/tx', {
method: 'POST',
headers: { 'Content-Type': 'text/plain' },
body: stakingTx.toHex()
})
.then(response => response.text())
.then(txid => {
console.log('Broadcasted Transaction ID:', txid);
})
.catch(error => {
console.error('Failed to broadcast transaction:', error);
});const { Babylon, signetNetwork } = require("@everstake/wallet-sdk/babylon");
const { Psbt } = require('bitcoinjs-lib');
// Token ID.
const token = process.env.TOKEN; // 3155f389-d943-4966-8e18-f159c2a6ef66
// create instance of Babylon class
const babylon = new Babylon(signetNetwork, Buffer.from(keyPair.publicKey), token);
// stake tx hash which need to unbond
const stakeTxHash = '00000000000000000000000000000000000000'
// get unsigned tx
const unsignedUnbondingTx = await babylon.unbonding(stakeTxHash);
// sign tx
const signedTx = unsignedUnbondingTx.psbt.signInput(0, keyPair);
signedTx.finalizeAllInputs();
const tx = Psbt.fromHex(signedTx.toHex()).extractTransaction();
// collect data and send it to Babylon API
const stakerSignature = tx.ins[0].witness[0].toString('hex')
const unbondingTxHex = tx.toHex()
await babylon.sendUnbondingTx({
staker_signed_signature_hex: stakerSignature,
staking_tx_hash_hex: stakeTxHash,
unbonding_tx_hash_hex: tx.getId(),
unbonding_tx_hex: unbondingTxHex,
})const { Babylon, signetNetwork } = require("@everstake/wallet-sdk/babylon");
const { Psbt } = require('bitcoinjs-lib');
// Token ID.
const token = process.env.TOKEN; // 3155f389-d943-4966-8e18-f159c2a6ef66
// create instance of Babylon class
const babylon = new Babylon(signetNetwork, Buffer.from(keyPair.publicKey), token);
// stake tx hash which need to withdraw
const stakeTxHash = '00000000000000000000000000000000000000'
// get unsigned tx
const unsignedWithdrawalTx = await babylon.withdrawEarlyUnbonded(stakeTxHash);
// or you should use withdrawTimelockUnbonded instead of withdrawEarlyUnbonded, it depends on conditions
// sign tx
const signedTx = unsignedWithdrawalTx.psbt.signInput(0, keyPair);
signedTx.finalizeAllInputs();
const tx = Psbt.fromHex(signedTx.toHex()).extractTransaction();
// sent to network
fetch('https://mempool.space/signet/api/tx', {
method: 'POST',
headers: { 'Content-Type': 'text/plain' },
body: tx.toHex()
})
.then(response => response.text())
.then(txid => {
console.log('Broadcasted Transaction ID:', txid);
})
.catch(error => {
console.error('Failed to broadcast transaction:', error);
});const { Babylon, signetNetwork } = require("@everstake/wallet-sdk/babylon");
// Token ID.
const token = process.env.TOKEN; // 3155f389-d943-4966-8e18-f159c2a6ef66
// create instance of Babylon class
const babylon = new Babylon(signetNetwork, keyPair.publicKey, token);
console.log(await babylon.getDelegations()); // list of delegations