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

Colin/conf hash #80

Closed
wants to merge 3 commits into from
Closed
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
13 changes: 12 additions & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

bam "github.com/cosmos/cosmos-sdk/baseapp"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/common"
rlp "github.com/ethereum/go-ethereum/rlp"
"github.com/tendermint/go-amino"
cryptoAmino "github.com/tendermint/tendermint/crypto/encoding/amino"
Expand All @@ -31,6 +32,8 @@ type ChildChain struct {

cdc *amino.Codec

rootchainAddress common.Address

txIndex uint16

feeAmount uint64
Expand Down Expand Up @@ -74,7 +77,7 @@ func NewChildChain(logger log.Logger, db dbm.DB, traceStore io.Writer) *ChildCha
app.SetEndBlocker(app.endBlocker)

// NOTE: type AnteHandler func(ctx Context, tx Tx) (newCtx Context, result Result, abort bool)
app.SetAnteHandler(auth.NewAnteHandler(app.utxoMapper, app.feeUpdater))
app.SetAnteHandler(auth.NewAnteHandler(app.utxoMapper, app.getRootchainAddress, app.feeUpdater))

err := app.LoadLatestVersion(app.capKeyMainStore)
if err != nil {
Expand All @@ -101,6 +104,7 @@ func (app *ChildChain) initChainer(ctx sdk.Context, req abci.RequestInitChain) a
app.utxoMapper.AddUTXO(ctx, utxo)
}

app.rootchainAddress = ethcmn.HexToAddress(genesisState.RootChain)
app.validatorAddress = ethcmn.HexToAddress(genesisState.Validator.Address)

// load the initial stake information
Expand Down Expand Up @@ -164,6 +168,13 @@ func (app *ChildChain) feeUpdater(output []utxo.Output) sdk.Error {
return nil
}

// Helper function for getting rootchain address
// Rootchain Address will be set when genesis state is loaded
// not upon child chain constructor call
func (app *ChildChain) getRootchainAddress() common.Address {
return app.rootchainAddress
}

func MakeCodec() *amino.Codec {
cdc := amino.NewCodec()
cdc.RegisterInterface((*sdk.Msg)(nil), nil)
Expand Down
13 changes: 10 additions & 3 deletions app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,14 @@ import (

*/

var rootchainAddress = common.BytesToAddress([]byte("rootchain contract address"))

func newChildChain() *ChildChain {
logger := log.NewTMLogger(log.NewSyncWriter(os.Stdout)).With("module", "sdk/app")
db := dbm.NewMemDB()
return NewChildChain(logger, db, nil)
app := NewChildChain(logger, db, nil)
app.rootchainAddress = rootchainAddress
return app
}

// Creates a deposit of value 100 for each address in input
Expand All @@ -47,6 +51,7 @@ func InitTestChain(cc *ChildChain, valAddr common.Address, addrs ...common.Addre
}

genState := GenesisState{
RootChain: rootchainAddress.String(),
Validator: genValidator,
UTXOs: genUTXOs,
}
Expand Down Expand Up @@ -85,8 +90,10 @@ func GenerateSimpleMsg(Owner0, NewOwner0 common.Address, position [4]uint64, amo

// Returns a confirmsig array signed by privKey0 and privKey1
func CreateConfirmSig(position types.PlasmaPosition, privKey0, privKey1 *ecdsa.PrivateKey, two_inputs bool) (confirmSigs [2]types.Signature) {
confirmBytes := position.GetSignBytes()
hash := ethcrypto.Keccak256(confirmBytes)
rootchainHash := ethcrypto.Keccak256(rootchainAddress.Bytes())
posHash := ethcrypto.Keccak256(position.GetSignBytes())
confirmHash := append(rootchainHash, posHash...)
hash := ethcrypto.Keccak256(confirmHash)
confirmSig, _ := ethcrypto.Sign(hash, privKey0)

var confirmSig1 []byte
Expand Down
1 change: 1 addition & 0 deletions app/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

// State to Unmarshal
type GenesisState struct {
RootChain string `json: rootchain_address`
Validator GenesisValidator `json: validator`
UTXOs []GenesisUTXO `json:"UTXOs"`
}
Expand Down
2 changes: 1 addition & 1 deletion app/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ func TestGenesisState(t *testing.T) {
logger := log.NewTMLogger(log.NewSyncWriter(os.Stdout)).With("module", "sdk/app")
db := dbm.NewMemDB()
app := NewChildChain(logger, db, nil)

addrs := []common.Address{utils.GenerateAddress(), utils.GenerateAddress()}

var genUTXOs []GenesisUTXO
Expand All @@ -36,6 +35,7 @@ func TestGenesisState(t *testing.T) {
}

genState := GenesisState{
RootChain: rootchainAddress.String(),
Validator: genValidator,
UTXOs: genUTXOs,
}
Expand Down
16 changes: 8 additions & 8 deletions auth/ante.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (

// NewAnteHandler returns an AnteHandler that checks signatures,
// confirm signatures, and increments the feeAmount
func NewAnteHandler(utxoMapper utxo.Mapper, feeUpdater utxo.FeeUpdater) sdk.AnteHandler {
func NewAnteHandler(utxoMapper utxo.Mapper, rootchainAddress types.GetAddress, feeUpdater utxo.FeeUpdater) sdk.AnteHandler {
Copy link
Member

Choose a reason for hiding this comment

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

Why is this a function? Why can't u just pass in a common.Address?

Copy link
Member Author

Choose a reason for hiding this comment

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

At the time SetAnteHandler is called, rootchainAddress has not been set. rootchainAddress gets set in InitChainer which is after the baseapp has been sealed

return func(
ctx sdk.Context, tx sdk.Tx,
) (_ sdk.Context, _ sdk.Result, abort bool) {
Expand Down Expand Up @@ -64,10 +64,9 @@ func NewAnteHandler(utxoMapper utxo.Mapper, feeUpdater utxo.FeeUpdater) sdk.Ante
if !res.IsOK() {
return ctx, res, true
}
posSignBytes := position0.GetSignBytes()

// Verify that confirmation signature
res = processConfirmSig(ctx, utxoMapper, position0, addr0, spendMsg.ConfirmSigs0, posSignBytes)
res = processConfirmSig(ctx, utxoMapper, position0, addr0, rootchainAddress(), spendMsg.ConfirmSigs0)
if !res.IsOK() {
return ctx, res, true
}
Expand All @@ -88,9 +87,7 @@ func NewAnteHandler(utxoMapper utxo.Mapper, feeUpdater utxo.FeeUpdater) sdk.Ante
return ctx, res, true
}

posSignBytes = position1.GetSignBytes()

res = processConfirmSig(ctx, utxoMapper, position1, addr1, spendMsg.ConfirmSigs1, posSignBytes)
res = processConfirmSig(ctx, utxoMapper, position1, addr1, rootchainAddress(), spendMsg.ConfirmSigs1)
if !res.IsOK() {
return ctx, res, true
}
Expand Down Expand Up @@ -122,7 +119,7 @@ func processSig(

func processConfirmSig(
ctx sdk.Context, utxoMapper utxo.Mapper,
position types.PlasmaPosition, addr common.Address, sigs [2]types.Signature, signBytes []byte) (
position types.PlasmaPosition, addr, rootchainAddress common.Address, sigs [2]types.Signature) (
res sdk.Result) {

// Verify utxo exists
Expand All @@ -136,7 +133,10 @@ func processConfirmSig(
}
inputAddresses := plasmaUTXO.GetInputAddresses()

hash := ethcrypto.Keccak256(signBytes)
rootchainHash := ethcrypto.Keccak256(rootchainAddress.Bytes())

posHash := ethcrypto.Keccak256(position.GetSignBytes())
hash := ethcrypto.Keccak256(append(rootchainHash, posHash...))

pubKey0, err0 := ethcrypto.SigToPub(hash, sigs[0].Bytes())
if err0 != nil || !reflect.DeepEqual(ethcrypto.PubkeyToAddress(*pubKey0).Bytes(), inputAddresses[0].Bytes()) {
Expand Down
18 changes: 13 additions & 5 deletions auth/ante_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ import (
"testing"
)

var rootchainAddress = common.BytesToAddress([]byte("rootchain contract address"))

func GetRootchainAddress() common.Address {
return rootchainAddress
}

func setup() (sdk.Context, utxo.Mapper, utxo.FeeUpdater) {
ms, capKey := utxo.SetupMultiStore()

Expand Down Expand Up @@ -61,8 +67,10 @@ func GenSpendMsg() types.SpendMsg {

// Returns a confirmsig array signed by privKey0 and privKey1
func CreateConfirmSig(position types.PlasmaPosition, privKey0, privKey1 *ecdsa.PrivateKey, two_inputs bool) (confirmSigs [2]types.Signature) {
confirmBytes := position.GetSignBytes()
hash := ethcrypto.Keccak256(confirmBytes)
rootchainHash := ethcrypto.Keccak256(rootchainAddress.Bytes())
posHash := ethcrypto.Keccak256(position.GetSignBytes())
confHash := append(rootchainHash, posHash...)
hash := ethcrypto.Keccak256(confHash)
confirmSig, _ := ethcrypto.Sign(hash, privKey0)

var confirmSig1 []byte
Expand Down Expand Up @@ -106,7 +114,7 @@ func TestNoSigs(t *testing.T) {
var msg = GenSpendMsg()
tx := types.NewBaseTx(msg, []types.Signature{})

handler := NewAnteHandler(mapper, feeUpdater)
handler := NewAnteHandler(mapper, GetRootchainAddress, feeUpdater)
_, res, abort := handler(ctx, tx)

assert.Equal(t, true, abort, "did not abort with no signatures")
Expand All @@ -123,7 +131,7 @@ func TestNotEnoughSigs(t *testing.T) {
sig, _ := ethcrypto.Sign(hash, priv)
tx := types.NewBaseTx(msg, []types.Signature{types.Signature{sig}})

handler := NewAnteHandler(mapper, feeUpdater)
handler := NewAnteHandler(mapper, GetRootchainAddress, feeUpdater)
_, res, abort := handler(ctx, tx)

assert.Equal(t, true, abort, "did not abort with incorrect number of signatures")
Expand Down Expand Up @@ -224,7 +232,7 @@ func TestDifferentCases(t *testing.T) {
owner_index1 := utils.GetIndex(tc.input1.owner_index)
tx := GetTx(msg, keys[tc.input0.owner_index], keys[owner_index1], tc.input1.owner_index != -1)

handler := NewAnteHandler(mapper, feeUpdater)
handler := NewAnteHandler(mapper, GetRootchainAddress, feeUpdater)
_, res, abort := handler(ctx, tx)

assert.Equal(t, true, abort, fmt.Sprintf("did not abort on utxo that does not exist. Case: %d", index))
Expand Down
24 changes: 17 additions & 7 deletions client/plasmacli/cmd/confirmSig.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ import (
)

const (
flagAddr = "addr"
flagAddr = "addr"
flagRootchainAddr = "rootchainAddr"
)

func init() {
rootCmd.AddCommand(signCmd)
signCmd.Flags().String(flagAddr, "", "Address to sign with")
signCmd.Flags().String(flagRootchainAddr, "", "Contract Address of rootchain")
viper.BindPFlags(signCmd.Flags())
}

Expand Down Expand Up @@ -60,16 +62,24 @@ var signCmd = &cobra.Command{
return err
}

// get the rootchain address
rootAddrStr := viper.GetString(flagRootchainAddr)
rootAddr, err := client.StrToAddress(rootAddrStr)
if err != nil {
return err
}

// sign positions
signBytes1 := pos[0].GetSignBytes()
hash1 := ethcrypto.Keccak256(signBytes1)
sig1, err := ks.SignHashWithPassphrase(acct, passphrase, hash1)
rootHash := ethcrypto.Keccak256(rootAddr.Bytes())
posHash := ethcrypto.Keccak256(pos[0].GetSignBytes())
confirmHash := ethcrypto.Keccak256(append(rootHash, posHash...))
sig1, err := ks.SignHashWithPassphrase(acct, passphrase, confirmHash)

var sig2 []byte
if len(pos) > 1 {
signBytes2 := pos[1].GetSignBytes()
hash2 := ethcrypto.Keccak256(signBytes2)
sig2, err = ks.SignHashWithPassphrase(acct, passphrase, hash2)
posHash = ethcrypto.Keccak256(pos[1].GetSignBytes())
confirmHash = ethcrypto.Keccak256(append(rootHash, posHash...))
sig2, err = ks.SignHashWithPassphrase(acct, passphrase, confirmHash)
if err != nil {
return err
}
Expand Down
2 changes: 2 additions & 0 deletions types/utxo.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ const (

var _ utxo.UTXO = &BaseUTXO{}

type GetAddress func() common.Address
Copy link
Member

Choose a reason for hiding this comment

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

Should be named GetOwner


// Implements UTXO interface
type BaseUTXO struct {
InputAddresses [2]common.Address
Expand Down