@@ -6,27 +6,27 @@ import (
6
6
"net/http"
7
7
8
8
"github.com/gorilla/mux"
9
+ "github.com/rs/zerolog/log"
9
10
"go.dedis.ch/dela"
10
11
"go.dedis.ch/dela/cli/node"
12
+ "go.dedis.ch/dela/core/execution"
13
+ "go.dedis.ch/dela/core/txn"
14
+ "go.dedis.ch/dela/core/txn/signed"
15
+ "go.dedis.ch/dela/crypto/bls"
11
16
"go.dedis.ch/dela/mino/proxy"
12
- "go.dedis.ch/kyber/v3/suites"
17
+ "go.dedis.ch/hbt/server/blockchain/calypso"
18
+ purbkv "go.dedis.ch/purb-db/store/kv"
13
19
14
20
"golang.org/x/xerrors"
15
21
)
16
22
17
- // suite is the Kyber suite for Pedersen.
18
- var suite = suites .MustFind ("Ed25519" )
19
-
20
- const separator = ":"
21
- const malformedEncoded = "malformed encoded: %s"
22
-
23
23
// RegisterAction is an action to register the HTTP handlers
24
24
//
25
25
// - implements node.ActionTemplate
26
26
type RegisterAction struct {}
27
27
28
28
// Execute implements node.ActionTemplate. It registers the handlers using the
29
- // default proxy from the the injector.
29
+ // default proxy from the injector.
30
30
func (a * RegisterAction ) Execute (ctx node.Context ) error {
31
31
var p proxy.Proxy
32
32
err := ctx .Injector .Resolve (& p )
@@ -36,11 +36,10 @@ func (a *RegisterAction) Execute(ctx node.Context) error {
36
36
37
37
router := mux .NewRouter ()
38
38
39
- s := & secretHandler {& ctx }
39
+ s := & secretHandler {ctx }
40
40
router .HandleFunc ("/secret" , s .addSecret ).Methods ("POST" )
41
41
router .HandleFunc ("/secret/list" , s .listSecrets ).Methods ("GET" )
42
42
router .HandleFunc ("/secret" , s .getSecret ).Methods ("GET" )
43
- router .HandleFunc ("/secret/reveal" , s .revealSecret ).Methods ("POST" )
44
43
45
44
router .NotFoundHandler = http .HandlerFunc (notFoundHandler )
46
45
router .MethodNotAllowedHandler = http .HandlerFunc (notAllowedHandler )
@@ -54,49 +53,74 @@ func (a *RegisterAction) Execute(ctx node.Context) error {
54
53
55
54
type DocID []byte
56
55
57
- // a secretData is a struct to hold the secret data: the document ID and the
58
- // encrypted key to access the document
59
- type secretData struct {
60
- docID DocID `json:"docid"`
61
- encryptedKey string `json:"secret"`
62
- }
63
-
64
56
type secretHandler struct {
65
- ctx * node.Context
57
+ ctx node.Context
66
58
}
67
59
68
60
// addSecret adds a new secret in the blockchain
69
61
func (s * secretHandler ) addSecret (w http.ResponseWriter , r * http.Request ) {
70
- ctx := * (s .ctx )
62
+ err := r .ParseMultipartForm (32 << 20 )
63
+ if err != nil {
64
+ log .Fatal ().Err (err )
65
+ }
71
66
72
- // Decode the request
73
- var sec secretData
74
- err := json .NewDecoder (r .Body ).Decode (& sec )
67
+ secret := r .FormValue ("secret" )
68
+ id := r .FormValue ("id" )
69
+ dela .Logger .Info ().Msgf ("received doc ID=%v with secret=%v" , id , secret )
70
+
71
+ // get the calypso contract
72
+ var c calypso.Contract
73
+ err = s .ctx .Injector .Resolve (& c )
75
74
if err != nil {
76
- http .Error (w , err .Error (), http .StatusBadRequest )
75
+ dela .Logger .Error ().Err (err ).Msg ("failed to resolve calypso contract" )
76
+ http .Error (w , fmt .Sprintf ("failed to resolve calypso contract: %v" , err ),
77
+ http .StatusInternalServerError )
78
+ return
79
+ }
80
+
81
+ var db purbkv.DB
82
+ err = s .ctx .Injector .Resolve (& db )
83
+ if err != nil {
84
+ dela .Logger .Error ().Err (err ).Msg ("failed to resolve database" )
85
+ http .Error (w , fmt .Sprintf ("failed to resolve database: %v" , err ),
86
+ http .StatusInternalServerError )
77
87
return
78
88
}
79
89
80
90
// add the secret to the blockchain
81
91
// the secret is added to the blockchain with the document ID as the key
82
92
// and the encrypted key as the value
83
- // TODO add it to the blockchain
84
- dela .Logger .Info ().Msgf ("secret added to the blockchain: ID=%v secret=%v" , sec .docID ,
85
- sec .encryptedKey )
93
+
94
+ err = db .Update (func (txn purbkv.WritableTx ) error {
95
+ b , err := txn .GetBucketOrCreate ([]byte ("bucket:secret" ))
96
+ if err != nil {
97
+ return err
98
+ }
99
+
100
+ err = c .Execute (b , makeStep (calypso .CmdArg , string (calypso .CmdCreateSecret ),
101
+ calypso .SecretNameArg , id , calypso .SecretArg , secret ))
102
+
103
+ return err
104
+ })
105
+
106
+ if err != nil {
107
+ dela .Logger .Error ().Err (err ).Msg ("failed to add secret to the blockchain" )
108
+ http .Error (w , fmt .Sprintf ("failed to add secret to the blockchain: %v" , err ),
109
+ http .StatusInternalServerError )
110
+ return
111
+ }
112
+
113
+ dela .Logger .Info ().Msgf ("secret added to the blockchain: ID=%v secret=%v" , id , secret )
86
114
}
87
115
88
116
// listSecrets lists all secrets in the blockchain
89
- func (s * secretHandler ) listSecrets (w http.ResponseWriter , r * http.Request ) {
90
- ctx := * (s .ctx )
91
-
117
+ func (s * secretHandler ) listSecrets (_ http.ResponseWriter , _ * http.Request ) {
92
118
// list all secrets from the blockchain
93
119
94
120
}
95
121
96
122
// getSecret gets a secret from the blockchain
97
123
func (s * secretHandler ) getSecret (w http.ResponseWriter , r * http.Request ) {
98
- ctx := * (s .ctx )
99
-
100
124
// Decode the request
101
125
var id DocID
102
126
err := json .NewDecoder (r .Body ).Decode (& id )
@@ -160,3 +184,27 @@ func notAllowedHandler(w http.ResponseWriter, r *http.Request) {
160
184
w .WriteHeader (http .StatusMethodNotAllowed )
161
185
fmt .Fprintln (w , string (buf ))
162
186
}
187
+
188
+ // -----------------------------------------------------------------------------
189
+ // Utility functions
190
+ var nounce uint64
191
+
192
+ func makeStep (args ... string ) execution.Step {
193
+ return execution.Step {Current : makeTx (args ... )}
194
+ }
195
+
196
+ func makeTx (args ... string ) txn.Transaction {
197
+ signer := bls .NewSigner ()
198
+
199
+ options := []signed.TransactionOption {}
200
+ for i := 0 ; i < len (args )- 1 ; i += 2 {
201
+ options = append (options , signed .WithArg (args [i ], []byte (args [i + 1 ])))
202
+ }
203
+
204
+ tx , err := signed .NewTransaction (nounce , signer .GetPublicKey (), options ... )
205
+ if err != nil {
206
+ nounce ++
207
+ }
208
+
209
+ return tx
210
+ }
0 commit comments