Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
227 commits
Select commit Hold shift + click to select a range
bccf098
bump midnight-js
andrew-fleming Apr 16, 2025
780132e
remove unused deps
andrew-fleming Apr 16, 2025
3419a25
remove eslint and prettier, add biome
andrew-fleming Apr 16, 2025
45a2563
run fmt
andrew-fleming Apr 16, 2025
fec71f5
run lint
andrew-fleming Apr 16, 2025
86565b3
organize imports
andrew-fleming Apr 16, 2025
24c2e78
remove dev deps from contracts/
andrew-fleming Apr 17, 2025
82e64e5
remove unused deps
andrew-fleming Apr 17, 2025
ebb2728
update turbo and scripts
andrew-fleming Apr 17, 2025
cd757c0
add clean script
andrew-fleming Apr 17, 2025
a5fb60b
add clean script
andrew-fleming Apr 17, 2025
b4fb0df
clean up test config
andrew-fleming Apr 18, 2025
3462103
remove jest reporters
andrew-fleming Apr 18, 2025
de6ab59
bump turbo to 2.5
andrew-fleming Apr 18, 2025
7c211d7
bump turbo
andrew-fleming Apr 24, 2025
5cfcf80
fix package names
andrew-fleming Apr 25, 2025
f49e2d3
fix conflicts
andrew-fleming Apr 26, 2025
f23d40b
set up compact compiler and builder
andrew-fleming Apr 27, 2025
69e86c1
update scripts
andrew-fleming Apr 27, 2025
065f48b
update yarn.lock
andrew-fleming Apr 27, 2025
dfef7ec
update readme
andrew-fleming Apr 27, 2025
d4678fd
fix fmt
andrew-fleming Apr 28, 2025
8cdd29e
fix conflicts
andrew-fleming Apr 29, 2025
6135d9c
fix fmt
andrew-fleming Apr 30, 2025
8bbfce3
remove lingering file
andrew-fleming Apr 30, 2025
7634d77
update biome, fix fmt
andrew-fleming Apr 30, 2025
18ab0f3
add requirements in dev section
andrew-fleming Apr 30, 2025
bedff4f
add devdeps to contracts packages
andrew-fleming Apr 30, 2025
dc7ae8f
simplify workspace
andrew-fleming May 1, 2025
ff967e5
remove unnecessary button rule
andrew-fleming May 1, 2025
8ce9ac0
fix fmt
andrew-fleming May 1, 2025
f869fac
remove useExhaustiveDeps rule
andrew-fleming May 1, 2025
d0995ce
add contract template
andrew-fleming May 3, 2025
efef77a
start ownable shielded
andrew-fleming May 7, 2025
e13bdc8
fix pk circuit
andrew-fleming May 7, 2025
f41efd5
add custom witness context
andrew-fleming May 7, 2025
67b9035
add setWitnessContext method
andrew-fleming May 7, 2025
565449b
add ownable tests
andrew-fleming May 7, 2025
98ffd94
add ownablePK
andrew-fleming May 12, 2025
7a73e92
clean up mock and sim
andrew-fleming May 12, 2025
60de48e
fix import
andrew-fleming May 12, 2025
bf94cde
fix fmt
andrew-fleming May 12, 2025
5c50938
fix lint
andrew-fleming May 12, 2025
10c9295
remove og ownable
andrew-fleming May 12, 2025
cba873b
fix name, fmt, lint
andrew-fleming May 12, 2025
4e0e1a5
fix conflicts
andrew-fleming May 14, 2025
82bb3f7
add documentation for ownablePK
andrew-fleming May 14, 2025
3ce5eec
add comments to simulator
andrew-fleming May 15, 2025
d4f36f5
allow ownership proposals to zero (to cancel)
andrew-fleming May 15, 2025
5741be7
fix fmt
andrew-fleming May 15, 2025
5b18320
Merge branch 'main' into ownable-shielded
andrew-fleming Jun 30, 2025
7120d98
migrate ownable to vitest, fix package, bump compact version
andrew-fleming Jun 30, 2025
5a0135a
fix fmt
andrew-fleming Jun 30, 2025
451d048
fix conflicts
andrew-fleming Jul 10, 2025
c127991
fix conflicts
andrew-fleming Jul 14, 2025
f9bf9ff
fix conflicts
andrew-fleming Jul 23, 2025
9c504ea
Merge branch 'main' into ownable-shielded
andrew-fleming Jul 23, 2025
d3c7a62
Init Shielded AcessControl
emnul Jul 23, 2025
0a637c3
Merge branch 'main' into shielded-access-control
emnul Jul 29, 2025
5f9268e
Update ShieldedAccessControl to 0.24.0
emnul Jul 29, 2025
bd81f55
Add disclosure closer to disclosure point
emnul Jul 30, 2025
c21b348
Merge branch 'main' into shielded-access-control
emnul Jul 31, 2025
01c0b6a
Revert "Add disclosure closer to disclosure point"
emnul Jul 31, 2025
04ff4f4
Update contract design
emnul Aug 5, 2025
8fca267
expose merkletree ledger var in TS
emnul Aug 5, 2025
cbea939
Merge branch 'main' into ownable-shielded
andrew-fleming Aug 5, 2025
b4cc0e0
Update contract interface
emnul Aug 5, 2025
73ffd2b
Remove _roleIds
emnul Aug 5, 2025
8af10e2
revert changes
emnul Aug 6, 2025
ecdc1d8
update syntax to compact 0.24.0
andrew-fleming Aug 6, 2025
cdb21bc
Circuit performance speedup
emnul Aug 7, 2025
20b14b8
add initial pk design with init tests, sim, and witnesses
andrew-fleming Aug 8, 2025
39df0c6
remove comment
andrew-fleming Aug 8, 2025
7d74870
Update design and create witness implementations
emnul Aug 8, 2025
b28361d
fmt file
emnul Aug 8, 2025
5acfaf8
Fix lints
emnul Aug 8, 2025
5f9a9eb
remove old pk module, add abstract class and state manager for simulator
andrew-fleming Aug 9, 2025
97cc94e
move helper test files
andrew-fleming Aug 9, 2025
74945e0
update simulators
andrew-fleming Aug 9, 2025
6698dcc
fix import
andrew-fleming Aug 9, 2025
0e657a1
add sim options type
andrew-fleming Aug 9, 2025
5eb4499
fix generate nonce in witness
andrew-fleming Aug 9, 2025
2ddadc8
improve simulator with options in constructor
andrew-fleming Aug 9, 2025
de93d48
add transferOwnership tests
andrew-fleming Aug 9, 2025
d7fe2a7
start hash chaining
andrew-fleming Aug 10, 2025
47d7480
integrate instance salt to hash
andrew-fleming Aug 11, 2025
cf582c0
integrate instance salt to hash
andrew-fleming Aug 11, 2025
f5f58d2
update constructor in sim
andrew-fleming Aug 11, 2025
6f233b1
update tests with salt
andrew-fleming Aug 11, 2025
9a64314
fix fmt
andrew-fleming Aug 11, 2025
0bafd7b
remove underscore in module name
andrew-fleming Aug 11, 2025
dee9254
remove unused file
andrew-fleming Aug 11, 2025
a47ea19
fix var names
andrew-fleming Aug 12, 2025
9233b47
add witness injection to sim
andrew-fleming Aug 12, 2025
4ed1123
add callerCtx
andrew-fleming Aug 12, 2025
876f9ef
add revealKey to renounce obfuscated
andrew-fleming Aug 12, 2025
f9c52d9
fmt docs
emnul Aug 12, 2025
fb01e61
remove obfuscated renounce
andrew-fleming Aug 12, 2025
c05509f
fix circuit name, fmt and lint
andrew-fleming Aug 12, 2025
249663b
fix error msg
andrew-fleming Aug 12, 2025
a5289fd
tidy up test
andrew-fleming Aug 12, 2025
4adaf86
add initialize checks, add underscore to hash circuit
andrew-fleming Aug 12, 2025
954d63f
change PS/witness val name to secretNonce
andrew-fleming Aug 12, 2025
e1ee7e4
improve test description
andrew-fleming Aug 12, 2025
6bbe434
remove unnecessary line
andrew-fleming Aug 12, 2025
54116e2
Add contractAddress to nonce generation scheme
emnul Aug 12, 2025
4401981
add in-code docs, add _computeOwnerId
andrew-fleming Aug 13, 2025
8c22092
add _computeOwnerId and test
andrew-fleming Aug 13, 2025
a3ff1ad
add reqs to _computeOwnerId
andrew-fleming Aug 13, 2025
6325d0b
add generatePubKeyPair util
andrew-fleming Aug 13, 2025
a1507ef
remove line
andrew-fleming Aug 13, 2025
771bfc0
add init option to mock, test when not initialized
andrew-fleming Aug 13, 2025
e9c1de5
tidy up code
andrew-fleming Aug 13, 2025
323b66f
parameterize tests
andrew-fleming Aug 13, 2025
00efb15
fix fmt
andrew-fleming Aug 13, 2025
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
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
537 changes: 537 additions & 0 deletions contracts/src/access/ShieldedAccessControl.compact

