From e638c7c67df29c1ab5b2026d3615487bc7cfad4f Mon Sep 17 00:00:00 2001 From: TATAUFO Date: Thu, 21 May 2020 14:50:50 +0800 Subject: [PATCH] add test case --- crypto/bitcoin/btc_crypto.go | 83 +++++++++++++-- crypto/bitcoin/btc_crypto_test.go | 155 ++++++++++++++++++++++++++++- crypto/ethereum/eth_crypto.go | 100 ++++++++++++++++--- crypto/ethereum/eth_crypto_test.go | 154 +++++++++++++++++++++++++++- 4 files changed, 463 insertions(+), 29 deletions(-) diff --git a/crypto/bitcoin/btc_crypto.go b/crypto/bitcoin/btc_crypto.go index a9a7801..0923259 100644 --- a/crypto/bitcoin/btc_crypto.go +++ b/crypto/bitcoin/btc_crypto.go @@ -243,7 +243,49 @@ func (e BEngine) Unmarshal(privKeyBytes, pubKeyBytes []byte) (privKey *crypto.Pr } func (e BEngine) unmarshalPrivKey(input []byte) (*crypto.PrivateKey, error) { - return nil, nil + p := crypto.PrivateKey{} + aMap := make(map[string]interface{}) + err := json.Unmarshal(input, &aMap) + if err != nil { + return nil, err + } + p.Source = aMap["source"].(string) + p.SigType = aMap["sigType"].(string) + + if p.Source == e.name { + if p.SigType == crypto.Signature2PublicKey { + pk, err := hex.DecodeString(aMap["privKey"].(string)) + if err != nil { + return nil, err + } + privKey, err := parsePriKey(pk) + if err != nil { + return nil, err + } + p.PriKey = privKey + } else if p.SigType == crypto.MultipleSignatures { + pks := aMap["privKey"].([]interface{}) + var privKeys []interface{} + for _, v := range pks { + pk, err := hex.DecodeString(v.(string)) + if err != nil { + return nil, err + } + privKey, err := parsePriKey(pk) + if err != nil { + return nil, err + } + privKeys = append(privKeys, privKey) + } + p.PriKey = privKeys + } else { + return nil, crypto.ErrSigTypeNotSupport + } + } else { + return nil, crypto.ErrSourceNotMatch + } + + return &p, nil } func (e BEngine) unmarshalPubKey(input []byte) (*crypto.PublicKey, error) { @@ -269,7 +311,7 @@ func (e BEngine) unmarshalPubKey(input []byte) (*crypto.PublicKey, error) { p.PubKey = pubKey.ToECDSA() } else if p.SigType == crypto.MultipleSignatures { pks := aMap["pubKey"].([]interface{}) - var pubKeys []*ecdsa.PublicKey + var pubKeys []interface{} for _, v := range pks { pk, err := hex.DecodeString(v.(string)) if err != nil { @@ -308,7 +350,34 @@ func (e BEngine) Marshal(privKey *crypto.PrivateKey, pubKey *crypto.PublicKey) ( } func (e BEngine) marshalPrivKey(a *crypto.PrivateKey) ([]byte, error) { - return []byte{}, nil + aMap := make(map[string]interface{}) + aMap["source"] = a.Source + aMap["sigType"] = a.SigType + if a.Source == e.name { + if a.SigType == crypto.Signature2PublicKey { + pk := a.PriKey.(*btc.PrivateKey) + bpk := (btc.PrivateKey)(*pk) + aMap["privKey"] = hex.EncodeToString(bpk.Serialize()) + } else if a.SigType == crypto.MultipleSignatures { + switch a.PriKey.(type) { + case []interface{}: + pks := a.PriKey.([]interface{}) + privKey := make([]string, len(pks)) + for i, v := range pks { + pk := v.(*btc.PrivateKey) + bpk := (btc.PrivateKey)(*pk) + privKey[i] = hex.EncodeToString(bpk.Serialize()) + } + aMap["privKey"] = privKey + } + + } else { + return nil, crypto.ErrSigTypeNotSupport + } + } else { + return nil, crypto.ErrSourceNotMatch + } + return json.Marshal(aMap) } func (e BEngine) marshalPubKey(a *crypto.PublicKey) ([]byte, error) { @@ -322,14 +391,6 @@ func (e BEngine) marshalPubKey(a *crypto.PublicKey) ([]byte, error) { aMap["pubKey"] = hex.EncodeToString(bpk.SerializeUncompressed()) } else if a.SigType == crypto.MultipleSignatures { switch a.PubKey.(type) { - case []*ecdsa.PublicKey: - pks := a.PubKey.([]*ecdsa.PublicKey) - pubKey := make([]string, len(pks)) - for i, pk := range pks { - bpk := (btc.PublicKey)(*pk) - pubKey[i] = hex.EncodeToString(bpk.SerializeUncompressed()) - } - aMap["pubKey"] = pubKey case []interface{}: pks := a.PubKey.([]interface{}) pubKey := make([]string, len(pks)) diff --git a/crypto/bitcoin/btc_crypto_test.go b/crypto/bitcoin/btc_crypto_test.go index 5233e94..9911b99 100644 --- a/crypto/bitcoin/btc_crypto_test.go +++ b/crypto/bitcoin/btc_crypto_test.go @@ -377,12 +377,12 @@ func TestEEngine_EncryptKeyMS(t *testing.T) { pks = append(pks, pk1, pk2, pk3) privateKey := &crypto.PrivateKey{Source: crypto.BTC, SigType: crypto.MultipleSignatures, PriKey: pks} - keyJson, err := E.EncryptKey(privateKey, "123") + keyJSON, err := E.EncryptKey(privateKey, "123") if err != nil { t.Error("encrypt key fail", err) } - newPrivateKey, _, err := E.DecryptKey(keyJson, "123") + newPrivateKey, _, err := E.DecryptKey(keyJSON, "123") if err != nil { t.Error("decrypt key fail") } @@ -402,3 +402,154 @@ func TestEEngine_EncryptKeyMS(t *testing.T) { } } + +func TestMarshal(t *testing.T) { + E := New() + + privKey, _, err := E.GenKey(crypto.Signature2PublicKey) + if err != nil { + t.Error(err) + } + + privKeyBytes, err := E.marshalPrivKey(privKey) + if err != nil { + t.Error(err) + } + + uPrivKey, err := E.unmarshalPrivKey(privKeyBytes) + if err != nil { + t.Error(err) + } + + if privKey.Source != uPrivKey.Source { + t.Error("source not match") + } + if privKey.SigType != uPrivKey.SigType { + t.Error("sig type not match") + } + + if privKey.PriKey.(*btc.PrivateKey).D.Cmp(uPrivKey.PriKey.(*btc.PrivateKey).D) != 0 { + t.Error("private key not match") + } + + size := 3 + privKey, _, err = E.GenKey(crypto.MultipleSignatures, size) + if err != nil { + t.Error(err) + } + + privKeyBytes, err = E.marshalPrivKey(privKey) + if err != nil { + t.Error(err) + } + + uPrivKey, err = E.unmarshalPrivKey(privKeyBytes) + if err != nil { + t.Error(err) + } + + if privKey.Source != uPrivKey.Source { + t.Error("source not match") + } + if privKey.SigType != uPrivKey.SigType { + t.Error("sig type not match") + } + + for i := 0; i < size; i++ { + if privKey.PriKey.([]interface{})[i].(*btc.PrivateKey).D.Cmp(uPrivKey.PriKey.([]interface{})[i].(*btc.PrivateKey).D) != 0 { + t.Error("private key not match") + } + } + + _, pubKey, err := E.GenKey(crypto.Signature2PublicKey) + if err != nil { + t.Error(err) + } + + pubKeyBytes, err := E.marshalPubKey(pubKey) + if err != nil { + t.Error(err) + } + + uPubKey, err := E.unmarshalPubKey(pubKeyBytes) + if err != nil { + t.Error(err) + } + + if pubKey.Source != uPubKey.Source { + t.Error("source not match") + } + if pubKey.SigType != uPubKey.SigType { + t.Error("sig type not match") + } + + if pubKey.PubKey.(*ecdsa.PublicKey).X.Cmp(uPubKey.PubKey.(*ecdsa.PublicKey).X) != 0 { + t.Error("private key not match") + } + if pubKey.PubKey.(*ecdsa.PublicKey).Y.Cmp(uPubKey.PubKey.(*ecdsa.PublicKey).Y) != 0 { + t.Error("private key not match") + } + + _, pubKey, err = E.GenKey(crypto.MultipleSignatures, size) + if err != nil { + t.Error(err) + } + + pubKeyBytes, err = E.marshalPubKey(pubKey) + if err != nil { + t.Error(err) + } + + uPubKey, err = E.unmarshalPubKey(pubKeyBytes) + if err != nil { + t.Error(err) + } + + if pubKey.Source != uPubKey.Source { + t.Error("source not match") + } + if pubKey.SigType != uPubKey.SigType { + t.Error("sig type not match") + } + + for i := 0; i < size; i++ { + if pubKey.PubKey.([]interface{})[i].(*ecdsa.PublicKey).X.Cmp(uPubKey.PubKey.([]interface{})[i].(*ecdsa.PublicKey).X) != 0 { + t.Error("public key not match") + } + if pubKey.PubKey.([]interface{})[i].(*ecdsa.PublicKey).Y.Cmp(uPubKey.PubKey.([]interface{})[i].(*ecdsa.PublicKey).Y) != 0 { + t.Error("public key not match") + } + } + + privKey, pubKey, err = E.GenKey(crypto.MultipleSignatures, size) + if err != nil { + t.Error(err) + } + + privKeyBytes, pubKeyBytes, err = E.Marshal(privKey, pubKey) + if err != nil { + t.Error(err) + } + + uPrivKey, uPubKey, err = E.Unmarshal(privKeyBytes, pubKeyBytes) + if err != nil { + t.Error(err) + } + + if pubKey.Source != uPubKey.Source { + t.Error("source not match") + } + if pubKey.SigType != uPubKey.SigType { + t.Error("sig type not match") + } + + for i := 0; i < size; i++ { + if pubKey.PubKey.([]interface{})[i].(*ecdsa.PublicKey).X.Cmp(uPubKey.PubKey.([]interface{})[i].(*ecdsa.PublicKey).X) != 0 { + t.Error("public key not match") + } + if pubKey.PubKey.([]interface{})[i].(*ecdsa.PublicKey).Y.Cmp(uPubKey.PubKey.([]interface{})[i].(*ecdsa.PublicKey).Y) != 0 { + t.Error("public key not match") + } + } + +} diff --git a/crypto/ethereum/eth_crypto.go b/crypto/ethereum/eth_crypto.go index 92c8d1d..4681aa6 100644 --- a/crypto/ethereum/eth_crypto.go +++ b/crypto/ethereum/eth_crypto.go @@ -217,7 +217,49 @@ func (e EEngine) Unmarshal(privKeyBytes, pubKeyBytes []byte) (privKey *crypto.Pr } func (e EEngine) unmarshalPrivKey(input []byte) (*crypto.PrivateKey, error) { - return nil, nil + p := crypto.PrivateKey{} + aMap := make(map[string]interface{}) + err := json.Unmarshal(input, &aMap) + if err != nil { + return nil, err + } + p.Source = aMap["source"].(string) + p.SigType = aMap["sigType"].(string) + + if p.Source == e.name { + if p.SigType == crypto.Signature2PublicKey { + pk, err := hex.DecodeString(aMap["privKey"].(string)) + if err != nil { + return nil, err + } + privKey, err := parsePriKey(pk) + if err != nil { + return nil, err + } + p.PriKey = privKey + } else if p.SigType == crypto.MultipleSignatures { + pks := aMap["privKey"].([]interface{}) + var privKeys []interface{} + for _, v := range pks { + pk, err := hex.DecodeString(v.(string)) + if err != nil { + return nil, err + } + privKey, err := parsePriKey(pk) + if err != nil { + return nil, err + } + privKeys = append(privKeys, privKey) + } + p.PriKey = privKeys + } else { + return nil, crypto.ErrSigTypeNotSupport + } + } else { + return nil, crypto.ErrSourceNotMatch + } + + return &p, nil } func (e EEngine) unmarshalPubKey(input []byte) (*crypto.PublicKey, error) { @@ -237,20 +279,20 @@ func (e EEngine) unmarshalPubKey(input []byte) (*crypto.PublicKey, error) { if err != nil { return nil, err } - pubKey, err := eth.UnmarshalPubkey(pk) + pubKey, err := parsePubKey(pk) if err != nil { return nil, err } p.PubKey = pubKey } else if p.SigType == crypto.MultipleSignatures { pks := aMap["pubKey"].([]interface{}) - var pubKeys []*ecdsa.PublicKey + var pubKeys []interface{} for _, v := range pks { pk, err := hex.DecodeString(v.(string)) if err != nil { return nil, err } - pubKey, err := eth.UnmarshalPubkey(pk) + pubKey, err := parsePubKey(pk) if err != nil { return nil, err } @@ -283,7 +325,38 @@ func (e EEngine) Marshal(privKey *crypto.PrivateKey, pubKey *crypto.PublicKey) ( } func (e EEngine) marshalPrivKey(a *crypto.PrivateKey) ([]byte, error) { - return []byte{}, nil + aMap := make(map[string]interface{}) + aMap["source"] = a.Source + aMap["sigType"] = a.SigType + if a.Source == e.name { + if a.SigType == crypto.Signature2PublicKey { + pk, err := parsePriKey(a.PriKey) + if err != nil { + return nil, err + } + aMap["privKey"] = hex.EncodeToString(eth.FromECDSA(pk)) + } else if a.SigType == crypto.MultipleSignatures { + switch a.PriKey.(type) { + case []interface{}: + pks := a.PriKey.([]interface{}) + privKey := make([]string, len(pks)) + for i, v := range pks { + pk, err := parsePriKey(v) + if err != nil { + return nil, err + } + privKey[i] = hex.EncodeToString(eth.FromECDSA(pk)) + } + aMap["privKey"] = privKey + } + + } else { + return nil, crypto.ErrSigTypeNotSupport + } + } else { + return nil, crypto.ErrSourceNotMatch + } + return json.Marshal(aMap) } func (e EEngine) marshalPubKey(a *crypto.PublicKey) ([]byte, error) { @@ -292,22 +365,21 @@ func (e EEngine) marshalPubKey(a *crypto.PublicKey) ([]byte, error) { aMap["sigType"] = a.SigType if a.Source == e.name { if a.SigType == crypto.Signature2PublicKey { - pk := a.PubKey.(*ecdsa.PublicKey) + pk, err := parsePubKey(a.PubKey) + if err != nil { + return nil, err + } aMap["pubKey"] = hex.EncodeToString(eth.FromECDSAPub(pk)) } else if a.SigType == crypto.MultipleSignatures { switch a.PubKey.(type) { - case []*ecdsa.PublicKey: - pks := a.PubKey.([]*ecdsa.PublicKey) - pubKey := make([]string, len(pks)) - for i, pk := range pks { - pubKey[i] = hex.EncodeToString(eth.FromECDSAPub(pk)) - } - aMap["pubKey"] = pubKey case []interface{}: pks := a.PubKey.([]interface{}) pubKey := make([]string, len(pks)) for i, v := range pks { - pk := v.(*ecdsa.PublicKey) + pk, err := parsePubKey(v) + if err != nil { + return nil, err + } pubKey[i] = hex.EncodeToString(eth.FromECDSAPub(pk)) } aMap["pubKey"] = pubKey diff --git a/crypto/ethereum/eth_crypto_test.go b/crypto/ethereum/eth_crypto_test.go index 0924804..82090ec 100644 --- a/crypto/ethereum/eth_crypto_test.go +++ b/crypto/ethereum/eth_crypto_test.go @@ -372,12 +372,12 @@ func TestEEngine_EncryptKeyMS(t *testing.T) { pks = append(pks, pk1, pk2, pk3) privateKey := &crypto.PrivateKey{Source: crypto.ETH, SigType: crypto.MultipleSignatures, PriKey: pks} - keyJson, err := E.EncryptKey(privateKey, "123") + keyJSON, err := E.EncryptKey(privateKey, "123") if err != nil { t.Error("encrypt key fail", err) } - newPrivateKey, _, err := E.DecryptKey(keyJson, "123") + newPrivateKey, _, err := E.DecryptKey(keyJSON, "123") if err != nil { t.Error("decrypt key fail") } @@ -397,3 +397,153 @@ func TestEEngine_EncryptKeyMS(t *testing.T) { } } + +func TestMarshal(t *testing.T) { + E := New() + + privKey, _, err := E.GenKey(crypto.Signature2PublicKey) + if err != nil { + t.Error(err) + } + + privKeyBytes, err := E.marshalPrivKey(privKey) + if err != nil { + t.Error(err) + } + + uPrivKey, err := E.unmarshalPrivKey(privKeyBytes) + if err != nil { + t.Error(err) + } + + if privKey.Source != uPrivKey.Source { + t.Error("source not match") + } + if privKey.SigType != uPrivKey.SigType { + t.Error("sig type not match") + } + if privKey.PriKey.(*ecdsa.PrivateKey).D.Cmp(uPrivKey.PriKey.(*ecdsa.PrivateKey).D) != 0 { + t.Error("private key not match") + } + + size := 3 + privKey, _, err = E.GenKey(crypto.MultipleSignatures, size) + if err != nil { + t.Error(err) + } + + privKeyBytes, err = E.marshalPrivKey(privKey) + if err != nil { + t.Error(err) + } + + uPrivKey, err = E.unmarshalPrivKey(privKeyBytes) + if err != nil { + t.Error(err) + } + + if privKey.Source != uPrivKey.Source { + t.Error("source not match") + } + if privKey.SigType != uPrivKey.SigType { + t.Error("sig type not match") + } + + for i := 0; i < size; i++ { + if privKey.PriKey.([]interface{})[i].(*ecdsa.PrivateKey).D.Cmp(uPrivKey.PriKey.([]interface{})[i].(*ecdsa.PrivateKey).D) != 0 { + t.Error("private key not match") + } + } + + _, pubKey, err := E.GenKey(crypto.Signature2PublicKey) + if err != nil { + t.Error(err) + } + + pubKeyBytes, err := E.marshalPubKey(pubKey) + if err != nil { + t.Error(err) + } + + uPubKey, err := E.unmarshalPubKey(pubKeyBytes) + if err != nil { + t.Error(err) + } + + if pubKey.Source != uPubKey.Source { + t.Error("source not match") + } + if pubKey.SigType != uPubKey.SigType { + t.Error("sig type not match") + } + + if pubKey.PubKey.(*ecdsa.PublicKey).X.Cmp(uPubKey.PubKey.(*ecdsa.PublicKey).X) != 0 { + t.Error("private key not match") + } + if pubKey.PubKey.(*ecdsa.PublicKey).Y.Cmp(uPubKey.PubKey.(*ecdsa.PublicKey).Y) != 0 { + t.Error("private key not match") + } + + _, pubKey, err = E.GenKey(crypto.MultipleSignatures, size) + if err != nil { + t.Error(err) + } + + pubKeyBytes, err = E.marshalPubKey(pubKey) + if err != nil { + t.Error(err) + } + + uPubKey, err = E.unmarshalPubKey(pubKeyBytes) + if err != nil { + t.Error(err) + } + + if pubKey.Source != uPubKey.Source { + t.Error("source not match") + } + if pubKey.SigType != uPubKey.SigType { + t.Error("sig type not match") + } + + for i := 0; i < size; i++ { + if pubKey.PubKey.([]interface{})[i].(*ecdsa.PublicKey).X.Cmp(uPubKey.PubKey.([]interface{})[i].(*ecdsa.PublicKey).X) != 0 { + t.Error("public key not match") + } + if pubKey.PubKey.([]interface{})[i].(*ecdsa.PublicKey).Y.Cmp(uPubKey.PubKey.([]interface{})[i].(*ecdsa.PublicKey).Y) != 0 { + t.Error("public key not match") + } + } + + privKey, pubKey, err = E.GenKey(crypto.MultipleSignatures, size) + if err != nil { + t.Error(err) + } + + privKeyBytes, pubKeyBytes, err = E.Marshal(privKey, pubKey) + if err != nil { + t.Error(err) + } + + uPrivKey, uPubKey, err = E.Unmarshal(privKeyBytes, pubKeyBytes) + if err != nil { + t.Error(err) + } + + if pubKey.Source != uPubKey.Source { + t.Error("source not match") + } + if pubKey.SigType != uPubKey.SigType { + t.Error("sig type not match") + } + + for i := 0; i < size; i++ { + if pubKey.PubKey.([]interface{})[i].(*ecdsa.PublicKey).X.Cmp(uPubKey.PubKey.([]interface{})[i].(*ecdsa.PublicKey).X) != 0 { + t.Error("public key not match") + } + if pubKey.PubKey.([]interface{})[i].(*ecdsa.PublicKey).Y.Cmp(uPubKey.PubKey.([]interface{})[i].(*ecdsa.PublicKey).Y) != 0 { + t.Error("public key not match") + } + } + +}