From cf930b9aaaded6be067ff2ee926e2862978aa139 Mon Sep 17 00:00:00 2001 From: Huiyi Li Date: Wed, 1 Apr 2020 14:29:43 -0700 Subject: [PATCH] signer,accounts,cmd add logic for supporting database backed clef --- accounts/keystore/keystore.go | 6 ++++-- cmd/clef/main.go | 10 +++++++++- signer/core/api.go | 29 +++++++++++++++++++++++++---- signer/core/api_test.go | 5 ++++- 4 files changed, 42 insertions(+), 8 deletions(-) diff --git a/accounts/keystore/keystore.go b/accounts/keystore/keystore.go index 6a14bfad9d..4ab30848d0 100644 --- a/accounts/keystore/keystore.go +++ b/accounts/keystore/keystore.go @@ -23,6 +23,8 @@ var ( // KeyStoreScheme is the protocol scheme prefixing account and wallet URLs. const KeyStoreScheme = "keystore" +const keystoreDBTableName = "keystore" + // KeyStore is the interface which abstracts all needed operations required type KeyStore interface { // Wallets implements accounts.Backend, returning all single-key wallets from the KeyStore. @@ -120,8 +122,8 @@ func NewPlaintextKeyStore(keydir string) KeyStore { } // NewKeyStoreDB creates a keystore for the given database -func NewKeyStoreDB(path, table string, scryptN, scryptP int) (KeyStore, error) { - kvstore, err := dbutil.NewKVStore(path, table) +func NewKeyStoreDB(path string, scryptN, scryptP int) (KeyStore, error) { + kvstore, err := dbutil.NewKVStore(path, keystoreDBTableName) if err != nil { return nil, err } diff --git a/cmd/clef/main.go b/cmd/clef/main.go index 7a5f6d34fa..c9f393461f 100644 --- a/cmd/clef/main.go +++ b/cmd/clef/main.go @@ -500,9 +500,17 @@ func signer(c *cli.Context) error { nousb = c.GlobalBool(utils.NoUSBFlag.Name) scpath = c.GlobalString(utils.SmartCardDaemonPathFlag.Name) ) + ksDB := c.GlobalString(keystoreDBFlag.Name) + if ksDB != "" { + // if keystoreDBFlag is set, ignore keystoreFlag + ksLoc = ksDB + } log.Info("Starting signer", "chainid", chainId, "keystore", ksLoc, "light-kdf", lightKdf, "advanced", advanced) - am := core.StartClefAccountManager(ksLoc, nousb, lightKdf, scpath) + am, err := core.StartClefAccountManager(ksLoc, nousb, lightKdf, scpath) + if err != nil { + utils.Fatalf(err.Error()) + } apiImpl := core.NewSignerAPI(am, chainId, nousb, ui, db, advanced, pwStorage) // Establish the bidirectional communication, by creating a new UI backend and registering diff --git a/signer/core/api.go b/signer/core/api.go index 33bbd4b615..3af04e971e 100644 --- a/signer/core/api.go +++ b/signer/core/api.go @@ -23,6 +23,7 @@ import ( "fmt" "math/big" "os" + "path/filepath" "reflect" "github.com/ethereum/go-ethereum/accounts" @@ -125,7 +126,8 @@ type Metadata struct { Origin string `json:"Origin"` } -func StartClefAccountManager(ksLocation string, nousb, lightKDF bool, scpath string) *accounts.Manager { +// StartClefAccountManager initializes and start clef Account Manager +func StartClefAccountManager(ksLocation string, nousb, lightKDF bool, scpath string) (*accounts.Manager, error) { var ( backends []accounts.Backend n, p = keystore.StandardScryptN, keystore.StandardScryptP @@ -133,9 +135,28 @@ func StartClefAccountManager(ksLocation string, nousb, lightKDF bool, scpath str if lightKDF { n, p = keystore.LightScryptN, keystore.LightScryptP } + // check keystore type + var fsKeystore bool + if len(ksLocation) > 0 { + ext := filepath.Ext(ksLocation) + if ext == ".yaml" { + fsKeystore = false + } else { + fsKeystore = true + } + } + // support password based accounts if len(ksLocation) > 0 { - backends = append(backends, keystore.NewKeyStore(ksLocation, n, p)) + if fsKeystore { + backends = append(backends, keystore.NewKeyStore(ksLocation, n, p)) + } else { + ks, err := keystore.NewKeyStoreDB(ksLocation, n, p) + if err != nil { + return nil, err + } + backends = append(backends, ks) + } } if !nousb { // Start a USB hub for Ledger hardware wallets @@ -162,7 +183,7 @@ func StartClefAccountManager(ksLocation string, nousb, lightKDF bool, scpath str } // Start a smart card hub - if len(scpath) > 0 { + if len(scpath) > 0 && fsKeystore { // Sanity check that the smartcard path is valid fi, err := os.Stat(scpath) if err != nil { @@ -181,7 +202,7 @@ func StartClefAccountManager(ksLocation string, nousb, lightKDF bool, scpath str } // Clef doesn't allow insecure http account unlock. - return accounts.NewManager(&accounts.Config{InsecureUnlockAllowed: false}, backends...) + return accounts.NewManager(&accounts.Config{InsecureUnlockAllowed: false}, backends...), nil } // MetadataFromContext extracts Metadata from a given context.Context diff --git a/signer/core/api_test.go b/signer/core/api_test.go index 160f0e39b1..018c53b06d 100644 --- a/signer/core/api_test.go +++ b/signer/core/api_test.go @@ -125,7 +125,10 @@ func setup(ksLoc string, t *testing.T) (*core.SignerAPI, *headlessUi) { t.Fatal(err.Error()) } ui := &headlessUi{make(chan string, 20), make(chan string, 20)} - am := core.StartClefAccountManager(ksLoc, true, true, "") + am, err := core.StartClefAccountManager(ksLoc, true, true, "") + if err != nil { + t.Fatal(err.Error()) + } api := core.NewSignerAPI(am, 1337, true, ui, db, true, storage.NewNoStorage()) return api, ui }