change the interface not to use pointer

this will save some conversion of calling function

Signed-off-by: Leo Chen <leo@harmony.one>
pull/347/head
Leo Chen 6 years ago
parent a84b645cd8
commit f814a0b630
  1. 8
      internal/utils/utils.go
  2. 6
      internal/utils/utils_test.go

@ -156,7 +156,7 @@ func Load(path string, v interface{}) error {
} }
// LoadPrivateKey parses the key string in base64 format and return PrivKey // LoadPrivateKey parses the key string in base64 format and return PrivKey
func LoadPrivateKey(key string) (*p2p_crypto.PrivKey, error) { func LoadPrivateKey(key string) (p2p_crypto.PrivKey, error) {
if key != "" { if key != "" {
k1, err := p2p_crypto.ConfigDecodeKey(key) k1, err := p2p_crypto.ConfigDecodeKey(key)
if err != nil { if err != nil {
@ -166,15 +166,15 @@ func LoadPrivateKey(key string) (*p2p_crypto.PrivKey, error) {
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to unmarshal private key: %v", err) return nil, fmt.Errorf("failed to unmarshal private key: %v", err)
} }
return &priKey, nil return priKey, nil
} }
return nil, fmt.Errorf("empty key string") return nil, fmt.Errorf("empty key string")
} }
// SavePrivateKey convert the PrivKey to base64 format and return string // SavePrivateKey convert the PrivKey to base64 format and return string
func SavePrivateKey(key *p2p_crypto.PrivKey) (string, error) { func SavePrivateKey(key p2p_crypto.PrivKey) (string, error) {
if key != nil { if key != nil {
b, err := p2p_crypto.MarshalPrivateKey(*key) b, err := p2p_crypto.MarshalPrivateKey(key)
if err != nil { if err != nil {
return "", fmt.Errorf("failed to marshal private key: %v", err) return "", fmt.Errorf("failed to marshal private key: %v", err)
} }

@ -106,7 +106,7 @@ func TestSaveLoadPrivateKey(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("failed to generate p2p key: %v", err) t.Fatalf("failed to generate p2p key: %v", err)
} }
str, err := SavePrivateKey(&pk) str, err := SavePrivateKey(pk)
if err != nil { if err != nil {
t.Fatalf("failed to save private key: %v", err) t.Fatalf("failed to save private key: %v", err)
} }
@ -116,10 +116,10 @@ func TestSaveLoadPrivateKey(t *testing.T) {
t.Fatalf("failed to load key: %v", err) t.Fatalf("failed to load key: %v", err)
} }
if !crypto.KeyEqual(pk, *pk1) { if !crypto.KeyEqual(pk, pk1) {
t.Errorf("loaded key is not right") t.Errorf("loaded key is not right")
b1, _ := pk.Bytes() b1, _ := pk.Bytes()
b2, _ := (*pk1).Bytes() b2, _ := pk1.Bytes()
t.Errorf("expecting pk: %v\n", b1) t.Errorf("expecting pk: %v\n", b1)
t.Errorf("got pk1: %v\n", b2) t.Errorf("got pk1: %v\n", b2)
} }

Loading…
Cancel
Save