Skip to content
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

Pegzone witness #3

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module github.com/DE-labtory/erc20peggy

go 1.12

require (
github.com/cosmos/cosmos-sdk v0.34.3
github.com/gogo/protobuf v1.2.1
github.com/gorilla/mux v1.7.1
github.com/spf13/cobra v0.0.3
github.com/tendermint/tendermint v0.31.5
)
179 changes: 179 additions & 0 deletions go.sum

Large diffs are not rendered by default.

17 changes: 17 additions & 0 deletions pegzone/app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* Copyright 2019 DE-labtory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package pegzone
61 changes: 61 additions & 0 deletions pegzone/x/witness/client/cli/tx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright 2019 DE-labtory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package cli

import (
"encoding/json"

"github.com/DE-labtory/erc20peggy/pegzone/x/witness"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/client/utils"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
authtxb "github.com/cosmos/cosmos-sdk/x/auth/client/txbuilder"
"github.com/spf13/cobra"
)

func GetLockCmd(cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "lock [address] [data]",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc).WithAccountDecoder(cdc)
if err := cliCtx.EnsureAccountExists(); err != nil {
return err
}

data, err := json.Marshal(args[0])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is data args[1], not args[0]?

You said Use is "lock [address] [data]".
Then, [address] is args[0], [data] is args[1].

if err != nil {
return err
}

msg := witness.NewMsgLock(data, args[1], cliCtx.GetFromAddress())
err = msg.ValidateBasic()
if err != nil {
return err
}

cliCtx.PrintResponse = true
return utils.GenerateOrBroadcastMsgs(
cliCtx,
authtxb.NewTxBuilderFromCLI().WithTxEncoder(utils.GetTxEncoder(cdc)),
[]sdk.Msg{msg},
false,
)
},
}
}
78 changes: 78 additions & 0 deletions pegzone/x/witness/client/rest/rest.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Copyright 2019 DE-labtory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package rest

import (
"fmt"
"net/http"

"github.com/DE-labtory/erc20peggy/pegzone/x/witness"
"github.com/cosmos/cosmos-sdk/client/context"
clientrest "github.com/cosmos/cosmos-sdk/client/rest"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/rest"
"github.com/gorilla/mux"
)

const (
restName = "pegzone"
)

// RegisterRoutes - Central function to define routes that get registered by the main application
func RegisterRoutes(cliCtx context.CLIContext, r *mux.Router, cdc *codec.Codec) {
r.HandleFunc(fmt.Sprintf("/%s/lock", restName), lockHandler(cdc, cliCtx)).Methods("POST")
}

type lockReq struct {
BaseReq rest.BaseReq `json:"base_req"`
Data []byte `json:"data"`
Address string `json:"address"`
Owner string `json:"owner"`
}

func lockHandler(cdc *codec.Codec, cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req lockReq

if !rest.ReadRESTReq(w, r, cdc, &req) {
rest.WriteErrorResponse(w, http.StatusBadRequest, "failed to parse request")
return
}

baseReq := req.BaseReq.Sanitize()
if !baseReq.ValidateBasic(w) {
return
}

addr, err := sdk.AccAddressFromBech32(req.Owner)
if err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}

// create the message
msg := witness.NewMsgLock(req.Data, req.Address, addr)
err = msg.ValidateBasic()
if err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}

clientrest.WriteGenerateStdTxResponse(w, cdc, cliCtx, baseReq, []sdk.Msg{msg})
}
}
24 changes: 24 additions & 0 deletions pegzone/x/witness/codec.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright 2019 DE-labtory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package witness

import "github.com/cosmos/cosmos-sdk/codec"

// RegisterCodec registers concrete types on the Amino codec
func RegisterCodec(cdc *codec.Codec) {
cdc.RegisterConcrete(MsgLock{}, "nameservice/SetName", nil)
}
40 changes: 40 additions & 0 deletions pegzone/x/witness/handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright 2019 DE-labtory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package witness

import (
"reflect"

sdk "github.com/cosmos/cosmos-sdk/types"
)

func NewHandler(keeper Keeper) sdk.Handler {
return func(ctx sdk.Context, msg sdk.Msg) sdk.Result {
switch msg := msg.(type) {
case MsgLock:
return handleMsgLock(ctx, keeper, msg)
default:
errMsg := "Unrecognized lock Msg type: " + reflect.TypeOf(msg).Name()
return sdk.ErrUnknownRequest(errMsg).Result()
}
}
}

// Handle lock message
func handleMsgLock(ctx sdk.Context, keeper Keeper, msg MsgLock) sdk.Result {
return sdk.Result{}
}
36 changes: 36 additions & 0 deletions pegzone/x/witness/keeper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2019 DE-labtory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package witness

import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/gogo/protobuf/codec"
)

type Keeper struct {
cdc *codec.Codec
storeKey sdk.StoreKey

//todo use cosmos erc20 keeper or client?
}

func NewKeeper(cdc *codec.Codec, storeKey sdk.StoreKey) Keeper {
return Keeper{
cdc: cdc,
storeKey: storeKey,
}
}
67 changes: 67 additions & 0 deletions pegzone/x/witness/msgs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright 2019 DE-labtory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package witness

import (
"encoding/json"

sdk "github.com/cosmos/cosmos-sdk/types"
)

type MsgLock struct {
Data []byte
Address string
Owner sdk.AccAddress
}

func NewMsgLock(data []byte, address string, owner sdk.AccAddress) MsgLock {
return MsgLock{
Address: address,
Data: data,
Owner: owner,
}
}

func (msg MsgLock) Route() string {
return "witness"
}

func (msg MsgLock) Type() string {
return "lock"
}

func (msg MsgLock) ValidateBasic() sdk.Error {
if msg.Owner.Empty() {
return sdk.ErrInvalidAddress(msg.Owner.String())
}
if len(msg.Address) == 0 || len(msg.Data) == 0 {
return sdk.ErrUnknownRequest("Name and/or Value cannot be empty")
}
return nil
}

func (msg MsgLock) GetSignBytes() []byte {
b, err := json.Marshal(msg)
if err != nil {
panic(err)
}
return sdk.MustSortJSON(b)
}

func (msg MsgLock) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{msg.Owner}
}