|
1 | 1 | import * as bitcoinjslib from 'bitcoinjs-lib'; |
2 | 2 | import * as utxolib from '@bitgo/utxo-lib'; |
3 | 3 | import * as vendor from '@bitgo/babylonlabs-io-btc-staking-ts'; |
| 4 | +import { |
| 5 | + BIP322Sig, |
| 6 | + BTCSigType, |
| 7 | + ProofOfPossessionBTC, |
| 8 | +} from '@babylonlabs-io/babylon-proto-ts/dist/generated/babylon/btcstaking/v1/pop'; |
4 | 9 |
|
5 | 10 | import { getStakingParams } from './stakingParams'; |
6 | 11 |
|
| 12 | +/** |
| 13 | + * Subclass of BabylonBtcStakingManager with the sole purpose of forcing |
| 14 | + * a ECDSA signature. |
| 15 | + */ |
| 16 | +class BitGoStakingManager extends vendor.BabylonBtcStakingManager { |
| 17 | + constructor( |
| 18 | + network: bitcoinjslib.Network, |
| 19 | + stakingParams: vendor.VersionedStakingParams[], |
| 20 | + btcProvider: vendor.BtcProvider, |
| 21 | + babylonProvider: vendor.BabylonProvider |
| 22 | + ) { |
| 23 | + super(network, stakingParams, btcProvider, babylonProvider); |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * Creates a proof of possession for the staker based on ECDSA signature. |
| 28 | + * |
| 29 | + * This is a parameterized version of the superclass method which infers |
| 30 | + * the signature type from the stakerBtcAddress. |
| 31 | + * |
| 32 | + * @param bech32Address - The staker's bech32 address on the babylon network. |
| 33 | + * @param stakerBtcAddress - The staker's BTC address. |
| 34 | + * @param sigType - The signature type (BIP322 or ECDSA). |
| 35 | + * @returns The proof of possession. |
| 36 | + */ |
| 37 | + async createProofOfPossessionWithSigType( |
| 38 | + bech32Address: string, |
| 39 | + stakerBtcAddress: string, |
| 40 | + sigType: BTCSigType |
| 41 | + ): Promise<ProofOfPossessionBTC> { |
| 42 | + const signedBabylonAddress = await this.btcProvider.signMessage( |
| 43 | + vendor.SigningStep.PROOF_OF_POSSESSION, |
| 44 | + bech32Address, |
| 45 | + sigType === BTCSigType.BIP322 ? 'bip322-simple' : 'ecdsa' |
| 46 | + ); |
| 47 | + |
| 48 | + let btcSig: Uint8Array; |
| 49 | + if (sigType === BTCSigType.BIP322) { |
| 50 | + const bip322Sig = BIP322Sig.fromPartial({ |
| 51 | + address: stakerBtcAddress, |
| 52 | + sig: Buffer.from(signedBabylonAddress, 'base64'), |
| 53 | + }); |
| 54 | + // Encode the BIP322 protobuf message to a Uint8Array |
| 55 | + btcSig = BIP322Sig.encode(bip322Sig).finish(); |
| 56 | + } else { |
| 57 | + // Encode the ECDSA signature to a Uint8Array |
| 58 | + btcSig = Buffer.from(signedBabylonAddress, 'base64'); |
| 59 | + } |
| 60 | + |
| 61 | + return { |
| 62 | + btcSigType: sigType, |
| 63 | + btcSig, |
| 64 | + }; |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Creates a proof of possession for the staker based on ECDSA signature. |
| 69 | + * @param bech32Address - The staker's bech32 address on the babylon network. |
| 70 | + * @param stakerBtcAddress |
| 71 | + * @returns The proof of possession. |
| 72 | + */ |
| 73 | + async createProofOfPossession(bech32Address: string, stakerBtcAddress: string): Promise<ProofOfPossessionBTC> { |
| 74 | + // force the ECDSA signature type |
| 75 | + return this.createProofOfPossessionWithSigType(bech32Address, stakerBtcAddress, BTCSigType.ECDSA); |
| 76 | + } |
| 77 | +} |
| 78 | + |
7 | 79 | export const mockBabylonProvider: vendor.BabylonProvider = { |
8 | 80 | signTransaction(): Promise<Uint8Array> { |
9 | 81 | throw new Error('Function not implemented.'); |
@@ -31,10 +103,5 @@ export function createStakingManager( |
31 | 103 | throw new Error('Unsupported network'); |
32 | 104 | } |
33 | 105 | } |
34 | | - return new vendor.BabylonBtcStakingManager( |
35 | | - network, |
36 | | - stakingParams ?? getStakingParams(network), |
37 | | - btcProvider, |
38 | | - babylonProvider |
39 | | - ); |
| 106 | + return new BitGoStakingManager(network, stakingParams ?? getStakingParams(network), btcProvider, babylonProvider); |
40 | 107 | } |
0 commit comments