From 1dd4e8905d53fce033f39dbfd2d7b82522bf336a Mon Sep 17 00:00:00 2001 From: Rongjian Lan Date: Thu, 2 May 2019 01:46:39 -0700 Subject: [PATCH] Use user key to send transaction --- .../restclientsupport/puzzle_service.go | 15 +++++++------ api/service/restclientsupport/service.go | 14 ++++++------- contracts/Puzzle.go | 2 +- contracts/Puzzle.sol | 4 ++-- node/puzzle_contract.go | 21 ++++++++++++------- 5 files changed, 30 insertions(+), 26 deletions(-) diff --git a/api/service/restclientsupport/puzzle_service.go b/api/service/restclientsupport/puzzle_service.go index 8876c1339..0fe818b8c 100644 --- a/api/service/restclientsupport/puzzle_service.go +++ b/api/service/restclientsupport/puzzle_service.go @@ -6,7 +6,6 @@ import ( "net/http" "strconv" - "github.com/ethereum/go-ethereum/common" "github.com/harmony-one/harmony/internal/utils" ) @@ -46,12 +45,12 @@ func (s *Service) Play(w http.ResponseWriter, r *http.Request) { // Payout triggers play payout of puzzle smart contract. func (s *Service) Payout(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") - address := r.FormValue("address") - newLevel := r.FormValue("new_level") + key := r.FormValue("key") + newLevel := r.FormValue("level") sequence := r.FormValue("sequence") newLevelInt, err := strconv.Atoi(newLevel) - fmt.Println("Payout: address", address, "new_level", newLevelInt) + fmt.Println("Payout: key", key, "new_level", newLevelInt) res := &Response{ Success: false, } @@ -60,7 +59,7 @@ func (s *Service) Payout(w http.ResponseWriter, r *http.Request) { json.NewEncoder(w).Encode(res) return } - txID, err := s.CreateTransactionForPayoutMethod(common.HexToAddress(address), newLevelInt, sequence) + txID, err := s.CreateTransactionForPayoutMethod(key, newLevelInt, sequence) if err != nil { utils.GetLogInstance().Error("Payout error", err) @@ -75,9 +74,9 @@ func (s *Service) Payout(w http.ResponseWriter, r *http.Request) { // End triggers endGame of puzzle smart contract. func (s *Service) End(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") - address := r.FormValue("address") + key := r.FormValue("key") - fmt.Println("Payout: address", address) + fmt.Println("endgame: key", key) res := &Response{ Success: false, } @@ -87,7 +86,7 @@ func (s *Service) End(w http.ResponseWriter, r *http.Request) { return } - txID, err := s.CreateTransactionForEndMethod(common.HexToAddress(address)) + txID, err := s.CreateTransactionForEndMethod(key) if err != nil { utils.GetLogInstance().Error("Payout error", err) json.NewEncoder(w).Encode(res) diff --git a/api/service/restclientsupport/service.go b/api/service/restclientsupport/service.go index 549ef7467..1f98095ff 100644 --- a/api/service/restclientsupport/service.go +++ b/api/service/restclientsupport/service.go @@ -36,8 +36,8 @@ type Service struct { CallFaucetContract func(common.Address) common.Hash GetAccountBalance func(common.Address) (*big.Int, error) CreateTransactionForPlayMethod func(string, int64) (string, error) - CreateTransactionForPayoutMethod func(common.Address, int, string) (string, error) - CreateTransactionForEndMethod func(common.Address) (string, error) + CreateTransactionForPayoutMethod func(string, int, string) (string, error) + CreateTransactionForEndMethod func(string) (string, error) } // New returns new client support service. @@ -47,8 +47,8 @@ func New( CreateTransactionForPickWinner func() error, CallFaucetContract func(common.Address) common.Hash, GetAccountBalance func(common.Address) (*big.Int, error), CreateTransactionForPlayMethod func(string, int64) (string, error), - CreateTransactionForPayoutMethod func(common.Address, int, string) (string, error), - CreateTransactionForEndMethod func(common.Address) (string, error)) *Service { + CreateTransactionForPayoutMethod func(string, int, string) (string, error), + CreateTransactionForEndMethod func(string) (string, error)) *Service { return &Service{ CreateTransactionForEnterMethod: CreateTransactionForEnterMethod, GetResult: GetResult, @@ -103,15 +103,15 @@ func (s *Service) Run() *http.Server { s.router.Path("/winner").HandlerFunc(s.Winner) // Routing for puzzle app. - s.router.Path("/play").Queries("address", "{[0-9A-Fa-fx]*?}", "amount", "{[0-9]*?}").HandlerFunc(s.Play).Methods("GET") + s.router.Path("/play").Queries("key", "{[0-9A-Fa-fx]*?}", "amount", "{[0-9]*?}").HandlerFunc(s.Play).Methods("GET") s.router.Path("/play").HandlerFunc(s.Play) // Set up router for payout. - s.router.Path("/payout").Queries("address", "{[0-9A-Fa-fx]*?}", "level", "{[0-9]*?}", "sequence", "{[A-Za-z]*?}").HandlerFunc(s.Payout).Methods("GET") + s.router.Path("/payout").Queries("key", "{[0-9A-Fa-fx]*?}", "level", "{[0-9]*?}", "sequence", "{[A-Za-z]*?}").HandlerFunc(s.Payout).Methods("GET") s.router.Path("/payout").HandlerFunc(s.Payout) // Set up router for endgame. - s.router.Path("/end").Queries("address", "{[0-9A-Fa-fx]*?}").HandlerFunc(s.End).Methods("GET") + s.router.Path("/end").Queries("key", "{[0-9A-Fa-fx]*?}").HandlerFunc(s.End).Methods("GET") s.router.Path("/end").HandlerFunc(s.End) // Do serving now. utils.GetLogInstance().Info("Listening on ", "port: ", Port) diff --git a/contracts/Puzzle.go b/contracts/Puzzle.go index 30cfecc31..f2add03cf 100644 --- a/contracts/Puzzle.go +++ b/contracts/Puzzle.go @@ -31,7 +31,7 @@ var ( const PuzzleABI = "[{\"constant\":false,\"inputs\":[{\"name\":\"player\",\"type\":\"address\"}],\"name\":\"endGame\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"manager\",\"outputs\":[{\"name\":\"\",\"type\":\"address\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"player\",\"type\":\"address\"},{\"name\":\"level\",\"type\":\"uint256\"},{\"name\":\"\",\"type\":\"string\"}],\"name\":\"payout\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"player\",\"type\":\"address\"},{\"name\":\"level\",\"type\":\"uint256\"}],\"name\":\"setLevel\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"getPlayers\",\"outputs\":[{\"name\":\"\",\"type\":\"address[]\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[],\"name\":\"play\",\"outputs\":[],\"payable\":true,\"stateMutability\":\"payable\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"player\",\"type\":\"address\"}],\"name\":\"resetPlayer\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[],\"name\":\"reset\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"name\":\"players\",\"outputs\":[{\"name\":\"\",\"type\":\"address\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[],\"payable\":true,\"stateMutability\":\"payable\",\"type\":\"constructor\"}]" // PuzzleBin is the compiled bytecode used for deploying new contracts. -const PuzzleBin = `0x6080604052600480546001600160a01b03191633179055610984806100256000396000f3fe6080604052600436106100865760003560e01c80638b5b9ccc116100595780638b5b9ccc146101f257806393e84cd914610257578063c95e09091461025f578063d826f88f14610292578063f71d96cb146102a757610086565b80632a035b6c1461008b578063481c6a75146100c057806352bcd7c8146100f1578063722dcd8f146101b9575b600080fd5b34801561009757600080fd5b506100be600480360360208110156100ae57600080fd5b50356001600160a01b03166102d1565b005b3480156100cc57600080fd5b506100d56103ba565b604080516001600160a01b039092168252519081900360200190f35b3480156100fd57600080fd5b506100be6004803603606081101561011457600080fd5b6001600160a01b038235169160208101359181019060608101604082013564010000000081111561014457600080fd5b82018360208201111561015657600080fd5b8035906020019184600183028401116401000000008311171561017857600080fd5b91908080601f0160208091040260200160405190810160405280939291908181526020018383808284376000920191909152509295506103c9945050505050565b3480156101c557600080fd5b506100be600480360360408110156101dc57600080fd5b506001600160a01b038135169060200135610576565b3480156101fe57600080fd5b50610207610592565b60408051602080825283518183015283519192839290830191858101910280838360005b8381101561024357818101518382015260200161022b565b505050509050019250505060405180910390f35b6100be6105f5565b34801561026b57600080fd5b506100be6004803603602081101561028257600080fd5b50356001600160a01b031661072f565b34801561029e57600080fd5b506100be610803565b3480156102b357600080fd5b506100d5600480360360208110156102ca57600080fd5b50356108e9565b6004546040805180820190915260138152600160681b72556e617574686f72697a656420416363657373026020820152906001600160a01b0316331461039857604051600160e51b62461bcd0281526004018080602001828103825283818151815260200191508051906020019080838360005b8381101561035d578181015183820152602001610345565b50505050905090810190601f16801561038a5780820380516001836020036101000a031916815260200191505b509250505060405180910390fd5b506001600160a01b03166000908152600260205260409020805460ff19169055565b6004546001600160a01b031681565b6004546040805180820190915260138152600160681b72556e617574686f72697a656420416363657373026020820152906001600160a01b0316331461045457604051600160e51b62461bcd0281526004018080602001828103825283818151815260200191508051906020019080838360008381101561035d578181015183820152602001610345565b506001600160a01b038316600090815260026020908152604091829020548251808401909352601f83527f506c61796572206973206e6f7420696e20616e206163746976652067616d65009183019190915260ff1615156001146104fd57604051600160e51b62461bcd0281526004018080602001828103825283818151815260200191508051906020019080838360008381101561035d578181015183820152602001610345565b506001600160a01b03831660009081526020819052604090205482036105238484610576565b6001600160a01b03841660008181526001602052604080822054905160059091048402929183156108fc02918491818181858888f1935050505015801561056e573d6000803e3d6000fd5b505050505050565b6001600160a01b03909116600090815260208190526040902055565b606060058054806020026020016040519081016040528092919081815260200182805480156105ea57602002820191906000526020600020905b81546001600160a01b031681526001909101906020018083116105cc575b505050505090505b90565b60408051808201909152601181527f496e73756666696369656e742046756e6400000000000000000000000000000060208201526801158e460913d0000034101561068557604051600160e51b62461bcd0281526004018080602001828103825283818151815260200191508051906020019080838360008381101561035d578181015183820152602001610345565b503360009081526003602052604090205460ff1615156106fc57336000818152600360205260408120805460ff191660019081179091556005805491820181559091527f036b6384b5eca791c62761152d0c79bb0604c104a5fb6f4eb0703f3154bb3db00180546001600160a01b03191690911790555b3360009081526020818152604080832083905560018083528184203490556002909252909120805460ff19169091179055565b6004546040805180820190915260138152600160681b72556e617574686f72697a656420416363657373026020820152906001600160a01b031633146107ba57604051600160e51b62461bcd0281526004018080602001828103825283818151815260200191508051906020019080838360008381101561035d578181015183820152602001610345565b506001600160a01b031660009081526020818152604080832083905560028252808320805460ff1990811690915560018352818420849055600390925290912080549091169055565b6004546040805180820190915260138152600160681b72556e617574686f72697a656420416363657373026020820152906001600160a01b0316331461088e57604051600160e51b62461bcd0281526004018080602001828103825283818151815260200191508051906020019080838360008381101561035d578181015183820152602001610345565b5060055460005b818110156108d75760006005828154811015156108ae57fe5b6000918252602090912001546001600160a01b031690506108ce8161072f565b50600101610895565b5060006108e5600582610911565b5050565b60058054829081106108f757fe5b6000918252602090912001546001600160a01b0316905081565b8154818355818111156109355760008381526020902061093591810190830161093a565b505050565b6105f291905b808211156109545760008155600101610940565b509056fea165627a7a723058204706efee8388d70aabafd388a42b5b54a6268e521a1d1401aec9adf14b0e6f980029` +const PuzzleBin = `0x6080604052600480546001600160a01b0319163317905561087a806100256000396000f3fe6080604052600436106100865760003560e01c80638b5b9ccc116100595780638b5b9ccc146101f257806393e84cd914610257578063c95e09091461025f578063d826f88f14610292578063f71d96cb146102a757610086565b80632a035b6c1461008b578063481c6a75146100c057806352bcd7c8146100f1578063722dcd8f146101b9575b600080fd5b34801561009757600080fd5b506100be600480360360208110156100ae57600080fd5b50356001600160a01b03166102d1565b005b3480156100cc57600080fd5b506100d56102f2565b604080516001600160a01b039092168252519081900360200190f35b3480156100fd57600080fd5b506100be6004803603606081101561011457600080fd5b6001600160a01b038235169160208101359181019060608101604082013564010000000081111561014457600080fd5b82018360208201111561015657600080fd5b8035906020019184600183028401116401000000008311171561017857600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600092019190915250929550610301945050505050565b3480156101c557600080fd5b506100be600480360360408110156101dc57600080fd5b506001600160a01b03813516906020013561045e565b3480156101fe57600080fd5b5061020761047a565b60408051602080825283518183015283519192839290830191858101910280838360005b8381101561024357818101518382015260200161022b565b505050509050019250505060405180910390f35b6100be6104dd565b34801561026b57600080fd5b506100be6004803603602081101561028257600080fd5b50356001600160a01b0316610617565b34801561029e57600080fd5b506100be6106f2565b3480156102b357600080fd5b506100d5600480360360208110156102ca57600080fd5b50356107df565b6001600160a01b03166000908152600260205260409020805460ff19169055565b6004546001600160a01b031681565b6001600160a01b038316600090815260026020908152604091829020548251808401909352601f83527f506c61796572206973206e6f7420696e20616e206163746976652067616d65009183019190915260ff1615156001146103e557604051600160e51b62461bcd0281526004018080602001828103825283818151815260200191508051906020019080838360005b838110156103aa578181015183820152602001610392565b50505050905090810190601f1680156103d75780820380516001836020036101000a031916815260200191505b509250505060405180910390fd5b506001600160a01b038316600090815260208190526040902054820361040b848461045e565b6001600160a01b03841660008181526001602052604080822054905160059091048402929183156108fc02918491818181858888f19350505050158015610456573d6000803e3d6000fd5b505050505050565b6001600160a01b03909116600090815260208190526040902055565b606060058054806020026020016040519081016040528092919081815260200182805480156104d257602002820191906000526020600020905b81546001600160a01b031681526001909101906020018083116104b4575b505050505090505b90565b60408051808201909152601181527f496e73756666696369656e742046756e6400000000000000000000000000000060208201526801158e460913d0000034101561056d57604051600160e51b62461bcd028152600401808060200182810382528381815181526020019150805190602001908083836000838110156103aa578181015183820152602001610392565b503360009081526003602052604090205460ff1615156105e457336000818152600360205260408120805460ff191660019081179091556005805491820181559091527f036b6384b5eca791c62761152d0c79bb0604c104a5fb6f4eb0703f3154bb3db00180546001600160a01b03191690911790555b3360009081526020818152604080832083905560018083528184203490556002909252909120805460ff19169091179055565b60045460408051808201909152601381527f556e617574686f72697a656420416363657373000000000000000000000000006020820152906001600160a01b031633146106a957604051600160e51b62461bcd028152600401808060200182810382528381815181526020019150805190602001908083836000838110156103aa578181015183820152602001610392565b506001600160a01b031660009081526020818152604080832083905560028252808320805460ff1990811690915560018352818420849055600390925290912080549091169055565b60045460408051808201909152601381527f556e617574686f72697a656420416363657373000000000000000000000000006020820152906001600160a01b0316331461078457604051600160e51b62461bcd028152600401808060200182810382528381815181526020019150805190602001908083836000838110156103aa578181015183820152602001610392565b5060055460005b818110156107cd5760006005828154811015156107a457fe5b6000918252602090912001546001600160a01b031690506107c481610617565b5060010161078b565b5060006107db600582610807565b5050565b60058054829081106107ed57fe5b6000918252602090912001546001600160a01b0316905081565b81548183558181111561082b5760008381526020902061082b918101908301610830565b505050565b6104da91905b8082111561084a5760008155600101610836565b509056fea165627a7a7230582009aeb602bdc932b8bcb2aca8e93a3fc246646a3ed47985331344f85ae6f56f810029` // DeployPuzzle deploys a new Ethereum contract, binding an instance of Puzzle to it. func DeployPuzzle(auth *bind.TransactOpts, backend bind.ContractBackend) (common.Address, *types.Transaction, *Puzzle, error) { diff --git a/contracts/Puzzle.sol b/contracts/Puzzle.sol index bf2ce4620..c6bd91b88 100644 --- a/contracts/Puzzle.sol +++ b/contracts/Puzzle.sol @@ -46,7 +46,7 @@ contract Puzzle { /** * @dev pay the player if they have crossed their last best level. */ - function payout(address payable player, uint level, string memory /*sequence*/) public restricted { + function payout(address payable player, uint level, string memory /*sequence*/) public { require(playerInGame[player] == true, NOT_IN_GAME); uint progress = level - playerLevel[player]; //if a later transaction for a higher level comes earlier. @@ -58,7 +58,7 @@ contract Puzzle { /** * @dev set the player's game state to inactive. */ - function endGame(address player) public restricted { + function endGame(address player) public { delete playerInGame[player]; } diff --git a/node/puzzle_contract.go b/node/puzzle_contract.go index c66e6f63a..7d7a36a3c 100644 --- a/node/puzzle_contract.go +++ b/node/puzzle_contract.go @@ -102,7 +102,7 @@ func (node *Node) CreateTransactionForPlayMethod(priKey string, amount int64) (s } // CreateTransactionForPayoutMethod generates transaction for payout method and add it into pending tx list. -func (node *Node) CreateTransactionForPayoutMethod(address common.Address, level int, sequence string) (string, error) { +func (node *Node) CreateTransactionForPayoutMethod(priKey string, level int, sequence string) (string, error) { var err error toAddress := node.PuzzleContractAddress @@ -111,6 +111,10 @@ func (node *Node) CreateTransactionForPayoutMethod(address common.Address, level utils.GetLogInstance().Error("Failed to generate staking contract's ABI", "error", err) return "", err } + + key, err := crypto.HexToECDSA(priKey) + address := crypto.PubkeyToAddress(key.PublicKey) + // add params for address payable player, uint8 new_level, steps string fmt.Println("Payout: address", address) bytesData, err := abi.Pack(Payout, address, big.NewInt(int64(level)), sequence) @@ -119,11 +123,10 @@ func (node *Node) CreateTransactionForPayoutMethod(address common.Address, level return "", err } - key := node.PuzzleManagerPrivateKey if key == nil { - return "", fmt.Errorf("PuzzleManagerPrivateKey is nil") + return "", fmt.Errorf("user key is nil") } - nonce := node.GetNonceOfAddress(crypto.PubkeyToAddress(key.PublicKey)) + nonce := node.GetNonceOfAddress(address) Amount := big.NewInt(0) tx := types.NewTransaction( nonce, @@ -148,7 +151,7 @@ func (node *Node) CreateTransactionForPayoutMethod(address common.Address, level } // CreateTransactionForEndMethod generates transaction for endGame method and add it into pending tx list. -func (node *Node) CreateTransactionForEndMethod(address common.Address) (string, error) { +func (node *Node) CreateTransactionForEndMethod(priKey string) (string, error) { var err error toAddress := node.PuzzleContractAddress @@ -157,6 +160,9 @@ func (node *Node) CreateTransactionForEndMethod(address common.Address) (string, utils.GetLogInstance().Error("Failed to generate staking contract's ABI", "error", err) return "", err } + key, err := crypto.HexToECDSA(priKey) + address := crypto.PubkeyToAddress(key.PublicKey) + // add params for address payable player, uint8 new_level, steps string fmt.Println("EndGame: address", address) bytesData, err := abi.Pack(EndGame, address) @@ -165,11 +171,10 @@ func (node *Node) CreateTransactionForEndMethod(address common.Address) (string, return "", err } - key := node.PuzzleManagerPrivateKey if key == nil { - return "", fmt.Errorf("PuzzleManagerPrivateKey is nil") + return "", fmt.Errorf("user key is nil") } - nonce := node.GetNonceOfAddress(crypto.PubkeyToAddress(key.PublicKey)) + nonce := node.GetNonceOfAddress(address) Amount := big.NewInt(0) tx := types.NewTransaction( nonce,