Skip to content
Closed
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
365 commits
Select commit Hold shift + click to select a range
81ebb9f
add ledger and witness docs
andrew-fleming Aug 13, 2025
c5417c1
Revert "Add contractAddress to nonce generation scheme"
emnul Aug 13, 2025
db5821f
Update hashing scheme
emnul Aug 13, 2025
98c86db
Update hash function in witness
emnul Aug 13, 2025
b556fad
improve computeOwnerId assertion
andrew-fleming Aug 15, 2025
a880f8d
use _computeOwnerId in assertOnlyOwner
andrew-fleming Aug 15, 2025
de69018
fix fmt
andrew-fleming Aug 15, 2025
92b46a8
add wrapAsEither circuits
andrew-fleming Aug 16, 2025
9fc5bbe
add option to compile directory in compact
andrew-fleming Aug 17, 2025
98b7a32
add granular compile scripts
andrew-fleming Aug 17, 2025
ae9e311
fix fmt
andrew-fleming Aug 17, 2025
4a9885b
use fast compilation prior to tests, cache tests
andrew-fleming Aug 17, 2025
6fb3f74
add circuit tag
andrew-fleming Aug 17, 2025
6161001
fix conflicts
andrew-fleming Aug 17, 2025
2ac92b5
move ZOwnablePK witness interface
andrew-fleming Aug 17, 2025
db87258
improve in-code docs
andrew-fleming Aug 18, 2025
9d78542
add ZOwnablePK api
andrew-fleming Aug 18, 2025
96f95c9
add withNonce to PS
andrew-fleming Aug 18, 2025
c16cd7d
add ZOwnablePK docs (less setup)
andrew-fleming Aug 18, 2025
3586158
improve sec prop section
andrew-fleming Aug 18, 2025
33148b0
fix fmt
andrew-fleming Aug 18, 2025
71ad93f
add usage section
andrew-fleming Aug 18, 2025
d707d28
revert changes
andrew-fleming Aug 18, 2025
e018e03
fix typo
andrew-fleming Aug 18, 2025
d113802
update readme with targeted compilation
andrew-fleming Aug 18, 2025
b3a0bd4
add k and rows
andrew-fleming Aug 24, 2025
f3e2872
add constraints to docs
andrew-fleming Aug 24, 2025
7b57e93
add SKIP_ZK env var to compile
andrew-fleming Aug 25, 2025
822fcd6
improve docs
andrew-fleming Aug 25, 2025
1c36f67
update README
andrew-fleming Aug 25, 2025
c676fd0
Apply suggestions from code review
andrew-fleming Aug 25, 2025
0cd3c59
change generic H to SHA256 in docs
andrew-fleming Aug 25, 2025
06f3c83
change generic H to SHA256
andrew-fleming Aug 25, 2025
8e28bda
fix lang version in mock
andrew-fleming Aug 25, 2025
9b3e97c
add bad owner id hash scenario
andrew-fleming Aug 26, 2025
35d95d3
Merge branch 'main' into shielded-access-control
emnul Aug 26, 2025
490af83
Apply suggestions from code review
andrew-fleming Aug 26, 2025
018ba5c
improve create proxy constraints, cast circuits to the extracted type…
andrew-fleming Aug 26, 2025
12a06c7
Apply suggestions from code review
andrew-fleming Aug 26, 2025
1b55a09
Remove old dir, rename files
emnul Aug 26, 2025
8e5dd6b
Apply suggestions from code review
andrew-fleming Aug 27, 2025
b100b9e
add AGPK section
andrew-fleming Aug 27, 2025
15aff68
fix guide links in access api, add agpk ref
andrew-fleming Aug 27, 2025
524dab9
add agpk recommendation
andrew-fleming Aug 27, 2025
7ba770d
WIP Experimental re-design
emnul Aug 27, 2025
a3e6a3a
add descriptions to circuits in sim
andrew-fleming Aug 27, 2025
a22ba6c
fix sim state mngr, improve docs
andrew-fleming Aug 27, 2025
37bb7aa
fix fmt
andrew-fleming Aug 27, 2025
02227ab
add non-deterministic sig warning
andrew-fleming Aug 27, 2025
b584c53
add period
andrew-fleming Aug 27, 2025
418a7b5
improve agpk principles
andrew-fleming Aug 27, 2025
bd18c19
improve clarity on 'never used elsewhere'
andrew-fleming Aug 27, 2025
acf3397
add wit_ prefix to witnesses
andrew-fleming Aug 28, 2025
8489924
Apply suggestions from code review
andrew-fleming Aug 28, 2025
67972b9
remove artifacts from inputs
andrew-fleming Aug 28, 2025
f23e56a
fix fmt
andrew-fleming Aug 28, 2025
efaa2a0
Apply suggestions from code review
andrew-fleming Aug 28, 2025
be5b418
Merge branch 'main' into improve-turbo-restructure
andrew-fleming Aug 28, 2025
a902206
Merge branch 'main' into shielded-access-control
emnul Aug 28, 2025
7ad70e6
Merge branch 'improve-turbo-restructure' into shielded-access-control
emnul Aug 28, 2025
eb3d4bc
rebase
andrew-fleming Aug 28, 2025
32a80ff
Merge branch 'add-ownable-shielded' of https://github.com/andrew-flem…
emnul Aug 28, 2025
365b7fd
Merge branch 'main' into shielded-access-control
emnul Aug 28, 2025
e48e6b8
Merge branch 'add-ownable-shielded' into shielded-access-control
emnul Aug 28, 2025
c16a6be
Construct simulator and witnesses for impl
emnul Aug 29, 2025
5882005
Merge branch 'main' into shielded-access-control
emnul Sep 2, 2025
9117fc6
Restrict usage to ZSwapCoinPubKeys, simplify logic
emnul Sep 2, 2025
e77d127
Update *.compact testing dependencies
emnul Sep 2, 2025
0c33611
Add helper fn, update API for improved flexibility
emnul Sep 2, 2025
e3cb30d
Use helper in witness impl
emnul Sep 2, 2025
95821e0
Init tests
emnul Sep 2, 2025
52000a2
Update simulator initialization
emnul Sep 2, 2025
3af7b9a
Update hashing scheme
emnul Sep 3, 2025
8d23061
Fix incorrect default MerkleTreePath construction
emnul Sep 3, 2025
47f5d3e
Improve typesafety of try catch block, add debugging logic
emnul Sep 3, 2025
c607923
Add initialization checks, correct role commitment checks
emnul Sep 3, 2025
dc5aed7
Merge branch 'main' into shielded-access-control
emnul Sep 3, 2025
4140f3b
Use correct MT API
emnul Sep 4, 2025
67f36aa
Add utility fn and improve logging
emnul Sep 4, 2025
99fa0be
add test
emnul Sep 4, 2025
4b1ff86
Fix typo in filename
emnul Sep 8, 2025
9a735b2
Update imports
emnul Sep 8, 2025
2f375ba
Update witness fn signatures
emnul Sep 8, 2025
a5a1763
Update constructor, witnesses setter
emnul Sep 8, 2025
353b379
Add bad index tests
emnul Sep 8, 2025
1fa7122
Merge branch 'main' into shielded-access-control
emnul Sep 8, 2025
26d3576
Update Role field name
emnul Sep 8, 2025
da274c4
Add tests
emnul Sep 8, 2025
7ae6407
Remove _unsafeGrantRole
emnul Sep 8, 2025
5a1b0b4
Improve tests
emnul Sep 8, 2025
923d779
Add helper method
emnul Sep 8, 2025
07dd40b
Change privateState fn signatures
emnul Sep 9, 2025
fdcf13c
Add tests
emnul Sep 9, 2025
b08a600
Merge branch 'main' into shielded-access-control
emnul Sep 11, 2025
7e6a6a0
Should not throw if commitment in nullifer set
emnul Sep 11, 2025
8e5c26f
Export nullifiers for testing
emnul Sep 11, 2025
dd0cd82
Rename var and change return behavior
emnul Sep 11, 2025
f24eeb0
Add _checkRole, grantRole tests
emnul Sep 11, 2025
2cb227e
fmt files
emnul Sep 11, 2025
061ac4a
WIP
emnul Sep 12, 2025
a98144e
Update contracts/src/access/witnesses/ShieldedAccessControlWitnesses.ts
emnul Sep 15, 2025
9d9c256
Optimize loop
emnul Sep 15, 2025
52936ef
Merge branch 'shielded-access-control' of https://github.com/OpenZepp…
emnul Sep 15, 2025
5d2da06
Refactor Shielded Design
emnul Sep 17, 2025
713d3d4
Move resetProxy call to end of fn
emnul Sep 17, 2025
8a5fbba
Fixes incorrect indexing bug
emnul Sep 17, 2025
2fce76c
WIP refactor tests
emnul Sep 17, 2025
306ec1f
Merge branch 'main' into shielded-access-control
emnul Sep 17, 2025
2fb86e7
Fix compiler errors, refactor mock and witnesses
emnul Sep 17, 2025
a2e644d
Refactor test suite
emnul Sep 19, 2025
16928c3
Refactor simulator for new design
emnul Sep 19, 2025
740bb83
WIP
emnul Sep 25, 2025
a6feeff
Remove old
emnul Feb 24, 2026
ccdbe85
Merge branch 'main' into shielded-access-control
emnul Feb 24, 2026
7df0378
Remove unused file
emnul Feb 24, 2026
a6f0eaa
WIP
emnul Feb 24, 2026
dc5001f
Use standard insert method over insertHash
emnul Feb 25, 2026
5a817c9
Update circuit docs
emnul Feb 26, 2026
88355c8
Improve naming, remove dead code, narrow circuit responsibility
emnul Feb 26, 2026
266e0fd
Update module docs
emnul Feb 26, 2026
efdf636
Add nominal type aliases
emnul Feb 26, 2026
1186e51
Refactor witness file
emnul Feb 26, 2026
e819d8d
Change prefix
emnul Feb 26, 2026
1b59084
Refactor simulator
emnul Feb 26, 2026
aef1ed3
Update error message
emnul Feb 26, 2026
1f3349d
rename param
emnul Feb 26, 2026
d4f6b8d
Refactor tests
emnul Feb 26, 2026
e67fc4a
Add Initializable
emnul Feb 27, 2026
01153f0
Refactor sim
emnul Feb 27, 2026
1a3bb3d
improve naming
emnul Feb 27, 2026
bf99e94
WIP refactor tests
emnul Feb 27, 2026
16b0ecd
Add helper methods to sim
emnul Feb 27, 2026
5ddbe1e
Enforce single use role commitments
emnul Feb 28, 2026
f91d346
WIP tests
emnul Feb 28, 2026
b541593
Refactor docs
emnul Feb 28, 2026
13d142c
move disclosure closer to disclosure point
emnul Mar 1, 2026
a5268f6
Reduce metadata leakage
emnul Mar 2, 2026
6a4d55e
Update witness file
emnul Mar 2, 2026
82170b8
Export circuit from Mock
emnul Mar 2, 2026
f3279b4
Update simulator
emnul Mar 2, 2026
0be3e5c
Update method name, assert Initialized in computeAccountId
emnul Mar 2, 2026
5c67ac8
fmt file
emnul Mar 2, 2026
9601c4d
Add assertion to _checkRole
emnul Mar 2, 2026
8053d17
fmt file
emnul Mar 2, 2026
287f7bb
Update default Merkle tree value
emnul Mar 2, 2026
8aedd88
Simplify return statement
emnul Mar 3, 2026
d80091b
fmt files
emnul Mar 3, 2026
d3fa7f7
reorganize code, inline auth logic in _grant/revokeRole
emnul Mar 3, 2026
3a016b1
Update circuit info
emnul Mar 4, 2026
3bb9e4c
WIP commit
emnul Mar 5, 2026
e3cf7e9
Revert "WIP commit"
emnul Mar 5, 2026
1c5f803
Reapply "WIP commit"
emnul Mar 5, 2026
8d9713a
WIP commit
emnul Mar 5, 2026
ae13104
Merge branch 'main' into shielded-access-control
pepebndc Mar 6, 2026
ecbe0da
Add tests for shielded access control
pepebndc Mar 6, 2026
a34d7f5
Merge branch 'shielded-access-control' of https://github.com/OpenZepp…
emnul Mar 6, 2026
d081342
Assert instance salt is not 0
emnul Mar 7, 2026
dc8b522
Merge remote-tracking branch 'origin/feat/tests-shielded-access-contr…
emnul Mar 8, 2026
154938e
Apply suggestions from code review
emnul Mar 8, 2026
c7b3d2a
Merge branch 'shielded-access-control' of https://github.com/OpenZepp…
emnul Mar 8, 2026
3cfa7af
strange compact compiler bug
emnul Mar 8, 2026
a5fe6d1
add tests
emnul Mar 8, 2026
ac33519
update assertion message
emnul Mar 8, 2026
12818f8
Sync mock implementation
emnul Mar 9, 2026
e5b402d
update tests
emnul Mar 9, 2026
a10b9bb
big refactor
emnul Mar 10, 2026
09e4a21
Remove unused witness
emnul Mar 10, 2026
813fa99
update simulator
emnul Mar 10, 2026
36a6d0a
refactor tests
emnul Mar 10, 2026
f2ee664
Update description
emnul Mar 10, 2026
1a74761
Update test
emnul Mar 10, 2026
67b85d1
Refactor contract
emnul Mar 11, 2026
8fefb4e
Refactor simulator
emnul Mar 11, 2026
e8ba943
refactor: harden the shieldedaccesscontrol lib by some improvements a…
0xisk Mar 11, 2026
81002bf
Revert "refactor: harden the shieldedaccesscontrol lib by some improv…
0xisk Mar 11, 2026
d67a369
fmt files, add export to mock
emnul Mar 11, 2026
7546ffd
Add additional documentation
emnul Mar 11, 2026
5f654fc
remove unused types
emnul Mar 11, 2026
ac1077a
Add tests and lint files
emnul Mar 11, 2026
bec51ca
Merge branch 'shielded-access-control' of https://github.com/OpenZepp…
emnul Mar 11, 2026
4757096
fmt file
emnul Mar 11, 2026
187f23f
add test
emnul Mar 12, 2026
593bcc0
refactor files: roleId -> role
emnul Mar 12, 2026
b775993
rm old tests
emnul Mar 12, 2026
4e59f2b
Merge branch 'main' into shielded-access-control
pepebndc Mar 12, 2026
196843e
Simplify internal circuit signature
emnul Mar 12, 2026
036c66c
Merge branch 'shielded-access-control' of https://github.com/OpenZepp…
emnul Mar 12, 2026
1900f04
Remove initialization checks from internal circuits
emnul Mar 12, 2026
fcb9a16
Revert "Remove initialization checks from internal circuits"
emnul Mar 12, 2026
fbe1df8
Reapply "Remove initialization checks from internal circuits"
emnul Mar 12, 2026
80dd34d
Update docs reorganize code
emnul Mar 13, 2026
d38c6dc
Add unchecked circuits
emnul Mar 13, 2026
1b6c2a5
remove init checks from get / set admin circuits
emnul Mar 13, 2026
819c821
fix circuit def
emnul Mar 13, 2026
15a113b
Add unexported circuits to mock
emnul Mar 13, 2026
8f536e0
Fmt Mock
emnul Mar 13, 2026
452c2d9
Add _uncheckedCircuits to sim, tests, fix mock export
emnul Mar 13, 2026
d989f34
Remove admin identifier
emnul Mar 13, 2026
ac4853d
Update docs
emnul Mar 13, 2026
c7d06c0
remove ledger artifact from wit, update sim
andrew-fleming Mar 14, 2026
7e27f80
remove ledger artifact from wit, update sim in zownable
andrew-fleming Mar 14, 2026
7ea82e9
update sim readme
andrew-fleming Mar 14, 2026
c74f42f
add changelog entry
andrew-fleming Mar 14, 2026
f802a54
improve witness docs
andrew-fleming Mar 14, 2026
bcb4ce1
update changelog
andrew-fleming Mar 14, 2026
1b4f5c3
Merge branch 'main' into remove-artifact-ledger-from-witnesses
andrew-fleming Mar 16, 2026
72ed9a5
refactor: test should compare circuit result not callback
emnul Mar 16, 2026
bbbdba9
refactor: test should compare root value not object
emnul Mar 16, 2026
9ed5fec
refactor: DEFAULT_ADMIN_ROLE into pure circuit
emnul Mar 16, 2026
85c71d9
Update mock, sim, and tests with DEFAULT_ADMIN_ROLE circuit
emnul Mar 16, 2026
bc2d635
Update type
emnul Mar 16, 2026
dbdff4c
Update contracts/src/access/witnesses/ShieldedAccessControlWitnesses.ts
emnul Mar 16, 2026
b407b7c
Add matcher for assertion
emnul Mar 16, 2026
67b4753
Merge branch 'shielded-access-control' of https://github.com/OpenZepp…
emnul Mar 16, 2026
6d6e064
Throw error if role nonce is undefined
emnul Mar 17, 2026
7e5f144
Add README to contracts directory
emnul Mar 17, 2026
e001b10
Add disclaimer to all Mock files
emnul Mar 17, 2026
dc3737e
Update contracts/src/access/ShieldedAccessControl.compact
emnul Mar 17, 2026
7c1427a
Update circuit requirements
emnul Mar 17, 2026
99b18c6
Refactor: Remove _uncheckedAssert, keep _grant / _revoke DRY
emnul Mar 17, 2026
75e6dcb
Refactor: remove references to _uncheckedAssert
emnul Mar 17, 2026
72fa7b3
Remove warning from pure circuit
emnul Mar 17, 2026
b2defa9
revert changes to simulator test utils
emnul Mar 17, 2026
79ad5a3
rename circuit
emnul Mar 17, 2026
944d251
Update circuitInfo
emnul Mar 17, 2026
9c86e1a
Remove tests
emnul Mar 17, 2026
8c6ad6d
Merge branch 'main' into shielded-access-control
emnul Mar 17, 2026
e7f5be1
Merge branch 'remove-artifact-ledger-from-witnesses' into shielded-ac…
emnul Mar 17, 2026
bc1ded2
Use generic witness pattern
emnul Mar 17, 2026
748ee62
Add interface to computeAccountId locally, export _computeNullifier a…
emnul Mar 18, 2026
a7566b1
Update tests and simulator
emnul Mar 18, 2026
d24eae9
Remove README
emnul Mar 18, 2026
7b2ef89
Merge branch 'main' into shielded-access-control
emnul Mar 18, 2026
8c8ea72
Update contracts/src/access/ShieldedAccessControl.compact
emnul Mar 18, 2026
c5b34f6
Update contracts/src/access/ShieldedAccessControl.compact
emnul Mar 18, 2026
38b4696
Update contracts/src/access/ShieldedAccessControl.compact
emnul Mar 18, 2026
cc56b39
Update contracts/src/access/ShieldedAccessControl.compact
emnul Mar 18, 2026
dbc85f4
update _setRoleAdmin docs
emnul Mar 18, 2026
8a21058
Merge branch 'shielded-access-control' of https://github.com/OpenZepp…
emnul Mar 18, 2026
7729e67
Update circuit _computeNull and _computeRoleCom names
emnul Mar 18, 2026
9bd47ea
Refactor names in tests / sim
emnul Mar 18, 2026
28cc91e
Refactor _computeAccountId to depend on pure variant, rename pure var…
emnul Mar 18, 2026
ea50c07
Update error message
emnul Mar 19, 2026
9d7f148
Merge branch 'main' into shielded-access-control
0xisk Mar 23, 2026
00d521b
chore: add warning for the exported grant and revoke functions
0xisk Mar 23, 2026
dcf6e7e
chore(access): same ordering everywhere (#410)
0xisk Mar 27, 2026
3c41f95
refactor(access): adding more test cases for shielded access control …
0xisk Mar 27, 2026
6c3f220
Merge branch 'main' into shielded-access-control
andrew-fleming Mar 27, 2026
a7a9aa4
Refactor shielded access (#412)
andrew-fleming Mar 31, 2026
0366037
Merge branch 'main' into shielded-access-control
andrew-fleming Apr 6, 2026
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
32 changes: 32 additions & 0 deletions contracts/shieldedAccessControl/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"name": "@openzeppelin-compact/shielded-access-control",
"private": true,
"type": "module",
"main": "dist/index.js",
"module": "dist/index.js",
"types": "./dist/index.d.ts",
"exports": {
".": {
"types": "./dist/index.d.ts",
"require": "./dist/index.js",
"import": "./dist/index.js",
"default": "./dist/index.js"
}
},
"scripts": {
"compact": "compact-compiler",
"build": "compact-builder && tsc",
"test": "vitest run",
"types": "tsc -p tsconfig.json --noEmit",
"clean": "git clean -fXd"
},
"dependencies": {
"@openzeppelin-compact/compact": "workspace:^"
},
"devDependencies": {
"@types/node": "22.14.0",
"ts-node": "^10.9.2",
"typescript": "^5.2.2",
"vitest": "^3.1.3"
}
}
659 changes: 659 additions & 0 deletions contracts/shieldedAccessControl/src/ShieldedAccessControl.compact

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// SPDX-License-Identifier: MIT

pragma language_version >= 0.16.0;

/**
* @module ShieldedAccessControlUtils.
* @description A library for common utilities used in the Shielded Access Control module.
*/
module ShieldedAccessControlUtils {
import CompactStandardLibrary;

/**
* @description Returns whether `keyOrAddress` is a ContractAddress type.
*
* Disclosures:
*
* - The type data of `keyOrAddress` - a ZswapCoinPublicKey or ContractAddress.
*
* @param {Either<ZswapCoinPublicKey, ContractAddress>} keyOrAddress - The target value to check, either a ZswapCoinPublicKey or a ContractAddress.
* @return {Boolean} - Returns true if `keyOrAddress` is a ContractAddress.
*/
export pure circuit isContractAddress(keyOrAddress: Either<ZswapCoinPublicKey, ContractAddress>): Boolean {
return disclose(!keyOrAddress.is_left);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// SPDX-License-Identifier: MIT

pragma language_version >= 0.16.0;

import CompactStandardLibrary;

import "../../ShieldedAccessControl" prefix ShieldedAccessControl_;

export {
ZswapCoinPublicKey,
ContractAddress,
Either,
Maybe,
ShieldedAccessControl_DEFAULT_ADMIN_ROLE,
ShieldedAccessControl__salt,
ShieldedAccessControl__operatorRoles
};

export circuit hasRole(roleId: Bytes<32>, account: Either<ZswapCoinPublicKey, ContractAddress>, nonce: Bytes<32>): Boolean {
return ShieldedAccessControl_hasRole(roleId, account, nonce);
}

export circuit assertOnlyRole(roleId: Bytes<32>, nonce: Bytes<32>): [] {
ShieldedAccessControl_assertOnlyRole(roleId, nonce);
}

export circuit _checkRole(roleId: Bytes<32>, account: Either<ZswapCoinPublicKey, ContractAddress>, nonce: Bytes<32>): [] {
ShieldedAccessControl__checkRole(roleId, account, nonce);
}

export circuit _checkMerkleTree(roleId: Bytes<32>, account: Bytes<32>, nonce: Bytes<32>): Boolean {
return ShieldedAccessControl__checkMerkleTree(roleId, account, nonce);
}

export circuit getRoleAdmin(roleId: Bytes<32>): Bytes<32> {
return ShieldedAccessControl_getRoleAdmin(roleId);
}

export circuit grantRole(roleId: Bytes<32>, account: Either<ZswapCoinPublicKey, ContractAddress>, nonce: Bytes<32>): [] {
ShieldedAccessControl_grantRole(roleId, account, nonce);
}

export circuit revokeRole(roleId: Bytes<32>, account: Either<ZswapCoinPublicKey, ContractAddress>, nonce: Bytes<32>): [] {
ShieldedAccessControl_revokeRole(roleId, account, nonce);
}

export circuit renounceRole(roleId: Bytes<32>, callerConfirmation: Either<ZswapCoinPublicKey, ContractAddress>, nonce: Bytes<32>): [] {
ShieldedAccessControl_renounceRole(roleId, callerConfirmation, nonce);
}

export circuit _setRoleAdmin(roleId: Bytes<32>, adminRole: Bytes<32>): [] {
ShieldedAccessControl__setRoleAdmin(roleId, adminRole);
}

export circuit _grantRole(roleId: Bytes<32>, account: Either<ZswapCoinPublicKey, ContractAddress>, nonce: Bytes<32>): Boolean {
return ShieldedAccessControl__grantRole(roleId, account, nonce);
}

export circuit _unsafeGrantRole(roleId: Bytes<32>, account: Either<ZswapCoinPublicKey, ContractAddress>, nonce: Bytes<32>): Boolean {
return ShieldedAccessControl__unsafeGrantRole(roleId, account, nonce);
}

export circuit _revokeRole(roleId: Bytes<32>, account: Either<ZswapCoinPublicKey, ContractAddress>, nonce: Bytes<32>): Boolean {
return ShieldedAccessControl__revokeRole(roleId, account, nonce);
}

export circuit _requestRole(roleId: Bytes<32>): [] {
ShieldedAccessControl__requestRole(roleId);
}

export circuit _recoverRoles(): [] {
ShieldedAccessControl__recoverRoles();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
import { Buffer } from 'node:buffer';
import {
constructorContext,
decodeCoinPublicKey,
type MerkleTreePath,
QueryContext,
type WitnessContext,
} from '@midnight-ntwrk/compact-runtime';
import { encodeContractAddress } from '@midnight-ntwrk/ledger';
import { sampleContractAddress } from '@midnight-ntwrk/zswap';
import {
type ContractAddress,
type Either,
type Ledger,
Contract as MockShieldedAccessControl,
type ZswapCoinPublicKey,
} from '../artifacts/MockShieldedAccessControl/contract/index.cjs'; // Combined imports

const { hkdfSync } = await import('node:crypto');

const KEYLEN = 32;

/**
* @description The respective `nonce` value for a given `roleId` should be at the same index
* for each array of `Buffer`s
*/
export type ShieldedAccessControlPrivateState = {
secretKey: Buffer;
nonces: Buffer[];
roleIds: Buffer[];
};

/**
* @description Generates a nonce value using the following scheme: HKDF-SHA256(SK, "role-nonce" | roleId | PK)
* @param secretKey - The secret key associated with the contract.
* @param roleId - The role identifier.
* @param salt - A salt value.
* @param account - The public key of an account.
*
* @returns A unique nonce value for `roleId`
Comment thread
0xisk marked this conversation as resolved.
Outdated
*/
function generateNonce(
secretKey: Buffer,
roleId: Buffer,
salt: Buffer,
account: Buffer,
): Buffer {
const domainString = Buffer.from('role-nonce');
const info = Buffer.concat([domainString, roleId, account]);
const nonce = hkdfSync('sha512', secretKey, salt, info, KEYLEN);
Comment thread
0xisk marked this conversation as resolved.
Outdated

return Buffer.from(nonce);
}

/**
* @description A stub function that simulates a successful role approval
* @param account - The public key of an account.
* @param roleId - The role identifier.
* @param nonce - The nonce associated with `roleId`.
*
* @returns Whether the account was approved for a role
*/
function sendRoleRequestToAdmin(
_account: Buffer,
_roleId: Buffer,
_nonce: Buffer,
) {
return true;
}

export const ShieldedAccessControlWitnesses = {
/**
* @description Typescript implementation of the `getRoleCommitmentPath` witness function.
* @param privateState - The current private state.
* @param ledger - A snapshot of the current ledger state.
* @param roleCommitment - The role commitment to query.
* @param index - The index of `roleCommitment`in the Merkle tree.
*
* @returns An array of the private state and the Merkle tree path of `roleCommitment`
* in the `_operatorRoles` Merkle tree.
*/
getRoleCommitmentPath: (
{
ledger,
privateState,
}: WitnessContext<Ledger, ShieldedAccessControlPrivateState>,
roleCommitment: Uint8Array,
index: bigint,
): [ShieldedAccessControlPrivateState, MerkleTreePath<Uint8Array>] => {
const merkleTreePath =
ledger.ShieldedAccessControl__operatorRoles.pathForLeaf(
index,
roleCommitment,
);
return [privateState, merkleTreePath];
},
/**
* @description Typescript implementation of the `recoverNonce` witness function. Simulates calls to the `hasRole` circuit
* to determine if the account has the specified role. Updates the private state with any found roles.
* @param privateState - The current private state.
* @param ledger - A snapshot of the current ledger state.
* @param contractAddress - The address of the contract.
* @param account - The public key associated with a role.
* @param salt - A salt value.
*
* @returns An array of the new private state and the empty tuple
*/
recoverRoles: (
{
ledger,
privateState,
contractAddress,
}: WitnessContext<Ledger, ShieldedAccessControlPrivateState>,
account: Uint8Array,
salt: Uint8Array,
): [ShieldedAccessControlPrivateState, []] => {
const roles = [ledger.ShieldedAccessControl_DEFAULT_ADMIN_ROLE];
const coinPubKey = decodeCoinPublicKey(account);
const newPrivateState: ShieldedAccessControlPrivateState = {
secretKey: privateState.secretKey,
roleIds: [],
nonces: [],
};

const contract =
new MockShieldedAccessControl<ShieldedAccessControlPrivateState>(
ShieldedAccessControlWitnesses,
);
const {
currentPrivateState,
currentContractState,
currentZswapLocalState,
} = contract.initialState(
constructorContext(
{ secretKey: privateState.secretKey, nonces: [], roleIds: [] },
coinPubKey,
),
);
const circuitContext = {
currentPrivateState,
currentZswapLocalState,
originalState: currentContractState,
transactionContext: new QueryContext(
currentContractState.data,
contractAddress,
),
};

for (let i = 0; i < roles.length; i++) {
const role = roles[i];
const nonce = generateNonce(
privateState.secretKey,
Buffer.from(role),
Buffer.from(salt),
Buffer.from(account),
);
const eitherAccount: Either<ZswapCoinPublicKey, ContractAddress> = {
is_left: true,
left: { bytes: account },
right: { bytes: encodeContractAddress(sampleContractAddress()) },
};

try {
const hasRole = contract.impureCircuits.hasRole(
circuitContext,
role,
eitherAccount,
nonce,
);
if (hasRole) {
newPrivateState.nonces.push(nonce);
newPrivateState.roleIds.push(Buffer.from(role));
}
} catch (err) {
console.log(err);
}
}

return [newPrivateState, []];
},
/**
* @description Typescript implementation of the `requestRole` witness function.
* @param privateState - The current private state.
* @param roleId - The role identifier.
* @param account - The public key requesting a role.
* @param salt - A salt value.
*
* @returns An array of the new private state and an empty array
*/
requestRole: (
{ privateState }: WitnessContext<Ledger, ShieldedAccessControlPrivateState>,
roleId: Uint8Array,
account: Uint8Array,
salt: Uint8Array,
): [ShieldedAccessControlPrivateState, []] => {
const saltBuff = Buffer.from(salt);
const roleIdBuff = Buffer.from(roleId);
const accountBuff = Buffer.from(account);
const nonce = generateNonce(
privateState.secretKey,
roleIdBuff,
saltBuff,
accountBuff,
);
const isApproved = sendRoleRequestToAdmin(accountBuff, roleIdBuff, nonce);

if (isApproved) {
privateState.nonces.push(nonce);
privateState.roleIds.push(roleIdBuff);
}

return [privateState, []];
},
};
5 changes: 5 additions & 0 deletions contracts/shieldedAccessControl/tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"extends": "./tsconfig.json",
"exclude": ["src/test/**/*.ts"],
"compilerOptions": {}
}
25 changes: 25 additions & 0 deletions contracts/shieldedAccessControl/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"include": [
"src/**/*.ts"
],
"compilerOptions": {
"rootDir": "src",
"outDir": "dist",
"declaration": true,
"lib": [
"ES2022"
],
"target": "ES2022",
"module": "nodenext",
"moduleResolution": "nodenext",
"allowJs": true,
"forceConsistentCasingInFileNames": true,
"noImplicitAny": true,
"strict": true,
"isolatedModules": true,
"sourceMap": true,
"resolveJsonModule": true,
"esModuleInterop": true,
"skipLibCheck": true
}
}
10 changes: 10 additions & 0 deletions contracts/shieldedAccessControl/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { defineConfig } from 'vitest/config';

export default defineConfig({
test: {
globals: true,
environment: 'node',
include: ['src/test/**/*.test.ts'],
reporters: 'verbose',
},
});
Empty file.
Empty file.
Loading
Loading