From e94aa421c69b8eee2f2e06654f9a288cdfe4f546 Mon Sep 17 00:00:00 2001 From: "Daniel A. Nagy" Date: Fri, 8 May 2015 16:17:19 +0200 Subject: New API call for signatures. --- xeth/xeth.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'xeth/xeth.go') diff --git a/xeth/xeth.go b/xeth/xeth.go index ad8596803..2ca0e80d7 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) -- cgit v1.2.3 From a487396b764c8dac409f9ee1ef32c29c4cefb7d9 Mon Sep 17 00:00:00 2001 From: "Daniel A. Nagy" Date: Fri, 8 May 2015 16:36:13 +0200 Subject: eth_sign added to API for signing arbitrary data. --- xeth/xeth.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'xeth/xeth.go') diff --git a/xeth/xeth.go b/xeth/xeth.go index 2ca0e80d7..dc2d4f06f 100644 --- a/xeth/xeth.go +++ b/xeth/xeth.go @@ -813,25 +813,25 @@ func (self *XEth) ConfirmTransaction(tx string) bool { return self.frontend.ConfirmTransaction(tx) } -func (self *XEth) Sign(fromStr, hashStr string) (string, error) { +func (self *XEth) Sign(fromStr, hashStr string, didUnlock bool) (string, error) { var ( from = common.HexToAddress(fromStr) hash = common.HexToHash(hashStr) ) - sig, err := self.backend.AccountManager().Sign(accounts.Account{Address: from.Bytes()}, hash) + sig, err := self.backend.AccountManager().Sign(accounts.Account{Address: from.Bytes()}, hash.Bytes()) if err == accounts.ErrLocked { if didUnlock { - return fmt.Errorf("signer account still locked after successful unlock") + return "", fmt.Errorf("signer account still locked after successful unlock") } if !self.frontend.UnlockAccount(from.Bytes()) { - return fmt.Errorf("could not unlock signer account") + return "", fmt.Errorf("could not unlock signer account") } // retry signing, the account should now be unlocked. - return self.Sign(fromStr, hashStr) + return self.Sign(fromStr, hashStr, true) } else if err != nil { - return err + return "", err } - return common.toHex(sig) + return common.ToHex(sig), nil } func (self *XEth) Transact(fromStr, toStr, nonceStr, valueStr, gasStr, gasPriceStr, codeStr string) (string, error) { -- cgit v1.2.3 From 49559e6d5e7c365f1aa081e94cb46f3483833647 Mon Sep 17 00:00:00 2001 From: "Daniel A. Nagy" Date: Mon, 11 May 2015 15:46:18 +0200 Subject: Interactive signature creation refactored into separate doSign function. --- xeth/xeth.go | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) (limited to 'xeth/xeth.go') diff --git a/xeth/xeth.go b/xeth/xeth.go index 06cd9dc1b..76ca4b9b4 100644 --- a/xeth/xeth.go +++ b/xeth/xeth.go @@ -815,22 +815,30 @@ func (self *XEth) ConfirmTransaction(tx string) bool { return self.frontend.ConfirmTransaction(tx) } -func (self *XEth) Sign(fromStr, hashStr string, didUnlock bool) (string, error) { - var ( - from = common.HexToAddress(fromStr) - hash = common.HexToHash(hashStr) - ) - sig, err := self.backend.AccountManager().Sign(accounts.Account{Address: from.Bytes()}, hash.Bytes()) +func (self *XEth) doSign(from common.Address, hash []byte, didUnlock bool) ([]byte, error) { + 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") + return nil, fmt.Errorf("signer account still locked after successful unlock") } if !self.frontend.UnlockAccount(from.Bytes()) { - return "", fmt.Errorf("could not unlock signer account") + return nil, fmt.Errorf("could not unlock signer account") } // retry signing, the account should now be unlocked. - return self.Sign(fromStr, hashStr, true) + return self.doSign(from, hash, true) } else if err != nil { + return nil, err + } + return sig, nil +} + +func (self *XEth) Sign(fromStr, hashStr string, didUnlock bool) (string, error) { + var ( + from = common.HexToAddress(fromStr) + hash = common.HexToHash(hashStr) + ) + sig, err := self.doSign(from, hash.Bytes(), didUnlock) + if err != nil { return "", err } return common.ToHex(sig), nil @@ -928,17 +936,9 @@ func (self *XEth) Transact(fromStr, toStr, nonceStr, valueStr, gasStr, gasPriceS } func (self *XEth) sign(tx *types.Transaction, from common.Address, didUnlock bool) error { - sig, err := self.backend.AccountManager().Sign(accounts.Account{Address: from.Bytes()}, tx.Hash().Bytes()) - if err == accounts.ErrLocked { - if didUnlock { - return fmt.Errorf("sender account still locked after successful unlock") - } - if !self.frontend.UnlockAccount(from.Bytes()) { - return fmt.Errorf("could not unlock sender account") - } - // retry signing, the account should now be unlocked. - return self.sign(tx, from, true) - } else if err != nil { + hash := tx.Hash().Bytes() + sig, err := self.doSign(from, hash, didUnlock) + if err != nil { return err } tx.SetSignatureValues(sig) -- cgit v1.2.3 From 51d4566cbf5c91e429313f1765d5a7d2afc13634 Mon Sep 17 00:00:00 2001 From: "Daniel A. Nagy" Date: Mon, 11 May 2015 15:54:19 +0200 Subject: Only allow doSign to sign hashes, enforced by using the type common.Hash --- xeth/xeth.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'xeth/xeth.go') diff --git a/xeth/xeth.go b/xeth/xeth.go index 76ca4b9b4..47b833a34 100644 --- a/xeth/xeth.go +++ b/xeth/xeth.go @@ -815,8 +815,8 @@ func (self *XEth) ConfirmTransaction(tx string) bool { return self.frontend.ConfirmTransaction(tx) } -func (self *XEth) doSign(from common.Address, hash []byte, didUnlock bool) ([]byte, error) { - sig, err := self.backend.AccountManager().Sign(accounts.Account{Address: from.Bytes()}, hash) +func (self *XEth) doSign(from common.Address, hash common.Hash, didUnlock bool) ([]byte, error) { + sig, err := self.backend.AccountManager().Sign(accounts.Account{Address: from.Bytes()}, hash.Bytes()) if err == accounts.ErrLocked { if didUnlock { return nil, fmt.Errorf("signer account still locked after successful unlock") @@ -837,7 +837,7 @@ func (self *XEth) Sign(fromStr, hashStr string, didUnlock bool) (string, error) from = common.HexToAddress(fromStr) hash = common.HexToHash(hashStr) ) - sig, err := self.doSign(from, hash.Bytes(), didUnlock) + sig, err := self.doSign(from, hash, didUnlock) if err != nil { return "", err } @@ -936,7 +936,7 @@ func (self *XEth) Transact(fromStr, toStr, nonceStr, valueStr, gasStr, gasPriceS } func (self *XEth) sign(tx *types.Transaction, from common.Address, didUnlock bool) error { - hash := tx.Hash().Bytes() + hash := tx.Hash() sig, err := self.doSign(from, hash, didUnlock) if err != nil { return err -- cgit v1.2.3