From a4018792414f1c97eff911d5630c86073527be2c Mon Sep 17 00:00:00 2001 From: Alex Guo Date: Tue, 8 Jun 2021 22:38:56 +0000 Subject: [PATCH 1/6] address issue #198, converting MIRACL signature to herumi signature --- .../blobbercore/handler/storage_handler.go | 2 +- code/go/0chain.net/core/encryption/keys.go | 26 +++++++++++++++++++ .../0chain.net/core/encryption/keys_test.go | 19 ++++++++++++++ 3 files changed, 46 insertions(+), 1 deletion(-) diff --git a/code/go/0chain.net/blobbercore/handler/storage_handler.go b/code/go/0chain.net/blobbercore/handler/storage_handler.go index 92b924484..b4517bd31 100644 --- a/code/go/0chain.net/blobbercore/handler/storage_handler.go +++ b/code/go/0chain.net/blobbercore/handler/storage_handler.go @@ -700,7 +700,7 @@ func (fsh *StorageHandler) CalculateHash(ctx context.Context, r *http.Request) ( // verifySignatureFromRequest verifyes signature passed as common.ClientSignatureHeader header. func verifySignatureFromRequest(r *http.Request, pbK string) (bool, error) { - sign := r.Header.Get(common.ClientSignatureHeader) + sign := encryption.MiraclToHerumiSig(r.Header.Get(common.ClientSignatureHeader)) if len(sign) < 64 { return false, nil } diff --git a/code/go/0chain.net/core/encryption/keys.go b/code/go/0chain.net/core/encryption/keys.go index 809259866..2018df9e0 100644 --- a/code/go/0chain.net/core/encryption/keys.go +++ b/code/go/0chain.net/core/encryption/keys.go @@ -3,6 +3,7 @@ package encryption import ( "bufio" "io" + "strings" "0chain.net/core/common" "0chain.net/core/config" @@ -66,3 +67,28 @@ func MiraclToHerumiPK(pk string) string { } return p.SerializeToHexStr() } + +// Converts signature 'sig' to format that the herumi/bls library likes. +// zwallets are using MIRACL library which send a MIRACL signature not herumi +// lib. +// +// If the 'sig' was not in MIRACL format, we just return the original sig. +var miraclExampleSig = `(0d4dbad6d2586d5e01b6b7fbad77e4adfa81212c52b4a0b885e19c58e0944764,110061aa16d5ba36eef0ad4503be346908d3513c0a2aedfd0d2923411b420eca)` +func MiraclToHerumiSig(sig string) string { + if len(sig) <= 2 { + return sig + } + if sig[0] != miraclExampleSig[0] { + return sig + } + withoutParens := sig[1: (len(sig)-1) ] + comma := strings.Index(withoutParens, ",") + if comma < 0 { + return "00" + } + n1 := withoutParens[0:comma] + n2 := withoutParens[ (comma+1) : len(withoutParens)] + var sign bls.Sign + sign.SetHexString("1 " + n1 + " " + n2) + return sign.SerializeToHexStr() +} diff --git a/code/go/0chain.net/core/encryption/keys_test.go b/code/go/0chain.net/core/encryption/keys_test.go index 050c8e8ca..561089e00 100644 --- a/code/go/0chain.net/core/encryption/keys_test.go +++ b/code/go/0chain.net/core/encryption/keys_test.go @@ -1,6 +1,7 @@ package encryption import ( + "fmt" "testing" "github.com/herumi/bls-go-binary/bls" "github.com/stretchr/testify/require" @@ -17,3 +18,21 @@ func TestMiraclToHerumiPK(t *testing.T) { err := pk.DeserializeHexStr(pk1) require.NoError(t, err) } + +func TestMiraclToHerumiSig(t *testing.T) { + miraclsig1 := `(0d4dbad6d2586d5e01b6b7fbad77e4adfa81212c52b4a0b885e19c58e0944764,110061aa16d5ba36eef0ad4503be346908d3513c0a2aedfd0d2923411b420eca)` + sig1 := MiraclToHerumiSig(miraclsig1) + + require.EqualValues(t, sig1, "644794e0589ce185b8a0b4522c2181faade477adfbb7b6015e6d58d2d6ba4d0d") + + // Assert DeserializeHexStr works on the output of MiraclToHerumiSig + var sig bls.Sign + err := sig.DeserializeHexStr(sig1) + require.NoError(t, err) + + // Test that passing in normal herumi sig just gets back the original. + sig2 := MiraclToHerumiSig(sig1) + if sig1 != sig2 { + panic("Sigs should've been the same") + } +} From 1e166a3655c7eda33e409dac4f35fae716cb593a Mon Sep 17 00:00:00 2001 From: Alex Guo Date: Tue, 8 Jun 2021 22:40:48 +0000 Subject: [PATCH 2/6] fix indent in keys_test.go TestMiraclToHerumiSig --- .../0chain.net/core/encryption/keys_test.go | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/code/go/0chain.net/core/encryption/keys_test.go b/code/go/0chain.net/core/encryption/keys_test.go index 561089e00..d731aad3c 100644 --- a/code/go/0chain.net/core/encryption/keys_test.go +++ b/code/go/0chain.net/core/encryption/keys_test.go @@ -20,19 +20,19 @@ func TestMiraclToHerumiPK(t *testing.T) { } func TestMiraclToHerumiSig(t *testing.T) { - miraclsig1 := `(0d4dbad6d2586d5e01b6b7fbad77e4adfa81212c52b4a0b885e19c58e0944764,110061aa16d5ba36eef0ad4503be346908d3513c0a2aedfd0d2923411b420eca)` - sig1 := MiraclToHerumiSig(miraclsig1) + miraclsig1 := `(0d4dbad6d2586d5e01b6b7fbad77e4adfa81212c52b4a0b885e19c58e0944764,110061aa16d5ba36eef0ad4503be346908d3513c0a2aedfd0d2923411b420eca)` + sig1 := MiraclToHerumiSig(miraclsig1) require.EqualValues(t, sig1, "644794e0589ce185b8a0b4522c2181faade477adfbb7b6015e6d58d2d6ba4d0d") - // Assert DeserializeHexStr works on the output of MiraclToHerumiSig - var sig bls.Sign - err := sig.DeserializeHexStr(sig1) - require.NoError(t, err) + // Assert DeserializeHexStr works on the output of MiraclToHerumiSig + var sig bls.Sign + err := sig.DeserializeHexStr(sig1) + require.NoError(t, err) - // Test that passing in normal herumi sig just gets back the original. - sig2 := MiraclToHerumiSig(sig1) - if sig1 != sig2 { - panic("Sigs should've been the same") - } + // Test that passing in normal herumi sig just gets back the original. + sig2 := MiraclToHerumiSig(sig1) + if sig1 != sig2 { + panic("Sigs should've been the same") + } } From 1964bc46ff67888b0374a82ed0c0072857efa29f Mon Sep 17 00:00:00 2001 From: Alex Guo Date: Tue, 8 Jun 2021 23:08:21 +0000 Subject: [PATCH 3/6] fix lint by deleting unused import fmt --- code/go/0chain.net/core/encryption/keys_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/code/go/0chain.net/core/encryption/keys_test.go b/code/go/0chain.net/core/encryption/keys_test.go index d731aad3c..a36c40ceb 100644 --- a/code/go/0chain.net/core/encryption/keys_test.go +++ b/code/go/0chain.net/core/encryption/keys_test.go @@ -1,7 +1,6 @@ package encryption import ( - "fmt" "testing" "github.com/herumi/bls-go-binary/bls" "github.com/stretchr/testify/require" From 23e0ec6ebcfb7e65b378e16dc4241cb05d7be36d Mon Sep 17 00:00:00 2001 From: Alex Guo Date: Tue, 8 Jun 2021 23:19:13 +0000 Subject: [PATCH 4/6] fix lint by checking for error return of SexHexString --- code/go/0chain.net/core/encryption/keys.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/code/go/0chain.net/core/encryption/keys.go b/code/go/0chain.net/core/encryption/keys.go index 2018df9e0..7eaefbf53 100644 --- a/code/go/0chain.net/core/encryption/keys.go +++ b/code/go/0chain.net/core/encryption/keys.go @@ -89,6 +89,9 @@ func MiraclToHerumiSig(sig string) string { n1 := withoutParens[0:comma] n2 := withoutParens[ (comma+1) : len(withoutParens)] var sign bls.Sign - sign.SetHexString("1 " + n1 + " " + n2) + err := sign.SetHexString("1 " + n1 + " " + n2) + if err != nil { + Logger.Error("MiraclToHerumiSig: " + err.Error()) + } return sign.SerializeToHexStr() } From 4b812815b40f5d2fa1e8eb3eee9c68cf91df277a Mon Sep 17 00:00:00 2001 From: Alex Guo Date: Tue, 8 Jun 2021 23:26:33 +0000 Subject: [PATCH 5/6] fix lint by making this slice more beautiful --- code/go/0chain.net/core/encryption/keys.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/code/go/0chain.net/core/encryption/keys.go b/code/go/0chain.net/core/encryption/keys.go index 7eaefbf53..eef6876a4 100644 --- a/code/go/0chain.net/core/encryption/keys.go +++ b/code/go/0chain.net/core/encryption/keys.go @@ -87,7 +87,7 @@ func MiraclToHerumiSig(sig string) string { return "00" } n1 := withoutParens[0:comma] - n2 := withoutParens[ (comma+1) : len(withoutParens)] + n2 := withoutParens[(comma+1):] var sign bls.Sign err := sign.SetHexString("1 " + n1 + " " + n2) if err != nil { From 7468b0e220bf254b0ac636750da300c2649b0f6f Mon Sep 17 00:00:00 2001 From: Alex Guo Date: Tue, 8 Jun 2021 23:43:03 +0000 Subject: [PATCH 6/6] address andrenerds comments --- code/go/0chain.net/core/encryption/keys.go | 2 +- code/go/0chain.net/core/encryption/keys_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/code/go/0chain.net/core/encryption/keys.go b/code/go/0chain.net/core/encryption/keys.go index eef6876a4..e6e874fe6 100644 --- a/code/go/0chain.net/core/encryption/keys.go +++ b/code/go/0chain.net/core/encryption/keys.go @@ -73,7 +73,7 @@ func MiraclToHerumiPK(pk string) string { // lib. // // If the 'sig' was not in MIRACL format, we just return the original sig. -var miraclExampleSig = `(0d4dbad6d2586d5e01b6b7fbad77e4adfa81212c52b4a0b885e19c58e0944764,110061aa16d5ba36eef0ad4503be346908d3513c0a2aedfd0d2923411b420eca)` +const miraclExampleSig = `(0d4dbad6d2586d5e01b6b7fbad77e4adfa81212c52b4a0b885e19c58e0944764,110061aa16d5ba36eef0ad4503be346908d3513c0a2aedfd0d2923411b420eca)` func MiraclToHerumiSig(sig string) string { if len(sig) <= 2 { return sig diff --git a/code/go/0chain.net/core/encryption/keys_test.go b/code/go/0chain.net/core/encryption/keys_test.go index a36c40ceb..309c8616b 100644 --- a/code/go/0chain.net/core/encryption/keys_test.go +++ b/code/go/0chain.net/core/encryption/keys_test.go @@ -32,6 +32,6 @@ func TestMiraclToHerumiSig(t *testing.T) { // Test that passing in normal herumi sig just gets back the original. sig2 := MiraclToHerumiSig(sig1) if sig1 != sig2 { - panic("Sigs should've been the same") + panic("Signatures should be the same.") } }