Skip to content

Commit

Permalink
refactor account inspect
Browse files Browse the repository at this point in the history
  • Loading branch information
TATAUFO committed May 28, 2020
1 parent 8253e6a commit 6184a0d
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 9 deletions.
8 changes: 6 additions & 2 deletions cmd/pdu/account.go
Expand Up @@ -113,8 +113,12 @@ func inspect() error {
if err != nil {
return err
}
fmt.Println("private key: ", priKey.PriKey)
fmt.Println("publick key: ", pubKey.PubKey)
privKeyM, pubKeyM, err := utils.DisplayKey(priKey, pubKey)
if err != nil {
return err
}
fmt.Println("private key: ", privKeyM)
fmt.Println("publick key: ", pubKeyM)
return nil
}

Expand Down
5 changes: 5 additions & 0 deletions crypto/bitcoin/btc_crypto.go
Expand Up @@ -191,6 +191,11 @@ func (e BEngine) Marshal(privKey *crypto.PrivateKey, pubKey *crypto.PublicKey) (
return crypto.Marshal(e.name, privKey, pubKey, parseKeyToString, parsePubKeyToString)
}

// Display display private & public key
func (e BEngine) DisplayKey(privKey *crypto.PrivateKey, pubKey *crypto.PublicKey) (map[string]interface{}, map[string]interface{}, error) {
return crypto.DisplayKey(e.name, privKey, pubKey, parseKeyToString, parsePubKeyToString)
}

