diff --git a/server/blockchain/web/action.go b/server/blockchain/web/action.go index ad90cd30..2bd1bcff 100644 --- a/server/blockchain/web/action.go +++ b/server/blockchain/web/action.go @@ -37,9 +37,12 @@ func (a *RegisterAction) Execute(ctx node.Context) error { router := mux.NewRouter() s := &secretHandler{ctx} + router.HandleFunc("/secret/smc", s.advertiseSmc).Methods("POST") + 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/admin/list", s.listSecrets).Methods("GET") + router.HandleFunc("/secret/admin", s.getSecret).Methods("GET") router.NotFoundHandler = http.HandlerFunc(notFoundHandler) router.MethodNotAllowedHandler = http.HandlerFunc(notAllowedHandler) @@ -57,6 +60,60 @@ type secretHandler struct { ctx node.Context } +// advertiseSmc advertises the SMC public key and its roster to the blockchain +func (s *secretHandler) advertiseSmc(w http.ResponseWriter, r *http.Request) { + err := r.ParseMultipartForm(32 << 20) + if err != nil { + log.Fatal().Err(err) + } + + smckey := r.FormValue("smckey") + roster := r.FormValue("roster") + dela.Logger.Info().Msgf("received SMC pubkey %v from SMC roster %v", smckey, roster) + + // get the calypso contract + var c calypso.Contract + err = s.ctx.Injector.Resolve(&c) + if err != nil { + 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 PURB database") + http.Error(w, fmt.Sprintf("failed to resolve database: %v", err), + http.StatusInternalServerError) + return + } + + 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.CmdAdvertiseSmc), + calypso.SmcPublicKeyArg, smckey, calypso.RosterArg, roster)) + + return err + }) + + if err != nil { + dela.Logger.Error().Err(err).Msg("failed to advertise SMC to the blockchain") + http.Error(w, fmt.Sprintf("failed to advertise SMC to the blockchain: %v", err), + http.StatusInternalServerError) + return + } + + dela.Logger.Info().Msg("SMC advertised to the blockchain") + + return +} + // addSecret adds a new secret in the blockchain func (s *secretHandler) addSecret(w http.ResponseWriter, r *http.Request) { err := r.ParseMultipartForm(32 << 20) @@ -64,6 +121,7 @@ func (s *secretHandler) addSecret(w http.ResponseWriter, r *http.Request) { log.Fatal().Err(err) } + smckey := r.FormValue("smckey") secret := r.FormValue("secret") id := r.FormValue("id") dela.Logger.Info().Msgf("received doc ID=%v with secret=%v", id, secret) @@ -82,7 +140,7 @@ func (s *secretHandler) addSecret(w http.ResponseWriter, r *http.Request) { 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.Error(w, fmt.Sprintf("failed to resolve PURB database: %v", err), http.StatusInternalServerError) return } @@ -98,6 +156,7 @@ func (s *secretHandler) addSecret(w http.ResponseWriter, r *http.Request) { } err = c.Execute(b, makeStep(calypso.CmdArg, string(calypso.CmdCreateSecret), + calypso.SmcPublicKeyArg, smckey, calypso.SecretNameArg, id, calypso.SecretArg, secret)) return err @@ -114,8 +173,39 @@ func (s *secretHandler) addSecret(w http.ResponseWriter, r *http.Request) { } // listSecrets lists all secrets in the blockchain -func (s *secretHandler) listSecrets(_ http.ResponseWriter, _ *http.Request) { +func (s *secretHandler) listSecrets(w http.ResponseWriter, r *http.Request) { // list all secrets from the blockchain + r.ParseForm() + + pubkey := r.Form.Get("pubkey") + dela.Logger.Info().Msgf("received request from %v to list the secrets", pubkey) + + // get the calypso contract + var c calypso.Contract + err := s.ctx.Injector.Resolve(&c) + if err != nil { + 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 + } + + err = db.View(func(txn purbkv.ReadableTx) error { + b := txn.GetBucket([]byte("bucket:secret")) + + err = c.Execute(b, makeStep(calypso.CmdArg, string(calypso.CmdListSecrets))) + + return err + }) } diff --git a/server/scripts/publish_roster.sh b/server/scripts/publish_roster.sh index 2f224f8f..e392bf79 100755 --- a/server/scripts/publish_roster.sh +++ b/server/scripts/publish_roster.sh @@ -9,16 +9,18 @@ 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) +#K=$(grep 'Pubkey:' smckey.pub | sed 's/🔑 Pubkey: //' | xxd -r -p | base64) +K=$(grep 'Pubkey:' smckey.pub | sed 's/🔑 Pubkey: //') V=$(cat roster.txt) +echo -e "${GREEN}[PUBLISH]${NC} the roster ${V} on the blockchain using key ${K}" -echo -e "${GREEN}[PUBLISH]${NC} the roster V=${V} on the blockchain using K=${K}" -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}\"\ - --args value:command --args WRITE +ARGS="--args go.dedis.ch/dela.ContractArg --args go.dedis.ch/dela.Value \ + --args value:key --args ${K} \ + --args value:value --args ${V} \ + --args value:command --args WRITE" + +echo -e ${ARGS} +# shellcheck disable=SC2090 +LLVL="debug" chaincli --config /tmp/blockchain1 pool add --key /tmp/priv.key ${ARGS} diff --git a/server/scripts/start_smc.sh b/server/scripts/start_smc.sh index 6a6c3dce..44619365 100755 --- a/server/scripts/start_smc.sh +++ b/server/scripts/start_smc.sh @@ -104,8 +104,8 @@ while [ ${i} -lt ${N} ] do i=$((i + 1)); p=$((P + i)); - V="${V},127.0.0.1:${p}"; + V="${V};127.0.0.1:${p}"; done -tmux send-keys -t "${MASTERPANE}" "echo ${V} > roster.txt" C-m +tmux send-keys -t "${MASTERPANE}" "echo \"${V}\" > roster.txt" C-m tmux select-pane -t "${MASTERPANE}" diff --git a/server/scripts/stop_chain.sh b/server/scripts/stop_chain.sh index ff957cac..e08755c5 100755 --- a/server/scripts/stop_chain.sh +++ b/server/scripts/stop_chain.sh @@ -9,4 +9,4 @@ pkill chaincli rm -rf /tmp/blockchain* # remove blockchain pk -rm -f private.key +rm -f /tmp/priv.key diff --git a/server/scripts/stop_registry.sh b/server/scripts/stop_registry.sh index 0a41c98d..a2bccd44 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 registratrycli +pkill registrycli diff --git a/server/test/admin/blockchain.go b/server/test/admin/blockchain.go index b56ff3c6..3e3f4392 100644 --- a/server/test/admin/blockchain.go +++ b/server/test/admin/blockchain.go @@ -25,7 +25,7 @@ func BlockchainGetDocIDs(adminPubkey kyber.Point) []registry.RegistrationID { log.Fatal().Msgf("error: %v", err) } - resp, err := http.Get(blockchainServer + "/secret/list?pubkey=" + string(encoded)) + resp, err := http.Get(blockchainServer + "/secret/admin/list?pubkey=" + string(encoded)) if err != nil { log.Fatal().Msgf("error: %v", err) } @@ -67,7 +67,7 @@ func BlockchainGetSecret(id registry.RegistrationID, pk kyber.Point) (smc.Secret log.Fatal().Msgf("error: %v", err) } - resp, err := http.Get(blockchainServer + "/secret?pubkey=" + string(encodedPk) + "&id=" + string(id.ID)) + resp, err := http.Get(blockchainServer + "/secret/admin?pubkey=" + string(encodedPk) + "&id=" + string(id.ID)) if err != nil { log.Fatal().Msgf("error: %v", err) }