Commit 64a6c2c1 authored by Bas van Kervel's avatar Bas van Kervel

eth: add new RPC method (personal.) SignAndSendTransaction

parent e798e4fd
...@@ -147,9 +147,21 @@ func (am *Manager) Sign(addr common.Address, hash []byte) (signature []byte, err ...@@ -147,9 +147,21 @@ func (am *Manager) Sign(addr common.Address, hash []byte) (signature []byte, err
return crypto.Sign(hash, unlockedKey.PrivateKey) return crypto.Sign(hash, unlockedKey.PrivateKey)
} }
// SignWithPassphrase signs hash if the private key matching the given address can be
// decrypted with the given passphrase.
func (am *Manager) SignWithPassphrase(addr common.Address, passphrase string, hash []byte) (signature []byte, err error) {
_, key, err := am.getDecryptedKey(Account{Address: addr}, passphrase)
if err != nil {
return nil, err
}
defer zeroKey(key.PrivateKey)
return crypto.Sign(hash, key.PrivateKey)
}
// Unlock unlocks the given account indefinitely. // Unlock unlocks the given account indefinitely.
func (am *Manager) Unlock(a Account, keyAuth string) error { func (am *Manager) Unlock(a Account, passphrase string) error {
return am.TimedUnlock(a, keyAuth, 0) return am.TimedUnlock(a, passphrase, 0)
} }
// Lock removes the private key with the given address from memory. // Lock removes the private key with the given address from memory.
......
...@@ -81,6 +81,34 @@ func TestSign(t *testing.T) { ...@@ -81,6 +81,34 @@ func TestSign(t *testing.T) {
} }
} }
func TestSignWithPassphrase(t *testing.T) {
dir, am := tmpManager(t, true)
defer os.RemoveAll(dir)
pass := "passwd"
acc, err := am.NewAccount(pass)
if err != nil {
t.Fatal(err)
}
if _, unlocked := am.unlocked[acc.Address]; unlocked {
t.Fatal("expected account to be locked")
}
_, err = am.SignWithPassphrase(acc.Address, pass, testSigData)
if err != nil {
t.Fatal(err)
}
if _, unlocked := am.unlocked[acc.Address]; unlocked {
t.Fatal("expected account to be locked")
}
if _, err = am.SignWithPassphrase(acc.Address, "invalid passwd", testSigData); err == nil {
t.Fatal("expected SignHash to fail with invalid password")
}
}
func TestTimedUnlock(t *testing.T) { func TestTimedUnlock(t *testing.T) {
dir, am := tmpManager(t, true) dir, am := tmpManager(t, true)
defer os.RemoveAll(dir) defer os.RemoveAll(dir)
......
...@@ -41,7 +41,7 @@ import ( ...@@ -41,7 +41,7 @@ import (
) )
var ( var (
passwordRegexp = regexp.MustCompile("personal.[nu]") passwordRegexp = regexp.MustCompile("personal.[nus]")
onlyws = regexp.MustCompile("^\\s*$") onlyws = regexp.MustCompile("^\\s*$")
exit = regexp.MustCompile("^\\s*exit\\s*;*\\s*$") exit = regexp.MustCompile("^\\s*exit\\s*;*\\s*$")
) )
......
This diff is collapsed.
...@@ -26,6 +26,7 @@ import ( ...@@ -26,6 +26,7 @@ import (
"path/filepath" "path/filepath"
"regexp" "regexp"
"strings" "strings"
"sync"
"time" "time"
"github.com/ethereum/ethash" "github.com/ethereum/ethash"
...@@ -113,6 +114,7 @@ type Ethereum struct { ...@@ -113,6 +114,7 @@ type Ethereum struct {
// Handlers // Handlers
txPool *core.TxPool txPool *core.TxPool
txMu sync.Mutex
blockchain *core.BlockChain blockchain *core.BlockChain
accountManager *accounts.Manager accountManager *accounts.Manager
pow *ethash.Ethash pow *ethash.Ethash
...@@ -293,7 +295,7 @@ func (s *Ethereum) APIs() []rpc.API { ...@@ -293,7 +295,7 @@ func (s *Ethereum) APIs() []rpc.API {
}, { }, {
Namespace: "personal", Namespace: "personal",
Version: "1.0", Version: "1.0",
Service: NewPrivateAccountAPI(s.accountManager), Service: NewPrivateAccountAPI(s),
Public: false, Public: false,
}, { }, {
Namespace: "eth", Namespace: "eth",
......
...@@ -431,6 +431,12 @@ web3._extend({ ...@@ -431,6 +431,12 @@ web3._extend({
name: 'importRawKey', name: 'importRawKey',
call: 'personal_importRawKey', call: 'personal_importRawKey',
params: 2 params: 2
}),
new web3._extend.Method({
name: 'signAndSendTransaction',
call: 'personal_signAndSendTransaction',
params: 2,
inputFormatter: [web3._extend.formatters.inputTransactionFormatter, null]
}) })
] ]
}); });
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment