DRY code
This commit is contained in:
parent
0a79849d6e
commit
217cae2021
|
@ -1,11 +1,6 @@
|
||||||
package std
|
package std
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
|
|
||||||
jsonc "github.com/gibson042/canonicaljson-go"
|
|
||||||
"github.com/gogo/protobuf/jsonpb"
|
|
||||||
|
|
||||||
"github.com/cosmos/cosmos-sdk/client"
|
"github.com/cosmos/cosmos-sdk/client"
|
||||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||||
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
||||||
|
@ -165,21 +160,5 @@ func NewSignDoc(num, seq uint64, cid, memo string, fee auth.StdFee, msgs ...Mess
|
||||||
// names adhere to their proto definition, default values are omitted, and follows
|
// names adhere to their proto definition, default values are omitted, and follows
|
||||||
// the JSON Canonical Form.
|
// the JSON Canonical Form.
|
||||||
func (sd *SignDoc) CanonicalSignBytes() ([]byte, error) {
|
func (sd *SignDoc) CanonicalSignBytes() ([]byte, error) {
|
||||||
jm := &jsonpb.Marshaler{EmitDefaults: false, OrigName: false}
|
return sdk.CanonicalSignBytes(sd)
|
||||||
buf := new(bytes.Buffer)
|
|
||||||
|
|
||||||
// first, encode via canonical Protocol Buffer JSON
|
|
||||||
if err := jm.Marshal(buf, sd); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
genericJSON := make(map[string]interface{})
|
|
||||||
|
|
||||||
// decode canonical proto encoding into a generic map
|
|
||||||
if err := jsonc.Unmarshal(buf.Bytes(), &genericJSON); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// finally, return the canonical JSON encoding via JSON Canonical Form
|
|
||||||
return jsonc.Marshal(genericJSON)
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,11 @@
|
||||||
package types
|
package types
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
|
|
||||||
|
jsonc "github.com/gibson042/canonicaljson-go"
|
||||||
|
"github.com/gogo/protobuf/jsonpb"
|
||||||
|
|
||||||
"github.com/cosmos/cosmos-sdk/codec"
|
"github.com/cosmos/cosmos-sdk/codec"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -9,3 +14,25 @@ func RegisterCodec(cdc *codec.Codec) {
|
||||||
cdc.RegisterInterface((*Msg)(nil), nil)
|
cdc.RegisterInterface((*Msg)(nil), nil)
|
||||||
cdc.RegisterInterface((*Tx)(nil), nil)
|
cdc.RegisterInterface((*Tx)(nil), nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CanonicalSignBytes returns a canonical JSON encoding of a message that can be
|
||||||
|
// signed over.
|
||||||
|
func CanonicalSignBytes(m codec.ProtoMarshaler) ([]byte, error) {
|
||||||
|
jm := &jsonpb.Marshaler{EmitDefaults: false, OrigName: false}
|
||||||
|
buf := new(bytes.Buffer)
|
||||||
|
|
||||||
|
// first, encode via canonical Protocol Buffer JSON
|
||||||
|
if err := jm.Marshal(buf, m); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
genericJSON := make(map[string]interface{})
|
||||||
|
|
||||||
|
// decode canonical proto encoding into a generic map
|
||||||
|
if err := jsonc.Unmarshal(buf.Bytes(), &genericJSON); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// finally, return the canonical JSON encoding via JSON Canonical Form
|
||||||
|
return jsonc.Marshal(genericJSON)
|
||||||
|
}
|
||||||
|
|
|
@ -7,7 +7,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
// Msg defines the interface a transaction message must fulfill.
|
// Msg defines the interface a transaction message must fulfill.
|
||||||
Msg interface {
|
Msg interface {
|
||||||
|
|
||||||
// Return the message type.
|
// Return the message type.
|
||||||
|
@ -45,7 +45,7 @@ type (
|
||||||
GetSignature() []byte
|
GetSignature() []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tx defines the interface a transaction must fulfill.
|
// Tx defines the interface a transaction must fulfill.
|
||||||
Tx interface {
|
Tx interface {
|
||||||
// Gets the all the transaction's messages.
|
// Gets the all the transaction's messages.
|
||||||
GetMsgs() []Msg
|
GetMsgs() []Msg
|
||||||
|
@ -56,7 +56,6 @@ type (
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
// TxDecoder unmarshals transaction bytes
|
// TxDecoder unmarshals transaction bytes
|
||||||
type TxDecoder func(txBytes []byte) (Tx, error)
|
type TxDecoder func(txBytes []byte) (Tx, error)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue