Skip to content

Commit

Permalink
Merge pull request #71 from dedis/scenario-blockchain
Browse files Browse the repository at this point in the history
Scenario blockchain
  • Loading branch information
jbsv authored Mar 30, 2024
2 parents 7cac687 + 6c9f49a commit a8fd536
Show file tree
Hide file tree
Showing 40 changed files with 452 additions and 322 deletions.
4 changes: 2 additions & 2 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion android/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions server/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions server/blockchain/calypso/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ func (m miniController) OnStart(_ cli.Flags, inj node.Injector) error {

calypso.RegisterContract(exec, contract)

inj.Inject(contract)

return nil
}

Expand Down
14 changes: 9 additions & 5 deletions server/blockchain/chaincli/chaincli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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(),
)

Expand Down
110 changes: 79 additions & 31 deletions server/blockchain/web/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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
}
33 changes: 16 additions & 17 deletions server/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
Loading

0 comments on commit a8fd536

Please sign in to comment.