Skip to content
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

Add public bridge (Open VAnchor) #190

Merged
merged 35 commits into from
Oct 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
8545cde
ups
akileshtangella Oct 8, 2022
aa43b3a
contracts compile
akileshtangella Oct 8, 2022
cff10ef
ups
akileshtangella Oct 8, 2022
917391f
test
akileshtangella Oct 8, 2022
540e2c1
verified merkle proof
akileshtangella Oct 8, 2022
ebb92c4
openVAnchor typescript implementation compiles
semaraugusto Oct 8, 2022
2e16439
chain id with type
akileshtangella Oct 8, 2022
bbbac7f
neg test
akileshtangella Oct 8, 2022
b7c1309
Updates and re-org the hashers/merkle trees to de-duplicate
drewstone Oct 8, 2022
120af61
Merge branch 'group/hackathon' into drew/group/hackathon
drewstone Oct 8, 2022
4e0e264
integrate tests with typescript interface for OpenVAnchor. Fix bugs i…
semaraugusto Oct 8, 2022
db3dbc0
In-progress updates for open vanchor
drewstone Oct 8, 2022
d886a05
Get open vanchor tests working again
drewstone Oct 8, 2022
ab7d8ce
Remove .only
drewstone Oct 8, 2022
21ea03d
merges
akileshtangella Oct 8, 2022
2c8b1cc
compiling openvbridge
akileshtangella Oct 8, 2022
10e6315
ups
akileshtangella Oct 9, 2022
0a15f31
ups
akileshtangella Oct 9, 2022
327418f
ups
akileshtangella Oct 9, 2022
3617a00
Get cross chain test working
drewstone Oct 9, 2022
f6522b7
Fmt
drewstone Oct 9, 2022
b6ec90c
Fmt
drewstone Oct 9, 2022
e341122
Merge main
drewstone Oct 9, 2022
58bd48c
Remove only
drewstone Oct 9, 2022
7a610a7
Ups
drewstone Oct 9, 2022
1d3ab17
Publish
akileshtangella Oct 9, 2022
b521a6d
Publish
akileshtangella Oct 9, 2022
7255270
Remove unused from vbridge
drewstone Oct 9, 2022
1437f0c
remove bn
akileshtangella Oct 9, 2022
0137824
Publish
akileshtangella Oct 9, 2022
cdaba50
changes to tests
akileshtangella Oct 9, 2022
e20fab2
Merge branch 'main' into group/hackathon
drewstone Oct 18, 2022
7cb1fde
Update comment
drewstone Oct 18, 2022
c0a204f
Fmt
drewstone Oct 18, 2022
527ddd7
Remove debug file
nepoche Oct 25, 2022
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
4 changes: 2 additions & 2 deletions contracts/anchors/AnchorBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

pragma solidity ^0.8.0;

import "../trees/MerkleTreePoseidon.sol";
import "../trees/MerkleTree.sol";
import "../interfaces/IAnchorVerifier.sol";
import "./LinkableAnchor.sol";

