Skip to content

Commit 756023f

Browse files
committed
Using hex for txid computation
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 <[email protected]>
1 parent ef962ca commit 756023f

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

protos/utils/proputils.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@ import (
2121

2222
"errors"
2323

24-
"encoding/base64"
2524
"encoding/binary"
2625

26+
"encoding/hex"
27+
2728
"github.com/golang/protobuf/proto"
2829
"github.com/hyperledger/fabric/bccsp"
2930
"github.com/hyperledger/fabric/bccsp/factory"
@@ -533,7 +534,7 @@ func ComputeProposalTxID(nonce, creator []byte) (string, error) {
533534
if err != nil {
534535
return "", err
535536
}
536-
return base64.StdEncoding.EncodeToString(digest), nil
537+
return hex.EncodeToString(digest), nil
537538
}
538539

539540
func CheckProposalTxID(txid string, nonce, creator []byte) error {

protos/utils/proputils_test.go

+37
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ import (
2525
"fmt"
2626
"os"
2727

28+
"crypto/sha256"
29+
"encoding/hex"
30+
2831
"github.com/hyperledger/fabric/common/util"
2932
"github.com/hyperledger/fabric/msp"
3033
mspmgmt "github.com/hyperledger/fabric/msp/mgmt"
@@ -338,6 +341,40 @@ func TestEnvelope(t *testing.T) {
338341
}
339342
}
340343

344+
func TestProposalTxID(t *testing.T) {
345+
nonce := []byte{1}
346+
creator := []byte{2}
347+
348+
txid, err := ComputeProposalTxID(nonce, creator)
349+
assert.NotEmpty(t, txid, "TxID cannot be empty.")
350+
assert.NoError(t, err, "Failed computing txID")
351+
assert.Nil(t, CheckProposalTxID(txid, nonce, creator))
352+
assert.Error(t, CheckProposalTxID("", nonce, creator))
353+
354+
txid, err = ComputeProposalTxID(nil, nil)
355+
assert.NotEmpty(t, txid, "TxID cannot be empty.")
356+
assert.NoError(t, err, "Failed computing txID")
357+
}
358+
359+
func TestComputeProposalTxID(t *testing.T) {
360+
txid, err := ComputeProposalTxID([]byte{1}, []byte{1})
361+
assert.NoError(t, err, "Failed computing TxID")
362+
363+
// Compute the function computed by ComputeProposalTxID,
364+
// namely, base64(sha256(nonce||creator))
365+
hf := sha256.New()
366+
hf.Write([]byte{1})
367+
hf.Write([]byte{1})
368+
hashOut := hf.Sum(nil)
369+
txid2 := hex.EncodeToString(hashOut)
370+
371+
t.Logf("% x\n", hashOut)
372+
t.Logf("% s\n", txid)
373+
t.Logf("% s\n", txid2)
374+
375+
assert.Equal(t, txid, txid2)
376+
}
377+
341378
var signer msp.SigningIdentity
342379
var signerSerialized []byte
343380

0 commit comments

Comments
 (0)