Skip to content

Commit

Permalink
add utxo
Browse files Browse the repository at this point in the history
  • Loading branch information
liangping committed Jun 8, 2024
1 parent 40c0f4f commit d28422c
Show file tree
Hide file tree
Showing 6 changed files with 666 additions and 58 deletions.
22 changes: 18 additions & 4 deletions proto/side/btclightclient/bitcoin.proto
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,25 @@ enum SigningStatus {
// Bitcoin Signing Request
message BitcoinSigningRequest {
string address = 1;
string tx_bytes = 2;
SigningStatus status = 3;
uint64 sequence = 4;
string txid = 2;
string tx_bytes = 3;
SigningStatus status = 4;
uint64 sequence = 5;

// The vault address that the request is associated with
string vault_address = 5;
string vault_address = 6;
}

// Bitcoin UTXO
message UTXO {
string txid = 1;
uint64 vout = 2;
string address = 3;
uint64 amount = 4;
// height is used for calculating confirmations
uint64 height = 5;
bytes pub_key_script = 6;
bool is_coinbase = 7;
bool is_locked = 8;
}

24 changes: 22 additions & 2 deletions x/btclightclient/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ func (k Keeper) ProcessBitcoinDepositTransaction(ctx sdk.Context, msg *types.Msg
return types.ErrTransactionNotIncluded
}

for _, out := range uTx.MsgTx().TxOut {
for i, out := range uTx.MsgTx().TxOut {
// check if the output is a valid address
pks, err := txscript.ParsePkScript(out.PkScript)
if err != nil {
Expand All @@ -261,14 +261,34 @@ func (k Keeper) ProcessBitcoinDepositTransaction(ctx sdk.Context, msg *types.Msg
k.bankKeeper.MintCoins(ctx, types.ModuleName, coins)
k.bankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, senderAddr, coins)

ctx.Logger().Info("Voucher token minted", "address", senderAddr.String(), "amount", coins.String())
utxo := types.UTXO{
Txid: uTx.Hash().String(),
Vout: uint64(i),
Amount: uint64(out.Value),
PubKeyScript: out.PkScript,
Height: header.Height,
Address: addr.EncodeAddress(),
IsCoinbase: false,
IsLocked: false,
}

println("save utxo", utxo.Txid, utxo.Vout)

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

}

}

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
32 changes: 25 additions & 7 deletions x/btclightclient/keeper/keeper_withdraw.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,38 @@ func (k Keeper) IncrementRequestSequence(ctx sdk.Context) uint64 {
}

// New signing request
func (k Keeper) NewSigningRequest(ctx sdk.Context, sender string, txBytes string) *types.BitcoinSigningRequest {
// sender: the address of the sender
// 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 {

// create a new bitcoin transaction
// tx := wire.NewMsgTx(wire.TxVersion)

// outScript, err := txscript.PayToAddrScript(sender)

signingRequest := &types.BitcoinSigningRequest{
Address: sender,
TxBytes: txBytes,
Status: types.SigningStatus_SIGNING_STATUS_CREATED,
Sequence: k.IncrementRequestSequence(ctx),
Address: sender,
TxBytes: "",
Status: types.SigningStatus_SIGNING_STATUS_CREATED,
Sequence: k.IncrementRequestSequence(ctx),
VaultAddress: vault,
}

store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshal(signingRequest)
store.Set(types.BtcSigningRequestKey(signingRequest.Sequence), bz)

return signingRequest
}

// GetSigningRequest returns the signing request
func (k Keeper) GetSigningRequest(ctx sdk.Context, hash string) *types.BitcoinSigningRequest {
store := ctx.KVStore(k.storeKey)
var signingRequest types.BitcoinSigningRequest
bz := store.Get(types.BtcBlockHeaderHashKey(hash))
// TODO replace the key with the hash
bz := store.Get(types.BtcSigningRequestKey(1))
k.cdc.MustUnmarshal(bz, &signingRequest)
return &signingRequest
}
Expand All @@ -47,7 +64,8 @@ func (k Keeper) GetSigningRequest(ctx sdk.Context, hash string) *types.BitcoinSi
func (k Keeper) SetSigningRequest(ctx sdk.Context, txHash string, signingRequest *types.BitcoinSigningRequest) {
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshal(signingRequest)
store.Set(types.BtcSigningRequestKey(txHash), bz)
// TODO replace the key with the hash
store.Set(types.BtcSigningRequestKey(1), bz)
}

// IterateSigningRequests iterates through all signing requests
Expand Down
29 changes: 29 additions & 0 deletions x/btclightclient/keeper/utxo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package keeper

import (
"cosmossdk.io/collections"
"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
)

type UTXOViewKeeper interface {
}

type UTXOKeeper interface {
UTXOViewKeeper
}

var _ UTXOKeeper = (*BaseUTXOKeeper)(nil)

type BaseUTXOViewKeeper struct {
UTXOs *collections.IndexedMap[collections.Pair[sdk.AccAddress, string], math.Int, BalancesIndexes]
}

func NewBaseUTXOViewKeeper() *BaseUTXOViewKeeper {
return &BaseUTXOViewKeeper{
UTXOs: &collections.NewIndexedMap(sb, types.BalancesPrefix, "utxos", collections.PairKeyCodec(sdk.AccAddressKey, collections.StringKey), types.BalanceValueCodec, newBalancesIndexes(sb)),
}
}

type BaseUTXOKeeper struct {
}
Loading

0 comments on commit d28422c

Please sign in to comment.