From 788cfe2d2f6f9721ebbdc05231d489964f340e1e Mon Sep 17 00:00:00 2001 From: Philip Kwan Date: Tue, 23 Jul 2019 12:16:22 +0800 Subject: [PATCH] fix crypto.go for 2 issues: 1) add a SHA3-256 hash of the message before passing it to be signed for the input fulfillment 2) use base64url encode, instead of base58 encode, to the resulting fulfillment string after it is signed --- pkg/transaction/crypto.go | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/pkg/transaction/crypto.go b/pkg/transaction/crypto.go index 02835f4..59925ad 100644 --- a/pkg/transaction/crypto.go +++ b/pkg/transaction/crypto.go @@ -7,9 +7,10 @@ import ( "strings" "github.com/go-interledger/cryptoconditions" - "github.com/mr-tron/base58/base58" "github.com/pkg/errors" "golang.org/x/crypto/ed25519" + "golang.org/x/crypto/sha3" + "github.com/kalaspuffar/base64url" ) type KeyPair struct { @@ -71,9 +72,13 @@ func (t *Transaction) Sign(keyPairs []*KeyPair) error { bytes_to_sign := []byte(serializedTxn.String()) + h3_256 := sha3.New256() + h3_256.Write(bytes_to_sign) + h3_256Hash := h3_256.Sum(nil) + // rand reader is ignored within Sign method; crypto.Hash(0) is sanity check to // make sure bytes_to_sign is not hashed already - ed25519.PrivateKey cannot sign hashed msg - signature, err := keyPair.PrivateKey.Sign(rand.Reader, bytes_to_sign[:], crypto.Hash(0)) + signature, err := keyPair.PrivateKey.Sign(rand.Reader, h3_256Hash, crypto.Hash(0)) // https://tools.ietf.org/html/draft-thomas-crypto-conditions-03#section-8.5 ed25519Fulfillment, err := cryptoconditions.NewEd25519Sha256(keyPair.PublicKey, signature) @@ -87,7 +92,7 @@ func (t *Transaction) Sign(keyPairs []*KeyPair) error { if err != nil { return err } - ffSt := base58.Encode(ff) + ffSt := base64url.Encode(ff) signedTx.Inputs[idx].Fulfillment = &ffSt } //Create ID of transaction (hash of body)