Example/Template DarkFi smart contract
|
|
8 месяцев назад | |
|---|---|---|
| client | 8 месяцев назад | |
| proof | 8 месяцев назад | |
| src | 8 месяцев назад | |
| .gitignore | 8 месяцев назад | |
| Cargo.lock | 8 месяцев назад | |
| Cargo.toml | 8 месяцев назад | |
| LICENSE.md | 8 месяцев назад | |
| Makefile | 8 месяцев назад | |
| README.md | 8 месяцев назад | |
| flake.lock | 8 месяцев назад | |
| flake.nix | 8 месяцев назад |
This repository is an example smart contract on DarkFi.
It shows how to write a Rust WASM contract that contains a ZK proof of knowing a secret key and shows how to create signatures.
The source code is entirely documented, so it's best recommended to read through it all to understand the structure.
lib.rs and entrypoint.rs define the actual smart contract, while
main.rs shows a simple tool that acts as the "client-side" of the
smart contract and explains how to create the contract calls that
become part of the transaction on DarkFi that will use this smart
contract.
The smart contract implements a simple membership proof and provides
two functions: Register and Deregister. These two functions simply
add or remove a public key commitment from a database on-chain,
provided that the functions' executions are valid and they pass.
The project requires the Rust WASM toolchain to compile the smart
contract, stable Rust to compile the helper client binary, and the
zkas compiler provided by DarkFi.
We can obtain the wasm32-unknown-unknown toolchain with rustup:
$ rustup target add wasm32-unknown-unknown
The zkas compiler can be compiled from the DarkFi repository:
$ git clone https://codeberg.org/darkrenaissance/darkfi && cd darkfi
$ make zkas
This project provides a convenient Makefile that
is able to compile everything. Just make sure to update the path
to the zkas compiler or have it in your $PATH.
$ make
Running make should result in having both membership and
membership.wasm in the git project root directory.
We can also use nix to produce a deterministic
build of the WASM module. This is useful when we want to prove this
source code matches the deployed binary on the DarkFi blockchain.
The provided flake.nix contains the code necessary to produce such
a build.
$ nix build && cp -f result/membership.wasm .
TODO
Once deployed, we should get a contract ID for our deployed contract.
For the example, let's assume it's 4eHp8tazRggmt8vhvs7To3rkUydPMrqDADKZYgEpr3uu.
We can use the membership tool to generate an identity:
$ ./membership generate
Secret key: efksR5iyVzpBsaWDuoUL47EFe9LMspdMVLqQCk5Mgvk
Public key: 9RQLjpVS93oevYpC5Tj3brfQXDpb63wqos813aSWgP1k
And then we can create a register call with our new secret key:
$ ./membership register 4eHp8tazRggmt8vhvs7To3rkUydPMrqDADKZYgEpr3uu efksR5iyVzpBsaWDuoUL47EFe9LMspdMVLqQCk5Mgvk > register.call
Once we have this call, we can use the drk wallet tool to create
the actual transaction and attach the fee call to pay any necessary
execution fees on-chain.
$ drk tx-from-calls < register.call | drk attach-fee > register.tx
$ drk broadcast < register.tx
(Refer to the DarkFi Book for more advanced usage)