-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c74f8eb
commit e3253a9
Showing
22 changed files
with
989 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
go 1.21.0 | ||
|
||
use ./node-registrar | ||
use ( | ||
./node-registrar | ||
./registrar-cli | ||
) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
PWD := $(shell pwd) | ||
GOPATH := $(shell go env GOPATH) | ||
|
||
all: verifiers test | ||
|
||
test: | ||
@echo "Running Tests" | ||
go test -v ./... | ||
|
||
coverage: clean | ||
@echo "Installing gopherbadger" && go get -u github.com/jpoles1/gopherbadger && go install github.com/jpoles1/gopherbadger | ||
mkdir coverage | ||
go test -v -vet=off ./... -coverprofile=coverage/coverage.out | ||
go tool cover -html=coverage/coverage.out -o coverage/coverage.html | ||
@${GOPATH}/bin/gopherbadger -png=false -md="README.md" | ||
rm coverage.out | ||
go mod tidy | ||
|
||
clean: | ||
rm ./coverage -rf | ||
rm ./bin -rf | ||
|
||
getverifiers: | ||
@echo "Installing golangci-lint" && go install github.com/golangci/golangci-lint/cmd/golangci-lint | ||
go mod tidy | ||
|
||
lint: | ||
@echo "Running $@" | ||
golangci-lint run -c ../.golangci.yml | ||
|
||
build: | ||
@echo "Running $@" | ||
@go build -ldflags=\ | ||
"-X 'github.com/threefoldtech/tfgrid4-sdk-go/node-registrar/tools/registrar-cli/cmd.commit=$(shell git rev-parse HEAD)'\ | ||
-X 'github.com/threefoldtech/tfgrid4-sdk-go/node-registrar/tools/registrar-cli/cmd.version=$(shell git tag --sort=-version:refname | head -n 1)'"\ | ||
-o bin/registrar-cli main.go |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# New Farm Tool | ||
|
||
## Overview | ||
|
||
This tool allows users to create/get/update farm/account/node on a specified network. | ||
|
||
## Installation | ||
|
||
1. Clone the repository: | ||
|
||
```sh | ||
git clone https://github.com/threefoldtech/tfgrid4-sdk-go | ||
``` | ||
|
||
2. Navigate to the project directory: | ||
|
||
```sh | ||
cd node-registrar/tools/registrar-cli | ||
``` | ||
|
||
3. Build the application: | ||
|
||
```sh | ||
go build -o registrar-cli main.go | ||
``` | ||
|
||
## Usage | ||
|
||
```sh | ||
./registrar-cli -seed <seed> -network <network> -farm_name <farm_name> | ||
``` | ||
|
||
### Parameters | ||
|
||
- `-seed` (required): A hexadecimal string used as a private key seed. | ||
- `-network` (required): Specifies the network (`dev`, `qa`, `test`, `main`). | ||
- `-farm_name` (required): The name of the farm to create. | ||
|
||
### Example Usage | ||
|
||
```sh | ||
./registrar-cli -seed aabbccddeeff00112233445566778899aabbccddeeff00112233445566778899 -network dev -farm_name MyFarm | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Package cmd for parsing command line arguments | ||
package cmd | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// createCmd represents the create command | ||
var createCmd = &cobra.Command{ | ||
Use: "create", | ||
Short: "create account/farm on Threefold grid4", | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(createCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
// Package cmd for parsing command line arguments | ||
package cmd | ||
|
||
import ( | ||
"crypto/ed25519" | ||
"crypto/rand" | ||
"encoding/hex" | ||
"fmt" | ||
|
||
"github.com/rs/zerolog/log" | ||
"github.com/spf13/cobra" | ||
"github.com/threefoldtech/tfgrid4-sdk-go/node-registrar/client" | ||
) | ||
|
||
// createAccountCmd represents the cancel command | ||
var createAccountCmd = &cobra.Command{ | ||
Use: "account", | ||
Short: "create new account in node registrar", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
seed, err := cmd.Flags().GetString("seed") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
network, err := cmd.Flags().GetString("network") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
relays, err := cmd.Flags().GetStringArray("relays") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
rmbEncKey, err := cmd.Flags().GetString("rmb-enc-key") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
u, ok := urls[network] | ||
if !ok { | ||
return fmt.Errorf("invalid network %s", network) | ||
} | ||
|
||
if len(seed) == 0 { | ||
seed, err = generateRandomSeed() | ||
if err != nil { | ||
return err | ||
} | ||
log.Info().Msgf("New Seed (Hex): %s", seed) | ||
} | ||
|
||
seedBytes, err := hex.DecodeString(seed) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
privateKey := ed25519.NewKeyFromSeed(seedBytes) | ||
publicKey := privateKey.Public().(ed25519.PublicKey) | ||
|
||
log.Info().Msgf("public key (Hex): %s", hex.EncodeToString(publicKey)) | ||
|
||
cli, err := client.NewRegistrarClient(u, seedBytes) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
account, err := cli.CreateAccount(relays, rmbEncKey) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
log.Info().Uint64("twinID", account.TwinID).Msg("account is created successfully") | ||
|
||
return nil | ||
}, | ||
} | ||
|
||
func init() { | ||
createCmd.AddCommand(createAccountCmd) | ||
createAccountCmd.Flags().StringP("seed", "s", "", "account seed key") | ||
createAccountCmd.Flags().StringP("network", "n", "", "network (dev, qa, test, main)") | ||
createAccountCmd.Flags().StringArrayP("relays", "r", nil, "relays urls") | ||
createAccountCmd.Flags().StringP("rmb-enc-key", "k", "", "rmb encryption key") | ||
} | ||
|
||
func generateRandomSeed() (string, error) { | ||
s := make([]byte, 32) | ||
_, err := rand.Read(s) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
seed := hex.EncodeToString(s) | ||
return seed, nil | ||
} | ||
|
||
func createAccount(c client.RegistrarClient, relays []string, rmbEncKey string) (client.Account, error) { | ||
account, err := c.CreateAccount(relays, rmbEncKey) | ||
if err != nil { | ||
log.Fatal().Err(err).Msg("failed to create new account on registrar") | ||
} | ||
|
||
return account, err | ||
} | ||
|
||
func parseSeed(seed string) (ed25519.PublicKey, ed25519.PrivateKey, []byte, error) { | ||
privateKeyBytes, err := hex.DecodeString(seed) | ||
if err != nil { | ||
return nil, nil, nil, err | ||
} | ||
|
||
privateKey := ed25519.NewKeyFromSeed(privateKeyBytes) | ||
publicKey := privateKey.Public().(ed25519.PublicKey) | ||
|
||
return publicKey, privateKey, privateKeyBytes, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// Package cmd for parsing command line arguments | ||
package cmd | ||
|
||
import ( | ||
"encoding/hex" | ||
"fmt" | ||
|
||
"github.com/rs/zerolog/log" | ||
"github.com/spf13/cobra" | ||
"github.com/threefoldtech/tfgrid4-sdk-go/node-registrar/client" | ||
) | ||
|
||
// createFarmCmd represents the cancel command | ||
var createFarmCmd = &cobra.Command{ | ||
Use: "farm", | ||
Short: "create new farm in node registrar", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
seed, err := cmd.Flags().GetString("seed") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
network, err := cmd.Flags().GetString("network") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
farmName, err := cmd.Flags().GetString("farm-name") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
dedicated, err := cmd.Flags().GetBool("dedicated") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
u, ok := urls[network] | ||
if !ok { | ||
return fmt.Errorf("invalid network %s", network) | ||
} | ||
|
||
if len(seed) == 0 { | ||
return fmt.Errorf("can not initialize registrar client with no seed") | ||
} | ||
|
||
seedBytes, err := hex.DecodeString(seed) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
cli, err := client.NewRegistrarClient(u, seedBytes) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
farm, err := cli.CreateFarm(farmName, dedicated) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
log.Info().Uint64("farmID", farm).Msg("farm is created successfully") | ||
|
||
return nil | ||
}, | ||
} | ||
|
||
func init() { | ||
createCmd.AddCommand(createFarmCmd) | ||
createFarmCmd.Flags().StringP("seed", "s", "", "account seed key") | ||
createFarmCmd.Flags().StringP("network", "n", "", "network (dev, qa, test, main)") | ||
createFarmCmd.Flags().StringP("farm-name", "f", "", "farm name") | ||
createFarmCmd.Flags().BoolP("dedicated", "d", false, "is farm dedicated") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Package cmd for parsing command line arguments | ||
package cmd | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// getCmd represents the get command | ||
var getCmd = &cobra.Command{ | ||
Use: "get", | ||
Short: "get account/farm/node from Threefold grid4", | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(getCmd) | ||
} |
Oops, something went wrong.