diff --git a/proto/side/btclightclient/bitcoin.proto b/proto/side/btclightclient/bitcoin.proto index 9a532863..9c38d152 100644 --- a/proto/side/btclightclient/bitcoin.proto +++ b/proto/side/btclightclient/bitcoin.proto @@ -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; } diff --git a/x/btclightclient/keeper/keeper.go b/x/btclightclient/keeper/keeper.go index 2581c3c2..03f6e128 100644 --- a/x/btclightclient/keeper/keeper.go +++ b/x/btclightclient/keeper/keeper.go @@ -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 { @@ -261,7 +261,21 @@ 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()) + } } @@ -269,6 +283,12 @@ func (k Keeper) ProcessBitcoinDepositTransaction(ctx sdk.Context, msg *types.Msg 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 diff --git a/x/btclightclient/keeper/keeper_withdraw.go b/x/btclightclient/keeper/keeper_withdraw.go index ad515900..eaa3ff88 100644 --- a/x/btclightclient/keeper/keeper_withdraw.go +++ b/x/btclightclient/keeper/keeper_withdraw.go @@ -24,13 +24,29 @@ 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 } @@ -38,7 +54,8 @@ func (k Keeper) NewSigningRequest(ctx sdk.Context, sender string, txBytes string 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 } @@ -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 diff --git a/x/btclightclient/keeper/utxo.go b/x/btclightclient/keeper/utxo.go new file mode 100644 index 00000000..93cc78f3 --- /dev/null +++ b/x/btclightclient/keeper/utxo.go @@ -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 { +} diff --git a/x/btclightclient/types/bitcoin.pb.go b/x/btclightclient/types/bitcoin.pb.go index fd19d1da..247eceab 100644 --- a/x/btclightclient/types/bitcoin.pb.go +++ b/x/btclightclient/types/bitcoin.pb.go @@ -179,11 +179,12 @@ func (m *BlockHeader) GetNtx() uint64 { // Bitcoin Signing Request type BitcoinSigningRequest struct { Address string `protobuf:"bytes,1,opt,name=address,proto3" json:"address,omitempty"` - TxBytes string `protobuf:"bytes,2,opt,name=tx_bytes,json=txBytes,proto3" json:"tx_bytes,omitempty"` - Status SigningStatus `protobuf:"varint,3,opt,name=status,proto3,enum=side.btclightclient.SigningStatus" json:"status,omitempty"` - Sequence uint64 `protobuf:"varint,4,opt,name=sequence,proto3" json:"sequence,omitempty"` + Txid string `protobuf:"bytes,2,opt,name=txid,proto3" json:"txid,omitempty"` + TxBytes string `protobuf:"bytes,3,opt,name=tx_bytes,json=txBytes,proto3" json:"tx_bytes,omitempty"` + Status SigningStatus `protobuf:"varint,4,opt,name=status,proto3,enum=side.btclightclient.SigningStatus" json:"status,omitempty"` + Sequence uint64 `protobuf:"varint,5,opt,name=sequence,proto3" json:"sequence,omitempty"` // The vault address that the request is associated with - VaultAddress string `protobuf:"bytes,5,opt,name=vault_address,json=vaultAddress,proto3" json:"vault_address,omitempty"` + VaultAddress string `protobuf:"bytes,6,opt,name=vault_address,json=vaultAddress,proto3" json:"vault_address,omitempty"` } func (m *BitcoinSigningRequest) Reset() { *m = BitcoinSigningRequest{} } @@ -226,6 +227,13 @@ func (m *BitcoinSigningRequest) GetAddress() string { return "" } +func (m *BitcoinSigningRequest) GetTxid() string { + if m != nil { + return m.Txid + } + return "" +} + func (m *BitcoinSigningRequest) GetTxBytes() string { if m != nil { return m.TxBytes @@ -254,48 +262,159 @@ func (m *BitcoinSigningRequest) GetVaultAddress() string { return "" } +// Bitcoin UTXO +type UTXO struct { + Txid string `protobuf:"bytes,1,opt,name=txid,proto3" json:"txid,omitempty"` + Vout uint64 `protobuf:"varint,2,opt,name=vout,proto3" json:"vout,omitempty"` + Address string `protobuf:"bytes,3,opt,name=address,proto3" json:"address,omitempty"` + Amount uint64 `protobuf:"varint,4,opt,name=amount,proto3" json:"amount,omitempty"` + // height is used for calculating confirmations + Height uint64 `protobuf:"varint,5,opt,name=height,proto3" json:"height,omitempty"` + PubKeyScript []byte `protobuf:"bytes,6,opt,name=pub_key_script,json=pubKeyScript,proto3" json:"pub_key_script,omitempty"` + IsCoinbase bool `protobuf:"varint,7,opt,name=is_coinbase,json=isCoinbase,proto3" json:"is_coinbase,omitempty"` + IsLocked bool `protobuf:"varint,8,opt,name=is_locked,json=isLocked,proto3" json:"is_locked,omitempty"` +} + +func (m *UTXO) Reset() { *m = UTXO{} } +func (m *UTXO) String() string { return proto.CompactTextString(m) } +func (*UTXO) ProtoMessage() {} +func (*UTXO) Descriptor() ([]byte, []int) { + return fileDescriptor_d7dee3af17ecec77, []int{2} +} +func (m *UTXO) XXX_Unmarshal(b []byte) error { + return m.Unmarshal(b) +} +func (m *UTXO) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_UTXO.Marshal(b, m, deterministic) + } else { + b = b[:cap(b)] + n, err := m.MarshalToSizedBuffer(b) + if err != nil { + return nil, err + } + return b[:n], nil + } +} +func (m *UTXO) XXX_Merge(src proto.Message) { + xxx_messageInfo_UTXO.Merge(m, src) +} +func (m *UTXO) XXX_Size() int { + return m.Size() +} +func (m *UTXO) XXX_DiscardUnknown() { + xxx_messageInfo_UTXO.DiscardUnknown(m) +} + +var xxx_messageInfo_UTXO proto.InternalMessageInfo + +func (m *UTXO) GetTxid() string { + if m != nil { + return m.Txid + } + return "" +} + +func (m *UTXO) GetVout() uint64 { + if m != nil { + return m.Vout + } + return 0 +} + +func (m *UTXO) GetAddress() string { + if m != nil { + return m.Address + } + return "" +} + +func (m *UTXO) GetAmount() uint64 { + if m != nil { + return m.Amount + } + return 0 +} + +func (m *UTXO) GetHeight() uint64 { + if m != nil { + return m.Height + } + return 0 +} + +func (m *UTXO) GetPubKeyScript() []byte { + if m != nil { + return m.PubKeyScript + } + return nil +} + +func (m *UTXO) GetIsCoinbase() bool { + if m != nil { + return m.IsCoinbase + } + return false +} + +func (m *UTXO) GetIsLocked() bool { + if m != nil { + return m.IsLocked + } + return false +} + func init() { proto.RegisterEnum("side.btclightclient.SigningStatus", SigningStatus_name, SigningStatus_value) proto.RegisterType((*BlockHeader)(nil), "side.btclightclient.BlockHeader") proto.RegisterType((*BitcoinSigningRequest)(nil), "side.btclightclient.BitcoinSigningRequest") + proto.RegisterType((*UTXO)(nil), "side.btclightclient.UTXO") } func init() { proto.RegisterFile("side/btclightclient/bitcoin.proto", fileDescriptor_d7dee3af17ecec77) } var fileDescriptor_d7dee3af17ecec77 = []byte{ - // 508 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x6c, 0x53, 0xcb, 0x6e, 0xd3, 0x40, - 0x14, 0xcd, 0x34, 0xef, 0x5b, 0x8a, 0xc2, 0xf4, 0xc1, 0x34, 0x20, 0x53, 0xc2, 0xa6, 0x62, 0xe1, - 0x48, 0x74, 0xc7, 0x2e, 0x0f, 0xb7, 0x04, 0xa9, 0x29, 0x1a, 0xa7, 0x1b, 0x36, 0x56, 0xec, 0x8c, - 0xec, 0x51, 0x1d, 0x4f, 0xf0, 0x8c, 0xa3, 0xf4, 0x2f, 0xf8, 0x25, 0x76, 0xb0, 0xeb, 0x92, 0x25, - 0x4a, 0x7e, 0x81, 0x0f, 0x40, 0x33, 0x4e, 0x90, 0x6a, 0x75, 0x13, 0xdd, 0x73, 0xef, 0xb9, 0xe7, - 0xdc, 0x1c, 0xdb, 0xf0, 0x56, 0xf2, 0x19, 0xeb, 0xfa, 0x2a, 0x88, 0x79, 0x18, 0xe9, 0x5f, 0x96, - 0xa8, 0xae, 0xcf, 0x55, 0x20, 0x78, 0x62, 0x2f, 0x52, 0xa1, 0x04, 0x3e, 0xd4, 0x14, 0xfb, 0x31, - 0xa5, 0x7d, 0x14, 0x8a, 0x50, 0x98, 0x79, 0x57, 0x57, 0x39, 0xb5, 0xf3, 0x17, 0xc1, 0x7e, 0x3f, - 0x16, 0xc1, 0xdd, 0x27, 0x36, 0x9d, 0xb1, 0x14, 0x13, 0xa8, 0x2f, 0x59, 0x2a, 0xb9, 0x48, 0x08, - 0x3a, 0x43, 0xe7, 0x15, 0xba, 0x83, 0x18, 0x43, 0x25, 0x9a, 0xca, 0x88, 0xec, 0x9d, 0xa1, 0xf3, - 0x26, 0x35, 0x35, 0x3e, 0x81, 0x5a, 0xc4, 0xb4, 0x07, 0x29, 0x1b, 0xf2, 0x16, 0x61, 0x1b, 0x0e, - 0x17, 0x29, 0x5b, 0x72, 0x91, 0x49, 0xcf, 0xd7, 0xea, 0x9e, 0x59, 0xad, 0x98, 0xd5, 0x17, 0xbb, - 0x51, 0xee, 0xab, 0x75, 0xde, 0xc0, 0xfe, 0x9c, 0xa5, 0x77, 0x31, 0xf3, 0x52, 0x21, 0x14, 0xa9, - 0x1a, 0x1e, 0xe4, 0x2d, 0x2a, 0x84, 0xc2, 0x47, 0x50, 0x4d, 0x44, 0x12, 0x30, 0x52, 0x33, 0x3e, - 0x39, 0xd0, 0x27, 0xf9, 0x5c, 0x49, 0x52, 0xcf, 0x4f, 0xd2, 0xb5, 0xee, 0x29, 0x3e, 0x67, 0xa4, - 0x61, 0x88, 0xa6, 0xc6, 0x2d, 0x28, 0x27, 0x6a, 0x45, 0x9a, 0xa6, 0xa5, 0xcb, 0xce, 0x2f, 0x04, - 0xc7, 0xfd, 0x3c, 0x33, 0x97, 0x87, 0x09, 0x4f, 0x42, 0xca, 0xbe, 0x65, 0x4c, 0x2a, 0x1d, 0xc0, - 0x74, 0x36, 0x4b, 0x99, 0x94, 0x26, 0x80, 0x26, 0xdd, 0x41, 0x7c, 0x0a, 0x0d, 0xb5, 0xf2, 0xfc, - 0x7b, 0xc5, 0xe4, 0x36, 0x84, 0xba, 0x5a, 0xf5, 0x35, 0xc4, 0x1f, 0xa1, 0x26, 0xd5, 0x54, 0x65, - 0xd2, 0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x50, 0xf7, 0x0b, 0x26, 0xfd, 0x9a, 0x61, 0x5d, 0xfc, 0x0b, - 0x00, 0x00, 0xff, 0xff, 0x2c, 0x90, 0x12, 0x79, 0x2c, 0x03, 0x00, 0x00, + // 627 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x6c, 0x53, 0x4d, 0x6f, 0xda, 0x4c, + 0x10, 0xc6, 0xc1, 0x10, 0x98, 0x7c, 0x88, 0x77, 0xf3, 0xf1, 0x3a, 0xa4, 0x72, 0x53, 0xda, 0x43, + 0xd4, 0x03, 0x48, 0xcd, 0xad, 0x37, 0x3e, 0x9c, 0x94, 0xb6, 0x21, 0xd5, 0x9a, 0x48, 0x55, 0x2f, + 0x96, 0x6d, 0x56, 0xb0, 0x0a, 0x78, 0x5d, 0xef, 0x1a, 0xc1, 0xbf, 0xe8, 0x5f, 0xea, 0xad, 0xc7, + 0x1c, 0x7b, 0x68, 0xa5, 0x2a, 0xf9, 0x0b, 0xfd, 0x01, 0xd5, 0x8e, 0x21, 0x0a, 0x28, 0x17, 0x6b, + 0x9e, 0x99, 0x67, 0x66, 0x9e, 0x99, 0xf1, 0xc2, 0x0b, 0xc9, 0x07, 0xac, 0x11, 0xa8, 0x70, 0xcc, + 0x87, 0x23, 0xfd, 0x65, 0x91, 0x6a, 0x04, 0x5c, 0x85, 0x82, 0x47, 0xf5, 0x38, 0x11, 0x4a, 0x90, + 0x3d, 0x4d, 0xa9, 0xaf, 0x52, 0xaa, 0xfb, 0x43, 0x31, 0x14, 0x18, 0x6f, 0x68, 0x2b, 0xa3, 0xd6, + 0xfe, 0x1a, 0xb0, 0xd5, 0x1a, 0x8b, 0xf0, 0xe6, 0x1d, 0xf3, 0x07, 0x2c, 0x21, 0x16, 0x6c, 0x4e, + 0x59, 0x22, 0xb9, 0x88, 0x2c, 0xe3, 0xc4, 0x38, 0x35, 0xe9, 0x12, 0x12, 0x02, 0xe6, 0xc8, 0x97, + 0x23, 0x6b, 0xe3, 0xc4, 0x38, 0x2d, 0x53, 0xb4, 0xc9, 0x21, 0x14, 0x47, 0x4c, 0xf7, 0xb0, 0xf2, + 0x48, 0x5e, 0x20, 0x52, 0x87, 0xbd, 0x38, 0x61, 0x53, 0x2e, 0x52, 0xe9, 0x05, 0xba, 0xba, 0x87, + 0xa9, 0x26, 0xa6, 0xfe, 0xb7, 0x0c, 0x65, 0x7d, 0x75, 0x9d, 0xe7, 0xb0, 0x35, 0x61, 0xc9, 0xcd, + 0x98, 0x79, 0x89, 0x10, 0xca, 0x2a, 0x20, 0x0f, 0x32, 0x17, 0x15, 0x42, 0x91, 0x7d, 0x28, 0x44, + 0x22, 0x0a, 0x99, 0x55, 0xc4, 0x3e, 0x19, 0xd0, 0x92, 0x02, 0xae, 0xa4, 0xb5, 0x99, 0x49, 0xd2, + 0xb6, 0xf6, 0x29, 0x3e, 0x61, 0x56, 0x09, 0x89, 0x68, 0x93, 0x0a, 0xe4, 0x23, 0x35, 0xb3, 0xca, + 0xe8, 0xd2, 0x66, 0xed, 0xb7, 0x01, 0x07, 0xad, 0x6c, 0x67, 0x2e, 0x1f, 0x46, 0x3c, 0x1a, 0x52, + 0xf6, 0x35, 0x65, 0x52, 0xe9, 0x05, 0xf8, 0x83, 0x41, 0xc2, 0xa4, 0xc4, 0x05, 0x94, 0xe9, 0x12, + 0x62, 0xe5, 0x19, 0x1f, 0x2c, 0x17, 0xa0, 0x6d, 0x72, 0x04, 0x25, 0x35, 0xf3, 0x82, 0xb9, 0x62, + 0x12, 0x57, 0x50, 0xa6, 0x9b, 0x6a, 0xd6, 0xd2, 0x90, 0xbc, 0x85, 0xa2, 0x54, 0xbe, 0x4a, 0x25, + 0x8e, 0xbd, 0xfb, 0xa6, 0x56, 0x7f, 0xe2, 0x2a, 0xf5, 0x45, 0x77, 0x17, 0x99, 0x74, 0x91, 0x41, + 0xaa, 0x50, 0x92, 0x5a, 0x8f, 0x9e, 0xb8,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 0x28, 0xdb, 0xa2, 0x57, 0xcd, + 0x4e, 0xbb, 0xe9, 0xea, 0xd4, 0x3c, 0x79, 0x06, 0xd6, 0x7a, 0xd9, 0xab, 0xde, 0x79, 0x97, 0x5e, + 0x3a, 0x9d, 0x8a, 0x49, 0x8e, 0xe1, 0xff, 0xb5, 0x28, 0x75, 0xde, 0x3b, 0x6d, 0x9d, 0x5a, 0x68, + 0x5d, 0xfe, 0xb8, 0xb3, 0x8d, 0xdb, 0x3b, 0xdb, 0xf8, 0x73, 0x67, 0x1b, 0xdf, 0xee, 0xed, 0xdc, + 0xed, 0xbd, 0x9d, 0xfb, 0x79, 0x6f, 0xe7, 0xbe, 0x9c, 0x0d, 0xb9, 0x1a, 0xa5, 0x41, 0x3d, 0x14, + 0x93, 0x86, 0xfe, 0x67, 0xf0, 0xa5, 0x86, 0x62, 0x8c, 0xa0, 0x31, 0x5b, 0x7f, 0xfb, 0x6a, 0x1e, + 0x33, 0x19, 0x14, 0x91, 0x75, 0xf6, 0x2f, 0x00, 0x00, 0xff, 0xff, 0xb1, 0xe6, 0x7c, 0xd1, 0x1f, + 0x04, 0x00, 0x00, } func (m *BlockHeader) Marshal() (dAtA []byte, err error) { @@ -399,23 +518,30 @@ func (m *BitcoinSigningRequest) MarshalToSizedBuffer(dAtA []byte) (int, error) { copy(dAtA[i:], m.VaultAddress) i = encodeVarintBitcoin(dAtA, i, uint64(len(m.VaultAddress))) i-- - dAtA[i] = 0x2a + dAtA[i] = 0x32 } if m.Sequence != 0 { i = encodeVarintBitcoin(dAtA, i, uint64(m.Sequence)) i-- - dAtA[i] = 0x20 + dAtA[i] = 0x28 } if m.Status != 0 { i = encodeVarintBitcoin(dAtA, i, uint64(m.Status)) i-- - dAtA[i] = 0x18 + dAtA[i] = 0x20 } if len(m.TxBytes) > 0 { i -= len(m.TxBytes) copy(dAtA[i:], m.TxBytes) i = encodeVarintBitcoin(dAtA, i, uint64(len(m.TxBytes))) i-- + dAtA[i] = 0x1a + } + if len(m.Txid) > 0 { + i -= len(m.Txid) + copy(dAtA[i:], m.Txid) + i = encodeVarintBitcoin(dAtA, i, uint64(len(m.Txid))) + i-- dAtA[i] = 0x12 } if len(m.Address) > 0 { @@ -428,6 +554,85 @@ func (m *BitcoinSigningRequest) MarshalToSizedBuffer(dAtA []byte) (int, error) { return len(dAtA) - i, nil } +func (m *UTXO) Marshal() (dAtA []byte, err error) { + size := m.Size() + dAtA = make([]byte, size) + n, err := m.MarshalToSizedBuffer(dAtA[:size]) + if err != nil { + return nil, err + } + return dAtA[:n], nil +} + +func (m *UTXO) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *UTXO) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + _ = i + var l int + _ = l + if m.IsLocked { + i-- + if m.IsLocked { + dAtA[i] = 1 + } else { + dAtA[i] = 0 + } + i-- + dAtA[i] = 0x40 + } + if m.IsCoinbase { + i-- + if m.IsCoinbase { + dAtA[i] = 1 + } else { + dAtA[i] = 0 + } + i-- + dAtA[i] = 0x38 + } + if len(m.PubKeyScript) > 0 { + i -= len(m.PubKeyScript) + copy(dAtA[i:], m.PubKeyScript) + i = encodeVarintBitcoin(dAtA, i, uint64(len(m.PubKeyScript))) + i-- + dAtA[i] = 0x32 + } + if m.Height != 0 { + i = encodeVarintBitcoin(dAtA, i, uint64(m.Height)) + i-- + dAtA[i] = 0x28 + } + if m.Amount != 0 { + i = encodeVarintBitcoin(dAtA, i, uint64(m.Amount)) + i-- + dAtA[i] = 0x20 + } + if len(m.Address) > 0 { + i -= len(m.Address) + copy(dAtA[i:], m.Address) + i = encodeVarintBitcoin(dAtA, i, uint64(len(m.Address))) + i-- + dAtA[i] = 0x1a + } + if m.Vout != 0 { + i = encodeVarintBitcoin(dAtA, i, uint64(m.Vout)) + i-- + dAtA[i] = 0x10 + } + if len(m.Txid) > 0 { + i -= len(m.Txid) + copy(dAtA[i:], m.Txid) + i = encodeVarintBitcoin(dAtA, i, uint64(len(m.Txid))) + i-- + dAtA[i] = 0xa + } + return len(dAtA) - i, nil +} + func encodeVarintBitcoin(dAtA []byte, offset int, v uint64) int { offset -= sovBitcoin(v) base := offset @@ -489,6 +694,10 @@ func (m *BitcoinSigningRequest) Size() (n int) { if l > 0 { n += 1 + l + sovBitcoin(uint64(l)) } + l = len(m.Txid) + if l > 0 { + n += 1 + l + sovBitcoin(uint64(l)) + } l = len(m.TxBytes) if l > 0 { n += 1 + l + sovBitcoin(uint64(l)) @@ -506,6 +715,42 @@ func (m *BitcoinSigningRequest) Size() (n int) { return n } +func (m *UTXO) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + l = len(m.Txid) + if l > 0 { + n += 1 + l + sovBitcoin(uint64(l)) + } + if m.Vout != 0 { + n += 1 + sovBitcoin(uint64(m.Vout)) + } + l = len(m.Address) + if l > 0 { + n += 1 + l + sovBitcoin(uint64(l)) + } + if m.Amount != 0 { + n += 1 + sovBitcoin(uint64(m.Amount)) + } + if m.Height != 0 { + n += 1 + sovBitcoin(uint64(m.Height)) + } + l = len(m.PubKeyScript) + if l > 0 { + n += 1 + l + sovBitcoin(uint64(l)) + } + if m.IsCoinbase { + n += 2 + } + if m.IsLocked { + n += 2 + } + return n +} + func sovBitcoin(x uint64) (n int) { return (math_bits.Len64(x|1) + 6) / 7 } @@ -847,6 +1092,38 @@ func (m *BitcoinSigningRequest) Unmarshal(dAtA []byte) error { m.Address = string(dAtA[iNdEx:postIndex]) iNdEx = postIndex case 2: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Txid", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowBitcoin + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + stringLen |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + intStringLen := int(stringLen) + if intStringLen < 0 { + return ErrInvalidLengthBitcoin + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLengthBitcoin + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.Txid = string(dAtA[iNdEx:postIndex]) + iNdEx = postIndex + case 3: if wireType != 2 { return fmt.Errorf("proto: wrong wireType = %d for field TxBytes", wireType) } @@ -878,7 +1155,7 @@ func (m *BitcoinSigningRequest) Unmarshal(dAtA []byte) error { } m.TxBytes = string(dAtA[iNdEx:postIndex]) iNdEx = postIndex - case 3: + case 4: if wireType != 0 { return fmt.Errorf("proto: wrong wireType = %d for field Status", wireType) } @@ -897,7 +1174,7 @@ func (m *BitcoinSigningRequest) Unmarshal(dAtA []byte) error { break } } - case 4: + case 5: if wireType != 0 { return fmt.Errorf("proto: wrong wireType = %d for field Sequence", wireType) } @@ -916,7 +1193,7 @@ func (m *BitcoinSigningRequest) Unmarshal(dAtA []byte) error { break } } - case 5: + case 6: if wireType != 2 { return fmt.Errorf("proto: wrong wireType = %d for field VaultAddress", wireType) } @@ -969,6 +1246,251 @@ func (m *BitcoinSigningRequest) Unmarshal(dAtA []byte) error { } return nil } +func (m *UTXO) Unmarshal(dAtA []byte) error { + l := len(dAtA) + iNdEx := 0 + for iNdEx < l { + preIndex := iNdEx + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowBitcoin + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + fieldNum := int32(wire >> 3) + wireType := int(wire & 0x7) + if wireType == 4 { + return fmt.Errorf("proto: UTXO: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: UTXO: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + case 1: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Txid", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowBitcoin + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + stringLen |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + intStringLen := int(stringLen) + if intStringLen < 0 { + return ErrInvalidLengthBitcoin + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLengthBitcoin + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.Txid = string(dAtA[iNdEx:postIndex]) + iNdEx = postIndex + case 2: + if wireType != 0 { + return fmt.Errorf("proto: wrong wireType = %d for field Vout", wireType) + } + m.Vout = 0 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowBitcoin + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + m.Vout |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + case 3: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Address", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowBitcoin + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + stringLen |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + intStringLen := int(stringLen) + if intStringLen < 0 { + return ErrInvalidLengthBitcoin + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLengthBitcoin + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.Address = string(dAtA[iNdEx:postIndex]) + iNdEx = postIndex + case 4: + if wireType != 0 { + return fmt.Errorf("proto: wrong wireType = %d for field Amount", wireType) + } + m.Amount = 0 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowBitcoin + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + m.Amount |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + case 5: + if wireType != 0 { + return fmt.Errorf("proto: wrong wireType = %d for field Height", wireType) + } + m.Height = 0 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowBitcoin + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + m.Height |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + case 6: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field PubKeyScript", wireType) + } + var byteLen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowBitcoin + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + byteLen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if byteLen < 0 { + return ErrInvalidLengthBitcoin + } + postIndex := iNdEx + byteLen + if postIndex < 0 { + return ErrInvalidLengthBitcoin + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.PubKeyScript = append(m.PubKeyScript[:0], dAtA[iNdEx:postIndex]...) + if m.PubKeyScript == nil { + m.PubKeyScript = []byte{} + } + iNdEx = postIndex + case 7: + if wireType != 0 { + return fmt.Errorf("proto: wrong wireType = %d for field IsCoinbase", wireType) + } + var v int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowBitcoin + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + v |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + m.IsCoinbase = bool(v != 0) + case 8: + if wireType != 0 { + return fmt.Errorf("proto: wrong wireType = %d for field IsLocked", wireType) + } + var v int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowBitcoin + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + v |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + m.IsLocked = bool(v != 0) + default: + iNdEx = preIndex + skippy, err := skipBitcoin(dAtA[iNdEx:]) + if err != nil { + return err + } + if (skippy < 0) || (iNdEx+skippy) < 0 { + return ErrInvalidLengthBitcoin + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + iNdEx += skippy + } + } + + if iNdEx > l { + return io.ErrUnexpectedEOF + } + return nil +} func skipBitcoin(dAtA []byte) (n int, err error) { l := len(dAtA) iNdEx := 0 diff --git a/x/btclightclient/types/keys.go b/x/btclightclient/types/keys.go index ef5fc549..eafb2abd 100644 --- a/x/btclightclient/types/keys.go +++ b/x/btclightclient/types/keys.go @@ -30,6 +30,7 @@ var ( BtcBlockHeaderHeightPrefix = []byte{0x12} // prefix for each key to a block hash, for a height BtcBestBlockHeaderKey = []byte{0x13} // key for the best block height BtcSigningRequestPrefix = []byte{0x14} // prefix for each key to a signing request + BtcUtxoKeyPrefix = []byte{0x15} // prefix for each key to a utxo ChainCfg = &chaincfg.MainNetParams ) @@ -40,6 +41,10 @@ func Int64ToBytes(number uint64) []byte { return big.Bytes() } +func BtcUtxoKey(hash string, vout uint64) []byte { + return append(BtcBlockHeaderHashPrefix, []byte(hash)...) +} + func BtcBlockHeaderHashKey(hash string) []byte { return append(BtcBlockHeaderHashPrefix, []byte(hash)...) } @@ -48,6 +53,6 @@ func BtcBlockHeaderHeightKey(height uint64) []byte { return append(BtcBlockHeaderHashPrefix, Int64ToBytes(height)...) } -func BtcSigningRequestKey(hash string) []byte { - return append(BtcBlockHeaderHashPrefix, []byte(hash)...) +func BtcSigningRequestKey(sequence uint64) []byte { + return append(BtcSigningRequestPrefix, Int64ToBytes(sequence)...) }