Skip to content

Commit

Permalink
Merge pull request #75 from awrichar/data-param
Browse files Browse the repository at this point in the history
Add "data" parameter to all mint/transfer methods
  • Loading branch information
jimthematrix authored Sep 23, 2024
2 parents 501c898 + 2d546ad commit 2b3aa22
Show file tree
Hide file tree
Showing 28 changed files with 260 additions and 112 deletions.
1 change: 0 additions & 1 deletion solidity/contracts/erc20.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ pragma solidity ^0.8.20;

import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";
import {ERC20} from "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "hardhat/console.sol";

/// @title A sample implementation of a Zeto based fungible token with anonymity and no encryption
/// @author Kaleido, Inc.
Expand Down
27 changes: 27 additions & 0 deletions solidity/contracts/lib/interfaces/izeto.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright © 2024 Kaleido, Inc.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
pragma solidity ^0.8.20;

import {IZetoBase} from "./izeto_base.sol";

interface IZeto is IZetoBase {
event UTXOTransfer(
uint256[] inputs,
uint256[] outputs,
address indexed submitter,
bytes data
);
}
20 changes: 20 additions & 0 deletions solidity/contracts/lib/interfaces/izeto_base.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright © 2024 Kaleido, Inc.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
pragma solidity ^0.8.20;

interface IZetoBase {
event UTXOMint(uint256[] outputs, address indexed submitter, bytes data);
}
29 changes: 29 additions & 0 deletions solidity/contracts/lib/interfaces/izeto_encrypted.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright © 2024 Kaleido, Inc.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
pragma solidity ^0.8.20;

import {IZetoBase} from "./izeto_base.sol";

interface IZetoEncrypted is IZetoBase {
event UTXOTransferWithEncryptedValues(
uint256[] inputs,
uint256[] outputs,
uint256 encryptionNonce,
uint256[] encryptedValues,
address indexed submitter,
bytes data
);
}
1 change: 0 additions & 1 deletion solidity/contracts/lib/registry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import {OwnableUpgradeable} from "@openzeppelin/contracts-upgradeable/access/Own
import {SmtLib} from "@iden3/contracts/lib/SmtLib.sol";
import {PoseidonUnit2L, PoseidonUnit3L} from "@iden3/contracts/lib/Poseidon.sol";
import {Commonlib} from "./common.sol";
import "hardhat/console.sol";

uint256 constant MAX_SMT_DEPTH = 64;

Expand Down
7 changes: 4 additions & 3 deletions solidity/contracts/lib/zeto_base.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// limitations under the License.
pragma solidity ^0.8.20;

