Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions commands/box/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"fmt"
"io/ioutil"
"net"
"os"
"path"
"strconv"

Expand All @@ -22,6 +23,7 @@ import (
"github.com/BOXFoundation/boxd/util"
"github.com/BOXFoundation/boxd/wallet"
"github.com/BOXFoundation/boxd/wallet/account"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

Expand All @@ -36,6 +38,8 @@ const (
//
var (
DefaultWalletDir = path.Join(util.HomeDir(), ".box_keystore")
OutFilePath string
InFilePath string
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove the two global variable

)

// GetRPCAddr gets rpc addr
Expand Down Expand Up @@ -136,3 +140,18 @@ func IsHexFormat(str string) bool {
}
return true
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IsHexFormat and GetCurrentFilePath is redundant

// GetCurrentFilePath returns a rooted path name corresponding to the current directory.
func GetCurrentFilePath() string {
filePath, err := os.Getwd()
if err != nil {
return ""
}
return filePath
}

//SetFlag set a flage to flag the file
func SetFlag(cmd *cobra.Command) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AddInputFlag
AddOutPutFlag

cmd.PersistentFlags().StringVar(&OutFilePath, "o", GetCurrentFilePath(), "output file path, default current directory")
cmd.PersistentFlags().StringVar(&InFilePath, "i", "", "input file path, default nil")
}
87 changes: 41 additions & 46 deletions commands/box/contract/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,14 @@ var rootCmd = &cobra.Command{
`,
}

var (
compileCmd = &cobra.Command{
Use: "compile",
Short: "Compile contract source file",
Run: compile,
}
)

func init() {
root.RootCmd.AddCommand(rootCmd)
rootCmd.AddCommand(
Expand Down Expand Up @@ -154,12 +162,9 @@ func init() {
Short: "view contract details",
Run: detailAbi,
},
&cobra.Command{
Use: "compile [sol_file_path] [optional|ouput_file_path]",
Short: "Compile contract source file",
Run: compile,
},
compileCmd,
)
common.SetFlag(compileCmd)
}

func importAbi(cmd *cobra.Command, args []string) {
Expand Down Expand Up @@ -238,9 +243,7 @@ func importAbi(cmd *cobra.Command, args []string) {
fmt.Printf("Would you want to change current index into %d. [Y/n]\n", max+1)
var input string
fmt.Scanf("%s", &input)
switch {
case input == "n" || input == "N":
default:
if input != "n" && input != "N" {
setabi(&cobra.Command{}, []string{strconv.Itoa(max + 1)})
}
}
Expand Down Expand Up @@ -1113,19 +1116,22 @@ func parseContractData(data string) (bytecode string, abi string, err error) {
return "", "", err
}
if len(binData) == 1 {
bytecode = binData[0]
abi = abiData[0]
bytecode = string(binData[0])
abi = string(abiData[0])
} else if len(binData) > 1 {
fmt.Printf("%v\n", solName)
for i, v := range solName {
fmt.Printf("%d:%s\n", i, v)
}
fmt.Println("Which one do you want to deploy?")
var input string
fmt.Scanf("%s", &input)
for i, v := range solName {
if input == v {
bytecode = binData[i]
abi = abiData[i]
if input == v || input == strconv.Itoa(i) {
bytecode = string(binData[i])
abi = string(abiData[i])
}
}
//if len(bytecode) == 0, there is no match in compiling sol file
if len(bytecode) == 0 {
return "", "", errors.New("Please make sure your input is correct")
}
Expand All @@ -1140,7 +1146,7 @@ func parseContractData(data string) (bytecode string, abi string, err error) {
return bytecode, abi, nil
}

func compileSol(filepath string) (binData []string, abiData []string, solName []string, err error) {
func compileSol(filepath string) (binData [][]byte, abiData [][]byte, solName []string, err error) {
cmd := exec.Command("solc", filepath, "--bin", "--abi")
//CombinedOutput runs the command and returns its combined standard output and standard error.
output, err := cmd.CombinedOutput()
Expand All @@ -1150,30 +1156,18 @@ func compileSol(filepath string) (binData []string, abiData []string, solName []
}
binMatches := solcBinReg.FindAllSubmatch(output, -1)
for _, v := range binMatches {
for i, k := range v {
if (i+1)%2 == 0 {
binData = append(binData, string(k))
}
}
binData = append(binData, v[1])
}
abiMathes := solcAbiReg.FindAllSubmatch(output, -1)
for _, v := range abiMathes {
for i, k := range v {
if (i+1)%2 == 0 {
abiData = append(abiData, string(k))
}
}
abiData = append(abiData, v[1])
}
solFilePath = filepath
nameMatches := solNameReg.FindAllSubmatch(output, -1)
for _, v := range nameMatches {
for i, k := range v {
if (i+1)%2 == 0 {
solName = append(solName, string(k))
}
}
solName = append(solName, string(v[1]))
}
if len(abiData) == 0 && len(binData) == 0 && len(solName) == 0 {
if len(abiData) == 0 || len(binData) == 0 || len(solName) == 0 {
return nil, nil, nil, errors.New("no matche in sol file")
}
return binData, abiData, solName, nil
Expand Down Expand Up @@ -1291,25 +1285,26 @@ func detailAbi(cmd *cobra.Command, args []string) {
}

func compile(cmd *cobra.Command, args []string) {
if len(args) == 0 || len(args) > 2 {
if len(args) != 0 {
fmt.Println(cmd.Use)
return
}
filepath := args[0]
if err := util.FileExists(filepath); err != nil {
if len(common.InFilePath) == 0 {
fmt.Println("please input the path of peer.key")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

contract source file

return
}
if err := util.FileExists(common.InFilePath); err != nil {
fmt.Println(err)
return
}
var outputDir string
if len(args) == 1 {
currentFilePath, err := os.Getwd()
if err != nil {
fmt.Println(err)
return
}
outputDir = currentFilePath + "/"
} else if len(args) == 2 {
outputDir = args[1] + "/"
var (
outputDir string
filepath = common.InFilePath
)
if common.OutFilePath == common.GetCurrentFilePath() {
outputDir = common.GetCurrentFilePath() + "/"
} else {
outputDir = common.OutFilePath + "/"
if _, err := os.Stat(outputDir); err != nil {
fmt.Printf("output dir %s error: %s\n", outputDir, err)
return
Expand All @@ -1323,14 +1318,14 @@ func compile(cmd *cobra.Command, args []string) {
for i := 0; i < len(binData); i++ {
binFile := outputDir + solName[i] + ".bin"
// write bin data to file
if err := ioutil.WriteFile(binFile, []byte(binData[i]), 0644); err != nil {
if err := ioutil.WriteFile(binFile, binData[i], 0644); err != nil {
fmt.Println(err)
return
}
fmt.Println("generate bin file:", binFile)
abiFile := outputDir + solName[i] + ".abi"
// write abi data to file
if err := ioutil.WriteFile(abiFile, []byte(abiData[i]), 0644); err != nil {
if err := ioutil.WriteFile(abiFile, abiData[i], 0644); err != nil {
fmt.Println(err)
return
}
Expand Down
113 changes: 77 additions & 36 deletions commands/box/util/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ import (
"io/ioutil"
"math"
"os"
"path/filepath"
"strconv"

"github.com/BOXFoundation/boxd/commands/box/common"
root "github.com/BOXFoundation/boxd/commands/box/root"
"github.com/BOXFoundation/boxd/core/txlogic"
"github.com/BOXFoundation/boxd/core/types"
Expand All @@ -35,6 +37,19 @@ var rootCmd = &cobra.Command{
// Run: func(cmd *cobra.Command, args []string) { },
}

var (
peerIDCmd = &cobra.Command{
Use: "peerid [optional|peerkey]",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

showpeerid

Short: "conversion peer key to peer ID",
Run: peerID,
}
createPeerIDCmd = &cobra.Command{
Use: "createpeerid ",
Short: "create peer key",
Run: createPeerID,
}
)

// Init adds the sub command to the root command.
func init() {
root.RootCmd.AddCommand(rootCmd)
Expand Down Expand Up @@ -69,17 +84,11 @@ func init() {
Short: "creat p2pKH address",
Run: makeP2PKHAddress,
},
&cobra.Command{
Use: "peerid [optional|peerkey_path, peerkey]",
Short: "conversion peer key to peer ID",
Run: peerID,
},
&cobra.Command{
Use: "createpeerid [optional|peerkey_path]",
Short: "create peer key",
Run: createPeerID,
},
peerIDCmd,
createPeerIDCmd,
)
common.SetFlag(peerIDCmd)
common.SetFlag(createPeerIDCmd)
}

func encode(cmd *cobra.Command, args []string) {
Expand Down Expand Up @@ -251,26 +260,31 @@ func makeP2PKHAddress(cmd *cobra.Command, args []string) {
}

func peerID(cmd *cobra.Command, args []string) {
if len(args) != 1 {
fmt.Println(cmd.Use)
return
}
var (
data string
err error
dataByte []byte
)
if err := util.FileExists(args[0]); err == nil {
dataByte, err = ioutil.ReadFile(args[0])
var data string
switch len(args) {
case 0:
if len(common.InFilePath) == 0 {
fmt.Println("please input the path of peer.key")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

println(cmd.Use)

return
}
if err := util.FileExists(common.InFilePath); err != nil {
fmt.Println(err)
return
}
dataByte, err := ioutil.ReadFile(common.InFilePath)
if err != nil {
fmt.Printf("read data from %s error: %s\n", args[0], err)
return
}
data = string(dataByte)
} else if _, err := libcrypto.ConfigDecodeKey(args[0]); err == nil {
case 1:
if _, err := libcrypto.ConfigDecodeKey(args[0]); err != nil {
fmt.Printf("read data from %s, error: %s.", args[0], err)
return
}
data = args[0]
} else {
fmt.Println("invailb argument:", args[0])
default:
fmt.Println(cmd.Use)
return
}
decodeData, err := base64.StdEncoding.DecodeString(data)
Expand All @@ -292,27 +306,48 @@ func peerID(cmd *cobra.Command, args []string) {
}

func createPeerID(cmd *cobra.Command, args []string) {
if len(args) > 1 {
if len(args) != 0 {
fmt.Println(cmd.Use)
return
}
var (
peerKeyPath string
err error
sameFile = common.OutFilePath + "/peer.key"
)
if len(args) == 0 {
peerKeyPath, err = os.Getwd()
if err != nil {
fmt.Println(err)
//case 1: check whether peer.key exists. If it doesn't exist, it will generate peer.key. Otherwise, it will return
if common.OutFilePath == common.GetCurrentFilePath() {
if err := util.FileExists(sameFile); err == nil {
fmt.Printf("%s has already existed.\n", sameFile)
return
}
peerKeyPath += "/peer.key"
} else if len(args) == 1 {
if err = util.FileExists(args[0]); err != nil {
fmt.Println(err)
return
peerKeyPath = common.OutFilePath + "/peer.key"
} else {
//case 2: setting the path of output file, the path has already existed.
//case 2.1:if you input a directory, it will check whether the peer.key exits.
//if it doesn't exit, it will generate. Otherwise, it will return
//case 2.2:if you input a file path, it will return. And tell you the file has already existed.
if fileInfo, err := os.Stat(common.OutFilePath); err == nil {
if fileInfo.IsDir() {
if err := util.FileExists(sameFile); err == nil {
fmt.Printf("%s has already existed.\n", sameFile)
return
}
peerKeyPath = common.OutFilePath + "/peer.key"
} else {
fmt.Printf("%s has already existed.\n", common.OutFilePath)
return
}
} else {
//case 3: setting the path of output file, the path doesn't exist. Check the directory of the file whether it exists.
//case 3.1: If it doesnit exist, it will return
//case 3.2:If it exists, it will generate a file to save the peerID
if _, err := os.Stat(filepath.Dir(common.OutFilePath)); err == nil {
peerKeyPath = common.OutFilePath
} else {
fmt.Println(err)
return
}
}
peerKeyPath = args[0] + "/peer.key"
}
key, _, err := libcrypto.GenerateEd25519Key(rand.Reader)
if err != nil {
Expand All @@ -331,4 +366,10 @@ func createPeerID(cmd *cobra.Command, args []string) {
return
}
fmt.Println("generate peer key:", peerKeyPath)
peerID, err := peer.IDFromPublicKey(key.GetPublic())
if err != nil {
fmt.Println("get peer ID from public key error:", err)
return
}
fmt.Println("peer ID:", peerID.Pretty())
}