About

September 12, 2024 ยท View on GitHub

test workflow

About

The fhe-erc3643 package is an almost full port of the entire Tokeny Solutions TREX package using FHE to encrypt token amounts accross the entire TREX platform. The TFHE lib is deployed on the ERC20 token itself as well as all the ModularCompliance architecture.

It is essentially a single Hardhat project with an integrated CLI (via the hardhat task system) that allows the user to deploy a full FHE-TREX token on a local Zama node.

  • Only the country parameter is still unencrypted. It could be easily done in future version.
  • Few modules have yet to be converted into FHE.
  • The vast majority of the TREX test suite has also been converted. Consequently, running the test suite on a real node is currently impracticable.
  • Due to the large amount of tests, it is not possible to run the full test suite in a single hardhat test command. The node.js runtime eventually will run out of memory.
  • The DvA (Delivery versus Action) contract is also fully converted as well as its entire test suite. A fully running example is available at A full running script is available at ./test/scripts/fhevm/DVA.sh
  • OnChainID is included as-is along with its test suite, just renamed to identity (added solidity 0.8.24 support + linter corrections). No FHE features have been included.

Development Roadmap

  • Improve the hardhat-fhevm hardhat plug-in to support multiple nodes
  • Add command trex new-token to create multiple tokens on the same factory. Right now it only supports a single token
  • Remove the --unpause flag in the trex setup command
  • Test on Zama's dev node
  • Test on Anvil/Hardhat node
  • Implement DvD (Delivery versus Delivery)
  • Convert missing modules to FHEVM (Country-related modules, TimeTransfersLimitsModule.sol, MaxBalanceModule.sol, TransferFeesModule.sol)
  • Encrypt the country property
  • linter. Current version raises too many lint errors mostly due to original TREX + onChainID code.

Install & Test

Install

npm install

Test on hardhat network

npm run test

Test on local fhevm node (very slow)

npm run test:fhevm

Start/stop/restart a local fhevm node

# starts a new local node (if not already running)
npm run start
# stops the running local node
npm run stop
# restart a running local node
npm run restart

How to use the FHE token manually

Setup a TREX token (slow)

The new token address is displayed at the end of the deployment process.

npx hardhat --network fhevm trex setup --mint 1000 --unpause
  • --mint option to distribute tokens to all token holders (alice, bob, etc.)
  • --unpause option to activate the token
  • --help to list all the available commands (mint, burn, transfer, etc.)

Inspect the lastest deployed TREX token

npx hardhat --network fhevm token show

Wallet and token aliases

Instead of using addresses you can execute CLI commands using wallet aliases. The available of aliases are listed in the table below. Token address is optional, if not provided, the CLI will use the latest valid deployed token. You can also use the symbol/name of the token instead of its address

# use token address
npx hardhat --network fhevm token mint --token 0x3Aa5ebB10DC797CAC828524e59A333d0A371443c --agent "token-agent" --user alice --amount 10n
# use token symbol
npx hardhat --network fhevm token mint --token MEG --agent "token-agent" --user alice --amount 10n
# resolve token address automatically
npx hardhat --network fhevm token mint --agent "token-agent" --user alice --amount 10n

Interacting with the TREX token

Use the token commands to interact with the token

npx hardhat --network fhevm token mint --token <token address> --agent "token-agent" --user alice --amount 10n

Example interacting with the ExchangeMonthlyLimitsModule compliance module

The following interaction is fully FHE compliant (from start to finish). (See ./test/scripts/fhevm/ExchangeMonthlyLimits.sh)

# setup the TREX environment
npx hardhat --network fhevm trex setup --mint 1000 --unpause
# register bob'id as an exchanger 
npx hardhat --network fhevm token exchangemonthly:add-id --token 0x47DA632524c03ED15D293e34256D28BD0d38c7a4 --owner token-owner --user bob
# specify bob's limits
npx hardhat --network fhevm token exchangemonthly:set-exchange-limit --token 0x47DA632524c03ED15D293e34256D28BD0d38c7a4 --owner token-owner --exchange-id bob --limit 100
# Alice transfers 10 tokens to bob
npx hardhat --network fhevm token transfer --token 0x47DA632524c03ED15D293e34256D28BD0d38c7a4 --wallet alice --to bob --amount 10
# Display bob's remaining credits
npx hardhat --network fhevm token exchangemonthly:get-monthly-counter --token 0x47DA632524c03ED15D293e34256D28BD0d38c7a4 --exchange-id bob --investor-id alice --decrypt

Example running the DvA contract in full FHE mode

A full running script is available at ./test/scripts/fhevm/DVA.sh

# For a complete list of DvA commands:
npx hardhat transfer-manager --help

The ๐Ÿšง hardhat-fhevm ๐Ÿšง npm package

fhe-erc3643 uses the hardhat-fhevm hardhat plugin, which offers a set of hardhat tasks to develop solidity contracts on top of Zama's FHEVM. It supports both the mock and local node modes. The hardhat-fhevm hardhat plugin is currently in alpha testing. Future versions should be fully re-architectured to better support external nodes like Anvil, Hardhat node and Zama dev-node.

The test wallets and roles

NameWallet indexWallet aliasesRoleNote
๐Ÿš€ admin0adminowner of the TREXFactory
๐Ÿซ foo-university1foo-universityclaim issuerstored in the token's Identity registry
๐Ÿ›๏ธ bar-government2bar-governmentclaim issuerstored in the token's Identity registry
๐Ÿฆ super-bank3super-bank, token-ownertoken owner
๐Ÿ‘จโ€๐Ÿš€ token-agent4token-agenttoken agent
๐Ÿ‘ฉ alice5alicetoken holderhas an identity stored in the token's Identity registry
๐Ÿ‘ฑ๐Ÿผโ€โ™‚๏ธ bob6bobtoken holderhas an identity stored in the token's Identity registry
๐Ÿ‘ฑ๐Ÿผโ€โ™‚๏ธ charlie7charlietoken holderhas an identity stored in the token's Identity registry
๐Ÿ‘ฑ๐Ÿผโ€โ™‚๏ธ david8davidtoken holderhas an identity stored in the token's Identity registry
๐Ÿ‘ฉ eve9evetoken holderhas an identity stored in the token's Identity registry
๐Ÿฆˆ MEGALODON__TREX tokenthe deployed TREX token

Limitations:

  • For debugging reasons, all events are also emitting the encrypted handles. This should be removed if necessary.
  • country is still un-encrypted. (Future versions)
  • On the FHEVM node: Some tests are running when manually executed using the CLI while failing when run via Chai + Mocha
  • DVA has been ported but the corresponding CLI is not available
  • Some CLI commands are missing (transfer-from, etc.)
  • Modules that have not been converted yet: ConditionalTransferModule, MaxBalanceModule, TransferFeesModule.