Skip to content

Commit b950a29

Browse files
Bas van Kervelobscuren
authored andcommitted
[release/1.4.5] eth: add new RPC method (personal.) SignAndSendTransaction
(cherry picked from commit 64a6c2c) Conflicts: cmd/geth/js.go internal/web3ext/web3ext.go
1 parent 4c69536 commit b950a29

File tree

6 files changed

+207
-99
lines changed

6 files changed

+207
-99
lines changed

accounts/account_manager.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,21 @@ func (am *Manager) Sign(addr common.Address, hash []byte) (signature []byte, err
147147
return crypto.Sign(hash, unlockedKey.PrivateKey)
148148
}
149149

150+
// SignWithPassphrase signs hash if the private key matching the given address can be
151+
// decrypted with the given passphrase.
152+
func (am *Manager) SignWithPassphrase(addr common.Address, passphrase string, hash []byte) (signature []byte, err error) {
153+
_, key, err := am.getDecryptedKey(Account{Address: addr}, passphrase)
154+
if err != nil {
155+
return nil, err
156+
}
157+
158+
defer zeroKey(key.PrivateKey)
159+
return crypto.Sign(hash, key.PrivateKey)
160+
}
161+
150162
// Unlock unlocks the given account indefinitely.
151-
func (am *Manager) Unlock(a Account, keyAuth string) error {
152-
return am.TimedUnlock(a, keyAuth, 0)
163+
func (am *Manager) Unlock(a Account, passphrase string) error {
164+
return am.TimedUnlock(a, passphrase, 0)
153165
}
154166

155167
// Lock removes the private key with the given address from memory.

accounts/accounts_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,34 @@ func TestSign(t *testing.T) {
8181
}
8282
}
8383

84+
func TestSignWithPassphrase(t *testing.T) {
85+
dir, am := tmpManager(t, true)
86+
defer os.RemoveAll(dir)
87+
88+
pass := "passwd"
89+
acc, err := am.NewAccount(pass)
90+
if err != nil {
91+
t.Fatal(err)
92+
}
93+
94+
if _, unlocked := am.unlocked[acc.Address]; unlocked {
95+
t.Fatal("expected account to be locked")
96+
}
97+
98+
_, err = am.SignWithPassphrase(acc.Address, pass, testSigData)
99+
if err != nil {
100+
t.Fatal(err)
101+
}
102+
103+
if _, unlocked := am.unlocked[acc.Address]; unlocked {
104+
t.Fatal("expected account to be locked")
105+
}
106+
107+
if _, err = am.SignWithPassphrase(acc.Address, "invalid passwd", testSigData); err == nil {
108+
t.Fatal("expected SignHash to fail with invalid password")
109+
}
110+
}
111+
84112
func TestTimedUnlock(t *testing.T) {
85113
dir, am := tmpManager(t, true)
86114
defer os.RemoveAll(dir)

cmd/geth/js.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ import (
4141
)
4242

4343
var (
44-
passwordRegexp = regexp.MustCompile("personal.[nu]")
4544
leadingSpace = regexp.MustCompile("^ ")
45+
passwordRegexp = regexp.MustCompile("personal.[nus]")
4646
onlyws = regexp.MustCompile("^\\s*$")
4747
exit = regexp.MustCompile("^\\s*exit\\s*;*\\s*$")
4848
)

0 commit comments

Comments
 (0)