From 463b91ad6ea6be06efa404b2dd2d9eaebbfcae18 Mon Sep 17 00:00:00 2001 From: Maxime Zammit Date: Mon, 25 Mar 2024 16:09:40 +0100 Subject: [PATCH 1/6] add formAdmin struct --- .gitignore | 2 + contracts/evoting/admin_test.go | 45 ++++++++++++++++++ contracts/evoting/json/adminForm.go | 50 ++++++++++++++++++++ contracts/evoting/json/mod.go | 1 + contracts/evoting/types/admin.go | 71 +++++++++++++++++++++++++++++ web/backend/src/controllers/dela.ts | 1 - 6 files changed, 169 insertions(+), 1 deletion(-) create mode 100644 contracts/evoting/admin_test.go create mode 100644 contracts/evoting/json/adminForm.go create mode 100644 contracts/evoting/types/admin.go diff --git a/.gitignore b/.gitignore index 361116e9b..613ac6555 100644 --- a/.gitignore +++ b/.gitignore @@ -19,3 +19,5 @@ dela/ bin/ nodes/ cookies.txt + +.tool-versions \ No newline at end of file diff --git a/contracts/evoting/admin_test.go b/contracts/evoting/admin_test.go new file mode 100644 index 000000000..14da46317 --- /dev/null +++ b/contracts/evoting/admin_test.go @@ -0,0 +1,45 @@ +package evoting + +import ( + "github.com/c4dt/d-voting/contracts/evoting/types" + "github.com/stretchr/testify/require" + "go.dedis.ch/dela/serde" + sjson "go.dedis.ch/dela/serde/json" + "testing" +) + +var ctxAdminTest serde.Context + +var formFacAdminTest serde.Factory +var transactionFacAdminTest serde.Factory + +func init() { + ciphervoteFac := types.CiphervoteFactory{} + formFacAdminTest = types.NewFormFactory(ciphervoteFac, fakeAuthorityFactory{}) + transactionFacAdminTest = types.NewTransactionFactory(ciphervoteFac) + + ctxAdminTest = sjson.NewContext() +} + +func TestAdmin_serde(t *testing.T) { + adminFormID := "myID" + adminFormList := []int{111111, 222222, 333333, 123456} + + adminForm := types.AdminForm{FormID: adminFormID, AdminList: adminFormList} + + value, err := adminForm.Serialize(ctxAdminTest) + + require.NoError(t, err) + + // deserialization + newAdminForm := types.AdminForm{} + + msgs, err := newAdminForm.Deserialize(ctxAdminTest, value) + + require.NoError(t, err) + + updatedAdminForm := msgs.(types.AdminForm) + + require.Equal(t, adminFormID, updatedAdminForm.FormID) + require.Equal(t, adminFormList, updatedAdminForm.AdminList) +} diff --git a/contracts/evoting/json/adminForm.go b/contracts/evoting/json/adminForm.go new file mode 100644 index 000000000..64212dec4 --- /dev/null +++ b/contracts/evoting/json/adminForm.go @@ -0,0 +1,50 @@ +package json + +import ( + "github.com/c4dt/d-voting/contracts/evoting/types" + "go.dedis.ch/dela/serde" + "golang.org/x/xerrors" +) + +type adminFormFormat struct{} + +func (adminFormFormat) Encode(ctx serde.Context, message serde.Message) ([]byte, error) { + switch m := message.(type) { + case types.AdminForm: + adminFormJSON := AdminFormJSON{ + FormID: m.FormID, + AdminList: m.AdminList, + } + + buff, err := ctx.Marshal(&adminFormJSON) + if err != nil { + return nil, xerrors.Errorf("failed to marshal form: %v", err) + } + + return buff, nil + default: + return nil, xerrors.Errorf("Unknown format: %T", message) + } +} + +func (adminFormFormat) Decode(ctx serde.Context, data []byte) (serde.Message, error) { + var adminFormJSON AdminFormJSON + + err := ctx.Unmarshal(data, &adminFormJSON) + if err != nil { + return nil, xerrors.Errorf("failed to unmarshal form: %v", err) + } + + return types.AdminForm{ + FormID: adminFormJSON.FormID, + AdminList: adminFormJSON.AdminList, + }, nil +} + +type AdminFormJSON struct { + // FormID is the hex-encoded SHA265 of the Tx ID that creates the form + FormID string + + // List of SCIPER with admin rights + AdminList []int +} diff --git a/contracts/evoting/json/mod.go b/contracts/evoting/json/mod.go index c9845338a..be6023adc 100644 --- a/contracts/evoting/json/mod.go +++ b/contracts/evoting/json/mod.go @@ -12,4 +12,5 @@ func init() { types.RegisterSuffragiaFormat(serde.FormatJSON, suffragiaFormat{}) types.RegisterCiphervoteFormat(serde.FormatJSON, ciphervoteFormat{}) types.RegisterTransactionFormat(serde.FormatJSON, transactionFormat{}) + types.RegisterAdminFormFormat(serde.FormatJSON, adminFormFormat{}) } diff --git a/contracts/evoting/types/admin.go b/contracts/evoting/types/admin.go new file mode 100644 index 000000000..dd5ab0462 --- /dev/null +++ b/contracts/evoting/types/admin.go @@ -0,0 +1,71 @@ +package types + +import ( + "encoding/hex" + "go.dedis.ch/dela/core/store" + "go.dedis.ch/dela/serde" + "go.dedis.ch/dela/serde/registry" + "golang.org/x/xerrors" +) + +var adminFormFormat = registry.NewSimpleRegistry() + +func RegisterAdminFormFormat(format serde.Format, engine serde.FormatEngine) { + adminFormFormat.Register(format, engine) +} + +type AdminForm struct { + // FormID is the hex-encoded SHA265 of the Tx ID that creates the form + FormID string + + // List of SCIPER with admin rights + AdminList []int +} + +func (af AdminForm) Serialize(ctx serde.Context) ([]byte, error) { + format := adminFormFormat.Get(ctx.GetFormat()) + + data, err := format.Encode(ctx, af) + if err != nil { + return nil, xerrors.Errorf("Failed to encode AdminForm: %v", err) + } + + return data, nil +} + +func (af AdminForm) Deserialize(ctx serde.Context, data []byte) (serde.Message, error) { + format := adminFormFormat.Get(ctx.GetFormat()) + + message, err := format.Decode(ctx, data) + if err != nil { + return nil, xerrors.Errorf("Failed to decode: %v", err) + } + + return message, nil +} + +func AdminFormFromStore(ctx serde.Context, adminFormFac serde.Factory, adminFormIDHex string, store store.Readable) (AdminForm, error) { + adminForm := AdminForm{} + + adminFormIDBuf, err := hex.DecodeString(adminFormIDHex) + if err != nil { + return adminForm, xerrors.Errorf("Failed to decode adminFormIDHex: %v", err) + } + + adminFormBuf, err := store.Get(adminFormIDBuf) + if err != nil { + return adminForm, xerrors.Errorf("While getting data for form: %v", err) + } + if len(adminFormBuf) == 0 { + return adminForm, xerrors.Errorf("No form found") + } + + message, err := adminFormFac.Deserialize(ctx, adminFormBuf) + + adminForm, ok := message.(AdminForm) + if !ok { + return adminForm, xerrors.Errorf("Wrong message type: %T", message) + } + + return adminForm, nil +} diff --git a/web/backend/src/controllers/dela.ts b/web/backend/src/controllers/dela.ts index 8c725f9fc..3afd08a67 100644 --- a/web/backend/src/controllers/dela.ts +++ b/web/backend/src/controllers/dela.ts @@ -265,6 +265,5 @@ delaRouter.use('/*', (req, res) => { } const dataStr = JSON.stringify(bodyData); - sendToDela(dataStr, req, res); }); From b9e2bf704cdc75ff1b41816a977bd80a7e76b6d0 Mon Sep 17 00:00:00 2001 From: Maxime Zammit Date: Mon, 25 Mar 2024 16:20:10 +0100 Subject: [PATCH 2/6] patch linting --- contracts/evoting/types/admin.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/contracts/evoting/types/admin.go b/contracts/evoting/types/admin.go index dd5ab0462..e5d26706e 100644 --- a/contracts/evoting/types/admin.go +++ b/contracts/evoting/types/admin.go @@ -61,6 +61,9 @@ func AdminFormFromStore(ctx serde.Context, adminFormFac serde.Factory, adminForm } message, err := adminFormFac.Deserialize(ctx, adminFormBuf) + if err != nil { + return adminForm, xerrors.Errorf("While deserializing: %v", err) + } adminForm, ok := message.(AdminForm) if !ok { From a1ec49afa503ab893b7d4525b570dfbfdcadba93 Mon Sep 17 00:00:00 2001 From: Maxime Zammit Date: Wed, 27 Mar 2024 09:05:42 +0100 Subject: [PATCH 3/6] address changes --- .gitignore | 4 +--- contracts/evoting/admin_test.go | 3 +++ web/backend/src/controllers/dela.ts | 1 + 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 613ac6555..5d296b0ec 100644 --- a/.gitignore +++ b/.gitignore @@ -18,6 +18,4 @@ deb-package/dist/** dela/ bin/ nodes/ -cookies.txt - -.tool-versions \ No newline at end of file +cookies.txt \ No newline at end of file diff --git a/contracts/evoting/admin_test.go b/contracts/evoting/admin_test.go index 14da46317..9c1121d13 100644 --- a/contracts/evoting/admin_test.go +++ b/contracts/evoting/admin_test.go @@ -21,6 +21,9 @@ func init() { ctxAdminTest = sjson.NewContext() } +// This test create an Admin Form structure which is then serialized and +// deserialized to check whether these operations work as intended. +// Given an AdminForm we should retrieve the same AdminForm after these operations. func TestAdmin_serde(t *testing.T) { adminFormID := "myID" adminFormList := []int{111111, 222222, 333333, 123456} diff --git a/web/backend/src/controllers/dela.ts b/web/backend/src/controllers/dela.ts index 3afd08a67..8c725f9fc 100644 --- a/web/backend/src/controllers/dela.ts +++ b/web/backend/src/controllers/dela.ts @@ -265,5 +265,6 @@ delaRouter.use('/*', (req, res) => { } const dataStr = JSON.stringify(bodyData); + sendToDela(dataStr, req, res); }); From f35398f0e040c9dc851763188f0147631595ec40 Mon Sep 17 00:00:00 2001 From: Maxime Zammit Date: Wed, 27 Mar 2024 09:11:00 +0100 Subject: [PATCH 4/6] change comment formatting --- contracts/evoting/admin_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contracts/evoting/admin_test.go b/contracts/evoting/admin_test.go index 9c1121d13..dee5ddde4 100644 --- a/contracts/evoting/admin_test.go +++ b/contracts/evoting/admin_test.go @@ -23,7 +23,7 @@ func init() { // This test create an Admin Form structure which is then serialized and // deserialized to check whether these operations work as intended. -// Given an AdminForm we should retrieve the same AdminForm after these operations. +// Serialization/Deserialization of an AdminForm should not change its values. func TestAdmin_serde(t *testing.T) { adminFormID := "myID" adminFormList := []int{111111, 222222, 333333, 123456} From 823103a4bf5abee6bedb8aa3cdcf59bd6d5eb0bd Mon Sep 17 00:00:00 2001 From: Maxime <61186830+MaximeZmt@users.noreply.github.com> Date: Wed, 27 Mar 2024 09:14:52 +0100 Subject: [PATCH 5/6] Update .gitignore edit .gitignore to add new line at the end as before --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 5d296b0ec..361116e9b 100644 --- a/.gitignore +++ b/.gitignore @@ -18,4 +18,4 @@ deb-package/dist/** dela/ bin/ nodes/ -cookies.txt \ No newline at end of file +cookies.txt From 4a972d03a708fc1033d84b74ca93e434c769ea47 Mon Sep 17 00:00:00 2001 From: Maxime Zammit Date: Wed, 27 Mar 2024 09:25:38 +0100 Subject: [PATCH 6/6] make encode function logic simpler --- contracts/evoting/json/adminForm.go | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/contracts/evoting/json/adminForm.go b/contracts/evoting/json/adminForm.go index 64212dec4..29255548b 100644 --- a/contracts/evoting/json/adminForm.go +++ b/contracts/evoting/json/adminForm.go @@ -9,22 +9,22 @@ import ( type adminFormFormat struct{} func (adminFormFormat) Encode(ctx serde.Context, message serde.Message) ([]byte, error) { - switch m := message.(type) { - case types.AdminForm: - adminFormJSON := AdminFormJSON{ - FormID: m.FormID, - AdminList: m.AdminList, - } - - buff, err := ctx.Marshal(&adminFormJSON) - if err != nil { - return nil, xerrors.Errorf("failed to marshal form: %v", err) - } - - return buff, nil - default: + adminForm, ok := message.(types.AdminForm) + if !ok { return nil, xerrors.Errorf("Unknown format: %T", message) } + + adminFormJSON := AdminFormJSON{ + FormID: adminForm.FormID, + AdminList: adminForm.AdminList, + } + + buff, err := ctx.Marshal(&adminFormJSON) + if err != nil { + return nil, xerrors.Errorf("failed to marshal form: %v", err) + } + + return buff, nil } func (adminFormFormat) Decode(ctx serde.Context, data []byte) (serde.Message, error) {