Expand Down Expand Up @@ -37,7 +37,7 @@ abstract contract AnchorBase is LinkableAnchor {
constructor(
address _handler,
IAnchorVerifier _verifier,
IPoseidonT3 _hasher,
IHasher _hasher,
uint32 _merkleTreeHeight,
uint8 _maxEdges
) LinkableAnchor(_handler, _hasher, _merkleTreeHeight, _maxEdges) {
Expand Down
16 changes: 8 additions & 8 deletions contracts/anchors/LinkableAnchor.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

pragma solidity ^0.8.0;

import "../trees/MerkleTreePoseidon.sol";
import "../trees/MerkleTree.sol";
import "../utils/ChainIdWithType.sol";
import "../interfaces/ILinkableAnchor.sol";
import "@openzeppelin/contracts/security/ReentrancyGuard.sol";
Expand Down Expand Up @@ -36,11 +36,11 @@ import "@openzeppelin/contracts/security/ReentrancyGuard.sol";
An example usage of this system is the:
- VAnchor.sol - for variable sized private bridging of assets
*/
abstract contract LinkableAnchor is ILinkableAnchor, MerkleTreePoseidon, ReentrancyGuard, ChainIdWithType {
abstract contract LinkableAnchor is ILinkableAnchor, MerkleTree, ReentrancyGuard, ChainIdWithType {
uint32 proposalNonce = 0;
address public handler;

// The maximum number of edges this tree can support.
// The maximum number of edges this tree can support for zero-knowledge linkability.
uint8 public immutable maxEdges;

/**
Expand Down Expand Up @@ -80,10 +80,10 @@ abstract contract LinkableAnchor is ILinkableAnchor, MerkleTreePoseidon, Reentra
*/
constructor(
address _handler,
IPoseidonT3 _hasher,
IHasher _hasher,
uint32 _merkleTreeHeight,
uint8 _maxEdges
) MerkleTreePoseidon(_merkleTreeHeight, _hasher) {
) MerkleTree(_merkleTreeHeight, _hasher) {
handler = _handler;
maxEdges = _maxEdges;
}
Expand Down Expand Up @@ -147,7 +147,7 @@ abstract contract LinkableAnchor is ILinkableAnchor, MerkleTreePoseidon, Reentra
} else {
edges[i] = Edge({
// merkle tree height for zeros
root: zeros(levels - 1),
root: hasher.zeros(levels - 1),
chainID: 0,
latestLeafIndex: 0,
srcResourceID: 0x0
Expand All @@ -169,7 +169,7 @@ abstract contract LinkableAnchor is ILinkableAnchor, MerkleTreePoseidon, Reentra
roots[i] = edgeList[i].root;
} else {
// merkle tree height for zeros
roots[i] = zeros(levels - 1);
roots[i] = hasher.zeros(levels - 1);
}
}

Expand Down Expand Up @@ -215,7 +215,7 @@ abstract contract LinkableAnchor is ILinkableAnchor, MerkleTreePoseidon, Reentra
rootIndex++;
}
while (rootIndex != maxEdges + 1) {
require(_roots[rootIndex] == zeros(levels - 1), "non-existent edge is not set to the default root");
require(_roots[rootIndex] == hasher.zeros(levels - 1), "non-existent edge is not set to the default root");
rootIndex++;
}
return true;
Expand Down
4 changes: 2 additions & 2 deletions contracts/identity-vanchor/IdentityVAnchor.sol
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ contract IdentityVAnchor is IdentityVAnchorBase {
ISemaphoreGroups _semaphore,
IAnchorVerifier _verifier,
uint8 _levels,
IPoseidonT3 _hasher,
IHasher _hasher,
address _handler,
address _token,
uint8 _maxEdges,
Expand Down Expand Up @@ -267,7 +267,7 @@ contract IdentityVAnchor is IdentityVAnchorBase {
@notice Checks whether the zkSNARK proof is valid
@param _args The zkSNARK proof parameters
*/
function _executeVerification(IdentityVAnchorEncodeInputs.Proof memory _args) view internal {
function _executeVerification(IdentityVAnchorEncodeInputs.Proof memory _args) internal {
if (_args.inputNullifiers.length == 2) {
// bytes32[2] memory identityRoots = abi.decode(_args.identityRoots, (bytes32[2]));
(bytes memory encodedInput, bytes32[] memory roots) = IdentityVAnchorEncodeInputs._encodeInputs2(_args, maxEdges);
Expand Down
2 changes: 1 addition & 1 deletion contracts/identity-vanchor/IdentityVAnchorBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ abstract contract IdentityVAnchorBase is AnchorBase {
constructor(
IAnchorVerifier _verifier,
uint8 _levels,
IPoseidonT3 _hasher,
IHasher _hasher,
address _handler,
uint8 _maxEdges
)
Expand Down
50 changes: 50 additions & 0 deletions contracts/interfaces/IOpenLinkableAnchor.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* Copyright 2021 Webb Technologies
* SPDX-License-Identifier: GPL-3.0-or-later-only
*/

pragma solidity ^0.8.0;

/**
@title ILinkableAnchor Interface
@notice The interface supports updating edges for a graph-like functionality.
It also supports setting handlers and verifiers for handling updates
to the edge data of a LinkableAnchor as well as the verifier used in
verifying proofs of knowledge of leaves in one-of-many merkle trees.

The ILinkableAnchor interface can also be used with the VAnchor system
to control the minimal and maximum withdrawal and deposit limits respectively.
*/
interface IOpenLinkableAnchor {
/**
@notice Sets the handler for updating edges and other contract state
@param handler The new handler address
@param nonce The nonce for tracking update counts
*/
function setHandler(address handler, uint32 nonce) external;


/**
@notice Sets the minimal withdrawal limit for the anchor
@param minimalWithdrawalAmount The new minimal withdrawal limit
*/
function configureMinimalWithdrawalLimit(uint256 minimalWithdrawalAmount, uint32 nonce) external;

/**
@notice Sets the maximal deposit limit for the anchor
@param maximumDepositAmount The new maximal deposit limit
*/
function configureMaximumDepositLimit(uint256 maximumDepositAmount, uint32 nonce) external;

/**
@notice The function is used to update the edge data of a LinkableAnchor
@param root The merkle root of the linked anchor on the `sourceChainID`'s chain
@param latestLeafIndex The index of the leaf updating the merkle tree with root `root`
@param target The target resource ID of the linked anchor
*/
function updateEdge(
bytes32 root,
uint32 latestLeafIndex,
bytes32 target
) external payable;
}
2 changes: 1 addition & 1 deletion contracts/mocks/LinkableAnchorMock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ contract LinkableAnchorMock is LinkableAnchor {
constructor(
address _handler,
IAnchorVerifier _verifier,
IPoseidonT3 _hasher,
IHasher _hasher,
uint32 _merkleTreeHeight,
uint8 _maxEdges
) LinkableAnchor(_handler, _hasher, _merkleTreeHeight, _maxEdges) {
Expand Down
8 changes: 4 additions & 4 deletions contracts/mocks/MerkleTreePoseidonMock.sol
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "../trees/MerkleTreePoseidon.sol";
import "../trees/Hasher.sol";
import "../trees/MerkleTree.sol";
import "../trees/IHasher.sol";

contract MerkleTreePoseidonMock is MerkleTreePoseidon {
constructor(uint32 _treeLevels, IPoseidonT3 _hasher) MerkleTreePoseidon(_treeLevels, _hasher) {}
contract MerkleTreePoseidonMock is MerkleTree {
constructor(uint32 _treeLevels, IHasher _hasher) MerkleTree(_treeLevels, _hasher) {}

function insert(bytes32 _leaf) public {
_insert(_leaf);
Expand Down
104 changes: 104 additions & 0 deletions contracts/open-vanchor/OpenAnchorBase.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/**
* Copyright 2021 Webb Technologies
* SPDX-License-Identifier: GPL-3.0-or-later-only
*/

pragma solidity ^0.8.0;

import "./OpenLinkableAnchor.sol";

/**
@title AnchorBase contract
@notice Base contract for interoperable anchors. Each anchor base
is a LinkableAnchor which allows it to be connected to other LinkableAnchors.
*/
abstract contract OpenAnchorBase is OpenLinkableAnchor {
mapping(bytes32 => bool) public nullifierHashes;
// map to store all commitments to prevent accidental deposits with the same commitment
mapping(bytes32 => bool) public commitments;

event Insertion(bytes32 indexed commitment, uint32 leafIndex, uint256 timestamp);

constructor(
address _handler,
IHasher _hasher,
uint32 _merkleTreeHeight
) OpenLinkableAnchor(_handler, _hasher, _merkleTreeHeight) {
}

/**
@notice Inserts a commitment into the tree
@notice This is an internal function and meant to be used by a child contract.
@param _commitment The note commitment = Poseidon(chainId, nullifier, secret)
@return uint32 The index of the inserted commitment
*/
function insert(bytes32 _commitment) internal returns(uint32) {
require(!commitments[_commitment], "The commitment has been submitted");

uint32 insertedIndex = _insert(_commitment);
commitments[_commitment] = true;
emit Insertion(_commitment, insertedIndex, block.timestamp);

return insertedIndex;
}

/**
@notice Inserts two commitments into the tree. Useful for contracts
that need to insert two commitments at once.
@notice This is an internal function and meant to be used by a child contract.
@param _firstCommitment The first note commitment
@param _secondCommitment The second note commitment
@return uint32 The index of the first inserted commitment
*/
function insertTwo(bytes32 _firstCommitment, bytes32 _secondCommitment) internal returns(uint32) {
require(!commitments[_firstCommitment], "The commitment has been submitted");
require(!commitments[_secondCommitment], "The commitment has been submitted");

uint32 insertedIndex = _insertTwo(_firstCommitment, _secondCommitment);
commitments[_firstCommitment] = true;
commitments[_secondCommitment] = true;
emit Insertion(_firstCommitment, insertedIndex, block.timestamp);
emit Insertion(_secondCommitment, insertedIndex + 1, block.timestamp);

return insertedIndex;
}

/**
@notice Whether a note is already spent
@param _nullifierHash The nullifier hash of the deposit note
@return bool Whether the note is already spent
*/
function isSpent(bytes32 _nullifierHash) public view returns (bool) {
return nullifierHashes[_nullifierHash];
}

/**
@notice Whether an array of notes is already spent
@param _nullifierHashes The array of nullifier hashes of the deposit notes
@return bool[] An array indicated whether each note's nullifier hash is already spent
*/
function isSpentArray(bytes32[] calldata _nullifierHashes) external view returns (bool[] memory) {
bool[] memory spent = new bool[](_nullifierHashes.length);
for (uint256 i = 0; i < _nullifierHashes.length; i++) {
if (isSpent(_nullifierHashes[i])) {
spent[i] = true;
}
}

return spent;
}

/**
@notice Set a new handler with a nonce
@dev Can only be called by the `AnchorHandler` contract
@param _handler The new handler address
@param _nonce The nonce for updating the new handler
*/
function setHandler(address _handler, uint32 _nonce) override onlyHandler external {
require(_handler != address(0), "Handler cannot be 0");
require(proposalNonce < _nonce, "Invalid nonce");
require(_nonce < proposalNonce + 1048, "Nonce must not increment more than 1048");
handler = _handler;
proposalNonce = _nonce;
}
}
Loading