Skip to content

Instantly share code, notes, and snippets.

@thomashoneyman
Created May 9, 2024 00:01
Show Gist options
  • Save thomashoneyman/59d4d60d203d52d1a11dca57ef30502a to your computer and use it in GitHub Desktop.
Save thomashoneyman/59d4d60d203d52d1a11dca57ef30502a to your computer and use it in GitHub Desktop.
; We have to have a namespace to define a keyset in the repl...
(begin-tx)
(module guards GOV
(defcap GOV () true)
(defun success () true)
(defconst GUARD_SUCCESS (create-user-guard (success))))
(define-namespace 'test guards.GUARD_SUCCESS guards.GUARD_SUCCESS)
(print "Defined namespace: test")
(commit-tx)
; Then we can define our admin keyset
(begin-tx)
(namespace "test")
(env-data
{ "admin-keyset": { "keys": [ "admin-public-key" ], "pred": "keys-all" }
, "other-keyset": { "keys": [ "other-public-key" ], "pred": "keys-all" } })
(define-keyset "test.admin-keyset" (read-keyset "admin-keyset"))
(define-keyset "test.other-keyset" (read-keyset "other-keyset"))
(print "Defined keyset: test.admin-keyset, test.other-keyset")
(commit-tx)
(begin-tx)
(namespace "test")
(module source-contract GOV
(defcap GOV () (enforce-guard (keyset-ref-guard "test.admin-keyset")))
(defcap INTERNAL () true)
(defun private ()
(require-capability (INTERNAL))
true)
)
(commit-tx)
(begin-tx)
(namespace "test")
(module caller-good GOV
; NOTE: This governance is shared as the source contract, and therefore this
; module can call private functions from that contract.
(defcap GOV () (enforce-guard (keyset-ref-guard "test.admin-keyset")))
(defun call-private ()
(with-capability (test.source-contract.INTERNAL)
(test.source-contract.private)))
)
(commit-tx)
(begin-tx)
(namespace "test")
(module caller-bad GOV
; NOTE: This governance is not shared withthe source contract, and therefore
; this module cannot call private functions from that contract.
(defcap GOV () (enforce-guard (keyset-ref-guard "test.other-keyset")))
(defun call-private () (test.source-contract.private))
)
(commit-tx)
; Now we test!
(begin-tx)
; The private function is indeed private
(expect-failure
"Cannot call source-contract.private directly"
"require-capability: not granted: (test.source-contract.INTERNAL)"
(test.source-contract.private))
; Even if you attempt to acquire the INTERNAL cap, which can only be acquired
; by the source contract itself or someone able to acquire governance for it.
(env-sigs [ { "key": "other-public-key", "caps": [] } ])
(expect-failure
"Cannot acquire source-contract.INTERNAL outside the module or governance"
"require-capability: not granted: (test.source-contract.INTERNAL)"
(test.caller-bad.call-private))
; But if your contract shares governance with the source contract, then you can
; call whatever you want.
(env-sigs [ { "key": "admin-public-key", "caps": [] } ])
(expect
"Can acquire source-contract.INTERNAL and call private function if governance is shared"
true
(test.caller-good.call-private))
(commit-tx)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment