OSDN Git Service

Fix sign api (#603)
authorYongfeng LI <wliyongfeng@gmail.com>
Thu, 12 Apr 2018 06:39:48 +0000 (14:39 +0800)
committerPaladz <yzhu101@uottawa.ca>
Thu, 12 Apr 2018 06:39:48 +0000 (14:39 +0800)
* return error when sign-submit-transaction has no sign

* upgrade version

* correct the way to check sign

* Revert "correct the way to check sign"

This reverts commit 04484a82a540bf705a9ebecc8f1cdb373f98e801.

* fix the way to check sign

api/api.go
api/transact.go
blockchain/txbuilder/witness.go
version/version.go

index 5970713..3d67719 100644 (file)
@@ -193,6 +193,7 @@ func (a *API) buildHandler() {
                m.Handle("/build-transaction", jsonHandler(a.build))
                m.Handle("/sign-transaction", jsonHandler(a.pseudohsmSignTemplates))
                m.Handle("/submit-transaction", jsonHandler(a.submit))
+               // TODO remove this api, separate sign and submit process
                m.Handle("/sign-submit-transaction", jsonHandler(a.signSubmit))
                m.Handle("/get-transaction", jsonHandler(a.getTransaction))
                m.Handle("/list-transactions", jsonHandler(a.listTransactions))
index 1322462..4a72dd4 100644 (file)
@@ -9,6 +9,7 @@ import (
 
        log "github.com/sirupsen/logrus"
 
+       "github.com/bytom/blockchain/pseudohsm"
        "github.com/bytom/blockchain/txbuilder"
        "github.com/bytom/errors"
        "github.com/bytom/net/http/reqid"
@@ -182,13 +183,17 @@ func (a *API) submit(ctx context.Context, ins struct {
 
 // POST /sign-submit-transaction
 func (a *API) signSubmit(ctx context.Context, x struct {
-       Password string           `json:"password"`
+       Password string             `json:"password"`
        Txs      txbuilder.Template `json:"transaction"`
 }) Response {
        if err := txbuilder.Sign(ctx, &x.Txs, nil, x.Password, a.pseudohsmSignTemplate); err != nil {
                log.WithField("build err", err).Error("fail on sign transaction.")
                return NewErrorResponse(err)
        }
+
+       if signCount, complete := txbuilder.SignInfo(&x.Txs); !complete && signCount == 0 {
+               return NewErrorResponse(pseudohsm.ErrLoadKey)
+       }
        log.Info("Sign Transaction complete.")
 
        txID, err := a.submitSingle(nil, &x.Txs)
index 3efc546..d942b3d 100644 (file)
@@ -53,6 +53,26 @@ func signedCount(signs []chainjson.HexBytes) (count int) {
        return
 }
 
+// SignInfo
+func SignInfo(txTemplate *Template) (int, bool) {
+       var count int
+       for _, sigInst := range txTemplate.SigningInstructions {
+               for _, wc := range sigInst.WitnessComponents {
+                       switch sw := wc.(type) {
+                       case *SignatureWitness:
+                               if count = signedCount(sw.Sigs); count < sw.Quorum {
+                                       return count, false
+                               }
+                       case *RawTxSigWitness:
+                               if count = signedCount(sw.Sigs); count < sw.Quorum {
+                                       return count, false
+                               }
+                       }
+               }
+       }
+       return count, true
+}
+
 // SignProgress check is all the sign requirement are satisfy
 func SignProgress(txTemplate *Template) bool {
        for _, sigInst := range txTemplate.SigningInstructions {
index 782833d..ade179f 100644 (file)
@@ -2,7 +2,7 @@ package version
 
 var (
        // The full version string
-       Version = "0.4.5"
+       Version = "0.4.6"
        // GitCommit is set with --ldflags "-X main.gitCommit=$(git rev-parse HEAD)"
        GitCommit string
 )