Skip to content

Commit

Permalink
got wallet creation working
Browse files Browse the repository at this point in the history
  • Loading branch information
cbarraford committed Jul 4, 2019
1 parent 920318c commit 9ec57f9
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 13 deletions.
4 changes: 2 additions & 2 deletions cmd/msigcli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ func main() {
// Read in the configuration file for the sdk
config := sdk.GetConfig()
config.SetBech32PrefixForAccount("tbnb", "tbnbp")
config.SetBech32PrefixForValidator(sdk.Bech32PrefixValAddr, sdk.Bech32PrefixValPub)
config.SetBech32PrefixForConsensusNode(sdk.Bech32PrefixConsAddr, sdk.Bech32PrefixConsPub)
config.SetBech32PrefixForValidator("tbnbv", "tbnbvp")
config.SetBech32PrefixForConsensusNode("tbnbc", "tbnbcp")
config.Seal()

rootCmd := &cobra.Command{
Expand Down
6 changes: 3 additions & 3 deletions cmd/msigd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ func main() {
cdc := app.MakeCodec()

config := sdk.GetConfig()
config.SetBech32PrefixForAccount(sdk.Bech32PrefixAccAddr, sdk.Bech32PrefixAccPub)
config.SetBech32PrefixForValidator(sdk.Bech32PrefixValAddr, sdk.Bech32PrefixValPub)
config.SetBech32PrefixForConsensusNode(sdk.Bech32PrefixConsAddr, sdk.Bech32PrefixConsPub)
config.SetBech32PrefixForAccount("tbnb", "tbnbp")
config.SetBech32PrefixForValidator("tbnbv", "tbnbvp")
config.SetBech32PrefixForConsensusNode("tbnbc", "tbnbcp")
config.Seal()

ctx := server.NewDefaultContext()
Expand Down
54 changes: 50 additions & 4 deletions x/multisig/client/rest/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"strings"
Expand All @@ -15,7 +16,6 @@ import (
"github.com/cosmos/cosmos-sdk/client/flags"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/rest"
authrest "github.com/cosmos/cosmos-sdk/x/auth/client/rest"
"github.com/cosmos/cosmos-sdk/x/auth/client/utils"
"github.com/cosmos/cosmos-sdk/x/auth/types"

Expand Down Expand Up @@ -46,7 +46,7 @@ func RegisterRoutes(cliCtx context.CLIContext, r *mux.Router, storeName string)
r.HandleFunc(fmt.Sprintf("/%s/tx", storeName), createUnsignedTransactionHandler(cliCtx)).Methods("PUT")
r.HandleFunc(fmt.Sprintf("/%s/sign/multi", storeName), multiSignHandler(cliCtx)).Methods("POST")

authrest.RegisterTxRoutes(cliCtx, r)
r.HandleFunc(fmt.Sprintf("/%s/broadcast", storeName), broadcastTxRequest(cliCtx)).Methods("POST")
}

func getWalletHandler(cliCtx context.CLIContext, storeName string) http.HandlerFunc {
Expand Down Expand Up @@ -398,7 +398,6 @@ func createWalletHandler(cliCtx context.CLIContext) http.HandlerFunc {
if !baseReq.ValidateBasic(w) {
return
}
log.Printf("REQ: %+v", req)

signers := make([]sdk.AccAddress, len(req.Signers))
for i, _ := range req.Signers {
Expand All @@ -411,7 +410,6 @@ func createWalletHandler(cliCtx context.CLIContext) http.HandlerFunc {

// create the message
msg := mtypes.NewMsgCreateWallet(req.Name, req.PubKeys, req.MinSigTx, signers)
log.Printf("MSG: %+v", msg)
err = msg.ValidateBasic()
if err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
Expand All @@ -432,3 +430,51 @@ func validateMultisigThreshold(k, nKeys int) error {
}
return nil
}

// BroadcastReq defines a tx broadcasting request.
type BroadcastReq struct {
Tx types.StdTx `json:"tx"`
Mode string `json:"mode"`
}

// broadcastTxRequest implements a tx broadcasting handler that is responsible
// for broadcasting a valid and signed tx to a full node. The tx can be
// broadcasted via a sync|async|block mechanism.
func broadcastTxRequest(cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req BroadcastReq

body, err := ioutil.ReadAll(r.Body)
if err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
log.Printf("Foo1")
return
}

err = cliCtx.Codec.UnmarshalJSON(body, &req)
if err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
log.Printf("Foo2")
return
}

txBytes, err := cliCtx.Codec.MarshalBinaryLengthPrefixed(req.Tx)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
log.Printf("Foo3")
return
}

cliCtx = cliCtx.WithBroadcastMode(req.Mode)

res, err := cliCtx.BroadcastTx(txBytes)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
log.Printf("Foo4")
return
}

log.Printf("Foo5")
rest.PostProcessResponse(w, cliCtx, res)
}
}
3 changes: 0 additions & 3 deletions x/multisig/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package multisig

import (
"fmt"
"log"

sdk "github.com/cosmos/cosmos-sdk/types"
)
Expand Down Expand Up @@ -36,8 +35,6 @@ func handleMsgCreateWallet(ctx sdk.Context, keeper Keeper, msg MsgCreateWallet)
fmt.Sprintf("Error creating new wallet: %s", err.Error()),
).Result()
}
log.Printf("Wallet: %+v", wallet)
log.Printf("MSG: %+v", msg)
current := keeper.GetWallet(ctx, wallet.Address.String())
if !current.Address.Empty() {
return sdk.ErrUnauthorized("Wallet already exists").Result()
Expand Down
2 changes: 1 addition & 1 deletion x/multisig/types/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ const RouterKey = ModuleName // this was defined in your key.go file

// MsgCreateWallet defines a CreateWallet message
type MsgCreateWallet struct {
MinSigTx int `json:"min_sig_tx"`
Name string `json:"name"`
PubKeys []string `json:"pub_keys"`
MinSigTx int `json:"min_sig_tx"`
Signers []sdk.AccAddress `json:"signers"`
}

Expand Down

0 comments on commit 9ec57f9

Please sign in to comment.