2016-09-05 09:07:57 -07:00
|
|
|
// Copyright 2016 The go-ethereum Authors
|
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
// Contains all the wrappers from the accounts package to support client side key
|
|
|
|
// management on mobile platforms.
|
|
|
|
|
|
|
|
package geth
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/accounts"
|
2017-01-24 01:49:20 -08:00
|
|
|
"github.com/ethereum/go-ethereum/accounts/keystore"
|
2016-09-05 09:07:57 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// StandardScryptN is the N parameter of Scrypt encryption algorithm, using 256MB
|
|
|
|
// memory and taking approximately 1s CPU time on a modern processor.
|
2017-01-24 01:49:20 -08:00
|
|
|
StandardScryptN = int(keystore.StandardScryptN)
|
2016-09-05 09:07:57 -07:00
|
|
|
|
|
|
|
// StandardScryptP is the P parameter of Scrypt encryption algorithm, using 256MB
|
|
|
|
// memory and taking approximately 1s CPU time on a modern processor.
|
2017-01-24 01:49:20 -08:00
|
|
|
StandardScryptP = int(keystore.StandardScryptP)
|
2016-09-05 09:07:57 -07:00
|
|
|
|
|
|
|
// LightScryptN is the N parameter of Scrypt encryption algorithm, using 4MB
|
|
|
|
// memory and taking approximately 100ms CPU time on a modern processor.
|
2017-01-24 01:49:20 -08:00
|
|
|
LightScryptN = int(keystore.LightScryptN)
|
2016-09-05 09:07:57 -07:00
|
|
|
|
|
|
|
// LightScryptP is the P parameter of Scrypt encryption algorithm, using 4MB
|
|
|
|
// memory and taking approximately 100ms CPU time on a modern processor.
|
2017-01-24 01:49:20 -08:00
|
|
|
LightScryptP = int(keystore.LightScryptP)
|
2016-09-05 09:07:57 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
// Account represents a stored key.
|
|
|
|
type Account struct{ account accounts.Account }
|
|
|
|
|
|
|
|
// Accounts represents a slice of accounts.
|
|
|
|
type Accounts struct{ accounts []accounts.Account }
|
|
|
|
|
|
|
|
// Size returns the number of accounts in the slice.
|
|
|
|
func (a *Accounts) Size() int {
|
|
|
|
return len(a.accounts)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get returns the account at the given index from the slice.
|
2016-12-08 04:09:26 -08:00
|
|
|
func (a *Accounts) Get(index int) (account *Account, _ error) {
|
2016-09-05 09:07:57 -07:00
|
|
|
if index < 0 || index >= len(a.accounts) {
|
|
|
|
return nil, errors.New("index out of bounds")
|
|
|
|
}
|
|
|
|
return &Account{a.accounts[index]}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set sets the account at the given index in the slice.
|
|
|
|
func (a *Accounts) Set(index int, account *Account) error {
|
|
|
|
if index < 0 || index >= len(a.accounts) {
|
|
|
|
return errors.New("index out of bounds")
|
|
|
|
}
|
|
|
|
a.accounts[index] = account.account
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetAddress retrieves the address associated with the account.
|
|
|
|
func (a *Account) GetAddress() *Address {
|
|
|
|
return &Address{a.account.Address}
|
|
|
|
}
|
|
|
|
|
2017-02-08 05:53:02 -08:00
|
|
|
// GetURL retrieves the canonical URL of the account.
|
|
|
|
func (a *Account) GetURL() string {
|
|
|
|
return a.account.URL.String()
|
2016-09-05 09:07:57 -07:00
|
|
|
}
|
|
|
|
|
2017-01-24 01:49:20 -08:00
|
|
|
// KeyStore manages a key storage directory on disk.
|
|
|
|
type KeyStore struct{ keystore *keystore.KeyStore }
|
2016-09-05 09:07:57 -07:00
|
|
|
|
2017-01-24 01:49:20 -08:00
|
|
|
// NewKeyStore creates a keystore for the given directory.
|
|
|
|
func NewKeyStore(keydir string, scryptN, scryptP int) *KeyStore {
|
|
|
|
return &KeyStore{keystore: keystore.NewKeyStore(keydir, scryptN, scryptP)}
|
2016-09-05 09:07:57 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// HasAddress reports whether a key with the given address is present.
|
2017-01-24 01:49:20 -08:00
|
|
|
func (ks *KeyStore) HasAddress(address *Address) bool {
|
|
|
|
return ks.keystore.HasAddress(address.address)
|
2016-09-05 09:07:57 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetAccounts returns all key files present in the directory.
|
2017-01-24 01:49:20 -08:00
|
|
|
func (ks *KeyStore) GetAccounts() *Accounts {
|
|
|
|
return &Accounts{ks.keystore.Accounts()}
|
2016-09-05 09:07:57 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteAccount deletes the key matched by account if the passphrase is correct.
|
|
|
|
// If a contains no filename, the address must match a unique key.
|
2017-01-24 01:49:20 -08:00
|
|
|
func (ks *KeyStore) DeleteAccount(account *Account, passphrase string) error {
|
|
|
|
return ks.keystore.Delete(account.account, passphrase)
|
2016-09-05 09:07:57 -07:00
|
|
|
}
|
|
|
|
|
2017-01-24 01:49:20 -08:00
|
|
|
// SignHash calculates a ECDSA signature for the given hash. The produced signature
|
2016-12-08 04:42:31 -08:00
|
|
|
// is in the [R || S || V] format where V is 0 or 1.
|
2017-01-24 01:49:20 -08:00
|
|
|
func (ks *KeyStore) SignHash(address *Address, hash []byte) (signature []byte, _ error) {
|
|
|
|
return ks.keystore.SignHash(accounts.Account{Address: address.address}, hash)
|
2016-09-05 09:07:57 -07:00
|
|
|
}
|
|
|
|
|
2017-01-24 01:49:20 -08:00
|
|
|
// SignTx signs the given transaction with the requested account.
|
|
|
|
func (ks *KeyStore) SignTx(account *Account, tx *Transaction, chainID *BigInt) (*Transaction, error) {
|
2017-05-12 17:00:39 -07:00
|
|
|
if chainID == nil { // Null passed from mobile app
|
|
|
|
chainID = new(BigInt)
|
|
|
|
}
|
2017-01-24 01:49:20 -08:00
|
|
|
signed, err := ks.keystore.SignTx(account.account, tx.tx, chainID.bigint)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &Transaction{signed}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SignHashPassphrase signs hash if the private key matching the given address can
|
2017-01-06 07:47:15 -08:00
|
|
|
// be decrypted with the given passphrase. The produced signature is in the
|
2016-12-08 04:42:31 -08:00
|
|
|
// [R || S || V] format where V is 0 or 1.
|
2017-01-24 01:49:20 -08:00
|
|
|
func (ks *KeyStore) SignHashPassphrase(account *Account, passphrase string, hash []byte) (signature []byte, _ error) {
|
|
|
|
return ks.keystore.SignHashWithPassphrase(account.account, passphrase, hash)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SignTxPassphrase signs the transaction if the private key matching the
|
|
|
|
// given address can be decrypted with the given passphrase.
|
|
|
|
func (ks *KeyStore) SignTxPassphrase(account *Account, passphrase string, tx *Transaction, chainID *BigInt) (*Transaction, error) {
|
2017-05-12 17:00:39 -07:00
|
|
|
if chainID == nil { // Null passed from mobile app
|
|
|
|
chainID = new(BigInt)
|
|
|
|
}
|
2017-01-24 01:49:20 -08:00
|
|
|
signed, err := ks.keystore.SignTxWithPassphrase(account.account, passphrase, tx.tx, chainID.bigint)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &Transaction{signed}, nil
|
2016-09-05 09:07:57 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Unlock unlocks the given account indefinitely.
|
2017-01-24 01:49:20 -08:00
|
|
|
func (ks *KeyStore) Unlock(account *Account, passphrase string) error {
|
|
|
|
return ks.keystore.TimedUnlock(account.account, passphrase, 0)
|
2016-09-05 09:07:57 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Lock removes the private key with the given address from memory.
|
2017-01-24 01:49:20 -08:00
|
|
|
func (ks *KeyStore) Lock(address *Address) error {
|
|
|
|
return ks.keystore.Lock(address.address)
|
2016-09-05 09:07:57 -07:00
|
|
|
}
|
|
|
|
|
2016-12-08 04:42:31 -08:00
|
|
|
// TimedUnlock unlocks the given account with the passphrase. The account stays
|
|
|
|
// unlocked for the duration of timeout (nanoseconds). A timeout of 0 unlocks the
|
|
|
|
// account until the program exits. The account must match a unique key file.
|
2016-09-05 09:07:57 -07:00
|
|
|
//
|
|
|
|
// If the account address is already unlocked for a duration, TimedUnlock extends or
|
|
|
|
// shortens the active unlock timeout. If the address was previously unlocked
|
|
|
|
// indefinitely the timeout is not altered.
|
2017-01-24 01:49:20 -08:00
|
|
|
func (ks *KeyStore) TimedUnlock(account *Account, passphrase string, timeout int64) error {
|
|
|
|
return ks.keystore.TimedUnlock(account.account, passphrase, time.Duration(timeout))
|
2016-09-05 09:07:57 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewAccount generates a new key and stores it into the key directory,
|
|
|
|
// encrypting it with the passphrase.
|
2017-01-24 01:49:20 -08:00
|
|
|
func (ks *KeyStore) NewAccount(passphrase string) (*Account, error) {
|
|
|
|
account, err := ks.keystore.NewAccount(passphrase)
|
2016-09-05 09:07:57 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &Account{account}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ExportKey exports as a JSON key, encrypted with newPassphrase.
|
2017-01-24 01:49:20 -08:00
|
|
|
func (ks *KeyStore) ExportKey(account *Account, passphrase, newPassphrase string) (key []byte, _ error) {
|
|
|
|
return ks.keystore.Export(account.account, passphrase, newPassphrase)
|
2016-09-05 09:07:57 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// ImportKey stores the given encrypted JSON key into the key directory.
|
2017-01-24 01:49:20 -08:00
|
|
|
func (ks *KeyStore) ImportKey(keyJSON []byte, passphrase, newPassphrase string) (account *Account, _ error) {
|
|
|
|
acc, err := ks.keystore.Import(keyJSON, passphrase, newPassphrase)
|
2016-09-05 09:07:57 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-12-08 04:09:26 -08:00
|
|
|
return &Account{acc}, nil
|
2016-09-05 09:07:57 -07:00
|
|
|
}
|
|
|
|
|
2016-12-08 04:42:31 -08:00
|
|
|
// UpdateAccount changes the passphrase of an existing account.
|
2017-01-24 01:49:20 -08:00
|
|
|
func (ks *KeyStore) UpdateAccount(account *Account, passphrase, newPassphrase string) error {
|
|
|
|
return ks.keystore.Update(account.account, passphrase, newPassphrase)
|
2016-09-05 09:07:57 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// ImportPreSaleKey decrypts the given Ethereum presale wallet and stores
|
|
|
|
// a key file in the key directory. The key file is encrypted with the same passphrase.
|
2017-01-24 01:49:20 -08:00
|
|
|
func (ks *KeyStore) ImportPreSaleKey(keyJSON []byte, passphrase string) (ccount *Account, _ error) {
|
|
|
|
account, err := ks.keystore.ImportPreSaleKey(keyJSON, passphrase)
|
2016-09-05 09:07:57 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &Account{account}, nil
|
|
|
|
}
|