From e94aa421c69b8eee2f2e06654f9a288cdfe4f546 Mon Sep 17 00:00:00 2001 From: "Daniel A. Nagy" Date: Fri, 8 May 2015 16:17:19 +0200 Subject: [PATCH] New API call for signatures. --- rpc/api.go | 11 +++++++++++ rpc/args.go | 5 +++++ xeth/xeth.go | 21 +++++++++++++++++++++ 3 files changed, 37 insertions(+) diff --git a/rpc/api.go b/rpc/api.go index 6ba0d93e25..28ba41c86e 100644 --- a/rpc/api.go +++ b/rpc/api.go @@ -158,6 +158,17 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err v := api.xethAtStateNum(args.BlockNumber).CodeAtBytes(args.Address) *reply = newHexData(v) + case "eth_sign": + args := new(NewSigArgs) + if err := json.Unmarshal(req.Params, &args); err != nil { + return err + } + v, err := api.xeth.Sign(args.From, args.Data) + if err != nil { + return err + } + *reply = v + case "eth_sendTransaction", "eth_transact": args := new(NewTxArgs) if err := json.Unmarshal(req.Params, &args); err != nil { diff --git a/rpc/args.go b/rpc/args.go index 58a7504158..6c98d12672 100644 --- a/rpc/args.go +++ b/rpc/args.go @@ -166,6 +166,11 @@ type NewTxArgs struct { BlockNumber int64 } +type NewSigArgs struct { + From string + Data string +} + func (args *NewTxArgs) UnmarshalJSON(b []byte) (err error) { var obj []json.RawMessage var ext struct { diff --git a/xeth/xeth.go b/xeth/xeth.go index ad85968030..2ca0e80d71 100644 --- a/xeth/xeth.go +++ b/xeth/xeth.go @@ -813,6 +813,27 @@ func (self *XEth) ConfirmTransaction(tx string) bool { return self.frontend.ConfirmTransaction(tx) } +func (self *XEth) Sign(fromStr, hashStr string) (string, error) { + var ( + from = common.HexToAddress(fromStr) + hash = common.HexToHash(hashStr) + ) + sig, err := self.backend.AccountManager().Sign(accounts.Account{Address: from.Bytes()}, hash) + if err == accounts.ErrLocked { + if didUnlock { + return fmt.Errorf("signer account still locked after successful unlock") + } + if !self.frontend.UnlockAccount(from.Bytes()) { + return fmt.Errorf("could not unlock signer account") + } + // retry signing, the account should now be unlocked. + return self.Sign(fromStr, hashStr) + } else if err != nil { + return err + } + return common.toHex(sig) +} + func (self *XEth) Transact(fromStr, toStr, nonceStr, valueStr, gasStr, gasPriceStr, codeStr string) (string, error) { // this minimalistic recoding is enough (works for natspec.js)