OSDN Git Service

Add passwords and Remove xprv cache (#267)
authorBlockmeta-区块元 <blockmeta@8btc.com>
Tue, 23 Jan 2018 02:38:01 +0000 (10:38 +0800)
committerPaladz <yzhu101@uottawa.ca>
Tue, 23 Jan 2018 02:38:01 +0000 (10:38 +0800)
* Fix Key not Found Problem

Remove XPrv cache
Change password string to array

* Add password array

fix test bug

* Truncate useless codes

Return error ,no more "if"

blockchain/hsm.go
blockchain/hsm_test.go
blockchain/pseudohsm/keycache.go
blockchain/pseudohsm/keycache_test.go
blockchain/pseudohsm/pseudohsm.go
blockchain/transact.go
blockchain/txbuilder/rawtxsig_witness.go
blockchain/txbuilder/signature_witness.go
blockchain/txbuilder/txbuilder.go
test/util.go

index 3cafdae..b187228 100644 (file)
@@ -47,31 +47,23 @@ func (bcr *BlockchainReactor) pseudohsmDeleteKey(ctx context.Context, x struct {
        if err := bcr.hsm.XDelete(x.XPub, x.Password); err != nil {
                return resWrapper(nil, err)
        }
-
        return resWrapper(nil)
 }
 
 func (bcr *BlockchainReactor) pseudohsmSignTemplates(ctx context.Context, x struct {
-       Auth string             `json:"auth"`
-       Txs  txbuilder.Template `json:"transaction"`
+       Password []string             `json:"password"`
+       Txs      txbuilder.Template `json:"transaction"`
 }) Response {
-       var err error
-       if err = txbuilder.Sign(ctx, &x.Txs, nil, x.Auth, bcr.pseudohsmSignTemplate); err != nil {
+       if err := txbuilder.Sign(ctx, &x.Txs, nil, x.Password, bcr.pseudohsmSignTemplate); err != nil {
                log.WithField("build err", err).Error("fail on sign transaction.")
                return resWrapper(nil, err)
        }
-
        log.Info("Sign Transaction complete.")
        return resWrapper(&x.Txs)
 }
 
 func (bcr *BlockchainReactor) pseudohsmSignTemplate(ctx context.Context, xpub chainkd.XPub, path [][]byte, data [32]byte, password string) ([]byte, error) {
-       sigBytes, err := bcr.hsm.XSign(xpub, path, data[:], password)
-       if err == pseudohsm.ErrNoKey {
-               log.Error(err)
-               return nil, nil
-       }
-       return sigBytes, err
+       return bcr.hsm.XSign(xpub, path, data[:], password)
 }
 
 func (bcr *BlockchainReactor) pseudohsmResetPassword(ctx context.Context, x struct {
index c3e5a68..2449ab9 100755 (executable)
@@ -91,7 +91,7 @@ func TestHSM(t *testing.T) {
        }
        //go accounts.ProcessBlocks(ctx)
 
-       err = txbuilder.Sign(ctx, tmpl, nil, "password", func(_ context.Context, xpub chainkd.XPub, path [][]byte, data [32]byte, password string) ([]byte, error) {
+       err = txbuilder.Sign(ctx, tmpl, nil, []string{"password"}, func(_ context.Context, xpub chainkd.XPub, path [][]byte, data [32]byte, password string) ([]byte, error) {
                sigBytes, err := hsm.XSign(xpub, path, data[:], password)
                if err != nil {
                        return nil, nil
index bb8191a..4423998 100644 (file)
@@ -147,14 +147,14 @@ func (kc *keyCache) find(xpub XPub) (XPub, error) {
                        }
                }
                if (xpub.XPub == chainkd.XPub{}) {
-                       return XPub{}, ErrNoKey
+                       return XPub{}, ErrLoadKey
                }
        }
        switch len(matches) {
        case 1:
                return matches[0], nil
        case 0:
-               return XPub{}, ErrNoKey
+               return XPub{}, ErrLoadKey
        default:
                err := &AmbiguousKeyError{Pubkey: hex.EncodeToString(xpub.XPub[:]), Matches: make([]XPub, len(matches))}
                copy(err.Matches, matches)
index 5c0c504..a32f422 100644 (file)
@@ -270,10 +270,10 @@ func TestCacheFind(t *testing.T) {
                        },
                },
                // no match error
-               {Query: nomatchKey, WantError: ErrNoKey},
-               {Query: XPub{File: nomatchKey.File}, WantError: ErrNoKey},
-               {Query: XPub{File: filepath.Base(nomatchKey.File)}, WantError: ErrNoKey},
-               {Query: XPub{XPub: nomatchKey.XPub}, WantError: ErrNoKey},
+               {Query: nomatchKey, WantError: ErrLoadKey},
+               {Query: XPub{File: nomatchKey.File}, WantError: ErrLoadKey},
+               {Query: XPub{File: filepath.Base(nomatchKey.File)}, WantError: ErrLoadKey},
+               {Query: XPub{XPub: nomatchKey.XPub}, WantError: ErrLoadKey},
        }
        for i, test := range tests {
                a, err := cache.find(test.Query)
index e870638..19e0133 100644 (file)
@@ -19,7 +19,7 @@ var (
        ErrDuplicateKeyAlias    = errors.New("duplicate key alias")
        ErrDuplicateKey         = errors.New("duplicate key")
        ErrInvalidAfter         = errors.New("invalid after")
-       ErrNoKey                = errors.New("key not found")
+       ErrLoadKey              = errors.New("key not found or wrong password ")
        ErrInvalidKeySize       = errors.New("key invalid size")
        ErrTooManyAliasesToList = errors.New("requested aliases exceeds limit")
        ErrAmbiguousAlias       = errors.New("multiple keys match alias")
@@ -32,7 +32,7 @@ type HSM struct {
        cacheMu  sync.Mutex
        keyStore keyStore
        cache    *keyCache
-       kdCache  map[chainkd.XPub]chainkd.XPrv
+       //kdCache  map[chainkd.XPub]chainkd.XPrv
 }
 
 // XPub type for pubkey for anyone can see
@@ -48,7 +48,7 @@ func New(keypath string) (*HSM, error) {
        return &HSM{
                keyStore: &keyStorePassphrase{keydir, LightScryptN, LightScryptP},
                cache:    newKeyCache(keydir),
-               kdCache:  make(map[chainkd.XPub]chainkd.XPrv),
+               //kdCache:  make(map[chainkd.XPub]chainkd.XPrv),
        }, nil
 }
 
@@ -110,15 +110,15 @@ func (h *HSM) LoadChainKDKey(xpub chainkd.XPub, auth string) (xprv chainkd.XPrv,
        h.cacheMu.Lock()
        defer h.cacheMu.Unlock()
 
-       if xprv, ok := h.kdCache[xpub]; ok {
-               return xprv, nil
-       }
+       //if xprv, ok := h.kdCache[xpub]; ok {
+       //      return xprv, nil
+       //}
 
-       xpb, xkey, err := h.loadDecryptedKey(xpub, auth)
+       _, xkey, err := h.loadDecryptedKey(xpub, auth)
        if err != nil {
-               return xprv, ErrNoKey
+               return xprv, ErrLoadKey
        }
-       h.kdCache[xpb.XPub] = xkey.XPrv
+       //h.kdCache[xpb.XPub] = xkey.XPrv
        return xkey.XPrv, nil
 }
 
@@ -137,6 +137,7 @@ func (h *HSM) XDelete(xpub chainkd.XPub, auth string) error {
                return err
        }
 
+       h.cacheMu.Lock()
        // The order is crucial here. The key is dropped from the
        // cache after the file is gone so that a reload happening in
        // between won't insert it into the cache again.
@@ -144,8 +145,6 @@ func (h *HSM) XDelete(xpub chainkd.XPub, auth string) error {
        if err == nil {
                h.cache.delete(xpb)
        }
-       h.cacheMu.Lock()
-       delete(h.kdCache, xpub)
        h.cacheMu.Unlock()
        return err
 }
index 5e55ebe..153a9e8 100644 (file)
@@ -236,12 +236,12 @@ func (bcr *BlockchainReactor) submit(ctx context.Context, tpl *txbuilder.Templat
 
 // POST /sign-submit-transaction
 func (bcr *BlockchainReactor) signSubmit(ctx context.Context, x struct {
-       Auth string             `json:"auth"`
+       Password []string             `json:"password"`
        Txs  txbuilder.Template `json:"transaction"`
 }) Response {
 
        var err error
-       if err = txbuilder.Sign(ctx, &x.Txs, nil, x.Auth, bcr.pseudohsmSignTemplate); err != nil {
+       if err = txbuilder.Sign(ctx, &x.Txs, nil, x.Password, bcr.pseudohsmSignTemplate); err != nil {
                log.WithField("build err", err).Error("fail on sign transaction.")
                return resWrapper(nil, err)
        }
index 7cf1573..a616a29 100755 (executable)
@@ -20,7 +20,7 @@ type RawTxSigWitness struct {
        Sigs   []chainjson.HexBytes `json:"signatures"`
 }
 
-func (sw *RawTxSigWitness) sign(ctx context.Context, tpl *Template, index uint32, xpubs []chainkd.XPub, auth string, signFn SignFunc) error {
+func (sw *RawTxSigWitness) sign(ctx context.Context, tpl *Template, index uint32, xpubs []chainkd.XPub, auth []string, signFn SignFunc) error {
        if len(sw.Sigs) < len(sw.Keys) {
                // Each key in sw.Keys may produce a signature in sw.Sigs. Make
                // sure there are enough slots in sw.Sigs and that we preserve any
@@ -48,7 +48,7 @@ func (sw *RawTxSigWitness) sign(ctx context.Context, tpl *Template, index uint32
                for i, p := range keyID.DerivationPath {
                        path[i] = p
                }
-               sigBytes, err := signFn(ctx, keyID.XPub, path, tpl.Hash(index).Byte32(), auth)
+               sigBytes, err := signFn(ctx, keyID.XPub, path, tpl.Hash(index).Byte32(), auth[i])
                if err != nil {
                        return errors.WithDetailf(err, "computing signature %d", i)
                }
index 0f28216..6b5e073 100755 (executable)
@@ -47,7 +47,7 @@ var ErrEmptyProgram = errors.New("empty signature program")
 //  - the mintime and maxtime of the transaction (if non-zero)
 //  - the outputID and (if non-empty) reference data of the current input
 //  - the assetID, amount, control program, and (if non-empty) reference data of each output.
-func (sw *SignatureWitness) sign(ctx context.Context, tpl *Template, index uint32, xpubs []chainkd.XPub, auth string, signFn SignFunc) error {
+func (sw *SignatureWitness) sign(ctx context.Context, tpl *Template, index uint32, xpubs []chainkd.XPub, auth []string, signFn SignFunc) error {
        // Compute the predicate to sign. This is either a
        // txsighash program if tpl.AllowAdditional is false (i.e., the tx is complete
        // and no further changes are allowed) or a program enforcing
@@ -91,7 +91,7 @@ func (sw *SignatureWitness) sign(ctx context.Context, tpl *Template, index uint3
                for i, p := range keyID.DerivationPath {
                        path[i] = p
                }
-               sigBytes, err := signFn(ctx, keyID.XPub, path, h, auth)
+               sigBytes, err := signFn(ctx, keyID.XPub, path, h, auth[i])
                if err != nil {
                        return errors.WithDetailf(err, "computing signature %d", i)
                }
index 8ae2cc3..fa0c5e6 100755 (executable)
@@ -71,7 +71,7 @@ func Build(ctx context.Context, tx *legacy.TxData, actions []Action, maxTime tim
        return tpl, nil
 }
 
-func Sign(ctx context.Context, tpl *Template, xpubs []chainkd.XPub, auth string, signFn SignFunc) error {
+func Sign(ctx context.Context, tpl *Template, xpubs []chainkd.XPub, auth []string, signFn SignFunc) error {
        for i, sigInst := range tpl.SigningInstructions {
                for j, wc := range sigInst.WitnessComponents {
                        switch sw := wc.(type) {
index e0b2eed..b8d88b5 100644 (file)
@@ -63,11 +63,8 @@ func MockTx(utxo *account.UTXO, testAccount *account.Account) (*txbuilder.Templa
 }
 
 func MockSign(tpl *txbuilder.Template, hsm *pseudohsm.HSM) error {
-       return txbuilder.Sign(nil, tpl, nil, "password", func(_ context.Context, xpub chainkd.XPub, path [][]byte, data [32]byte, password string) ([]byte, error) {
+       return txbuilder.Sign(nil, tpl, nil, []string{"password", "password"}, func(_ context.Context, xpub chainkd.XPub, path [][]byte, data [32]byte, password string) ([]byte, error) {
                sigBytes, err := hsm.XSign(xpub, path, data[:], password)
-               if err != nil {
-                       return nil, nil
-               }
                return sigBytes, err
        })
 }