diff --git a/android/build.gradle b/android/build.gradle index e19dda9e..2bc6268d 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -15,8 +15,8 @@ buildscript { } plugins { - id 'com.android.application' version '8.2.1' apply false - id 'com.android.library' version '8.2.1' apply false + id 'com.android.application' version '8.3.1' apply false + id 'com.android.library' version '8.3.1' apply false id 'org.jetbrains.kotlin.android' version '1.7.0' apply false } diff --git a/android/gradle/wrapper/gradle-wrapper.properties b/android/gradle/wrapper/gradle-wrapper.properties index a05958e4..93dedd5b 100644 --- a/android/gradle/wrapper/gradle-wrapper.properties +++ b/android/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ #Wed Jun 08 11:24:29 CEST 2022 distributionBase=GRADLE_USER_HOME -distributionUrl=https\://services.gradle.org/distributions/gradle-8.2-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-bin.zip distributionPath=wrapper/dists zipStorePath=wrapper/dists zipStoreBase=GRADLE_USER_HOME diff --git a/server/Makefile b/server/Makefile index bdebf450..06e04f72 100644 --- a/server/Makefile +++ b/server/Makefile @@ -26,13 +26,13 @@ build: tidy # Building executable @go build ./smc/smccli @go build ./blockchain/chaincli - @go build ./registration + @go build ./registry/registrycli install: tidy # Building executable @go install ./smc/smccli @go install ./blockchain/chaincli - @go install ./registration + @go install ./registry/registrycli coverage: tidy # Test and generate a coverage output usable by sonarcloud diff --git a/server/blockchain/calypso/controller/controller.go b/server/blockchain/calypso/controller/controller.go index e9bfeca6..d50559ac 100644 --- a/server/blockchain/calypso/controller/controller.go +++ b/server/blockchain/calypso/controller/controller.go @@ -44,6 +44,8 @@ func (m miniController) OnStart(_ cli.Flags, inj node.Injector) error { calypso.RegisterContract(exec, contract) + inj.Inject(contract) + return nil } diff --git a/server/blockchain/chaincli/chaincli.go b/server/blockchain/chaincli/chaincli.go index d55aa18c..2b209a77 100644 --- a/server/blockchain/chaincli/chaincli.go +++ b/server/blockchain/chaincli/chaincli.go @@ -10,11 +10,13 @@ import ( "go.dedis.ch/dela/cli/node" access "go.dedis.ch/dela/contracts/access/controller" cosipbft "go.dedis.ch/dela/core/ordering/cosipbft/controller" - db "go.dedis.ch/dela/core/store/kv/controller" + kv "go.dedis.ch/dela/core/store/kv/controller" pool "go.dedis.ch/dela/core/txn/pool/controller" signed "go.dedis.ch/dela/core/txn/signed/controller" - mino "go.dedis.ch/dela/mino/minogrpc/controller" + minogrpc "go.dedis.ch/dela/mino/minogrpc/controller" proxy "go.dedis.ch/dela/mino/proxy/http/controller" + "go.dedis.ch/hbt/server/blockchain/web" + purbkv "go.dedis.ch/purb-db/store/kv/controller" ) func main() { @@ -37,13 +39,15 @@ func runWithCfg(args []string, cfg config) error { builder := node.NewBuilderWithCfg( cfg.Channel, cfg.Writer, - db.NewController(), - mino.NewController(), + purbkv.NewController(), + proxy.NewController(), + web.NewController(), + minogrpc.NewController(), + kv.NewController(), cosipbft.NewController(), signed.NewManagerController(), pool.NewController(), access.NewController(), - proxy.NewController(), calypso.NewController(), ) diff --git a/server/blockchain/web/action.go b/server/blockchain/web/action.go index a2f0f11f..ad90cd30 100644 --- a/server/blockchain/web/action.go +++ b/server/blockchain/web/action.go @@ -6,27 +6,27 @@ import ( "net/http" "github.com/gorilla/mux" + "github.com/rs/zerolog/log" "go.dedis.ch/dela" "go.dedis.ch/dela/cli/node" + "go.dedis.ch/dela/core/execution" + "go.dedis.ch/dela/core/txn" + "go.dedis.ch/dela/core/txn/signed" + "go.dedis.ch/dela/crypto/bls" "go.dedis.ch/dela/mino/proxy" - "go.dedis.ch/kyber/v3/suites" + "go.dedis.ch/hbt/server/blockchain/calypso" + purbkv "go.dedis.ch/purb-db/store/kv" "golang.org/x/xerrors" ) -// suite is the Kyber suite for Pedersen. -var suite = suites.MustFind("Ed25519") - -const separator = ":" -const malformedEncoded = "malformed encoded: %s" - // RegisterAction is an action to register the HTTP handlers // // - implements node.ActionTemplate type RegisterAction struct{} // Execute implements node.ActionTemplate. It registers the handlers using the -// default proxy from the the injector. +// default proxy from the injector. func (a *RegisterAction) Execute(ctx node.Context) error { var p proxy.Proxy err := ctx.Injector.Resolve(&p) @@ -36,11 +36,10 @@ func (a *RegisterAction) Execute(ctx node.Context) error { router := mux.NewRouter() - s := &secretHandler{&ctx} + s := &secretHandler{ctx} router.HandleFunc("/secret", s.addSecret).Methods("POST") router.HandleFunc("/secret/list", s.listSecrets).Methods("GET") router.HandleFunc("/secret", s.getSecret).Methods("GET") - router.HandleFunc("/secret/reveal", s.revealSecret).Methods("POST") router.NotFoundHandler = http.HandlerFunc(notFoundHandler) router.MethodNotAllowedHandler = http.HandlerFunc(notAllowedHandler) @@ -54,49 +53,74 @@ func (a *RegisterAction) Execute(ctx node.Context) error { type DocID []byte -// a secretData is a struct to hold the secret data: the document ID and the -// encrypted key to access the document -type secretData struct { - docID DocID `json:"docid"` - encryptedKey string `json:"secret"` -} - type secretHandler struct { - ctx *node.Context + ctx node.Context } // addSecret adds a new secret in the blockchain func (s *secretHandler) addSecret(w http.ResponseWriter, r *http.Request) { - ctx := *(s.ctx) + err := r.ParseMultipartForm(32 << 20) + if err != nil { + log.Fatal().Err(err) + } - // Decode the request - var sec secretData - err := json.NewDecoder(r.Body).Decode(&sec) + secret := r.FormValue("secret") + id := r.FormValue("id") + dela.Logger.Info().Msgf("received doc ID=%v with secret=%v", id, secret) + + // get the calypso contract + var c calypso.Contract + err = s.ctx.Injector.Resolve(&c) if err != nil { - http.Error(w, err.Error(), http.StatusBadRequest) + dela.Logger.Error().Err(err).Msg("failed to resolve calypso contract") + http.Error(w, fmt.Sprintf("failed to resolve calypso contract: %v", err), + http.StatusInternalServerError) + return + } + + var db purbkv.DB + err = s.ctx.Injector.Resolve(&db) + if err != nil { + dela.Logger.Error().Err(err).Msg("failed to resolve database") + http.Error(w, fmt.Sprintf("failed to resolve database: %v", err), + http.StatusInternalServerError) return } // add the secret to the blockchain // the secret is added to the blockchain with the document ID as the key // and the encrypted key as the value - // TODO add it to the blockchain - dela.Logger.Info().Msgf("secret added to the blockchain: ID=%v secret=%v", sec.docID, - sec.encryptedKey) + + err = db.Update(func(txn purbkv.WritableTx) error { + b, err := txn.GetBucketOrCreate([]byte("bucket:secret")) + if err != nil { + return err + } + + err = c.Execute(b, makeStep(calypso.CmdArg, string(calypso.CmdCreateSecret), + calypso.SecretNameArg, id, calypso.SecretArg, secret)) + + return err + }) + + if err != nil { + dela.Logger.Error().Err(err).Msg("failed to add secret to the blockchain") + http.Error(w, fmt.Sprintf("failed to add secret to the blockchain: %v", err), + http.StatusInternalServerError) + return + } + + dela.Logger.Info().Msgf("secret added to the blockchain: ID=%v secret=%v", id, secret) } // listSecrets lists all secrets in the blockchain -func (s *secretHandler) listSecrets(w http.ResponseWriter, r *http.Request) { - ctx := *(s.ctx) - +func (s *secretHandler) listSecrets(_ http.ResponseWriter, _ *http.Request) { // list all secrets from the blockchain } // getSecret gets a secret from the blockchain func (s *secretHandler) getSecret(w http.ResponseWriter, r *http.Request) { - ctx := *(s.ctx) - // Decode the request var id DocID err := json.NewDecoder(r.Body).Decode(&id) @@ -160,3 +184,27 @@ func notAllowedHandler(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusMethodNotAllowed) fmt.Fprintln(w, string(buf)) } + +// ----------------------------------------------------------------------------- +// Utility functions +var nounce uint64 + +func makeStep(args ...string) execution.Step { + return execution.Step{Current: makeTx(args...)} +} + +func makeTx(args ...string) txn.Transaction { + signer := bls.NewSigner() + + options := []signed.TransactionOption{} + for i := 0; i < len(args)-1; i += 2 { + options = append(options, signed.WithArg(args[i], []byte(args[i+1]))) + } + + tx, err := signed.NewTransaction(nounce, signer.GetPublicKey(), options...) + if err != nil { + nounce++ + } + + return tx +} diff --git a/server/go.mod b/server/go.mod index 60c5e4a4..ee38992d 100644 --- a/server/go.mod +++ b/server/go.mod @@ -2,18 +2,16 @@ module go.dedis.ch/hbt/server go 1.21 -// replace go.dedis.ch/dela => ../../dela - require ( github.com/gorilla/mux v1.8.1 github.com/rs/zerolog v1.31.0 - github.com/spf13/viper v1.18.1 - github.com/steinfletcher/apitest v1.5.15 + github.com/spf13/viper v1.18.2 github.com/stretchr/testify v1.8.4 - go.dedis.ch/dela v0.0.0-20230814162536-4bcfa7981c82 - go.dedis.ch/kyber/v3 v3.1.0 - go.mongodb.org/mongo-driver v1.13.1 - golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 + go.dedis.ch/dela v0.0.0-20240330091609-33fd2a361d2d + go.dedis.ch/kyber/v3 v3.1.1-0.20231024084410-31ea167adbbb + go.dedis.ch/purb-db v0.0.1 + go.mongodb.org/mongo-driver v1.13.2 + golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028 ) require ( @@ -29,18 +27,17 @@ require ( github.com/klauspost/compress v1.17.0 // indirect github.com/magiconair/properties v1.8.7 // indirect github.com/mattn/go-colorable v0.1.13 // indirect - github.com/mattn/go-isatty v0.0.19 // indirect - github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect + github.com/mattn/go-isatty v0.0.20 // indirect github.com/mitchellh/mapstructure v1.5.0 // indirect github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe // indirect github.com/opentracing-contrib/go-grpc v0.0.0-20210225150812-73cb765af46e // indirect github.com/opentracing/opentracing-go v1.2.0 // indirect - github.com/pelletier/go-toml/v2 v2.1.0 // indirect + github.com/pelletier/go-toml/v2 v2.1.1 // indirect github.com/pkg/errors v0.9.1 // indirect github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect - github.com/prometheus/client_golang v1.17.0 // indirect + github.com/prometheus/client_golang v1.19.0 // indirect github.com/prometheus/client_model v0.5.0 // indirect - github.com/prometheus/common v0.44.0 // indirect + github.com/prometheus/common v0.48.0 // indirect github.com/prometheus/procfs v0.12.0 // indirect github.com/rs/xid v1.5.0 // indirect github.com/russross/blackfriday/v2 v2.1.0 // indirect @@ -50,6 +47,7 @@ require ( github.com/spf13/afero v1.11.0 // indirect github.com/spf13/cast v1.6.0 // indirect github.com/spf13/pflag v1.0.5 // indirect + github.com/stretchr/objx v0.5.2 // indirect github.com/subosito/gotenv v1.6.0 // indirect github.com/uber/jaeger-client-go v2.30.0+incompatible // indirect github.com/uber/jaeger-lib v2.4.1+incompatible // indirect @@ -60,15 +58,16 @@ require ( github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d // indirect go.dedis.ch/fixbuf v1.0.3 // indirect + go.dedis.ch/libpurb v0.0.0-20231108133532-c70e1b84b632 // indirect go.dedis.ch/protobuf v1.0.11 // indirect go.etcd.io/bbolt v1.3.7 // indirect go.uber.org/atomic v1.11.0 // indirect go.uber.org/multierr v1.9.0 // indirect - golang.org/x/crypto v0.16.0 // indirect - golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect - golang.org/x/net v0.19.0 // indirect + golang.org/x/crypto v0.21.0 // indirect + golang.org/x/exp v0.0.0-20240318143956-a85f2c67cd81 // indirect + golang.org/x/net v0.21.0 // indirect golang.org/x/sync v0.5.0 // indirect - golang.org/x/sys v0.15.0 // indirect + golang.org/x/sys v0.18.0 // indirect golang.org/x/text v0.14.0 // indirect google.golang.org/genproto/googleapis/rpc v0.0.0-20231120223509-83a465c0220f // indirect google.golang.org/grpc v1.59.0 // indirect diff --git a/server/go.sum b/server/go.sum index 4aff7d65..5951d0c2 100644 --- a/server/go.sum +++ b/server/go.sum @@ -33,8 +33,8 @@ github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEW github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= -github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= +github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY= github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ= github.com/grpc-ecosystem/grpc-opentracing v0.0.0-20180507213350-8e809c8a8645/go.mod h1:6iZfnjpejD4L/4DwD7NryNaJyCQdzwWwH2MWhCA90Kw= @@ -52,10 +52,9 @@ github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3v github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= -github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA= github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= -github.com/matttproud/golang_protobuf_extensions v1.0.4 h1:mmDVorXM7PCGKw94cs5zkfA9PSy5pEvNWRP0ET0TIVo= -github.com/matttproud/golang_protobuf_extensions v1.0.4/go.mod h1:BSXmuO+STAnVfrANrmjBb36TMTDstsz7MSK+HVaYKv4= +github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= +github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY= github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe h1:iruDEfMl2E6fbMZ9s0scYfZQ84/6SPL6zC8ACM2oIL0= @@ -65,19 +64,19 @@ github.com/opentracing-contrib/go-grpc v0.0.0-20210225150812-73cb765af46e/go.mod github.com/opentracing/opentracing-go v1.1.0/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o= github.com/opentracing/opentracing-go v1.2.0 h1:uEJPy/1a5RIPAJ0Ov+OIO8OxWu77jEv+1B0VhjKrZUs= github.com/opentracing/opentracing-go v1.2.0/go.mod h1:GxEUsuufX4nBwe+T+Wl9TAgYrxe9dPLANfrWvHYVTgc= -github.com/pelletier/go-toml/v2 v2.1.0 h1:FnwAJ4oYMvbT/34k9zzHuZNrhlz48GB3/s6at6/MHO4= -github.com/pelletier/go-toml/v2 v2.1.0/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc= +github.com/pelletier/go-toml/v2 v2.1.1 h1:LWAJwfNvjQZCFIDKWYQaM62NcYeYViCmWIwmOStowAI= +github.com/pelletier/go-toml/v2 v2.1.1/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U= github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/prometheus/client_golang v1.17.0 h1:rl2sfwZMtSthVU752MqfjQozy7blglC+1SOtjMAMh+Q= -github.com/prometheus/client_golang v1.17.0/go.mod h1:VeL+gMmOAxkS2IqfCq0ZmHSL+LjWfWDUmp1mBz9JgUY= +github.com/prometheus/client_golang v1.19.0 h1:ygXvpU1AoN1MhdzckN+PyD9QJOSD4x7kmXYlnfbA6JU= +github.com/prometheus/client_golang v1.19.0/go.mod h1:ZRM9uEAypZakd+q/x7+gmsvXdURP+DABIEIjnmDdp+k= github.com/prometheus/client_model v0.5.0 h1:VQw1hfvPvk3Uv6Qf29VrPF32JB6rtbgI6cYPYQjL0Qw= github.com/prometheus/client_model v0.5.0/go.mod h1:dTiFglRmd66nLR9Pv9f0mZi7B7fk5Pm3gvsjB5tr+kI= -github.com/prometheus/common v0.44.0 h1:+5BrQJwiBB9xsMygAB3TNvpQKOwlkc25LbISbrdOOfY= -github.com/prometheus/common v0.44.0/go.mod h1:ofAIvZbQ1e/nugmZGz4/qCb9Ap1VoSTIO7x0VV9VvuY= +github.com/prometheus/common v0.48.0 h1:QO8U2CdOzSn1BBsmXJXduaaW+dY/5QLjfB8svtSzKKE= +github.com/prometheus/common v0.48.0/go.mod h1:0/KsvlIEfPQCQ5I2iNSAWKPZziNCvRs5EC6ILDTlAPc= github.com/prometheus/procfs v0.12.0 h1:jluTpSng7V9hY0O2R9DzzJHYb2xULk9VTR1V1R/k6Bo= github.com/prometheus/procfs v0.12.0/go.mod h1:pcuDEFsWDnvcgNzo4EEweacyhjeA9Zk3cnaOZAZEfOo= github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ= @@ -100,14 +99,13 @@ github.com/spf13/cast v1.6.0 h1:GEiTHELF+vaR5dhz3VqZfFSzZjYbgeKDpBxQVS4GYJ0= github.com/spf13/cast v1.6.0/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo= github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= -github.com/spf13/viper v1.18.1 h1:rmuU42rScKWlhhJDyXZRKJQHXFX02chSVW1IvkPGiVM= -github.com/spf13/viper v1.18.1/go.mod h1:EKmWIqdnk5lOcmR72yw6hS+8OPYcwD0jteitLMVB+yk= -github.com/steinfletcher/apitest v1.5.15 h1:AAdTN0yMbf0VMH/PMt9uB2I7jljepO6i+5uhm1PjH3c= -github.com/steinfletcher/apitest v1.5.15/go.mod h1:mF+KnYaIkuHM0C4JgGzkIIOJAEjo+EA5tTjJ+bHXnQc= +github.com/spf13/viper v1.18.2 h1:LUXCnvUvSM6FXAsj6nnfc8Q2tp1dIgUfY9Kc8GsSOiQ= +github.com/spf13/viper v1.18.2/go.mod h1:EKmWIqdnk5lOcmR72yw6hS+8OPYcwD0jteitLMVB+yk= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= -github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY= +github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= @@ -133,22 +131,26 @@ github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsr github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d h1:splanxYIlg+5LfHAM6xpdFEAYOk8iySO56hMFq6uLyA= github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d/go.mod h1:rHwXgn7JulP+udvsHwJoVG1YGAP6VLg4y9I5dyZdqmA= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= -go.dedis.ch/dela v0.0.0-20230814162536-4bcfa7981c82 h1:nSPgGOk0izc9hSmUVGe8TT+rSLuePz+AH0VxtIywOBU= -go.dedis.ch/dela v0.0.0-20230814162536-4bcfa7981c82/go.mod h1:FkatZT04/Bc6cm3ShMMuDc83/zX2OTmqBl5NhuW6W4s= +go.dedis.ch/dela v0.0.0-20240330091609-33fd2a361d2d h1:XZpqRPToitJS/siNEg0Ubbx48m0oCxehCWBcQFjAb74= +go.dedis.ch/dela v0.0.0-20240330091609-33fd2a361d2d/go.mod h1:uFDET0WctO22NxqycVcgZhJM6lH9n1wFiGn2GyMx/+I= go.dedis.ch/fixbuf v1.0.3 h1:hGcV9Cd/znUxlusJ64eAlExS+5cJDIyTyEG+otu5wQs= go.dedis.ch/fixbuf v1.0.3/go.mod h1:yzJMt34Wa5xD37V5RTdmp38cz3QhMagdGoem9anUalw= go.dedis.ch/kyber/v3 v3.0.4/go.mod h1:OzvaEnPvKlyrWyp3kGXlFdp7ap1VC6RkZDTaPikqhsQ= go.dedis.ch/kyber/v3 v3.0.9/go.mod h1:rhNjUUg6ahf8HEg5HUvVBYoWY4boAafX8tYxX+PS+qg= -go.dedis.ch/kyber/v3 v3.1.0 h1:ghu+kiRgM5JyD9TJ0hTIxTLQlJBR/ehjWvWwYW3XsC0= -go.dedis.ch/kyber/v3 v3.1.0/go.mod h1:kXy7p3STAurkADD+/aZcsznZGKVHEqbtmdIzvPfrs1U= +go.dedis.ch/kyber/v3 v3.1.1-0.20231024084410-31ea167adbbb h1:G7GmitWgVMlkBaSzHJJo05bydYhLFCSPzKwCHTwf3NM= +go.dedis.ch/kyber/v3 v3.1.1-0.20231024084410-31ea167adbbb/go.mod h1:nL0a5f3E//lnOCKumtzMmb8qzykxOVsihqp8C1Eb5rc= +go.dedis.ch/libpurb v0.0.0-20231108133532-c70e1b84b632 h1:e3rOQfoyJm6ywIbuBcRwoKdWSUsPmcW9qdHO47dOmyc= +go.dedis.ch/libpurb v0.0.0-20231108133532-c70e1b84b632/go.mod h1:XCi40g75txGSLusqJOanJytpkxiQLJzRb9RRV9UN2p4= go.dedis.ch/protobuf v1.0.5/go.mod h1:eIV4wicvi6JK0q/QnfIEGeSFNG0ZeB24kzut5+HaRLo= go.dedis.ch/protobuf v1.0.7/go.mod h1:pv5ysfkDX/EawiPqcW3ikOxsL5t+BqnV6xHSmE79KI4= go.dedis.ch/protobuf v1.0.11 h1:FTYVIEzY/bfl37lu3pR4lIj+F9Vp1jE8oh91VmxKgLo= go.dedis.ch/protobuf v1.0.11/go.mod h1:97QR256dnkimeNdfmURz0wAMNVbd1VmLXhG1CrTYrJ4= +go.dedis.ch/purb-db v0.0.1 h1:JzZDdkd9t1wuW2X8jHXBMoakctzBkc5EW36VXev42+M= +go.dedis.ch/purb-db v0.0.1/go.mod h1:AFLiJofwHp8w9ZzG9I/Mpy7fKlsHKK13oAish6U0NuQ= go.etcd.io/bbolt v1.3.7 h1:j+zJOnnEjF/kyHlDDgGnVL/AIqIJPq8UoB2GSNfkUfQ= go.etcd.io/bbolt v1.3.7/go.mod h1:N9Mkw9X8x5fupy0IKsmuqVtoGDyxsaDlbk4Rd05IAQw= -go.mongodb.org/mongo-driver v1.13.1 h1:YIc7HTYsKndGK4RFzJ3covLz1byri52x0IoMB0Pt/vk= -go.mongodb.org/mongo-driver v1.13.1/go.mod h1:wcDf1JBCXy2mOW0bWHwO/IOYqdca1MPCwDtFu/Z9+eo= +go.mongodb.org/mongo-driver v1.13.2 h1:0BCe7YmG5KyiujRFA4xcU9071IFe4OFE4S3azDrsqz4= +go.mongodb.org/mongo-driver v1.13.2/go.mod h1:wcDf1JBCXy2mOW0bWHwO/IOYqdca1MPCwDtFu/Z9+eo= go.uber.org/atomic v1.11.0 h1:ZvwS0R+56ePWxUNi+Atn9dWONBPp/AUETXlHW0DxSjE= go.uber.org/atomic v1.11.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0= go.uber.org/multierr v1.9.0 h1:7fIwc/ZtS0q++VgcfqFDxSBZVv/Xo49/SYnDFupUwlI= @@ -157,10 +159,10 @@ golang.org/x/crypto v0.0.0-20190123085648-057139ce5d2b/go.mod h1:6SG95UA2DQfeDnf golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= -golang.org/x/crypto v0.16.0 h1:mMMrFzRSCF0GvB7Ne27XVtVAaXLrPmgPC7/v0tkwHaY= -golang.org/x/crypto v0.16.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4= -golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjsbSXD66ic0XW0js0R9g= -golang.org/x/exp v0.0.0-20230905200255-921286631fa9/go.mod h1:S2oDrQGGwySpoQPVqRShND87VCbxmc6bL1Yd2oYrm6k= +golang.org/x/crypto v0.21.0 h1:X31++rzVUdKhX5sWmSOFZxx8UW/ldWx55cbf08iNAMA= +golang.org/x/crypto v0.21.0/go.mod h1:0BP7YvVV9gBbVKyeTG0Gyn+gZm94bibOW5BjDEYAOMs= +golang.org/x/exp v0.0.0-20240318143956-a85f2c67cd81 h1:6R2FC06FonbXQ8pK11/PDFY6N6LWlf9KlzibaCapmqc= +golang.org/x/exp v0.0.0-20240318143956-a85f2c67cd81/go.mod h1:CQ1k9gNrJ50XIzaKCRR2hssIjF07kZFEiieALBM/ARQ= golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= @@ -169,10 +171,9 @@ golang.org/x/net v0.0.0-20190921015927-1a5e07d1ff72/go.mod h1:z5CRVTTTmAJ677TzLL golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= -golang.org/x/net v0.19.0 h1:zTwKpTd2XuCqf8huc7Fo2iSy+4RHPd10s4KzeTnVr1c= -golang.org/x/net v0.19.0/go.mod h1:CfAk/cbD4CthTvqiEl8NpboMuiuOYsAr/7NOjZJtv1U= +golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4= +golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= -golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.5.0 h1:60k92dhOjHxJkrqnwsfl8KuaHbn/5dl0lUPUklKo3qE= @@ -187,8 +188,8 @@ golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc= -golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4= +golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= @@ -206,8 +207,8 @@ golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtn golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 h1:H2TDz8ibqkAF6YGhCdN3jS9O0/s90v0rJh3X/OLHEUk= -golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2/go.mod h1:K8+ghG5WaK9qNqU5K3HdILfMLy1f3aNYFI/wnl100a8= +golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028 h1:+cNy6SZtPcJQH3LJVLOSmiC7MMxXNOb3PU/VUEz+EhU= +golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028/go.mod h1:NDW/Ps6MPRej6fsCIbMTohpP40sJ/P/vI1MoTEGwX90= google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= google.golang.org/genproto/googleapis/rpc v0.0.0-20231120223509-83a465c0220f h1:ultW7fxlIvee4HYrtnaRPon9HpEgFk5zYpmfMgtKB5I= diff --git a/server/registration/registration_test.go b/server/registration/registration_test.go deleted file mode 100644 index 0533b8c6..00000000 --- a/server/registration/registration_test.go +++ /dev/null @@ -1,20 +0,0 @@ -package main - -import ( - "net/http" - "testing" - - "github.com/steinfletcher/apitest" -) - -func IgnoreTestCreateDocument(t *testing.T) { - apitest.New(). - Handler(newApp().UserRouter). - Post("/document"). - Header("Content-Type", "application/json"). - Body(`{"name":"John Doe","passport":"12XY3456789","role":0,"picture":"1234567890987654321","registered":false}`). - Expect(t). - Body(`{"document_id": 1}`). - Status(http.StatusOK). - End() -} diff --git a/server/registration/.gitignore b/server/registry/.gitignore similarity index 100% rename from server/registration/.gitignore rename to server/registry/.gitignore diff --git a/server/registration/api.rest b/server/registry/api.rest similarity index 100% rename from server/registration/api.rest rename to server/registry/api.rest diff --git a/server/registration/config/config.go b/server/registry/config/config.go similarity index 100% rename from server/registration/config/config.go rename to server/registry/config/config.go diff --git a/server/registration/config/config.json b/server/registry/config/config.json similarity index 100% rename from server/registration/config/config.json rename to server/registry/config/config.json diff --git a/server/registration/database/database.go b/server/registry/database/database.go similarity index 72% rename from server/registration/database/database.go rename to server/registry/database/database.go index 8dec5a22..ad3f95eb 100644 --- a/server/registration/database/database.go +++ b/server/registry/database/database.go @@ -1,6 +1,6 @@ package database -import "go.dedis.ch/hbt/server/registration/registry" +import "go.dedis.ch/hbt/server/registry/registry" // Database defines a generic CRUD interface to the database type Database interface { @@ -10,9 +10,9 @@ type Database interface { Create(*registry.RegistrationData) (*registry.RegistrationID, error) // Read retrieves a document from the database - // it takes the document ID and the hash as arguments + // it takes the document ID as argument // and returns the document or an error - Read(registry.RegistrationID, []byte) (*registry.RegistrationData, error) + Read(registry.RegistrationID) (*registry.RegistrationData, error) // Update updates a document in the database // it takes the document ID and the updated document as an argument @@ -20,9 +20,9 @@ type Database interface { Update(registry.RegistrationID, *registry.RegistrationData) error // Delete deletes a document from the database - // it takes the document ID and the hash as arguments + // it takes the document ID as argument // and returns nil or an error - Delete(registry.RegistrationID, []byte) error + Delete(registry.RegistrationID) error // Disconnect disconnects from the database Disconnect() error diff --git a/server/registration/database/mongodb/dbtypes.go b/server/registry/database/mongodb/dbtypes.go similarity index 100% rename from server/registration/database/mongodb/dbtypes.go rename to server/registry/database/mongodb/dbtypes.go diff --git a/server/registration/database/mongodb/mongodb.go b/server/registry/database/mongodb/mongodb.go similarity index 91% rename from server/registration/database/mongodb/mongodb.go rename to server/registry/database/mongodb/mongodb.go index addf7384..374e4b5e 100644 --- a/server/registration/database/mongodb/mongodb.go +++ b/server/registry/database/mongodb/mongodb.go @@ -4,9 +4,9 @@ import ( "context" "errors" - "go.dedis.ch/hbt/server/registration/config" - "go.dedis.ch/hbt/server/registration/database" - "go.dedis.ch/hbt/server/registration/registry" + "go.dedis.ch/hbt/server/registry/config" + "go.dedis.ch/hbt/server/registry/database" + "go.dedis.ch/hbt/server/registry/registry" "go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo/options" @@ -71,7 +71,7 @@ func (d dbAccess) Create(data *registry.RegistrationData) (*registry.Registratio // Read reads a document from the DB // it is used to get the registered value of a document -func (d dbAccess) Read(id registry.RegistrationID, hash []byte) ( +func (d dbAccess) Read(id registry.RegistrationID) ( *registry.RegistrationData, error, ) { @@ -121,7 +121,7 @@ func (d dbAccess) Update( } // Delete updates a document in the DB -func (d dbAccess) Delete(id registry.RegistrationID, hash []byte) error { +func (d dbAccess) Delete(id registry.RegistrationID) error { var doc Document err := d.client.Database("registration").Collection("documents").FindOne(context.Background(), diff --git a/server/registration/docs/howto.md b/server/registry/docs/howto.md similarity index 100% rename from server/registration/docs/howto.md rename to server/registry/docs/howto.md diff --git a/server/registration/registry/admin/admin.go b/server/registry/registry/admin/admin.go similarity index 87% rename from server/registration/registry/admin/admin.go rename to server/registry/registry/admin/admin.go index e3338b28..0ba973bb 100644 --- a/server/registration/registry/admin/admin.go +++ b/server/registry/registry/admin/admin.go @@ -3,8 +3,8 @@ package admin import ( "net/http" - "go.dedis.ch/hbt/server/registration/database" - "go.dedis.ch/hbt/server/registration/registry/crud" + "go.dedis.ch/hbt/server/registry/database" + "go.dedis.ch/hbt/server/registry/registry/crud" ) var adminDB database.Database diff --git a/server/registration/registry/crud/crud.go b/server/registry/registry/crud/crud.go similarity index 79% rename from server/registration/registry/crud/crud.go rename to server/registry/registry/crud/crud.go index c5c37afc..2b9e435e 100644 --- a/server/registration/registry/crud/crud.go +++ b/server/registry/registry/crud/crud.go @@ -2,34 +2,35 @@ package crud import ( "encoding/json" - "log" "net/http" "strconv" - "go.dedis.ch/hbt/server/registration/database" - "go.dedis.ch/hbt/server/registration/registry" + "github.com/rs/zerolog/log" + + "go.dedis.ch/hbt/server/registry/database" + "go.dedis.ch/hbt/server/registry/registry" ) // CreateDocument translates the http request to create a new document in the database func CreateDocument(w http.ResponseWriter, r *http.Request, db database.Database) { err := r.ParseMultipartForm(32 << 20) if err != nil { - log.Fatal(err) + log.Fatal().Err(err) } name := r.FormValue("name") passport := r.FormValue("passport") role, err := strconv.ParseUint(r.FormValue("role"), 10, 32) if err != nil { - log.Fatal(err) + log.Fatal().Err(err) } picture, fileHeader, err := r.FormFile("portrait") if err != nil { - log.Println(err) + log.Error().Err(err) w.WriteHeader(http.StatusBadRequest) _, err = w.Write([]byte(err.Error())) if err != nil { - log.Println(err) + log.Error().Err(err) } return } @@ -37,11 +38,11 @@ func CreateDocument(w http.ResponseWriter, r *http.Request, db database.Database picData := make([]byte, fileHeader.Size) _, err = picture.Read(picData) if err != nil { - log.Println(err) + log.Error().Err(err) w.WriteHeader(http.StatusBadRequest) _, err = w.Write([]byte(err.Error())) if err != nil { - log.Println(err) + log.Error().Err(err) } return } @@ -60,7 +61,7 @@ func CreateDocument(w http.ResponseWriter, r *http.Request, db database.Database w.WriteHeader(http.StatusInternalServerError) _, err = w.Write([]byte(err.Error())) if err != nil { - log.Println(err) + log.Error().Err(err) } return } @@ -70,9 +71,9 @@ func CreateDocument(w http.ResponseWriter, r *http.Request, db database.Database encoder := json.NewEncoder(w) err = encoder.Encode(registrationID) if err != nil { - log.Println(err) + log.Error().Err(err) } - log.Println(registrationID) + log.Info().Msgf("Registration ID=%v", registrationID) } // GetDocument translates the http request to get a document from the database @@ -82,22 +83,20 @@ func GetDocument(w http.ResponseWriter, r *http.Request, db database.Database) { w.WriteHeader(http.StatusBadRequest) _, err := w.Write([]byte("missing id")) if err != nil { - log.Println(err) + log.Error().Err(err) } } - hash := r.FormValue("hash") - registrationID := registry.RegistrationID{ ID: []byte(id), } - data, err := db.Read(registrationID, []byte(hash)) + data, err := db.Read(registrationID) if err != nil { w.WriteHeader(http.StatusInternalServerError) _, err = w.Write([]byte(err.Error())) if err != nil { - log.Println(err) + log.Error().Err(err) } return } @@ -105,19 +104,19 @@ func GetDocument(w http.ResponseWriter, r *http.Request, db database.Database) { encoder := json.NewEncoder(w) err = encoder.Encode(data) if err != nil { - log.Println(err) + log.Error().Err(err) } - log.Println(data) + log.Info().Msgf("Get document id = %v, with data: %v", registrationID, data) } // UpdateDocument translates the http request to update a document in the database -func UpdateDocument(w http.ResponseWriter, r *http.Request, db database.Database, registered bool) { +func UpdateDocument(w http.ResponseWriter, r *http.Request, db database.Database, _ bool) { id := r.URL.Query().Get("id") if id == "" { w.WriteHeader(http.StatusBadRequest) _, err := w.Write([]byte("missing id")) if err != nil { - log.Println(err) + log.Error().Err(err) } } @@ -127,36 +126,36 @@ func UpdateDocument(w http.ResponseWriter, r *http.Request, db database.Database err := r.ParseMultipartForm(32 << 20) if err != nil { - log.Fatal(err) + log.Fatal().Err(err) } name := r.FormValue("name") passport := r.FormValue("passport") role, err := strconv.ParseUint(r.FormValue("role"), 10, 32) if err != nil { - log.Fatal(err) + log.Fatal().Err(err) } picture, fileHeader, err := r.FormFile("image") if err != nil { - log.Println(err) + log.Error().Err(err) w.WriteHeader(http.StatusBadRequest) _, err := w.Write([]byte(err.Error())) if err != nil { - log.Println(err) + log.Error().Err(err) } return } - log.Println(fileHeader) + log.Info().Msgf("file header = %v", fileHeader) picData := make([]byte, fileHeader.Size) _, err = picture.Read(picData) if err != nil { - log.Println(err) + log.Error().Err(err) w.WriteHeader(http.StatusBadRequest) _, err := w.Write([]byte(err.Error())) if err != nil { - log.Println(err) + log.Error().Err(err) } return } @@ -174,7 +173,7 @@ func UpdateDocument(w http.ResponseWriter, r *http.Request, db database.Database w.WriteHeader(http.StatusInternalServerError) _, err := w.Write([]byte(err.Error())) if err != nil { - log.Println(err) + log.Error().Err(err) } return } @@ -184,9 +183,9 @@ func UpdateDocument(w http.ResponseWriter, r *http.Request, db database.Database encoder := json.NewEncoder(w) err = encoder.Encode(registrationID) if err != nil { - log.Println(err) + log.Error().Err(err) } - log.Println(registrationID) + log.Info().Msgf("Updated registration id = %v", registrationID) } // DeleteDocument translates the http request to delete a document in the database @@ -196,25 +195,24 @@ func DeleteDocument(w http.ResponseWriter, r *http.Request, db database.Database w.WriteHeader(http.StatusBadRequest) _, err := w.Write([]byte("missing id")) if err != nil { - log.Println(err) + log.Error().Err(err) } } - hash := r.FormValue("hash") - registrationID := registry.RegistrationID{ ID: []byte(id), } - err := db.Delete(registrationID, []byte(hash)) + err := db.Delete(registrationID) if err != nil { w.WriteHeader(http.StatusInternalServerError) _, err := w.Write([]byte(err.Error())) if err != nil { - log.Println(err) + log.Error().Err(err) } return } w.WriteHeader(http.StatusOK) + log.Info().Msgf("Deleted registration id = %v", registrationID) } diff --git a/server/registration/registry/registry.go b/server/registry/registry/registry.go similarity index 100% rename from server/registration/registry/registry.go rename to server/registry/registry/registry.go diff --git a/server/registration/registry/user/user.go b/server/registry/registry/user/user.go similarity index 87% rename from server/registration/registry/user/user.go rename to server/registry/registry/user/user.go index 36bc31b6..8fbc43e3 100644 --- a/server/registration/registry/user/user.go +++ b/server/registry/registry/user/user.go @@ -3,8 +3,8 @@ package user import ( "net/http" - "go.dedis.ch/hbt/server/registration/database" - "go.dedis.ch/hbt/server/registration/registry/crud" + "go.dedis.ch/hbt/server/registry/database" + "go.dedis.ch/hbt/server/registry/registry/crud" ) var userDB database.Database diff --git a/server/registration/registration.go b/server/registry/registrycli/registrycli.go similarity index 90% rename from server/registration/registration.go rename to server/registry/registrycli/registrycli.go index 7b4f2039..b9c59d89 100644 --- a/server/registration/registration.go +++ b/server/registry/registrycli/registrycli.go @@ -7,13 +7,14 @@ import ( "time" "github.com/gorilla/mux" - "go.dedis.ch/hbt/server/registration/config" - "go.dedis.ch/hbt/server/registration/database/mongodb" - "go.dedis.ch/hbt/server/registration/registry/admin" - "go.dedis.ch/hbt/server/registration/registry/user" + "go.dedis.ch/hbt/server/registry/config" + "go.dedis.ch/hbt/server/registry/database/mongodb" + "go.dedis.ch/hbt/server/registry/registry/admin" + "go.dedis.ch/hbt/server/registry/registry/user" ) -// curl -F "name='John Doe'" -F "passport=12XY456789" -F "role=0" -F "image=@test/passport.jpg" -F "registered=false" localhost:3000/document +// curl -F "name='John Doe'" -F "passport=12XY456789" -F "role=0" -F "image=@test/passport.jpg" +// -F "registered=false" localhost:3000/document // application defines the application instance type application struct { diff --git a/server/registration/test/passport.jpg b/server/registry/test/passport.jpg similarity index 100% rename from server/registration/test/passport.jpg rename to server/registry/test/passport.jpg diff --git a/server/scripts/publish_roster.sh b/server/scripts/publish_roster.sh index 0d03d510..2f224f8f 100755 --- a/server/scripts/publish_roster.sh +++ b/server/scripts/publish_roster.sh @@ -9,15 +9,15 @@ set -e GREEN='\033[0;32m' # green color NC='\033[0m' # no Color - +KEYFILE=/tmp/priv.key # private key file K=$(grep 'Pubkey:' smckey.pub | sed 's/🔑 Pubkey: //' | xxd -r -p | base64) V=$(cat roster.txt) echo -e "${GREEN}[PUBLISH]${NC} the roster V=${V} on the blockchain using K=${K}" -chaincli --config /tmp/chain1 pool add\ - --key private.key\ +chaincli --config /tmp/blockchain1 pool add\ + --key ${KEYFILE}\ --args go.dedis.ch/dela.ContractArg --args go.dedis.ch/dela.Value\ --args value:key --args \"${K:0:8}\"\ --args value:value --args \"${K}:${V}\"\ diff --git a/server/scripts/setup.sh b/server/scripts/setup.sh index c4bbb0d2..07be3bd4 100755 --- a/server/scripts/setup.sh +++ b/server/scripts/setup.sh @@ -7,12 +7,15 @@ set -e GREEN='\033[0;32m' # green color RED='\033[1;31;46m' # red color NC='\033[0m' # no Color -L=info # default trace level +L=debug # default trace level S=hbt # session name echo -e "${GREEN}[TMUX setup]${NC}" set -o errexit +echo -e "${GREEN}[Make install...]${NC}" +make -C .. install + # Is tmux available ? command -v tmux >/dev/null 2>&1 || { echo >&2 "tmux is not on your PATH!"; exit 1; } @@ -32,10 +35,10 @@ tmux new-window -t ${S} -n smc tmux send-keys -t ${S}:smc.0 "./publish_roster.sh" C-m # attach to session -tmux select-pane -t ${S}:smc.0 -tmux send-keys -t ${S}:smc.0 "# TMUX MINI CHEAT SHEET" C-m -tmux send-keys -t ${S}:smc.0 "# Use 'tmux lscm' to list tmux commands" C-m -tmux send-keys -t ${S}:smc.0 "# Use 'Ctrl+B N (or P)' for next (or previous) window" C-m -tmux send-keys -t ${S}:smc.0 "# Use 'Ctrl+B ' to select pane" C-m -tmux send-keys -t ${S}:smc.0 "# './teardown.sh' to clean this tmux session" C-m +tmux select-pane -t ${S}:blockchain.0 +tmux send-keys -t ${S}:blockchain.0 "# TMUX MINI CHEAT SHEET" C-m +tmux send-keys -t ${S}:blockchain.0 "# Use 'tmux lscm' to list tmux commands" C-m +tmux send-keys -t ${S}:blockchain.0 "# Use 'Ctrl+B N (or P)' for next (or previous) window" C-m +tmux send-keys -t ${S}:blockchain.0 "# Use 'Ctrl+B ' to select pane" C-m +tmux send-keys -t ${S}:blockchain.0 "# './teardown.sh' to clean this tmux session" C-m tmux attach -t ${S} diff --git a/server/scripts/start_chain.sh b/server/scripts/start_chain.sh index e4a33d55..753b8d35 100755 --- a/server/scripts/start_chain.sh +++ b/server/scripts/start_chain.sh @@ -12,11 +12,13 @@ set -e GREEN='\033[0;32m' # green color RED='\033[1;31;46m' # red color NC='\033[0m' # no Color -L=info # default trace level +L=debug # default trace level S=hbt # session name W=blockchain N=4 # number of nodes P=10000 # base port number +PROXY=40000 # base proxy port number +KEYFILE=/tmp/priv.key # private key file echo -e "${GREEN}[PARSE parameters]${NC}" while getopts n:p:s:t:w: flag @@ -46,9 +48,9 @@ do done -echo -e "${GREEN}[PK]${NC} create a private key" -crypto bls signer new --save private.key - +echo -e "${GREEN}[PK]${NC} create a private key in ${KEYFILE}" +crypto bls signer new --save ${KEYFILE} --force +sleep 0.5 # Start a node in each pane but the main pane echo -e "${GREEN}[CREATE]${NC} ${N} nodes" @@ -56,9 +58,12 @@ i=1; while [ ${i} -le ${N} ] do p=$((P + i)) + proxy=$((PROXY + i)) echo -e "${GREEN}creating node #${i} on port ${p}${NC}" # session s, window 0, panes 1 to N - tmux send-keys -t ${S}:${W}.${i} "LLVL=${L} LOGF=./${W}${i}.log chaincli --config /tmp/${W}${i} start --listen tcp://127.0.0.1:${p}" C-m + tmux send-keys -t ${S}:${W}.${i} "LLVL=${L} LOGF=./${W}${i}.log chaincli --config /tmp/${W}${i} \ + start --listen tcp://127.0.0.1:${p} --proxyaddr localhost:${proxy} --public grpc://localhost:${p} \ + --routing tree --noTLS" C-m sleep 1 i=$((i + 1)); done @@ -67,10 +72,12 @@ done echo -e "${GREEN}[CONNECT]${NC} ${N} nodes and exchange certificates" i=2; p=$((P + 1)) +sleep 1 while [ ${i} -le ${N} ] do echo -e "joining node ${i} on master pane ${MASTERPANE}" - tmux send-keys -t "${MASTERPANE}" "chaincli --config /tmp/${W}${i} minogrpc join --address //127.0.0.1:${p} $(chaincli --config /tmp/${W}1 minogrpc token)" C-m + tmux send-keys -t "${MASTERPANE}" "chaincli --config /tmp/${W}${i} minogrpc join \ + --address grpc://127.0.0.1:${p} $(chaincli --config /tmp/${W}1 minogrpc token)" C-m sleep 1 i=$((i + 1)); done @@ -92,20 +99,20 @@ i=1; while [ ${i} -le ${N} ] do tmux send-keys -t "${MASTERPANE}" "chaincli --config /tmp/${W}${i} access add \ - --identity $(crypto bls signer read --path private.key --format BASE64_PUBKEY)" C-m + --identity $(crypto bls signer read --path ${KEYFILE} --format BASE64_PUBKEY)" C-m i=$((i + 1)); done echo -e "${GREEN}[GRANT]${NC} grant access for node 1 on the chain" # sent to master pane -tmux send-keys -t "${MASTERPANE}" "chaincli --config /tmp/${W}1 pool add\ - --key private.key\ - --args go.dedis.ch/dela.ContractArg --args go.dedis.ch/dela.Access\ - --args access:grant_id --args 0200000000000000000000000000000000000000000000000000000000000000\ - --args access:grant_contract --args go.dedis.ch/dela.Value\ - --args access:grant_command --args all\ - --args access:identity --args $(crypto bls signer read --path private.key --format BASE64_PUBKEY)\ +tmux send-keys -t "${MASTERPANE}" "chaincli --config /tmp/${W}1 pool add \ + --key ${KEYFILE} \ + --args go.dedis.ch/dela.ContractArg --args go.dedis.ch/dela.Access \ + --args access:grant_id --args 0200000000000000000000000000000000000000000000000000000000000000 \ + --args access:grant_contract --args go.dedis.ch/dela.Value \ + --args access:grant_command --args all \ + --args access:identity --args $(crypto bls signer read --path ${KEYFILE} --format BASE64_PUBKEY) \ --args access:command --args GRANT" C-m tmux select-pane -t "${MASTERPANE}" diff --git a/server/scripts/start_registry.sh b/server/scripts/start_registry.sh index 49ed1d82..1665977f 100755 --- a/server/scripts/start_registry.sh +++ b/server/scripts/start_registry.sh @@ -14,7 +14,6 @@ NC='\033[0m' # no Color L=info # default trace level S=hbt # session name W=registry # window name -P=12000 # base port number echo -e "${GREEN}[PARSE parameters]${NC}" while getopts n:p:s:t:w: flag @@ -37,7 +36,7 @@ tmux select-layout main-vertical # Start the registry server echo -e "${GREEN}[REGISTRY SERVER]${NC} starting" -tmux send-keys -t "${SLAVEPANE}" "registration" C-m +tmux send-keys -t "${SLAVEPANE}" "registrycli" C-m echo -e "${GREEN}[REGISTRY SERVER]${NC} started" tmux select-pane -t "${MASTERPANE}" diff --git a/server/scripts/start_smc.sh b/server/scripts/start_smc.sh index c7a340c7..6a6c3dce 100755 --- a/server/scripts/start_smc.sh +++ b/server/scripts/start_smc.sh @@ -16,7 +16,7 @@ S=hbt # session name W=smc # window name N=4 # number of nodes P=11000 # base port number -PROXY=40000 # base proxy port number +PROXY=41000 # base proxy port number echo -e "${GREEN}[PARSE parameters]${NC}" while getopts n:p:s:t:w: flag @@ -55,7 +55,9 @@ do proxy=$((PROXY + i)) echo -e "${GREEN}creating node #${i} on port ${p}${NC}" # session s, window 0, panes 1 to N - tmux send-keys -t ${S}:${W}.${i} "LLVL=${L} LOGF=./${W}${i}.log smccli --config /tmp/${W}${i} start --listen tcp://127.0.0.1:${p} --proxyaddr 127.0.0.1:${proxy}" C-m + tmux send-keys -t ${S}:${W}.${i} "LLVL=${L} LOGF=./${W}${i}.log smccli --config /tmp/${W}${i} \ + start --listen tcp://127.0.0.1:${p} --proxyaddr localhost:${proxy} --public grpc://localhost:${p} \ + --routing tree --noTLS" C-m sleep 0.5 i=$((i + 1)); done @@ -63,14 +65,18 @@ done echo -e "${GREEN}[CONNECT]${NC} ${N} nodes and exchange certificates" i=2; p=$((P + 1)) +sleep 1 +TOKEN=$(smccli --config /tmp/${W}1 minogrpc token) while [ ${i} -le ${N} ] do sleep 1 - tmux send-keys -t "${MASTERPANE}" "smccli --config /tmp/${W}${i} minogrpc join --address //127.0.0.1:${p} $(smccli --config /tmp/${W}1 minogrpc token)" C-m + tmux send-keys -t "${MASTERPANE}" "smccli --config /tmp/${W}${i} minogrpc join \ + --address grpc://127.0.0.1:${p} ${TOKEN}" C-m sleep 1 i=$((i + 1)); done + echo -e "${GREEN}[INITIALIZE DKG]${NC} on each node" i=1; while [ ${i} -le ${N} ] diff --git a/server/scripts/stop_registry.sh b/server/scripts/stop_registry.sh index 2ef95880..0a41c98d 100755 --- a/server/scripts/stop_registry.sh +++ b/server/scripts/stop_registry.sh @@ -3,4 +3,4 @@ # This script stops the registration server started by the start_registry.sh script # stop processes -pkill registration +pkill registratrycli diff --git a/server/smc/smc.go b/server/smc/smc.go index 3d9f7a2d..bb1ea587 100644 --- a/server/smc/smc.go +++ b/server/smc/smc.go @@ -1,9 +1,9 @@ package smc -import "go.dedis.ch/hbt/server/registration/registry" +import "go.dedis.ch/hbt/server/registry/registry" // SmcSecret contains the secret for the SMC type Secret struct { Data string `json:"secret"` - Id registry.RegistrationID `json:"id"` + ID registry.RegistrationID `json:"id"` } diff --git a/server/smc/smccli/controller/reveal.go b/server/smc/smccli/controller/reveal.go new file mode 100644 index 00000000..32dee76f --- /dev/null +++ b/server/smc/smccli/controller/reveal.go @@ -0,0 +1,40 @@ +package controller + +import ( + "go.dedis.ch/dela" + "go.dedis.ch/kyber/v3" + "golang.org/x/xerrors" +) + +// reveal decrypts a reencrypted message. +func reveal( + XhatEnc kyber.Point, + dkgPk kyber.Point, + userPrivateKey kyber.Scalar, + Cs []kyber.Point, +) ([]byte, error) { + dela.Logger.Info().Msgf("XhatEnc:%v", XhatEnc) + dela.Logger.Info().Msgf("dkgPk:%v", dkgPk) + dela.Logger.Info().Msgf("Cs:%v", Cs) + + xcInv := suite.Scalar().Neg(userPrivateKey) + XhatDec := suite.Point().Mul(xcInv, dkgPk) + Xhat := suite.Point().Add(XhatEnc, XhatDec) + XhatInv := suite.Point().Neg(Xhat) + + msg := make([]byte, 0, 128*len(Cs)) + + // Decrypt Cs to keyPointHat + for _, C := range Cs { + keyPointHat := suite.Point().Add(C, XhatInv) + keyPart, err := keyPointHat.Data() + if err != nil { + e := xerrors.Errorf("Error while decrypting Cs: %v", err) + dela.Logger.Error().Err(e).Msg("Failed revealing message") + return nil, e + } + msg = append(msg, keyPart...) + } + + return msg, nil +} diff --git a/server/smc/smccli/smccli.go b/server/smc/smccli/smccli.go index fd6e17c8..61c1df65 100644 --- a/server/smc/smccli/smccli.go +++ b/server/smc/smccli/smccli.go @@ -6,12 +6,12 @@ import ( "os" "go.dedis.ch/dela/cli/node" - kv "go.dedis.ch/dela/core/store/kv/controller" dkg "go.dedis.ch/dela/dkg/pedersen/controller" minogrpc "go.dedis.ch/dela/mino/minogrpc/controller" proxy "go.dedis.ch/dela/mino/proxy/http/controller" smc "go.dedis.ch/hbt/server/smc/smccli/controller" "go.dedis.ch/hbt/server/smc/smccli/web" + kv "go.dedis.ch/purb-db/store/kv/controller" ) type config struct { diff --git a/server/smc/smccli/web/action.go b/server/smc/smccli/web/action.go index 5e092b36..7667fc28 100644 --- a/server/smc/smccli/web/action.go +++ b/server/smc/smccli/web/action.go @@ -73,22 +73,21 @@ func (h *pubKeyHandler) ServeHTTP(w http.ResponseWriter, _ *http.Request) { pk, err := a.GetPublicKey() if err != nil { http.Error(w, fmt.Sprintf("failed retrieving public key: %v", err), - http.StatusInternalServerError) + http.StatusServiceUnavailable) return } - b, err := pk.MarshalBinary() + response, err := pk.MarshalBinary() if err != nil { http.Error(w, fmt.Sprintf("failed to marshal public key: %v", err), - http.StatusInternalServerError) + http.StatusInsufficientStorage) return } - encoder := json.NewEncoder(w) - err = encoder.Encode(b) + // Write the byte array to the response writer + _, err = w.Write(response) if err != nil { - http.Error(w, fmt.Sprintf("failed to respond: %v", err), - http.StatusInternalServerError) + http.Error(w, "Unable to write response", http.StatusNotExtended) return } } diff --git a/server/test/admin/blockchain.go b/server/test/admin/blockchain.go index 350c00f6..b56ff3c6 100644 --- a/server/test/admin/blockchain.go +++ b/server/test/admin/blockchain.go @@ -2,18 +2,17 @@ package admin import ( "encoding/json" - "fmt" "io" "net/http" "github.com/rs/zerolog/log" - "go.dedis.ch/hbt/server/registration/registry" + "go.dedis.ch/hbt/server/registry/registry" "go.dedis.ch/hbt/server/smc" "go.dedis.ch/kyber/v3" "go.dedis.ch/kyber/v3/suites" ) -const blockchainServer = "localhost:3003" +const blockchainServer = "http://localhost:40001" // suite is the Kyber suite for Pedersen. var suite = suites.MustFind("Ed25519") @@ -36,7 +35,7 @@ func BlockchainGetDocIDs(adminPubkey kyber.Point) []registry.RegistrationID { // Reading the response body body, err := io.ReadAll(resp.Body) if err != nil { - fmt.Println("Error reading response body:", err) + log.Error().Msgf("Error reading response body: %v", err) return nil } @@ -45,17 +44,17 @@ func BlockchainGetDocIDs(adminPubkey kyber.Point) []registry.RegistrationID { if resp.StatusCode == http.StatusOK { // Parsing JSON data if err := json.Unmarshal(body, &items); err != nil { - fmt.Println("Error parsing JSON:", err) + log.Error().Msgf("Error parsing JSON: %v", err) return nil } // Printing the list of IDs - fmt.Println("List of IDs:") - for _, item := range items { - fmt.Println(item.ID) + log.Info().Msg("List of IDs:") + for i, item := range items { + log.Info().Msgf("ID[%v] = %v", i, item.ID) } } else { - fmt.Println("Failed to fetch items. Status code:", resp.StatusCode) + log.Error().Msgf("Failed to fetch items. Status code:%v", resp.StatusCode) } return items diff --git a/server/test/admin/database.go b/server/test/admin/database.go index cb4de9a0..49c9f9c7 100644 --- a/server/test/admin/database.go +++ b/server/test/admin/database.go @@ -7,7 +7,7 @@ import ( "net/http" "github.com/rs/zerolog/log" - "go.dedis.ch/hbt/server/registration/registry" + "go.dedis.ch/hbt/server/registry/registry" ) const registrationServer = "localhost:3001" @@ -51,7 +51,8 @@ func RegistrationAdminUpdateDocument(docid registry.RegistrationID) error { log.Fatal().Msgf("error: %v", err) } - req, err := http.NewRequest("PUT", "localhost:3000/admin/document?id="+string(docid.ID), + req, err := http.NewRequest(http.MethodPut, + "localhost:3000/admin/document?id="+string(docid.ID), bytes.NewBuffer(out)) if err != nil { log.Fatal().Msgf("error: %v", err) @@ -70,7 +71,8 @@ func RegistrationAdminUpdateDocument(docid registry.RegistrationID) error { } func RegistrationAdminDeleteDocument(docid registry.RegistrationID) error { - req, err := http.NewRequest("DELETE", "localhost:3000/admin/document?id="+string(docid.ID), nil) + req, err := http.NewRequest(http.MethodDelete, + "localhost:3000/admin/document?id="+string(docid.ID), nil) if err != nil { log.Fatal().Msgf("error: %v", err) } diff --git a/server/test/admin/smc.go b/server/test/admin/smc.go index af7ec085..023b618d 100644 --- a/server/test/admin/smc.go +++ b/server/test/admin/smc.go @@ -55,9 +55,9 @@ func encodePublickey(pk kyber.Point) string { return hex.EncodeToString(pkbuff) } -func encodeSecret(secret []byte) string { - return hex.EncodeToString(secret) -} +// func encodeSecret(secret []byte) string { +// return hex.EncodeToString(secret) +// } func decodeReencrypted(xhatencstring string) (kyber.Point, error) { xhatencbuff, err := hex.DecodeString(xhatencstring) @@ -65,7 +65,7 @@ func decodeReencrypted(xhatencstring string) (kyber.Point, error) { return nil, err } - var xhatenc kyber.Point + xhatenc := suite.Point() err = xhatenc.UnmarshalBinary(xhatencbuff) if err != nil { return nil, err diff --git a/server/test/test.go b/server/test/test.go index 45ff36af..d45d6a45 100644 --- a/server/test/test.go +++ b/server/test/test.go @@ -4,7 +4,7 @@ import ( "os" "github.com/rs/zerolog/log" - "go.dedis.ch/hbt/server/registration/registry" + "go.dedis.ch/hbt/server/registry/registry" "go.dedis.ch/hbt/server/test/admin" "go.dedis.ch/hbt/server/test/key" "go.dedis.ch/hbt/server/test/user" @@ -13,9 +13,6 @@ import ( const keySize = 32 func main() { - // create a secret symmetric key - symKey := key.NewSymetric(keySize) - // PRETEND TO BE A USER // --------------------------------------------------------- @@ -24,6 +21,9 @@ func main() { doc := createDocument("John Doe", "12AB456789", 0, "./passport.jpg") log.Info().Msg("SUCCESS! created new document") + // create a secret symmetric key + symKey := key.NewSymetric(keySize) + // add the document to the registry log.Info().Msg("ADD document to the registry") docid, err := user.RegistrationAdd(doc, symKey) @@ -35,7 +35,7 @@ func main() { // get the SMC pub key log.Info().Msg("FETCH SMC key") smcKey := user.SmcGetKey() - log.Info().Msgf("SUCCESS! got SMC key: %v", smcKey) + log.Info().Msgf("SUCCESS! got SMC key: %v", smcKey.String()) // add secret = symKey to the blockchain log.Info().Msg("ADD secret to the blockchain") @@ -67,18 +67,18 @@ func main() { // secret.Data = K:Cs in a string format symKey2, err := admin.SmcReveal(xhatenc, smcKey, sk, secret.Data) + if err != nil { + log.Fatal().Msgf("error: %v", err) + } - if false == compare2ByteArrays(symKey, symKey2) { + if !compare2ByteArrays(symKey, symKey2) { log.Fatal().Msg("symmetric key mismatch") } - // encrypt binary array with symmetric key - // and save it to the registry - // TODO: get the encrypted document from the registry - // TODO: decrypt the document - optional + // TODO: decrypt the document // TODO: update the document status to registered - // TODO: encrypt the document - optional + // TODO: encrypt the document // TODO: save the document back to the registry } diff --git a/server/test/user/blockchain.go b/server/test/user/blockchain.go index 9dde999f..fe98173e 100644 --- a/server/test/user/blockchain.go +++ b/server/test/user/blockchain.go @@ -1,19 +1,21 @@ package user import ( + "bytes" "encoding/hex" + "io" + "mime/multipart" "net/http" - "net/url" "github.com/rs/zerolog/log" "go.dedis.ch/dela" - "go.dedis.ch/hbt/server/registration/registry" + "go.dedis.ch/hbt/server/registry/registry" "go.dedis.ch/kyber/v3" "go.dedis.ch/kyber/v3/suites" "golang.org/x/xerrors" ) -const blockchainServer = "localhost:3003" +const blockchainServer = "http://localhost:40001" // suite is the Kyber suite for Pedersen. var suite = suites.MustFind("Ed25519") @@ -29,13 +31,38 @@ func BlockchainEncryptAndAddSecret( log.Fatal().Msgf("error: %v", err) } - // Add the secret to the blockchain - resp, err := http.PostForm(blockchainServer+"/secret", - url.Values{ - "secret": {encryptedSecret}, - "id": {string(id.ID)}, - }) + var body bytes.Buffer + w := multipart.NewWriter(&body) + fw, err := w.CreateFormField("secret") + if err != nil { + log.Fatal().Msgf("error: %v", err) + } + _, err = io.Copy(fw, bytes.NewReader([]byte(encryptedSecret))) + if err != nil { + log.Fatal().Msgf("error: %v", err) + } + + fw, err = w.CreateFormField("id") + if err != nil { + log.Fatal().Msgf("error: %v", err) + } + _, err = io.Copy(fw, bytes.NewReader(id.ID)) + if err != nil { + log.Fatal().Msgf("error: %v", err) + } + + w.Close() + + req, err := http.NewRequest(http.MethodPost, blockchainServer+"/secret", &body) + if err != nil { + log.Fatal().Msgf("error: %v", err) + } + + // Don't forget to set the content type, this will contain the boundary. + req.Header.Set("Content-Type", w.FormDataContentType()) + + resp, err := http.DefaultClient.Do(req) if err != nil { log.Fatal().Msgf("error: %v", err) } diff --git a/server/test/user/database.go b/server/test/user/database.go index f01e9dfc..e932ae07 100644 --- a/server/test/user/database.go +++ b/server/test/user/database.go @@ -11,7 +11,7 @@ import ( "strconv" "github.com/rs/zerolog/log" - "go.dedis.ch/hbt/server/registration/registry" + "go.dedis.ch/hbt/server/registry/registry" "go.dedis.ch/hbt/server/test/key" ) @@ -129,7 +129,8 @@ func RegistrationGet(docid registry.RegistrationID, symKey []byte) registry.Regi // RegistrationDelete deletes the registration data from the database func RegistrationDelete(docid registry.RegistrationID) error { - req, err := http.NewRequest("DELETE", registrationServer+"/document?id="+string(docid.ID), nil) + req, err := http.NewRequest(http.MethodDelete, + registrationServer+"/document?id="+string(docid.ID), nil) if err != nil { log.Fatal().Msgf("error: %v", err) } @@ -149,76 +150,76 @@ func RegistrationDelete(docid registry.RegistrationID) error { // --------------------------------------------------------------------------- // The following functions are used to encrypt and decrypt the registration -func encryptRegistrationData(data registry.RegistrationData, symKey []byte) ( - registry.EncryptedData, - error, -) { - // Convert the struct to a byte array - buf := new(bytes.Buffer) - - // encrypt the data.Name - err := binary.Write(buf, binary.LittleEndian, data.Name) - if err != nil { - return registry.EncryptedData{}, err - } - - encName, err := key.Encrypt(symKey, buf.Bytes()) - if err != nil { - return registry.EncryptedData{}, err - } - - // encrypt the data.Passport - err = binary.Write(buf, binary.LittleEndian, data.Name) - if err != nil { - return registry.EncryptedData{}, err - } - - encPassport, err := key.Encrypt(symKey, buf.Bytes()) - if err != nil { - return registry.EncryptedData{}, err - } - - // encrypt the data.Role - err = binary.Write(buf, binary.LittleEndian, data.Role) - if err != nil { - return registry.EncryptedData{}, err - } - - encRole, err := key.Encrypt(symKey, buf.Bytes()) - if err != nil { - return registry.EncryptedData{}, err - } - - // encrypt the data.Picture - err = binary.Write(buf, binary.LittleEndian, data.Picture) - if err != nil { - return registry.EncryptedData{}, err - } - - encPicture, err := key.Encrypt(symKey, buf.Bytes()) - if err != nil { - return registry.EncryptedData{}, err - } - - // encrypt the data.Registered - err = binary.Write(buf, binary.LittleEndian, data.Registered) - if err != nil { - return registry.EncryptedData{}, err - } - - encRegistered, err := key.Encrypt(symKey, buf.Bytes()) - if err != nil { - return registry.EncryptedData{}, err - } - - return registry.EncryptedData{ - Name: encName, - Passport: encPassport, - Picture: encPicture, - Role: encRole, - Registered: encRegistered, - }, nil -} +// func encryptRegistrationData(data registry.RegistrationData, symKey []byte) ( +// registry.EncryptedData, +// error, +// ) { +// // Convert the struct to a byte array +// buf := new(bytes.Buffer) +// +// // encrypt the data.Name +// err := binary.Write(buf, binary.LittleEndian, data.Name) +// if err != nil { +// return registry.EncryptedData{}, err +// } +// +// encName, err := key.Encrypt(symKey, buf.Bytes()) +// if err != nil { +// return registry.EncryptedData{}, err +// } +// +// // encrypt the data.Passport +// err = binary.Write(buf, binary.LittleEndian, data.Name) +// if err != nil { +// return registry.EncryptedData{}, err +// } +// +// encPassport, err := key.Encrypt(symKey, buf.Bytes()) +// if err != nil { +// return registry.EncryptedData{}, err +// } +// +// // encrypt the data.Role +// err = binary.Write(buf, binary.LittleEndian, data.Role) +// if err != nil { +// return registry.EncryptedData{}, err +// } +// +// encRole, err := key.Encrypt(symKey, buf.Bytes()) +// if err != nil { +// return registry.EncryptedData{}, err +// } +// +// // encrypt the data.Picture +// err = binary.Write(buf, binary.LittleEndian, data.Picture) +// if err != nil { +// return registry.EncryptedData{}, err +// } +// +// encPicture, err := key.Encrypt(symKey, buf.Bytes()) +// if err != nil { +// return registry.EncryptedData{}, err +// } +// +// // encrypt the data.Registered +// err = binary.Write(buf, binary.LittleEndian, data.Registered) +// if err != nil { +// return registry.EncryptedData{}, err +// } +// +// encRegistered, err := key.Encrypt(symKey, buf.Bytes()) +// if err != nil { +// return registry.EncryptedData{}, err +// } +// +// return registry.EncryptedData{ +// Name: encName, +// Passport: encPassport, +// Picture: encPicture, +// Role: encRole, +// Registered: encRegistered, +// }, nil +// } func decryptRegistrationData(encrypted registry.EncryptedData, symKey []byte) ( registry.RegistrationData, diff --git a/server/test/user/smc.go b/server/test/user/smc.go index cd79be7e..41ee8808 100644 --- a/server/test/user/smc.go +++ b/server/test/user/smc.go @@ -1,30 +1,44 @@ package user import ( - "encoding/json" + "io" "net/http" "github.com/rs/zerolog/log" "go.dedis.ch/kyber/v3" ) -const smcServer = "http://localhost:40001" +// Define a struct to unmarshal the JSON response +type Response struct { + PubKey []byte `json:"pubkey"` +} + +const smcServer = "http://localhost:41001" func SmcGetKey() kyber.Point { resp, err := http.Get(smcServer + "/smc/pubkey") if err != nil { log.Fatal().Msgf("error: %v", err) } - defer resp.Body.Close() - // Decode the response - var data kyber.Point + // Check if the status code is OK + if resp.StatusCode != http.StatusOK { + log.Fatal().Msgf("Error received from server: %v", resp) + } + + // Read the response body into a byte array + body, err := io.ReadAll(resp.Body) + if err != nil { + log.Fatal().Msgf("error: %v", err) + } - err = json.NewDecoder(resp.Body).Decode(&data) + // Unmarshal the response + pk := suite.Point() + err = pk.UnmarshalBinary(body) if err != nil { - log.Error().Msgf("error decoding response: %v", err) + log.Error().Msgf("Error unmarshaling the pubkey: %v", err) } - return data + return pk }