From b59c8399fbe42390a3d41e945d03b1f21c1a9b8d Mon Sep 17 00:00:00 2001 From: bas-vk Date: Fri, 28 Oct 2016 21:25:49 +0200 Subject: internal/ethapi: add personal_sign and fix eth_sign to hash message (#2940) This commit includes several API changes: - The behavior of eth_sign is changed. It now accepts an arbitrary message, prepends the well-known string \x19Ethereum Signed Message:\n hashes the result using keccak256 and calculates the signature of the hash. This breaks backwards compatability! - personal_sign(hash, address [, password]) is added. It has the same semantics as eth_sign but also accepts a password. The private key used to sign the hash is temporarily unlocked in the scope of the request. - personal_recover(message, signature) is added and returns the address for the account that created a signature. --- crypto/crypto_test.go | 35 +++++++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 6 deletions(-) (limited to 'crypto/crypto_test.go') diff --git a/crypto/crypto_test.go b/crypto/crypto_test.go index 58b29da49..80c9a9aae 100644 --- a/crypto/crypto_test.go +++ b/crypto/crypto_test.go @@ -80,20 +80,28 @@ func Test0Key(t *testing.T) { } } -func TestSign(t *testing.T) { +func testSign(signfn func([]byte, *ecdsa.PrivateKey) ([]byte, error), t *testing.T) { key, _ := HexToECDSA(testPrivHex) addr := common.HexToAddress(testAddrHex) msg := Keccak256([]byte("foo")) - sig, err := Sign(msg, key) + sig, err := signfn(msg, key) if err != nil { t.Errorf("Sign error: %s", err) } + + // signfn can return a recover id of either [0,1] or [27,28]. + // In the latter case its an Ethereum signature, adjust recover id. + if sig[64] == 27 || sig[64] == 28 { + sig[64] -= 27 + } + recoveredPub, err := Ecrecover(msg, sig) if err != nil { t.Errorf("ECRecover error: %s", err) } - recoveredAddr := PubkeyToAddress(*ToECDSAPub(recoveredPub)) + pubKey := ToECDSAPub(recoveredPub) + recoveredAddr := PubkeyToAddress(*pubKey) if addr != recoveredAddr { t.Errorf("Address mismatch: want: %x have: %x", addr, recoveredAddr) } @@ -107,21 +115,36 @@ func TestSign(t *testing.T) { if addr != recoveredAddr2 { t.Errorf("Address mismatch: want: %x have: %x", addr, recoveredAddr2) } +} +func TestSign(t *testing.T) { + testSign(Sign, t) } -func TestInvalidSign(t *testing.T) { - _, err := Sign(make([]byte, 1), nil) +func TestSignEthereum(t *testing.T) { + testSign(SignEthereum, t) +} + +func testInvalidSign(signfn func([]byte, *ecdsa.PrivateKey) ([]byte, error), t *testing.T) { + _, err := signfn(make([]byte, 1), nil) if err == nil { t.Errorf("expected sign with hash 1 byte to error") } - _, err = Sign(make([]byte, 33), nil) + _, err = signfn(make([]byte, 33), nil) if err == nil { t.Errorf("expected sign with hash 33 byte to error") } } +func TestInvalidSign(t *testing.T) { + testInvalidSign(Sign, t) +} + +func TestInvalidSignEthereum(t *testing.T) { + testInvalidSign(SignEthereum, t) +} + func TestNewContractAddress(t *testing.T) { key, _ := HexToECDSA(testPrivHex) addr := common.HexToAddress(testAddrHex) -- cgit v1.2.3