Skip to content

Commit

Permalink
Merge pull request #18 from marigold-dev/crypto-stuff
Browse files Browse the repository at this point in the history
Crypto stuff
  • Loading branch information
aguillon authored Dec 29, 2023
2 parents 7c64e33 + 69b4e48 commit 2aed33c
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 15 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2022-2023 Marigold developers
Copyright (c) 2022-2024 Marigold developers

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
6 changes: 5 additions & 1 deletion lib/context.mligo
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ type actor = {
name : string
; initial_amount: tez
; address: address
; key: key
; secret: string
}

(** [init actors] initializes bootstrap accounts. *)
Expand All @@ -43,11 +45,13 @@ let init_with (actors: (string * tez) list) : actor list =
let () = Test.reset_state number_of_accounts default_amounts in
let (_counter, actors) =
List.fold_left (fun ((i, actors), (name, value) : (nat * actor list) * (string * tez)) ->
let address = Test.nth_bootstrap_account (int i) in
let (address, key, secret) = Test.get_bootstrap_account i in
let actor = {
name = name
; initial_amount = value
; address = address
; key = key
; secret = secret
}
in
(i + 1n, actor :: actors)
Expand Down
2 changes: 1 addition & 1 deletion ligo.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"type": "git",
"url": "git+https://github.com/marigold-dev/breathalyzer.git"
},
"version": "1.5.0",
"version": "1.6.0",
"main": "./lib/lib.mligo",
"author": "Marigold <[email protected]>",
"license": "MIT",
Expand Down
5 changes: 3 additions & 2 deletions test/test.mligo
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@

#import "../lib/lib.mligo" "Breath"
#import "test_result.mligo" "Result_suite"
#import "test_tezos.mligo" "Tezos_suite"
#import "test_tezos.mligo" "Tezos_suites"
#import "test_time.mligo" "Time_suite"

let () =
Breath.Model.run_suites Void [
Result_suite.suite
; Tezos_suite.suite
; Tezos_suites.views_suite
; Tezos_suites.keys_suite
; Time_suite.suite
]
65 changes: 55 additions & 10 deletions test/test_tezos.mligo
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#import "../lib/lib.mligo" "B"
#import "./simple_contract.jsligo" "Simple"

module Other = struct
module Views = struct
type storage = {
simple: address;
x: int
Expand All @@ -40,15 +40,15 @@ module Other = struct
storage.x
end

let (_, (alice, _, _)) = B.Context.init_default ()
let (_, (alice, bob, _)) = B.Context.init_default ()

let originated level =
B.Contract.originate level "Simple" (contract_of Simple) 0 0tez

let case_views_1 =
B.Model.case
"call_view"
"succeeds on a contract originated with originate_module"
"succeeds on an originated contract"
(fun (level: B.Logger.level) ->
let contract = originated level in
B.Result.reduce [
Expand All @@ -65,27 +65,72 @@ let case_views_1 =
let case_views_2 =
B.Model.case
"call_view"
"succeeds when a contract uses it on another contract, both originated with originate_module"
"succeeds when a contract uses it on another contract"
(fun (level: B.Logger.level) ->
let contract = originated level in
let initial_storage = { simple = contract.originated_address; x = 0 } in
let other =
B.Contract.originate level "Other" (contract_of Other) initial_storage 0tez
let view_contract =
B.Contract.originate level "Views" (contract_of Views) initial_storage 0tez
in
B.Result.reduce [
B.Context.call_as alice
contract (Increment 10);
B.Context.call_as alice
other (Default ());
view_contract (Default ());
B.Assert.is_equal
"should be 10"
(Tezos.call_view "view" () other.originated_address)
(Tezos.call_view "view" () view_contract.originated_address)
(Some 10)
])

let suite =
(* Simple signatures check to ensure Breathalyzer generates and stores the
correct public and private keys *)

module Signatures = struct
type storage = unit

[@entry]
let default (key, signature: key * signature) (_: storage): operation list * storage =
if Crypto.check key signature 0x1234 then
([], ())
else
failwith "Invalid signature"
end

let key_1 =
B.Model.case
"signature"
"can be checked for the right key"
(fun (level: B.Logger.level) ->
let contract = B.Contract.originate level "Signatures" (contract_of Signatures) () 0tez in
let sign = Test.sign alice.secret 0x1234 in
B.Result.reduce [
B.Context.call_as alice
contract (Default (alice.key, sign))
])

let key_2 =
B.Model.case
"signature"
"cannot be checked for the wrong key"
(fun (level: B.Logger.level) ->
let contract = B.Contract.originate level "Signatures" (contract_of Signatures) () 0tez in
let sign = Test.sign alice.secret 0x1234 in
B.Result.reduce [
B.Expect.fail_with_message "Invalid signature"
(B.Context.call_as alice
contract (Default (bob.key, sign)))
])

let keys_suite =
B.Model.suite
"Test suite for keys and signatures"
[ key_1
; key_2 ]

let views_suite =
B.Model.suite
"Test suite for Tezos operations"
"Test suite for Tezos views"
[ case_views_1
; case_views_2
]

0 comments on commit 2aed33c

Please sign in to comment.