Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions .golangci.bck.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
linters:
enable:
- asciicheck
- bodyclose
- dogsled
- dupl
- errcheck
- exportloopref
- goconst
- gofmt
- goimports
- revive
- gosimple
- govet
- ineffassign
- misspell
- nakedret
- nolintlint
- prealloc
- staticcheck
- stylecheck
- typecheck
- unconvert
- unused

issues:
max-same-issues: 50

linters-settings:
dogsled:
max-blank-identifiers: 3
46 changes: 26 additions & 20 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,35 +1,41 @@
version: "2"
linters:
enable:
- asciicheck
- bodyclose
- dogsled
- dupl
- errcheck
- exportloopref
- goconst
- gofmt
- goimports
- revive
- gosimple
- govet
- ineffassign
- misspell
- nakedret
- nolintlint
- prealloc
- revive
- staticcheck
- stylecheck
- typecheck
- unconvert
- unused

settings:
dogsled:
max-blank-identifiers: 3
exclusions:
generated: lax
presets:
- comments
- common-false-positives
- legacy
- std-error-handling
paths:
- third_party$
- builtin$
- examples$
issues:
max-same-issues: 50

linters-settings:
dogsled:
max-blank-identifiers: 3
golint:
min-confidence: 0
maligned:
suggest-new: true
formatters:
enable:
- gofmt
- goimports
exclusions:
generated: lax
paths:
- third_party$
- builtin$
- examples$
1 change: 1 addition & 0 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ network:
rpc: ""
registration: ""
lightweight_state: ""
likeness_registry: ""
private_key: ""
vault_address: "https://127.0.0.1:8200"
vault_mount_path: "secret_data"
Expand Down
7 changes: 7 additions & 0 deletions docs/spec/components/schemas/LikenessRequest.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
type: object
x-go-is-request: true
required:
- data
properties:
data:
$ref: '#/components/schemas/LikenessRequestData'
16 changes: 16 additions & 0 deletions docs/spec/components/schemas/LikenessRequestData.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
type: object
required:
- tx_data
- no_send
properties:
meta:
type: object
description: "Metadata if it is required"
example: {"source": "web", "user_id": 123}
tx_data:
type: string
no_send:
type: bool
description: Flag indicates whether transaction should be sent on-chain
example: true

Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
post:
tags:
- Likeness Registry
summary: Likeness Registry
operationId: likeness
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/LikenessRequest'
responses:
'200':
description: Success
content:
application/json:
schema:
type: object
properties:
data:
type: object
$ref: '#/components/schemas/Tx'
'400':
description: Bad Request Error
content:
application/json:
schema:
$ref: '#/components/schemas/Errors'
'500':
description: Internal Error
content:
application/json:
schema:
$ref: '#/components/schemas/Errors'
3 changes: 3 additions & 0 deletions internal/config/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type RelayerConfig struct {
RPC *ethclient.Client
RegistrationAddress common.Address
LightweightStateAddress *common.Address
LikenessRegistryAddress *common.Address
ChainID *big.Int
PrivateKey *ecdsa.PrivateKey
WhiteList whitelist
Expand All @@ -56,6 +57,7 @@ func (e *ethereum) RelayerConfig() *RelayerConfig {
RPC *ethclient.Client `fig:"rpc,required"`
RegistrationAddress common.Address `fig:"registration,required"`
LightweightStateAddress *common.Address `fig:"lightweight_state"`
LikenessRegistryAddress *common.Address `fig:"likeness_registry"`
PrivateKey *ecdsa.PrivateKey `fig:"private_key"`
VaultAddress string `fig:"vault_address"`
VaultMountPath string `fig:"vault_mount_path"`
Expand All @@ -76,6 +78,7 @@ func (e *ethereum) RelayerConfig() *RelayerConfig {
result.RPC = networkConfig.RPC
result.RegistrationAddress = networkConfig.RegistrationAddress
result.LightweightStateAddress = networkConfig.LightweightStateAddress
result.LikenessRegistryAddress = networkConfig.LikenessRegistryAddress

result.ChainID, err = result.RPC.ChainID(context.Background())
if err != nil {
Expand Down
77 changes: 77 additions & 0 deletions internal/service/handlers/likeness_registry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package handlers

import (
"net/http"
"strings"

"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/vm"
validation "github.com/go-ozzo/ozzo-validation/v4"
"github.com/rarimo/registration-relayer/internal/service/requests"
"gitlab.com/distributed_lab/ape"
"gitlab.com/distributed_lab/ape/problems"
"gitlab.com/distributed_lab/logan/v3"
"gitlab.com/distributed_lab/logan/v3/errors"
)

func LikenessRegistry(w http.ResponseWriter, r *http.Request) {
req, err := requests.NewLikenessRequest(r)
if err != nil {
Log(r).WithError(err).Error("failed to get request")
ape.RenderErr(w, problems.BadRequest(err)...)
return
}

logF := logan.F{
"user-agent": r.Header.Get("User-Agent"),
"calldata": req.Data.TxData,
}
if req.Data.Meta != nil {
logF = logF.Merge(*req.Data.Meta)
}

log := Log(r).WithFields(logF)
log.Debug("likeness registry request")

if RelayerConfig(r).LikenessRegistryAddress == nil {
Log(r).Error("likeness registry address is not set in config")
ape.RenderErr(w, problems.InternalError())
return
}

var txd txData
txd.dataBytes, err = hexutil.Decode(req.Data.TxData)
if err != nil {
Log(r).WithError(err).Error("failed to decode data")
ape.RenderErr(w, problems.BadRequest(err)...)
return
}

RelayerConfig(r).LockNonce()
defer RelayerConfig(r).UnlockNonce()

err = confGas(r, &txd, RelayerConfig(r).LikenessRegistryAddress)
if err != nil {
Log(r).WithError(err).Error("failed to configure gas and gasPrice")
// `errors.Is` is not working for rpc errors, they passed as a string without additional wrapping
// because of this we operate with raw strings
if strings.Contains(err.Error(), vm.ErrExecutionReverted.Error()) {
errParts := strings.Split(err.Error(), ":")
contractName := strings.TrimSpace(errParts[len(errParts)-2])
errMsg := errors.New(strings.TrimSpace(errParts[len(errParts)-1]))
ape.RenderErr(w, problems.BadRequest(validation.Errors{contractName: errMsg}.Filter())...)
return
}
ape.RenderErr(w, problems.InternalError())
return
}

tx, err := sendTx(r, &txd, RelayerConfig(r).LikenessRegistryAddress, req.Data.NoSend)
if err != nil {
Log(r).WithError(err).Error("failed to send tx")
ape.RenderErr(w, problems.InternalError())
return
}

ape.Render(w, newTxResponse(tx))
}
21 changes: 21 additions & 0 deletions internal/service/requests/likeness_registry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package requests

import (
"encoding/json"
"net/http"

validation "github.com/go-ozzo/ozzo-validation/v4"
"github.com/rarimo/registration-relayer/resources"
"gitlab.com/distributed_lab/logan/v3/errors"
)

func NewLikenessRequest(r *http.Request) (req resources.LikenessRequest, err error) {
err = json.NewDecoder(r.Body).Decode(&req)
if err != nil {
return req, errors.Wrap(err, "failed to unmarshal")
}

return req, validation.Errors{
"data/tx_data": validation.Validate(req.Data.TxData, validation.Required, validation.Match(calldataRegexp)),
}.Filter()
}
1 change: 1 addition & 0 deletions internal/service/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func (s *service) router() chi.Router {
r.Route("/v1", func(r chi.Router) {
r.Post("/register", handlers.Registration)
r.Post("/transit-state", handlers.TransitState)
r.Post("/likeness-registry", handlers.LikenessRegistry)
})
})

Expand Down
9 changes: 9 additions & 0 deletions resources/model_likeness_request.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
* GENERATED. Do not modify. Your changes might be overwritten!
*/

package resources

type LikenessRequest struct {
Data LikenessRequestData `json:"data"`
}
13 changes: 13 additions & 0 deletions resources/model_likeness_request_data.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* GENERATED. Do not modify. Your changes might be overwritten!
*/

package resources

type LikenessRequestData struct {
// Metadata if it is required
Meta *map[string]interface{} `json:"meta,omitempty"`
// Flag indicates whether transaction should be sent on-chain
NoSend bool `json:"no_send"`
TxData string `json:"tx_data"`
}
Loading