Skip to content
This repository was archived by the owner on Apr 4, 2023. It is now read-only.

Commit 701a0c4

Browse files
Merge pull request #3 from ava-labs/cleanup
Cleanup
2 parents bcecd83 + 331d89f commit 701a0c4

File tree

9 files changed

+215
-203
lines changed

9 files changed

+215
-203
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,5 @@ db*
4747
bin/
4848
build/
4949
vendor
50+
51+
.quark-cli-pk

README.md

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Build quarkvm:
1818
cd ${HOME}/go/src/github.com/ava-labs/quarkvm
1919
./scripts/build.sh
2020
```
21-
21+
## TODO: MIGRATE TO USING AVA-SIM
2222
*Step 1.* To interact with Avalanche network RPC chain APIs, download and run a [AvalancheGo](https://github.com/ava-labs/avalanchego#installation) node locally, as follows:
2323

2424
```bash
@@ -135,18 +135,15 @@ curl --location --request POST '127.0.0.1:9650/ext/P' \
135135
#
136136
```
137137

138-
Connect to quarkvm:
138+
*Step 7.* Interact with quarkVM using quark-cli:
139139

140140
```bash
141-
curl --location --request POST '127.0.0.1:9650/ext/vm/tGas3T58KzdjLHhBDMnH2TvrddhqTji5iZAMZ3RXs2NLpSnhH' \
142-
--header 'Content-Type: application/json' \
143-
--data-raw '{
144-
"jsonrpc":"2.0",
145-
"id" :1,
146-
"method" :"quarkvm.put",
147-
"params" :{
148-
"key":"foo",
149-
"value":"bar"
150-
}
151-
}'
141+
# TODO: add config file/env for key location and/or RPC
142+
quark-cli create
143+
quark-cli claim jim.avax
144+
quark-cli set jim.avax/twitter @jimbo
145+
quark-cli lifeline jim.avax
146+
quark-cli get jim.avax/twitter
147+
quark-cli info jim.avax (remaining life, num keys, claimed/unclaimed/expired)
148+
quark-cli keys jim.avax (get all keys values)
152149
```

agent/agent.go

Lines changed: 0 additions & 137 deletions
This file was deleted.

cmd/quarkcli/claim/claim.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package claim
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
"github.com/ava-labs/quarkvm/cmd/quarkcli/create"
8+
9+
"github.com/fatih/color"
10+
"github.com/spf13/cobra"
11+
)
12+
13+
func init() {
14+
cobra.EnablePrefixMatching = true
15+
}
16+
17+
// NewCommand implements "quark-cli" command.
18+
func NewCommand() *cobra.Command {
19+
cmd := &cobra.Command{
20+
Use: "claim [options] <prefix>",
21+
Short: "Claims the given prefix",
22+
Long: `
23+
Claims the given prefix.
24+
25+
# "jim" is the prefix
26+
$ quark-cli claim jim
27+
28+
`,
29+
RunE: claimFunc,
30+
}
31+
return cmd
32+
}
33+
34+
func claimFunc(cmd *cobra.Command, args []string) error {
35+
pk, err := create.LoadPK()
36+
if err != nil {
37+
return err
38+
}
39+
color.Green("loaded address %s", pk.PublicKey().Address())
40+
p := getClaimOp(args)
41+
fmt.Println("NOT IMPLEMENTED YET:", p)
42+
// get difficulty/blockhash to target
43+
// mine
44+
// submit -> wait -> re-mine
45+
return nil
46+
}
47+
48+
func getClaimOp(args []string) (prefix string) {
49+
if len(args) != 1 {
50+
fmt.Fprintf(os.Stderr, "expected one argument, got %d\n", len(args))
51+
os.Exit(128)
52+
}
53+
54+
return args[0]
55+
}

cmd/quarkcli/create/create.go

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package create
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"os"
7+
"path"
8+
9+
"github.com/ava-labs/quarkvm/crypto/ed25519"
10+
11+
"github.com/fatih/color"
12+
"github.com/spf13/cobra"
13+
)
14+
15+
const (
16+
privateKeyFile = ".quark-cli-pk"
17+
)
18+
19+
func init() {
20+
cobra.EnablePrefixMatching = true
21+
}
22+
23+
// NewCommand implements "quark-cli" command.
24+
func NewCommand() *cobra.Command {
25+
cmd := &cobra.Command{
26+
Use: "create [options]",
27+
Short: "Creates a new key in the default location",
28+
Long: `
29+
Creates a new key in the default location.
30+
31+
$ quark-cli create
32+
33+
`,
34+
RunE: createFunc,
35+
}
36+
return cmd
37+
}
38+
39+
func getPKLocation() (string, error) {
40+
p, err := os.Getwd()
41+
if err != nil {
42+
return "", err
43+
}
44+
return path.Join(p, privateKeyFile), nil
45+
}
46+
47+
// TODO: run before all functions (erroring if can't load)
48+
func LoadPK() (ed25519.PrivateKey, error) {
49+
pkLocation, err := getPKLocation()
50+
if err != nil {
51+
return nil, err
52+
}
53+
54+
pk, err := os.ReadFile(pkLocation)
55+
if err != nil {
56+
return nil, err
57+
}
58+
return ed25519.LoadPrivateKey(pk)
59+
}
60+
61+
func createFunc(cmd *cobra.Command, args []string) error {
62+
// Error if key already exists
63+
pkLocation, err := getPKLocation()
64+
if err != nil {
65+
return err
66+
}
67+
if _, err := os.Stat(pkLocation); err == nil {
68+
return fmt.Errorf("file already exists at %s", pkLocation)
69+
} else if !errors.Is(err, os.ErrNotExist) {
70+
return err
71+
}
72+
73+
// Generate new key and save to disk
74+
// TODO: encrypt key
75+
pk, err := ed25519.NewPrivateKey()
76+
if err != nil {
77+
return err
78+
}
79+
if err := os.WriteFile(pkLocation, pk.Bytes(), 0644); err != nil {
80+
return err
81+
}
82+
color.Green("created address %s and saved to %s", pk.PublicKey().Address(), pkLocation)
83+
return nil
84+
}

cmd/quarkvmctl/main.go renamed to cmd/quarkcli/main.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
// Copyright (C) 2019-2021, Ava Labs, Inc. All rights reserved.
22
// See the file LICENSE for licensing terms.
33

4-
// quarkvmctl is a set of quarkvm client commands
5-
// to interact with KVVM servers.
64
package main
75

86
import (
97
"fmt"
108
"os"
119

12-
"github.com/ava-labs/quarkvm/cmd/quarkvmctl/put"
10+
"github.com/ava-labs/quarkvm/cmd/quarkcli/claim"
11+
"github.com/ava-labs/quarkvm/cmd/quarkcli/create"
12+
"github.com/ava-labs/quarkvm/cmd/quarkcli/put"
13+
1314
"github.com/spf13/cobra"
1415
)
1516

1617
var rootCmd = &cobra.Command{
17-
Use: "quarkvmctl",
18-
Short: "Quark KVVM client CLI",
19-
SuggestFor: []string{"quarkvm-ctl"},
18+
Use: "quark-cli",
19+
Short: "QuarkVM client CLI",
20+
SuggestFor: []string{"quark-cli"},
2021
}
2122

2223
func init() {
@@ -25,13 +26,15 @@ func init() {
2526

2627
func init() {
2728
rootCmd.AddCommand(
29+
create.NewCommand(),
30+
claim.NewCommand(),
2831
put.NewCommand(),
2932
)
3033
}
3134

3235
func main() {
3336
if err := rootCmd.Execute(); err != nil {
34-
fmt.Fprintf(os.Stderr, "quarkvmctl failed %v\n", err)
37+
fmt.Fprintf(os.Stderr, "quark-cli failed %v\n", err)
3538
os.Exit(1)
3639
}
3740
os.Exit(0)

0 commit comments

Comments
 (0)