// EncryptKey encryptKey into file
func (e BEngine) EncryptKey(priKey *crypto.PrivateKey, pass string) ([]byte, error) {
return crypto.EncryptKey(e.name, priKey, pass, privKeyToKeyBytes)
Expand Down
41 changes: 35 additions & 6 deletions crypto/crypto.go
Expand Up @@ -93,6 +93,7 @@ type Engine interface {
Marshal(*PrivateKey, *PublicKey) ([]byte, []byte, error)
EncryptKey(*PrivateKey, string) ([]byte, error)
DecryptKey([]byte, string) (*PrivateKey, *PublicKey, error)
DisplayKey(*PrivateKey, *PublicKey) (map[string]interface{}, map[string]interface{}, error)
}

// EncryptedPrivateKey is encrypted private key in json
Expand Down Expand Up @@ -418,20 +419,47 @@ type funcParsePubKeyToString func(interface{}) (string, error)

// Marshal marshal private & public key to json
func Marshal(source string, privKey *PrivateKey, pubKey *PublicKey, parseKeyToString funcParseKeyToString, parsePubKeyToString funcParsePubKeyToString) (privKeyBytes []byte, pubKeyBytes []byte, err error) {
m := make(map[string]interface{})
if privKey != nil {
if privKeyBytes, err = marshalPrivKey(source, privKey, parseKeyToString); err != nil {
m, err = DisplayPrivKey(source, privKey, parseKeyToString)
if err != nil {
return
}
if privKeyBytes, err = json.Marshal(m); err != nil {
return
}
}
if pubKey != nil {
if pubKeyBytes, err = marshalPubKey(source, pubKey, parsePubKeyToString); err != nil {
m, err = DisplayPubKey(source, pubKey, parsePubKeyToString)
if err != nil {
return
}
if pubKeyBytes, err = json.Marshal(m); err != nil {
return
}
}
return
}

// DisplayKey display private & public key
func DisplayKey(source string, privKey *PrivateKey, pubKey *PublicKey, parseKeyToString funcParseKeyToString, parsePubKeyToString funcParsePubKeyToString) (privKeyM, pubKeyM map[string]interface{}, err error) {
if privKey != nil {
privKeyM, err = DisplayPrivKey(source, privKey, parseKeyToString)
if err != nil {
return
}
}
if pubKey != nil {
pubKeyM, err = DisplayPubKey(source, pubKey, parsePubKeyToString)
if err != nil {
return
}
}
return
}

func marshalPrivKey(source string, a *PrivateKey, parseKeyToString funcParseKeyToString) ([]byte, error) {
// DisplayPrivKey display the content of private key
func DisplayPrivKey(source string, a *PrivateKey, parseKeyToString funcParseKeyToString) (map[string]interface{}, error) {
aMap := make(map[string]interface{})
aMap["source"] = a.Source
aMap["sigType"] = a.SigType
Expand Down Expand Up @@ -464,10 +492,11 @@ func marshalPrivKey(source string, a *PrivateKey, parseKeyToString funcParseKeyT
} else {
return nil, ErrSourceNotMatch
}
return json.Marshal(aMap)
return aMap, nil
}

func marshalPubKey(source string, a *PublicKey, parsePubKeyToString funcParsePubKeyToString) ([]byte, error) {
// DisplayPubKey display the content of public key
func DisplayPubKey(source string, a *PublicKey, parsePubKeyToString funcParsePubKeyToString) (map[string]interface{}, error) {
aMap := make(map[string]interface{})
aMap["source"] = a.Source
aMap["sigType"] = a.SigType
Expand Down Expand Up @@ -500,5 +529,5 @@ func marshalPubKey(source string, a *PublicKey, parsePubKeyToString funcParsePub
} else {
return nil, ErrSourceNotMatch
}
return json.Marshal(aMap)
return aMap, nil
}
7 changes: 6 additions & 1 deletion crypto/ethereum/eth_crypto.go
Expand Up @@ -165,10 +165,15 @@ func (e EEngine) Unmarshal(privKeyBytes, pubKeyBytes []byte) (privKey *crypto.Pr
}

// Marshal marshal private & public key to json
func (e EEngine) Marshal(privKey *crypto.PrivateKey, pubKey *crypto.PublicKey) (privKeyBytes []byte, pubKeyBytes []byte, err error) {
func (e EEngine) Marshal(privKey *crypto.PrivateKey, pubKey *crypto.PublicKey) ([]byte, []byte, error) {
return crypto.Marshal(e.name, privKey, pubKey, parseKeyToString, parsePubKeyToString)
}

// Display display private & public key
func (e EEngine) DisplayKey(privKey *crypto.PrivateKey, pubKey *crypto.PublicKey) (map[string]interface{}, map[string]interface{}, error) {
return crypto.DisplayKey(e.name, privKey, pubKey, parseKeyToString, parsePubKeyToString)
}

// EncryptKey encryptKey into file
func (e EEngine) EncryptKey(priKey *crypto.PrivateKey, pass string) ([]byte, error) {
return crypto.EncryptKey(e.name, priKey, pass, privKeyToKeyBytes)
Expand Down
5 changes: 5 additions & 0 deletions crypto/pdu/crypto.go
Expand Up @@ -183,6 +183,11 @@ func (e PEngine) Marshal(privKey *crypto.PrivateKey, pubKey *crypto.PublicKey) (
return crypto.Marshal(e.name, privKey, pubKey, parseKeyToString, parsePubKeyToString)
}

// Display display private & public key
func (e PEngine) DisplayKey(privKey *crypto.PrivateKey, pubKey *crypto.PublicKey) (map[string]interface{}, map[string]interface{}, error) {
return crypto.DisplayKey(e.name, privKey, pubKey, parseKeyToString, parsePubKeyToString)
}

// EncryptKey encryptKey into file
func (e PEngine) EncryptKey(priKey *crypto.PrivateKey, pass string) ([]byte, error) {
return crypto.EncryptKey(e.name, priKey, pass, privKeyToKeyBytes)
Expand Down
10 changes: 10 additions & 0 deletions crypto/utils/utils.go
Expand Up @@ -59,3 +59,13 @@ func DecryptKey(keyJSON []byte, passwd string) (*crypto.PrivateKey, *crypto.Publ

return engine.DecryptKey(keyJSON, passwd)
}

// DisplayKey decrypt private key from keyJSON file
func DisplayKey(privKey *crypto.PrivateKey, pubKey *crypto.PublicKey) (map[string]interface{}, map[string]interface{}, error) {
var engine crypto.Engine
engine, err := SelectEngine(privKey.Source)
if err != nil {
return nil, nil, err
}
return engine.DisplayKey(privKey, pubKey)
}

0 comments on commit 6184a0d

Please sign in to comment.