2017-02-07 04:47:34 -06:00
|
|
|
// Copyright 2017 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/>.
|
|
|
|
|
|
|
|
package keystore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math/big"
|
|
|
|
|
2017-02-08 12:25:52 -06:00
|
|
|
ethereum "github.com/ethereum/go-ethereum"
|
2017-02-07 04:47:34 -06:00
|
|
|
"github.com/ethereum/go-ethereum/accounts"
|
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
accounts, eth, clique, signer: support for external signer API (#18079)
* accounts, eth, clique: implement external backend + move sighash calc to backend
* signer: implement account_Version on external API
* accounts/external: enable ipc, add copyright
* accounts, internal, signer: formatting
* node: go fmt
* flags: disallow --dev in combo with --externalsigner
* accounts: remove clique-specific signing method, replace with more generic
* accounts, consensus: formatting + fix error in tests
* signer/core: remove (test-) import cycle
* clique: remove unused import
* accounts: remove CliqueHash and avoid dependency on package crypto
* consensus/clique: unduplicate header encoding
2019-02-05 04:23:57 -06:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2017-02-07 04:47:34 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
// keystoreWallet implements the accounts.Wallet interface for the original
|
|
|
|
// keystore.
|
|
|
|
type keystoreWallet struct {
|
|
|
|
account accounts.Account // Single account contained in this wallet
|
|
|
|
keystore *KeyStore // Keystore where the account originates from
|
|
|
|
}
|
|
|
|
|
|
|
|
// URL implements accounts.Wallet, returning the URL of the account within.
|
2017-02-08 07:53:02 -06:00
|
|
|
func (w *keystoreWallet) URL() accounts.URL {
|
2017-02-07 04:47:34 -06:00
|
|
|
return w.account.URL
|
|
|
|
}
|
|
|
|
|
2017-08-09 04:51:16 -05:00
|
|
|
// Status implements accounts.Wallet, returning whether the account held by the
|
|
|
|
// keystore wallet is unlocked or not.
|
|
|
|
func (w *keystoreWallet) Status() (string, error) {
|
2017-02-08 07:53:02 -06:00
|
|
|
w.keystore.mu.RLock()
|
|
|
|
defer w.keystore.mu.RUnlock()
|
|
|
|
|
|
|
|
if _, ok := w.keystore.unlocked[w.account.Address]; ok {
|
2017-08-09 04:51:16 -05:00
|
|
|
return "Unlocked", nil
|
2017-02-08 07:53:02 -06:00
|
|
|
}
|
2017-08-09 04:51:16 -05:00
|
|
|
return "Locked", nil
|
2017-02-07 04:47:34 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Open implements accounts.Wallet, but is a noop for plain wallets since there
|
|
|
|
// is no connection or decryption step necessary to access the list of accounts.
|
|
|
|
func (w *keystoreWallet) Open(passphrase string) error { return nil }
|
|
|
|
|
2019-01-05 14:27:57 -06:00
|
|
|
// Close implements accounts.Wallet, but is a noop for plain wallets since there
|
|
|
|
// is no meaningful open operation.
|
2017-02-07 04:47:34 -06:00
|
|
|
func (w *keystoreWallet) Close() error { return nil }
|
|
|
|
|
|
|
|
// Accounts implements accounts.Wallet, returning an account list consisting of
|
|
|
|
// a single account that the plain kestore wallet contains.
|
|
|
|
func (w *keystoreWallet) Accounts() []accounts.Account {
|
|
|
|
return []accounts.Account{w.account}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Contains implements accounts.Wallet, returning whether a particular account is
|
|
|
|
// or is not wrapped by this wallet instance.
|
|
|
|
func (w *keystoreWallet) Contains(account accounts.Account) bool {
|
2017-02-08 07:53:02 -06:00
|
|
|
return account.Address == w.account.Address && (account.URL == (accounts.URL{}) || account.URL == w.account.URL)
|
2017-02-07 04:47:34 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Derive implements accounts.Wallet, but is a noop for plain wallets since there
|
|
|
|
// is no notion of hierarchical account derivation for plain keystore accounts.
|
2017-02-08 12:25:52 -06:00
|
|
|
func (w *keystoreWallet) Derive(path accounts.DerivationPath, pin bool) (accounts.Account, error) {
|
2017-02-07 04:47:34 -06:00
|
|
|
return accounts.Account{}, accounts.ErrNotSupported
|
|
|
|
}
|
|
|
|
|
2017-02-08 12:25:52 -06:00
|
|
|
// SelfDerive implements accounts.Wallet, but is a noop for plain wallets since
|
|
|
|
// there is no notion of hierarchical account derivation for plain keystore accounts.
|
|
|
|
func (w *keystoreWallet) SelfDerive(base accounts.DerivationPath, chain ethereum.ChainStateReader) {}
|
|
|
|
|
accounts, eth, clique, signer: support for external signer API (#18079)
* accounts, eth, clique: implement external backend + move sighash calc to backend
* signer: implement account_Version on external API
* accounts/external: enable ipc, add copyright
* accounts, internal, signer: formatting
* node: go fmt
* flags: disallow --dev in combo with --externalsigner
* accounts: remove clique-specific signing method, replace with more generic
* accounts, consensus: formatting + fix error in tests
* signer/core: remove (test-) import cycle
* clique: remove unused import
* accounts: remove CliqueHash and avoid dependency on package crypto
* consensus/clique: unduplicate header encoding
2019-02-05 04:23:57 -06:00
|
|
|
// signHash attempts to sign the given hash with
|
2017-02-07 04:47:34 -06:00
|
|
|
// the given account. If the wallet does not wrap this particular account, an
|
|
|
|
// error is returned to avoid account leakage (even though in theory we may be
|
|
|
|
// able to sign via our shared keystore backend).
|
accounts, eth, clique, signer: support for external signer API (#18079)
* accounts, eth, clique: implement external backend + move sighash calc to backend
* signer: implement account_Version on external API
* accounts/external: enable ipc, add copyright
* accounts, internal, signer: formatting
* node: go fmt
* flags: disallow --dev in combo with --externalsigner
* accounts: remove clique-specific signing method, replace with more generic
* accounts, consensus: formatting + fix error in tests
* signer/core: remove (test-) import cycle
* clique: remove unused import
* accounts: remove CliqueHash and avoid dependency on package crypto
* consensus/clique: unduplicate header encoding
2019-02-05 04:23:57 -06:00
|
|
|
func (w *keystoreWallet) signHash(account accounts.Account, hash []byte) ([]byte, error) {
|
2017-02-07 04:47:34 -06:00
|
|
|
// Make sure the requested account is contained within
|
2019-01-07 02:35:44 -06:00
|
|
|
if !w.Contains(account) {
|
2017-02-07 04:47:34 -06:00
|
|
|
return nil, accounts.ErrUnknownAccount
|
|
|
|
}
|
|
|
|
// Account seems valid, request the keystore to sign
|
|
|
|
return w.keystore.SignHash(account, hash)
|
|
|
|
}
|
|
|
|
|
accounts, eth, clique, signer: support for external signer API (#18079)
* accounts, eth, clique: implement external backend + move sighash calc to backend
* signer: implement account_Version on external API
* accounts/external: enable ipc, add copyright
* accounts, internal, signer: formatting
* node: go fmt
* flags: disallow --dev in combo with --externalsigner
* accounts: remove clique-specific signing method, replace with more generic
* accounts, consensus: formatting + fix error in tests
* signer/core: remove (test-) import cycle
* clique: remove unused import
* accounts: remove CliqueHash and avoid dependency on package crypto
* consensus/clique: unduplicate header encoding
2019-02-05 04:23:57 -06:00
|
|
|
// SignData signs keccak256(data). The mimetype parameter describes the type of data being signed
|
|
|
|
func (w *keystoreWallet) SignData(account accounts.Account, mimeType string, data []byte) ([]byte, error) {
|
|
|
|
return w.signHash(account, crypto.Keccak256(data))
|
|
|
|
}
|
|
|
|
|
signer, clef: implement EIP191/712 (#17789)
* Named functions and defined a basic EIP191 content type list
* Written basic content type functions
* Added ecRecover method in the clef api
* Updated the extapi changelog and addded indications in the README
* Changed the version of the external API
* Added tests for 0x45
* Implementing UnmarshalJSON() for TypedData
* Working on TypedData
* Solved the auditlog issue
* Changed method to signTypedData
* Changed mimes and implemented the 'encodeType' function for EIP-712
* Polished docstrings, ran goimports and swapped fmt.Errorf with errors.New where possible
* Drafted recursive encodeData
* Ran goimports and gofmt
* Drafted first version of EIP-712, including tests
* Temporarily switched to using common.Address in tests
* Drafted text/validator and and rewritten []byte as hexutil.Bytes
* Solved stringified address encoding issue
* Changed the property type required by signData from bytes to interface{}
* Fixed bugs in 'data/typed' signs
* Brought legal warning back after temporarily disabling it for development
* Added example RPC calls for account_signData and account_signTypedData
* Named functions and defined a basic EIP191 content type list
* Written basic content type functions
* Added ecRecover method in the clef api
* Updated the extapi changelog and addded indications in the README
* Added tests for 0x45
* Implementing UnmarshalJSON() for TypedData
* Working on TypedData
* Solved the auditlog issue
* Changed method to signTypedData
* Changed mimes and implemented the 'encodeType' function for EIP-712
* Polished docstrings, ran goimports and swapped fmt.Errorf with errors.New where possible
* Drafted recursive encodeData
* Ran goimports and gofmt
* Drafted first version of EIP-712, including tests
* Temporarily switched to using common.Address in tests
* Drafted text/validator and and rewritten []byte as hexutil.Bytes
* Solved stringified address encoding issue
* Changed the property type required by signData from bytes to interface{}
* Fixed bugs in 'data/typed' signs
* Brought legal warning back after temporarily disabling it for development
* Added example RPC calls for account_signData and account_signTypedData
* Polished and fixed PR
* Polished and fixed PR
* Solved malformed data panics and also wrote tests
* Solved malformed data panics and also wrote tests
* Added alphabetical sorting to type dependencies
* Added alphabetical sorting to type dependencies
* Added pretty print to data/typed UI
* Added pretty print to data/typed UI
* signer: more tests for typed data
* signer: more tests for typed data
* Fixed TestMalformedData4 errors and renamed IsValid to Validate
* Fixed TestMalformedData4 errors and renamed IsValid to Validate
* Fixed more new failing tests and deanonymised some functions
* Fixed more new failing tests and deanonymised some functions
* Added types to EIP712 output in cliui
* Added types to EIP712 output in cliui
* Fixed regexp issues
* Fixed regexp issues
* Added pseudo-failing test
* Added pseudo-failing test
* Fixed false positive test
* Fixed false positive test
* Added PrettyPrint method
* Added PrettyPrint method
* signer: refactor formatting and UI
* signer: make ui use new message format for signing
* Fixed breaking changes
* Fixed rules_test failing test
* Added extra regexp for reference types
* signer: more hard types
* Fixed failing test, formatted files
* signer: use golang/x keccak
* Fixed goimports error
* clef, signer: address some review concerns
* Implemented latest recommendations
* Fixed comments and uintint256 issue
* accounts, signer: fix mimetypes, add interface to sign data with passphrase
* signer, accounts: remove duplicated code, pass hash preimages to signing
* signer: prevent panic in type assertions, make cliui print rawdata as quotable-safe
* signer: linter fixes, remove deprecated crypto dependency
* accounts: fix goimport
2019-02-06 01:30:49 -06:00
|
|
|
// SignDataWithPassphrase signs keccak256(data). The mimetype parameter describes the type of data being signed
|
|
|
|
func (w *keystoreWallet) SignDataWithPassphrase(account accounts.Account, passphrase, mimeType string, data []byte) ([]byte, error) {
|
2017-02-07 04:47:34 -06:00
|
|
|
// Make sure the requested account is contained within
|
2019-01-07 02:35:44 -06:00
|
|
|
if !w.Contains(account) {
|
2017-02-07 04:47:34 -06:00
|
|
|
return nil, accounts.ErrUnknownAccount
|
|
|
|
}
|
|
|
|
// Account seems valid, request the keystore to sign
|
signer, clef: implement EIP191/712 (#17789)
* Named functions and defined a basic EIP191 content type list
* Written basic content type functions
* Added ecRecover method in the clef api
* Updated the extapi changelog and addded indications in the README
* Changed the version of the external API
* Added tests for 0x45
* Implementing UnmarshalJSON() for TypedData
* Working on TypedData
* Solved the auditlog issue
* Changed method to signTypedData
* Changed mimes and implemented the 'encodeType' function for EIP-712
* Polished docstrings, ran goimports and swapped fmt.Errorf with errors.New where possible
* Drafted recursive encodeData
* Ran goimports and gofmt
* Drafted first version of EIP-712, including tests
* Temporarily switched to using common.Address in tests
* Drafted text/validator and and rewritten []byte as hexutil.Bytes
* Solved stringified address encoding issue
* Changed the property type required by signData from bytes to interface{}
* Fixed bugs in 'data/typed' signs
* Brought legal warning back after temporarily disabling it for development
* Added example RPC calls for account_signData and account_signTypedData
* Named functions and defined a basic EIP191 content type list
* Written basic content type functions
* Added ecRecover method in the clef api
* Updated the extapi changelog and addded indications in the README
* Added tests for 0x45
* Implementing UnmarshalJSON() for TypedData
* Working on TypedData
* Solved the auditlog issue
* Changed method to signTypedData
* Changed mimes and implemented the 'encodeType' function for EIP-712
* Polished docstrings, ran goimports and swapped fmt.Errorf with errors.New where possible
* Drafted recursive encodeData
* Ran goimports and gofmt
* Drafted first version of EIP-712, including tests
* Temporarily switched to using common.Address in tests
* Drafted text/validator and and rewritten []byte as hexutil.Bytes
* Solved stringified address encoding issue
* Changed the property type required by signData from bytes to interface{}
* Fixed bugs in 'data/typed' signs
* Brought legal warning back after temporarily disabling it for development
* Added example RPC calls for account_signData and account_signTypedData
* Polished and fixed PR
* Polished and fixed PR
* Solved malformed data panics and also wrote tests
* Solved malformed data panics and also wrote tests
* Added alphabetical sorting to type dependencies
* Added alphabetical sorting to type dependencies
* Added pretty print to data/typed UI
* Added pretty print to data/typed UI
* signer: more tests for typed data
* signer: more tests for typed data
* Fixed TestMalformedData4 errors and renamed IsValid to Validate
* Fixed TestMalformedData4 errors and renamed IsValid to Validate
* Fixed more new failing tests and deanonymised some functions
* Fixed more new failing tests and deanonymised some functions
* Added types to EIP712 output in cliui
* Added types to EIP712 output in cliui
* Fixed regexp issues
* Fixed regexp issues
* Added pseudo-failing test
* Added pseudo-failing test
* Fixed false positive test
* Fixed false positive test
* Added PrettyPrint method
* Added PrettyPrint method
* signer: refactor formatting and UI
* signer: make ui use new message format for signing
* Fixed breaking changes
* Fixed rules_test failing test
* Added extra regexp for reference types
* signer: more hard types
* Fixed failing test, formatted files
* signer: use golang/x keccak
* Fixed goimports error
* clef, signer: address some review concerns
* Implemented latest recommendations
* Fixed comments and uintint256 issue
* accounts, signer: fix mimetypes, add interface to sign data with passphrase
* signer, accounts: remove duplicated code, pass hash preimages to signing
* signer: prevent panic in type assertions, make cliui print rawdata as quotable-safe
* signer: linter fixes, remove deprecated crypto dependency
* accounts: fix goimport
2019-02-06 01:30:49 -06:00
|
|
|
return w.keystore.SignHashWithPassphrase(account, passphrase, crypto.Keccak256(data))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *keystoreWallet) SignText(account accounts.Account, text []byte) ([]byte, error) {
|
|
|
|
return w.signHash(account, accounts.TextHash(text))
|
2017-02-07 04:47:34 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// SignHashWithPassphrase implements accounts.Wallet, attempting to sign the
|
|
|
|
// given hash with the given account using passphrase as extra authentication.
|
accounts, eth, clique, signer: support for external signer API (#18079)
* accounts, eth, clique: implement external backend + move sighash calc to backend
* signer: implement account_Version on external API
* accounts/external: enable ipc, add copyright
* accounts, internal, signer: formatting
* node: go fmt
* flags: disallow --dev in combo with --externalsigner
* accounts: remove clique-specific signing method, replace with more generic
* accounts, consensus: formatting + fix error in tests
* signer/core: remove (test-) import cycle
* clique: remove unused import
* accounts: remove CliqueHash and avoid dependency on package crypto
* consensus/clique: unduplicate header encoding
2019-02-05 04:23:57 -06:00
|
|
|
func (w *keystoreWallet) SignTextWithPassphrase(account accounts.Account, passphrase string, text []byte) ([]byte, error) {
|
2017-02-07 04:47:34 -06:00
|
|
|
// Make sure the requested account is contained within
|
2019-01-07 02:35:44 -06:00
|
|
|
if !w.Contains(account) {
|
2017-02-07 04:47:34 -06:00
|
|
|
return nil, accounts.ErrUnknownAccount
|
|
|
|
}
|
|
|
|
// Account seems valid, request the keystore to sign
|
accounts, eth, clique, signer: support for external signer API (#18079)
* accounts, eth, clique: implement external backend + move sighash calc to backend
* signer: implement account_Version on external API
* accounts/external: enable ipc, add copyright
* accounts, internal, signer: formatting
* node: go fmt
* flags: disallow --dev in combo with --externalsigner
* accounts: remove clique-specific signing method, replace with more generic
* accounts, consensus: formatting + fix error in tests
* signer/core: remove (test-) import cycle
* clique: remove unused import
* accounts: remove CliqueHash and avoid dependency on package crypto
* consensus/clique: unduplicate header encoding
2019-02-05 04:23:57 -06:00
|
|
|
return w.keystore.SignHashWithPassphrase(account, passphrase, accounts.TextHash(text))
|
2017-02-07 04:47:34 -06:00
|
|
|
}
|
|
|
|
|
signer, clef: implement EIP191/712 (#17789)
* Named functions and defined a basic EIP191 content type list
* Written basic content type functions
* Added ecRecover method in the clef api
* Updated the extapi changelog and addded indications in the README
* Changed the version of the external API
* Added tests for 0x45
* Implementing UnmarshalJSON() for TypedData
* Working on TypedData
* Solved the auditlog issue
* Changed method to signTypedData
* Changed mimes and implemented the 'encodeType' function for EIP-712
* Polished docstrings, ran goimports and swapped fmt.Errorf with errors.New where possible
* Drafted recursive encodeData
* Ran goimports and gofmt
* Drafted first version of EIP-712, including tests
* Temporarily switched to using common.Address in tests
* Drafted text/validator and and rewritten []byte as hexutil.Bytes
* Solved stringified address encoding issue
* Changed the property type required by signData from bytes to interface{}
* Fixed bugs in 'data/typed' signs
* Brought legal warning back after temporarily disabling it for development
* Added example RPC calls for account_signData and account_signTypedData
* Named functions and defined a basic EIP191 content type list
* Written basic content type functions
* Added ecRecover method in the clef api
* Updated the extapi changelog and addded indications in the README
* Added tests for 0x45
* Implementing UnmarshalJSON() for TypedData
* Working on TypedData
* Solved the auditlog issue
* Changed method to signTypedData
* Changed mimes and implemented the 'encodeType' function for EIP-712
* Polished docstrings, ran goimports and swapped fmt.Errorf with errors.New where possible
* Drafted recursive encodeData
* Ran goimports and gofmt
* Drafted first version of EIP-712, including tests
* Temporarily switched to using common.Address in tests
* Drafted text/validator and and rewritten []byte as hexutil.Bytes
* Solved stringified address encoding issue
* Changed the property type required by signData from bytes to interface{}
* Fixed bugs in 'data/typed' signs
* Brought legal warning back after temporarily disabling it for development
* Added example RPC calls for account_signData and account_signTypedData
* Polished and fixed PR
* Polished and fixed PR
* Solved malformed data panics and also wrote tests
* Solved malformed data panics and also wrote tests
* Added alphabetical sorting to type dependencies
* Added alphabetical sorting to type dependencies
* Added pretty print to data/typed UI
* Added pretty print to data/typed UI
* signer: more tests for typed data
* signer: more tests for typed data
* Fixed TestMalformedData4 errors and renamed IsValid to Validate
* Fixed TestMalformedData4 errors and renamed IsValid to Validate
* Fixed more new failing tests and deanonymised some functions
* Fixed more new failing tests and deanonymised some functions
* Added types to EIP712 output in cliui
* Added types to EIP712 output in cliui
* Fixed regexp issues
* Fixed regexp issues
* Added pseudo-failing test
* Added pseudo-failing test
* Fixed false positive test
* Fixed false positive test
* Added PrettyPrint method
* Added PrettyPrint method
* signer: refactor formatting and UI
* signer: make ui use new message format for signing
* Fixed breaking changes
* Fixed rules_test failing test
* Added extra regexp for reference types
* signer: more hard types
* Fixed failing test, formatted files
* signer: use golang/x keccak
* Fixed goimports error
* clef, signer: address some review concerns
* Implemented latest recommendations
* Fixed comments and uintint256 issue
* accounts, signer: fix mimetypes, add interface to sign data with passphrase
* signer, accounts: remove duplicated code, pass hash preimages to signing
* signer: prevent panic in type assertions, make cliui print rawdata as quotable-safe
* signer: linter fixes, remove deprecated crypto dependency
* accounts: fix goimport
2019-02-06 01:30:49 -06:00
|
|
|
// SignTx implements accounts.Wallet, attempting to sign the given transaction
|
|
|
|
// with the given account. If the wallet does not wrap this particular account,
|
|
|
|
// an error is returned to avoid account leakage (even though in theory we may
|
|
|
|
// be able to sign via our shared keystore backend).
|
|
|
|
func (w *keystoreWallet) SignTx(account accounts.Account, tx *types.Transaction, chainID *big.Int) (*types.Transaction, error) {
|
|
|
|
// Make sure the requested account is contained within
|
|
|
|
if !w.Contains(account) {
|
|
|
|
return nil, accounts.ErrUnknownAccount
|
|
|
|
}
|
|
|
|
// Account seems valid, request the keystore to sign
|
|
|
|
return w.keystore.SignTx(account, tx, chainID)
|
|
|
|
}
|
|
|
|
|
2017-02-07 04:47:34 -06:00
|
|
|
// SignTxWithPassphrase implements accounts.Wallet, attempting to sign the given
|
|
|
|
// transaction with the given account using passphrase as extra authentication.
|
|
|
|
func (w *keystoreWallet) SignTxWithPassphrase(account accounts.Account, passphrase string, tx *types.Transaction, chainID *big.Int) (*types.Transaction, error) {
|
|
|
|
// Make sure the requested account is contained within
|
2019-01-07 02:35:44 -06:00
|
|
|
if !w.Contains(account) {
|
2017-02-07 04:47:34 -06:00
|
|
|
return nil, accounts.ErrUnknownAccount
|
|
|
|
}
|
|
|
|
// Account seems valid, request the keystore to sign
|
|
|
|
return w.keystore.SignTxWithPassphrase(account, passphrase, tx, chainID)
|
|
|
|
}
|