Skip to content

Commit 88167f3

Browse files
zeligfjl
authored andcommitted
add privkey to clientIdentity + tests
1 parent c8a8aa0 commit 88167f3

2 files changed

Lines changed: 21 additions & 5 deletions

File tree

‎p2p/client_identity.go‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ import (
77

88
// ClientIdentity represents the identity of a peer.
99
type ClientIdentity interface {
10-
String() string // human readable identity
11-
Pubkey() []byte // 512-bit public key
10+
String() string // human readable identity
11+
Pubkey() []byte // 512-bit public key
12+
PrivKey() []byte // 512-bit private key
1213
}
1314

1415
type SimpleClientIdentity struct {
@@ -17,17 +18,19 @@ type SimpleClientIdentity struct {
1718
customIdentifier string
1819
os string
1920
implementation string
21+
privkey []byte
2022
pubkey []byte
2123
}
2224

23-
func NewSimpleClientIdentity(clientIdentifier string, version string, customIdentifier string, pubkey []byte) *SimpleClientIdentity {
25+
func NewSimpleClientIdentity(clientIdentifier string, version string, customIdentifier string, privkey []byte, pubkey []byte) *SimpleClientIdentity {
2426
clientIdentity := &SimpleClientIdentity{
2527
clientIdentifier: clientIdentifier,
2628
version: version,
2729
customIdentifier: customIdentifier,
2830
os: runtime.GOOS,
2931
implementation: runtime.Version(),
3032
pubkey: pubkey,
33+
privkey: privkey,
3134
}
3235

3336
return clientIdentity
@@ -50,8 +53,12 @@ func (c *SimpleClientIdentity) String() string {
5053
c.implementation)
5154
}
5255

56+
func (c *SimpleClientIdentity) Privkey() []byte {
57+
return c.privkey
58+
}
59+
5360
func (c *SimpleClientIdentity) Pubkey() []byte {
54-
return []byte(c.pubkey)
61+
return c.pubkey
5562
}
5663

5764
func (c *SimpleClientIdentity) SetCustomIdentifier(customIdentifier string) {

‎p2p/client_identity_test.go‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
11
package p2p
22

33
import (
4+
"bytes"
45
"fmt"
56
"runtime"
67
"testing"
78
)
89

910
func TestClientIdentity(t *testing.T) {
10-
clientIdentity := NewSimpleClientIdentity("Ethereum(G)", "0.5.16", "test", []byte("pubkey"))
11+
clientIdentity := NewSimpleClientIdentity("Ethereum(G)", "0.5.16", "test", []byte("privkey"), []byte("pubkey"))
12+
key := clientIdentity.Privkey()
13+
if !bytes.Equal(key, []byte("privkey")) {
14+
t.Errorf("Expected Privkey to be %x, got %x", key, []byte("privkey"))
15+
}
16+
key = clientIdentity.Pubkey()
17+
if !bytes.Equal(key, []byte("pubkey")) {
18+
t.Errorf("Expected Pubkey to be %x, got %x", key, []byte("pubkey"))
19+
}
1120
clientString := clientIdentity.String()
1221
expected := fmt.Sprintf("Ethereum(G)/v0.5.16/test/%s/%s", runtime.GOOS, runtime.Version())
1322
if clientString != expected {

0 commit comments

Comments
 (0)