Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Complete utxo keeper and btc withdrawal #79

Merged
merged 8 commits into from
Jun 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions cmd/sided/cmd/config.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cmd

import (
"github.com/btcsuite/btcd/chaincfg"
sdk "github.com/cosmos/cosmos-sdk/types"
)

Expand All @@ -19,7 +18,7 @@ func initSDKConfig() {
config.SetBech32PrefixForAccount(bech32Prefix, accountPubKeyPrefix)
config.SetBech32PrefixForValidator(validatorAddressPrefix, validatorPubKeyPrefix)
config.SetBech32PrefixForConsensusNode(consNodeAddressPrefix, consNodePubKeyPrefix)
config.SetBtcChainCfg(&chaincfg.SigNetParams)
// config.SetBtcChainCfg(&chaincfg.SigNetParams)

config.Seal()
}
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ require (
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 // indirect
github.com/ChainSafe/go-schnorrkel v1.0.0 // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/aead/siphash v1.0.1 // indirect
github.com/armon/go-metrics v0.4.1 // indirect
github.com/aws/aws-sdk-go v1.44.203 // indirect
github.com/beorn7/perks v1.0.1 // indirect
Expand Down Expand Up @@ -128,6 +129,7 @@ require (
github.com/jhump/protoreflect v1.15.6 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/jmhodges/levigo v1.0.0 // indirect
github.com/kkdai/bstream v0.0.0-20161212061736-f391b8402d23 // indirect
github.com/klauspost/compress v1.16.7 // indirect
github.com/klauspost/pgzip v1.2.5 // indirect
github.com/kr/pretty v0.3.1 // indirect
Expand Down Expand Up @@ -171,6 +173,7 @@ require (
github.com/spf13/afero v1.9.5 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/viper v1.16.0 // indirect
github.com/stretchr/objx v0.5.0 // indirect
github.com/subosito/gotenv v1.4.2 // indirect
github.com/syndtr/goleveldb v1.0.1-0.20220721030215-126854af5e6d // indirect
github.com/tendermint/go-amino v0.16.0 // indirect
Expand Down Expand Up @@ -215,6 +218,7 @@ require (
github.com/Stride-Labs/stride/v16 v16.0.0
github.com/btcsuite/btcd v0.24.1-0.20240318151728-2fc99e0496d2
github.com/btcsuite/btcd/btcutil v1.1.5
github.com/btcsuite/btcd/btcutil/psbt v1.1.9
github.com/btcsuite/btcd/chaincfg/chainhash v1.1.0
github.com/bufbuild/buf v1.7.0
github.com/cosmos/cosmos-proto v1.0.0-beta.4
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ github.com/Stride-Labs/stride/v16 v16.0.0/go.mod h1:0I+YGZ6EiuN8XCbeg+KHpo+gge+R
github.com/VividCortex/gohistogram v1.0.0 h1:6+hBz+qvs0JOrrNhhmR7lFxo5sINxBCGXrdtl/UvroE=
github.com/VividCortex/gohistogram v1.0.0/go.mod h1:Pf5mBqqDxYaXu3hDrrU+w6nw50o/4+TcAqDqk/vUH7g=
github.com/adlio/schema v1.3.3 h1:oBJn8I02PyTB466pZO1UZEn1TV5XLlifBSyMrmHl/1I=
github.com/aead/siphash v1.0.1 h1:FwHfE/T45KPKYuuSAKyyvE+oPWcaQ+CUmFW0bPlM+kg=
github.com/aead/siphash v1.0.1/go.mod h1:Nywa3cDsYNNK3gaciGTWPwHt0wlpNV15vwmswBAUSII=
github.com/afex/hystrix-go v0.0.0-20180502004556-fa1af6a1f4f5/go.mod h1:SkGFH1ia65gfNATL8TAiHDNxPzPdmEL5uirI2Uyuz6c=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
Expand Down Expand Up @@ -281,6 +282,8 @@ github.com/btcsuite/btcd/btcutil v1.0.0/go.mod h1:Uoxwv0pqYWhD//tfTiipkxNfdhG9Ur
github.com/btcsuite/btcd/btcutil v1.1.0/go.mod h1:5OapHB7A2hBBWLm48mmw4MOHNJCcUBTwmWH/0Jn8VHE=
github.com/btcsuite/btcd/btcutil v1.1.5 h1:+wER79R5670vs/ZusMTF1yTcRYE5GUsFbdjdisflzM8=
github.com/btcsuite/btcd/btcutil v1.1.5/go.mod h1:PSZZ4UitpLBWzxGd5VGOrLnmOjtPP/a6HaFo12zMs00=
github.com/btcsuite/btcd/btcutil/psbt v1.1.9 h1:UmfOIiWMZcVMOLaN+lxbbLSuoINGS1WmK1TZNI0b4yk=
github.com/btcsuite/btcd/btcutil/psbt v1.1.9/go.mod h1:ehBEvU91lxSlXtA+zZz3iFYx7Yq9eqnKx4/kSrnsvMY=
github.com/btcsuite/btcd/chaincfg/chainhash v1.0.0/go.mod h1:7SFka0XMvUgj3hfZtydOrQY2mwhPclbT2snogU7SQQc=
github.com/btcsuite/btcd/chaincfg/chainhash v1.0.1/go.mod h1:7SFka0XMvUgj3hfZtydOrQY2mwhPclbT2snogU7SQQc=
github.com/btcsuite/btcd/chaincfg/chainhash v1.1.0 h1:59Kx4K6lzOW5w6nFlA0v5+lk/6sjybR934QNHSJZPTQ=
Expand Down Expand Up @@ -754,6 +757,7 @@ github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7V
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/kkdai/bstream v0.0.0-20161212061736-f391b8402d23 h1:FOOIBWrEkLgmlgGfMuZT83xIwfPDxEI2OHu6xUmJMFE=
github.com/kkdai/bstream v0.0.0-20161212061736-f391b8402d23/go.mod h1:J+Gs4SYgM6CZQHDETBtE9HaSEkGmuNXF86RwHhHUvq4=
github.com/klauspost/compress v1.10.3/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs=
github.com/klauspost/compress v1.11.7/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs=
Expand Down
2 changes: 2 additions & 0 deletions proto/side/btcbridge/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ message MsgWithdrawBitcoinRequest {
string sender = 1;
// withdraw amount in satoshi, etc: 100000000sat = 1btc
string amount = 2;
// fee rate in sats/vB
int64 fee_rate = 3;
}

// MsgWithdrawBitcoinResponse defines the Msg/WithdrawBitcoin response type.
Expand Down
36 changes: 22 additions & 14 deletions x/btcbridge/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"bytes"
"encoding/base64"
"fmt"
"slices"

Check failure on line 7 in x/btcbridge/keeper/keeper.go

View workflow job for this annotation

GitHub Actions / tests (01)

github.com/cosmos/[email protected] (replaced by ../cosmos-sdk): reading ../cosmos-sdk/go.mod: open /home/runner/work/side/cosmos-sdk/go.mod: no such file or directory

"github.com/btcsuite/btcd/blockchain"
"github.com/btcsuite/btcd/btcutil"
Expand All @@ -13,14 +13,16 @@
"github.com/btcsuite/btcd/txscript"
"github.com/btcsuite/btcd/wire"
"github.com/cometbft/cometbft/libs/log"
"github.com/cosmos/cosmos-sdk/codec"

Check failure on line 16 in x/btcbridge/keeper/keeper.go

View workflow job for this annotation

GitHub Actions / tests (01)

github.com/cosmos/[email protected]: replacement directory ../cosmos-sdk does not exist
storetypes "github.com/cosmos/cosmos-sdk/store/types"

Check failure on line 17 in x/btcbridge/keeper/keeper.go

View workflow job for this annotation

GitHub Actions / tests (01)

github.com/cosmos/[email protected]: replacement directory ../cosmos-sdk does not exist
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/sideprotocol/side/x/btcbridge/types"
)

type (
Keeper struct {
BaseUTXOKeeper

cdc codec.BinaryCodec
storeKey storetypes.StoreKey
memKey storetypes.StoreKey
Expand All @@ -37,10 +39,11 @@
bankKeeper types.BankKeeper,
) *Keeper {
return &Keeper{
cdc: cdc,
storeKey: storeKey,
memKey: memKey,
bankKeeper: bankKeeper,
cdc: cdc,
storeKey: storeKey,
memKey: memKey,
bankKeeper: bankKeeper,
BaseUTXOKeeper: *NewBaseUTXOKeeper(cdc, storeKey),
}
}

Expand Down Expand Up @@ -238,6 +241,7 @@
return types.ErrTransactionNotIncluded
}

// mint voucher token and save utxo if the receiver is a vault address
for i, out := range uTx.MsgTx().TxOut {
// check if the output is a valid address
pks, err := txscript.ParsePkScript(out.PkScript)
Expand All @@ -249,9 +253,8 @@
return err
}

// TODO remove the true
// check if the receiver is one of the voucher addresses
if true || slices.Contains(param.BtcVoucherAddress, addr.EncodeAddress()) {
if slices.Contains(param.BtcVoucherAddress, addr.EncodeAddress()) {
// mint the voucher token
coins := sdk.NewCoins(sdk.NewCoin(param.BtcVoucherDenom, sdk.NewInt(out.Value)))
senderAddr, err := sdk.AccAddressFromBech32(sender.EncodeAddress())
Expand All @@ -272,23 +275,28 @@
IsLocked: false,
}

println("save utxo", utxo.Txid, utxo.Vout)
k.SetUTXO(ctx, &utxo)
k.SetOwnerUTXO(ctx, &utxo)

ctx.Logger().Info("Minted Bitcoin Voucher", "index", i, "address", addr.EncodeAddress(), "amount", out.Value, "sender", sender.EncodeAddress(), "senderAddr", senderAddr.String(), "coins", coins.String())

}
}

// spend locked utxos if the sender is a vault address
if slices.Contains(param.BtcVoucherAddress, sender.EncodeAddress()) {
for _, in := range uTx.MsgTx().TxIn {
hash := in.PreviousOutPoint.Hash.String()
vout := in.PreviousOutPoint.Index

if k.IsUTXOLocked(ctx, hash, uint64(vout)) {
k.SpendUTXO(ctx, hash, uint64(vout))
}
}
}

return nil
}

func (k Keeper) SetUtxo(ctx sdk.Context, utxo types.UTXO) {
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshal(&utxo)
store.Set(types.BtcUtxoKey(utxo.Txid, utxo.Vout), bz)
}

func (k Keeper) GetBlockHeader(ctx sdk.Context, hash string) *types.BlockHeader {
store := ctx.KVStore(k.storeKey)
var blockHeader types.BlockHeader
Expand Down
31 changes: 25 additions & 6 deletions x/btcbridge/keeper/keeper_withdraw.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,35 @@ func (k Keeper) IncrementRequestSequence(ctx sdk.Context) uint64 {
// txBytes: the transaction bytes
// vault: the address of the vault, default is empty.
// If empty, the vault will be Bitcoin vault, otherwise it will be Ordinals or Runes vault
func (k Keeper) NewSigningRequest(ctx sdk.Context, sender string, coin sdk.Coin, vault string) *types.BitcoinSigningRequest {
func (k Keeper) NewSigningRequest(ctx sdk.Context, sender string, coin sdk.Coin, feeRate int64, vault string) (*types.BitcoinSigningRequest, error) {
if len(vault) == 0 {
// default to the first vault in the params for now
// TODO: select an appropriate vault according to the utxos
vault = k.GetParams(ctx).Senders[0]
}

utxos := k.GetOrderedUTXOsByAddr(ctx, vault)
if len(utxos) == 0 {
return nil, types.ErrInsufficientUTXOs
}

// create a new bitcoin transaction
// tx := wire.NewMsgTx(wire.TxVersion)
psbt, selectedUTXOs, err := types.BuildPsbt(utxos, sender, coin.Amount.Int64(), feeRate, vault)
if err != nil {
return nil, types.ErrFailToBuildTransaction
}

psbtB64, err := psbt.B64Encode()
if err != nil {
return nil, types.ErrFailToSerializePsbt
}

// outScript, err := txscript.PayToAddrScript(sender)
// lock the selected utxos
k.LockUTXOs(ctx, selectedUTXOs)

signingRequest := &types.BitcoinSigningRequest{
Address: sender,
Psbt: "",
Txid: psbt.UnsignedTx.TxHash().String(),
Psbt: psbtB64,
Status: types.SigningStatus_SIGNING_STATUS_CREATED,
Sequence: k.IncrementRequestSequence(ctx),
VaultAddress: vault,
Expand All @@ -47,7 +66,7 @@ func (k Keeper) NewSigningRequest(ctx sdk.Context, sender string, coin sdk.Coin,
bz := k.cdc.MustMarshal(signingRequest)
store.Set(types.BtcSigningRequestKey(signingRequest.Sequence), bz)

return signingRequest
return signingRequest, nil
}

// GetSigningRequest returns the signing request
Expand Down
5 changes: 4 additions & 1 deletion x/btcbridge/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,10 @@ func (m msgServer) WithdrawBitcoin(goCtx context.Context, msg *types.MsgWithdraw

m.bankKeeper.SendCoinsFromAccountToModule(ctx, sender, types.ModuleName, sdk.NewCoins(coin))

//request := types.BitcoinSigningRequest(sender, coin)
_, err = m.Keeper.NewSigningRequest(ctx, msg.Sender, coin, msg.FeeRate, "")
if err != nil {
return nil, err
}

// Emit events
m.EmitEvent(ctx, msg.Sender,
Expand Down
Loading
Loading