Skip to content

Commit

Permalink
vmwithcontracts lint
Browse files Browse the repository at this point in the history
  • Loading branch information
samliok committed Oct 8, 2024
1 parent d00518b commit 9e30569
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (h *Handler) Root() *cli.Handler {
}

func (h *Handler) DefaultActor() (
ids.ID, *cli.PrivateKey, chain.AuthFactory,
ids.ID, *auth.PrivateKey, chain.AuthFactory,
*jsonrpc.JSONRPCClient, *vm.JSONRPCClient, *ws.WebSocketClient, error,
) {
addr, priv, err := h.h.GetDefaultKey(true)
Expand Down Expand Up @@ -73,7 +73,7 @@ func (h *Handler) DefaultActor() (
return ids.Empty, nil, nil, nil, nil, nil, err
}
// For [defaultActor], we always send requests to the first returned URI.
return chainID, &cli.PrivateKey{
return chainID, &auth.PrivateKey{
Address: addr,
Bytes: priv,
}, factory, jcli,
Expand Down
17 changes: 8 additions & 9 deletions examples/vmwithcontracts/cmd/vmwithcontracts-cli/cmd/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/spf13/cobra"

"github.com/ava-labs/hypersdk/auth"
"github.com/ava-labs/hypersdk/cli"
"github.com/ava-labs/hypersdk/crypto/bls"
"github.com/ava-labs/hypersdk/crypto/ed25519"
"github.com/ava-labs/hypersdk/crypto/secp256r1"
Expand All @@ -31,14 +30,14 @@ func checkKeyType(k string) error {
}
}

func generatePrivateKey(k string) (*cli.PrivateKey, error) {
func generatePrivateKey(k string) (*auth.PrivateKey, error) {
switch k {
case ed25519Key:
p, err := ed25519.GeneratePrivateKey()
if err != nil {
return nil, err
}
return &cli.PrivateKey{
return &auth.PrivateKey{
Address: auth.NewED25519Address(p.PublicKey()),
Bytes: p[:],
}, nil
Expand All @@ -47,7 +46,7 @@ func generatePrivateKey(k string) (*cli.PrivateKey, error) {
if err != nil {
return nil, err
}
return &cli.PrivateKey{
return &auth.PrivateKey{
Address: auth.NewSECP256R1Address(p.PublicKey()),
Bytes: p[:],
}, nil
Expand All @@ -56,7 +55,7 @@ func generatePrivateKey(k string) (*cli.PrivateKey, error) {
if err != nil {
return nil, err
}
return &cli.PrivateKey{
return &auth.PrivateKey{
Address: auth.NewBLSAddress(bls.PublicFromPrivateKey(p)),
Bytes: bls.PrivateKeyToBytes(p),
}, nil
Expand All @@ -65,15 +64,15 @@ func generatePrivateKey(k string) (*cli.PrivateKey, error) {
}
}

func loadPrivateKey(k string, path string) (*cli.PrivateKey, error) {
func loadPrivateKey(k string, path string) (*auth.PrivateKey, error) {
switch k {
case ed25519Key:
p, err := utils.LoadBytes(path, ed25519.PrivateKeyLen)
if err != nil {
return nil, err
}
pk := ed25519.PrivateKey(p)
return &cli.PrivateKey{
return &auth.PrivateKey{
Address: auth.NewED25519Address(pk.PublicKey()),
Bytes: p,
}, nil
Expand All @@ -83,7 +82,7 @@ func loadPrivateKey(k string, path string) (*cli.PrivateKey, error) {
return nil, err
}
pk := secp256r1.PrivateKey(p)
return &cli.PrivateKey{
return &auth.PrivateKey{
Address: auth.NewSECP256R1Address(pk.PublicKey()),
Bytes: p,
}, nil
Expand All @@ -97,7 +96,7 @@ func loadPrivateKey(k string, path string) (*cli.PrivateKey, error) {
if err != nil {
return nil, err
}
return &cli.PrivateKey{
return &auth.PrivateKey{
Address: auth.NewBLSAddress(bls.PublicFromPrivateKey(privKey)),
Bytes: p,
}, nil
Expand Down
38 changes: 5 additions & 33 deletions examples/vmwithcontracts/cmd/vmwithcontracts-cli/cmd/spam.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,10 @@ import (
"github.com/ava-labs/hypersdk/api/ws"
"github.com/ava-labs/hypersdk/auth"
"github.com/ava-labs/hypersdk/chain"
"github.com/ava-labs/hypersdk/cli"
"github.com/ava-labs/hypersdk/codec"
"github.com/ava-labs/hypersdk/crypto/bls"
"github.com/ava-labs/hypersdk/crypto/ed25519"
"github.com/ava-labs/hypersdk/crypto/secp256r1"
"github.com/ava-labs/hypersdk/examples/vmwithcontracts/actions"
"github.com/ava-labs/hypersdk/examples/vmwithcontracts/consts"
"github.com/ava-labs/hypersdk/examples/vmwithcontracts/vm"
"github.com/ava-labs/hypersdk/pubsub"
"github.com/ava-labs/hypersdk/utils"
)

type SpamHelper struct {
Expand All @@ -29,27 +23,10 @@ type SpamHelper struct {
ws *ws.WebSocketClient
}

func (sh *SpamHelper) CreateAccount() (*cli.PrivateKey, error) {
func (sh *SpamHelper) CreateAccount() (*auth.PrivateKey, error) {
return generatePrivateKey(sh.keyType)
}

func (*SpamHelper) GetFactory(pk *cli.PrivateKey) (chain.AuthFactory, error) {
switch pk.Address[0] {
case auth.ED25519ID:
return auth.NewED25519Factory(ed25519.PrivateKey(pk.Bytes)), nil
case auth.SECP256R1ID:
return auth.NewSECP256R1Factory(secp256r1.PrivateKey(pk.Bytes)), nil
case auth.BLSID:
p, err := bls.PrivateKeyFromBytes(pk.Bytes)
if err != nil {
return nil, err
}
return auth.NewBLSFactory(p), nil
default:
return nil, ErrInvalidKeyType
}
}

func (sh *SpamHelper) CreateClient(uri string) error {
sh.cli = vm.NewJSONRPCClient(uri)
ws, err := ws.NewWebSocketClient(uri, ws.DefaultHandshakeTimeout, pubsub.MaxPendingMessages, pubsub.MaxReadMessageSize)
Expand All @@ -64,18 +41,12 @@ func (sh *SpamHelper) GetParser(ctx context.Context) (chain.Parser, error) {
return sh.cli.Parser(ctx)
}

func (sh *SpamHelper) LookupBalance(choice int, address codec.Address) (uint64, error) {
func (sh *SpamHelper) LookupBalance(address codec.Address) (uint64, error) {
balance, err := sh.cli.Balance(context.TODO(), address)
if err != nil {
return 0, err
}
utils.Outf(
"%d) {{cyan}}address:{{/}} %s {{cyan}}balance:{{/}} %s %s\n",
choice,
address,
utils.FormatBalance(balance),
consts.Symbol,
)

return balance, err
}

Expand Down Expand Up @@ -103,6 +74,7 @@ var runSpamCmd = &cobra.Command{
return checkKeyType(args[0])
},
RunE: func(_ *cobra.Command, args []string) error {
return handler.Root().Spam(&SpamHelper{keyType: args[0]})
ctx := context.Background()
return handler.Root().Spam(ctx, &SpamHelper{keyType: args[0]}, false)
},
}
2 changes: 1 addition & 1 deletion examples/vmwithcontracts/tests/e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ var _ = ginkgo.SynchronizedBeforeSuite(func() []byte {

// Import HyperSDK e2e test coverage and inject VMWithContracts name
// and workload factory to orchestrate the test.
he2e.SetWorkload(consts.Name, workloadFactory, parser, expectedABI)
he2e.SetWorkload(consts.Name, workloadFactory, expectedABI, parser, nil, nil)

tc := e2e.NewTestContext()

Expand Down

0 comments on commit 9e30569

Please sign in to comment.