Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor and free storage #30

Merged
merged 5 commits into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 3 additions & 12 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,23 +59,14 @@ jobs:
asset_path: ./_build/app/metadata.json
asset_name: metadata.json
asset_content_type: application/json
- name: Upload unit contract
- name: Upload contract
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ github.token }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: ./_build/app/tzsafe_unit.tez
asset_name: tzsafe_unit.tez
asset_content_type: application/text
- name: Upload bytes contract
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ github.token }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: ./_build/app/tzsafe_bytes.tez
asset_name: tzsafe_bytes.tez
asset_path: ./_build/app/tzsafe.tez
asset_name: tzsafe.tez
asset_content_type: application/text
- name: Upload artifact
uses: actions/upload-artifact@v3
Expand Down
10 changes: 4 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ BUILT_APP_DIRECTORY:=$(BUILD_DIRECTORY)/$(APP_DIRECTORY)

# Ligo compiler
LIGO_COMPILER_ARGS:=--protocol nairobi
LIGO_VERSION:=0.70.1
LIGO_VERSION:=0.73.0
LIGO?=ligo
LIGO_BUILD=$(LIGO) compile contract $(LIGO_COMPILER_ARGS)
LIGO_TEST=$(LIGO) run test
LIGO_CURRENT_VERSION:=$(shell $(LIGO) --version)
LIGO_INSTALL=$(LIGO) install

# Tezos binaries
TEZOS_BINARIES_VERSION:=v17.1-1
TEZOS_BINARIES_VERSION:=v15.1-1
TEZOS_BINARIES_REPO:=https://github.com/serokell/tezos-packaging/releases/download/
TEZOS_BINARIES_URL:=$(TEZOS_BINARIES_REPO)$(TEZOS_BINARIES_VERSION)

Expand All @@ -42,8 +42,7 @@ build-metadata:

build-contract: check-ligo-version
mkdir -p $(BUILT_APP_DIRECTORY)
$(LIGO_BUILD) $(APP_DIRECTORY)/main_unit.mligo > $(BUILT_APP_DIRECTORY)/$(PROJECT_NAME)_unit.tez
$(LIGO_BUILD) $(APP_DIRECTORY)/main_bytes.mligo > $(BUILT_APP_DIRECTORY)/$(PROJECT_NAME)_bytes.tez
$(LIGO_BUILD) $(APP_DIRECTORY)/main.mligo > $(BUILT_APP_DIRECTORY)/$(PROJECT_NAME).tez

test:
$(LIGO_TEST) $(TEST_DIRECTORY)/test.mligo
Expand Down Expand Up @@ -72,8 +71,7 @@ gen-wallet:

