2016-09-11 11:44:14 +00:00
|
|
|
package geth
|
2016-07-27 11:47:41 +00:00
|
|
|
|
2016-06-15 19:50:35 +00:00
|
|
|
import (
|
2016-06-20 15:21:45 +00:00
|
|
|
"errors"
|
2016-06-15 19:50:35 +00:00
|
|
|
"fmt"
|
|
|
|
|
2016-09-27 13:51:08 +00:00
|
|
|
"github.com/ethereum/go-ethereum/accounts"
|
2016-06-22 18:56:27 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2016-06-21 14:34:38 +00:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2016-09-11 11:44:14 +00:00
|
|
|
"github.com/status-im/status-go/extkeys"
|
2016-06-15 19:50:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2016-08-21 06:45:59 +00:00
|
|
|
ErrAddressToAccountMappingFailure = errors.New("cannot retreive a valid account for a given address")
|
|
|
|
ErrAccountToKeyMappingFailure = errors.New("cannot retreive a valid key for a given account")
|
2016-08-23 21:32:04 +00:00
|
|
|
ErrUnlockCalled = errors.New("no need to unlock accounts, login instead")
|
2016-08-21 06:45:59 +00:00
|
|
|
ErrWhisperIdentityInjectionFailure = errors.New("failed to inject identity into Whisper")
|
2016-08-29 00:31:16 +00:00
|
|
|
ErrWhisperClearIdentitiesFailure = errors.New("failed to clear whisper identities")
|
2016-08-23 21:32:04 +00:00
|
|
|
ErrWhisperNoIdentityFound = errors.New("failed to locate identity previously injected into Whisper")
|
|
|
|
ErrNoAccountSelected = errors.New("no account has been selected, please login")
|
2016-09-11 11:44:14 +00:00
|
|
|
ErrInvalidMasterKeyCreated = errors.New("can not create master extended key")
|
2016-12-07 21:07:08 +00:00
|
|
|
ErrInvalidAccountAddressOrKey = errors.New("cannot parse address or key to valid account address")
|
2016-06-15 19:50:35 +00:00
|
|
|
)
|
|
|
|
|
2016-09-27 13:51:08 +00:00
|
|
|
// CreateAccount creates an internal geth account
|
2016-08-23 21:32:04 +00:00
|
|
|
// BIP44-compatible keys are generated: CKD#1 is stored as account key, CKD#2 stored as sub-account root
|
|
|
|
// Public key of CKD#1 is returned, with CKD#2 securely encoded into account key file (to be used for
|
|
|
|
// sub-account derivations)
|
2016-09-11 11:44:14 +00:00
|
|
|
func CreateAccount(password string) (address, pubKey, mnemonic string, err error) {
|
2016-08-23 21:32:04 +00:00
|
|
|
// generate mnemonic phrase
|
2016-09-11 11:44:14 +00:00
|
|
|
m := extkeys.NewMnemonic(extkeys.Salt)
|
2016-08-23 21:32:04 +00:00
|
|
|
mnemonic, err = m.MnemonicPhrase(128, extkeys.EnglishLanguage)
|
|
|
|
if err != nil {
|
2016-09-11 11:44:14 +00:00
|
|
|
return "", "", "", fmt.Errorf("can not create mnemonic seed: %v", err)
|
2016-08-23 21:32:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// generate extended master key (see BIP32)
|
|
|
|
extKey, err := extkeys.NewMaster(m.MnemonicSeed(mnemonic, password), []byte(extkeys.Salt))
|
|
|
|
if err != nil {
|
2016-09-11 11:44:14 +00:00
|
|
|
return "", "", "", fmt.Errorf("can not create master extended key: %v", err)
|
2016-08-23 21:32:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// import created key into account keystore
|
|
|
|
address, pubKey, err = importExtendedKey(extKey, password)
|
|
|
|
if err != nil {
|
|
|
|
return "", "", "", err
|
|
|
|
}
|
2016-06-29 11:32:04 +00:00
|
|
|
|
2016-08-23 21:32:04 +00:00
|
|
|
return address, pubKey, mnemonic, nil
|
2016-08-18 00:15:58 +00:00
|
|
|
}
|
|
|
|
|
2016-09-27 13:51:08 +00:00
|
|
|
// CreateChildAccount creates sub-account for an account identified by parent address.
|
2016-08-23 21:32:04 +00:00
|
|
|
// CKD#2 is used as root for master accounts (when parentAddress is "").
|
|
|
|
// Otherwise (when parentAddress != ""), child is derived directly from parent.
|
2016-09-11 11:44:14 +00:00
|
|
|
func CreateChildAccount(parentAddress, password string) (address, pubKey string, err error) {
|
2016-12-07 21:07:08 +00:00
|
|
|
nodeManager := NodeManagerInstance()
|
2016-09-11 11:44:14 +00:00
|
|
|
accountManager, err := nodeManager.AccountManager()
|
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
2016-08-23 21:32:04 +00:00
|
|
|
}
|
|
|
|
|
2016-09-27 13:51:08 +00:00
|
|
|
if parentAddress == "" && nodeManager.SelectedAccount != nil { // derive from selected account by default
|
|
|
|
parentAddress = string(nodeManager.SelectedAccount.Address.Hex())
|
2016-08-23 21:32:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if parentAddress == "" {
|
|
|
|
return "", "", ErrNoAccountSelected
|
|
|
|
}
|
|
|
|
|
2016-12-07 21:07:08 +00:00
|
|
|
account, err := ParseAccountString(accountManager, parentAddress)
|
2016-08-23 21:32:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", "", ErrAddressToAccountMappingFailure
|
|
|
|
}
|
|
|
|
|
2016-09-27 13:51:08 +00:00
|
|
|
// make sure that given password can decrypt key associated with a given parent address
|
2016-08-23 21:32:04 +00:00
|
|
|
account, accountKey, err := accountManager.AccountDecryptedKey(account, password)
|
|
|
|
if err != nil {
|
|
|
|
return "", "", fmt.Errorf("%s: %v", ErrAccountToKeyMappingFailure.Error(), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
parentKey, err := extkeys.NewKeyFromString(accountKey.ExtendedKey.String())
|
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
// derive child key
|
|
|
|
childKey, err := parentKey.Child(accountKey.SubAccountIndex)
|
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
accountManager.IncSubAccountIndex(account, password)
|
2016-09-27 13:51:08 +00:00
|
|
|
accountKey.SubAccountIndex++
|
2016-08-18 00:15:58 +00:00
|
|
|
|
2016-08-23 21:32:04 +00:00
|
|
|
// import derived key into account keystore
|
|
|
|
address, pubKey, err = importExtendedKey(childKey, password)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2016-08-18 00:15:58 +00:00
|
|
|
|
2016-09-27 13:51:08 +00:00
|
|
|
// update in-memory selected account
|
|
|
|
if nodeManager.SelectedAccount != nil {
|
|
|
|
nodeManager.SelectedAccount.AccountKey = accountKey
|
|
|
|
}
|
|
|
|
|
2016-08-23 21:32:04 +00:00
|
|
|
return address, pubKey, nil
|
|
|
|
}
|
2016-08-21 06:45:59 +00:00
|
|
|
|
2016-09-27 13:51:08 +00:00
|
|
|
// RecoverAccount re-creates master key using given details.
|
2016-08-23 21:32:04 +00:00
|
|
|
// Once master key is re-generated, it is inserted into keystore (if not already there).
|
2016-09-11 11:44:14 +00:00
|
|
|
func RecoverAccount(password, mnemonic string) (address, pubKey string, err error) {
|
2016-08-23 21:32:04 +00:00
|
|
|
// re-create extended key (see BIP32)
|
2016-09-11 11:44:14 +00:00
|
|
|
m := extkeys.NewMnemonic(extkeys.Salt)
|
2016-08-23 21:32:04 +00:00
|
|
|
extKey, err := extkeys.NewMaster(m.MnemonicSeed(mnemonic, password), []byte(extkeys.Salt))
|
|
|
|
if err != nil {
|
2016-09-11 11:44:14 +00:00
|
|
|
return "", "", ErrInvalidMasterKeyCreated
|
2016-08-23 21:32:04 +00:00
|
|
|
}
|
2016-08-18 00:15:58 +00:00
|
|
|
|
2016-08-23 21:32:04 +00:00
|
|
|
// import re-created key into account keystore
|
|
|
|
address, pubKey, err = importExtendedKey(extKey, password)
|
|
|
|
if err != nil {
|
|
|
|
return
|
2016-08-18 00:15:58 +00:00
|
|
|
}
|
2016-06-15 19:50:35 +00:00
|
|
|
|
2016-08-23 21:32:04 +00:00
|
|
|
return address, pubKey, nil
|
2016-06-15 19:50:35 +00:00
|
|
|
}
|
2016-06-20 15:21:45 +00:00
|
|
|
|
2016-09-27 13:51:08 +00:00
|
|
|
// SelectAccount selects current account, by verifying that address has corresponding account which can be decrypted
|
2016-08-23 21:32:04 +00:00
|
|
|
// using provided password. Once verification is done, decrypted key is injected into Whisper (as a single identity,
|
|
|
|
// all previous identities are removed).
|
2016-09-11 11:44:14 +00:00
|
|
|
func SelectAccount(address, password string) error {
|
2016-12-07 21:07:08 +00:00
|
|
|
nodeManager := NodeManagerInstance()
|
2016-09-11 11:44:14 +00:00
|
|
|
accountManager, err := nodeManager.AccountManager()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2016-08-21 06:45:59 +00:00
|
|
|
}
|
2016-06-21 18:29:38 +00:00
|
|
|
|
2016-12-07 21:07:08 +00:00
|
|
|
account, err := ParseAccountString(accountManager, address)
|
2016-08-21 06:45:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return ErrAddressToAccountMappingFailure
|
|
|
|
}
|
2016-06-21 18:29:38 +00:00
|
|
|
|
2016-08-21 06:45:59 +00:00
|
|
|
account, accountKey, err := accountManager.AccountDecryptedKey(account, password)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("%s: %v", ErrAccountToKeyMappingFailure.Error(), err)
|
|
|
|
}
|
2016-07-04 12:28:49 +00:00
|
|
|
|
2016-09-11 11:44:14 +00:00
|
|
|
whisperService, err := nodeManager.WhisperService()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2016-06-21 18:29:38 +00:00
|
|
|
}
|
2016-09-11 11:44:14 +00:00
|
|
|
|
2016-08-21 06:45:59 +00:00
|
|
|
if err := whisperService.InjectIdentity(accountKey.PrivateKey); err != nil {
|
|
|
|
return ErrWhisperIdentityInjectionFailure
|
|
|
|
}
|
|
|
|
|
2016-09-27 13:51:08 +00:00
|
|
|
// persist account key for easier recovery of currently selected key
|
|
|
|
subAccounts, err := findSubAccounts(accountKey.ExtendedKey, accountKey.SubAccountIndex)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
nodeManager.SelectedAccount = &SelectedExtKey{
|
|
|
|
Address: account.Address,
|
|
|
|
AccountKey: accountKey,
|
|
|
|
SubAccounts: subAccounts,
|
|
|
|
}
|
2016-08-23 21:32:04 +00:00
|
|
|
|
2016-08-21 06:45:59 +00:00
|
|
|
return nil
|
|
|
|
}
|
2016-06-21 18:29:38 +00:00
|
|
|
|
2016-09-27 13:51:08 +00:00
|
|
|
// Logout clears whisper identities
|
2016-09-11 11:44:14 +00:00
|
|
|
func Logout() error {
|
2016-12-07 21:07:08 +00:00
|
|
|
nodeManager := NodeManagerInstance()
|
2016-09-11 11:44:14 +00:00
|
|
|
whisperService, err := nodeManager.WhisperService()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2016-08-29 00:31:16 +00:00
|
|
|
}
|
|
|
|
|
2016-09-11 11:44:14 +00:00
|
|
|
err = whisperService.ClearIdentities()
|
2016-08-29 00:31:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("%s: %v", ErrWhisperClearIdentitiesFailure, err)
|
|
|
|
}
|
|
|
|
|
2016-09-27 13:51:08 +00:00
|
|
|
nodeManager.SelectedAccount = nil
|
2016-08-23 21:32:04 +00:00
|
|
|
|
2016-08-29 00:31:16 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-09-27 13:51:08 +00:00
|
|
|
// UnlockAccount unlocks an existing account for a certain duration and
|
2016-08-21 06:45:59 +00:00
|
|
|
// inject the account as a whisper identity if the account was created as
|
|
|
|
// a whisper enabled account
|
2016-09-11 11:44:14 +00:00
|
|
|
func UnlockAccount(address, password string, seconds int) error {
|
2016-08-21 06:45:59 +00:00
|
|
|
return ErrUnlockCalled
|
2016-06-21 18:29:38 +00:00
|
|
|
}
|
|
|
|
|
2016-08-23 21:32:04 +00:00
|
|
|
// importExtendedKey processes incoming extended key, extracts required info and creates corresponding account key.
|
|
|
|
// Once account key is formed, that key is put (if not already) into keystore i.e. key is *encoded* into key file.
|
|
|
|
func importExtendedKey(extKey *extkeys.ExtendedKey, password string) (address, pubKey string, err error) {
|
2016-12-07 21:07:08 +00:00
|
|
|
accountManager, err := NodeManagerInstance().AccountManager()
|
2016-09-11 11:44:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
|
2016-08-23 21:32:04 +00:00
|
|
|
// imports extended key, create key file (if necessary)
|
|
|
|
account, err := accountManager.ImportExtendedKey(extKey, password)
|
|
|
|
if err != nil {
|
2016-09-11 11:44:14 +00:00
|
|
|
return "", "", err
|
2016-08-23 21:32:04 +00:00
|
|
|
}
|
|
|
|
address = fmt.Sprintf("%x", account.Address)
|
|
|
|
|
|
|
|
// obtain public key to return
|
|
|
|
account, key, err := accountManager.AccountDecryptedKey(account, password)
|
|
|
|
if err != nil {
|
2016-09-11 11:44:14 +00:00
|
|
|
return address, "", err
|
2016-08-23 21:32:04 +00:00
|
|
|
}
|
|
|
|
pubKey = common.ToHex(crypto.FromECDSAPub(&key.PrivateKey.PublicKey))
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2016-09-27 13:51:08 +00:00
|
|
|
|
|
|
|
func onAccountsListRequest(entities []accounts.Account) []accounts.Account {
|
2016-12-07 21:07:08 +00:00
|
|
|
nodeManager := NodeManagerInstance()
|
2016-09-27 13:51:08 +00:00
|
|
|
|
|
|
|
if nodeManager.SelectedAccount == nil {
|
|
|
|
return []accounts.Account{}
|
|
|
|
}
|
|
|
|
|
|
|
|
refreshSelectedAccount()
|
|
|
|
|
|
|
|
filtered := make([]accounts.Account, 0)
|
|
|
|
for _, account := range entities {
|
|
|
|
// main account
|
|
|
|
if nodeManager.SelectedAccount.Address.Hex() == account.Address.Hex() {
|
|
|
|
filtered = append(filtered, account)
|
|
|
|
} else {
|
|
|
|
// sub accounts
|
|
|
|
for _, subAccount := range nodeManager.SelectedAccount.SubAccounts {
|
|
|
|
if subAccount.Address.Hex() == account.Address.Hex() {
|
|
|
|
filtered = append(filtered, account)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return filtered
|
|
|
|
}
|
|
|
|
|
|
|
|
// refreshSelectedAccount re-populates list of sub-accounts of the currently selected account (if any)
|
|
|
|
func refreshSelectedAccount() {
|
2016-12-07 21:07:08 +00:00
|
|
|
nodeManager := NodeManagerInstance()
|
2016-09-27 13:51:08 +00:00
|
|
|
|
|
|
|
if nodeManager.SelectedAccount == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
accountKey := nodeManager.SelectedAccount.AccountKey
|
|
|
|
if accountKey == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// re-populate list of sub-accounts
|
|
|
|
subAccounts, err := findSubAccounts(accountKey.ExtendedKey, accountKey.SubAccountIndex)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
nodeManager.SelectedAccount = &SelectedExtKey{
|
|
|
|
Address: nodeManager.SelectedAccount.Address,
|
|
|
|
AccountKey: nodeManager.SelectedAccount.AccountKey,
|
|
|
|
SubAccounts: subAccounts,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// findSubAccounts traverses cached accounts and adds as a sub-accounts any
|
|
|
|
// that belong to the currently selected account.
|
|
|
|
// The extKey is CKD#2 := root of sub-accounts of the main account
|
|
|
|
func findSubAccounts(extKey *extkeys.ExtendedKey, subAccountIndex uint32) ([]accounts.Account, error) {
|
2016-12-07 21:07:08 +00:00
|
|
|
nodeManager := NodeManagerInstance()
|
2016-09-27 13:51:08 +00:00
|
|
|
accountManager, err := nodeManager.AccountManager()
|
|
|
|
if err != nil {
|
|
|
|
return []accounts.Account{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
subAccounts := make([]accounts.Account, 0)
|
|
|
|
if extKey.Depth == 5 { // CKD#2 level
|
|
|
|
// gather possible sub-account addresses
|
|
|
|
subAccountAddresses := make([]common.Address, 0)
|
|
|
|
for i := uint32(0); i < subAccountIndex; i++ {
|
|
|
|
childKey, err := extKey.Child(i)
|
|
|
|
if err != nil {
|
|
|
|
return []accounts.Account{}, err
|
|
|
|
}
|
|
|
|
subAccountAddresses = append(subAccountAddresses, crypto.PubkeyToAddress(childKey.ToECDSA().PublicKey))
|
|
|
|
}
|
|
|
|
|
|
|
|
// see if any of the gathered addresses actually exist in cached accounts list
|
|
|
|
for _, cachedAccount := range accountManager.Accounts() {
|
|
|
|
for _, possibleAddress := range subAccountAddresses {
|
|
|
|
if possibleAddress.Hex() == cachedAccount.Address.Hex() {
|
|
|
|
subAccounts = append(subAccounts, cachedAccount)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return subAccounts, nil
|
|
|
|
}
|