add functions to generate keypairs for libp2p

add two functions and the tests

Signed-off-by: Leo Chen <leo@harmony.one>
pull/280/head
Leo Chen 6 years ago
parent f8b14043eb
commit a93672449b
  1. 14
      internal/utils/utils.go
  2. 30
      internal/utils/utils_test.go

@ -6,11 +6,14 @@ import (
"encoding/json" "encoding/json"
"io" "io"
"log" "log"
mrand "math/rand"
"os" "os"
"regexp" "regexp"
"strconv" "strconv"
"sync" "sync"
p2p_crypto "github.com/libp2p/go-libp2p-crypto"
"github.com/dedis/kyber" "github.com/dedis/kyber"
"github.com/harmony-one/harmony/crypto" "github.com/harmony-one/harmony/crypto"
"github.com/harmony-one/harmony/crypto/pki" "github.com/harmony-one/harmony/crypto/pki"
@ -70,6 +73,17 @@ func GenKey(ip, port string) (kyber.Scalar, kyber.Point) {
return priKey, pubKey return priKey, pubKey
} }
// GenKeyP2P generates a pair of RSA keys used in libp2p host
func GenKeyP2P(ip, port string) (p2p_crypto.PrivKey, p2p_crypto.PubKey, error) {
r := mrand.New(mrand.NewSource(int64(GetUniqueIDFromIPPort(ip, port))))
return p2p_crypto.GenerateKeyPairWithReader(p2p_crypto.RSA, 2048, r)
}
// GenKeyP2PRand generates a pair of RSA keys used in libp2p host, using random seed
func GenKeyP2PRand() (p2p_crypto.PrivKey, p2p_crypto.PubKey, error) {
return p2p_crypto.GenerateKeyPair(p2p_crypto.RSA, 2048)
}
// AllocateShard uses the number of current nodes and number of shards // AllocateShard uses the number of current nodes and number of shards
// to return the shardNum a new node belongs to, it also tells whether the node is a leader // to return the shardNum a new node belongs to, it also tells whether the node is a leader
func AllocateShard(numOfAddedNodes, numOfShards int) (int, bool) { func AllocateShard(numOfAddedNodes, numOfShards int) (int, bool) {

@ -4,6 +4,7 @@ import (
"testing" "testing"
"github.com/harmony-one/harmony/p2p" "github.com/harmony-one/harmony/p2p"
crypto "github.com/libp2p/go-libp2p-crypto"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
@ -47,6 +48,35 @@ func TestGenKey(t *testing.T) {
GenKey("3.3.3.3", "3456") GenKey("3.3.3.3", "3456")
} }
// Test for GenKeyP2P
func TestGenKeyP2P(t *testing.T) {
pi, pb, err := GenKeyP2P("127.0.0.1", "8888")
if err != nil {
t.Errorf("GenKeyP2p Error: %v", err)
}
kpi, _ := crypto.MarshalPrivateKey(pi)
kpb, _ := crypto.MarshalPublicKey(pb)
if len(kpi) != 1198 {
t.Errorf("Length of Private Key Error: %v, expected 1198", len(kpi))
}
if len(kpb) != 299 {
t.Errorf("Length of Public Key Error: %v, expected 299", len(kpb))
}
}
// Test for GenKeyP2PRand, noted the length of private key can be random
// thus we don't test it here.
func TestGenKeyP2PRand(t *testing.T) {
_, pb, err := GenKeyP2PRand()
if err != nil {
t.Errorf("GenKeyP2PRand Error: %v", err)
}
kpb, _ := crypto.MarshalPublicKey(pb)
if len(kpb) != 299 {
t.Errorf("Length of Public Key Error: %v, expected 299", len(kpb))
}
}
// Test for GetUniqueIDFromPeer // Test for GetUniqueIDFromPeer
func TestGetUniqueIDFromPeer(t *testing.T) { func TestGetUniqueIDFromPeer(t *testing.T) {
peer := p2p.Peer{IP: "1.1.1.1", Port: "123"} peer := p2p.Peer{IP: "1.1.1.1", Port: "123"}

Loading…
Cancel
Save