|
| 1 | +import assert from 'assert'; |
| 2 | + |
| 3 | +import * as vendor from '@bitgo/babylonlabs-io-btc-staking-ts'; |
| 4 | +import * as babylonProtobuf from '@babylonlabs-io/babylon-proto-ts'; |
| 5 | +import * as bitcoinjslib from 'bitcoinjs-lib'; |
| 6 | +import { ECPairInterface } from '@bitgo/utxo-lib'; |
| 7 | +import { Descriptor } from '@bitgo/wasm-miniscript'; |
| 8 | +import { toWrappedPsbt } from '@bitgo/utxo-core/descriptor'; |
| 9 | + |
| 10 | +import { BabylonDescriptorBuilder } from './descriptor'; |
| 11 | + |
| 12 | +export const mockBabylonProvider: vendor.BabylonProvider = { |
| 13 | + signTransaction(): Promise<Uint8Array> { |
| 14 | + throw new Error('Function not implemented.'); |
| 15 | + }, |
| 16 | +}; |
| 17 | + |
| 18 | +export type ValueWithTypeUrl<T> = { typeUrl: string; value: T }; |
| 19 | + |
| 20 | +export function getSignedPsbt( |
| 21 | + psbt: bitcoinjslib.Psbt, |
| 22 | + descriptor: Descriptor, |
| 23 | + signers: ECPairInterface[], |
| 24 | + { finalize = false } |
| 25 | +): bitcoinjslib.Psbt { |
| 26 | + const wrappedPsbt = toWrappedPsbt(psbt.toBuffer()); |
| 27 | + const signedInputs = psbt.data.inputs.flatMap((input, i) => { |
| 28 | + assert(input.witnessUtxo); |
| 29 | + if (Buffer.from(descriptor.scriptPubkey()).equals(input.witnessUtxo.script)) { |
| 30 | + wrappedPsbt.updateInputWithDescriptor(i, descriptor); |
| 31 | + const signResults = signers.map((signer) => { |
| 32 | + assert(signer.privateKey); |
| 33 | + return wrappedPsbt.signWithPrv(signer.privateKey); |
| 34 | + }); |
| 35 | + return [[i, signResults]]; |
| 36 | + } |
| 37 | + return []; |
| 38 | + }); |
| 39 | + assert(signedInputs.length > 0); |
| 40 | + if (finalize) { |
| 41 | + wrappedPsbt.finalize(); |
| 42 | + } |
| 43 | + return bitcoinjslib.Psbt.fromBuffer(Buffer.from(wrappedPsbt.serialize())); |
| 44 | +} |
| 45 | + |
| 46 | +export function getBtcProviderForECKey( |
| 47 | + descriptorBuilder: BabylonDescriptorBuilder, |
| 48 | + stakerKey: ECPairInterface |
| 49 | +): vendor.BtcProvider { |
| 50 | + function signWithDescriptor( |
| 51 | + psbt: bitcoinjslib.Psbt, |
| 52 | + descriptor: Descriptor, |
| 53 | + key: ECPairInterface |
| 54 | + ): bitcoinjslib.Psbt { |
| 55 | + psbt = getSignedPsbt(psbt, descriptor, [key], { finalize: false }); |
| 56 | + // BUG: we need to blindly finalize here even though we have not fully signed |
| 57 | + psbt.finalizeAllInputs(); |
| 58 | + return psbt; |
| 59 | + } |
| 60 | + |
| 61 | + return { |
| 62 | + async signMessage(signingStep: vendor.SigningStep, message: string, type: 'ecdsa'): Promise<string> { |
| 63 | + assert(type === 'ecdsa'); |
| 64 | + switch (signingStep) { |
| 65 | + case 'proof-of-possession': |
| 66 | + return stakerKey.sign(Buffer.from(message, 'hex')).toString('hex'); |
| 67 | + default: |
| 68 | + throw new Error(`unexpected signing step: ${signingStep}`); |
| 69 | + } |
| 70 | + }, |
| 71 | + async signPsbt(signingStep: vendor.SigningStep, psbtHex: string): Promise<string> { |
| 72 | + const psbt = bitcoinjslib.Psbt.fromHex(psbtHex); |
| 73 | + switch (signingStep) { |
| 74 | + case 'staking-slashing': |
| 75 | + return signWithDescriptor(psbt, descriptorBuilder.getStakingDescriptor(), stakerKey).toHex(); |
| 76 | + case 'unbonding-slashing': |
| 77 | + return signWithDescriptor(psbt, descriptorBuilder.getUnbondingDescriptor(), stakerKey).toHex(); |
| 78 | + default: |
| 79 | + throw new Error(`unexpected signing step: ${signingStep}`); |
| 80 | + } |
| 81 | + }, |
| 82 | + }; |
| 83 | +} |
| 84 | + |
| 85 | +type Result = { |
| 86 | + unsignedDelegationMsg: ValueWithTypeUrl<babylonProtobuf.btcstakingtx.MsgCreateBTCDelegation>; |
| 87 | + stakingTx: bitcoinjslib.Transaction; |
| 88 | +}; |
| 89 | + |
| 90 | +/* |
| 91 | + * This is mostly lifted from |
| 92 | + * https://github.com/babylonlabs-io/btc-staking-ts/blob/v0.4.0-rc.2/src/staking/manager.ts#L100-L172 |
| 93 | + * |
| 94 | + * The difference is that here we are returning an _unsigned_ delegation message. |
| 95 | + */ |
| 96 | +export async function createUnsignedPreStakeRegistrationBabylonTransaction( |
| 97 | + manager: vendor.BabylonBtcStakingManager, |
| 98 | + stakingParams: vendor.VersionedStakingParams[], |
| 99 | + network: bitcoinjslib.Network, |
| 100 | + stakerBtcInfo: vendor.StakerInfo, |
| 101 | + stakingInput: vendor.StakingInputs, |
| 102 | + babylonBtcTipHeight: number, |
| 103 | + inputUTXOs: vendor.UTXO[], |
| 104 | + feeRateSatB: number, |
| 105 | + babylonAddress: string |
| 106 | +): Promise<Result> { |
| 107 | + if (babylonBtcTipHeight === 0) { |
| 108 | + throw new Error('Babylon BTC tip height cannot be 0'); |
| 109 | + } |
| 110 | + if (inputUTXOs.length === 0) { |
| 111 | + throw new Error('No input UTXOs provided'); |
| 112 | + } |
| 113 | + if (!vendor.isValidBabylonAddress(babylonAddress)) { |
| 114 | + throw new Error('Invalid Babylon address'); |
| 115 | + } |
| 116 | + |
| 117 | + // Get the Babylon params based on the BTC tip height from Babylon chain |
| 118 | + const params = vendor.getBabylonParamByBtcHeight(babylonBtcTipHeight, stakingParams); |
| 119 | + |
| 120 | + const staking = new vendor.Staking( |
| 121 | + network, |
| 122 | + stakerBtcInfo, |
| 123 | + params, |
| 124 | + stakingInput.finalityProviderPkNoCoordHex, |
| 125 | + stakingInput.stakingTimelock |
| 126 | + ); |
| 127 | + |
| 128 | + // Create unsigned staking transaction |
| 129 | + const { transaction } = staking.createStakingTransaction(stakingInput.stakingAmountSat, inputUTXOs, feeRateSatB); |
| 130 | + |
| 131 | + // Create delegation message without including inclusion proof |
| 132 | + const msg = await manager.createBtcDelegationMsg( |
| 133 | + staking, |
| 134 | + stakingInput, |
| 135 | + transaction, |
| 136 | + babylonAddress, |
| 137 | + stakerBtcInfo, |
| 138 | + params |
| 139 | + ); |
| 140 | + return { |
| 141 | + unsignedDelegationMsg: msg, |
| 142 | + stakingTx: transaction, |
| 143 | + }; |
| 144 | +} |
| 145 | + |
| 146 | +export async function createUnsignedPreStakeRegistrationBabylonTransactionWithBtcProvider( |
| 147 | + btcProvider: vendor.BtcProvider, |
| 148 | + stakingParams: vendor.VersionedStakingParams, |
| 149 | + network: bitcoinjslib.Network, |
| 150 | + stakerBtcInfo: vendor.StakerInfo, |
| 151 | + stakingInput: vendor.StakingInputs, |
| 152 | + babylonBtcTipHeight: number, |
| 153 | + inputUTXOs: vendor.UTXO[], |
| 154 | + feeRateSatB: number, |
| 155 | + babylonAddress: string |
| 156 | +): Promise<Result> { |
| 157 | + const manager = new vendor.BabylonBtcStakingManager(network, [stakingParams], btcProvider, mockBabylonProvider); |
| 158 | + return await createUnsignedPreStakeRegistrationBabylonTransaction( |
| 159 | + manager, |
| 160 | + [stakingParams], |
| 161 | + network, |
| 162 | + stakerBtcInfo, |
| 163 | + stakingInput, |
| 164 | + babylonBtcTipHeight, |
| 165 | + inputUTXOs, |
| 166 | + feeRateSatB, |
| 167 | + babylonAddress |
| 168 | + ); |
| 169 | +} |
0 commit comments