example/wasm-hello-world: rmrfed

This commit is contained in:
skoupidi
2026-01-02 18:17:20 +02:00
parent 28659e261f
commit e579f5a6ab
11 changed files with 0 additions and 707 deletions

View File

@@ -1,2 +0,0 @@
Cargo.lock
target

View File

@@ -1,25 +0,0 @@
[package]
name = "wasm_hello_world"
version = "0.0.1"
authors = ["Dyne.org foundation <foundation@dyne.org>"]
license = "AGPL-3.0-only"
edition = "2021"
[workspace]
[lib]
crate-type = ["cdylib", "rlib"]
[dependencies]
darkfi-sdk = { path = "../../src/sdk", features = ["wasm"] }
darkfi-serial = { path = "../../src/serial", features = ["derive", "crypto"] }
# We need to disable random using "custom" which makes the crate a noop
# so the wasm32-unknown-unknown target is enabled.
[target.'cfg(target_arch = "wasm32")'.dependencies]
getrandom = { version = "0.2.8", features = ["custom"] }
[features]
default = []
no-entrypoint = []
client = [ "darkfi-serial/async" ]

View File

@@ -1,45 +0,0 @@
.POSIX:
# Cargo binary
CARGO = cargo
# wasm build target
WASM_TARGET = wasm32-unknown-unknown
# Cargo package name
PKGNAME = $(shell grep '^name = ' Cargo.toml | cut -d' ' -f3 | tr -d '"')
# wasm contract binary
WASM_BIN = $(PKGNAME:=.wasm)
# zkas compiler binary
ZKAS = "../../zkas"
# zkas circuits
PROOFS_SRC = $(shell find proof -type f -name '*.zk')
PROOFS_BIN = $(PROOFS_SRC:=.bin)
# wasm source files
WASM_SRC = \
Cargo.toml \
$(shell find src -type f -name '*.rs')
all: $(WASM_BIN)
$(PROOFS_BIN): $(PROOFS_SRC)
$(ZKAS) $(basename $@) -o $@
$(WASM_BIN): $(WASM_SRC) $(PROOFS_BIN)
RUSTFLAGS="$(RUSTFLAGS)" $(CARGO) build --target=$(WASM_TARGET) \
--release --package $(PKGNAME)
cp -f target/$(WASM_TARGET)/release/$@ $@
clippy: all
RUSTFLAGS="$(RUSTFLAGS)" $(CARGO) clippy --target=$(WASM_TARGET) \
--release --package $(PKGNAME)
clean:
RUSTFLAGS="$(RUSTFLAGS)" $(CARGO) clean --target=$(WASM_TARGET) \
--release --package $(PKGNAME)
rm -f $(PROOFS_BIN) $(WASM_BIN)
.PHONY: all clippy clean

View File

@@ -1,4 +0,0 @@
/target
Cargo.lock
rustfmt.toml
client

View File

@@ -1,29 +0,0 @@
[package]
name = "client"
version = "0.0.1"
description = "Basic client for wasm hello world example."
authors = ["Dyne.org foundation <foundation@dyne.org>"]
repository = "https://codeberg.org/darkrenaissance/darkfi"
license = "AGPL-3.0-only"
edition = "2021"
[workspace]
[dependencies]
# The contract
wasm_hello_world = {path = "../", features = [ "client" ]}
# Darkfi
darkfi = {path = "../../../", features = ["tx", "rpc"]}
darkfi-sdk = {path = "../../../src/sdk"}
darkfi-serial = {path = "../../../src/serial"}
# Misc
clap = {version = "4.4.11", features = ["derive"]}
rand = "0.8.5"
smol = "2.0.2"
url = "2.5.7"
[patch.crates-io]
halo2_proofs = {git="https://github.com/parazyd/halo2", branch="v031"}
halo2_gadgets = {git="https://github.com/parazyd/halo2", branch="v031"}

View File

