Skip to content

Commit

Permalink
fix publish-roster.sh
Browse files Browse the repository at this point in the history
  • Loading branch information
jbsv committed Apr 2, 2024
1 parent a8fd536 commit 89ab236
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 19 deletions.
98 changes: 94 additions & 4 deletions server/blockchain/web/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -57,13 +60,68 @@ 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

Check failure on line 114 in server/blockchain/web/action.go

View workflow job for this annotation

GitHub Actions / lint

S1023: redundant `return` statement (gosimple)
}

// addSecret adds a new secret in the blockchain
func (s *secretHandler) addSecret(w http.ResponseWriter, r *http.Request) {
err := r.ParseMultipartForm(32 << 20)
if err != nil {
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)
Expand All @@ -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
}
Expand All @@ -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
Expand All @@ -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()

Check failure on line 178 in server/blockchain/web/action.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `r.ParseForm` is not checked (errcheck)

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
})

}

Expand Down
20 changes: 11 additions & 9 deletions server/scripts/publish_roster.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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}
4 changes: 2 additions & 2 deletions server/scripts/start_smc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
2 changes: 1 addition & 1 deletion server/scripts/stop_chain.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ pkill chaincli
rm -rf /tmp/blockchain*

# remove blockchain pk
rm -f private.key
rm -f /tmp/priv.key
2 changes: 1 addition & 1 deletion server/scripts/stop_registry.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
# This script stops the registration server started by the start_registry.sh script

# stop processes
pkill registratrycli
pkill registrycli
4 changes: 2 additions & 2 deletions server/test/admin/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit 89ab236

Please sign in to comment.