deploy:
$(eval SIGNER := $(shell TEZOS_CLIENT_UNSAFE_DISABLE_DISCLAIMER=yes ./_build/octez-client --endpoint https://ghostnet.tezos.marigold.dev show address wallet_address | grep Hash | awk '{print $$2}'))
$(BUILD_DIRECTORY)/octez-client --endpoint https://ghostnet.tezos.marigold.dev originate contract $(PROJECT_NAME)_unit transferring 2 from wallet_address running $(BUILT_APP_DIRECTORY)/$(PROJECT_NAME)_unit.tez --init '(Pair 0 {} {"$(SIGNER)";} 1 604800 {})' --burn-cap 2 -f
$(BUILD_DIRECTORY)/octez-client --endpoint https://ghostnet.tezos.marigold.dev originate contract $(PROJECT_NAME)_bytes transferring 2 from wallet_address running $(BUILT_APP_DIRECTORY)/$(PROJECT_NAME)_bytes.tez --init '(Pair 0 {} {"$(SIGNER)";} 1 604800 {})' --burn-cap 2 -f
$(BUILD_DIRECTORY)/octez-client --endpoint https://ghostnet.tezos.marigold.dev originate contract $(PROJECT_NAME) transferring 2 from wallet_address running $(BUILT_APP_DIRECTORY)/$(PROJECT_NAME).tez --init '(Pair 0 {} {} {"$(SIGNER)"; "tz1inzFwAjE4oWXMabJFZdPHoDQN5S4XB3wH"} 1 604800 {})' --burn-cap 3 -f

get-tezos-binary:
wget -O $(BUILD_DIRECTORY)/octez-client $(TEZOS_BINARIES_URL)/octez-client
Expand Down
25 changes: 25 additions & 0 deletions app/main.mligo
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
(* MIT License
Copyright (c) 2022 Marigold <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE. *)

#import "../src/lib.mligo" "Lib"

type result = Lib.result
type request = Lib.request

let main (request : request) : result =
Lib.contract request
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "TzSafe",
"version": "0.3.0",
"version": "0.3.1",
"author": "Marigold <[email protected]>",
"description": "TzSafe is a multisig wallet aiming at providing better assurance of security and management of ownership than a traditional single-signed wallet. The implementation adheres to the guidelines specified in TZIP-26.",
"description": "TzSafe is a multisig wallet aiming at providing better assurance of security and management of ownership than a traditional single-signed wallet. The implementation adheres to the guidelines specified in TZIP-27.",
"scripts": {
"test": "ligo run test test/test.mligo --project-root ."
},
Expand Down
2 changes: 1 addition & 1 deletion src/common/errors.mligo
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ let unknown_contract = "Unknown contract"
let invalidated_threshold = "Threshold must be greater than 1"
let no_owner = "Require at least one owner in the contract"
let no_enough_owner = "Number of owner should be greater than threshold"
let unresolved = "This proposal has been resolved"
let already_resolved = "This proposal has been resolved"
let no_enough_signature_to_resolve = "No enough signature to resolve the proposal"
let no_proposal = "There is no content in proposal"
let no_owners = "No owner to be added or removed"
Expand Down
35 changes: 10 additions & 25 deletions src/internal/conditions.mligo
Original file line number Diff line number Diff line change
Expand Up @@ -29,34 +29,23 @@ type storage_types_proposal_state = Storage.Types.proposal_state
type effective_period = Storage.Types.effective_period
type proposal_content = Proposal_content.Types.t

[@inline]
let only_owner (type a) (storage : a storage_types) : unit =
let only_owner (storage : storage_types) : unit =
assert_with_error (Set.mem (Tezos.get_sender ()) storage.owners) Errors.only_owner

[@inline]
let amount_must_be_zero_tez (an_amout : tez) : unit =
assert_with_error (an_amout = 0tez) Errors.amount_must_be_zero_tez
let amount_must_be_zero_tez (amount : tez) : unit =
assert_with_error (amount = 0tez) Errors.amount_must_be_zero_tez

[@inline]
let unsigned (type a) (proposal : a storage_types_proposal) : unit =
let unsigned (proposal : storage_types_proposal) : unit =
assert_with_error (not Map.mem (Tezos.get_sender ()) proposal.signatures) Errors.has_already_signed

[@inline]
let ready_to_execute (state : storage_types_proposal_state) : unit =
assert_with_error (not (state = (Proposing : storage_types_proposal_state))) Errors.no_enough_signature_to_resolve

[@inline]
let unresolved (state : storage_types_proposal_state) : unit =
assert_with_error (state = (Proposing : storage_types_proposal_state)) Errors.unresolved

[@inline]
let check_proposal (type a) (content: a proposal_content) : unit =
let check_proposal (content: proposal_content) : unit =
match content with
| Transfer t ->
assert_with_error (not (t.amount = 0tez)) Errors.amount_is_zero
| Execute _ -> ()
| Execute_lambda e ->
assert_with_error (Util.is_some e.lambda) Errors.no_proposal
| Execute_lambda _ -> ()
| Adjust_threshold t ->
assert_with_error (t > 0n) Errors.invalidated_threshold
| Add_owners s ->
Expand All @@ -66,24 +55,20 @@ let check_proposal (type a) (content: a proposal_content) : unit =
| Adjust_effective_period p ->
assert_with_error (p > 0) Errors.invalid_effective_period

[@inline]
let not_empty_content (type a) (proposals_content: (a proposal_content) list) : unit =
let not_empty_content (proposals_content: proposal_content list) : unit =
let () = assert_with_error ((List.length proposals_content) > 0n) Errors.no_proposal in
List.iter check_proposal proposals_content

[@inline]
let check_setting (type a) (storage : a storage_types) : unit =
let check_setting (storage : storage_types) : unit =
let () = assert_with_error (Set.cardinal storage.owners > 0n) Errors.no_owner in
let () = assert_with_error (Set.cardinal storage.owners >= storage.threshold) Errors.no_enough_owner in
let () = assert_with_error (storage.threshold > 0n) Errors.invalidated_threshold in
let () = assert_with_error (storage.effective_period > 0) Errors.invalid_effective_period in
()

[@inline]
let check_proposals_content (type a) (pack_from_input : bytes) (from_storage: (a proposal_content) list) : unit =
let check_proposals_content (from_input: bytes) (from_storage: proposal_content list) : unit =
let pack_from_storage = Bytes.pack from_storage in
assert_with_error (pack_from_input = pack_from_storage) Errors.not_the_same_content
assert_with_error (from_input = pack_from_storage) Errors.not_the_same_content

[@inline]
let within_expiration_time (created_timestamp: timestamp) (effective_period: effective_period) : unit =
assert_with_error (created_timestamp + effective_period > Tezos.get_now ()) Errors.pass_expiration_time
94 changes: 51 additions & 43 deletions src/internal/contract.mligo
Original file line number Diff line number Diff line change
Expand Up @@ -24,98 +24,106 @@
#import "storage.mligo" "Storage"
#import "conditions.mligo" "Conditions"
#import "execution.mligo" "Execution"
#import "event.mligo" "Event"

type parameter_types = Parameter.Types.t
type payload = Parameter.Types.payload
type challenge_id = Parameter.Types.challenge_id
type storage_types = Storage.Types.t
type storage_types_proposal = Storage.Types.proposal
type storage_types_proposal_state = Storage.Types.proposal_state
type effective_period = Storage.Types.effective_period
type proposal_content = Proposal_content.Types.t

type 'a request = 'a parameter_types * 'a storage_types
type 'a result = operation list * 'a storage_types
type request = parameter_types * storage_types
type result = operation list * storage_types


(**
* Default entrypoint
*)
let default (type a) (_, s : unit * a storage_types) : a result =
let event = Tezos.emit "%receiving_tez" (Tezos.get_sender (), Tezos.get_amount ()) in
let default (_, s : unit * storage_types) : result =
let event = Tezos.emit "%receiving_tez" ({ from = Tezos.get_sender (); amount = Tezos.get_amount (); } : Event.Types.receiving_tez) in
([event], s)

(**
* Proposal creation
*)
let create_proposal (type a) (proposal_content, storage : (a proposal_content) list * a storage_types) : a result =

let create_proposal (proposal_contents, storage : proposal_content list * storage_types) : result =
let () = Conditions.only_owner storage in
let () = Conditions.amount_must_be_zero_tez (Tezos.get_amount ()) in
let () = Conditions.not_empty_content proposal_content in
let proposal = Storage.Op.create_proposal proposal_content in
let () = Conditions.not_empty_content proposal_contents in
let proposal = Storage.Op.create_proposal proposal_contents in
let storage = Storage.Op.register_proposal(proposal, storage) in
let event = Tezos.emit "%create_proposal" (bytes storage.proposal_counter, proposal) in
let packed_proposal_contents = Bytes.pack proposal_contents in
let event = Tezos.emit "%create_proposal" ({ challenge_id = bytes storage.proposal_counter; payload = packed_proposal_contents } : Event.Types.create_proposal) in
([event], storage)

(**
* Proposal signature only
* Proposal Signing
*)

let sign_proposal (type a)
( proposal_id, proposal_content, agreement, storage
: Parameter.Types.proposal_id
* (a proposal_content) list
let sign_proposal
( challenge_id, proposal_content, agreement, storage
: challenge_id
* payload
* Parameter.Types.agreement
* a storage_types)
: a result =
* storage_types)
: result =
let () = Conditions.only_owner storage in
let () = Conditions.amount_must_be_zero_tez (Tezos.get_amount ()) in
let proposal = Storage.Op.retrieve_proposal(proposal_id, storage) in
let () = Conditions.unresolved proposal.state in
let proposal = Storage.Op.retrieve_proposal (challenge_id, storage) in
let () = Conditions.unsigned proposal in
let () = Conditions.within_expiration_time proposal.proposer.timestamp storage.effective_period in
let packed_proposal_content = Bytes.pack proposal_content in
let () = Conditions.check_proposals_content packed_proposal_content proposal.contents in
let () = Conditions.check_proposals_content proposal_content proposal.contents in
let owner = Tezos.get_sender () in
let proposal = Storage.Op.update_signature (proposal, owner, agreement) in
let storage = Storage.Op.update_proposal(proposal_id, proposal, storage) in
let event = Tezos.emit "%sign_proposal" (proposal_id, owner, agreement) in
let storage = Storage.Op.update_proposal(challenge_id, proposal, storage) in
let event = Tezos.emit "%sign_proposal" ({challenge_id; signer = owner; agreement } : Event.Types.sign_proposal) in
([event], storage)

(**
* Proposal Execution
*)

let resolve_proposal (type a)
( proposal_id, packed_proposal_content, storage
: Parameter.Types.proposal_id
* bytes
* a storage_types)
: a result =
let resolve_proposal
( challenge_id, proposal_content, storage
: challenge_id
* payload
* storage_types)
: result =
let () = Conditions.only_owner storage in
let () = Conditions.amount_must_be_zero_tez (Tezos.get_amount ()) in
let proposal = Storage.Op.retrieve_proposal(proposal_id, storage) in
let () = Conditions.unresolved proposal.state in
let () = Conditions.check_proposals_content packed_proposal_content proposal.contents in
let owner = Tezos.get_sender () in
let proposal = Storage.Op.retrieve_proposal(challenge_id, storage) in
let () = Conditions.check_proposals_content proposal_content proposal.contents in
let expiration_time = proposal.proposer.timestamp + storage.effective_period in
let proposal = Storage.Op.update_proposal_state (proposal, storage.owners, storage.threshold, expiration_time) in
let () = Conditions.ready_to_execute proposal.state in
let storage = Storage.Op.update_proposal(proposal_id, proposal, storage) in
let ops, proposal, storage = Execution.perform_operations proposal storage in
let storage = Storage.Op.update_proposal(proposal_id, proposal, storage) in
let event = Tezos.emit "%resolve_proposal" (proposal_id, owner) in
let poe = Tezos.emit "%proof_of_event" (proposal_id, packed_proposal_content) in
(poe::event::ops, storage)
let storage = Storage.Op.update_proposal(challenge_id, proposal, storage) in
let ops, storage = Execution.perform_operations challenge_id proposal storage in
let event = Tezos.emit "%resolve_proposal" ({ challenge_id = challenge_id; proposal_state = proposal.state } : Event.Types.resolve_proposal) in
(event::ops, storage)

(**
* Update Metadata
*)
let update_metadata (key, value, storage : string * bytes * storage_types) : result =
let s = Storage.Op.update_metadata (key, value, storage) in
([], s)

let contract (type a) (action, storage : a request) : a result =
let contract (action, storage : request) : result =
let ops, storage =
match action with
| Default u -> default (u, storage)
| Create_proposal (proposal_params) ->
create_proposal (proposal_params, storage)
| Sign_proposal (proposal_id, proposal_content, agreement) ->
sign_proposal (proposal_id, proposal_content, agreement, storage)
| Proof_of_event_challenge { challenge_id; payload; } ->
| Create_proposal { proposal_contents } ->
create_proposal (proposal_contents, storage)
| Sign_proposal { challenge_id; payload; agreement } ->
sign_proposal (challenge_id, payload, agreement, storage)
| Proof_of_event_challenge { challenge_id; payload } ->
resolve_proposal (challenge_id, payload, storage)
| Update_metadata { key; value } ->
update_metadata(key, value, storage)
in
let _ = Conditions.check_setting storage in
(ops, storage)
34 changes: 34 additions & 0 deletions src/internal/event.mligo
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
(* MIT License
Copyright (c) 2022 Marigold <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE. *)

#import "parameter.mligo" "Parameter"
#import "storage.mligo" "Storage"

module Types = struct

type challenge_id = Parameter.Types.challenge_id
type payload = Parameter.Types.payload
type agreement = Parameter.Types.agreement
type proposal_state = Storage.Types.proposal_state

type create_proposal = { challenge_id: challenge_id; payload: payload}
type sign_proposal = { challenge_id: challenge_id; signer: address; agreement: agreement}
type resolve_proposal = { challenge_id: challenge_id; proposal_state: proposal_state}
type proof_of_event = { challenge_id: challenge_id; payload: payload}
type receiving_tez = { from :address ; amount : tez}
end
Loading
Loading