import {IZetoBase} from "./interfaces/izeto_base.sol";
import {Commonlib} from "./common.sol";
import {Registry} from "./registry.sol";
import {ZetoCommon} from "./zeto_common.sol";
Expand All @@ -25,7 +26,7 @@ import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
/// without using nullifiers. Each UTXO's spending status is explicitly tracked.
/// @author Kaleido, Inc.
/// @dev Implements common functionalities of Zeto based tokens without nullifiers
abstract contract ZetoBase is ZetoCommon {
abstract contract ZetoBase is IZetoBase, ZetoCommon {
enum UTXOStatus {
UNKNOWN, // default value for the empty UTXO slots
UNSPENT,
Expand Down Expand Up @@ -114,7 +115,7 @@ abstract contract ZetoBase is ZetoCommon {

// This function is used to mint new UTXOs, as an example implementation,
// which is only callable by the owner.
function _mint(uint256[] memory utxos) internal virtual {
function _mint(uint256[] memory utxos, bytes calldata data) internal virtual {
for (uint256 i = 0; i < utxos.length; ++i) {
uint256 utxo = utxos[i];
if (_utxos[utxo] == UTXOStatus.UNSPENT) {
Expand All @@ -125,6 +126,6 @@ abstract contract ZetoBase is ZetoCommon {

_utxos[utxo] = UTXOStatus.UNSPENT;
}
emit UTXOMint(utxos, msg.sender);
emit UTXOMint(utxos, msg.sender, data);
}
}
16 changes: 0 additions & 16 deletions solidity/contracts/lib/zeto_common.sol
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,6 @@ import {OwnableUpgradeable} from "@openzeppelin/contracts-upgradeable/access/Own
/// @author Kaleido, Inc.
/// @dev Implements common functionalities of Zeto based tokens
abstract contract ZetoCommon is OwnableUpgradeable {
event UTXOMint(uint256[] outputs, address indexed submitter);

event UTXOTransfer(
uint256[] inputs,
uint256[] outputs,
address indexed submitter
);

event UTXOTransferWithEncryptedValues(
uint256[] inputs,
uint256[] outputs,
uint256 encryptionNonce,
uint256[] encryptedValues,
address indexed submitter
);

error UTXONotMinted(uint256 utxo);
error UTXOAlreadyOwned(uint256 utxo);
error UTXOAlreadySpent(uint256 utxo);
Expand Down
7 changes: 4 additions & 3 deletions solidity/contracts/lib/zeto_nullifier.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// limitations under the License.
pragma solidity ^0.8.20;

import {IZetoBase} from "./interfaces/izeto_base.sol";
import {Commonlib} from "./common.sol";
import {Registry} from "./registry.sol";
import {ZetoCommon} from "./zeto_common.sol";
Expand All @@ -27,7 +28,7 @@ uint256 constant MAX_SMT_DEPTH = 64;
/// @title A sample base implementation of a Zeto based token contract with nullifiers
/// @author Kaleido, Inc.
/// @dev Implements common functionalities of Zeto based tokens using nullifiers
abstract contract ZetoNullifier is ZetoCommon {
abstract contract ZetoNullifier is IZetoBase, ZetoCommon {
SmtLib.Data internal _commitmentsTree;
using SmtLib for SmtLib.Data;
mapping(uint256 => bool) private _nullifiers;
Expand Down Expand Up @@ -110,7 +111,7 @@ abstract contract ZetoNullifier is ZetoCommon {

// This function is used to mint new UTXOs, as an example implementation,
// which is only callable by the owner.
function _mint(uint256[] memory utxos) internal virtual {
function _mint(uint256[] memory utxos, bytes calldata data) internal virtual {
for (uint256 i = 0; i < utxos.length; ++i) {
uint256 utxo = utxos[i];
if (utxo == 0) {
Expand All @@ -126,7 +127,7 @@ abstract contract ZetoNullifier is ZetoCommon {
_commitmentsTree.addLeaf(utxo, utxo);
}

emit UTXOMint(utxos, msg.sender);
emit UTXOMint(utxos, msg.sender, data);
}

function getRoot() public view returns (uint256) {
Expand Down
18 changes: 10 additions & 8 deletions solidity/contracts/zeto_anon.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// limitations under the License.
pragma solidity ^0.8.20;

import {IZeto} from "./lib/interfaces/izeto.sol";
import {Groth16Verifier_CheckHashesValue} from "./lib/verifier_check_hashes_value.sol";
import {Groth16Verifier_CheckInputsOutputsValue} from "./lib/verifier_check_inputs_outputs_value.sol";
import {Groth16Verifier_Anon} from "./lib/verifier_anon.sol";
Expand All @@ -25,7 +26,6 @@ import {ZetoFungible} from "./lib/zeto_fungible.sol";
import {ZetoFungibleWithdraw} from "./lib/zeto_fungible_withdraw.sol";
import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";
import {UUPSUpgradeable} from "@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol";
import "hardhat/console.sol";

/// @title A sample implementation of a Zeto based fungible token with anonymity and no encryption
/// @author Kaleido, Inc.
Expand All @@ -34,7 +34,7 @@ import "hardhat/console.sol";
/// - the sum of the input values match the sum of output values
/// - the hashes in the input and output match the `hash(value, salt, owner public key)` formula
/// - the sender possesses the private BabyJubjub key, whose public key is part of the pre-image of the input commitment hashes
contract Zeto_Anon is ZetoBase, ZetoFungibleWithdraw, UUPSUpgradeable {
contract Zeto_Anon is IZeto, ZetoBase, ZetoFungibleWithdraw, UUPSUpgradeable {
Groth16Verifier_Anon internal verifier;

function initialize(
Expand Down Expand Up @@ -63,7 +63,8 @@ contract Zeto_Anon is ZetoBase, ZetoFungibleWithdraw, UUPSUpgradeable {
function transfer(
uint256[2] memory inputs,
uint256[2] memory outputs,
Commonlib.Proof calldata proof
Commonlib.Proof calldata proof,
bytes calldata data
) public returns (bool) {
require(
validateTransactionProposal(inputs, outputs, proof),
Expand Down Expand Up @@ -91,20 +92,21 @@ contract Zeto_Anon is ZetoBase, ZetoFungibleWithdraw, UUPSUpgradeable {
inputArray[i] = inputs[i];
outputArray[i] = outputs[i];
}
emit UTXOTransfer(inputArray, outputArray, msg.sender);
emit UTXOTransfer(inputArray, outputArray, msg.sender, data);

return true;
}

function deposit(
uint256 amount,
uint256 utxo,
Commonlib.Proof calldata proof
Commonlib.Proof calldata proof,
bytes calldata data
) public {
_deposit(amount, utxo, proof);
uint256[] memory utxos = new uint256[](1);
utxos[0] = utxo;
_mint(utxos);
_mint(utxos, data);
}

function withdraw(
Expand All @@ -118,7 +120,7 @@ contract Zeto_Anon is ZetoBase, ZetoFungibleWithdraw, UUPSUpgradeable {
processInputsAndOutputs(inputs, [output, 0]);
}

function mint(uint256[] memory utxos) public onlyOwner {
_mint(utxos);
function mint(uint256[] memory utxos, bytes calldata data) public onlyOwner {
_mint(utxos, data);
}
}
19 changes: 11 additions & 8 deletions solidity/contracts/zeto_anon_enc.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// limitations under the License.
pragma solidity ^0.8.20;

import {IZetoEncrypted} from "./lib/interfaces/izeto_encrypted.sol";
import {Groth16Verifier_CheckHashesValue} from "./lib/verifier_check_hashes_value.sol";
import {Groth16Verifier_CheckInputsOutputsValue} from "./lib/verifier_check_inputs_outputs_value.sol";
import {Groth16Verifier_AnonEnc} from "./lib/verifier_anon_enc.sol";
Expand All @@ -25,7 +26,6 @@ import {Registry} from "./lib/registry.sol";
import {Commonlib} from "./lib/common.sol";
import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";
import {UUPSUpgradeable} from "@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol";
import "hardhat/console.sol";

/// @title A sample implementation of a Zeto based fungible token with anonymity, and encryption
/// @author Kaleido, Inc.
Expand All @@ -36,7 +36,7 @@ import "hardhat/console.sol";
/// - the sender possesses the private BabyJubjub key, whose public key is part of the pre-image of the input commitment hashes
/// - the encrypted value in the input is derived from the receiver's UTXO value and encrypted with a shared secret using
/// the ECDH protocol between the sender and receiver (this guarantees data availability for the receiver)
contract Zeto_AnonEnc is ZetoBase, ZetoFungibleWithdraw, UUPSUpgradeable {
contract Zeto_AnonEnc is IZetoEncrypted, ZetoBase, ZetoFungibleWithdraw, UUPSUpgradeable {
Groth16Verifier_AnonEnc internal verifier;

function initialize(
Expand Down Expand Up @@ -67,7 +67,8 @@ contract Zeto_AnonEnc is ZetoBase, ZetoFungibleWithdraw, UUPSUpgradeable {
uint256[2] memory outputs,
uint256 encryptionNonce,
uint256[4] memory encryptedValues,
Commonlib.Proof calldata proof
Commonlib.Proof calldata proof,
bytes calldata data
) public returns (bool) {
require(
validateTransactionProposal(inputs, outputs, proof),
Expand Down Expand Up @@ -112,20 +113,22 @@ contract Zeto_AnonEnc is ZetoBase, ZetoFungibleWithdraw, UUPSUpgradeable {
outputArray,
encryptionNonce,
encryptedValuesArray,
msg.sender
msg.sender,
data
);
return true;
}

function deposit(
uint256 amount,
uint256 utxo,
Commonlib.Proof calldata proof
Commonlib.Proof calldata proof,
bytes calldata data
) public {
_deposit(amount, utxo, proof);
uint256[] memory utxos = new uint256[](1);
utxos[0] = utxo;
_mint(utxos);
_mint(utxos, data);
}

function withdraw(
Expand All @@ -139,7 +142,7 @@ contract Zeto_AnonEnc is ZetoBase, ZetoFungibleWithdraw, UUPSUpgradeable {
processInputsAndOutputs(inputs, [output, 0]);
}

function mint(uint256[] memory utxos) public onlyOwner {
_mint(utxos);
function mint(uint256[] memory utxos, bytes calldata data) public onlyOwner {
_mint(utxos, data);
}
}
18 changes: 11 additions & 7 deletions solidity/contracts/zeto_anon_enc_nullifier.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// limitations under the License.
pragma solidity ^0.8.20;

import {IZetoEncrypted} from "./lib/interfaces/izeto_encrypted.sol";
import {Groth16Verifier_CheckHashesValue} from "./lib/verifier_check_hashes_value.sol";
import {Groth16Verifier_CheckNullifierValue} from "./lib/verifier_check_nullifier_value.sol";
import {Groth16Verifier_AnonEncNullifier} from "./lib/verifier_anon_enc_nullifier.sol";
Expand All @@ -23,7 +24,6 @@ import {ZetoFungibleWithdrawWithNullifiers} from "./lib/zeto_fungible_withdraw_n
import {Registry} from "./lib/registry.sol";
import {Commonlib} from "./lib/common.sol";
import {UUPSUpgradeable} from "@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol";
import "hardhat/console.sol";

/// @title A sample implementation of a Zeto based fungible token with anonymity, encryption and history masking
/// @author Kaleido, Inc.
Expand All @@ -35,6 +35,7 @@ import "hardhat/console.sol";
/// - the encrypted value in the input is derived from the receiver's UTXO value and encrypted with a shared secret using the ECDH protocol between the sender and receiver (this guarantees data availability for the receiver)
/// - the nullifiers represent input commitments that are included in a Sparse Merkle Tree represented by the root hash
contract Zeto_AnonEncNullifier is
IZetoEncrypted,
ZetoNullifier,
ZetoFungibleWithdrawWithNullifiers,
UUPSUpgradeable
Expand Down Expand Up @@ -77,7 +78,8 @@ contract Zeto_AnonEncNullifier is
uint256 root,
uint256 encryptionNonce,
uint256[4] memory encryptedValues,
Commonlib.Proof calldata proof
Commonlib.Proof calldata proof,
bytes calldata data
) public returns (bool) {
require(
validateTransactionProposal(nullifiers, outputs, root),
Expand Down Expand Up @@ -126,20 +128,22 @@ contract Zeto_AnonEncNullifier is
outputArray,
encryptionNonce,
encryptedValuesArray,
msg.sender
msg.sender,
data
);
return true;
}

function deposit(
uint256 amount,
uint256 utxo,
Commonlib.Proof calldata proof
Commonlib.Proof calldata proof,
bytes calldata data
) public {
_deposit(amount, utxo, proof);
uint256[] memory utxos = new uint256[](1);
utxos[0] = utxo;
_mint(utxos);
_mint(utxos, data);
}

function withdraw(
Expand All @@ -154,7 +158,7 @@ contract Zeto_AnonEncNullifier is
processInputsAndOutputs(nullifiers, [output, 0]);
}

function mint(uint256[] memory utxos) public onlyOwner {
_mint(utxos);
function mint(uint256[] memory utxos, bytes calldata data) public onlyOwner {
_mint(utxos, data);
}
}
Loading

0 comments on commit 2b3aa22

Please sign in to comment.