2018-07-08 22:09:07 +00:00
package cli
import (
"fmt"
2018-09-01 20:41:40 +00:00
"io/ioutil"
2018-09-01 23:29:51 +00:00
"os"
2018-07-08 22:09:07 +00:00
2018-07-14 22:27:56 +00:00
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/viper"
2018-07-14 00:09:02 +00:00
"github.com/cosmos/cosmos-sdk/client/context"
2018-07-14 22:27:56 +00:00
"github.com/cosmos/cosmos-sdk/client/keys"
2018-07-14 00:09:02 +00:00
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/wire"
2018-07-15 14:08:08 +00:00
authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli"
2018-07-14 22:27:56 +00:00
2018-07-14 00:09:02 +00:00
"github.com/kava-labs/kava/internal/x/paychan"
2018-07-08 22:09:07 +00:00
)
// list of functions that return pointers to cobra commands
// No local storage needed for cli acting as a sender
2018-09-01 20:41:40 +00:00
func CreateChannelCmd ( cdc * wire . Codec ) * cobra . Command {
2018-07-14 00:09:02 +00:00
flagTo := "to"
2018-09-01 20:41:40 +00:00
flagCoins := "amount"
2018-07-14 00:09:02 +00:00
cmd := & cobra . Command {
Use : "create" ,
Short : "Create a new payment channel" ,
2018-09-01 20:41:40 +00:00
Long : "Create a new unidirectional payment channel from a local address to a remote address, funded with some amount of coins. These coins are removed from the sender account and put into the channel." ,
2018-07-14 00:09:02 +00:00
Args : cobra . NoArgs ,
2018-07-08 22:09:07 +00:00
RunE : func ( cmd * cobra . Command , args [ ] string ) error {
2018-07-14 22:27:56 +00:00
// Create a "client context" stuct populated with info from common flags
2018-07-15 14:08:08 +00:00
ctx := context . NewCoreContextFromViper ( ) . WithDecoder ( authcmd . GetAccountDecoder ( cdc ) )
2018-09-01 20:41:40 +00:00
// ctx.PrintResponse = true TODO is this needed for channelID
2018-07-08 22:09:07 +00:00
2018-07-14 00:09:02 +00:00
// Get sender adress
2018-09-01 20:41:40 +00:00
sender , err := ctx . GetFromAddress ( )
2018-07-14 00:09:02 +00:00
if err != nil {
return err
}
2018-07-08 22:09:07 +00:00
2018-07-14 00:09:02 +00:00
// Get receiver address
toStr := viper . GetString ( flagTo )
2018-09-01 23:29:51 +00:00
receiver , err := sdk . AccAddressFromBech32 ( toStr )
2018-07-08 22:09:07 +00:00
if err != nil {
return err
}
2018-07-14 00:09:02 +00:00
// Get channel funding amount
2018-09-01 20:41:40 +00:00
coinsString := viper . GetString ( flagCoins )
coins , err := sdk . ParseCoins ( coinsString )
2018-07-08 22:09:07 +00:00
if err != nil {
return err
}
2018-07-14 00:09:02 +00:00
// Create the create channel msg to send
msg := paychan . MsgCreate {
2018-09-01 23:29:51 +00:00
Participants : [ 2 ] sdk . AccAddress { sender , receiver } ,
2018-09-01 20:41:40 +00:00
Coins : coins ,
}
err = msg . ValidateBasic ( )
if err != nil {
return err
2018-07-08 22:09:07 +00:00
}
2018-09-01 20:41:40 +00:00
2018-07-14 22:27:56 +00:00
// Build and sign the transaction, then broadcast to the blockchain
2018-09-01 23:29:51 +00:00
err = ctx . EnsureSignBuildBroadcast ( ctx . FromAddressName , [ ] sdk . Msg { msg } , cdc )
2018-07-14 00:09:02 +00:00
if err != nil {
return err
}
2018-07-14 22:27:56 +00:00
return nil
2018-07-14 00:09:02 +00:00
} ,
}
2018-09-01 20:41:40 +00:00
cmd . Flags ( ) . String ( flagTo , "" , "Recipient address of the payment channel." )
2018-09-01 23:29:51 +00:00
cmd . Flags ( ) . String ( flagCoins , "" , "Amount of coins to fund the payment channel with." )
2018-07-14 00:09:02 +00:00
return cmd
}
2018-07-08 22:09:07 +00:00
2018-09-01 20:41:40 +00:00
func GeneratePaymentCmd ( cdc * wire . Codec ) * cobra . Command {
flagId := "id" // ChannelID
flagReceiverAmount := "r-amount" // amount the receiver should received on closing the channel
flagSenderAmount := "s-amount" //
2018-07-14 00:09:02 +00:00
cmd := & cobra . Command {
2018-09-01 20:41:40 +00:00
Use : "pay" ,
Short : "Generate a ." , // TODO descriptions
Long : "Generate a new " ,
2018-07-14 00:09:02 +00:00
Args : cobra . NoArgs ,
RunE : func ( cmd * cobra . Command , args [ ] string ) error {
2018-07-14 22:27:56 +00:00
// Create a "client context" stuct populated with info from common flags
2018-07-15 14:08:08 +00:00
ctx := context . NewCoreContextFromViper ( ) . WithDecoder ( authcmd . GetAccountDecoder ( cdc ) )
2018-09-01 20:41:40 +00:00
// ctx.PrintResponse = false TODO is this needed to stop any other output messing up json?
2018-07-14 22:27:56 +00:00
2018-07-14 00:09:02 +00:00
// Get sender adress
2018-09-01 20:41:40 +00:00
// senderAddress, err := ctx.GetFromAddress()
// if err != nil {
// return err
// }
2018-07-14 22:27:56 +00:00
// Get the paychan id
2018-09-01 23:29:51 +00:00
id := paychan . ChannelID ( viper . GetInt64 ( flagId ) ) // TODO make this default to pulling id from chain
2018-07-14 22:27:56 +00:00
2018-09-01 20:41:40 +00:00
// Get channel receiver amount
senderCoins , err := sdk . ParseCoins ( viper . GetString ( flagSenderAmount ) )
2018-07-14 00:09:02 +00:00
if err != nil {
return err
}
// Get channel receiver amount
2018-09-01 20:41:40 +00:00
receiverCoins , err := sdk . ParseCoins ( viper . GetString ( flagReceiverAmount ) )
2018-07-14 00:09:02 +00:00
if err != nil {
return err
}
2018-07-14 22:27:56 +00:00
// create close paychan msg
2018-09-01 20:41:40 +00:00
update := paychan . Update {
ChannelID : id ,
Payout : paychan . Payout { senderCoins , receiverCoins } ,
// empty sigs
}
// Sign the update as the sender
keybase , err := keys . GetKeyBase ( )
if err != nil {
return err
}
name := ctx . FromAddressName
passphrase , err := ctx . GetPassphraseFromStdin ( name )
if err != nil {
return err
2018-07-14 00:09:02 +00:00
}
2018-09-01 20:41:40 +00:00
bz := update . GetSignBytes ( )
2018-07-14 00:09:02 +00:00
2018-09-01 20:41:40 +00:00
sig , pubKey , err := keybase . Sign ( name , passphrase , bz )
2018-07-08 22:09:07 +00:00
if err != nil {
return err
}
2018-09-01 23:29:51 +00:00
update . Sigs = [ 1 ] paychan . UpdateSignature { {
2018-09-01 20:41:40 +00:00
PubKey : pubKey ,
CryptoSignature : sig ,
2018-09-01 23:29:51 +00:00
} }
2018-09-01 20:41:40 +00:00
// Print out the update
2018-09-01 23:29:51 +00:00
jsonUpdate , err := wire . MarshalJSONIndent ( cdc , update )
if err != nil {
return err
}
2018-09-01 20:41:40 +00:00
fmt . Println ( string ( jsonUpdate ) )
2018-07-14 00:09:02 +00:00
2018-07-14 22:27:56 +00:00
return nil
2018-07-14 00:09:02 +00:00
} ,
}
cmd . Flags ( ) . Int ( flagId , 0 , "ID of the payment channel." )
2018-09-01 20:41:40 +00:00
cmd . Flags ( ) . String ( flagSenderAmount , "" , "" )
cmd . Flags ( ) . String ( flagReceiverAmount , "" , "" )
2018-07-14 00:09:02 +00:00
return cmd
}
2018-09-01 23:29:51 +00:00
func VerifyPaymentCmd ( cdc * wire . Codec , paychanStoreName string ) * cobra . Command {
2018-09-01 20:41:40 +00:00
cmd := & cobra . Command {
Use : "verify" ,
Short : "" , // TODO
Long : "" ,
Args : cobra . NoArgs ,
RunE : func ( cmd * cobra . Command , args [ ] string ) error {
2018-09-01 23:29:51 +00:00
// Create a "client context" stuct populated with info from common flags
ctx := context . NewCoreContextFromViper ( )
2018-09-01 20:41:40 +00:00
// read in update
bz , err := ioutil . ReadAll ( os . Stdin )
if err != nil {
// TODO add nice message about how to feed in stdin
return err
}
// decode json
var update paychan . Update
cdc . UnmarshalJSON ( bz , & update )
2018-09-01 23:29:51 +00:00
// get the channel from the node
2018-09-01 20:41:40 +00:00
res , err := ctx . QueryStore ( paychan . GetChannelKey ( update . ChannelID ) , paychanStoreName )
if len ( res ) == 0 || err != nil {
return errors . Errorf ( "channel with ID '%d' does not exist" , update . ChannelID )
}
var channel paychan . Channel
cdc . MustUnmarshalBinary ( res , & channel )
//verify
2018-09-01 23:29:51 +00:00
updateIsOK := paychan . VerifyUpdate ( channel , update )
2018-09-01 20:41:40 +00:00
// print result
fmt . Println ( updateIsOK )
return nil
} ,
}
return cmd
}
2018-09-01 23:29:51 +00:00
func SubmitPaymentCmd ( cdc * wire . Codec ) * cobra . Command {
2018-09-01 20:41:40 +00:00
cmd := & cobra . Command {
Use : "submit" ,
Short : "" ,
Long : "" ,
Args : cobra . NoArgs ,
RunE : func ( cmd * cobra . Command , args [ ] string ) error {
// Create a "client context" stuct populated with info from common flags
ctx := context . NewCoreContextFromViper ( ) . WithDecoder ( authcmd . GetAccountDecoder ( cdc ) )
// ctx.PrintResponse = true TODO is this needed for channelID
// Get sender adress
submitter , err := ctx . GetFromAddress ( )
if err != nil {
return err
}
// read in update
bz , err := ioutil . ReadAll ( os . Stdin )
if err != nil {
return err
}
// decode json
var update paychan . Update
cdc . UnmarshalJSON ( bz , & update )
// Create the create channel msg to send
msg := paychan . MsgSubmitUpdate {
Update : update ,
Submitter : submitter ,
}
err = msg . ValidateBasic ( )
if err != nil {
return err
}
// Build and sign the transaction, then broadcast to the blockchain
2018-09-01 23:29:51 +00:00
err = ctx . EnsureSignBuildBroadcast ( ctx . FromAddressName , [ ] sdk . Msg { msg } , cdc )
2018-09-01 20:41:40 +00:00
if err != nil {
return err
}
return nil
} ,
}
return cmd
}
/ *
2018-07-14 22:27:56 +00:00
func ClosePaychanCmd ( cdc * wire . Codec ) * cobra . Command {
flagState := "state"
2018-07-14 00:09:02 +00:00
2018-07-14 22:27:56 +00:00
cmd := & cobra . Command {
Use : "close" ,
Short : "Close a payment channel, given a state" ,
2018-07-15 14:08:08 +00:00
Long : "Close an existing payment channel with a state received from a sender. This signs it as the receiver before submitting to the blockchain." ,
2018-07-14 22:27:56 +00:00
Args : cobra . NoArgs ,
RunE : func ( cmd * cobra . Command , args [ ] string ) error {
2018-07-15 14:08:08 +00:00
ctx := context . NewCoreContextFromViper ( ) . WithDecoder ( authcmd . GetAccountDecoder ( cdc ) )
2018-07-14 22:27:56 +00:00
// Get the sender-signed close tx
state := viper . GetString ( flagState )
txBytes , err := base64 . StdEncoding . DecodeString ( state )
if err != nil {
return err
}
stdTx := auth . StdTx { }
2018-07-15 14:08:08 +00:00
cdc . UnmarshalBinary ( txBytes , & stdTx )
2018-07-14 22:27:56 +00:00
// Sign close tx
// ensure contxt has up to date account and sequence numbers
ctx , err = Ensure ( ctx )
if err != nil {
return err
}
// Sign message (asks user for password)
_ , sig , err := UserSignMsg ( ctx , ctx . FromAddressName , stdTx . Msg )
if err != nil {
return err
}
// Append signature to close tx
stdTx . Signatures = append ( stdTx . Signatures , sig )
// encode close tx
txBytes , err = cdc . MarshalBinary ( stdTx )
if err != nil {
return err
}
// Broadcast close tx to the blockchain
res , err := ctx . BroadcastTx ( txBytes )
if err != nil {
return err
}
fmt . Printf ( "Committed at block %d. Hash: %s\n" , res . Height , res . Hash . String ( ) )
return nil
} ,
}
cmd . Flags ( ) . String ( flagState , "" , "State received from sender." )
return cmd
}
// HELPER FUNCTIONS
// This is a partial refactor of cosmos-sdk/client/context.
// Existing API was awkard to use for paychans.
func Ensure ( ctx context . CoreContext ) ( context . CoreContext , error ) {
ctx , err := context . EnsureAccountNumber ( ctx )
2018-07-14 00:09:02 +00:00
if err != nil {
2018-07-14 22:27:56 +00:00
return ctx , err
2018-07-14 00:09:02 +00:00
}
// default to next sequence number if none provided
2018-07-14 22:27:56 +00:00
ctx , err = context . EnsureSequence ( ctx )
2018-07-14 00:09:02 +00:00
if err != nil {
2018-07-14 22:27:56 +00:00
return ctx , err
2018-07-14 00:09:02 +00:00
}
2018-07-14 22:27:56 +00:00
return ctx , nil
}
func UserSignMsg ( ctx context . CoreContext , name string , msg sdk . Msg ) ( signMsg auth . StdSignMsg , stdSig auth . StdSignature , err error ) {
// TODO check how to handle non error return values on error. Returning empty versions doesn't seem right.
2018-07-14 00:09:02 +00:00
passphrase , err := ctx . GetPassphraseFromStdin ( name )
if err != nil {
2018-07-14 22:27:56 +00:00
return signMsg , stdSig , err
2018-07-14 00:09:02 +00:00
}
2018-07-14 22:27:56 +00:00
// build the Sign Messsage from the Standard Message
chainID := ctx . ChainID
if chainID == "" {
return signMsg , stdSig , errors . Errorf ( "Chain ID required but not specified" )
}
accnum := ctx . AccountNumber
sequence := ctx . Sequence
signMsg = auth . StdSignMsg {
ChainID : chainID ,
AccountNumbers : [ ] int64 { accnum } ,
Sequences : [ ] int64 { sequence } ,
Msg : msg ,
Fee : auth . NewStdFee ( ctx . Gas , sdk . Coin { } ) , // TODO run simulate to estimate gas?
}
keybase , err := keys . GetKeyBase ( )
2018-07-14 00:09:02 +00:00
if err != nil {
2018-07-14 22:27:56 +00:00
return signMsg , stdSig , err
}
// sign and build
bz := signMsg . Bytes ( )
sig , pubkey , err := keybase . Sign ( name , passphrase , bz )
if err != nil {
return signMsg , stdSig , err
}
stdSig = auth . StdSignature {
PubKey : pubkey ,
Signature : sig ,
AccountNumber : accnum ,
Sequence : sequence ,
2018-07-14 00:09:02 +00:00
}
2018-07-14 22:27:56 +00:00
return signMsg , stdSig , nil
2018-07-14 00:09:02 +00:00
}
2018-07-14 22:27:56 +00:00
func Build ( cdc * wire . Codec , signMsg auth . StdSignMsg , sig auth . StdSignature ) ( [ ] byte , error ) {
tx := auth . NewStdTx ( signMsg . Msg , signMsg . Fee , [ ] auth . StdSignature { sig } )
return cdc . MarshalBinary ( tx )
}
2018-07-14 00:09:02 +00:00
2018-07-14 22:27:56 +00:00
func EnsureSignBuild ( ctx context . CoreContext , name string , msg sdk . Msg , cdc * wire . Codec ) ( [ ] byte , error ) {
//Ensure context has up to date account and sequence numbers
ctx , err := Ensure ( ctx )
if err != nil {
return nil , err
2018-07-08 22:09:07 +00:00
}
2018-07-14 22:27:56 +00:00
// Sign message (asks user for password)
signMsg , sig , err := UserSignMsg ( ctx , name , msg )
if err != nil {
return nil , err
}
// Create tx and marshal
txBytes , err := Build ( cdc , signMsg , sig )
if err != nil {
return nil , err
}
return txBytes , nil
2018-07-08 22:09:07 +00:00
}
2018-08-24 23:18:41 +00:00
* /