Skip to content

Commit

Permalink
Nodepass (#15)
Browse files Browse the repository at this point in the history
* add native methods

* add gas user check

* stop whitelist file auto load

* add nodepass flag to unlock keystore file

* add keystore to genesisTool

* add flags for genesis tool
  • Loading branch information
chenquanyu authored Sep 5, 2022
1 parent 5abb6bf commit f61bb0b
Show file tree
Hide file tree
Showing 8 changed files with 159 additions and 74 deletions.
22 changes: 22 additions & 0 deletions accounts/keystore/keystore.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/event"
"github.com/google/uuid"
)

var (
Expand Down Expand Up @@ -505,3 +506,24 @@ func zeroKey(k *ecdsa.PrivateKey) {
b[i] = 0
}
}

func GenerateKeyJson(key *ecdsa.PrivateKey, password string) (string, error) {
// Create the keyfile object with a random UUID.
UUID, err := uuid.NewRandom()
if err != nil {
return "", err
}
keyStore := &Key{
Id: UUID,
Address: crypto.PubkeyToAddress(key.PublicKey),
PrivateKey: key,
}

// Encrypt key with passphrase.
scryptN, scryptP := StandardScryptN, StandardScryptP
keyjson, err := EncryptKey(keyStore, password, scryptN, scryptP)
if err != nil {
return "", err
}
return string(keyjson), nil
}
66 changes: 42 additions & 24 deletions cmd/geth/genesisTool.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,32 @@
* along with The Zion. If not, see <http://www.gnu.org/licenses/>.
*/


package main

import (
"encoding/json"

"github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/ethereum/go-ethereum/cmd/utils"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/consensus/hotstuff/tool"
"github.com/ethereum/go-ethereum/crypto"
"gopkg.in/urfave/cli.v1"
"strconv"
)