Large diffs are not rendered by default.

25 changes: 25 additions & 0 deletions contracts/src/access/ShieldedAccessControlUtils.compact
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);
}
}
288 changes: 288 additions & 0 deletions contracts/src/access/test/ShieldedAccessControl.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,288 @@
import {
CompactTypeBytes,
CompactTypeVector,
convert_bigint_to_Uint8Array,
persistentHash,
type WitnessContext,
} from '@midnight-ntwrk/compact-runtime';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import {
ContractAddress,
Either,
Ledger,
MerkleTreePath,
ShieldedAccessControl_Role as Role,
ZswapCoinPublicKey,
Contract as MockShieldedAccessControl
} from '../../../artifacts/MockShieldedAccessControl/contract/index.cjs';
import { fmtHexString, ShieldedAccessControlPrivateState, ShieldedAccessControlWitnesses } from '../witnesses/ShieldedAccessControlWitnesses.js';
import { ShieldedAccessControlSimulator } from './simulators/ShieldedAccessControlSimulator.js';
import * as utils from './utils/address.js';

// Helpers
const buildCommitment = (
accountId: Uint8Array,
roleId: Uint8Array,
index: bigint,
): Uint8Array => {
const rt_type = new CompactTypeVector(4, new CompactTypeBytes(32));
const bIndex = convert_bigint_to_Uint8Array(32, index);

const commitment = persistentHash(rt_type, [
accountId,
roleId,
bIndex,
COMMITMENT_DOMAIN,
]);

return commitment;
};

