Skip to content

Commit

Permalink
refactor eip712 signer out from resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
daveroga committed Sep 2, 2024
1 parent af00532 commit e84025b
Show file tree
Hide file tree
Showing 10 changed files with 333 additions and 465 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@
# Envs
configs/driver.yaml
.env*
resolvers.settings.yaml
*.settings.yaml
test.http
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ RUN CGO_ENABLED=0 go build -o ./driver ./cmd/driver/main.go
# Build an driver image
FROM scratch

COPY ./resolvers.settings.yaml /app/resolvers.settings.yaml
COPY ./*.settings.yaml /app/
COPY --from=base /build/driver /app/driver
COPY --from=base /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/

Expand All @@ -28,4 +28,4 @@ ENV HOST=0.0.0.0
ENV PORT=8080

# Command to run
ENTRYPOINT ["/app/driver", "/app/resolvers.settings.yaml"]
ENTRYPOINT ["/app/driver", "/app/resolvers.settings.yaml", "/app/signers.settings.yaml"]
30 changes: 27 additions & 3 deletions cmd/driver/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/iden3/driver-did-iden3/pkg/services"
"github.com/iden3/driver-did-iden3/pkg/services/blockchain/eth"
"github.com/iden3/driver-did-iden3/pkg/services/ens"
"github.com/iden3/driver-did-iden3/pkg/services/signers"
)

func main() {
Expand All @@ -34,7 +35,7 @@ func main() {
}

mux := app.Handlers{DidDocumentHandler: &app.DidDocumentHandler{
DidDocumentService: services.NewDidDocumentServices(initResolvers(), r),
DidDocumentService: services.NewDidDocumentServices(initResolvers(), r, services.WithSigners(initEIP712Signers())),
},
}

Expand Down Expand Up @@ -63,8 +64,7 @@ func initResolvers() *services.ResolverRegistry {
for chainName, chainSettings := range rs {
for networkName, networkSettings := range chainSettings {
prefix := fmt.Sprintf("%s:%s", chainName, networkName)
resolver, err := eth.NewResolver(networkSettings.NetworkURL, networkSettings.ContractAddress,
eth.WithSigner(networkSettings.WalletKey))
resolver, err := eth.NewResolver(networkSettings.NetworkURL, networkSettings.ContractAddress)
if err != nil {
log.Fatalf("failed configure resolver for network '%s': %v", prefix, err)
}
Expand All @@ -74,3 +74,27 @@ func initResolvers() *services.ResolverRegistry {

return resolvers
}

func initEIP712Signers() *services.EIP712SignerRegistry {
var path string
if len(os.Args) > 3 {
path = os.Args[2]
}
rs, err := configs.ParseSignersSettings(path)
if err != nil {
log.Fatal("can't read signers settings:", err)
}
chainSigners := services.NewChainEIP712Signers()
for chainName, chainSettings := range rs {
for networkName, networkSettings := range chainSettings {
prefix := fmt.Sprintf("%s:%s", chainName, networkName)
signer, err := signers.NewEIP712Signer(networkSettings.WalletKey)
if err != nil {
log.Fatalf("failed configure signer for network '%s': %v", prefix, err)
}
chainSigners.Add(prefix, signer)
}
}

return chainSigners
}
29 changes: 28 additions & 1 deletion pkg/app/configs/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,16 @@ import (
)

const defaultPathToResolverSettings = "./resolvers.settings.yaml"
const defaultPathToSignersSettings = "./signers.settings.yaml"

// ResolverSettings represent settings for resolver.
type ResolverSettings map[string]map[string]struct {
ContractAddress string `yaml:"contractAddress"`
NetworkURL string `yaml:"networkURL"`
WalletKey string `yaml:"walletKey"`
}

type SignersSettings map[string]map[string]struct {
WalletKey string `yaml:"walletKey"`
}

// Config structure represent yaml config for did driver.
Expand Down Expand Up @@ -61,3 +65,26 @@ func ParseResolversSettings(path string) (ResolverSettings, error) {

return settings, nil
}

// ParseSignersSettings parse yaml file with signers settings.
func ParseSignersSettings(path string) (SignersSettings, error) {
if path == "" {
path = defaultPathToSignersSettings
}
f, err := os.Open(filepath.Clean(path))
if err != nil {
return nil, err
}
defer func() {
if err := f.Close(); err != nil {
log.Println("failed to close setting file:", err)
}
}()

settings := SignersSettings{}
if err := yaml.NewDecoder(f).Decode(&settings); err != nil {
return nil, errors.Errorf("invalid yaml file: %v", settings)
}

return settings, nil
}
Loading

0 comments on commit e84025b

Please sign in to comment.