|
| 1 | +import { BIP32Interface, ECPairInterface } from '@bitgo/utxo-lib'; |
| 2 | +import { StakerInfo, StakingInputs, VersionedStakingParams } from '@bitgo/babylonlabs-io-btc-staking-ts'; |
| 3 | + |
| 4 | +import { BabylonDescriptorBuilder } from './descriptor'; |
| 5 | + |
| 6 | +export type JsonParams = { |
| 7 | + version: number; |
| 8 | + covenant_pks: string[]; |
| 9 | + covenant_quorum: number; |
| 10 | + min_staking_value_sat: number; |
| 11 | + max_staking_value_sat: number; |
| 12 | + min_staking_time_blocks: number; |
| 13 | + max_staking_time_blocks: number; |
| 14 | + slashing_pk_script: string; |
| 15 | + min_slashing_tx_fee_sat: number; |
| 16 | + slashing_rate: string; |
| 17 | + unbonding_time_blocks: number; |
| 18 | + unbonding_fee_sat: number; |
| 19 | + min_commission_rate: string; |
| 20 | + delegation_creation_base_gas_fee: number; |
| 21 | + allow_list_expiration_height: number; |
| 22 | + btc_activation_height: number; |
| 23 | +}; |
| 24 | + |
| 25 | +// Source: https://github.com/babylonlabs-io/babylon/blob/v1.99.0-snapshot.250211/app/upgrades/v1/testnet/btcstaking_params.go#L149-L159 |
| 26 | +export const testnetStakingParams: JsonParams = { |
| 27 | + version: 5 /* it's the sixth element in the array */, |
| 28 | + covenant_pks: [ |
| 29 | + 'fa9d882d45f4060bdb8042183828cd87544f1ea997380e586cab77d5fd698737', |
| 30 | + '0aee0509b16db71c999238a4827db945526859b13c95487ab46725357c9a9f25', |
| 31 | + '17921cf156ccb4e73d428f996ed11b245313e37e27c978ac4d2cc21eca4672e4', |
| 32 | + '113c3a32a9d320b72190a04a020a0db3976ef36972673258e9a38a364f3dc3b0', |
| 33 | + '79a71ffd71c503ef2e2f91bccfc8fcda7946f4653cef0d9f3dde20795ef3b9f0', |
| 34 | + '3bb93dfc8b61887d771f3630e9a63e97cbafcfcc78556a474df83a31a0ef899c', |
| 35 | + 'd21faf78c6751a0d38e6bd8028b907ff07e9a869a43fc837d6b3f8dff6119a36', |
| 36 | + '40afaf47c4ffa56de86410d8e47baa2bb6f04b604f4ea24323737ddc3fe092df', |
| 37 | + 'f5199efae3f28bb82476163a7e458c7ad445d9bffb0682d10d3bdb2cb41f8e8e', |
| 38 | + ], |
| 39 | + covenant_quorum: 6, |
| 40 | + min_staking_value_sat: 50000, |
| 41 | + max_staking_value_sat: 35000000000, |
| 42 | + min_staking_time_blocks: 10000, |
| 43 | + max_staking_time_blocks: 64000, |
| 44 | + slashing_pk_script: 'ABRb4SYk0IorQkCV18ByIcM0UNFL8Q==', |
| 45 | + min_slashing_tx_fee_sat: 5000, |
| 46 | + slashing_rate: '0.05', |
| 47 | + unbonding_time_blocks: 1008, |
| 48 | + unbonding_fee_sat: 2000, |
| 49 | + min_commission_rate: '0.03', |
| 50 | + delegation_creation_base_gas_fee: 1095000, |
| 51 | + allow_list_expiration_height: 26124, |
| 52 | + btc_activation_height: 227174, |
| 53 | +}; |
| 54 | + |
| 55 | +// https://github.com/babylonlabs-io/babylon/blob/v1.99.0-snapshot.250211/app/upgrades/v1/mainnet/btcstaking_params.go#L4-L28 |
| 56 | +export const mainnetStakingParams: JsonParams = { |
| 57 | + version: 0, |
| 58 | + covenant_pks: [ |
| 59 | + '43311589af63c2adda04fcd7792c038a05c12a4fe40351b3eb1612ff6b2e5a0e', |
| 60 | + 'd415b187c6e7ce9da46ac888d20df20737d6f16a41639e68ea055311e1535dd9', |
| 61 | + 'd27cd27dbff481bc6fc4aa39dd19405eb6010237784ecba13bab130a4a62df5d', |
| 62 | + 'a3e107fee8879f5cf901161dbf4ff61c252ba5fec6f6407fe81b9453d244c02c', |
| 63 | + 'c45753e856ad0abb06f68947604f11476c157d13b7efd54499eaa0f6918cf716', |
| 64 | + ], |
| 65 | + covenant_quorum: 3, |
| 66 | + min_staking_value_sat: 10000, |
| 67 | + max_staking_value_sat: 10000000000, |
| 68 | + min_staking_time_blocks: 10, |
| 69 | + max_staking_time_blocks: 65535, |
| 70 | + slashing_pk_script: 'dqkUAQEBAQEBAQEBAQEBAQEBAQEBAQGIrA==', |
| 71 | + min_slashing_tx_fee_sat: 1000, |
| 72 | + slashing_rate: '0.100000000000000000', |
| 73 | + unbonding_time_blocks: 101, |
| 74 | + unbonding_fee_sat: 1000, |
| 75 | + min_commission_rate: '0.03', |
| 76 | + delegation_creation_base_gas_fee: 1000, |
| 77 | + allow_list_expiration_height: 0, |
| 78 | + btc_activation_height: 100, |
| 79 | +}; |
| 80 | + |
| 81 | +export function toVersionedParams(p: JsonParams): VersionedStakingParams { |
| 82 | + return { |
| 83 | + version: p.version, |
| 84 | + btcActivationHeight: p.btc_activation_height, |
| 85 | + covenantNoCoordPks: p.covenant_pks, |
| 86 | + covenantQuorum: p.covenant_quorum, |
| 87 | + unbondingTime: p.unbonding_time_blocks, |
| 88 | + unbondingFeeSat: p.unbonding_fee_sat, |
| 89 | + maxStakingAmountSat: p.max_staking_value_sat, |
| 90 | + minStakingAmountSat: p.min_staking_value_sat, |
| 91 | + maxStakingTimeBlocks: p.max_staking_time_blocks, |
| 92 | + minStakingTimeBlocks: p.min_staking_time_blocks, |
| 93 | + slashing: { |
| 94 | + slashingPkScriptHex: Buffer.from(p.slashing_pk_script, 'base64').toString('hex'), |
| 95 | + slashingRate: parseFloat(p.slashing_rate), |
| 96 | + minSlashingTxFeeSat: p.min_slashing_tx_fee_sat, |
| 97 | + }, |
| 98 | + }; |
| 99 | +} |
| 100 | + |
| 101 | +// Source: https://btcstaking.testnet.babylonlabs.io/ "Babylon Foundation 0" |
| 102 | +export const testnetFinalityProvider0 = Buffer.from( |
| 103 | + 'd23c2c25e1fcf8fd1c21b9a402c19e2e309e531e45e92fb1e9805b6056b0cc76', |
| 104 | + 'hex' |
| 105 | +); |
| 106 | + |
| 107 | +export function getDescriptorBuilderForParams( |
| 108 | + userKey: BIP32Interface | ECPairInterface | Buffer, |
| 109 | + finalityProviderKeys: Buffer[], |
| 110 | + params: Pick< |
| 111 | + VersionedStakingParams, |
| 112 | + 'covenantNoCoordPks' | 'covenantQuorum' | 'minStakingTimeBlocks' | 'unbondingTime' |
| 113 | + > |
| 114 | +): BabylonDescriptorBuilder { |
| 115 | + if (!Buffer.isBuffer(userKey)) { |
| 116 | + userKey = userKey.publicKey; |
| 117 | + } |
| 118 | + return new BabylonDescriptorBuilder( |
| 119 | + userKey, |
| 120 | + finalityProviderKeys, |
| 121 | + params.covenantNoCoordPks.map((pk) => Buffer.from(pk, 'hex')), |
| 122 | + params.covenantQuorum, |
| 123 | + params.minStakingTimeBlocks, |
| 124 | + params.unbondingTime |
| 125 | + ); |
| 126 | +} |
| 127 | + |
| 128 | +export function getDescriptorProviderForStakingParams( |
| 129 | + stakerBtcInfo: StakerInfo, |
| 130 | + stakingInput: StakingInputs, |
| 131 | + stakingParams: VersionedStakingParams |
| 132 | +): BabylonDescriptorBuilder { |
| 133 | + const userKey = Buffer.from(stakerBtcInfo.publicKeyNoCoordHex, 'hex'); |
| 134 | + const finalityProviderKey = Buffer.from(stakingInput.finalityProviderPkNoCoordHex, 'hex'); |
| 135 | + return getDescriptorBuilderForParams(userKey, [finalityProviderKey], stakingParams); |
| 136 | +} |
| 137 | + |
| 138 | +export function getTestnetDescriptorBuilder( |
| 139 | + userKey: BIP32Interface | ECPairInterface | Buffer, |
| 140 | + { |
| 141 | + finalityProviderKeys = [testnetFinalityProvider0], |
| 142 | + }: { |
| 143 | + finalityProviderKeys?: Buffer[]; |
| 144 | + } = {} |
| 145 | +): BabylonDescriptorBuilder { |
| 146 | + return getDescriptorBuilderForParams(userKey, finalityProviderKeys, toVersionedParams(testnetStakingParams)); |
| 147 | +} |
0 commit comments