var (
genesisToolCommand = cli.Command{
Name: "genesisTool",
Usage: "A set of commands facilitating generating genesis configuration of maas chain",
Category: "MISCELLANEOUS COMMANDS",
Name: "genesisTool",
Usage: "A set of commands facilitating generating genesis configuration of maas chain",
Category: "MISCELLANEOUS COMMANDS",
Description: "",
Subcommands: []cli.Command{
{
Name: "generate",
Flags: []cli.Flag{
basePathFlag,
nodeCountFlag,
nodePassFlag,
},
Action: utils.MigrateFlags(generateMaasGensis),
},
Expand All @@ -52,26 +54,38 @@ var (
Name: "basePath",
Usage: "The path to store genesis configuration files",
}
nodeCountFlag = cli.IntFlag{
Name: "nodeCount",
Usage: "The node count",
}
nodePassFlag = cli.StringFlag{
Name: "nodePass",
Usage: "The node password to generate keystore json",
}
)

type KeystoreFile struct {
Address string `json:"address"`
Crypto keystore.CryptoJSON `json:"crypto"`
Id string `json:"id"`
Version int `json:"version"`
}

func generateMaasGensis(ctx *cli.Context) error {
if len(ctx.Args()) < 1 {
utils.Fatalf("This command requires an argument.")
}
basePath := ctx.String(basePathFlag.Name)
if basePath == ""{
if basePath == "" {
basePath = utils.DefaultBasePath()
} else if basePath[len(basePath)-1:len(basePath)] != "/" {
} else if basePath[len(basePath)-1:] != "/" {
basePath += "/"
}
nodeNum, err := strconv.Atoi(ctx.Args().First())
if err != nil {
utils.Fatalf("parse node number error %s", err.Error())
}

nodeNum := ctx.Int(nodeCountFlag.Name)
if nodeNum < 4 {
utils.Fatalf("got %v nodes, but hotstuff BFT requires at least 4 nodes", nodeNum)
}
nodePass := ctx.String(nodePassFlag.Name)
if nodePass == "" {
utils.Fatalf("node password id required")
}

genesis := new(utils.MaasGenesis)
genesis.Default()
Expand All @@ -86,13 +100,15 @@ func generateMaasGensis(ctx *cli.Context) error {
addr := crypto.PubkeyToAddress(key.PublicKey)

nodekey := hexutil.Encode(crypto.FromECDSA(key))
keyjson, _ := keystore.GenerateKeyJson(key, nodePass)
nodeInf, _ := tool.NodeKey2NodeInfo(nodekey)
staticInf := tool.NodeStaticInfoTemp(nodeInf)

node := &tool.Node{
Address: addr.Hex(),
NodeKey: nodekey,
Static: staticInf,
Address: addr.Hex(),
NodeKey: nodekey,
Static: staticInf,
KeyStore: keyjson,
}
nodes = append(nodes, node)
}
Expand Down Expand Up @@ -126,12 +142,14 @@ func generateMaasGensis(ctx *cli.Context) error {
pubInf,
"100000000000000000000000000000",
}

var keystoreObj KeystoreFile
json.Unmarshal([]byte(v.KeyStore), &keystoreObj)
metaNode := &utils.Node{
Address: v.Address,
NodeKey: v.NodeKey,
PubKey: pubInf,
Static: v.Static,
Address: v.Address,
NodeKey: v.NodeKey,
PubKey: pubInf,
Static: v.Static,
KeyStore: keystoreObj,
}

metaNodes = append(metaNodes, metaNode)
Expand All @@ -152,7 +170,7 @@ func generateMaasGensis(ctx *cli.Context) error {
contents[1] = string(staticNodesEnc)
filePaths[1] = basePath + "static-nodes.json"

sortedNodesEnc, _ := json.MarshalIndent(metaNodes,"", "\t")
sortedNodesEnc, _ := json.MarshalIndent(metaNodes, "", "\t")
contents[2] = string(sortedNodesEnc)
filePaths[2] = basePath + "nodes.json"
err = utils.DumpGenesis(filePaths, contents)
Expand Down
2 changes: 1 addition & 1 deletion cmd/geth/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ var (
utils.MinerNotifyFullFlag,
configFileFlag,
utils.CatalystFlag,
utils.NodeWhitePath,
utils.NodeWhitePathFlag,
}

rpcFlags = []cli.Flag{
Expand Down
10 changes: 5 additions & 5 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ var (
}
WhitelistFlag = cli.StringFlag{
Name: "whitelist",
Usage: "Comma separated block number-to-hash mappings to enforce (<number>=<hash>)",
Usage: "Only address in whitelist can connect",
}
BloomFilterSizeFlag = cli.Uint64Flag{
Name: "bloomfilter.size",
Expand Down Expand Up @@ -501,7 +501,7 @@ var (
Name: "allow-insecure-unlock",
Usage: "Allow insecure account unlocking when account-related RPCs are exposed by http",
}
NodeWhitePath = cli.StringFlag{
NodeWhitePathFlag = cli.StringFlag{
Name: "node.whitelist",
Usage: "node whitelist config file path",
}
Expand Down Expand Up @@ -1251,10 +1251,10 @@ func SetNodeConfig(ctx *cli.Context, cfg *node.Config) {
cfg.InsecureUnlockAllowed = ctx.GlobalBool(InsecureUnlockAllowedFlag.Name)
}
// set node whitelist file path
if !ctx.GlobalIsSet(NodeWhitePath.Name) {
ctx.GlobalSet(NodeWhitePath.Name, "node-whitelist.json")
if !ctx.GlobalIsSet(NodeWhitePathFlag.Name) {
ctx.GlobalSet(NodeWhitePathFlag.Name, "node-whitelist.json")
}
cfg.NodeWhitePath = ctx.GlobalString(NodeWhitePath.Name)
cfg.NodeWhitePath = ctx.GlobalString(NodeWhitePathFlag.Name)
// params.StartNodeWhiteLoadTask(cfg.NodeWhitePath)
}

Expand Down
78 changes: 39 additions & 39 deletions cmd/utils/maas_genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,31 +28,31 @@ import (

type MaasGenesis struct {
Config struct {
ChainId uint64 `json:"chainId"`
HomesteadBlock uint64 `json:"homesteadBlock"`
Eip150Block uint64 `json:"eip150Block"`
Eip155Block uint64 `json:"eip155Block"`
Eip158Block uint64 `json:"eip158Block"`
ByzantiumBlock uint64 `json:"byzantiumBlock"`
ChainId uint64 `json:"chainId"`
HomesteadBlock uint64 `json:"homesteadBlock"`
Eip150Block uint64 `json:"eip150Block"`
Eip155Block uint64 `json:"eip155Block"`
Eip158Block uint64 `json:"eip158Block"`
ByzantiumBlock uint64 `json:"byzantiumBlock"`
ConstantinopleBlock uint64 `json:"constantinopleBlock"`
PetersburgBlock uint64 `json:"petersburgBlock"`
IstanbulBlock uint64 `json:"istanbulBlock"`
HotStuff struct {
PetersburgBlock uint64 `json:"petersburgBlock"`
IstanbulBlock uint64 `json:"istanbulBlock"`
HotStuff struct {
Protocol string `json:"protocol"`
} `json:"hotstuff"`
} `json:"config"`
Alloc map[string]struct {
PublicKey string `json:"publicKey"`
Balance string `json:"balance"`
Balance string `json:"balance"`
} `json:"alloc"`
Coinbase string `json:"coinbase"`
Coinbase string `json:"coinbase"`
Difficulty string `json:"difficulty"`
ExtraData string `json:"extraData"`
GasLimit string `json:"gasLimit"`
Nonce string `json:"nonce"`
Mixhash string `json:"mixhash"`
ExtraData string `json:"extraData"`
GasLimit string `json:"gasLimit"`
Nonce string `json:"nonce"`
Mixhash string `json:"mixhash"`
ParentHash string `json:"parentHash"`
Timestamp string `json:"timestamp"`
Timestamp string `json:"timestamp"`
}

func (m *MaasGenesis) Encode() (string, error) {
Expand All @@ -66,26 +66,27 @@ func (m *MaasGenesis) Decode(data string) error {
return err
}


// Default genesis is not a valid block
// it's used only for initialization purpose
func (m *MaasGenesis) Default() {
m.Config = struct{
ChainId uint64 `json:"chainId"`
HomesteadBlock uint64 `json:"homesteadBlock"`
Eip150Block uint64 `json:"eip150Block"`
Eip155Block uint64 `json:"eip155Block"`
Eip158Block uint64 `json:"eip158Block"`
ByzantiumBlock uint64 `json:"byzantiumBlock"`
m.Config = struct {
ChainId uint64 `json:"chainId"`
HomesteadBlock uint64 `json:"homesteadBlock"`
Eip150Block uint64 `json:"eip150Block"`
Eip155Block uint64 `json:"eip155Block"`
Eip158Block uint64 `json:"eip158Block"`
ByzantiumBlock uint64 `json:"byzantiumBlock"`
ConstantinopleBlock uint64 `json:"constantinopleBlock"`
PetersburgBlock uint64 `json:"petersburgBlock"`
IstanbulBlock uint64 `json:"istanbulBlock"`
HotStuff struct {
PetersburgBlock uint64 `json:"petersburgBlock"`
IstanbulBlock uint64 `json:"istanbulBlock"`
HotStuff struct {
Protocol string `json:"protocol"`
} `json:"hotstuff"`
}{
10898,0,0,0,0,0,0,0,0,
struct {Protocol string `json:"protocol"`} {
10898, 0, 0, 0, 0, 0, 0, 0, 0,
struct {
Protocol string `json:"protocol"`
}{
"basic",
},
}
Expand All @@ -106,12 +107,11 @@ func DumpGenesis(filePaths [3]string, contents [3]string) error {
if err != nil {
return err
}
f,err := os.Create(file_)
defer f.Close()

if err !=nil {
f, err := os.Create(file_)
if err != nil {
return err
}
defer f.Close()
_, err = f.Write([]byte(contents[k]))
if err != nil {
return err
Expand All @@ -130,18 +130,18 @@ func ensureBaseDir(fpath string) error {
return os.MkdirAll(baseDir, 0755)
}


func DefaultBasePath() (basePath string) {
exePath, _ := os.Executable()
dir := filepath.Dir(exePath) + "/"
return dir
}

type Node struct{
Address string `json:"address"`
NodeKey string `json:"nodeKey"`
PubKey string `json:"pubKey"`
Static string `json:"static"`
type Node struct {
Address string `json:"address"`
NodeKey string `json:"nodeKey"`
KeyStore interface{} `json:"keystore"`
PubKey string `json:"pubKey"`
Static string `json:"static"`
}

func (m *Node) Encode() (string, error) {
Expand Down
11 changes: 7 additions & 4 deletions consensus/hotstuff/tool/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@ import (
"crypto/ecdsa"
"crypto/elliptic"
"fmt"
"strings"

"github.com/ethereum/go-ethereum/consensus/hotstuff"
"github.com/ethereum/go-ethereum/consensus/hotstuff/validator"
"strings"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"

//"github.com/ethereum/go-ethereum/consensus/hotstuff/backend"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
Expand All @@ -55,9 +57,10 @@ func Encode(validators []common.Address) (string, error) {
}

type Node struct {
Address string
NodeKey string
Static string
Address string
NodeKey string
Static string
KeyStore string
}

func SortNodes(src []*Node) []*Node {
Expand Down
Loading

0 comments on commit f61bb0b

Please sign in to comment.