chia-blockchain/chia/wallet/puzzles/did_innerpuz.clvm
matt-o-how 0096f755a1
DID Update Merge (#8129)
* fixed the bugs related to DID puzzles

* change test so that wallets recover into other wallets

* fix parent_info fetching when recovering

* fix did_test

* delete did tests related to singleton behaviours as that is tested elsewhere

* linting fixes

* update hash commit for did_innerpuz.clvm

* update DID wallet RPC calls

* more lint fixes

* delete further references to deprecated did_spend function

* fix bug in wallet state manager related to falsely detecting pool create

* added did_rpc test

* make sure amounts are uint64

* lint fixes

* Fix deadlock in DID wallet creation, and introduce create_new_did_wallet API call

* rename solution_to_pool_state

* Remove tests dir from packages

* added potential fix

* Allow getting unconfirmed balance from wallet_state_manager when under lock

* Remove a deadlock from create_new_did_wallet

* Update DID wallet test to use async check for farmed chia in wallet setup

* Fix unsigned arithmetic error

* Make DID wallet tests compatible with WalletStateManager lock checking

* check if removal belongs to the wallet

* unconfirmed

* did test cleanup

* fix temppuzhash to be an innerpuz

* clean up DID Wallet, add test for previously broken case

* added state_change call when coin added
added did_info for transaction sent

* update delete wallet parameters

* add comments to DID innerpuz

* fix duplicating bug with tx store

* re-enable did tests

* fix cc wallet bare raise

* remove unused assignation

* fix bare spend in did_wallet

* fix small bug

* messages are a cons box where the first value denotes type of message

* cc_wallet uses new parameter for get_confired_balance

* updates to the puzzle based upon suggestion by Richard

* update git submodule chia-blockchain-gui to did_branch

* updated gui to did_branch 76f9e6cea9f58a30984580b594631f3ae2679752

* updated gui to 041ac79be4

Co-authored-by: Adam Kelly <338792+aqk@users.noreply.github.com>
Co-authored-by: Yostra <straya@chia.net>
Co-authored-by: William Blanke <wjb98672@gmail.com>
2021-09-07 12:48:41 -07:00

163 lines
7.3 KiB
Plaintext

; The DID innerpuzzle is designed to sit inside the singleton layer and provide functionality related to being an identity.
; At the moment the two pieces of functionality are recovery and message creation.
; A DID's ID is it's Singleton ID
; Recovery is based around having a list of known other DIDs which can send messages approving you change the innerpuzzle of your DID singleton
(mod
(
MY_PUBKEY ; the public key of the owner used for signing transactions
RECOVERY_DID_LIST_HASH ; the list of DIDs that can send messages to you for recovery we store only the hash so that we don't have to reveal every time we make a message spend
NUM_VERIFICATIONS_REQUIRED ; how many of the above list are required for a recovery
Truths ; Truths are sent from the singleton layer
mode ; this indicates which spend mode we want. Create message, recover, or self-destruct
new_amount ; DIDs can receive payments so when we recreate ourselves sometimes we want to change our amount
message ; this is a list of messages when creating a message spend, or a new puzhash when recovering or self destructing
new_inner_puzhash ; this is used during the message creation spend to optionally give ourselves a new inner puzzle - this is useful for updating our recovery list
parent_innerpuzhash_amounts_for_recovery_ids ; during a recovery we need extra information about our recovery list coins
pubkey ; this is the new pubkey used for a recovery
recovery_list_reveal ; this is the reveal of the stored list of DIDs approved for recovery
)
;message is the new puzzle in the recovery and standard spend cases
;MOD_HASH, MY_PUBKEY, RECOVERY_DID_LIST_HASH are curried into the puzzle
;EXAMPLE SOLUTION (0xcafef00d 0x12341234 0x923bf9a7856b19d335a65f12d68957d497e1f0c16c0e14baf6d120e60753a1ce 2 1 100 (q "source code") 0xdeadbeef 0xcafef00d ((0xdadadada 0xdad5dad5 200) () (0xfafafafa 0xfaf5faf5 200)) 0xfadeddab (0x22222222 0x33333333 0x44444444))
(include condition_codes.clvm)
(include curry-and-treehash.clinc)
(include singleton_truths.clib)
; takes a lisp tree and returns the hash of it
(defun sha256tree1 (TREE)
(if (l TREE)
(sha256 2 (sha256tree1 (f TREE)) (sha256tree1 (r TREE)))
(sha256 1 TREE)
)
)
; recovery message module - gets values curried in to make the puzzle
(defun make_message_puzzle (recovering_coin newpuz pubkey)
(qq (q . (((unquote CREATE_COIN_ANNOUNCEMENT) (unquote recovering_coin)) ((unquote AGG_SIG_UNSAFE) (unquote pubkey) (unquote newpuz)))))
)
; this function creates the assert announcement for each message coin approving a recovery
(defun-inline create_consume_message (coin_id my_id new_innerpuz pubkey)
(list ASSERT_COIN_ANNOUNCEMENT (sha256 (sha256 coin_id (sha256tree1 (make_message_puzzle my_id new_innerpuz pubkey))) my_id))
)
; this function calculates a coin ID given the inner puzzle and singleton information
(defun create_coin_ID_for_recovery (SINGLETON_STRUCT launcher_id parent innerpuzhash amount)
(sha256 parent (calculate_full_puzzle_hash (c (f SINGLETON_STRUCT) (c launcher_id (r (r SINGLETON_STRUCT)))) innerpuzhash) amount)
)
;; return the full puzzlehash for a singleton with the innerpuzzle curried in
; puzzle-hash-of-curried-function is imported from curry-and-treehash.clinc
(defun-inline calculate_full_puzzle_hash (SINGLETON_STRUCT inner_puzzle_hash)
(puzzle-hash-of-curried-function (f SINGLETON_STRUCT)
inner_puzzle_hash
(sha256tree1 SINGLETON_STRUCT)
)
)
(defmacro create_new_coin (amount new_puz)
(qq (c CREATE_COIN (c (unquote new_puz) (c (unquote amount) ()))))
)
; this loops over our identities to check list, and checks if we have been given parent information for this identity
; the reason for this is because we might only require 3/5 of the IDs give approval messages for a recovery
; if we have the information for an identity then we create a consume message using that information
(defun check_messages_from_identities (SINGLETON_STRUCT num_verifications_required identities my_id output new_puz parent_innerpuzhash_amounts_for_recovery_ids pubkey num_verifications)
(if identities
(if (f parent_innerpuzhash_amounts_for_recovery_ids)
; if we have parent information then we should create a consume coin condition
(check_messages_from_identities
SINGLETON_STRUCT
num_verifications_required
(r identities)
my_id
(c
(create_consume_message
; create coin_id from DID
(create_coin_ID_for_recovery
SINGLETON_STRUCT
(f identities)
(f (f parent_innerpuzhash_amounts_for_recovery_ids))
(f (r (f parent_innerpuzhash_amounts_for_recovery_ids)))
(f (r (r (f parent_innerpuzhash_amounts_for_recovery_ids)))))
my_id
new_puz
pubkey)
output)
new_puz
(r parent_innerpuzhash_amounts_for_recovery_ids)
pubkey
(+ num_verifications 1)
)
; if no parent information found for this identity, move on to next in list
(check_messages_from_identities
SINGLETON_STRUCT
(r identities)
my_id
output
new_puz
(r parent_innerpuzhash_amounts_for_recovery_ids)
pubkey
num_verifications
)
)
;if we're out of identites to check for, return our output
(if (> num_verifications num_verifications_required)
(c (list AGG_SIG_UNSAFE pubkey new_puz) output)
(if (= num_verifications num_verifications_required)
(c (list AGG_SIG_UNSAFE pubkey new_puz) output)
(x)
)
)
)
)
; for a list of messages in the format (type . message) create a message
; type 0 is 0 value coin
; type 1 is coin announcement
; type 2 is puzzle announcement
(defun create_messages (messages)
(if messages
(c
(if (f (f messages))
(list (if (= (f (f messages)) 1) CREATE_COIN_ANNOUNCEMENT CREATE_PUZZLE_ANNOUNCEMENT) (r (f messages)))
(list CREATE_COIN (r (f messages)) 0)
)
(create_messages (r messages))
)
()
)
)
;Spend modes:
;0 = exit spend
;1 = create messages and recreate singleton
;2 = recovery
;MAIN
(if mode
(if (= mode 1)
; mode one - create messages and recreate singleton
(c (list CREATE_COIN new_inner_puzhash new_amount) (c (list AGG_SIG_ME MY_PUBKEY (sha256tree1 (list new_inner_puzhash new_amount message))) (create_messages message)))
; mode two - recovery
; check that recovery list is not empty
(if recovery_list_reveal
(if (= (sha256tree1 recovery_list_reveal) RECOVERY_DID_LIST_HASH)
(check_messages_from_identities (singleton_struct_truth Truths) NUM_VERIFICATIONS_REQUIRED recovery_list_reveal (my_id_truth Truths) (list (create_new_coin new_amount message)) message parent_innerpuzhash_amounts_for_recovery_ids pubkey 0)
(x)
)
(x)
)
)
; mode zero - exit spend
(list (list CREATE_COIN 0x00 -113) (list CREATE_COIN message new_amount) (list AGG_SIG_ME MY_PUBKEY (sha256tree1 (list new_amount message))))
)
)