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