Commit 9dd896aa authored by suyanlong's avatar suyanlong

1.Add generate tool for node

2.Add tool package for project
parent 1eedfdd2
...@@ -36,6 +36,7 @@ func main() { ...@@ -36,6 +36,7 @@ func main() {
initCMD, initCMD,
interchainCMD, interchainCMD,
p2pCMD, p2pCMD,
toolCMD,
//ruleCMD, //ruleCMD,
startCMD, startCMD,
versionCMD, versionCMD,
......
package main
import (
"crypto/x509"
"encoding/hex"
"encoding/pem"
"fmt"
crypto2 "github.com/libp2p/go-libp2p-core/crypto"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/link33/sidecar/tool"
"github.com/meshplus/bitxhub-kit/crypto"
"github.com/meshplus/bitxhub-kit/crypto/asym"
"github.com/urfave/cli"
)
var toolCMD = cli.Command{
Name: "tool",
Usage: "generate tool for node",
Subcommands: []cli.Command{
{
Name: "info",
Usage: "get sidecar keystore",
Action: func(ctx *cli.Context) error {
privKey, err := asym.GenerateKeyPair(crypto.Secp256k1)
tool.Asset(err)
dataPrivKey, err := privKey.Bytes()
tool.Asset(err)
fmt.Println("--------user PrivateKey----------begin")
fmt.Println(hex.EncodeToString(dataPrivKey))
fmt.Println("--------user PrivateKey----------end \n")
publicKey, err := privKey.PublicKey().Bytes()
tool.Asset(err)
fmt.Println("--------user PublicKey----------begin")
fmt.Println(hex.EncodeToString(publicKey))
fmt.Println("--------user PublicKey----------end \n")
address, err := privKey.PublicKey().Address()
tool.Asset(err)
fmt.Println("--------user Address----------begin")
fmt.Println(address.String())
fmt.Println("--------user Address----------end \n")
keyStore, err := asym.GenKeyStore(privKey, "link33")
data, err := keyStore.Pretty()
tool.Asset(err)
fmt.Println("--------user keyStore----------begin ")
fmt.Println(data)
fmt.Println("--------user keyStore----------end \n")
// 指定类型ECDSA_P256,随机产生p2p私钥
p2pPrivateKey, err := asym.GenerateKeyPair(crypto.ECDSA_P256)
tool.Asset(err)
// 编码16
priKeyEncode, err := p2pPrivateKey.Bytes()
tool.Asset(err)
// 产生x509 ecdsa.PrivateKey 类型私钥
stdKey, err := x509.ParseECPrivateKey(priKeyEncode)
tool.Asset(err)
pemEncodePrivrateData := pem.EncodeToMemory(&pem.Block{Type: "EC PRIVATE KEY", Bytes: priKeyEncode})
fmt.Println("--------p2p node peer PrivateKey ----------begin\n")
fmt.Println(string(pemEncodePrivrateData))
fmt.Println("--------p2p node peer PrivateKey----------end\n")
// 类型转换
k, err := asym.PrivateKeyFromStdKey(stdKey)
tool.Asset(err)
sdKey, err := asym.PrivKeyToStdKey(k)
tool.Asset(err)
_, pk, err := crypto2.KeyPairFromStdKey(&sdKey)
tool.Asset(err)
id, err := peer.IDFromPublicKey(pk)
tool.Asset(err)
fmt.Println("--------p2p node ID----------begin")
fmt.Println(id.Pretty())
fmt.Println("--------p2p node ID----------end")
return nil
},
},
},
}
...@@ -21,6 +21,7 @@ import ( ...@@ -21,6 +21,7 @@ import (
"github.com/link33/sidecar/internal/router" "github.com/link33/sidecar/internal/router"
"github.com/link33/sidecar/internal/txcrypto" "github.com/link33/sidecar/internal/txcrypto"
"github.com/link33/sidecar/pkg/plugins" "github.com/link33/sidecar/pkg/plugins"
"github.com/link33/sidecar/tool"
) )
// App represents the necessary data for starting the sidecar app // App represents the necessary data for starting the sidecar app
...@@ -38,14 +39,14 @@ type App struct { ...@@ -38,14 +39,14 @@ type App struct {
// NewSidecar instantiates sidecar instance. // NewSidecar instantiates sidecar instance.
func NewSidecar(repoRoot string, config *repo.Config) (internal.Launcher, error) { func NewSidecar(repoRoot string, config *repo.Config) (internal.Launcher, error) {
store, err := leveldb.New(filepath.Join(config.RepoRoot, "store")) store, err := leveldb.New(filepath.Join(config.RepoRoot, "store"))
Asset(err) tool.Asset(err)
logger := loggers.Logger(loggers.App) logger := loggers.Logger(loggers.App)
privateKey, err := repo.LoadPrivateKey(repoRoot) privateKey, err := repo.LoadPrivateKey(repoRoot)
Asset(err) tool.Asset(err)
addr, err := privateKey.PublicKey().Address() addr, err := privateKey.PublicKey().Address()
Asset(err) tool.Asset(err)
nodePrivKey, err := repo.LoadNodePrivateKey(repoRoot) nodePrivKey, err := repo.LoadNodePrivateKey(repoRoot)
Asset(err) tool.Asset(err)
var ( var (
//ck checker.Checker //ck checker.Checker
//cryptor txcrypto.Cryptor //cryptor txcrypto.Cryptor
...@@ -53,16 +54,16 @@ func NewSidecar(repoRoot string, config *repo.Config) (internal.Launcher, error) ...@@ -53,16 +54,16 @@ func NewSidecar(repoRoot string, config *repo.Config) (internal.Launcher, error)
) )
r := router.NewRouter(loggers.Logger(loggers.Router)) r := router.NewRouter(loggers.Logger(loggers.Router))
pm, err := peermgr.New(config, r, nodePrivKey, privateKey, 1, loggers.Logger(loggers.PeerMgr)) pm, err := peermgr.New(config, r, nodePrivKey, privateKey, 1, loggers.Logger(loggers.PeerMgr))
Asset(err) tool.Asset(err)
clients := plugins.CreateClients(config.Appchains, nil) clients := plugins.CreateClients(config.Appchains, nil)
persister := manger.NewPersister(addr.String(), store, loggers.Logger(loggers.Manger)) persister := manger.NewPersister(addr.String(), store, loggers.Logger(loggers.Manger))
appchainMgr := appchainmgr.New(persister) appchainMgr := appchainmgr.New(persister)
cryptor, err := txcrypto.NewDirectCryptor(appchainMgr, privateKey) cryptor, err := txcrypto.NewDirectCryptor(appchainMgr, privateKey)
Asset(err) tool.Asset(err)
clientPort := appchain.NewPorts(clients, cryptor, logger) clientPort := appchain.NewPorts(clients, cryptor, logger)
r.Adds(clientPort) r.Adds(clientPort)
mg, err := manger.NewManager(addr.String(), r, pm, appchainMgr, loggers.Logger(loggers.Manger)) mg, err := manger.NewManager(addr.String(), r, pm, appchainMgr, loggers.Logger(loggers.Manger))
Asset(err) tool.Asset(err)
apiServer := api.NewServer(config, pm, loggers.Logger(loggers.ApiServer)) apiServer := api.NewServer(config, pm, loggers.Logger(loggers.ApiServer))
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
return &App{ return &App{
......
...@@ -34,6 +34,7 @@ func loadPeers(peers []string, privateKey crypto2.PrivKey) (string, map[string]* ...@@ -34,6 +34,7 @@ func loadPeers(peers []string, privateKey crypto2.PrivKey) (string, map[string]*
} }
for _, p := range peers { for _, p := range peers {
//p: 127.0.0.1:4567/p2p/QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64
if strings.HasSuffix(p, id.String()) { if strings.HasSuffix(p, id.String()) {
idx := strings.LastIndex(p, "/p2p/") idx := strings.LastIndex(p, "/p2p/")
if idx == -1 { if idx == -1 {
......
package peermgr
import (
"fmt"
"testing"
peer2 "github.com/libp2p/go-libp2p-core/peer"
"github.com/meshplus/bitxhub-kit/crypto"
"github.com/meshplus/bitxhub-kit/crypto/asym"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestLoadPeers(t *testing.T) {
var (
port = 4567
)
var peers []string
var nodeKeys []crypto.PrivateKey
var ids []string
key, err := asym.GenerateKeyPair(crypto.ECDSA_P256)
require.Nil(t, err)
nodeKeys = append(nodeKeys, key)
libp2pKey, err := convertToLibp2pPrivKey(key)
require.Nil(t, err)
idTmp, err := peer2.IDFromPrivateKey(libp2pKey)
require.Nil(t, err)
ids = append(ids, idTmp.String())
peer := fmt.Sprintf("/ip4/127.0.0.1/tcp/%d/p2p/%s", port, idTmp)
peers = append(peers, peer, fmt.Sprintf("/ip4/104.236.76.40/tcp/4001/ipfs/QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzawe34"))
l, ps, err := loadPeers(peers, libp2pKey)
t.Log(l)
t.Log(ps)
assert.Nil(t, err)
}
...@@ -19,29 +19,30 @@ import ( ...@@ -19,29 +19,30 @@ import (
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
var router router2.Router var routerTmp router2.Router
func TestNew(t *testing.T) { func TestNew(t *testing.T) {
logger := log.NewWithModule("swarm") logger := log.NewWithModule("swarm")
// test wrong nodePrivKey // test wrong nodePrivKey
nodeKeys, privKeys, config, _ := genKeysAndConfig(t, 2, repo.DirectMode) nodeKeys, privKeys, config, _ := genKeysAndConfig(t, 2, repo.DirectMode)
_, err := New(config, router, nil, privKeys[0], 0, logger) _, err := New(config, routerTmp, nil, privKeys[0], 0, logger)
require.NotNil(t, err) require.NotNil(t, err)
// test new swarm in direct mode // test new swarm in direct mode
nodeKeys, privKeys, config, _ = genKeysAndConfig(t, 2, repo.DirectMode) nodeKeys, privKeys, config, _ = genKeysAndConfig(t, 2, repo.DirectMode)
_, err = New(config, router, nodeKeys[0], privKeys[0], 0, logger) _, err = New(config, routerTmp, nodeKeys[0], privKeys[0], 0, logger)
require.Nil(t, err) require.Nil(t, err)
_, err = New(config, router, nodeKeys[0], privKeys[0], 0, logger) _, err = New(config, routerTmp, nodeKeys[0], privKeys[0], 0, logger)
require.Nil(t, err) require.Nil(t, err)
// test new swarm in unsupport mode // test new swarm in unsupport mode
nodeKeys, privKeys, config, _ = genKeysAndConfig(t, 2, "") nodeKeys, privKeys, config, _ = genKeysAndConfig(t, 2, "")
_, err = New(config, router, nodeKeys[0], privKeys[0], 0, logger) _, err = New(config, routerTmp, nodeKeys[0], privKeys[0], 0, logger)
require.NotNil(t, err) require.NotNil(t, err)
} }
...@@ -49,12 +50,12 @@ func TestSwarm_Start(t *testing.T) { ...@@ -49,12 +50,12 @@ func TestSwarm_Start(t *testing.T) {
logger := log.NewWithModule("swarm") logger := log.NewWithModule("swarm")
nodeKeys, privKeys, config, _ := genKeysAndConfig(t, 2, repo.DirectMode) nodeKeys, privKeys, config, _ := genKeysAndConfig(t, 2, repo.DirectMode)
swarm1, err := New(config, router, nodeKeys[0], privKeys[0], 0, logger) swarm1, err := New(config, routerTmp, nodeKeys[0], privKeys[0], 0, logger)
require.Nil(t, err) require.Nil(t, err)
go swarm1.Start() go swarm1.Start()
swarm2, err := New(config, router, nodeKeys[1], privKeys[1], 0, logger) swarm2, err := New(config, routerTmp, nodeKeys[1], privKeys[1], 0, logger)
require.Nil(t, err) require.Nil(t, err)
go swarm2.Start() go swarm2.Start()
...@@ -213,7 +214,7 @@ func TestSwarm_Provider(t *testing.T) { ...@@ -213,7 +214,7 @@ func TestSwarm_Provider(t *testing.T) {
func prepare(t *testing.T) (*Swarm, []string, *Swarm, *pb.Message, string, string) { func prepare(t *testing.T) (*Swarm, []string, *Swarm, *pb.Message, string, string) {
nodeKeys, privKeys, config, ids := genKeysAndConfig(t, 2, repo.DirectMode) nodeKeys, privKeys, config, ids := genKeysAndConfig(t, 2, repo.DirectMode)
swarm, err := New(config, router, nodeKeys[0], privKeys[0], 0, log.NewWithModule("swarm")) swarm, err := New(config, routerTmp, nodeKeys[0], privKeys[0], 0, log.NewWithModule("swarm"))
require.Nil(t, err) require.Nil(t, err)
mockMsg := &pb.Message{Type: pb.Message_APPCHAIN_REGISTER} mockMsg := &pb.Message{Type: pb.Message_APPCHAIN_REGISTER}
......
...@@ -57,6 +57,10 @@ func New(config *repo.Config, router router.Router, nodePrivKey crypto.PrivateKe ...@@ -57,6 +57,10 @@ func New(config *repo.Config, router router.Router, nodePrivKey crypto.PrivateKe
} }
ll, remotes, err = loadPeers(config.Peer.Peers, libp2pPrivKey) ll, remotes, err = loadPeers(config.Peer.Peers, libp2pPrivKey)
if err != nil {
return nil, fmt.Errorf("load peers: %w", err)
}
var protocolIDs = []string{protocolID} var protocolIDs = []string{protocolID}
p2p, err := network.New( p2p, err := network.New(
network.WithLocalAddr(ll), network.WithLocalAddr(ll),
......
package tool
func Asset(err error) {
if err != nil {
panic(err)
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment