eth: add personal_importRawKey for runtime private key import

pull/2493/head
Ales Katona 9 years ago
parent 70b8b54cd2
commit 572da73d4d
  1. 7
      accounts/account_manager.go
  2. 11
      eth/api.go
  3. 27
      internal/web3ext/web3ext.go

@ -284,7 +284,12 @@ func (am *Manager) Import(keyJSON []byte, passphrase, newPassphrase string) (Acc
// ImportECDSA stores the given key into the key directory, encrypting it with the passphrase. // ImportECDSA stores the given key into the key directory, encrypting it with the passphrase.
func (am *Manager) ImportECDSA(priv *ecdsa.PrivateKey, passphrase string) (Account, error) { func (am *Manager) ImportECDSA(priv *ecdsa.PrivateKey, passphrase string) (Account, error) {
return am.importKey(newKeyFromECDSA(priv), passphrase) key := newKeyFromECDSA(priv)
if am.cache.hasAddress(key.Address) {
return Account{}, fmt.Errorf("account already exists")
}
return am.importKey(key, passphrase)
} }
func (am *Manager) importKey(key *Key, passphrase string) (Account, error) { func (am *Manager) importKey(key *Key, passphrase string) (Account, error) {

@ -18,6 +18,7 @@ package eth
import ( import (
"bytes" "bytes"
"encoding/hex"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
@ -439,6 +440,16 @@ func (s *PrivateAccountAPI) NewAccount(password string) (common.Address, error)
return common.Address{}, err return common.Address{}, err
} }
func (s *PrivateAccountAPI) ImportRawKey(privkey string, password string) (common.Address, error) {
hexkey, err := hex.DecodeString(privkey)
if err != nil {
return common.Address{}, err
}
acc, err := s.am.ImportECDSA(crypto.ToECDSA(hexkey), password)
return acc.Address, err
}
// UnlockAccount will unlock the account associated with the given address with // UnlockAccount will unlock the account associated with the given address with
// the given password for duration seconds. If duration is nil it will use a // the given password for duration seconds. If duration is nil it will use a
// default of 300 seconds. It returns an indication if the account was unlocked. // default of 300 seconds. It returns an indication if the account was unlocked.

@ -18,12 +18,13 @@
package web3ext package web3ext
var Modules = map[string]string{ var Modules = map[string]string{
"txpool": TxPool_JS, "txpool": TxPool_JS,
"admin": Admin_JS, "admin": Admin_JS,
"eth": Eth_JS, "personal": Personal_JS,
"miner": Miner_JS, "eth": Eth_JS,
"debug": Debug_JS, "miner": Miner_JS,
"net": Net_JS, "debug": Debug_JS,
"net": Net_JS,
} }
const TxPool_JS = ` const TxPool_JS = `
@ -175,6 +176,20 @@ web3._extend({
}); });
` `
const Personal_JS = `
web3._extend({
property: 'personal',
methods:
[
new web3._extend.Method({
name: 'importRawKey',
call: 'personal_importRawKey',
params: 2
})
]
});
`
const Eth_JS = ` const Eth_JS = `
web3._extend({ web3._extend({
property: 'eth', property: 'eth',

Loading…
Cancel
Save