Skip to content

Commit

Permalink
modify for supporting changed ToByte function's return value(s)
Browse files Browse the repository at this point in the history
  • Loading branch information
owljoa committed Oct 13, 2018
1 parent 503583e commit 12c6ccb
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 17 deletions.
34 changes: 21 additions & 13 deletions mocks/mock_key_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,11 @@ func MockStoreKey(key bifrost.Key, keyDirPath string) error {
keyFilePath := filepath.Join(keyDirPath, key.ID())

// byte formatted key
keyBytes := key.ToByte()
keyBytes, err := key.ToByte()
if err != nil {
return err
}

isPrivateKey := key.IsPrivate()

// make json marshal
Expand Down Expand Up @@ -224,19 +228,21 @@ func newMockPriKey(internalKey *ecdsa.PrivateKey) *MockPriKey {
func (mockPriKey *MockPriKey) ID() bifrost.KeyID {
// get keyBytes from key
mockPub := &mockPriKey.internalPriKey.PublicKey
pubKeyBytes := newMockPubKey(mockPub).ToByte()
keyBitString := elliptic.Marshal(mockPub.Curve, mockPub.X, mockPub.Y)

// get ski from keyBytes
hash := sha256.New()
hash.Write(pubKeyBytes)
ski := hash.Sum(nil)
hash.Write(keyBitString)
hashValue := hash.Sum(nil)

ski := make([]byte, 20)
ski = hashValue[:20]

return "IT" + base58.Encode(ski)
}

func (mockPriKey *MockPriKey) ToByte() []byte {
keyBytes, _ := x509.MarshalECPrivateKey(mockPriKey.internalPriKey)
return keyBytes
func (mockPriKey *MockPriKey) ToByte() ([]byte, error) {
return x509.MarshalECPrivateKey(mockPriKey.internalPriKey)
}

func (mockPriKey *MockPriKey) KeyGenOpt() string {
Expand All @@ -260,19 +266,21 @@ func newMockPubKey(internalKey *ecdsa.PublicKey) *MockPubKey {

func (mockPubKey *MockPubKey) ID() bifrost.KeyID {
// get keyBytes from key
keyBytes := mockPubKey.ToByte()
keyBitString := elliptic.Marshal(mockPubKey.internalPubKey.Curve, mockPubKey.internalPubKey.X, mockPubKey.internalPubKey.Y)

// get ski from keyBytes
hash := sha256.New()
hash.Write(keyBytes)
ski := hash.Sum(nil)
hash.Write(keyBitString)
hashValue := hash.Sum(nil)

ski := make([]byte, 20)
ski = hashValue[:20]

return "IT" + base58.Encode(ski)
}

func (mockPubKey *MockPubKey) ToByte() []byte {
keyBytes, _ := x509.MarshalPKIXPublicKey(mockPubKey.internalPubKey)
return keyBytes
func (mockPubKey *MockPubKey) ToByte() ([]byte, error) {
return x509.MarshalPKIXPublicKey(mockPubKey.internalPubKey)
}

func (mockPubKey *MockPubKey) KeyGenOpt() string {
Expand Down
10 changes: 7 additions & 3 deletions server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ import (
func TestServer_validateRequestPeerInfo_whenValidPeerInfo(t *testing.T) {
// given
keyOpt := mocks.NewMockKeyOpts()
keyBytes, err := keyOpt.PubKey.ToByte()
assert.NoError(t, err)

peerInfo := &bifrost.PeerInfo{
IP: "127.0.0.1",
PubKeyBytes: keyOpt.PubKey.ToByte(),
PubKeyBytes: keyBytes,
IsPrivate: keyOpt.PubKey.IsPrivate(),
}

Expand All @@ -43,17 +45,19 @@ func TestServer_BifrostStream(t *testing.T) {
s := mocks.NewMockServer()

keyOpt := mocks.NewMockKeyOpts()
keyBytes, err := keyOpt.PubKey.ToByte()
assert.NoError(t, err)

peerInfo := &bifrost.PeerInfo{
IP: "127.0.0.1",
PubKeyBytes: keyOpt.PubKey.ToByte(),
PubKeyBytes: keyBytes,
IsPrivate: keyOpt.PubKey.IsPrivate(),
}

mockStreamServer := mocks.NewMockStreamServer(*peerInfo)

// when
err := s.BifrostStream(mockStreamServer)
err = s.BifrostStream(mockStreamServer)

// then
assert.NoError(t, err)
Expand Down
6 changes: 5 additions & 1 deletion util.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ type KeyOpts struct {
}

func BuildResponsePeerInfo(pubKey Key, metaData map[string]string) (*pb.Envelope, error) {
b := pubKey.ToByte()
b, err := pubKey.ToByte()

if err != nil {
return nil, err
}

pi := &PeerInfo{
PubKeyBytes: b,
Expand Down

0 comments on commit 12c6ccb

Please sign in to comment.