@@ -1,37 +0,0 @@
.POSIX:
# Install prefix
PREFIX = $(HOME)/.cargo
# Cargo binary
CARGO = cargo
# Compile target
RUST_TARGET = $(shell rustc -Vv | grep '^host: ' | cut -d' ' -f2)
# Uncomment when doing musl static builds
#RUSTFLAGS = -C target-feature=+crt-static -C link-self-contained=yes
SRC = \
Cargo.toml \
$(shell find src -type f -name '*.rs') \
BIN = $(shell grep '^name = ' Cargo.toml | cut -d' ' -f3 | tr -d '"')
all: $(BIN)
$(BIN): $(SRC)
RUSTFLAGS="$(RUSTFLAGS)" $(CARGO) build --target=$(RUST_TARGET) --release --package $@
cp -f target/$(RUST_TARGET)/release/$@ $@
fmt:
$(CARGO) +nightly fmt --all
clippy:
RUSTFLAGS="$(RUSTFLAGS)" $(CARGO) clippy --target=$(RUST_TARGET) \
--release --all-features --workspace --tests
clean:
RUSTFLAGS="$(RUSTFLAGS)" $(CARGO) clean --target=$(RUST_TARGET) --release --package $(BIN)
rm -f $(BIN)
.PHONY: all fmt clippy clean

View File

