-
Notifications
You must be signed in to change notification settings - Fork 17
Private token policy #195
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
Merged
Merged
Private token policy #195
Changes from 13 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
cd9b18c
feat: private token policy
wooglie 0ddf259
chore: added policy test to github actions
wooglie 26e3b82
chore: remove soul-bound-policy test from github actions
wooglie 2b487d4
chore: allow updating uri regardles of this policy
wooglie 9ee751e
chore: update documentation
wooglie 5ada6a4
chore: log the output to the console if the test failed
wooglie 5bfd024
chore: updated the repl gh action
wooglie 91ddee8
chore: disable updating uri before revealing
wooglie 64dee03
chore: updated the documentation
wooglie 1fd447c
fix: typo in the docs
wooglie cd85fa5
Merge branch 'feat/multi-chain' into feat/private-token-policy-2
wooglie f31ede2
Merge branch 'feat/multi-chain' into feat/private-token-policy-2
wooglie af7ecef
Merge branch 'main' into feat/private-token-policy-2
wooglie a63fb7d
chore: stringify policy when comparing
wooglie File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
140 changes: 140 additions & 0 deletions
140
examples/policies/private-token-policy/private-token-policy-v1.pact
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
(namespace (read-msg 'ns)) | ||
|
||
(module private-token-policy-v1 GOVERNANCE | ||
|
||
(defconst ADMIN-KS:string "marmalade-examples.private-token-policy") | ||
|
||
(defcap GOVERNANCE () | ||
(enforce-guard ADMIN-KS)) | ||
|
||
(implements kip.token-policy-v2) | ||
(implements kip.updatable-uri-policy-v1) | ||
(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 | ||
true | ||
) | ||
|
||
(defun has-guard-policy:bool (policies) | ||
(> (length (filter (lambda (policy) (= policy marmalade-v2.guard-policy-v1)) policies)) 0)) | ||
|
||
(defun enforce-init:bool | ||
( token:object{token-info} | ||
) | ||
|
||
(enforce (has-guard-policy (at 'policies token)) "Guard policy is required for private tokens") | ||
|
||
(read-msg URI-GUARD-MSG-KEY) | ||
|
||
true | ||
) | ||
|
||
(defun enforce-mint:bool | ||
( token:object{token-info} | ||
account:string | ||
guard:guard | ||
amount:decimal | ||
) | ||
true | ||
) | ||
|
||
(defun enforce-burn:bool | ||
( token:object{token-info} | ||
account:string | ||
amount:decimal | ||
) | ||
true | ||
) | ||
|
||
(defun enforce-offer:bool | ||
( token:object{token-info} | ||
seller:string | ||
amount:decimal | ||
timeout:integer | ||
sale-id:string ) | ||
true | ||
) | ||
|
||
(defun enforce-buy:bool | ||
( token:object{token-info} | ||
seller:string | ||
buyer:string | ||
buyer-guard:guard | ||
amount:decimal | ||
sale-id:string ) | ||
true | ||
) | ||
|
||
(defun enforce-withdraw:bool | ||
( token:object{token-info} | ||
seller:string | ||
amount:decimal | ||
timeout:integer | ||
sale-id:string ) | ||
true | ||
) | ||
|
||
(defun enforce-transfer:bool | ||
( token:object{token-info} | ||
sender:string | ||
guard:guard | ||
receiver:string | ||
amount:decimal ) | ||
true | ||
) | ||
|
||
(defun enforce-update-uri:bool | ||
( token:object{kip.token-policy-v2.token-info} | ||
new-uri:string | ||
) | ||
(let ((revealed:bool (is-revealed (at 'id token)))) | ||
(enforce revealed "Update disabled prior to revealing") | ||
) | ||
) | ||
|
||
(defun reveal-uri:bool (token-id:string new-uri:string) | ||
(let* ( | ||
(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") | ||
|
||
(insert revealed-tokens token-id { 'revealed: true }) | ||
|
||
(marmalade-v2.ledger.update-uri token-id new-uri) | ||
|
||
(emit-event (TOKEN_REVEALED token-id new-uri)) | ||
|
||
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) |
138 changes: 138 additions & 0 deletions
138
examples/policies/private-token-policy/private-token-policy-v1.repl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
;;load policy manager, ledger | ||
(load "../../../pact/marmalade.repl") | ||
|
||
(begin-tx "load policy") | ||
(env-data { | ||
"ns": "marmalade-examples" | ||
, "private-token-policy": ["private-token-policy"] | ||
, "upgrade": false} | ||
) | ||
(env-sigs [ | ||
{ 'key: 'private-token-policy | ||
,'caps: [] | ||
}]) | ||
|
||
(ns.write-registry (read-msg 'ns) (read-keyset 'private-token-policy) true) | ||
(define-namespace | ||
(read-msg 'ns) | ||
(read-keyset 'private-token-policy) (read-keyset 'private-token-policy) | ||
) | ||
|
||
(namespace (read-msg 'ns)) | ||
|
||
(define-keyset (+ (read-msg 'ns) ".private-token-policy") (read-keyset 'private-token-policy)) | ||
|
||
(load "private-token-policy-v1.pact") | ||
(typecheck "marmalade-examples.private-token-policy-v1") | ||
|
||
(commit-tx) | ||
|
||
(begin-tx "Require guard-policy") | ||
(use marmalade-v2.ledger) | ||
(use marmalade-examples.private-token-policy-v1) | ||
(use mini-guard-utils) | ||
|
||
(env-data { | ||
"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) | ||
,"token-id-without-guard-policy": (create-token-id { 'uri: (hash "ipfs://secret-uri"), 'precision: 0, 'policies: [marmalade-examples.private-token-policy-v1] } ALWAYS-TRUE) | ||
}) | ||
|
||
(expect-failure "Failed to create a token without guard-policy" | ||
"Guard policy is required for private tokens" | ||
(create-token (read-msg 'token-id-without-guard-policy) 0 (hash "ipfs://secret-uri") [marmalade-examples.private-token-policy-v1] ALWAYS-TRUE)) | ||
|
||
(expect-failure "Failed to create a token without uri-guard" | ||
"Failure: Tx Failed: No such key in message: uri_guard" | ||
(create-token (read-msg 'token-id) 0 (hash "ipfs://secret-uri") [marmalade-examples.private-token-policy-v1 marmalade-v2.guard-policy-v1] ALWAYS-TRUE)) | ||
|
||
(commit-tx) | ||
|
||
(begin-tx "Create private token") | ||
(use marmalade-v2.ledger) | ||
(use marmalade-examples.private-token-policy-v1) | ||
(use marmalade-v2.guard-policy-v1 [GUARD_SUCCESS]) | ||
(use mini-guard-utils) | ||
|
||
(env-data { | ||
"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 created successfully" | ||
true | ||
(create-token (read-msg 'token-id) 0 (hash "ipfs://secret-uri") [marmalade-examples.private-token-policy-v1 marmalade-v2.guard-policy-v1] ALWAYS-TRUE)) | ||
|
||
(expect "create-token events" | ||
[ {"name": "marmalade-v2.guard-policy-v1.GUARDS","params": [(read-msg 'token-id) {"burn-guard": GUARD_SUCCESS,"mint-guard": GUARD_SUCCESS,"sale-guard": GUARD_SUCCESS,"transfer-guard": GUARD_SUCCESS,"uri-guard":(read-keyset 'uri_guard)}] }, | ||
{"name": "marmalade-v2.ledger.TOKEN","params": [(read-msg 'token-id) 0 [marmalade-examples.private-token-policy-v1 marmalade-v2.guard-policy-v1] (hash "ipfs://secret-uri") ALWAYS-TRUE]}] | ||
(map (remove "module-hash") (env-events true))) | ||
|
||
(commit-tx) | ||
|
||
(begin-tx "Reveal private token URI") | ||
(use marmalade-v2.ledger) | ||
(use marmalade-examples.private-token-policy-v1) | ||
(use mini-guard-utils) | ||
|
||
(env-data { | ||
"secret-uri": "ipfs://secret-uri" | ||
,"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 "shoud not be able to update uri before revealing" | ||
"Update disabled prior to revealing" | ||
(marmalade-v2.ledger.update-uri (read-msg 'token-id) "") | ||
) | ||
|
||
(expect-failure "fail if new URI is empty string" | ||
"URI cannot be empty" | ||
(reveal-uri (read-msg 'token-id) "")) | ||
|
||
(expect-failure "fail if new URI is wrong" | ||
"URI does not match the hash" | ||
(reveal-uri (read-msg 'token-id) "ipfs://wrong-uri")) | ||
|
||
(env-sigs [ | ||
{ 'key: 'e4c6807d79d8bf4695e10e5678ebf72862f59b71f971d39dd3349f4beeacd6e3 | ||
,'caps: [ | ||
(marmalade-v2.ledger.UPDATE-URI (read-msg 'token-id) (read-msg 'secret-uri)) | ||
,(marmalade-v2.guard-policy-v1.UPDATE-URI (read-msg 'token-id) (read-msg 'secret-uri))] | ||
}]) | ||
|
||
(expect "successfully reveal the URI" | ||
true | ||
(reveal-uri (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 reveal the URI again" | ||
"Token URI already revealed" | ||
(reveal-uri (read-msg 'token-id) "ipfs://something-new")) | ||
|
||
(env-sigs [ | ||
{ 'key: 'e4c6807d79d8bf4695e10e5678ebf72862f59b71f971d39dd3349f4beeacd6e3 | ||
,'caps: [ | ||
(marmalade-v2.ledger.UPDATE-URI (read-msg 'token-id) "ipfs://updated") | ||
,(marmalade-v2.guard-policy-v1.UPDATE-URI (read-msg 'token-id) "ipfs://updated")] | ||
}]) | ||
|
||
(expect "shoud be able to update uri after revealing" | ||
true | ||
(marmalade-v2.ledger.update-uri (read-msg 'token-id) "ipfs://updated") | ||
) | ||
|
||
(commit-tx) |
47 changes: 47 additions & 0 deletions
47
examples/policies/private-token-policy/private-token-policy.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# Private token policy | ||
|
||
Private token policy allows creators to make an airdrop without revealing the metadata of the token beforehand. The token URI can be revealed at any time, making the metadata known to all. | ||
|
||
## Requirements: | ||
|
||
Concrete policy `guard-policy` must be used in conjunction with `private-token-policy` to make sure only an authorized account can update the token URI. | ||
|
||
While creating a token, the URI should be the hash of the actual URI. This can be calculated using a local call to the node so there is no trace recorded on the chain. | ||
|
||
Note: token URI can still be updated by the `uri-guard` but only after revealing the initial URI. | ||
|
||
## Specification, tables, capabilities, events: | ||
|
||
**Schemas**: `revealed-tokens-schema` is a schema that stores which tokens have been revealed | ||
- `revealed`: shows if the URI has been revealed. | ||
|
||
**Tables**: `revealed-tokens` table stores which tokens have been revealed. | ||
- `id`: the id of the token | ||
|
||
**Capabilities**: | ||
- `GOVERNANCE`: enforces access control of contract upgrades. | ||
|
||
**Events**: | ||
- `TOKEN_REVEALED (token-id uri)`: Emitted when the token URI has been revealed. | ||
|
||
## Policy Functions | ||
|
||
**`enforce-init`:** Enforced during `marmalade-v2.ledger.create-token`, and will ensure the concrete `guard-policy` is present along with the URI guard. | ||
|
||
**`enforce-mint`:** Enabled without limitation. | ||
|
||
**`enforce-burn`:** Enabled without limitation. | ||
|
||
**`enforce-offer`:** Enabled without limitation. | ||
|
||
**`enforce-buy`:** Enabled without limitation. | ||
|
||
**`enforce-withdraw`:** Enabled without limitation. | ||
|
||
**`enforce-transfer`:** Enabled without limitation. | ||
|
||
**`enforce-update-uri`:** Enforced during `marmalade-v2.ledger.update-uri`, and will allow updating only if the token has been revealed before. | ||
|
||
**`reveal-uri`:** Will make sure that the saved hash of the URI matches the hashed new URI and will invoke `marmalade-v2.ledger.update-uri`. | ||
|
||
**`is-revealed`:** Check if the URI has been revealed. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.