const buildNullifier = (
roleCommitment: Uint8Array,
): Uint8Array => {
const rt_type = new CompactTypeVector(2, new CompactTypeBytes(32));

const nullifier = persistentHash(rt_type, [
roleCommitment,
NULLIFIER_DOMAIN,
]);

return nullifier;
};

const createIdHash = (
pk: ZswapCoinPublicKey,
nonce: Uint8Array,
): Uint8Array => {
const rt_type = new CompactTypeVector(2, new CompactTypeBytes(32));

const bPK = pk.bytes;
return persistentHash(rt_type, [bPK, nonce]);
};

// PKs
const [ADMIN, Z_ADMIN] = utils.generatePubKeyPair('ADMIN');
const [OPERATOR_1, Z_OPERATOR_1] = utils.generatePubKeyPair('OPERATOR_1');
const [OPERATOR_2, Z_OPERATOR_2] = utils.generatePubKeyPair('OPERATOR_2');
const [OPERATOR_3, Z_OPERATOR_3] = utils.generatePubKeyPair('OPERATOR_3');
const [UNAUTHORIZED, Z_UNAUTHORIZED] = utils.generatePubKeyPair('UNAUTHORIZED');

// Roles
const DEFAULT_ADMIN_ROLE = utils.zeroUint8Array();
const OPERATOR_1_ROLE = convert_bigint_to_Uint8Array(32, 1n);
const OPERATOR_2_ROLE = convert_bigint_to_Uint8Array(32, 2n);
const OPERATOR_3_ROLE = convert_bigint_to_Uint8Array(32, 3n);
const UNINITIALIZED_ROLE = convert_bigint_to_Uint8Array(32, 555n);
const BAD_ROLE = convert_bigint_to_Uint8Array(32, 99999999n);

