Skip to content

Commit

Permalink
chore: allow updating uri regardles of this policy
Browse files Browse the repository at this point in the history
  • Loading branch information
wooglie committed Apr 8, 2024
1 parent 26e3b82 commit 2b487d4
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@
(use kip.token-policy-v2 [token-info])
(use marmalade-v2.guard-policy-v1 [URI-GUARD-MSG-KEY])

(defschema revealed-tokens-schema
revealed:bool
)

(deftable revealed-tokens:{revealed-tokens-schema})

(defcap TOKEN_REVEALED (token-id:string uri:string)
@doc "Emitted when the token URI has been revealed"
@event
Expand Down Expand Up @@ -90,18 +96,43 @@
( token:object{kip.token-policy-v2.token-info}
new-uri:string
)
true
)

(defun reveal-uri:bool (token-id:string new-uri:string)
(let* (
(token-id:string (at 'id token))
(token-info:object{kip.token-policy-v2.token-info} (marmalade-v2.ledger.get-token-info token-id))
(token-uri-hash:string (at 'uri token-info))
(already-revealed:bool (is-revealed token-id))
)
(enforce (not already-revealed) "Token URI already revealed")

(enforce (not (= new-uri "")) "URI cannot be empty")

(enforce (= token-uri-hash (hash new-uri)) "URI does not match the hash")

(marmalade-v2.ledger.update-uri token-id new-uri)

(emit-event (TOKEN_REVEALED token-id new-uri))

(insert revealed-tokens token-id { 'revealed: true })

true
)
)
)

(defun is-revealed:bool (token-id:string)
(with-default-read revealed-tokens token-id
{ 'revealed : false }
{ 'revealed := revealed }
revealed
)
)
)

(if (read-msg 'upgrade)
true
(create-table revealed-tokens)
)

(enforce-guard ADMIN-KS)
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,19 @@
,"token-id": (create-token-id { 'uri: (hash "ipfs://secret-uri"), 'precision: 0, 'policies: [marmalade-examples.private-token-policy-v1 marmalade-v2.guard-policy-v1] } ALWAYS-TRUE)
,"uri-guard": {"keys": ["e4c6807d79d8bf4695e10e5678ebf72862f59b71f971d39dd3349f4beeacd6e3"], "pred": "keys-all"}
})

(expect "token has not been revealed"
false
(is-revealed (read-msg 'token-id))
)

(expect-failure "fail if new URI is empty string"
"URI cannot be empty"
(update-uri (read-msg 'token-id) ""))
(reveal-uri (read-msg 'token-id) ""))

(expect-failure "fail if new URI is wrong"
"URI does not match the hash"
(update-uri (read-msg 'token-id) "ipfs://wrong-uri"))
(reveal-uri (read-msg 'token-id) "ipfs://wrong-uri"))

(env-sigs [
{ 'key: 'e4c6807d79d8bf4695e10e5678ebf72862f59b71f971d39dd3349f4beeacd6e3
Expand All @@ -97,15 +102,20 @@

(expect "successfully reveal the URI"
true
(update-uri (read-msg 'token-id) (read-msg 'secret-uri)))
(reveal-uri (read-msg 'token-id) (read-msg 'secret-uri)))

(expect "update uri events"
[{"name": "marmalade-examples.private-token-policy-v1.TOKEN_REVEALED","params": [(read-msg 'token-id) (read-msg 'secret-uri)]}
,{"name": "marmalade-v2.ledger.UPDATE-URI","params": [(read-msg 'token-id) (read-msg 'secret-uri)]}]
(expect "reveal uri events"
[{"name": "marmalade-v2.ledger.UPDATE-URI","params": [(read-msg 'token-id) (read-msg 'secret-uri)]}
,{"name": "marmalade-examples.private-token-policy-v1.TOKEN_REVEALED","params": [(read-msg 'token-id) (read-msg 'secret-uri)]} ]
(map (remove "module-hash") (env-events true)))

(expect "token has been revealed"
true
(is-revealed (read-msg 'token-id))
)

(expect-failure "cannot update the URI after revealing"
"URI does not match the hash"
(update-uri (read-msg 'token-id) "ipfs://something-new"))
(expect-failure "cannot reveal the URI again"
"Token URI already revealed"
(reveal-uri (read-msg 'token-id) "ipfs://something-new"))

(commit-tx)

0 comments on commit 2b487d4

Please sign in to comment.