Skip to content

Commit

Permalink
Simplify auth in examples (#1105)
Browse files Browse the repository at this point in the history
* embed auth into hypersdk/chain/auth

Signed-off-by: Containerman17 <[email protected]>

* move auth to it's own package

Signed-off-by: Containerman17 <[email protected]>

---------

Signed-off-by: Containerman17 <[email protected]>
  • Loading branch information
containerman17 authored Jul 13, 2024
1 parent 7d5659d commit f275152
Show file tree
Hide file tree
Showing 25 changed files with 40 additions and 220 deletions.
5 changes: 2 additions & 3 deletions examples/morpheusvm/auth/bls.go → auth/bls.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/ava-labs/hypersdk/codec"
"github.com/ava-labs/hypersdk/crypto"
"github.com/ava-labs/hypersdk/crypto/bls"
"github.com/ava-labs/hypersdk/examples/morpheusvm/consts"
"github.com/ava-labs/hypersdk/utils"
)

Expand All @@ -36,7 +35,7 @@ func (b *BLS) address() codec.Address {
}

func (*BLS) GetTypeID() uint8 {
return consts.BLSID
return BLSID
}

func (*BLS) ComputeUnits(chain.Rules) uint64 {
Expand Down Expand Up @@ -113,5 +112,5 @@ func (*BLSFactory) MaxUnits() (uint64, uint64) {
}

func NewBLSAddress(pk *bls.PublicKey) codec.Address {
return codec.CreateAddress(consts.BLSID, utils.ToID(bls.PublicKeyToBytes(pk)))
return codec.CreateAddress(BLSID, utils.ToID(bls.PublicKeyToBytes(pk)))
}
11 changes: 8 additions & 3 deletions examples/tokenvm/auth/consts.go → auth/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,20 @@

package auth

import "github.com/ava-labs/hypersdk/vm"
import (
"github.com/ava-labs/hypersdk/vm"
)

// Note: Registry will error during initialization if a duplicate ID is assigned. We explicitly assign IDs to avoid accidental remapping.
const (
ed25519ID uint8 = 0
// Auth TypeIDs
ED25519ID uint8 = 0
SECP256R1ID uint8 = 1
BLSID uint8 = 2
)

func Engines() map[uint8]vm.AuthEngine {
return map[uint8]vm.AuthEngine{
ed25519ID: &ED25519AuthEngine{},
ED25519ID: &ED25519AuthEngine{},
}
}
4 changes: 2 additions & 2 deletions examples/tokenvm/auth/ed25519.go → auth/ed25519.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (d *ED25519) address() codec.Address {
}

func (*ED25519) GetTypeID() uint8 {
return ed25519ID
return ED25519ID
}

func (*ED25519) ComputeUnits(chain.Rules) uint64 {
Expand Down Expand Up @@ -149,5 +149,5 @@ func (b *ED25519Batch) Done() []func() error {
}

func NewED25519Address(pk ed25519.PublicKey) codec.Address {
return codec.CreateAddress(ed25519ID, utils.ToID(pk[:]))
return codec.CreateAddress(ED25519ID, utils.ToID(pk[:]))
}
5 changes: 2 additions & 3 deletions examples/morpheusvm/auth/secp256r1.go → auth/secp256r1.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/ava-labs/hypersdk/codec"
"github.com/ava-labs/hypersdk/crypto"
"github.com/ava-labs/hypersdk/crypto/secp256r1"
"github.com/ava-labs/hypersdk/examples/morpheusvm/consts"
"github.com/ava-labs/hypersdk/utils"
)

Expand All @@ -36,7 +35,7 @@ func (d *SECP256R1) address() codec.Address {
}

func (*SECP256R1) GetTypeID() uint8 {
return consts.SECP256R1ID
return SECP256R1ID
}

func (*SECP256R1) ComputeUnits(chain.Rules) uint64 {
Expand Down Expand Up @@ -103,5 +102,5 @@ func (*SECP256R1Factory) MaxUnits() (uint64, uint64) {
}

func NewSECP256R1Address(pk secp256r1.PublicKey) codec.Address {
return codec.CreateAddress(consts.SECP256R1ID, utils.ToID(pk[:]))
return codec.CreateAddress(SECP256R1ID, utils.ToID(pk[:]))
}
16 changes: 0 additions & 16 deletions examples/morpheusvm/auth/consts.go

This file was deleted.

154 changes: 0 additions & 154 deletions examples/morpheusvm/auth/ed25519.go

This file was deleted.

8 changes: 4 additions & 4 deletions examples/morpheusvm/cmd/morpheus-cli/cmd/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import (

"github.com/ava-labs/avalanchego/ids"

"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/morpheusvm/auth"
"github.com/ava-labs/hypersdk/examples/morpheusvm/consts"
"github.com/ava-labs/hypersdk/pubsub"
"github.com/ava-labs/hypersdk/rpc"
Expand Down Expand Up @@ -47,11 +47,11 @@ func (h *Handler) DefaultActor() (
}
var factory chain.AuthFactory
switch addr[0] {
case consts.ED25519ID:
case auth.ED25519ID:
factory = auth.NewED25519Factory(ed25519.PrivateKey(priv))
case consts.SECP256R1ID:
case auth.SECP256R1ID:
factory = auth.NewSECP256R1Factory(secp256r1.PrivateKey(priv))
case consts.BLSID:
case auth.BLSID:
p, err := bls.PrivateKeyFromBytes(priv)
if err != nil {
return ids.Empty, nil, nil, nil, nil, nil, err
Expand Down
8 changes: 4 additions & 4 deletions examples/morpheusvm/cmd/morpheus-cli/cmd/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import (
"github.com/ava-labs/avalanchego/ids"
"github.com/spf13/cobra"

"github.com/ava-labs/hypersdk/auth"
"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/morpheusvm/auth"
"github.com/ava-labs/hypersdk/examples/morpheusvm/consts"
"github.com/ava-labs/hypersdk/utils"

Expand All @@ -39,11 +39,11 @@ func checkKeyType(k string) error {

func getKeyType(addr codec.Address) (string, error) {
switch addr[0] {
case consts.ED25519ID:
case auth.ED25519ID:
return ed25519Key, nil
case consts.SECP256R1ID:
case auth.SECP256R1ID:
return secp256r1Key, nil
case consts.BLSID:
case auth.BLSID:
return blsKey, nil
default:
return "", ErrInvalidKeyType
Expand Down
8 changes: 4 additions & 4 deletions examples/morpheusvm/cmd/morpheus-cli/cmd/spam.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import (
"github.com/ava-labs/avalanchego/ids"
"github.com/spf13/cobra"

"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/morpheusvm/actions"
"github.com/ava-labs/hypersdk/examples/morpheusvm/auth"
"github.com/ava-labs/hypersdk/examples/morpheusvm/consts"
"github.com/ava-labs/hypersdk/pubsub"
"github.com/ava-labs/hypersdk/rpc"
Expand All @@ -27,11 +27,11 @@ import (

func getFactory(priv *cli.PrivateKey) (chain.AuthFactory, error) {
switch priv.Address[0] {
case consts.ED25519ID:
case auth.ED25519ID:
return auth.NewED25519Factory(ed25519.PrivateKey(priv.Bytes)), nil
case consts.SECP256R1ID:
case auth.SECP256R1ID:
return auth.NewSECP256R1Factory(secp256r1.PrivateKey(priv.Bytes)), nil
case consts.BLSID:
case auth.BLSID:
p, err := bls.PrivateKeyFromBytes(priv.Bytes)
if err != nil {
return nil, err
Expand Down
5 changes: 0 additions & 5 deletions examples/morpheusvm/consts/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,4 @@ package consts
const (
// Action TypeIDs
TransferID uint8 = 0

// Auth TypeIDs
ED25519ID uint8 = 0
SECP256R1ID uint8 = 1
BLSID uint8 = 2
)
2 changes: 1 addition & 1 deletion examples/morpheusvm/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import (
"github.com/ava-labs/avalanchego/snow"
"go.uber.org/zap"

"github.com/ava-labs/hypersdk/auth"
"github.com/ava-labs/hypersdk/builder"
"github.com/ava-labs/hypersdk/chain"
"github.com/ava-labs/hypersdk/examples/morpheusvm/actions"
"github.com/ava-labs/hypersdk/examples/morpheusvm/auth"
"github.com/ava-labs/hypersdk/examples/morpheusvm/config"
"github.com/ava-labs/hypersdk/examples/morpheusvm/consts"
"github.com/ava-labs/hypersdk/examples/morpheusvm/genesis"
Expand Down
2 changes: 1 addition & 1 deletion examples/morpheusvm/registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ package registry
import (
"github.com/ava-labs/avalanchego/utils/wrappers"

"github.com/ava-labs/hypersdk/auth"
"github.com/ava-labs/hypersdk/chain"
"github.com/ava-labs/hypersdk/codec"
"github.com/ava-labs/hypersdk/examples/morpheusvm/actions"
"github.com/ava-labs/hypersdk/examples/morpheusvm/auth"
"github.com/ava-labs/hypersdk/examples/morpheusvm/consts"
)

Expand Down
2 changes: 1 addition & 1 deletion examples/morpheusvm/tests/e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ import (
"github.com/fatih/color"
"github.com/stretchr/testify/require"

"github.com/ava-labs/hypersdk/auth"
"github.com/ava-labs/hypersdk/chain"
"github.com/ava-labs/hypersdk/codec"
"github.com/ava-labs/hypersdk/crypto/ed25519"
"github.com/ava-labs/hypersdk/examples/morpheusvm/actions"
"github.com/ava-labs/hypersdk/examples/morpheusvm/auth"
"github.com/ava-labs/hypersdk/examples/morpheusvm/consts"
"github.com/ava-labs/hypersdk/rpc"
"github.com/ava-labs/hypersdk/utils"
Expand Down
Loading

0 comments on commit f275152

Please sign in to comment.