// Nonces
const ADMIN_SECRET_NONCE = Buffer.alloc(32, 'ADMIN_SECRET_NONCE');
const OPERATOR_1_SECRET_NONCE = Buffer.alloc(32, 'OPERATOR_1_NONCE');
const OPERATOR_2_SECRET_NONCE = Buffer.alloc(32, 'OPERATOR_2_NONCE');
const OPERATOR_3_SECRET_NONCE = Buffer.alloc(32, 'OPERATOR_3_NONCE');
const BAD_NONCE = Buffer.alloc(32, 'BAD_NONCE');

// Constants
const COMMITMENT_DOMAIN = new Uint8Array(32);
new TextEncoder().encodeInto('ShieldedAccessControl:commitment', COMMITMENT_DOMAIN);
const NULLIFIER_DOMAIN = new Uint8Array(32);
new TextEncoder().encodeInto('ShieldedAccessControl:nullifier', NULLIFIER_DOMAIN);

const ADMIN_ID = createIdHash(Z_ADMIN, ADMIN_SECRET_NONCE);
const ADMIN_COMMITMENT = buildCommitment(ADMIN_ID, DEFAULT_ADMIN_ROLE, 0n);
const ADMIN_NULLIFIER = buildNullifier(ADMIN_COMMITMENT);

const OPERATOR_1_ID = createIdHash(Z_OPERATOR_1, OPERATOR_1_SECRET_NONCE);
const OPERATOR_2_ID = createIdHash(Z_OPERATOR_2, OPERATOR_2_SECRET_NONCE);
const OPERATOR_3_ID = createIdHash(Z_OPERATOR_3, OPERATOR_3_SECRET_NONCE);

const BAD_ID = createIdHash(Z_UNAUTHORIZED, new Uint8Array(32));
const BAD_INDEX = 99999999n;
const BAD_COMMITMENT = buildCommitment(BAD_ID, BAD_ROLE, BAD_INDEX);

let shieldedAccessControl: ShieldedAccessControlSimulator;


