aboutsummaryrefslogtreecommitdiffstats
path: root/bug/op_create.go
diff options
context:
space:
mode:
authorMichael Muré <batolettre@gmail.com>2020-11-08 19:15:06 +0100
committerMichael Muré <batolettre@gmail.com>2021-02-14 12:17:44 +0100
commit7163b2283b4542a4d4abfe9a71963f122322bde7 (patch)
tree93a52a65c5ba1fd3e4112b2aa53c2c438bb5400c /bug/op_create.go
parent5ae8a132772385c903a62de2ceec02a97f108a01 (diff)
downloadgit-bug-7163b2283b4542a4d4abfe9a71963f122322bde7.tar.gz
bug: Id from first operation data, not git + remove root link
Diffstat (limited to 'bug/op_create.go')
-rw-r--r--bug/op_create.go26
1 files changed, 24 insertions, 2 deletions
diff --git a/bug/op_create.go b/bug/op_create.go
index 9bb40d35..3c8ce658 100644
--- a/bug/op_create.go
+++ b/bug/op_create.go
@@ -1,6 +1,7 @@
package bug
import (
+ "crypto/rand"
"encoding/json"
"fmt"
"strings"
@@ -17,6 +18,10 @@ var _ Operation = &CreateOperation{}
// CreateOperation define the initial creation of a bug
type CreateOperation struct {
OpBase
+ // mandatory random bytes to ensure a better randomness of the data of the first
+ // operation of a bug, used to later generate the ID
+ // len(Nonce) should be > 20 and < 64 bytes
+ Nonce []byte `json:"nonce"`
Title string `json:"title"`
Message string `json:"message"`
Files []repository.Hash `json:"files"`
@@ -66,14 +71,19 @@ func (op *CreateOperation) Validate() error {
return err
}
+ if len(op.Nonce) > 64 {
+ return fmt.Errorf("create nonce is too big")
+ }
+ if len(op.Nonce) < 20 {
+ return fmt.Errorf("create nonce is too small")
+ }
+
if text.Empty(op.Title) {
return fmt.Errorf("title is empty")
}
-
if strings.Contains(op.Title, "\n") {
return fmt.Errorf("title should be a single line")
}
-
if !text.Safe(op.Title) {
return fmt.Errorf("title is not fully printable")
}
@@ -98,6 +108,7 @@ func (op *CreateOperation) UnmarshalJSON(data []byte) error {
}
aux := struct {
+ Nonce []byte `json:"nonce"`
Title string `json:"title"`
Message string `json:"message"`
Files []repository.Hash `json:"files"`
@@ -109,6 +120,7 @@ func (op *CreateOperation) UnmarshalJSON(data []byte) error {
}
op.OpBase = base
+ op.Nonce = aux.Nonce
op.Title = aux.Title
op.Message = aux.Message
op.Files = aux.Files
@@ -119,9 +131,19 @@ func (op *CreateOperation) UnmarshalJSON(data []byte) error {
// Sign post method for gqlgen
func (op *CreateOperation) IsAuthored() {}
+func makeNonce(len int) []byte {
+ result := make([]byte, len)
+ _, err := rand.Read(result)
+ if err != nil {
+ panic(err)
+ }
+ return result
+}
+
func NewCreateOp(author identity.Interface, unixTime int64, title, message string, files []repository.Hash) *CreateOperation {
return &CreateOperation{
OpBase: newOpBase(CreateOp, author, unixTime),
+ Nonce: makeNonce(20),
Title: title,
Message: message,
Files: files,