Skip to content

Commit 572da73

Browse files
committed
eth: add personal_importRawKey for runtime private key import
1 parent 70b8b54 commit 572da73

File tree

3 files changed

+38
-7
lines changed

3 files changed

+38
-7
lines changed

accounts/account_manager.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,12 @@ func (am *Manager) Import(keyJSON []byte, passphrase, newPassphrase string) (Acc
284284

285285
// ImportECDSA stores the given key into the key directory, encrypting it with the passphrase.
286286
func (am *Manager) ImportECDSA(priv *ecdsa.PrivateKey, passphrase string) (Account, error) {
287-
return am.importKey(newKeyFromECDSA(priv), passphrase)
287+
key := newKeyFromECDSA(priv)
288+
if am.cache.hasAddress(key.Address) {
289+
return Account{}, fmt.Errorf("account already exists")
290+
}
291+
292+
return am.importKey(key, passphrase)
288293
}
289294

290295
func (am *Manager) importKey(key *Key, passphrase string) (Account, error) {

eth/api.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package eth
1818

1919
import (
2020
"bytes"
21+
"encoding/hex"
2122
"encoding/json"
2223
"errors"
2324
"fmt"
@@ -439,6 +440,16 @@ func (s *PrivateAccountAPI) NewAccount(password string) (common.Address, error)
439440
return common.Address{}, err
440441
}
441442

443+
func (s *PrivateAccountAPI) ImportRawKey(privkey string, password string) (common.Address, error) {
444+
hexkey, err := hex.DecodeString(privkey)
445+
if err != nil {
446+
return common.Address{}, err
447+
}
448+
449+
acc, err := s.am.ImportECDSA(crypto.ToECDSA(hexkey), password)
450+
return acc.Address, err
451+
}
452+
442453
// UnlockAccount will unlock the account associated with the given address with
443454
// the given password for duration seconds. If duration is nil it will use a
444455
// default of 300 seconds. It returns an indication if the account was unlocked.

internal/web3ext/web3ext.go

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,13 @@
1818
package web3ext
1919

2020
var Modules = map[string]string{
21-
"txpool": TxPool_JS,
22-
"admin": Admin_JS,
23-
"eth": Eth_JS,
24-
"miner": Miner_JS,
25-
"debug": Debug_JS,
26-
"net": Net_JS,
21+
"txpool": TxPool_JS,
22+
"admin": Admin_JS,
23+
"personal": Personal_JS,
24+
"eth": Eth_JS,
25+
"miner": Miner_JS,
26+
"debug": Debug_JS,
27+
"net": Net_JS,
2728
}
2829

2930
const TxPool_JS = `
@@ -175,6 +176,20 @@ web3._extend({
175176
});
176177
`
177178

179+
const Personal_JS = `
180+
web3._extend({
181+
property: 'personal',
182+
methods:
183+
[
184+
new web3._extend.Method({
185+
name: 'importRawKey',
186+
call: 'personal_importRawKey',
187+
params: 2
188+
})
189+
]
190+
});
191+
`
192+
178193
const Eth_JS = `
179194
web3._extend({
180195
property: 'eth',

0 commit comments

Comments
 (0)