in progress: wire -> json for now
This commit is contained in:
parent
820152b873
commit
25f01a3e12
|
@ -8,17 +8,15 @@ import (
|
|||
"github.com/cosmos/cosmos-sdk/client"
|
||||
"github.com/cosmos/cosmos-sdk/client/keys"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
wire "github.com/cosmos/cosmos-sdk/wire"
|
||||
wire "github.com/tendermint/go-amino"
|
||||
)
|
||||
|
||||
func buildTx(cdc *wire.Codec, msg sdk.Msg) ([]byte, error) {
|
||||
func buildTx(cdc *wire.Codec, msg sdk.Msg, name string) ([]byte, error) {
|
||||
keybase, err := keys.GetKeyBase()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
name := viper.GetString(client.FlagName)
|
||||
|
||||
bz := msg.GetSignBytes()
|
||||
buf := client.BufferStdin()
|
||||
prompt := fmt.Sprintf("Password to sign with '%s':", name)
|
||||
|
@ -45,13 +43,12 @@ func buildTx(cdc *wire.Codec, msg sdk.Msg) ([]byte, error) {
|
|||
return txBytes, nil
|
||||
}
|
||||
|
||||
func getAddress() []byte {
|
||||
func getAddress(name string) []byte {
|
||||
keybase, err := keys.GetKeyBase()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
name := viper.GetString(client.FlagName)
|
||||
info, err := keybase.Get(name)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
|
|
|
@ -10,7 +10,7 @@ import (
|
|||
"github.com/cosmos/cosmos-sdk/client"
|
||||
"github.com/cosmos/cosmos-sdk/client/builder"
|
||||
|
||||
wire "github.com/cosmos/cosmos-sdk/wire"
|
||||
wire "github.com/tendermint/go-amino"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/x/ibc"
|
||||
)
|
||||
|
@ -22,9 +22,7 @@ func IBCRelayCmd(cdc *wire.Codec) *cobra.Command {
|
|||
Use: "relay",
|
||||
Run: cmdr.runIBCRelay,
|
||||
}
|
||||
cmd.Flags().String(flagTo, "", "Address to send coins")
|
||||
cmd.Flags().String(flagAmount, "", "Amount of coins to send")
|
||||
cmd.Flags().Int64(flagSequence, 0, "Sequence number to sign the tx")
|
||||
cmd.Flags().String(client.FlagName, "", "Name of the key to sign")
|
||||
return cmd
|
||||
}
|
||||
|
||||
|
@ -64,13 +62,16 @@ func (c relayCommander) refine(bz []byte, sequence int64) []byte {
|
|||
if err := c.cdc.UnmarshalBinary(bz, &packet); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
address := getAddress()
|
||||
|
||||
name := viper.GetString(client.FlagName)
|
||||
|
||||
address := getAddress(name)
|
||||
msg := ibc.IBCReceiveMsg{
|
||||
IBCPacket: packet,
|
||||
Relayer: address,
|
||||
Sequence: sequence,
|
||||
}
|
||||
res, err := buildTx(c.cdc, msg)
|
||||
res, err := buildTx(c.cdc, msg, name)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
|
|
@ -0,0 +1,16 @@
|
|||
package commands
|
||||
|
||||
import (
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
wire "github.com/tendermint/go-amino"
|
||||
)
|
||||
|
||||
func AddCommands(cmd *cobra.Command) {
|
||||
cdc := wire.NewCodec()
|
||||
|
||||
cmd.AddCommand(
|
||||
IBCTransferCmd(cdc),
|
||||
IBCRelayCmd(cdc),
|
||||
)
|
||||
}
|
|
@ -11,7 +11,7 @@ import (
|
|||
"github.com/cosmos/cosmos-sdk/client/builder"
|
||||
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
wire "github.com/cosmos/cosmos-sdk/wire"
|
||||
// wire "github.com/tendermint/go-amino"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/x/ibc"
|
||||
)
|
||||
|
@ -20,12 +20,19 @@ func IBCTransferCmd(cdc *wire.Codec) *cobra.Command {
|
|||
cmdr := sendCommander{cdc}
|
||||
|
||||
cmd := &cobra.Command{
|
||||
Use: "send",
|
||||
Use: "transfer",
|
||||
RunE: cmdr.runIBCTransfer,
|
||||
}
|
||||
cmd.Flags().String(flagTo, "", "Address to send coins")
|
||||
cmd.Flags().String(flagAmount, "", "Amount of coins to send")
|
||||
cmd.Flags().String(flagChain, "", "Destination chain to send coins")
|
||||
viper.BindPFlag(flagTo, cmd.Flags().Lookup(flagTo))
|
||||
viper.BindPFlag(flagAmount, cmd.Flags().Lookup(flagAmount))
|
||||
viper.BindPFlag(flagChain, cmd.Flags().Lookup(flagChain))
|
||||
cmd.MarkFlagRequired(flagTo)
|
||||
cmd.MarkFlagRequired(flagAmount)
|
||||
cmd.MarkFlagRequired(flagChain)
|
||||
|
||||
return cmd
|
||||
}
|
||||
|
||||
|
@ -34,13 +41,15 @@ type sendCommander struct {
|
|||
}
|
||||
|
||||
func (c sendCommander) runIBCTransfer(cmd *cobra.Command, args []string) error {
|
||||
address := getAddress()
|
||||
keyname := viper.GetString(client.FlagName)
|
||||
|
||||
address := getAddress(keyname)
|
||||
msg, err := buildMsg(address)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
txBytes, err := buildTx(c.cdc, msg)
|
||||
txBytes, err := buildTx(c.cdc, msg, keyname)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -1,25 +1,25 @@
|
|||
package ibc
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
|
||||
wire "github.com/cosmos/cosmos-sdk/wire"
|
||||
//wire "github.com/cosmos/cosmos-sdk/wire"
|
||||
)
|
||||
|
||||
type IBCMapper struct {
|
||||
ibcKey sdk.StoreKey
|
||||
|
||||
cdc *wire.Codec
|
||||
// cdc *wire.Codec
|
||||
}
|
||||
|
||||
func NewIBCMapper(ibcKey sdk.StoreKey) IBCMapper {
|
||||
cdc := wire.NewCodec()
|
||||
// cdc := wire.NewCodec()
|
||||
|
||||
return IBCMapper{
|
||||
ibcKey: ibcKey,
|
||||
cdc: cdc,
|
||||
// cdc: cdc,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -38,7 +38,7 @@ func EgressLengthKey(destChain string) []byte {
|
|||
func (ibcm IBCMapper) getEgressLength(store sdk.KVStore, destChain string) int64 {
|
||||
bz := store.Get(EgressLengthKey(destChain))
|
||||
if bz == nil {
|
||||
zero, err := ibcm.cdc.MarshalBinary(int64(0))
|
||||
zero, err := json.Marshal(int64(0)) //ibcm.cdc.MarshalBinary(int64(0))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
@ -46,7 +46,7 @@ func (ibcm IBCMapper) getEgressLength(store sdk.KVStore, destChain string) int64
|
|||
return 0
|
||||
}
|
||||
var res int64
|
||||
if err := ibcm.cdc.UnmarshalBinary(bz, &res); err != nil {
|
||||
if err := json.Unmarshal(bz, &res); /*ibcm.cdc.UnmarshalBinary(bz, &res)*/ err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return res
|
||||
|
@ -56,7 +56,7 @@ func (ibcm IBCMapper) GetIngressSequence(ctx sdk.Context, srcChain string) int64
|
|||
store := ctx.KVStore(ibcm.ibcKey)
|
||||
bz := store.Get(IngressKey(srcChain))
|
||||
if bz == nil {
|
||||
zero, err := ibcm.cdc.MarshalBinary(int64(0))
|
||||
zero, err := json.Marshal(int64(0)) //ibcm.cdc.MarshalBinary(int64(0))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
@ -64,7 +64,7 @@ func (ibcm IBCMapper) GetIngressSequence(ctx sdk.Context, srcChain string) int64
|
|||
return 0
|
||||
}
|
||||
var res int64
|
||||
if err := ibcm.cdc.UnmarshalBinary(bz, &res); err != nil {
|
||||
if err := json.Unmarshal(bz, &res); /*ibcm.cdc.UnmarshalBinary(bz, &res)*/ err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return res
|
||||
|
@ -72,7 +72,7 @@ func (ibcm IBCMapper) GetIngressSequence(ctx sdk.Context, srcChain string) int64
|
|||
|
||||
func (ibcm IBCMapper) SetIngressSequence(ctx sdk.Context, srcChain string, sequence int64) {
|
||||
store := ctx.KVStore(ibcm.ibcKey)
|
||||
bz, err := ibcm.cdc.MarshalBinary(sequence)
|
||||
bz, err := json.Marshal(sequence) // ibcm.cdc.MarshalBinary(sequence)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
@ -82,12 +82,12 @@ func (ibcm IBCMapper) SetIngressSequence(ctx sdk.Context, srcChain string, seque
|
|||
func (ibcm IBCMapper) PushPacket(ctx sdk.Context, packet IBCPacket) {
|
||||
store := ctx.KVStore(ibcm.ibcKey)
|
||||
len := ibcm.getEgressLength(store, packet.DestChain)
|
||||
packetbz, err := ibcm.cdc.MarshalBinary(packet)
|
||||
packetbz, err := json.Marshal(packet) // ibcm.cdc.MarshalBinary(packet)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
store.Set(EgressKey(packet.DestChain, len), packetbz)
|
||||
lenbz, err := ibcm.cdc.MarshalBinary(int64(len + 1))
|
||||
lenbz, err := json.Marshal(int64(len + 1)) // ibcm.cdc.MarshalBinary(int64(len + 1))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
|
|
@ -1,9 +1,10 @@
|
|||
package ibc
|
||||
|
||||
import (
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
"encoding/json"
|
||||
|
||||
wire "github.com/cosmos/cosmos-sdk/wire"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
//wire "github.com/tendermint/go-amino"
|
||||
)
|
||||
|
||||
type IBCPacket struct {
|
||||
|
@ -14,16 +15,17 @@ type IBCPacket struct {
|
|||
DestChain string
|
||||
}
|
||||
|
||||
/*
|
||||
func newCodec() *wire.Codec {
|
||||
return wire.NewCodec()
|
||||
}
|
||||
|
||||
*/
|
||||
type IBCTransferMsg struct {
|
||||
IBCPacket
|
||||
}
|
||||
|
||||
func (msg IBCTransferMsg) Type() string {
|
||||
return "ibctransfer"
|
||||
return "ibc"
|
||||
}
|
||||
|
||||
func (msg IBCTransferMsg) Get(key interface{}) interface{} {
|
||||
|
@ -31,12 +33,18 @@ func (msg IBCTransferMsg) Get(key interface{}) interface{} {
|
|||
}
|
||||
|
||||
func (msg IBCTransferMsg) GetSignBytes() []byte {
|
||||
cdc := newCodec()
|
||||
bz, err := cdc.MarshalBinary(msg.IBCPacket)
|
||||
/* cdc := newCodec()
|
||||
bz, err := cdc.MarshalBinary(msg.IBCPacket)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return bz*/
|
||||
res, err := json.Marshal(msg)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return bz
|
||||
|
||||
return res
|
||||
}
|
||||
|
||||
func (msg IBCTransferMsg) ValidateBasic() sdk.Error {
|
||||
|
@ -55,7 +63,7 @@ type IBCReceiveMsg struct {
|
|||
}
|
||||
|
||||
func (msg IBCReceiveMsg) Type() string {
|
||||
return "ibcreceive"
|
||||
return "ibc"
|
||||
}
|
||||
|
||||
func (msg IBCReceiveMsg) Get(key interface{}) interface{} {
|
||||
|
@ -63,12 +71,18 @@ func (msg IBCReceiveMsg) Get(key interface{}) interface{} {
|
|||
}
|
||||
|
||||
func (msg IBCReceiveMsg) GetSignBytes() []byte {
|
||||
cdc := newCodec()
|
||||
/*cdc := newCodec()
|
||||
bz, err := cdc.MarshalBinary(msg.IBCPacket)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return bz
|
||||
return bz*/
|
||||
res, err := json.Marshal(msg)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return res
|
||||
}
|
||||
|
||||
func (msg IBCReceiveMsg) ValidateBasic() sdk.Error {
|
||||
|
|
|
@ -0,0 +1,11 @@
|
|||
package ibc
|
||||
|
||||
import (
|
||||
wire "github.com/tendermint/go-amino"
|
||||
)
|
||||
|
||||
func RegisterWire(cdc *wire.Codec) {
|
||||
cdc.RegisterConcrete(IBCTransferMsg{}, "cosmos-sdk/IBCTransferMsg", nil)
|
||||
cdc.RegisterConcrete(IBCReceiveMsg{}, "cosmos-sdk/IBCReceiveMsg", nil)
|
||||
cdc.RegisterConcrete(IBCPacket{}, "cosmos-sdk/IBCPacket", nil)
|
||||
}
|
Loading…
Reference in New Issue