From e191fb3924cdc0a3e46371854623a19557ab78a5 Mon Sep 17 00:00:00 2001 From: Junlin Gao Date: Wed, 11 Dec 2019 17:02:48 -0800 Subject: [PATCH 1/5] Add serialize pubKey and privateKey to String function --- crypto/crypto.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/crypto/crypto.go b/crypto/crypto.go index d1186e7..63199b2 100644 --- a/crypto/crypto.go +++ b/crypto/crypto.go @@ -50,6 +50,22 @@ func ToPubKey(pubKey string) (ic.PubKey, error) { return ic.UnmarshalSecp256k1PublicKey(raw) } +func FromPubKey(pubKey ic.PubKey) (string, error) { + pkb, err := ic.MarshalPublicKey(pubKey) + if err != nil { + return "", err + } + return base64.StdEncoding.EncodeToString(pkb), nil +} + +func FromPrivateKeyToString(privKey ic.PrivKey) (string, error) { + prkb, err := ic.MarshalPrivateKey(privKey) + if err != nil { + return "", err + } + return base64.StdEncoding.EncodeToString(prkb), nil +} + // Secp256k1 private key string to ic.PrivKey interface func ToPrivKeyRaw(privKey []byte) (ic.PrivKey, error) { return ic.UnmarshalSecp256k1PrivateKey(privKey) From 83ec10ff60ae37c67987bedd05e2c38479c10037 Mon Sep 17 00:00:00 2001 From: Junlin Gao Date: Wed, 11 Dec 2019 17:05:11 -0800 Subject: [PATCH 2/5] change function name --- crypto/crypto.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crypto/crypto.go b/crypto/crypto.go index 63199b2..7653c59 100644 --- a/crypto/crypto.go +++ b/crypto/crypto.go @@ -58,7 +58,7 @@ func FromPubKey(pubKey ic.PubKey) (string, error) { return base64.StdEncoding.EncodeToString(pkb), nil } -func FromPrivateKeyToString(privKey ic.PrivKey) (string, error) { +func FromPrivKey(privKey ic.PrivKey) (string, error) { prkb, err := ic.MarshalPrivateKey(privKey) if err != nil { return "", err From 7ea9ac5e7da53b3ec075f0fc243e344a11a71a95 Mon Sep 17 00:00:00 2001 From: Junlin Gao Date: Wed, 11 Dec 2019 17:21:03 -0800 Subject: [PATCH 3/5] add testcase --- crypto/crypto.go | 2 +- crypto/crypto_test.go | 48 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/crypto/crypto.go b/crypto/crypto.go index 7653c59..a4763c2 100644 --- a/crypto/crypto.go +++ b/crypto/crypto.go @@ -47,7 +47,7 @@ func ToPubKey(pubKey string) (ic.PubKey, error) { if err != nil { return nil, err } - return ic.UnmarshalSecp256k1PublicKey(raw) + return ic.UnmarshalPublicKey(raw) } func FromPubKey(pubKey ic.PubKey) (string, error) { diff --git a/crypto/crypto_test.go b/crypto/crypto_test.go index e34fbac..be61f7d 100644 --- a/crypto/crypto_test.go +++ b/crypto/crypto_test.go @@ -1,6 +1,7 @@ package crypto import ( + "bytes" "testing" ledgerPb "github.com/tron-us/go-btfs-common/protos/ledger" @@ -55,3 +56,50 @@ func TestEncryptDecrypt(t *testing.T) { t.Errorf("Decrypt failed") } } + +func TestSerializeDeserializeKey(t *testing.T) { + privKey, err := ToPrivKey(KeyString) + if err != nil { + t.Error("ToPrivKey failed") + return + } + privKeyString, err := FromPrivKey(privKey) + if err != nil { + t.Error("FromPrivKey failed") + return + } + if privKeyString != KeyString { + t.Error("serialize and deserialize private key fail") + return + } + + pubKey := privKey.GetPublic() + pubKeyString, err := FromPubKey(pubKey) + if err != nil { + t.Error("FromPubKey failed") + return + } + + nPubKey, err := ToPubKey(pubKeyString) + if err != nil { + t.Error("ToPubKey failed") + return + } + + pubkeyRaw, err := pubKey.Raw() + if err != nil { + t.Error("get pubkey raw failed") + return + } + nPubkeyRaw, err := nPubKey.Raw() + if err != nil { + t.Error("get PubKey raw failed") + return + } + + if bytes.Compare(pubkeyRaw, nPubkeyRaw) != 0 { + t.Error("serialize and deserialize pub key fail") + return + } + +} From 8f7eabb51d347a49d3b410f8599f71d473907496 Mon Sep 17 00:00:00 2001 From: Junlin Gao Date: Wed, 11 Dec 2019 17:59:19 -0800 Subject: [PATCH 4/5] add zap.error --- crypto/crypto_test.go | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/crypto/crypto_test.go b/crypto/crypto_test.go index be61f7d..c5c0f20 100644 --- a/crypto/crypto_test.go +++ b/crypto/crypto_test.go @@ -2,6 +2,7 @@ package crypto import ( "bytes" + "go.uber.org/zap" "testing" ledgerPb "github.com/tron-us/go-btfs-common/protos/ledger" @@ -60,45 +61,45 @@ func TestEncryptDecrypt(t *testing.T) { func TestSerializeDeserializeKey(t *testing.T) { privKey, err := ToPrivKey(KeyString) if err != nil { - t.Error("ToPrivKey failed") + t.Error("ToPrivKey failed", zap.Error(err)) return } privKeyString, err := FromPrivKey(privKey) if err != nil { - t.Error("FromPrivKey failed") + t.Error("FromPrivKey failed", zap.Error(err)) return } if privKeyString != KeyString { - t.Error("serialize and deserialize private key fail") + t.Error("serialize and deserialize private key fail", zap.Error(err)) return } pubKey := privKey.GetPublic() pubKeyString, err := FromPubKey(pubKey) if err != nil { - t.Error("FromPubKey failed") + t.Error("FromPubKey failed", zap.Error(err)) return } nPubKey, err := ToPubKey(pubKeyString) if err != nil { - t.Error("ToPubKey failed") + t.Error("ToPubKey failed", zap.Error(err)) return } pubkeyRaw, err := pubKey.Raw() if err != nil { - t.Error("get pubkey raw failed") + t.Error("get pubkey raw failed", zap.Error(err)) return } nPubkeyRaw, err := nPubKey.Raw() if err != nil { - t.Error("get PubKey raw failed") + t.Error("get PubKey raw failed", zap.Error(err)) return } if bytes.Compare(pubkeyRaw, nPubkeyRaw) != 0 { - t.Error("serialize and deserialize pub key fail") + t.Error("serialize and deserialize pub key fail", zap.Error(err)) return } From 81b11e18437d47c07c6ca96bf402d4a3c630edf3 Mon Sep 17 00:00:00 2001 From: Junlin Gao Date: Wed, 11 Dec 2019 18:05:00 -0800 Subject: [PATCH 5/5] print error of test --- crypto/crypto_test.go | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/crypto/crypto_test.go b/crypto/crypto_test.go index c5c0f20..a02da94 100644 --- a/crypto/crypto_test.go +++ b/crypto/crypto_test.go @@ -2,7 +2,6 @@ package crypto import ( "bytes" - "go.uber.org/zap" "testing" ledgerPb "github.com/tron-us/go-btfs-common/protos/ledger" @@ -61,45 +60,45 @@ func TestEncryptDecrypt(t *testing.T) { func TestSerializeDeserializeKey(t *testing.T) { privKey, err := ToPrivKey(KeyString) if err != nil { - t.Error("ToPrivKey failed", zap.Error(err)) + t.Error("ToPrivKey failed", err) return } privKeyString, err := FromPrivKey(privKey) if err != nil { - t.Error("FromPrivKey failed", zap.Error(err)) + t.Error("FromPrivKey failed", err) return } if privKeyString != KeyString { - t.Error("serialize and deserialize private key fail", zap.Error(err)) + t.Error("serialize and deserialize private key fail", err) return } pubKey := privKey.GetPublic() pubKeyString, err := FromPubKey(pubKey) if err != nil { - t.Error("FromPubKey failed", zap.Error(err)) + t.Error("FromPubKey failed", err) return } nPubKey, err := ToPubKey(pubKeyString) if err != nil { - t.Error("ToPubKey failed", zap.Error(err)) + t.Error("ToPubKey failed", err) return } pubkeyRaw, err := pubKey.Raw() if err != nil { - t.Error("get pubkey raw failed", zap.Error(err)) + t.Error("get pubkey raw failed", err) return } nPubkeyRaw, err := nPubKey.Raw() if err != nil { - t.Error("get PubKey raw failed", zap.Error(err)) + t.Error("get PubKey raw failed", err) return } if bytes.Compare(pubkeyRaw, nPubkeyRaw) != 0 { - t.Error("serialize and deserialize pub key fail", zap.Error(err)) + t.Error("serialize and deserialize pub key fail", err) return }