Skip to content

Commit

Permalink
streamlined code; initialisation of history file happens at 'claw new…
Browse files Browse the repository at this point in the history
…', and a new hash is added to history file at each 'claw revise'
  • Loading branch information
joshkestenberg committed Jun 29, 2017
1 parent 1a012b9 commit 065e123
Showing 1 changed file with 11 additions and 17 deletions.
28 changes: 11 additions & 17 deletions contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@ func newContract(cmd *cobra.Command, args []string) error {
return err
}

// generate history file
hfstring := "// Do not modify this file yourself under any circumstances!"

if err := ioutil.WriteFile(filepath.Join(engagementPath, "history.toml"), []byte(hfstring), 0600); err != nil {
return err
}

paramsFile := generateParamsFile(tmpl)

// write the params file
Expand All @@ -110,7 +117,7 @@ func generateParamsFile(tmpl ContractTemplate) []byte {
const paramsFileDefault = `# This is a TOML file containing parameters for this contract
[meta]
# This must match the hash of the local template.md file. DO NOT CHANGE IT
# This hash must match the hash of the local template.md file. DO NOT MAKE CHANGES TO THIS HASH.
template = "{{ .TemplateHash}}"
[var]
Expand Down Expand Up @@ -235,18 +242,6 @@ func reviseContract(cmd *cobra.Command, args []string) error {
return err
}

// open history file (or create history file if it doesn't exist)
file, err := os.OpenFile("history.toml", os.O_RDWR|os.O_CREATE, 0600)
if err != nil {
return err
}

defer file.Close()

if _, err = file.WriteString("// Do not modify this file yourself under any circumstances!"); err != nil {
return err
}

// read history file
history_b, err := ioutil.ReadFile("history.toml")
if err != nil {
Expand All @@ -258,17 +253,14 @@ func reviseContract(cmd *cobra.Command, args []string) error {
byte_array := make([]byte, 3, 3)

for _, element := range all_b {
for _, b := range element {
byte_array = append(byte_array, b)
}
byte_array = append(byte_array, element...)
}

// hash params, history, and template data
h := sha256.New()
h.Write(byte_array)
t := time.Now()
hashtime := fmt.Sprintf("\n%s: '%X'", t, h.Sum(nil))
fmt.Println("A hash has been added to your history file; your changes are secure.")

// write hash to history file
hfile, err := os.OpenFile("history.toml", os.O_RDWR|os.O_APPEND, 0600)
Expand All @@ -282,6 +274,8 @@ func reviseContract(cmd *cobra.Command, args []string) error {
return err
}

fmt.Println("A revision hash has been added to your history file; your changes are secure.")

return nil

}

0 comments on commit 065e123

Please sign in to comment.