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. --- core/types/block_test.go | 2 +- core/types/transaction.go | 6 ++++-- core/types/transaction_test.go | 2 +- 3 files changed, 6 insertions(+), 4 deletions(-) (limited to 'core') diff --git a/core/types/block_test.go b/core/types/block_test.go index cdd8431f4..ac7f17c0d 100644 --- a/core/types/block_test.go +++ b/core/types/block_test.go @@ -51,7 +51,7 @@ func TestBlockEncoding(t *testing.T) { check("Size", block.Size(), common.StorageSize(len(blockEnc))) tx1 := NewTransaction(0, common.HexToAddress("095e7baea6a6c7c4c2dfeb977efac326af552d87"), big.NewInt(10), big.NewInt(50000), big.NewInt(10), nil) - tx1, _ = tx1.WithSignature(common.Hex2Bytes("9bea4c4daac7c7c52e093e6a4c35dbbcf8856f1af7b059ba20253e70848d094f8a8fae537ce25ed8cb5af9adac3f141af69bd515bd2ba031522df09b97dd72b100")) + tx1, _ = tx1.WithSignature(common.Hex2Bytes("9bea4c4daac7c7c52e093e6a4c35dbbcf8856f1af7b059ba20253e70848d094f8a8fae537ce25ed8cb5af9adac3f141af69bd515bd2ba031522df09b97dd72b11b")) check("len(Transactions)", len(block.Transactions()), 1) check("Transactions[0].Hash", block.Transactions()[0].Hash(), tx1.Hash()) diff --git a/core/types/transaction.go b/core/types/transaction.go index f0512ae7e..ceea4f959 100644 --- a/core/types/transaction.go +++ b/core/types/transaction.go @@ -318,6 +318,8 @@ func (tx *Transaction) publicKey(homestead bool) ([]byte, error) { return pub, nil } +// WithSignature returns a new transaction with the given signature. +// This signature needs to be formatted as described in the yellow paper (v+27). func (tx *Transaction) WithSignature(sig []byte) (*Transaction, error) { if len(sig) != 65 { panic(fmt.Sprintf("wrong size for signature: got %d, want 65", len(sig))) @@ -325,13 +327,13 @@ func (tx *Transaction) WithSignature(sig []byte) (*Transaction, error) { cpy := &Transaction{data: tx.data} cpy.data.R = new(big.Int).SetBytes(sig[:32]) cpy.data.S = new(big.Int).SetBytes(sig[32:64]) - cpy.data.V = sig[64] + 27 + cpy.data.V = sig[64] return cpy, nil } func (tx *Transaction) SignECDSA(prv *ecdsa.PrivateKey) (*Transaction, error) { h := tx.SigHash() - sig, err := crypto.Sign(h[:], prv) + sig, err := crypto.SignEthereum(h[:], prv) if err != nil { return nil, err } diff --git a/core/types/transaction_test.go b/core/types/transaction_test.go index 8b0b02c3e..98a78d221 100644 --- a/core/types/transaction_test.go +++ b/core/types/transaction_test.go @@ -46,7 +46,7 @@ var ( big.NewInt(1), common.FromHex("5544"), ).WithSignature( - common.Hex2Bytes("98ff921201554726367d2be8c804a7ff89ccf285ebc57dff8ae4c44b9c19ac4a8887321be575c8095f789dd4c743dfe42c1820f9231f98a962b210e3ac2452a301"), + common.Hex2Bytes("98ff921201554726367d2be8c804a7ff89ccf285ebc57dff8ae4c44b9c19ac4a8887321be575c8095f789dd4c743dfe42c1820f9231f98a962b210e3ac2452a31c"), ) ) -- cgit v1.2.3