Skip to content

Commit

Permalink
feat: add the tlsn middleware and some changes
Browse files Browse the repository at this point in the history
  • Loading branch information
henriquemarlon committed Oct 11, 2024
1 parent 9ce2683 commit 626ddb3
Show file tree
Hide file tree
Showing 11 changed files with 72 additions and 13 deletions.
10 changes: 5 additions & 5 deletions cmd/dapp/dapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ func NewDApp() *router.Router {

app.HandleAdvance("createBid", ah.BidAdvanceHandlers.CreateBidHandler)

app.HandleAdvance("createAuction", ms.RBAC.Middleware(ah.AuctionAdvanceHandlers.CreateAuctionHandler, "admin"))
app.HandleAdvance("finishAuction", ms.RBAC.Middleware(ah.AuctionAdvanceHandlers.FinishAuctionHandler, "admin"))
app.HandleAdvance("createAuction", ms.TLSN.Middleware(ah.AuctionAdvanceHandlers.CreateAuctionHandler))
app.HandleAdvance("finishAuction", ah.AuctionAdvanceHandlers.FinishAuctionHandler)

// app.HandleAdvance("withdrawApp", ms.RBAC.Middleware(ah.UserAdvanceHandlers.WithdrawAppHandler, "admin"))
app.HandleAdvance("withdraw", ah.UserAdvanceHandlers.WithdrawHandler)
Expand Down Expand Up @@ -89,11 +89,11 @@ func NewDAppMemory() *router.Router {

app.HandleAdvance("createBid", ah.BidAdvanceHandlers.CreateBidHandler)

app.HandleAdvance("createAuction", ms.RBAC.Middleware(ah.AuctionAdvanceHandlers.CreateAuctionHandler, "admin"))
app.HandleAdvance("finishAuction", ms.RBAC.Middleware(ah.AuctionAdvanceHandlers.FinishAuctionHandler, "admin"))
app.HandleAdvance("createAuction", ms.TLSN.Middleware(ah.AuctionAdvanceHandlers.CreateAuctionHandler))
app.HandleAdvance("finishAuction", ah.AuctionAdvanceHandlers.FinishAuctionHandler)

// app.HandleAdvance("withdrawApp", ms.RBAC.Middleware(ah.UserAdvanceHandlers.WithdrawAppHandler, "admin"))
app.HandleAdvance("withdrawStablecoin", ah.UserAdvanceHandlers.WithdrawHandler)
app.HandleAdvance("withdraw", ah.UserAdvanceHandlers.WithdrawHandler)

app.HandleAdvance("createUser", ms.RBAC.Middleware(ah.UserAdvanceHandlers.CreateUserHandler, "admin"))
app.HandleAdvance("deleteUser", ms.RBAC.Middleware(ah.UserAdvanceHandlers.DeleteUserByAddressHandler, "admin"))
Expand Down
2 changes: 2 additions & 0 deletions cmd/dapp/wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ var setInspectHandlers = wire.NewSet(
)

var setMiddleware = wire.NewSet(
middleware.NewTLSNMiddleware,
middleware.NewRBACMiddleware,
)

Expand Down Expand Up @@ -124,6 +125,7 @@ func NewInspectHandlersMemory() (*InspectHandlers, error) {
}

type Middlewares struct {
TLSN *middleware.TLSNMiddleware
RBAC *middleware.RBACMiddleware
}

Expand Down
9 changes: 7 additions & 2 deletions cmd/dapp/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions internal/domain/entity/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,16 @@ type UserRepository interface {
type User struct {
Id uint `json:"id" gorm:"primaryKey"`
Role string `json:"role,omitempty" gorm:"not null"`
Username string `json:"username,omitempty" gorm:"uniqueIndex;not null"`
Address custom_type.Address `json:"address,omitempty" gorm:"type:text;uniqueIndex;not null"`
CreatedAt int64 `json:"created_at,omitempty" gorm:"not null"`
UpdatedAt int64 `json:"updated_at,omitempty" gorm:"default:0"`
}

func NewUser(role string, address custom_type.Address, created_at int64) (*User, error) {
func NewUser(role string, username string, address custom_type.Address, created_at int64) (*User, error) {
user := &User{
Role: role,
Username: username,
Address: address,
CreatedAt: created_at,
}
Expand All @@ -41,7 +43,7 @@ func NewUser(role string, address custom_type.Address, created_at int64) (*User,
}

func (u *User) Validate() error {
if u.Role == "" || u.Address.Address == (common.Address{}) {
if u.Role == "" || u.Username == "" || u.Address.Address == (common.Address{}) {
return ErrInvalidUser
}
return nil
Expand Down
9 changes: 6 additions & 3 deletions internal/domain/entity/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ import (

func TestNewUser_Success(t *testing.T) {
role := "admin"
username := "test"
address := custom_type.Address{Address: common.HexToAddress("0x123")}
createdAt := time.Now().Unix()

user, err := NewUser(role, address, createdAt)
user, err := NewUser(role, username, address, createdAt)
assert.NoError(t, err)
assert.NotNil(t, user)
assert.Equal(t, role, user.Role)
Expand All @@ -24,18 +25,20 @@ func TestNewUser_Success(t *testing.T) {

func TestNewUser_Fail_InvalidUser(t *testing.T) {
role := ""
username := "test"
address := custom_type.Address{Address: common.HexToAddress("0x123")}
createdAt := time.Now().Unix()

user, err := NewUser(role, address, createdAt)
user, err := NewUser(role, username, address, createdAt)
assert.Error(t, err)
assert.Nil(t, user)
assert.Equal(t, ErrInvalidUser, err)

role = "admin"
username = ""
address = custom_type.Address{Address: common.Address{}}

user, err = NewUser(role, address, createdAt)
user, err = NewUser(role, username, address, createdAt)
assert.Error(t, err)
assert.Nil(t, user)
assert.Equal(t, ErrInvalidUser, err)
Expand Down
40 changes: 40 additions & 0 deletions internal/infra/cartesi/middleware/tlsn.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package middleware

import (
"database/sql"
"errors"
"fmt"

"github.com/rollmelette/rollmelette"
"github.com/tribeshq/tribes/internal/domain/entity"
"github.com/tribeshq/tribes/internal/usecase/user_usecase"
"github.com/tribeshq/tribes/pkg/custom_type"
"github.com/tribeshq/tribes/pkg/router"
)

type TLSNMiddleware struct {
UserRepository entity.UserRepository
}

func NewTLSNMiddleware(userRepository entity.UserRepository) *TLSNMiddleware {
return &TLSNMiddleware{
UserRepository: userRepository,
}
}

func (m *TLSNMiddleware) Middleware(handlerFunc router.AdvanceHandlerFunc) router.AdvanceHandlerFunc {
return func(env rollmelette.Env, metadata rollmelette.Metadata, deposit rollmelette.Deposit, payload []byte) error {
findUserByAddress := user_usecase.NewFindUserByAddressUseCase(m.UserRepository)
_, err := findUserByAddress.Execute(&user_usecase.FindUserByAddressInputDTO{
Address: custom_type.NewAddress(metadata.MsgSender),
})
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return fmt.Errorf("user not found during RBAC middleware check")
}
return err
}
// TODO: call tlsn verifier here
return handlerFunc(env, metadata, deposit, payload)
}
}
5 changes: 4 additions & 1 deletion internal/usecase/user_usecase/create_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ import (

type CreateUserInputDTO struct {
Role string `json:"role"`
Username string `json:"username"`
Address custom_type.Address `json:"address"`
}

type CreateUserOutputDTO struct {
Id uint `json:"id"`
Role string `json:"role"`
Username string `json:"username"`
Address custom_type.Address `json:"address"`
CreatedAt int64 `json:"created_at"`
}
Expand All @@ -29,7 +31,7 @@ func NewCreateUserUseCase(userRepository entity.UserRepository) *CreateUserUseCa
}

func (u *CreateUserUseCase) Execute(input *CreateUserInputDTO, metadata rollmelette.Metadata) (*CreateUserOutputDTO, error) {
user, err := entity.NewUser(input.Role, input.Address, metadata.BlockTimestamp)
user, err := entity.NewUser(input.Role, input.Username, input.Address, metadata.BlockTimestamp)
if err != nil {
return nil, err
}
Expand All @@ -40,6 +42,7 @@ func (u *CreateUserUseCase) Execute(input *CreateUserInputDTO, metadata rollmele
return &CreateUserOutputDTO{
Id: res.Id,
Role: res.Role,
Username: res.Username,
Address: res.Address,
CreatedAt: res.CreatedAt,
}, nil
Expand Down
1 change: 1 addition & 0 deletions internal/usecase/user_usecase/find_all_users.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func (u *FindAllUsersUseCase) Execute() (*FindAllUsersOutputDTO, error) {
output[i] = &FindUserOutputDTO{
Id: user.Id,
Role: user.Role,
Username: user.Username,
Address: user.Address,
CreatedAt: user.CreatedAt,
UpdatedAt: user.UpdatedAt,
Expand Down
1 change: 1 addition & 0 deletions internal/usecase/user_usecase/find_user_by_address.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func (u *FindUserByAddressUseCase) Execute(input *FindUserByAddressInputDTO) (*F
return &FindUserOutputDTO{
Id: res.Id,
Role: res.Role,
Username: res.Username,
Address: res.Address,
CreatedAt: res.CreatedAt,
UpdatedAt: res.UpdatedAt,
Expand Down
1 change: 1 addition & 0 deletions internal/usecase/user_usecase/find_user_by_role.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func (u *FindUserByRoleUseCase) Execute(input *FindUserByRoleInputDTO) (*FindUse
return &FindUserOutputDTO{
Id: res.Id,
Role: res.Role,
Username: res.Username,
Address: res.Address,
CreatedAt: res.CreatedAt,
UpdatedAt: res.UpdatedAt,
Expand Down
1 change: 1 addition & 0 deletions internal/usecase/user_usecase/general_dto.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import "github.com/tribeshq/tribes/pkg/custom_type"
type FindUserOutputDTO struct {
Id uint `json:"id"`
Role string `json:"role"`
Username string `json:"username"`
Address custom_type.Address `json:"address"`
CreatedAt int64 `json:"created_at"`
UpdatedAt int64 `json:"updated_at"`
Expand Down

0 comments on commit 626ddb3

Please sign in to comment.