-
Notifications
You must be signed in to change notification settings - Fork 330
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
Generate a MsgWirePayForMessage
via CLI
#32
Changes from 3 commits
a48ec72
83e51ec
7adb975
39a8b9c
39b7c24
ae8bf20
de38b72
e15b95b
db5a91c
285c42b
e425744
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package cli | ||
|
||
import ( | ||
"encoding/hex" | ||
"errors" | ||
|
||
"github.com/cosmos/cosmos-sdk/client" | ||
"github.com/cosmos/cosmos-sdk/client/flags" | ||
"github.com/cosmos/cosmos-sdk/client/tx" | ||
"github.com/lazyledger/lazyledger-app/x/lazyledgerapp/types" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// CmdCreatePayForMessage returns a cobra command that uses the key ring backend | ||
// and locally running node to create and broadcast a new WirePayForMessage | ||
// transaction. | ||
func CmdCreatePayForMessage() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "payForMessage [hexNamespace] [hexMessage]", | ||
Short: "Creates a new WirePayForMessage", | ||
Args: cobra.ExactArgs(2), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
clientCtx, err := client.GetClientTxContext(cmd) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// get the account name | ||
accName := clientCtx.GetFromName() | ||
if accName == "" { | ||
return errors.New("no account name provided, please use the --from flag") | ||
} | ||
|
||
// get info on the key | ||
keyInfo, err := clientCtx.Keyring.Key(accName) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// decode the namespace | ||
namespace, err := hex.DecodeString(args[0]) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// decode the message | ||
message, err := hex.DecodeString(args[1]) | ||
if err != nil { | ||
return err | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For better UX, maybe you can wrap error and provide info what argument (namespace or message) is wrong? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice catch! You just saved someone some time 🙂 not all heroes wear capes 😉 resolved db5a91c |
||
} | ||
|
||
// create the PayForMessage | ||
pfmMsg, err := types.NewMsgWirePayForMessage( | ||
namespace, | ||
message, | ||
keyInfo.GetPubKey().Bytes(), | ||
&types.TransactionFee{}, // transaction fee is not yet used | ||
types.SquareSize, | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// sign the PayForMessage's ShareCommitments | ||
err = pfmMsg.SignShareCommitments(accName, clientCtx.Keyring) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// run message checks | ||
if err = pfmMsg.ValidateBasic(); err != nil { | ||
return err | ||
} | ||
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), pfmMsg) | ||
}, | ||
} | ||
|
||
flags.AddTxFlagsToCmd(cmd) | ||
|
||
return cmd | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,27 @@ | ||
package keeper | ||
|
||
import "github.com/lazyledger/lazyledger-app/x/lazyledgerapp/types" | ||
import ( | ||
"context" | ||
|
||
"github.com/lazyledger/lazyledger-app/x/lazyledgerapp/types" | ||
) | ||
|
||
var _ types.MsgServer = msgServer{} | ||
|
||
// MsgServer is the server API for Msg service. | ||
type MsgServer interface { | ||
// PayForMessage allows the user to post data to made be available. | ||
PayForMessage(context.Context, *types.MsgWirePayForMessage) (*types.MsgPayForMessageResponse, error) | ||
// PayForMessage allows the user to post data to made be available. | ||
SignedTransactionDataPayForMessage(context.Context, *types.SignedTransactionDataPayForMessage) (*types.SignedTransactionDataPayForMessageResponse, error) | ||
} | ||
|
||
Comment on lines
+11
to
+18
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the relation of this interface to the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What was the reason again to only have one endpoint here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. because each There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That makes sense! |
||
type msgServer struct { | ||
Keeper | ||
} | ||
|
||
// NewMsgServerImpl returns an implementation of the bank MsgServer interface | ||
// for the provided Keeper. | ||
func NewMsgServerImpl(keeper Keeper) types.MsgServer { | ||
func NewMsgServerImpl(keeper Keeper) MsgServer { | ||
return &msgServer{Keeper: keeper} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ import ( | |
"errors" | ||
fmt "fmt" | ||
|
||
"github.com/cosmos/cosmos-sdk/crypto/keyring" | ||
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/lazyledger/lazyledger-core/crypto/merkle" | ||
|
@@ -24,7 +25,50 @@ const ( | |
// MsgWirePayForMessage | ||
/////////////////////////////////////// | ||
|
||
var _ sdk.MsgRequest = &MsgWirePayForMessage{} | ||
var _ sdk.Msg = &MsgWirePayForMessage{} | ||
|
||
// NewMsgWirePayForMessage creates a new MsgWirePayForMessage by using the | ||
// namespace and message to generate share commitments for the provided square sizes | ||
// Note that the share commitments generated still need to be signed using the Sign | ||
// method | ||
func NewMsgWirePayForMessage(namespace, message, pubK []byte, fee *TransactionFee, sizes ...uint64) (*MsgWirePayForMessage, error) { | ||
out := &MsgWirePayForMessage{ | ||
Fee: fee, | ||
Nonce: 0, | ||
MessageNameSpaceId: namespace, | ||
MessageSize: uint64(len(message)), | ||
Message: message, | ||
MessageShareCommitment: make([]ShareCommitAndSignature, len(sizes)), | ||
PublicKey: pubK, | ||
} | ||
|
||
// generate the share commitments | ||
for i, size := range sizes { | ||
commit, err := CreateCommitment(size, namespace, message) | ||
if err != nil { | ||
return nil, err | ||
} | ||
out.MessageShareCommitment[i] = ShareCommitAndSignature{K: size, ShareCommitment: commit} | ||
} | ||
return out, nil | ||
} | ||
|
||
// SignShareCommitments use the provided Keyring to sign each of the share commits | ||
// generated during the creation of the MsgWirePayForMessage | ||
func (msg *MsgWirePayForMessage) SignShareCommitments(accName string, ring keyring.Keyring) error { | ||
for i, commit := range msg.MessageShareCommitment { | ||
bytesToSign, err := msg.GetCommitmentSignBytes(commit.K) | ||
if err != nil { | ||
return err | ||
} | ||
sig, _, err := ring.Sign(accName, bytesToSign) | ||
if err != nil { | ||
return err | ||
} | ||
msg.MessageShareCommitment[i].Signature = sig | ||
} | ||
return nil | ||
} | ||
Comment on lines
+59
to
+72
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Dope! Can we create at least a small unit test for this method? |
||
|
||
func (msg *MsgWirePayForMessage) Route() string { return RouterKey } | ||
|
||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Other Tx are in JSON encoding or hex, too? (I mean on the cli)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is a good question. I'm not sure. In other tx, each argument is parsed individually, and the only ones handling raw bytes are using either amino or the Bech32 address encoding. For example
lazyledger-appd tx decode [amino-byte-string] [flags]
uses the amino byte string, and all addresses are inputted as a Bech32 string. Should it use amino? Something else to handle bytes?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NOOOOOOO! please don't :-D
But seriously, let's go with hex bytes for now. Let's open an issue about the UX/UI of the CLI. I'm also not sure what would be best here. We can address this much later anyway (before mainnet). Probably the main usage won't be over the CLI but from other chains too.
And I find
payForMessage [hexNamespace] [hexMessage]
clear and simple enough.