@@ -1,57 +0,0 @@
/* This file is part of DarkFi (https://dark.fi)
*
* Copyright (C) 2020-2026 Dyne.org foundation
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use darkfi::{
zk::{halo2::Value, Proof, ProvingKey, Witness, ZkCircuit},
zkas::ZkBinary,
Result,
};
use darkfi_sdk::crypto::Keypair;
use rand::rngs::OsRng;
use wasm_hello_world::HelloParams;
pub struct ContractCallDebris {
pub params: HelloParams,
pub proofs: Vec<Proof>,
}
/// Struct holding necessary information to build a wasm-hello-world contract call.
pub struct ContractCallBuilder {
/// Member keypair this call is for
pub member: Keypair,
/// `SecretCommitment` zkas circuit ZkBinary
pub commitment_zkbin: ZkBinary,
/// Proving key for the `SecretCommitment` zk circuit,
pub commitment_pk: ProvingKey,
}
impl ContractCallBuilder {
pub fn build(&self) -> Result<ContractCallDebris> {
// Build the commitment proof
let prover_witnesses = vec![Witness::Base(Value::known(self.member.secret.inner()))];
let (public_x, public_y) = self.member.public.xy();
let public_inputs = vec![public_x, public_y];
let circuit = ZkCircuit::new(prover_witnesses, &self.commitment_zkbin);
let proof = Proof::create(&self.commitment_pk, &[circuit], &public_inputs, &mut OsRng)?;
// Generate the params and call debris
let params = HelloParams { x: public_x, y: public_y };
let debris = ContractCallDebris { params, proofs: vec![proof] };
Ok(debris)
}
}

View File

@@ -1,283 +0,0 @@
/* This file is part of DarkFi (https://dark.fi)
*
* Copyright (C) 2020-2026 Dyne.org foundation
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use std::{collections::BTreeMap, str::FromStr, sync::Arc};
use clap::{Parser, Subcommand};
use darkfi::{
cli_desc,
rpc::{client::RpcClient, jsonrpc::JsonRequest, util::JsonValue},
tx::{ContractCallLeaf, TransactionBuilder},
util::encoding::base64,
zk::{empty_witnesses, ProvingKey, ZkCircuit},
zkas::ZkBinary,
Error, Result,
};
use darkfi_sdk::{
crypto::{ContractId, Keypair, PublicKey, SecretKey},
pasta::pallas,
ContractCall,
};
use darkfi_serial::{deserialize, serialize, Encodable};
use smol::Executor;
use url::Url;
use wasm_hello_world::{
ContractFunction, HELLO_CONTRACT_MEMBER_TREE, HELLO_CONTRACT_ZKAS_SECRETCOMMIT_NS,
};
mod commitment;
use commitment::ContractCallBuilder;
#[derive(Parser)]
#[command(about = cli_desc!())]
struct Args {
#[arg(short, long)]
/// Deployed Contract ID
contract_id: String,
#[arg(short, long, default_value = "tcp://127.0.0.1:8340")]
/// darkfid JSON-RPC endpoint
endpoint: Url,
#[command(subcommand)]
command: Subcmd,
}
#[derive(Subcommand)]
enum Subcmd {
/// Display current members
List {
/// Specific member to check if present (optional)
member: Option<String>,
},
/// Generate a transaction adding a new member
Register {
/// To be added member secret key
member_secret: String,
},
/// Generate a transaction removing a member
Deregister {
/// To be removed member secret key
member_secret: String,
},
}
fn main() -> Result<()> {
// Parse arguments
let args = Args::parse();
let contract_id = match ContractId::from_str(&args.contract_id) {
Ok(c) => c,
Err(e) => {
eprintln!("Invalid contract id: {e}");
return Err(Error::ParseFailed("Invalid contract id"));
}
};
// Initialize an executor
let executor = Arc::new(Executor::new());
smol::block_on(executor.run(async {
// Initialize an rpc client
let rpc_client = RpcClient::new(args.endpoint, executor.clone()).await?;
// Execute a subcommand
match args.command {
Subcmd::List { member } => {
match member {
// Check if specific member exists in our contract members tree
Some(member) => {
// Parse the member public key
let member = PublicKey::from_str(&member)?;
// Create the request params
let params = JsonValue::Array(vec![
JsonValue::String(contract_id.to_string()),
JsonValue::String(HELLO_CONTRACT_MEMBER_TREE.to_string()),
JsonValue::String(member.to_string()),
]);
// Execute the request
let req = JsonRequest::new("blockchain.get_contract_state_key", params);
let rep = rpc_client.request(req).await?;
// Parse response
let bytes = base64::decode(rep.get::<String>().unwrap()).unwrap();
// Print info message
println!("Member {member} was found!");
println!("Value validity check: {}", bytes.is_empty());
}
// Retrieve all contract members tree records
None => {
// Create the request params
let params = JsonValue::Array(vec![
JsonValue::String(contract_id.to_string()),
JsonValue::String(HELLO_CONTRACT_MEMBER_TREE.to_string()),
]);
// Execute the request
let req = JsonRequest::new("blockchain.get_contract_state", params);
let rep = rpc_client.request(req).await?;
// Parse response
let bytes = base64::decode(rep.get::<String>().unwrap()).unwrap();
let members: BTreeMap<Vec<u8>, Vec<u8>> = deserialize(&bytes)?;
// Print records
println!("{contract_id} members:");
if members.is_empty() {
println!("No members found");
} else {
let mut index = 1;
for member in members.keys() {
let member: pallas::Base = deserialize(member)?;
println!("{index}. {member:?}");
index += 1;
}
}
}
}
}
Subcmd::Register { member_secret } => {
// Parse the member secret key
let member_secret = SecretKey::from_str(&member_secret)?;
let member = Keypair::new(member_secret);
// Now we need to do a lookup for the zkas proof bincodes, and create
// the circuit objects and proving keys so we can build the transaction.
// We also do this through the RPC.
let params = JsonValue::Array(vec![JsonValue::String(contract_id.to_string())]);
// Execute the request
let req = JsonRequest::new("blockchain.lookup_zkas", params);
let rep = rpc_client.request(req).await?;
let params = rep.get::<Vec<JsonValue>>().unwrap();
// Parse response
let mut zkas_bins = Vec::with_capacity(params.len());
for param in params {
let zkas_ns = param[0].get::<String>().unwrap().clone();
let zkas_bincode_bytes =
base64::decode(param[1].get::<String>().unwrap()).unwrap();
zkas_bins.push((zkas_ns, zkas_bincode_bytes));
}
let Some(commitment_zkbin) =
zkas_bins.iter().find(|x| x.0 == HELLO_CONTRACT_ZKAS_SECRETCOMMIT_NS)
else {
return Err(Error::Custom("Secret commitment circuit not found".to_string()))
};
let commitment_zkbin = ZkBinary::decode(&commitment_zkbin.1)?;
let commitment_circuit =
ZkCircuit::new(empty_witnesses(&commitment_zkbin)?, &commitment_zkbin);
// Creating secret commitment circuit proving keys
let commitment_pk = ProvingKey::build(commitment_zkbin.k, &commitment_circuit);
// Create the contract call
let builder = ContractCallBuilder { member, commitment_zkbin, commitment_pk };
let debris = builder.build()?;
// Encode the call
let mut data = vec![ContractFunction::Register as u8];
debris.params.encode(&mut data)?;
let call = ContractCall { contract_id, data };
// Create the TransactionBuilder containing above call
let mut tx_builder = TransactionBuilder::new(
ContractCallLeaf { call, proofs: debris.proofs },
vec![],
)?;
// Build the transaction and attach the corresponding signatures
let mut tx = tx_builder.build()?;
let sigs = tx.create_sigs(&[])?;
tx.signatures.push(sigs);
println!("{}", base64::encode(&serialize(&tx)));
}
Subcmd::Deregister { member_secret } => {
// Parse the member secret key
let member_secret = SecretKey::from_str(&member_secret)?;
let member = Keypair::new(member_secret);
// Now we need to do a lookup for the zkas proof bincodes, and create
// the circuit objects and proving keys so we can build the transaction.
// We also do this through the RPC.
let params = JsonValue::Array(vec![JsonValue::String(contract_id.to_string())]);
// Execute the request
let req = JsonRequest::new("blockchain.lookup_zkas", params);
let rep = rpc_client.request(req).await?;
let params = rep.get::<Vec<JsonValue>>().unwrap();
// Parse response
let mut zkas_bins = Vec::with_capacity(params.len());
for param in params {
let zkas_ns = param[0].get::<String>().unwrap().clone();
let zkas_bincode_bytes =
base64::decode(param[1].get::<String>().unwrap()).unwrap();
zkas_bins.push((zkas_ns, zkas_bincode_bytes));
}
let Some(commitment_zkbin) =
zkas_bins.iter().find(|x| x.0 == HELLO_CONTRACT_ZKAS_SECRETCOMMIT_NS)
else {
return Err(Error::Custom("Secret commitment circuit not found".to_string()))
};
let commitment_zkbin = ZkBinary::decode(&commitment_zkbin.1)?;
let commitment_circuit =
ZkCircuit::new(empty_witnesses(&commitment_zkbin)?, &commitment_zkbin);
// Creating secret commitment circuit proving keys
let commitment_pk = ProvingKey::build(commitment_zkbin.k, &commitment_circuit);
// Create the contract call
let builder = ContractCallBuilder { member, commitment_zkbin, commitment_pk };
let debris = builder.build()?;
// Encode the call
let mut data = vec![ContractFunction::Deregister as u8];
debris.params.encode(&mut data)?;
let call = ContractCall { contract_id, data };
// Create the TransactionBuilder containing above call
let mut tx_builder = TransactionBuilder::new(
ContractCallLeaf { call, proofs: debris.proofs },
vec![],
)?;
// Build the transaction and attach the corresponding signatures
let mut tx = tx_builder.build()?;
let sigs = tx.create_sigs(&[])?;
tx.signatures.push(sigs);
println!("{}", base64::encode(&serialize(&tx)));
}
}
// Stop the rpc client
rpc_client.stop().await;
Ok(())
}))
}

View File

@@ -1,24 +0,0 @@
# The k parameter defining the number of rows used in our circuit (2^k)
k = 11;
field = "pallas";
# The constants we define for our circuit
constant "SecretCommitment" {
EcFixedPointBase NULLIFIER_K,
}
# The witness values we define in our circuit
witness "SecretCommitment" {
# Secret key used to derive the public key
Base secret,
}
# The definition of our circuit
circuit "SecretCommitment" {
# Derive the public key
pub = ec_mul_base(secret, NULLIFIER_K);
# Constrain the public key coordinates
constrain_instance(ec_get_x(pub));
constrain_instance(ec_get_y(pub));
}

View File

@@ -1,140 +0,0 @@
/* This file is part of DarkFi (https://dark.fi)
*
* Copyright (C) 2020-2026 Dyne.org foundation
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use darkfi_sdk::{
crypto::{poseidon_hash, ContractId, PublicKey},
dark_tree::DarkLeaf,
error::ContractResult,
msg,
pasta::pallas,
wasm::{
self,
db::{db_contains_key, db_del, db_init, db_lookup, db_set, zkas_db_set},
},
ContractCall, ContractError,
};
use darkfi_serial::{deserialize, serialize, Encodable};
use crate::{
ContractFunction, HelloParams, HELLO_CONTRACT_MEMBER_TREE, HELLO_CONTRACT_ZKAS_SECRETCOMMIT_NS,
};
darkfi_sdk::define_contract!(
init: init_contract,
exec: process_instruction,
apply: process_update,
metadata: get_metadata
);
/// This entrypoint function runs when the contract is (re)deployed and initialized.
/// We use this function to init all the necessary databases and prepare them with
/// initial data if necessary.
/// This is also the place where we bundle the zkas circuits that are to be used
/// with functions provided by the contract.
fn init_contract(cid: ContractId, _ix: &[u8]) -> ContractResult {
// zkas circuits can simply be embedded in the wasm and set up by using
// respective db functions.
// The special `zkas db` operations exist in order to be able to verify
// the circuits being bundled and enforcing a specific tree inside sled,
// and dlso creation of VerifyingKey.
let circuit_bincode = include_bytes!("../proof/secret_commitment.zk.bin");
// For that, we use `zkas_db_set` and pass in the bincode.
zkas_db_set(&circuit_bincode[..])?;
// Now we also want to create our own database to hold things.
// This `lookup || init` method is a redeployment guard.
if db_lookup(cid, HELLO_CONTRACT_MEMBER_TREE).is_err() {
db_init(cid, HELLO_CONTRACT_MEMBER_TREE)?;
}
Ok(())
}
fn get_metadata(_cid: ContractId, ix: &[u8]) -> ContractResult {
let call_idx = wasm::util::get_call_index()? as usize;
let calls: Vec<DarkLeaf<ContractCall>> = deserialize(ix)?;
let self_ = &calls[call_idx].data;
let _func = ContractFunction::try_from(self_.data[0])?;
// Deserialize the call parameters
let params: HelloParams = deserialize(&self_.data[1..])?;
// Public inputs for the ZK proofs we have to verify
let mut zk_public_inputs: Vec<(String, Vec<pallas::Base>)> = vec![];
// Public keys for the transaction signatures we have to verify
let signature_pubkeys: Vec<PublicKey> = vec![];
zk_public_inputs
.push((HELLO_CONTRACT_ZKAS_SECRETCOMMIT_NS.to_string(), vec![params.x, params.y]));
// Serialize everything gathered and return it
let mut metadata = vec![];
zk_public_inputs.encode(&mut metadata)?;
signature_pubkeys.encode(&mut metadata)?;
wasm::util::set_return_data(&metadata)
}
fn process_instruction(cid: ContractId, ix: &[u8]) -> ContractResult {
let call_idx = wasm::util::get_call_index()? as usize;
let calls: Vec<DarkLeaf<ContractCall>> = deserialize(ix)?;
let self_ = &calls[call_idx].data;
let func = ContractFunction::try_from(self_.data[0])?;
// Deserialize the call parameters
let params: HelloParams = deserialize(&self_.data[1..])?;
// Open the db
let db_members = db_lookup(cid, HELLO_CONTRACT_MEMBER_TREE)?;
// Pubkey commitment
let commitment = poseidon_hash([params.x, params.y]);
match func {
ContractFunction::Register => {
if db_contains_key(db_members, &serialize(&commitment))? {
msg!("Error: Member already in database");
return Err(ContractError::Custom(1))
}
}
ContractFunction::Deregister => {
if !db_contains_key(db_members, &serialize(&commitment))? {
msg!("Error: Member not in database");
return Err(ContractError::Custom(2))
}
}
}
wasm::util::set_return_data(&serialize(&commitment))
}
fn process_update(cid: ContractId, update_data: &[u8]) -> ContractResult {
let func = ContractFunction::try_from(update_data[0])?;
let db_members = db_lookup(cid, HELLO_CONTRACT_MEMBER_TREE)?;
let commitment = &update_data[1..];
match func {
ContractFunction::Register => db_set(db_members, commitment, &[])?,
ContractFunction::Deregister => db_del(db_members, commitment)?,
}
Ok(())
}

View File

@@ -1,61 +0,0 @@
/* This file is part of DarkFi (https://dark.fi)
*
* Copyright (C) 2020-2026 Dyne.org foundation
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use darkfi_sdk::{error::ContractError, pasta::pallas};
use darkfi_serial::{SerialDecodable, SerialEncodable};
#[cfg(feature = "client")]
use darkfi_serial::async_trait;
/// Functions available in the contract
#[repr(u8)]
pub enum ContractFunction {
Register = 0x00,
Deregister = 0x01,
}
impl TryFrom<u8> for ContractFunction {
type Error = ContractError;
fn try_from(b: u8) -> Result<Self, Self::Error> {
match b {
0x00 => Ok(Self::Register),
0x01 => Ok(Self::Deregister),
_ => Err(ContractError::InvalidFunction),
}
}
}
/// Function parameters
#[derive(Debug, Clone, Copy, SerialEncodable, SerialDecodable)]
pub struct HelloParams {
/// X coordinate of the public key
pub x: pallas::Base,
/// Y coordinate of the public key
pub y: pallas::Base,
}
#[cfg(not(feature = "no-entrypoint"))]
/// WASM entrypoint functions
pub mod entrypoint;
/// This is a sled tree that will be created
pub const HELLO_CONTRACT_MEMBER_TREE: &str = "members";
/// zkas circuit namespace
pub const HELLO_CONTRACT_ZKAS_SECRETCOMMIT_NS: &str = "SecretCommitment";