ソースを参照

add privkey to clientIdentity + tests

zelig 10 年 前
コミット
88167f39a6
2 ファイル変更21 行追加5 行削除
  1. 11 4
      p2p/client_identity.go
  2. 10 1
      p2p/client_identity_test.go

+ 11 - 4
p2p/client_identity.go

@@ -7,8 +7,9 @@ import (
 
 // ClientIdentity represents the identity of a peer.
 type ClientIdentity interface {
-	String() string // human readable identity
-	Pubkey() []byte // 512-bit public key
+	String() string  // human readable identity
+	Pubkey() []byte  // 512-bit public key
+	PrivKey() []byte // 512-bit private key
 }
 
 type SimpleClientIdentity struct {
@@ -17,10 +18,11 @@ type SimpleClientIdentity struct {
 	customIdentifier string
 	os               string
 	implementation   string
+	privkey          []byte
 	pubkey           []byte
 }
 
-func NewSimpleClientIdentity(clientIdentifier string, version string, customIdentifier string, pubkey []byte) *SimpleClientIdentity {
+func NewSimpleClientIdentity(clientIdentifier string, version string, customIdentifier string, privkey []byte, pubkey []byte) *SimpleClientIdentity {
 	clientIdentity := &SimpleClientIdentity{
 		clientIdentifier: clientIdentifier,
 		version:          version,
@@ -28,6 +30,7 @@ func NewSimpleClientIdentity(clientIdentifier string, version string, customIden
 		os:               runtime.GOOS,
 		implementation:   runtime.Version(),
 		pubkey:           pubkey,
+		privkey:          privkey,
 	}
 
 	return clientIdentity
@@ -50,8 +53,12 @@ func (c *SimpleClientIdentity) String() string {
 		c.implementation)
 }
 
+func (c *SimpleClientIdentity) Privkey() []byte {
+	return c.privkey
+}
+
 func (c *SimpleClientIdentity) Pubkey() []byte {
-	return []byte(c.pubkey)
+	return c.pubkey
 }
 
 func (c *SimpleClientIdentity) SetCustomIdentifier(customIdentifier string) {

+ 10 - 1
p2p/client_identity_test.go

@@ -1,13 +1,22 @@
 package p2p
 
 import (
+	"bytes"
 	"fmt"
 	"runtime"
 	"testing"
 )
 
 func TestClientIdentity(t *testing.T) {
-	clientIdentity := NewSimpleClientIdentity("Ethereum(G)", "0.5.16", "test", []byte("pubkey"))
+	clientIdentity := NewSimpleClientIdentity("Ethereum(G)", "0.5.16", "test", []byte("privkey"), []byte("pubkey"))
+	key := clientIdentity.Privkey()
+	if !bytes.Equal(key, []byte("privkey")) {
+		t.Errorf("Expected Privkey to be %x, got %x", key, []byte("privkey"))
+	}
+	key = clientIdentity.Pubkey()
+	if !bytes.Equal(key, []byte("pubkey")) {
+		t.Errorf("Expected Pubkey to be %x, got %x", key, []byte("pubkey"))
+	}
 	clientString := clientIdentity.String()
 	expected := fmt.Sprintf("Ethereum(G)/v0.5.16/test/%s/%s", runtime.GOOS, runtime.Version())
 	if clientString != expected {