describe('ShieldedAccessControl', () => {
beforeEach(() => {
// Create private state object and generate nonce
const PS = ShieldedAccessControlPrivateState.withRoleAndNonce(
Buffer.from(DEFAULT_ADMIN_ROLE),
ADMIN_SECRET_NONCE,
);
// Init contract for user with PS
shieldedAccessControl = new ShieldedAccessControlSimulator({
privateState: PS,
coinPK: ADMIN
});
});

describe('_computeRoleCommitment', () => {
it('computed commitment should match', () => {
expect(shieldedAccessControl._computeRoleCommitment(ADMIN_ID, DEFAULT_ADMIN_ROLE, 0n)).toEqual(ADMIN_COMMITMENT);
});

type ComputeRoleCommitmentCases = [
method: keyof ShieldedAccessControlSimulator,
isValidId: boolean,
isValidRole: boolean,
isValidIndex: boolean,
args: unknown[],
];

const checkedCircuits: ComputeRoleCommitmentCases[] = [
['_computeRoleCommitment', false, true, true, [BAD_ID, DEFAULT_ADMIN_ROLE, 0n]],
['_computeRoleCommitment', true, false, true, [ADMIN_ID, BAD_ROLE, 0n]],
['_computeRoleCommitment', true, true, false, [ADMIN_ID, DEFAULT_ADMIN_ROLE, BAD_INDEX]],
['_computeRoleCommitment', false, true, false, [BAD_ID, DEFAULT_ADMIN_ROLE, BAD_INDEX]],
['_computeRoleCommitment', false, false, false, [BAD_ID, BAD_ROLE, BAD_INDEX]],
['_computeRoleCommitment', true, false, false, [ADMIN_ID, BAD_ROLE, BAD_INDEX]],
['_computeRoleCommitment', false, false, true, [BAD_ID, BAD_ROLE, 0n]],
]

it.each(checkedCircuits)(
'%s should not match with isValidNonce(%s), isValidIndex(%s), isValidPath(%s)',
(circuitName, isValidId, isValidRole, isValidIndex, args) => {
// Test protected circuit
expect(() => {
(
shieldedAccessControl[circuitName] as (
...args: unknown[]
) => unknown
)(...args);
}).not.toEqual(ADMIN);
}
)
});

describe('_computeNullifier', () => {
it('should match nullifier', () => {
expect(shieldedAccessControl._computeNullifier(ADMIN_COMMITMENT)).toEqual(ADMIN_NULLIFIER);
});

it('should not match with bad commitment', () => {
expect(shieldedAccessControl._computeNullifier(BAD_COMMITMENT)).not.toEqual(ADMIN_NULLIFIER);
});
});

describe('_computeRoleId', () => {
const eitherAdmin = utils.createEitherTestUser('ADMIN');
const eitherUnauthorized = utils.createEitherTestUser('UNAUTHORIZED');

it('should match role id', () => {
expect(shieldedAccessControl._computeRoleId(eitherAdmin, ADMIN_SECRET_NONCE)).toEqual(ADMIN_ID);
});

it('should fail for contract address', () => {
const eitherContract = utils.createEitherTestContractAddress('CONTRACT')
expect(() => {
shieldedAccessControl._computeRoleId(eitherContract, ADMIN_SECRET_NONCE);
}).toThrow('ShieldedAccessControl: contract address owners are not yet supported');
});

type ComputeRoleIdCases = [
method: keyof ShieldedAccessControlSimulator,
isValidAccount: boolean,
isValidNonce: boolean,
args: unknown[],
];

const checkedCircuits: ComputeRoleIdCases[] = [
['_computeRoleId', true, false, [eitherAdmin, BAD_NONCE]],
['_computeRoleId', false, true, [eitherUnauthorized, ADMIN_SECRET_NONCE]],
['_computeRoleId', false, false, [eitherUnauthorized, BAD_NONCE]],
];

it.each(checkedCircuits)(
'%s should not match role id with invalidAccount=%s or invalidNonce=%s',
(circuitName, isValidAccount, isValidNonce, args) => {
// Test circuit
expect(() => {
(
shieldedAccessControl[circuitName] as (
...args: unknown[]
) => unknown
)(...args);
}).not.toEqual(ADMIN_ID);
}
)
});

// Complete testing once issue with pathForLeaf is resolved
describe.todo('wit_getRoleIndex', () => {
it.todo('should return 0 if no roles granted', () => {
const [_, index] = shieldedAccessControl.witnesses.wit_getRoleIndex(shieldedAccessControl.getWitnessContext(), UNINITIALIZED_ROLE, ADMIN_ID);
expect(index).toBe(0n);
});

it.todo('should return correct index', () => {
let granted = shieldedAccessControl._grantRole(DEFAULT_ADMIN_ROLE, ADMIN_ID);
expect(granted).toBe(true);
let [, adminIndex] = shieldedAccessControl.witnesses.wit_getRoleIndex(shieldedAccessControl.getWitnessContext(), DEFAULT_ADMIN_ROLE, ADMIN_ID);
expect(adminIndex).toBe(0n);

shieldedAccessControl.privateState.injectSecretNonce(OPERATOR_1_ROLE, OPERATOR_1_SECRET_NONCE);
granted = shieldedAccessControl._grantRole(OPERATOR_1_ROLE, OPERATOR_1_ID);
expect(granted).toBe(true);
const [, operatorIndex] = shieldedAccessControl.witnesses.wit_getRoleIndex(shieldedAccessControl.getWitnessContext(), OPERATOR_1_ROLE, OPERATOR_1_ID);
expect(operatorIndex).toBe(1n);

shieldedAccessControl.privateState.injectSecretNonce(OPERATOR_2_ROLE, OPERATOR_2_SECRET_NONCE);
granted = shieldedAccessControl._grantRole(OPERATOR_2_ROLE, OPERATOR_2_ID);
expect(granted).toBe(true);
shieldedAccessControl._grantRole(OPERATOR_2_ROLE, OPERATOR_2_ID);
const [, operatorIndex2] = shieldedAccessControl.witnesses.wit_getRoleIndex(shieldedAccessControl.getWitnessContext(), OPERATOR_2_ROLE, OPERATOR_2_ID);
expect(operatorIndex2).toBe(2n);

shieldedAccessControl.privateState.injectSecretNonce(OPERATOR_3_ROLE, OPERATOR_3_SECRET_NONCE);
shieldedAccessControl._grantRole(OPERATOR_3_ROLE, OPERATOR_3_ID);
const [_, operatorIndex3] = shieldedAccessControl.witnesses.wit_getRoleIndex(shieldedAccessControl.getWitnessContext(), OPERATOR_3_ROLE, OPERATOR_3_ID);
expect(operatorIndex3).toBe(3n);

let [, adminIndex2] = shieldedAccessControl.witnesses.wit_getRoleIndex(shieldedAccessControl.getWitnessContext(), DEFAULT_ADMIN_ROLE, ADMIN_ID);
expect(adminIndex2).toBe(0n);
});

it.todo('should return current Merkle tree index if role does not exist')
});

describe('wit_getRoleCommitmentPath', () => {
it('should return a Merkle tree path if one exists', () => {

});
});

describe('getRole', () => {
it('should return unapproved if role does not exist', () => {
expect(shieldedAccessControl.getRole(UNINITIALIZED_ROLE, ADMIN_ID).isApproved).toBe(false);
});

it('should return correct commitment', () => {
expect(shieldedAccessControl.getRole(DEFAULT_ADMIN_ROLE, ADMIN_ID).roleCommitment).toEqual(ADMIN_COMMITMENT);
});

it('should return correct nullifier', () => {
expect(shieldedAccessControl.getRole(DEFAULT_ADMIN_ROLE, ADMIN_ID).commitmentNullifier).toEqual(ADMIN_NULLIFIER);
});

it('should return approved role', () => {
shieldedAccessControl._grantRole(DEFAULT_ADMIN_ROLE, ADMIN_ID);
expect(shieldedAccessControl.getRole(DEFAULT_ADMIN_ROLE, ADMIN_ID).isApproved).toBe(true);
});
});

describe('_grantRole', () => {
it('should return true for new role', () => {
expect(shieldedAccessControl._grantRole(DEFAULT_ADMIN_ROLE, ADMIN_ID)).toBe(true);
});

it('should return false if role already granted', () => {
shieldedAccessControl._grantRole(DEFAULT_ADMIN_ROLE, ADMIN_ID);
expect(shieldedAccessControl._grantRole(DEFAULT_ADMIN_ROLE, ADMIN_ID)).toBe(false);
});
});

describe('')

});
Loading
Loading