From c33fb6010bf2efad9a38f96af8514f3b80c27377 Mon Sep 17 00:00:00 2001 From: vbasiuk Date: Mon, 23 Dec 2024 12:51:25 +0200 Subject: [PATCH] add support of discovery-protocol --- cmd/platform/main.go | 4 +- go.mod | 17 ++- go.sum | 46 +++---- internal/api/agent.go | 38 ++++-- internal/api/main_test.go | 3 +- internal/api/server.go | 4 +- internal/core/ports/discovery_service.go | 52 ++++++++ internal/core/services/discovery.go | 157 +++++++++++++++++++++++ 8 files changed, 280 insertions(+), 41 deletions(-) create mode 100644 internal/core/ports/discovery_service.go create mode 100644 internal/core/services/discovery.go diff --git a/cmd/platform/main.go b/cmd/platform/main.go index e8a058b4f..be39f1476 100644 --- a/cmd/platform/main.go +++ b/cmd/platform/main.go @@ -123,6 +123,7 @@ func main() { map[iden3comm.ProtocolMessage][]string{ iden3commProtocol.CredentialFetchRequestMessageType: {string(packers.MediaTypeZKPMessage)}, iden3commProtocol.RevocationStatusRequestMessageType: {"*"}, + iden3commProtocol.DiscoverFeatureQueriesMessageType: {"*"}, }, *cfg.MediaTypeManager.Enabled, ) @@ -155,6 +156,7 @@ func main() { displayMethodService := services.NewDisplayMethod(repositories.NewDisplayMethod(*storage)) keyService := services.NewKey(keyStore, claimsService, keyRepository) transactionService, err := gateways.NewTransaction(*networkResolver) + discoveryService := services.NewDiscovery(mediaTypeManager, packageManager) if err != nil { log.Error(ctx, "error creating transaction service", "err", err) return @@ -196,7 +198,7 @@ func main() { api.HandlerWithOptions( api.NewStrictHandlerWithOptions( - api.NewServer(cfg, identityService, accountService, connectionsService, claimsService, qrService, publisher, packageManager, *networkResolver, serverHealth, schemaService, linkService, displayMethodService, keyService), + api.NewServer(cfg, identityService, accountService, connectionsService, claimsService, qrService, publisher, packageManager, *networkResolver, serverHealth, schemaService, linkService, displayMethodService, keyService, discoveryService), middlewares(ctx, cfg.HTTPBasicAuth), api.StrictHTTPServerOptions{ RequestErrorHandlerFunc: errors.RequestErrorHandlerFunc, diff --git a/go.mod b/go.mod index 7f03497d4..1d5b088cb 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,8 @@ module github.com/polygonid/sh-id-platform -go 1.22.1 +go 1.22.6 + +toolchain go1.23.3 require ( github.com/alicebob/miniredis/v2 v2.33.0 @@ -10,7 +12,7 @@ require ( github.com/aws/aws-sdk-go-v2/service/kms v1.35.5 github.com/aws/aws-sdk-go-v2/service/secretsmanager v1.34.2 github.com/caarlos0/env/v11 v11.2.2 - github.com/ethereum/go-ethereum v1.14.8 + github.com/ethereum/go-ethereum v1.14.11 github.com/getkin/kin-openapi v0.127.0 github.com/go-chi/chi/v5 v5.1.0 github.com/go-chi/cors v1.2.1 @@ -24,7 +26,7 @@ require ( github.com/iden3/contracts-abi/onchain-credential-status-resolver/go/abi v0.0.0-20230911113809-c58b7e7a69b0 github.com/iden3/contracts-abi/state/go/abi v1.0.2-0.20230911112726-4533c5701af1 github.com/iden3/go-circuits/v2 v2.4.0 - github.com/iden3/go-iden3-auth/v2 v2.5.0 + github.com/iden3/go-iden3-auth/v2 v2.6.0 github.com/iden3/go-iden3-core/v2 v2.3.1 github.com/iden3/go-iden3-crypto v0.0.17 github.com/iden3/go-jwz/v2 v2.2.0 @@ -36,7 +38,7 @@ require ( github.com/iden3/go-rapidsnark/witness/wazero v0.0.0-20240914111027-9588ce2d7e1b github.com/iden3/go-schema-processor v1.3.1 github.com/iden3/go-schema-processor/v2 v2.6.1 - github.com/iden3/iden3comm/v2 v2.6.0 + github.com/iden3/iden3comm/v2 v2.9.2-0.20241220155923-303258bd747f github.com/iden3/merkletree-proof v0.3.0 github.com/ipfs/go-ipfs-api v0.7.0 github.com/jackc/pgconn v1.14.3 @@ -119,7 +121,7 @@ require ( github.com/cockroachdb/errors v1.11.3 // indirect github.com/cockroachdb/fifo v0.0.0-20240606204812-0bbfbd93a7ce // indirect github.com/cockroachdb/logtags v0.0.0-20230118201751-21c54148d20b // indirect - github.com/cockroachdb/pebble v1.1.1 // indirect + github.com/cockroachdb/pebble v1.1.2 // indirect github.com/cockroachdb/redact v1.1.5 // indirect github.com/consensys/bavard v0.1.13 // indirect github.com/consensys/gnark-crypto v0.12.1 // indirect @@ -136,7 +138,7 @@ require ( github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect github.com/dustinxie/ecc v0.0.0-20210511000915-959544187564 // indirect github.com/ethereum/c-kzg-4844 v1.0.1 // indirect - github.com/ethereum/go-verkle v0.1.1-0.20240306133620-7d920df305f0 // indirect + github.com/ethereum/go-verkle v0.1.1-0.20240829091221-dffa7562dbe9 // indirect github.com/ettle/strcase v0.2.0 // indirect github.com/fatih/color v1.17.0 // indirect github.com/fatih/structtag v1.2.0 // indirect @@ -191,6 +193,7 @@ require ( github.com/holiman/bloomfilter/v2 v2.0.3 // indirect github.com/holiman/uint256 v1.3.1 // indirect github.com/iden3/contracts-abi/rhs-storage/go/abi v0.0.0-20231006141557-7d13ef7e3c48 // indirect + github.com/iden3/driver-did-iden3 v0.0.5 // indirect github.com/iden3/go-iden3-core v1.0.2 // indirect github.com/iden3/go-rapidsnark/verifier v0.0.5 // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect @@ -308,7 +311,7 @@ require ( github.com/stbenjam/no-sprintf-host-port v0.1.1 // indirect github.com/stretchr/objx v0.5.2 // indirect github.com/subosito/gotenv v1.6.0 // indirect - github.com/supranational/blst v0.3.11 // indirect + github.com/supranational/blst v0.3.13 // indirect github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7 // indirect github.com/tdakkota/asciicheck v0.2.0 // indirect github.com/tetafro/godot v1.4.16 // indirect diff --git a/go.sum b/go.sum index c52b94436..572ee5fe8 100644 --- a/go.sum +++ b/go.sum @@ -108,14 +108,14 @@ github.com/breml/bidichk v0.2.7 h1:dAkKQPLl/Qrk7hnP6P+E0xOodrq8Us7+U0o4UBOAlQY= github.com/breml/bidichk v0.2.7/go.mod h1:YodjipAGI9fGcYM7II6wFvGhdMYsC5pHDlGzqvEW3tQ= github.com/breml/errchkjson v0.3.6 h1:VLhVkqSBH96AvXEyclMR37rZslRrY2kcyq+31HCsVrA= github.com/breml/errchkjson v0.3.6/go.mod h1:jhSDoFheAF2RSDOlCfhHO9KqhZgAYLyvHe7bRCX8f/U= -github.com/btcsuite/btcd v0.23.3 h1:4KH/JKy9WiCd+iUS9Mu0Zp7Dnj17TGdKrg9xc/FGj24= -github.com/btcsuite/btcd v0.23.3/go.mod h1:0QJIIN1wwIXF/3G/m87gIwGniDMDQqjVn4SZgnFpsYY= +github.com/btcsuite/btcd v0.24.2 h1:aLmxPguqxza+4ag8R1I2nnJjSu2iFn/kqtHTIImswcY= +github.com/btcsuite/btcd v0.24.2/go.mod h1:5C8ChTkl5ejr3WHj8tkQSCmydiMEPB0ZhQhehpq7Dgg= github.com/btcsuite/btcd/btcec/v2 v2.3.4 h1:3EJjcN70HCu/mwqlUsGK8GcNVyLVxFDlWurTXGPFfiQ= github.com/btcsuite/btcd/btcec/v2 v2.3.4/go.mod h1:zYzJ8etWJQIv1Ogk7OzpWjowwOdXY1W/17j2MW85J04= -github.com/btcsuite/btcd/btcutil v1.1.3 h1:xfbtw8lwpp0G6NwSHb+UE67ryTFHJAiNuipusjXSohQ= -github.com/btcsuite/btcd/btcutil v1.1.3/go.mod h1:UR7dsSJzJUfMmFiiLlIrMq1lS9jh9EdCV7FStZSnpi0= -github.com/btcsuite/btcd/chaincfg/chainhash v1.0.1 h1:q0rUy8C/TYNBQS1+CGKw68tLOFYSNEs0TFnxxnS9+4U= -github.com/btcsuite/btcd/chaincfg/chainhash v1.0.1/go.mod h1:7SFka0XMvUgj3hfZtydOrQY2mwhPclbT2snogU7SQQc= +github.com/btcsuite/btcd/btcutil v1.1.6 h1:zFL2+c3Lb9gEgqKNzowKUPQNb8jV7v5Oaodi/AYFd6c= +github.com/btcsuite/btcd/btcutil v1.1.6/go.mod h1:9dFymx8HpuLqBnsPELrImQeTQfKBQqzqGbbV3jK55aE= +github.com/btcsuite/btcd/chaincfg/chainhash v1.1.0 h1:59Kx4K6lzOW5w6nFlA0v5+lk/6sjybR934QNHSJZPTQ= +github.com/btcsuite/btcd/chaincfg/chainhash v1.1.0/go.mod h1:7SFka0XMvUgj3hfZtydOrQY2mwhPclbT2snogU7SQQc= github.com/butuzov/ireturn v0.3.0 h1:hTjMqWw3y5JC3kpnC5vXmFJAWI/m31jaCYQqzkS6PL0= github.com/butuzov/ireturn v0.3.0/go.mod h1:A09nIiwiqzN/IoVo9ogpa0Hzi9fex1kd9PSD6edP5ZA= github.com/butuzov/mirror v1.2.0 h1:9YVK1qIjNspaqWutSv8gsge2e/Xpq1eqEkslEUHy5cs= @@ -128,8 +128,8 @@ github.com/ccojocar/zxcvbn-go v1.0.2 h1:na/czXU8RrhXO4EZme6eQJLR4PzcGsahsBOAwU6I github.com/ccojocar/zxcvbn-go v1.0.2/go.mod h1:g1qkXtUSvHP8lhHp5GrSmTz6uWALGRMQdw6Qnz/hi60= github.com/cenkalti/backoff/v3 v3.2.2 h1:cfUAAO3yvKMYKPrvhDuHSwQnhZNk/RMHKdZqKTxfm6M= github.com/cenkalti/backoff/v3 v3.2.2/go.mod h1:cIeZDE3IrqwwJl6VUwCN6trj1oXrTS4rc0ij+ULvLYs= -github.com/cespare/cp v0.1.0 h1:SE+dxFebS7Iik5LK0tsi1k9ZCxEaFX4AjQmoyA+1dJk= -github.com/cespare/cp v0.1.0/go.mod h1:SOGHArjBr4JWaSDEVpWpo/hNg6RoKrls6Oh40hiwW+s= +github.com/cespare/cp v1.1.1 h1:nCb6ZLdB7NRaqsm91JtQTAme2SKJzXVsdPIPkyJr1MU= +github.com/cespare/cp v1.1.1/go.mod h1:SOGHArjBr4JWaSDEVpWpo/hNg6RoKrls6Oh40hiwW+s= github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= @@ -153,8 +153,8 @@ github.com/cockroachdb/fifo v0.0.0-20240606204812-0bbfbd93a7ce h1:giXvy4KSc/6g/e github.com/cockroachdb/fifo v0.0.0-20240606204812-0bbfbd93a7ce/go.mod h1:9/y3cnZ5GKakj/H4y9r9GTjCvAFta7KLgSHPJJYc52M= github.com/cockroachdb/logtags v0.0.0-20230118201751-21c54148d20b h1:r6VH0faHjZeQy818SGhaone5OnYfxFR/+AzdY3sf5aE= github.com/cockroachdb/logtags v0.0.0-20230118201751-21c54148d20b/go.mod h1:Vz9DsVWQQhf3vs21MhPMZpMGSht7O/2vFW2xusFUVOs= -github.com/cockroachdb/pebble v1.1.1 h1:XnKU22oiCLy2Xn8vp1re67cXg4SAasg/WDt1NtcRFaw= -github.com/cockroachdb/pebble v1.1.1/go.mod h1:4exszw1r40423ZsmkG/09AFEG83I0uDgfujJdbL6kYU= +github.com/cockroachdb/pebble v1.1.2 h1:CUh2IPtR4swHlEj48Rhfzw6l/d0qA31fItcIszQVIsA= +github.com/cockroachdb/pebble v1.1.2/go.mod h1:4exszw1r40423ZsmkG/09AFEG83I0uDgfujJdbL6kYU= github.com/cockroachdb/redact v1.1.5 h1:u1PMllDkdFfPWaNGMyLD1+so+aq3uUItthCFqzwPJ30= github.com/cockroachdb/redact v1.1.5/go.mod h1:BVNblN9mBWFyMyqK1k3AAiSxhvhfK2oOZZ2lK+dpvRg= github.com/cockroachdb/tokenbucket v0.0.0-20230807174530-cc333fc44b06 h1:zuQyyAKVxetITBuuhv3BI9cMrmStnpT18zmgmTxunpo= @@ -201,10 +201,10 @@ github.com/dustinxie/ecc v0.0.0-20210511000915-959544187564 h1:I6KUy4CI6hHjqnyJL github.com/dustinxie/ecc v0.0.0-20210511000915-959544187564/go.mod h1:yekO+3ZShy19S+bsmnERmznGy9Rfg6dWWWpiGJjNAz8= github.com/ethereum/c-kzg-4844 v1.0.1 h1:pGixCbGizcVKSwoV70ge48+PrbB+iSKs2rjgfE4yJmQ= github.com/ethereum/c-kzg-4844 v1.0.1/go.mod h1:VewdlzQmpT5QSrVhbBuGoCdFJkpaJlO1aQputP83wc0= -github.com/ethereum/go-ethereum v1.14.8 h1:NgOWvXS+lauK+zFukEvi85UmmsS/OkV0N23UZ1VTIig= -github.com/ethereum/go-ethereum v1.14.8/go.mod h1:TJhyuDq0JDppAkFXgqjwpdlQApywnu/m10kFPxh8vvs= -github.com/ethereum/go-verkle v0.1.1-0.20240306133620-7d920df305f0 h1:KrE8I4reeVvf7C1tm8elRjj4BdscTYzz/WAbYyf/JI4= -github.com/ethereum/go-verkle v0.1.1-0.20240306133620-7d920df305f0/go.mod h1:D9AJLVXSyZQXJQVk8oh1EwjISE+sJTn2duYIZC0dy3w= +github.com/ethereum/go-ethereum v1.14.11 h1:8nFDCUUE67rPc6AKxFj7JKaOa2W/W1Rse3oS6LvvxEY= +github.com/ethereum/go-ethereum v1.14.11/go.mod h1:+l/fr42Mma+xBnhefL/+z11/hcmJ2egl+ScIVPjhc7E= +github.com/ethereum/go-verkle v0.1.1-0.20240829091221-dffa7562dbe9 h1:8NfxH2iXvJ60YRB8ChToFTUzl8awsc3cJ8CbLjGIl/A= +github.com/ethereum/go-verkle v0.1.1-0.20240829091221-dffa7562dbe9/go.mod h1:M3b90YRnzqKyyzBEWJGqj8Qff4IDeXnzFw0P9bFw3uk= github.com/ettle/strcase v0.2.0 h1:fGNiVF21fHXpX1niBgk0aROov1LagYsOwV/xqKDKR/Q= github.com/ettle/strcase v0.2.0/go.mod h1:DajmHElDSaX76ITe3/VHVyMin4LWSJN5Z909Wp+ED1A= github.com/fatih/color v1.17.0 h1:GlRw1BRJxkpqUCBKzKOw098ed57fEsKeNjpTe3cSjK4= @@ -221,8 +221,8 @@ github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nos github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM= github.com/fzipp/gocyclo v0.6.0 h1:lsblElZG7d3ALtGMx9fmxeTKZaLLpU8mET09yN4BBLo= github.com/fzipp/gocyclo v0.6.0/go.mod h1:rXPyn8fnlpa0R2csP/31uerbiVBugk5whMdlyaLkLoA= -github.com/gballet/go-libpcsclite v0.0.0-20190607065134-2772fd86a8ff h1:tY80oXqGNY4FhTFhk+o9oFHGINQ/+vhlm8HFzi6znCI= -github.com/gballet/go-libpcsclite v0.0.0-20190607065134-2772fd86a8ff/go.mod h1:x7DCsMOv1taUwEWCzT4cmDeAkigA5/QCwUodaVOe8Ww= +github.com/gballet/go-libpcsclite v0.0.0-20191108122812-4678299bea08 h1:f6D9Hr8xV8uYKlyuj8XIruxlh9WjVjdh1gIicAS7ays= +github.com/gballet/go-libpcsclite v0.0.0-20191108122812-4678299bea08/go.mod h1:x7DCsMOv1taUwEWCzT4cmDeAkigA5/QCwUodaVOe8Ww= github.com/getkin/kin-openapi v0.127.0 h1:Mghqi3Dhryf3F8vR370nN67pAERW+3a95vomb3MAREY= github.com/getkin/kin-openapi v0.127.0/go.mod h1:OZrfXzUfGrNbsKj+xmFBx6E5c6yH3At/tAKSc2UszXM= github.com/getsentry/sentry-go v0.27.0 h1:Pv98CIbtB3LkMWmXi4Joa5OOcwbmnX88sF5qbK3r3Ps= @@ -417,10 +417,12 @@ github.com/iden3/contracts-abi/rhs-storage/go/abi v0.0.0-20231006141557-7d13ef7e github.com/iden3/contracts-abi/rhs-storage/go/abi v0.0.0-20231006141557-7d13ef7e3c48/go.mod h1:kJmVPMk4HfWyl2kcta34aad/K4TAfCwB29xX9PsR7LQ= github.com/iden3/contracts-abi/state/go/abi v1.0.2-0.20230911112726-4533c5701af1 h1:67302Oni3SpT2sbz7HXXEOR3zK3/usEC69W9ULILR1o= github.com/iden3/contracts-abi/state/go/abi v1.0.2-0.20230911112726-4533c5701af1/go.mod h1:TxgIrXCvxms3sbOdsy8kTvffUCIpEEifNy0fSXdkU4w= +github.com/iden3/driver-did-iden3 v0.0.5 h1:N3B9S3CBZHqWIZWspAiq2JFaLyq0eLZf26sN3ZEMAhQ= +github.com/iden3/driver-did-iden3 v0.0.5/go.mod h1:TcEG6fkExW6hgafjrU4ObOQ/HZqIRPQoL3TMU+URbS0= github.com/iden3/go-circuits/v2 v2.4.0 h1:m+7uYtrvJKuc+gVhbXDXl1BJQyK7sWdW7OWttM3R/8I= github.com/iden3/go-circuits/v2 v2.4.0/go.mod h1:k0uYx/ZdZPiDEIy7kI3MAixnREKcc7NdCKDRw8Q+iFA= -github.com/iden3/go-iden3-auth/v2 v2.5.0 h1:vvleEbb9WvZ5dH9FSLnE9pYCsdKh+jgIBLfr1tgP+7o= -github.com/iden3/go-iden3-auth/v2 v2.5.0/go.mod h1:z+7+loGUSX2loVVyyipwySvxAizrecz8sbDqdGwqKiI= +github.com/iden3/go-iden3-auth/v2 v2.6.0 h1:R+QD/H1zhtO9+uvZ73Z9CVU7Omx8xf8ckgv4Syv4IsI= +github.com/iden3/go-iden3-auth/v2 v2.6.0/go.mod h1:s6t4ierMRafmJPxHSfwDW3Mh5+ceNbUrtbdP1EVoqfI= github.com/iden3/go-iden3-core v1.0.2 h1:HwNDFeqcUv4ybZj5tH+58JKWKarn/qqBpNCqTLxGP0Y= github.com/iden3/go-iden3-core v1.0.2/go.mod h1:X4PjlJG8OsEQEsSbzzYqqAk2olYGZ2nuGqiUPyEYjOo= github.com/iden3/go-iden3-core/v2 v2.3.1 h1:ytQqiclnVAIWyRKR2LF31hfz4DGRBD6nMjiPILXGSKk= @@ -447,8 +449,8 @@ github.com/iden3/go-schema-processor v1.3.1 h1:LJfFInfYGMOp0bTKKC17R8q4XI+VtqhFL github.com/iden3/go-schema-processor v1.3.1/go.mod h1:NwJ1nuGdRlCFaN1/V6mS0AOAdvpLcGf4KKq0mluLG7U= github.com/iden3/go-schema-processor/v2 v2.6.1 h1:KD33sTAu2xAyG94SmrUAMxI+MREFTmSsavYy70gkTW4= github.com/iden3/go-schema-processor/v2 v2.6.1/go.mod h1:MYrkk55aEx3ZBKz0d/0rfxa7lBH4x9TfKGI1T/WBlpw= -github.com/iden3/iden3comm/v2 v2.6.0 h1:6cu0N2b9oluJGDMvM2C0r2IH1GtrMUmmHOcJTn3O0mQ= -github.com/iden3/iden3comm/v2 v2.6.0/go.mod h1:ZRnfFg4geX336Bp9+29ZQdSqchbsBoVcd7ARn6JXH5Y= +github.com/iden3/iden3comm/v2 v2.9.2-0.20241220155923-303258bd747f h1:ixUpMi3r+OcEsx9qdmXJ/3RtbV5EM7w7z1ahWfEveLY= +github.com/iden3/iden3comm/v2 v2.9.2-0.20241220155923-303258bd747f/go.mod h1:l8t+BkbHU3ri7kiExZYc4CTY6erkzkcBbYXw416nwhA= github.com/iden3/merkletree-proof v0.3.0 h1:NVlvtUBEgn4Etxxn+RsOmXP/qlI+85BdN8oUDTf3mxI= github.com/iden3/merkletree-proof v0.3.0/go.mod h1:+E2sBxMqhcn/fcu0LDGjmk3us+Vr+fxQUiZMxdpbgUE= github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= @@ -857,8 +859,8 @@ github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsT github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8= github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU= -github.com/supranational/blst v0.3.11 h1:LyU6FolezeWAhvQk0k6O/d49jqgO52MSDDfYgbeoEm4= -github.com/supranational/blst v0.3.11/go.mod h1:jZJtfjgudtNl4en1tzwPIV3KjUnQUvG3/j+w+fVonLw= +github.com/supranational/blst v0.3.13 h1:AYeSxdOMacwu7FBmpfloBz5pbFXDmJL33RuwnKtmTjk= +github.com/supranational/blst v0.3.13/go.mod h1:jZJtfjgudtNl4en1tzwPIV3KjUnQUvG3/j+w+fVonLw= github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7 h1:epCh84lMvA70Z7CTTCmYQn2CKbY8j86K7/FAIr141uY= github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7/go.mod h1:q4W45IWZaF22tdD+VEXcAWRA037jwmWEB5VWYORlTpc= github.com/tdakkota/asciicheck v0.2.0 h1:o8jvnUANo0qXtnslk2d3nMKTFNlOnJjRrNcj0j9qkHM= diff --git a/internal/api/agent.go b/internal/api/agent.go index 2c8684449..d50faf344 100644 --- a/internal/api/agent.go +++ b/internal/api/agent.go @@ -3,6 +3,8 @@ package api import ( "context" + "github.com/iden3/iden3comm/v2/protocol" + "github.com/polygonid/sh-id-platform/internal/core/domain" "github.com/polygonid/sh-id-platform/internal/core/ports" "github.com/polygonid/sh-id-platform/internal/log" ) @@ -20,16 +22,34 @@ func (s *Server) Agent(ctx context.Context, request AgentRequestObject) (AgentRe return Agent400JSONResponse{N400JSONResponse{"cannot proceed with the given request"}}, nil } - req, err := ports.NewAgentRequest(basicMessage) - if err != nil { - log.Error(ctx, "agent parsing request", "err", err) - return Agent400JSONResponse{N400JSONResponse{err.Error()}}, nil - } + var agent *domain.Agent - agent, err := s.claimService.Agent(ctx, req, mediatype) - if err != nil { - log.Error(ctx, "agent error", "err", err) - return Agent400JSONResponse{N400JSONResponse{err.Error()}}, nil + if basicMessage.Type == protocol.DiscoverFeatureQueriesMessageType { + req, err := ports.NewDiscoveryAgentRequest(basicMessage) + if err != nil { + log.Error(ctx, "agent parsing request", "err", err) + return Agent400JSONResponse{N400JSONResponse{err.Error()}}, nil + } + + agent, err = s.discoveryService.Agent(ctx, req) + if err != nil { + log.Error(ctx, "agent error", "err", err) + return Agent400JSONResponse{N400JSONResponse{err.Error()}}, nil + } + + } else { + + req, err := ports.NewAgentRequest(basicMessage) + if err != nil { + log.Error(ctx, "agent parsing request", "err", err) + return Agent400JSONResponse{N400JSONResponse{err.Error()}}, nil + } + + agent, err = s.claimService.Agent(ctx, req, mediatype) + if err != nil { + log.Error(ctx, "agent error", "err", err) + return Agent400JSONResponse{N400JSONResponse{err.Error()}}, nil + } } return Agent200JSONResponse{ diff --git a/internal/api/main_test.go b/internal/api/main_test.go index 04917db4d..032c6e670 100644 --- a/internal/api/main_test.go +++ b/internal/api/main_test.go @@ -306,7 +306,8 @@ func newTestServer(t *testing.T, st *db.Storage) *testServer { linkService := services.NewLinkService(storage, claimsService, qrService, repos.claims, repos.links, repos.schemas, schemaLoader, repos.sessions, pubSub, identityService, *networkResolver, cfg.UniversalLinks) displayMethodService := services.NewDisplayMethod(repos.displayMethod) keyService := services.NewKey(keyStore, claimsService, repos.keyRepository) - server := NewServer(&cfg, identityService, accountService, connectionService, claimsService, qrService, NewPublisherMock(), NewPackageManagerMock(), *networkResolver, nil, schemaService, linkService, displayMethodService, keyService) + discoveryService := services.NewDiscovery(mediaTypeManager, NewPackageManagerMock()) + server := NewServer(&cfg, identityService, accountService, connectionService, claimsService, qrService, NewPublisherMock(), NewPackageManagerMock(), *networkResolver, nil, schemaService, linkService, displayMethodService, keyService, discoveryService) return &testServer{ Server: server, diff --git a/internal/api/server.go b/internal/api/server.go index bd4617399..8a10bf148 100644 --- a/internal/api/server.go +++ b/internal/api/server.go @@ -31,10 +31,11 @@ type Server struct { schemaService ports.SchemaService displayMethodService ports.DisplayMethodService keyService ports.KeyService + discoveryService ports.DiscoveryService } // NewServer is a Server constructor -func NewServer(cfg *config.Configuration, identityService ports.IdentityService, accountService ports.AccountService, connectionsService ports.ConnectionService, claimsService ports.ClaimService, qrService ports.QrStoreService, publisherGateway ports.Publisher, packageManager *iden3comm.PackageManager, networkResolver network.Resolver, health *health.Status, schemaService ports.SchemaService, linkService ports.LinkService, displayMethodService ports.DisplayMethodService, keyService ports.KeyService) *Server { +func NewServer(cfg *config.Configuration, identityService ports.IdentityService, accountService ports.AccountService, connectionsService ports.ConnectionService, claimsService ports.ClaimService, qrService ports.QrStoreService, publisherGateway ports.Publisher, packageManager *iden3comm.PackageManager, networkResolver network.Resolver, health *health.Status, schemaService ports.SchemaService, linkService ports.LinkService, displayMethodService ports.DisplayMethodService, keyService ports.KeyService, discoveryService ports.DiscoveryService) *Server { return &Server{ cfg: cfg, accountService: accountService, @@ -50,6 +51,7 @@ func NewServer(cfg *config.Configuration, identityService ports.IdentityService, schemaService: schemaService, displayMethodService: displayMethodService, keyService: keyService, + discoveryService: discoveryService, } } diff --git a/internal/core/ports/discovery_service.go b/internal/core/ports/discovery_service.go new file mode 100644 index 000000000..ddde9d1b6 --- /dev/null +++ b/internal/core/ports/discovery_service.go @@ -0,0 +1,52 @@ +package ports + +import ( + "context" + "fmt" + + "github.com/iden3/go-iden3-core/v2/w3c" + comm "github.com/iden3/iden3comm/v2" + "github.com/iden3/iden3comm/v2/protocol" + "github.com/polygonid/sh-id-platform/internal/core/domain" +) + +// DiscoveryService is the interface implemented by the discovery service +type DiscoveryService interface { + Agent(ctx context.Context, req *AgentRequest) (*domain.Agent, error) +} + +// NewDiscoveryAgentRequest validates the inputs and returns a new AgentRequest +func NewDiscoveryAgentRequest(basicMessage *comm.BasicMessage) (*AgentRequest, error) { + var toDID, fromDID *w3c.DID + var err error + if basicMessage.To != "" { + toDID, err = w3c.ParseDID(basicMessage.To) + if err != nil { + return nil, err + } + } + + if basicMessage.From != "" { + fromDID, err = w3c.ParseDID(basicMessage.From) + if err != nil { + return nil, err + } + } + + if basicMessage.ID == "" { + return nil, fmt.Errorf("'id' field cannot be empty") + } + + if basicMessage.Type != protocol.DiscoverFeatureQueriesMessageType { + return nil, fmt.Errorf("invalid type") + } + + return &AgentRequest{ + Body: basicMessage.Body, + UserDID: fromDID, + IssuerDID: toDID, + ThreadID: basicMessage.ThreadID, + Typ: basicMessage.Typ, + Type: basicMessage.Type, + }, nil +} diff --git a/internal/core/services/discovery.go b/internal/core/services/discovery.go new file mode 100644 index 000000000..e58fd9fa2 --- /dev/null +++ b/internal/core/services/discovery.go @@ -0,0 +1,157 @@ +package services + +import ( + "context" + "encoding/json" + "fmt" + "regexp" + "strings" + + "github.com/google/uuid" + "github.com/iden3/iden3comm/v2" + "github.com/iden3/iden3comm/v2/packers" + "github.com/iden3/iden3comm/v2/protocol" + "github.com/polygonid/sh-id-platform/internal/core/domain" + "github.com/polygonid/sh-id-platform/internal/core/ports" + "github.com/polygonid/sh-id-platform/internal/log" +) + +type discovery struct { + mediatypeManager *MediaTypeManager + packerManager *iden3comm.PackageManager +} + +// NewDiscovery is a constructor for the discovery service +func NewDiscovery(mediatypeManager *MediaTypeManager, packerManager *iden3comm.PackageManager) *discovery { + d := &discovery{ + mediatypeManager: mediatypeManager, + packerManager: packerManager, + } + return d +} + +func (c *discovery) Agent(ctx context.Context, req *ports.AgentRequest) (*domain.Agent, error) { + if !c.mediatypeManager.AllowMediaType(req.Type, req.Typ) { + err := fmt.Errorf("unsupported media type '%s' for message type '%s'", req.Typ, req.Type) + log.Error(ctx, "agent: unsupported media type", "err", err) + return nil, err + } + + queries := &protocol.DiscoverFeatureQueriesMessageBody{} + err := json.Unmarshal(req.Body, queries) + if err != nil { + log.Error(ctx, "unmarshalling agent body", "err", err) + return nil, fmt.Errorf("invalid discover feature queries request body: %w", err) + } + + disclosures := []protocol.DiscoverFeatureDisclosure{} + for _, query := range queries.Queries { + var disclosuresToAppend []protocol.DiscoverFeatureDisclosure + switch query.FeatureType { + case protocol.DiscoveryProtocolFeatureTypeAccept: + disclosuresToAppend, err = c.handleAccept(ctx) + if err != nil { + return nil, err + } + case protocol.DiscoveryProtocolFeatureTypeGoalCode: + disclosuresToAppend = c.handleGoalCode(ctx) + case protocol.DiscoveryProtocolFeatureTypeProtocol: + disclosuresToAppend = c.handleProtocol(ctx) + case protocol.DiscoveryProtocolFeatureTypeHeader: + disclosuresToAppend = c.handleHeader(ctx) + } + disclosuresToAppend = c.handleMatch(ctx, disclosuresToAppend, query.Match) + disclosures = append(disclosures, disclosuresToAppend...) + } + + var from, to string + if req.IssuerDID != nil { + from = req.IssuerDID.String() + } + if req.UserDID != nil { + to = req.UserDID.String() + } + + return &domain.Agent{ + ID: uuid.NewString(), + Typ: packers.MediaTypePlainMessage, + Type: protocol.DiscoverFeatureDiscloseMessageType, + ThreadID: req.ThreadID, + Body: protocol.DiscoverFeatureDiscloseMessageBody{ + Disclosures: disclosures, + }, + From: from, + To: to, + }, nil +} + +func (d *discovery) handleAccept(_ context.Context) ([]protocol.DiscoverFeatureDisclosure, error) { + disclosures := []protocol.DiscoverFeatureDisclosure{} + + profiles := d.packerManager.GetSupportedProfiles() + for _, profile := range profiles { + disclosures = append(disclosures, protocol.DiscoverFeatureDisclosure{ + FeatureType: protocol.DiscoveryProtocolFeatureTypeAccept, + ID: profile, + }) + } + return disclosures, nil +} + +func (d *discovery) handleProtocol(_ context.Context) []protocol.DiscoverFeatureDisclosure { + disclosures := []protocol.DiscoverFeatureDisclosure{} + return disclosures +} + +func (d *discovery) handleGoalCode(_ context.Context) []protocol.DiscoverFeatureDisclosure { + disclosures := []protocol.DiscoverFeatureDisclosure{} + return disclosures +} + +func (d *discovery) handleHeader(_ context.Context) []protocol.DiscoverFeatureDisclosure { + headers := []string{ + "id", + "typ", + "type", + "thid", + "body", + "from", + "to", + "created_time", + "expires_time", + } + + disclosures := []protocol.DiscoverFeatureDisclosure{} + + for _, header := range headers { + disclosures = append(disclosures, protocol.DiscoverFeatureDisclosure{ + FeatureType: protocol.DiscoveryProtocolFeatureTypeHeader, + ID: header, + }) + } + + return disclosures +} + +func wildcardToRegExp(match string) *regexp.Regexp { + // Escape special regex characters and replace `*` with `.*` + regexPattern := regexp.QuoteMeta(match) + regexPattern = strings.ReplaceAll(regexPattern, "\\*", ".*") + regExp, _ := regexp.Compile("^" + regexPattern + "$") + return regExp +} + +func (d *discovery) handleMatch(_ context.Context, disclosures []protocol.DiscoverFeatureDisclosure, match string) []protocol.DiscoverFeatureDisclosure { + if match == "" || match == "*" { + return disclosures + } + + regExp := wildcardToRegExp(match) + var filtered []protocol.DiscoverFeatureDisclosure + for _, disclosure := range disclosures { + if regExp.MatchString(disclosure.ID) { + filtered = append(filtered, disclosure) + } + } + return filtered +}