Skip to content

Commit

Permalink
add test case
Browse files Browse the repository at this point in the history
  • Loading branch information
TATAUFO committed May 21, 2020
1 parent 2ee6fa9 commit e638c7c
Show file tree
Hide file tree
Showing 4 changed files with 463 additions and 29 deletions.
83 changes: 72 additions & 11 deletions crypto/bitcoin/btc_crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 {
Expand Down Expand Up @@ -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) {
Expand All @@ -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))
Expand Down
155 changes: 153 additions & 2 deletions crypto/bitcoin/btc_crypto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand All @@ -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")
}
}

}

0 comments on commit e638c7c

Please sign in to comment.