OSDN Git Service

put token management from BlockchainReactor to Wallet (#452)
authorYongfeng LI <wliyongfeng@gmail.com>
Wed, 21 Mar 2018 08:41:47 +0000 (16:41 +0800)
committerGitHub <noreply@github.com>
Wed, 21 Mar 2018 08:41:47 +0000 (16:41 +0800)
blockchain/accesstokens.go
blockchain/reactor.go
blockchain/wallet/wallet.go
blockchain/wallet/wallet_test.go
node/node.go

index 4752a4e..e5adc2b 100644 (file)
@@ -10,7 +10,7 @@ func (bcr *BlockchainReactor) createAccessToken(ctx context.Context, x struct {
        ID   string `json:"id"`
        Type string `json:"type"`
 }) Response {
-       token, err := bcr.accessTokens.Create(ctx, x.ID, x.Type)
+       token, err := bcr.wallet.Tokens.Create(ctx, x.ID, x.Type)
        if err != nil {
                return NewErrorResponse(err)
        }
@@ -18,7 +18,7 @@ func (bcr *BlockchainReactor) createAccessToken(ctx context.Context, x struct {
 }
 
 func (bcr *BlockchainReactor) listAccessTokens(ctx context.Context) Response {
-       tokens, err := bcr.accessTokens.List(ctx)
+       tokens, err := bcr.wallet.Tokens.List(ctx)
        if err != nil {
                log.Errorf("listAccessTokens: %v", err)
                return NewErrorResponse(err)
@@ -32,7 +32,7 @@ func (bcr *BlockchainReactor) deleteAccessToken(ctx context.Context, x struct {
        Token string `json:"token"`
 }) Response {
        //TODO Add delete permission verify.
-       if err := bcr.accessTokens.Delete(ctx, x.ID); err != nil {
+       if err := bcr.wallet.Tokens.Delete(ctx, x.ID); err != nil {
                return NewErrorResponse(err)
        }
        return NewSuccessResponse(nil)
@@ -42,7 +42,7 @@ func (bcr *BlockchainReactor) checkAccessToken(ctx context.Context, x struct {
        ID     string `json:"id"`
        Secret string `json:"secret"`
 }) Response {
-       if _, err := bcr.accessTokens.Check(ctx, x.ID, x.Secret); err != nil {
+       if _, err := bcr.wallet.Tokens.Check(ctx, x.ID, x.Secret); err != nil {
                return NewErrorResponse(err)
        }
 
index aaf9f71..b951877 100755 (executable)
@@ -9,7 +9,6 @@ import (
        log "github.com/sirupsen/logrus"
        cmn "github.com/tendermint/tmlibs/common"
 
-       "github.com/bytom/blockchain/accesstoken"
        "github.com/bytom/blockchain/txfeed"
        "github.com/bytom/blockchain/wallet"
        "github.com/bytom/mining/cpuminer"
@@ -62,7 +61,6 @@ type BlockchainReactor struct {
 
        chain         *protocol.Chain
        wallet        *wallet.Wallet
-       accessTokens  *accesstoken.CredentialStore
        txFeedTracker *txfeed.Tracker
        blockKeeper   *blockKeeper
        txPool        *protocol.TxPool
@@ -99,7 +97,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,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, miningEnable bool) *BlockchainReactor {
        newBlockCh := make(chan *bc.Hash, maxNewBlockChSize)
        bcr := &BlockchainReactor{
                chain:         chain,
@@ -111,7 +109,6 @@ func NewBlockchainReactor(chain *protocol.Chain, txPool *protocol.TxPool, sw *p2
                mux:           http.NewServeMux(),
                sw:            sw,
                txFeedTracker: txfeeds,
-               accessTokens:  accessTokens,
                miningEnable:  miningEnable,
                newBlockCh:    newBlockCh,
        }
index 938951b..e7603d2 100755 (executable)
@@ -8,6 +8,7 @@ import (
        "github.com/tendermint/go-wire/data/base58"
        "github.com/tendermint/tmlibs/db"
 
+       "github.com/bytom/blockchain/accesstoken"
        "github.com/bytom/blockchain/account"
        "github.com/bytom/blockchain/asset"
        "github.com/bytom/blockchain/pseudohsm"
@@ -50,6 +51,7 @@ type Wallet struct {
        AccountMgr     *account.Manager
        AssetReg       *asset.Registry
        Hsm            *pseudohsm.HSM
+       Tokens             *accesstoken.CredentialStore
        chain          *protocol.Chain
        rescanProgress chan struct{}
        ImportPrivKey  bool
@@ -58,13 +60,14 @@ type Wallet struct {
 
 //NewWallet return a new wallet instance
 func NewWallet(walletDB db.DB, account *account.Manager, asset *asset.Registry, hsm *pseudohsm.HSM,
-       chain *protocol.Chain) (*Wallet, error) {
+       accessTokens *accesstoken.CredentialStore, chain *protocol.Chain) (*Wallet, error) {
        w := &Wallet{
                DB:             walletDB,
                AccountMgr:     account,
                AssetReg:       asset,
                chain:          chain,
                Hsm:            hsm,
+               Tokens:         accessTokens,
                rescanProgress: make(chan struct{}, 1),
                keysInfo:       make([]KeyInfo, 0),
        }
index 383b4ad..c7efb2c 100755 (executable)
@@ -139,7 +139,7 @@ func TestExportAndImportPrivKey(t *testing.T) {
                t.Fatal(err)
        }
 
-       w, err := NewWallet(testDB, acntManager, reg, hsm, chain)
+       w, err := NewWallet(testDB, acntManager, reg, hsm, nil, chain)
        if err != nil {
                t.Fatal(err)
        }
index 3e73fb7..1935320 100755 (executable)
@@ -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, hsm, chain)
+               wallet, err = w.NewWallet(walletDB, accounts, assets, hsm, accessTokens, 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, wallet, txFeed, accessTokens, config.Mining)
+       bcReactor := bc.NewBlockchainReactor(chain, txPool,sw, wallet, txFeed, config.Mining)
 
        sw.AddReactor("BLOCKCHAIN", bcReactor)