Skip to content

Commit eff7248

Browse files
[Backport] pop: move pop constructor functions to datagen/ (#508) (#510)
1 parent 2d85f28 commit eff7248

File tree

11 files changed

+144
-127
lines changed

11 files changed

+144
-127
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
3939

4040
### Improvements
4141

42+
- [#508](https://github.com/babylonlabs-io/babylon/pull/508) Move PoP constructor functiosn to datagen/
4243
- [#499](https://github.com/babylonlabs-io/babylon/pull/499) Add `params-by-version` CLI command
4344

4445
### Bug fixes

test/e2e/btc_staking_e2e_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ func (s *BTCStakingTestSuite) Test6MultisigBTCDelegation() {
410410

411411
// NOTE: we use the multisig address for the BTC delegation
412412
multisigStakerAddr := sdk.MustAccAddressFromBech32(multisigAddr)
413-
pop, err := bstypes.NewPoPBTC(multisigStakerAddr, s.delBTCSK)
413+
pop, err := datagen.NewPoPBTC(multisigStakerAddr, s.delBTCSK)
414414
s.NoError(err)
415415

416416
// generate staking tx and slashing tx
@@ -480,7 +480,7 @@ func (s *BTCStakingTestSuite) Test7BTCDelegationFeeGrant() {
480480
unbondingTime := btcStkParams.UnbondingTimeBlocks
481481

482482
// NOTE: we use the grantee staker address for the BTC delegation PoP
483-
pop, err := bstypes.NewPoPBTC(granteeStakerAddr, s.delBTCSK)
483+
pop, err := datagen.NewPoPBTC(granteeStakerAddr, s.delBTCSK)
484484
s.NoError(err)
485485

486486
// generate staking tx and slashing tx
@@ -571,7 +571,7 @@ func (s *BTCStakingTestSuite) Test8BTCDelegationFeeGrantTyped() {
571571
unbondingTime := btcStkParams.UnbondingTimeBlocks
572572

573573
// NOTE: we use the grantee staker address for the BTC delegation PoP
574-
pop, err := bstypes.NewPoPBTC(granteeStakerAddr, s.delBTCSK)
574+
pop, err := datagen.NewPoPBTC(granteeStakerAddr, s.delBTCSK)
575575
s.NoError(err)
576576

577577
// generate staking tx and slashing tx

test/e2e/btc_staking_pre_approval_e2e_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ func (s *BTCStakingPreApprovalTestSuite) Test1CreateFinalityProviderAndDelegatio
9696

9797
// NOTE: we use the node's address for the BTC delegation
9898
stakerAddr := sdk.MustAccAddressFromBech32(nonValidatorNode.PublicAddress)
99-
pop, err := bstypes.NewPoPBTC(stakerAddr, s.delBTCSK)
99+
pop, err := datagen.NewPoPBTC(stakerAddr, s.delBTCSK)
100100
s.NoError(err)
101101

102102
// generate staking tx and slashing tx

test/e2e/configurer/chain/commands_btcstaking.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ func (n *NodeConfig) CreateBTCDelegationAndCheck(
406406

407407
// NOTE: we use the node's address for the BTC delegation
408408
del1Addr := sdk.MustAccAddressFromBech32(delAddr)
409-
popDel1, err := bstypes.NewPoPBTC(del1Addr, btcStakerSK)
409+
popDel1, err := datagen.NewPoPBTC(del1Addr, btcStakerSK)
410410
require.NoError(t, err)
411411

412412
testStakingInfo, stakingTx, inclusionProof, testUnbondingInfo, delegatorSig := n.BTCStakingUnbondSlashInfo(r, t, btcNet, params, fp, btcStakerSK, stakingTimeBlocks, stakingSatAmt)

testutil/btcstaking-helper/keeper.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ func (h *Helper) CreateDelegationWithBtcBlockHeight(
293293
staker := sdk.MustAccAddressFromBech32(datagen.GenRandomAccount().Address)
294294

295295
// PoP
296-
pop, err := types.NewPoPBTC(staker, delSK)
296+
pop, err := datagen.NewPoPBTC(staker, delSK)
297297
h.NoError(err)
298298
// generate staking tx info
299299
prevBlock, _ := datagen.GenRandomBtcdBlock(r, 0, nil)

testutil/datagen/btcstaking.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func GenRandomFinalityProviderWithBTCBabylonSKs(
6969
btcPK := btcSK.PubKey()
7070
bip340PK := bbn.NewBIP340PubKeyFromBTCPK(btcPK)
7171
// pop
72-
pop, err := bstypes.NewPoPBTC(fpAddr, btcSK)
72+
pop, err := NewPoPBTC(fpAddr, btcSK)
7373
if err != nil {
7474
return nil, err
7575
}
@@ -168,7 +168,7 @@ func GenRandomBTCDelegation(
168168
require.NoError(t, err)
169169
w := uint16(100) // TODO: parameterise w
170170

171-
pop, err := bstypes.NewPoPBTC(sdk.MustAccAddressFromBech32(staker.Address), delSK)
171+
pop, err := NewPoPBTC(sdk.MustAccAddressFromBech32(staker.Address), delSK)
172172
require.NoError(t, err)
173173

174174
del := &bstypes.BTCDelegation{
@@ -346,7 +346,7 @@ func GenRandomMsgCreateBtcDelegationAndMsgAddCovenantSignatures(
346346
delSlashingTxSig, err := unbondingSlashingInfo.GenDelSlashingTxSig(delSK)
347347
require.NoError(t, err)
348348

349-
pop, err := bstypes.NewPoPBTC(sdk.MustAccAddressFromBech32(stakerAddr.String()), delSK)
349+
pop, err := NewPoPBTC(sdk.MustAccAddressFromBech32(stakerAddr.String()), delSK)
350350
require.NoError(t, err)
351351

352352
msg := &bstypes.MsgCreateBTCDelegation{

testutil/datagen/pop.go

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
package datagen
2+
3+
import (
4+
"encoding/hex"
5+
6+
"github.com/babylonlabs-io/babylon/crypto/bip322"
7+
"github.com/babylonlabs-io/babylon/crypto/ecdsa"
8+
bbn "github.com/babylonlabs-io/babylon/types"
9+
bstypes "github.com/babylonlabs-io/babylon/x/btcstaking/types"
10+
"github.com/btcsuite/btcd/btcec/v2"
11+
"github.com/btcsuite/btcd/btcec/v2/schnorr"
12+
"github.com/btcsuite/btcd/btcutil"
13+
"github.com/btcsuite/btcd/chaincfg"
14+
"github.com/cometbft/cometbft/crypto/tmhash"
15+
sdk "github.com/cosmos/cosmos-sdk/types"
16+
)
17+
18+
type bip322Sign[A btcutil.Address] func(sg []byte,
19+
privKey *btcec.PrivateKey,
20+
net *chaincfg.Params) (A, []byte, error)
21+
22+
// NewPoPBTC generates a new proof of possession that sk_BTC and the address are held by the same person
23+
// a proof of possession contains only one signature
24+
// - pop.BtcSig = schnorr_sign(sk_BTC, bbnAddress)
25+
func NewPoPBTC(addr sdk.AccAddress, btcSK *btcec.PrivateKey) (*bstypes.ProofOfPossessionBTC, error) {
26+
pop := bstypes.ProofOfPossessionBTC{
27+
BtcSigType: bstypes.BTCSigType_BIP340, // by default, we use BIP-340 encoding for BTC signature
28+
}
29+
30+
// generate pop.BtcSig = schnorr_sign(sk_BTC, hash(bbnAddress))
31+
// NOTE: *schnorr.Sign has to take the hash of the message.
32+
// So we have to hash the address before signing
33+
hash := tmhash.Sum(addr.Bytes())
34+
btcSig, err := schnorr.Sign(btcSK, hash)
35+
if err != nil {
36+
return nil, err
37+
}
38+
bip340Sig := bbn.NewBIP340SignatureFromBTCSig(btcSig)
39+
pop.BtcSig = bip340Sig.MustMarshal()
40+
41+
return &pop, nil
42+
}
43+
44+
// NewPoPWithECDSABTCSig generates a new proof of possession where Bitcoin signature is in ECDSA format
45+
// a proof of possession contains two signatures:
46+
// - pop.BtcSig = ecdsa_sign(sk_BTC, addr)
47+
func NewPoPBTCWithECDSABTCSig(addr sdk.AccAddress, btcSK *btcec.PrivateKey) (*bstypes.ProofOfPossessionBTC, error) {
48+
pop := bstypes.ProofOfPossessionBTC{
49+
BtcSigType: bstypes.BTCSigType_ECDSA,
50+
}
51+
52+
// generate pop.BtcSig = ecdsa_sign(sk_BTC, pop.BabylonSig)
53+
// NOTE: ecdsa.Sign has to take the message as string.
54+
// So we have to hex addr before signing
55+
addrHex := hex.EncodeToString(addr.Bytes())
56+
btcSig := ecdsa.Sign(btcSK, addrHex)
57+
pop.BtcSig = btcSig
58+
59+
return &pop, nil
60+
}
61+
62+
func newPoPBTCWithBIP322Sig[A btcutil.Address](
63+
addressToSign sdk.AccAddress,
64+
btcSK *btcec.PrivateKey,
65+
net *chaincfg.Params,
66+
bip322SignFn bip322Sign[A],
67+
) (*bstypes.ProofOfPossessionBTC, error) {
68+
pop := bstypes.ProofOfPossessionBTC{
69+
BtcSigType: bstypes.BTCSigType_BIP322,
70+
}
71+
72+
bip322SigEncoded, err := newBIP322Sig(tmhash.Sum(addressToSign.Bytes()), btcSK, net, bip322SignFn)
73+
if err != nil {
74+
return nil, err
75+
}
76+
pop.BtcSig = bip322SigEncoded
77+
78+
return &pop, nil
79+
}
80+
81+
func newBIP322Sig[A btcutil.Address](
82+
msgToSign []byte,
83+
btcSK *btcec.PrivateKey,
84+
net *chaincfg.Params,
85+
bip322SignFn bip322Sign[A],
86+
) ([]byte, error) {
87+
address, witnessSignture, err := bip322SignFn(
88+
msgToSign,
89+
btcSK,
90+
net,
91+
)
92+
if err != nil {
93+
return nil, err
94+
}
95+
96+
bip322Sig := bstypes.BIP322Sig{
97+
Address: address.EncodeAddress(),
98+
Sig: witnessSignture,
99+
}
100+
101+
return bip322Sig.Marshal()
102+
}
103+
104+
// NewPoPBTCWithBIP322P2WPKHSig creates a proof of possession of type BIP322
105+
// that signs the address with the BTC secret key.
106+
func NewPoPBTCWithBIP322P2WPKHSig(
107+
addr sdk.AccAddress,
108+
btcSK *btcec.PrivateKey,
109+
net *chaincfg.Params,
110+
) (*bstypes.ProofOfPossessionBTC, error) {
111+
return newPoPBTCWithBIP322Sig(addr, btcSK, net, bip322.SignWithP2WPKHAddress)
112+
}
113+
114+
func NewPoPBTCWithBIP322P2TRBIP86Sig(
115+
addrToSign sdk.AccAddress,
116+
btcSK *btcec.PrivateKey,
117+
net *chaincfg.Params,
118+
) (*bstypes.ProofOfPossessionBTC, error) {
119+
return newPoPBTCWithBIP322Sig(addrToSign, btcSK, net, bip322.SignWithP2TrSpendAddress)
120+
}

x/btcstaking/keeper/msg_server_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1007,7 +1007,7 @@ func TestDoNotAllowDelegationWithoutFinalityProvider(t *testing.T) {
10071007
stakerAddr := sdk.MustAccAddressFromBech32(acc.Address)
10081008

10091009
// PoP
1010-
pop, err := types.NewPoPBTC(stakerAddr, delSK)
1010+
pop, err := datagen.NewPoPBTC(stakerAddr, delSK)
10111011
require.NoError(t, err)
10121012
// generate staking tx info
10131013
prevBlock, _ := datagen.GenRandomBtcdBlock(r, 0, nil)

x/btcstaking/types/pop.go

Lines changed: 0 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -20,102 +20,6 @@ import (
2020

2121
type checkStakerKey func(stakerKey *bbn.BIP340PubKey) error
2222

23-
type bip322Sign[A btcutil.Address] func(sg []byte,
24-
privKey *btcec.PrivateKey,
25-
net *chaincfg.Params) (A, []byte, error)
26-
27-
// NewPoPBTC generates a new proof of possession that sk_BTC and the address are held by the same person
28-
// a proof of possession contains only one signature
29-
// - pop.BtcSig = schnorr_sign(sk_BTC, bbnAddress)
30-
func NewPoPBTC(addr sdk.AccAddress, btcSK *btcec.PrivateKey) (*ProofOfPossessionBTC, error) {
31-
pop := ProofOfPossessionBTC{
32-
BtcSigType: BTCSigType_BIP340, // by default, we use BIP-340 encoding for BTC signature
33-
}
34-
35-
// generate pop.BtcSig = schnorr_sign(sk_BTC, hash(bbnAddress))
36-
// NOTE: *schnorr.Sign has to take the hash of the message.
37-
// So we have to hash the address before signing
38-
hash := tmhash.Sum(addr.Bytes())
39-
btcSig, err := schnorr.Sign(btcSK, hash)
40-
if err != nil {
41-
return nil, err
42-
}
43-
bip340Sig := bbn.NewBIP340SignatureFromBTCSig(btcSig)
44-
pop.BtcSig = bip340Sig.MustMarshal()
45-
46-
return &pop, nil
47-
}
48-
49-
// NewPoPWithECDSABTCSig generates a new proof of possession where Bitcoin signature is in ECDSA format
50-
// a proof of possession contains two signatures:
51-
// - pop.BtcSig = ecdsa_sign(sk_BTC, addr)
52-
func NewPoPBTCWithECDSABTCSig(addr sdk.AccAddress, btcSK *btcec.PrivateKey) (*ProofOfPossessionBTC, error) {
53-
pop := ProofOfPossessionBTC{
54-
BtcSigType: BTCSigType_ECDSA,
55-
}
56-
57-
// generate pop.BtcSig = ecdsa_sign(sk_BTC, pop.BabylonSig)
58-
// NOTE: ecdsa.Sign has to take the message as string.
59-
// So we have to hex addr before signing
60-
addrHex := hex.EncodeToString(addr.Bytes())
61-
btcSig := ecdsa.Sign(btcSK, addrHex)
62-
pop.BtcSig = btcSig
63-
64-
return &pop, nil
65-
}
66-
67-
func newPoPBTCWithBIP322Sig[A btcutil.Address](
68-
addressToSign sdk.AccAddress,
69-
btcSK *btcec.PrivateKey,
70-
net *chaincfg.Params,
71-
bip322SignFn bip322Sign[A],
72-
) (*ProofOfPossessionBTC, error) {
73-
pop := ProofOfPossessionBTC{
74-
BtcSigType: BTCSigType_BIP322,
75-
}
76-
77-
bip322SigEncoded, err := newBIP322Sig(tmhash.Sum(addressToSign.Bytes()), btcSK, net, bip322SignFn)
78-
if err != nil {
79-
return nil, err
80-
}
81-
pop.BtcSig = bip322SigEncoded
82-
83-
return &pop, nil
84-
}
85-
86-
func newBIP322Sig[A btcutil.Address](
87-
msgToSign []byte,
88-
btcSK *btcec.PrivateKey,
89-
net *chaincfg.Params,
90-
bip322SignFn bip322Sign[A],
91-
) ([]byte, error) {
92-
address, witnessSignture, err := bip322SignFn(
93-
msgToSign,
94-
btcSK,
95-
net,
96-
)
97-
if err != nil {
98-
return nil, err
99-
}
100-
101-
bip322Sig := BIP322Sig{
102-
Address: address.EncodeAddress(),
103-
Sig: witnessSignture,
104-
}
105-
106-
return bip322Sig.Marshal()
107-
}
108-
109-
// NewPoPBTCWithBIP322P2WPKHSig creates a proof of possession of type BIP322
110-
// that signs the address with the BTC secret key.
111-
func NewPoPBTCWithBIP322P2WPKHSig(
112-
addr sdk.AccAddress,
113-
btcSK *btcec.PrivateKey,
114-
net *chaincfg.Params,
115-
) (*ProofOfPossessionBTC, error) {
116-
return newPoPBTCWithBIP322Sig(addr, btcSK, net, bip322.SignWithP2WPKHAddress)
117-
}
118-
11923
func NewPoPBTCFromHex(popHex string) (*ProofOfPossessionBTC, error) {
12024
popBytes, err := hex.DecodeString(popHex)
12125
if err != nil {
@@ -185,14 +89,6 @@ func (pop *ProofOfPossessionBTC) VerifyBIP340(stakerAddr sdk.AccAddress, bip340P
18589
return VerifyBIP340(pop.BtcSigType, pop.BtcSig, bip340PK, stakerAddr.Bytes())
18690
}
18791

188-
func NewPoPBTCWithBIP322P2TRBIP86Sig(
189-
addrToSign sdk.AccAddress,
190-
btcSK *btcec.PrivateKey,
191-
net *chaincfg.Params,
192-
) (*ProofOfPossessionBTC, error) {
193-
return newPoPBTCWithBIP322Sig(addrToSign, btcSK, net, bip322.SignWithP2TrSpendAddress)
194-
}
195-
19692
// isSupportedAddressAndWitness checks whether provided address and witness are
19793
// valid for proof of possession verification.
19894
// Currently the only supported options are:

0 commit comments

Comments
 (0)