Skip to content

Commit

Permalink
Using hex for txid computation
Browse files Browse the repository at this point in the history
This change-set removes base64 encoding from the
computation of txid to use hex which guarantees
compatibility with the node-sdk.

Change-Id: I1c59390064d0432451dadf996f33d7bd106851f6
Signed-off-by: Angelo De Caro <adc@zurich.ibm.com>
  • Loading branch information
adecaro committed Feb 19, 2017
1 parent ef962ca commit 756023f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
5 changes: 3 additions & 2 deletions protos/utils/proputils.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ import (

"errors"

"encoding/base64"
"encoding/binary"

"encoding/hex"

"github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric/bccsp"
"github.com/hyperledger/fabric/bccsp/factory"
Expand Down Expand Up @@ -533,7 +534,7 @@ func ComputeProposalTxID(nonce, creator []byte) (string, error) {
if err != nil {
return "", err
}
return base64.StdEncoding.EncodeToString(digest), nil
return hex.EncodeToString(digest), nil
}

func CheckProposalTxID(txid string, nonce, creator []byte) error {
Expand Down
37 changes: 37 additions & 0 deletions protos/utils/proputils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ import (
"fmt"
"os"

"crypto/sha256"
"encoding/hex"

"github.com/hyperledger/fabric/common/util"
"github.com/hyperledger/fabric/msp"
mspmgmt "github.com/hyperledger/fabric/msp/mgmt"
Expand Down Expand Up @@ -338,6 +341,40 @@ func TestEnvelope(t *testing.T) {
}
}

func TestProposalTxID(t *testing.T) {
nonce := []byte{1}
creator := []byte{2}

txid, err := ComputeProposalTxID(nonce, creator)
assert.NotEmpty(t, txid, "TxID cannot be empty.")
assert.NoError(t, err, "Failed computing txID")
assert.Nil(t, CheckProposalTxID(txid, nonce, creator))
assert.Error(t, CheckProposalTxID("", nonce, creator))

txid, err = ComputeProposalTxID(nil, nil)
assert.NotEmpty(t, txid, "TxID cannot be empty.")
assert.NoError(t, err, "Failed computing txID")
}

func TestComputeProposalTxID(t *testing.T) {
txid, err := ComputeProposalTxID([]byte{1}, []byte{1})
assert.NoError(t, err, "Failed computing TxID")

// Compute the function computed by ComputeProposalTxID,
// namely, base64(sha256(nonce||creator))
hf := sha256.New()
hf.Write([]byte{1})
hf.Write([]byte{1})
hashOut := hf.Sum(nil)
txid2 := hex.EncodeToString(hashOut)

t.Logf("% x\n", hashOut)
t.Logf("% s\n", txid)
t.Logf("% s\n", txid2)

assert.Equal(t, txid, txid2)
}

var signer msp.SigningIdentity
var signerSerialized []byte

Expand Down

0 comments on commit 756023f

Please sign in to comment.