Example/Template DarkFi smart contract

skoupidi 8b44418fa7 chore: minor cleanups 8 ay önce
client 8b44418fa7 chore: minor cleanups 8 ay önce
proof b52f11d6ce Add project 8 ay önce
src 8b44418fa7 chore: minor cleanups 8 ay önce
.gitignore b52f11d6ce Add project 8 ay önce
Cargo.lock b52f11d6ce Add project 8 ay önce
Cargo.toml b52f11d6ce Add project 8 ay önce
LICENSE.md b52f11d6ce Add project 8 ay önce
Makefile b52f11d6ce Add project 8 ay önce
README.md b52f11d6ce Add project 8 ay önce
flake.lock b52f11d6ce Add project 8 ay önce
flake.nix b52f11d6ce Add project 8 ay önce

README.md

DarkFi template smart contract

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.

Compiling

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.

Deterministic WASM build

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 .

Deploying on DarkFi

TODO

Creating a transaction using this smart contract

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)

License

GNU AGPL-3