From 004559d119ee14b9dc33a5071ca11656e705e940 Mon Sep 17 00:00:00 2001 From: Yongfeng LI Date: Wed, 21 Mar 2018 16:21:20 +0800 Subject: [PATCH] put hsm from BlockchainReactor to Wallet (#451) --- blockchain/hsm.go | 10 +++++----- blockchain/reactor.go | 5 +---- blockchain/wallet.go | 4 ++-- blockchain/wallet/wallet.go | 16 +++++++++------- blockchain/wallet/wallet_test.go | 10 +++++----- node/node.go | 4 ++-- 6 files changed, 24 insertions(+), 25 deletions(-) diff --git a/blockchain/hsm.go b/blockchain/hsm.go index 444d8d92..0c36e3dc 100644 --- a/blockchain/hsm.go +++ b/blockchain/hsm.go @@ -24,7 +24,7 @@ func (bcr *BlockchainReactor) pseudohsmCreateKey(ctx context.Context, in struct Alias string `json:"alias"` Password string `json:"password"` }) Response { - xpub, err := bcr.hsm.XCreate(in.Alias, in.Password) + xpub, err := bcr.wallet.Hsm.XCreate(in.Alias, in.Password) if err != nil { return NewErrorResponse(err) } @@ -32,14 +32,14 @@ func (bcr *BlockchainReactor) pseudohsmCreateKey(ctx context.Context, in struct } func (bcr *BlockchainReactor) pseudohsmListKeys(ctx context.Context) Response { - return NewSuccessResponse(bcr.hsm.ListKeys()) + return NewSuccessResponse(bcr.wallet.Hsm.ListKeys()) } func (bcr *BlockchainReactor) pseudohsmDeleteKey(ctx context.Context, x struct { Password string `json:"password"` XPub chainkd.XPub `json:"xpub"` }) Response { - if err := bcr.hsm.XDelete(x.XPub, x.Password); err != nil { + if err := bcr.wallet.Hsm.XDelete(x.XPub, x.Password); err != nil { return NewErrorResponse(err) } return NewSuccessResponse(nil) @@ -63,7 +63,7 @@ func (bcr *BlockchainReactor) pseudohsmSignTemplates(ctx context.Context, x stru } func (bcr *BlockchainReactor) pseudohsmSignTemplate(ctx context.Context, xpub chainkd.XPub, path [][]byte, data [32]byte, password string) ([]byte, error) { - return bcr.hsm.XSign(xpub, path, data[:], password) + return bcr.wallet.Hsm.XSign(xpub, path, data[:], password) } func (bcr *BlockchainReactor) pseudohsmResetPassword(ctx context.Context, x struct { @@ -71,5 +71,5 @@ func (bcr *BlockchainReactor) pseudohsmResetPassword(ctx context.Context, x stru NewPassword string XPub chainkd.XPub `json:"xpubs"` }) error { - return bcr.hsm.ResetPassword(x.XPub, x.OldPassword, x.NewPassword) + return bcr.wallet.Hsm.ResetPassword(x.XPub, x.OldPassword, x.NewPassword) } diff --git a/blockchain/reactor.go b/blockchain/reactor.go index 284640c6..aaf9f71d 100755 --- a/blockchain/reactor.go +++ b/blockchain/reactor.go @@ -10,7 +10,6 @@ import ( cmn "github.com/tendermint/tmlibs/common" "github.com/bytom/blockchain/accesstoken" - "github.com/bytom/blockchain/pseudohsm" "github.com/bytom/blockchain/txfeed" "github.com/bytom/blockchain/wallet" "github.com/bytom/mining/cpuminer" @@ -67,7 +66,6 @@ type BlockchainReactor struct { txFeedTracker *txfeed.Tracker blockKeeper *blockKeeper txPool *protocol.TxPool - hsm *pseudohsm.HSM mining *cpuminer.CPUMiner miningPool *miningpool.MiningPool mux *http.ServeMux @@ -101,7 +99,7 @@ func maxBytes(h http.Handler) http.Handler { } // NewBlockchainReactor returns the reactor of whole blockchain. -func NewBlockchainReactor(chain *protocol.Chain, txPool *protocol.TxPool, sw *p2p.Switch, hsm *pseudohsm.HSM, wallet *wallet.Wallet, txfeeds *txfeed.Tracker, accessTokens *accesstoken.CredentialStore, miningEnable bool) *BlockchainReactor { +func NewBlockchainReactor(chain *protocol.Chain, txPool *protocol.TxPool, sw *p2p.Switch,wallet *wallet.Wallet, txfeeds *txfeed.Tracker, accessTokens *accesstoken.CredentialStore, miningEnable bool) *BlockchainReactor { newBlockCh := make(chan *bc.Hash, maxNewBlockChSize) bcr := &BlockchainReactor{ chain: chain, @@ -112,7 +110,6 @@ func NewBlockchainReactor(chain *protocol.Chain, txPool *protocol.TxPool, sw *p2 miningPool: miningpool.NewMiningPool(chain, wallet.AccountMgr, txPool, newBlockCh), mux: http.NewServeMux(), sw: sw, - hsm: hsm, txFeedTracker: txfeeds, accessTokens: accessTokens, miningEnable: miningEnable, diff --git a/blockchain/wallet.go b/blockchain/wallet.go index e470a4ca..bdd88497 100644 --- a/blockchain/wallet.go +++ b/blockchain/wallet.go @@ -24,7 +24,7 @@ func (bcr *BlockchainReactor) walletExportKey(ctx context.Context, in struct { Password string `json:"password"` XPub chainkd.XPub `json:"xpub"` }) Response { - key, err := bcr.wallet.ExportAccountPrivKey(bcr.hsm, in.XPub, in.Password) + key, err := bcr.wallet.ExportAccountPrivKey(in.XPub, in.Password) if err != nil { return NewErrorResponse(err) } @@ -54,7 +54,7 @@ func (bcr *BlockchainReactor) walletImportKey(ctx context.Context, in KeyImportP var xprv [64]byte copy(xprv[:], rawData[:64]) - xpub, err := bcr.wallet.ImportAccountPrivKey(bcr.hsm, xprv, in.KeyAlias, in.Password, in.Index, in.AccountAlias) + xpub, err := bcr.wallet.ImportAccountPrivKey(xprv, in.KeyAlias, in.Password, in.Index, in.AccountAlias) if err != nil { return NewErrorResponse(err) } diff --git a/blockchain/wallet/wallet.go b/blockchain/wallet/wallet.go index 607c4a92..938951b3 100755 --- a/blockchain/wallet/wallet.go +++ b/blockchain/wallet/wallet.go @@ -49,6 +49,7 @@ type Wallet struct { status StatusInfo AccountMgr *account.Manager AssetReg *asset.Registry + Hsm *pseudohsm.HSM chain *protocol.Chain rescanProgress chan struct{} ImportPrivKey bool @@ -56,13 +57,14 @@ type Wallet struct { } //NewWallet return a new wallet instance -func NewWallet(walletDB db.DB, account *account.Manager, asset *asset.Registry, +func NewWallet(walletDB db.DB, account *account.Manager, asset *asset.Registry, hsm *pseudohsm.HSM, chain *protocol.Chain) (*Wallet, error) { w := &Wallet{ DB: walletDB, AccountMgr: account, AssetReg: asset, chain: chain, + Hsm: hsm, rescanProgress: make(chan struct{}, 1), keysInfo: make([]KeyInfo, 0), } @@ -228,8 +230,8 @@ func getRescanNotification(w *Wallet) { // ExportAccountPrivKey exports the account private key as a WIF for encoding as a string // in the Wallet Import Formt. -func (w *Wallet) ExportAccountPrivKey(hsm *pseudohsm.HSM, xpub chainkd.XPub, auth string) (*string, error) { - xprv, err := hsm.LoadChainKDKey(xpub, auth) +func (w *Wallet) ExportAccountPrivKey(xpub chainkd.XPub, auth string) (*string, error) { + xprv, err := w.Hsm.LoadChainKDKey(xpub, auth) if err != nil { return nil, err } @@ -242,11 +244,11 @@ func (w *Wallet) ExportAccountPrivKey(hsm *pseudohsm.HSM, xpub chainkd.XPub, aut } // ImportAccountPrivKey imports the account key in the Wallet Import Formt. -func (w *Wallet) ImportAccountPrivKey(hsm *pseudohsm.HSM, xprv chainkd.XPrv, keyAlias, auth string, index uint64, accountAlias string) (*pseudohsm.XPub, error) { - if hsm.HasAlias(keyAlias) { +func (w *Wallet) ImportAccountPrivKey(xprv chainkd.XPrv, keyAlias, auth string, index uint64, accountAlias string) (*pseudohsm.XPub, error) { + if w.Hsm.HasAlias(keyAlias) { return nil, pseudohsm.ErrDuplicateKeyAlias } - if hsm.HasKey(xprv) { + if w.Hsm.HasKey(xprv) { return nil, pseudohsm.ErrDuplicateKey } @@ -254,7 +256,7 @@ func (w *Wallet) ImportAccountPrivKey(hsm *pseudohsm.HSM, xprv chainkd.XPrv, key return nil, account.ErrDuplicateAlias } - xpub, _, err := hsm.ImportXPrvKey(auth, keyAlias, xprv) + xpub, _, err := w.Hsm.ImportXPrvKey(auth, keyAlias, xprv) if err != nil { return nil, err } diff --git a/blockchain/wallet/wallet_test.go b/blockchain/wallet/wallet_test.go index 13ce73e7..383b4ad3 100755 --- a/blockchain/wallet/wallet_test.go +++ b/blockchain/wallet/wallet_test.go @@ -139,7 +139,7 @@ func TestExportAndImportPrivKey(t *testing.T) { t.Fatal(err) } - w, err := NewWallet(testDB, acntManager, reg, chain) + w, err := NewWallet(testDB, acntManager, reg, hsm, chain) if err != nil { t.Fatal(err) } @@ -150,7 +150,7 @@ func TestExportAndImportPrivKey(t *testing.T) { t.Fatal(err) } - priv, err := w.ExportAccountPrivKey(hsm, xpub.XPub, pwd) + priv, err := w.ExportAccountPrivKey(xpub.XPub, pwd) wantPriv, err := hsm.LoadChainKDKey(xpub.XPub, pwd) if err != nil { @@ -178,14 +178,14 @@ func TestExportAndImportPrivKey(t *testing.T) { var xprv [64]byte copy(xprv[:], rawPriv[:64]) - _, err = w.ImportAccountPrivKey(hsm, xprv, xpub.Alias, pwd, 0, acnt1.Alias) + _, err = w.ImportAccountPrivKey(xprv, xpub.Alias, pwd, 0, acnt1.Alias) if err != pseudohsm.ErrDuplicateKeyAlias { t.Fatal(err) } hsm.XDelete(xpub.XPub, pwd) - _, err = w.ImportAccountPrivKey(hsm, xprv, xpub.Alias, pwd, 0, acnt1.Alias) + _, err = w.ImportAccountPrivKey(xprv, xpub.Alias, pwd, 0, acnt1.Alias) if err != account.ErrDuplicateAlias { t.Fatal(err) } @@ -196,7 +196,7 @@ func TestExportAndImportPrivKey(t *testing.T) { w.AccountMgr.DeleteAccount(accountInfo) - acnt2, err := w.ImportAccountPrivKey(hsm, xprv, xpub.Alias, pwd, 0, acnt1.Alias) + acnt2, err := w.ImportAccountPrivKey(xprv, xpub.Alias, pwd, 0, acnt1.Alias) if err != nil { t.Fatal(err) } diff --git a/node/node.go b/node/node.go index aeca6c06..3e73fb79 100755 --- a/node/node.go +++ b/node/node.go @@ -197,7 +197,7 @@ func NewNode(config *cfg.Config) *Node { walletDB := dbm.NewDB("wallet", config.DBBackend, config.DBDir()) accounts = account.NewManager(walletDB, chain) assets = asset.NewRegistry(walletDB, chain) - wallet, err = w.NewWallet(walletDB, accounts, assets, chain) + wallet, err = w.NewWallet(walletDB, accounts, assets, hsm, chain) if err != nil { log.WithField("error", err).Error("init NewWallet") } @@ -210,7 +210,7 @@ func NewNode(config *cfg.Config) *Node { go accounts.ExpireReservations(ctx, expireReservationsPeriod) } - bcReactor := bc.NewBlockchainReactor(chain, txPool,sw, hsm, wallet, txFeed, accessTokens, config.Mining) + bcReactor := bc.NewBlockchainReactor(chain, txPool,sw, wallet, txFeed, accessTokens, config.Mining) sw.AddReactor("BLOCKCHAIN", bcReactor) -- 2.11.0