add personal_recover

This commit is contained in:
Bas van Kervel 2016-08-25 13:50:47 +02:00
parent 85bfe8382d
commit 42a26000ea
2 changed files with 34 additions and 0 deletions

View file

@ -312,6 +312,35 @@ func (s *PrivateAccountAPI) Sign(ctx context.Context, message string, addr commo
return common.ToHex(signature), nil return common.ToHex(signature), nil
} }
// recover returns the address for the account that was used to create the signature.
//
// https://github.com/ethereum/go-ethereum/wiki/Management-APIs#personal_recover
func (s *PrivateAccountAPI) Recover(ctx context.Context, message string, signature string) (common.Address, error) {
var (
sig = common.FromHex(signature)
hash = crypto.Keccak256(common.FromHex(message))
)
if len(sig) != 65 {
return common.Address{}, fmt.Errorf("signature must be 65 bytes long")
}
// see crypto.Ecrecover description
if sig[64] == 27 || sig[64] == 28 {
sig[64] -= 27
}
rpk, err := crypto.Ecrecover(hash, sig)
if err != nil {
return common.Address{}, err
}
pubKey := crypto.ToECDSAPub(rpk)
recoveredAddr := crypto.PubkeyToAddress(*pubKey)
return recoveredAddr, nil
}
// SignAndSendTransaction was renamed to SendTransaction. This method is deprecated // SignAndSendTransaction was renamed to SendTransaction. This method is deprecated
// and will be removed in the future. It primary goal is to give clients time to update. // and will be removed in the future. It primary goal is to give clients time to update.
func (s *PrivateAccountAPI) SignAndSendTransaction(ctx context.Context, args SendTxArgs, passwd string) (common.Hash, error) { func (s *PrivateAccountAPI) SignAndSendTransaction(ctx context.Context, args SendTxArgs, passwd string) (common.Hash, error) {

View file

@ -434,6 +434,11 @@ web3._extend({
call: 'personal_sign', call: 'personal_sign',
params: 3, params: 3,
inputFormatter: [null, web3._extend.formatters.inputAddressFormatter, null] inputFormatter: [null, web3._extend.formatters.inputAddressFormatter, null]
}),
new web3._extend.Method({
name: 'recover',
call: 'personal_recover',
params: 2
}) })
] ]
}) })