Skip to content
This repository has been archived by the owner on Jan 16, 2025. It is now read-only.

Commit

Permalink
Send in the whole chunk and process it in wasm
Browse files Browse the repository at this point in the history
  • Loading branch information
Daksh14 committed Jan 6, 2024
1 parent 361484c commit 8355ef0
Show file tree
Hide file tree
Showing 10 changed files with 161 additions and 143 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.3.1]

- Change the whole api to support `wasm32-unkonwn-unknown`
- Set json as the function argument and return value format with types defined in assets/schema.json
- Add the compact module to make the web assembly API compatiblite on non rust endpoints

## [Old Changelog below]

## [Unreleased]

### Added
Expand Down
8 changes: 6 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
[package]
name = "dusk-wallet-core"
version = "0.21.6"
version = "0.21.8"
edition = "2021"
description = "The core functionality of the Dusk wallet"
license = "MPL-2.0"
respository = "https://github.com/dusk-network/wallet-core"
repository = "https://github.com/dusk-network/wallet-core"

[lib]
crate-type = ["cdylib", "rlib"]
Expand Down Expand Up @@ -75,3 +75,7 @@ wasmer = "=3.1"

[build-dependencies]
schemafy_lib = "0.6"

[profile.release]
lto = true
opt-level = 'z'
Binary file modified assets/dusk_wallet_core.wasm
Binary file not shown.
88 changes: 42 additions & 46 deletions assets/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -448,32 +448,29 @@
}
}
},
"RkyvTreeLeafResponse": {
"description": "The response of the public_spend_keys function",
"RkyvTreeLeafArgs": {
"description": "The arguments of the rkyv tree leaf function",
"type": "object",
"required": [
"block_height",
"note",
"last_pos"
"bytes",
"seed"
],
"properties": {
"block_height": {
"description": "The block height of the note.",
"type": "integer",
"format": "uint64"
},
"note": {
"description": "Bytes of note at the block_height",
"bytes": {
"description": "Bytes that are rkyv serialized into a phoenix_core::transaction::TreeLeaf",
"type": "array",
"items": {
"type": "integer",
"format": "uint8"
}
},
"last_pos": {
"description": "Last position of the note",
"type": "integer",
"format": "uint64"
"seed": {
"description": "Seed used to derive the keys of the wallet",
"type": "array",
"items": {
"type": "integer",
"format": "uint8"
}
}
}
},
Expand Down Expand Up @@ -576,49 +573,48 @@
}
}
},
"CheckNoteOwnershipArgs": {
"description": "Arguments of the check_note_ownership function",
"CheckNoteOwnershipResponse": {
"description": "Response of check_note_ownership function",
"type": "object",
"required": ["note", "seed"],
"required": ["notes", "last_pos", "block_heights", "public_spend_keys", "nullifiers"],
"properties": {
"note": {
"description": "A singular note we want to check the validity of",
"notes": {
"description": "The raw owned note",
"type": "array",
"items": {
"type": "integer",
"format": "uint8"
"type": "array",
"items": {
"type": "integer",
"format": "uint8"
}
}
},
"seed": {
"description": "The seed to generate the view keys from",
"last_pos": {
"description": "The last position of the note",
"type": "integer",
"format": "uint64"
},
"block_heights": {
"description": "The block heights of the notes in the same order the notes were returned seperated by comma",
"type": "string"
},
"public_spend_keys": {
"description": "The public spend keys of the notes in the same order the notes were returned",
"type": "array",
"items": {
"type": "integer",
"format": "uint8"
"type": "string"
}
}
}
},
"CheckNoteOwnershipResponse": {
"description": "Response of check_note_ownership function",
"type": "object",
"required": ["is_owned", "nullifier"],
"properties": {
"is_owned": {
"description": "Is the note owned by any of the view keys in the provided seed",
"type": "boolean"
},
"nullifier": {
"description": "Nullifier of the note that we were checking the ownership of",
"nullifiers": {
"description": "The nullifiers of the notes in the same order the notes were returned",
"type": "array",
"items": {
"type": "integer",
"format": "uint8"
"type": "array",
"items": {
"type": "integer",
"format": "uint8"
}
}
},
"public_spend_key": {
"description": "A base 58 encoded public spend key string",
"type": "string"
}
}
},
Expand Down
4 changes: 0 additions & 4 deletions build.sh

This file was deleted.

Binary file modified dusk-wallet-core-0.21.0.wasm
Binary file not shown.
110 changes: 70 additions & 40 deletions src/compat/crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,74 +4,104 @@
//
// Copyright (c) DUSK NETWORK. All rights reserved.

use core::mem::size_of;

use dusk_bls12_381::BlsScalar;
use dusk_bytes::Serializable;
use phoenix_core::Note;
use phoenix_core::{
transaction::{ArchivedTreeLeaf, TreeLeaf},
Note,
};

use alloc::vec::Vec;
use alloc::{string::ToString, vec::Vec};

use crate::alloc::borrow::ToOwned;
use crate::{
key::{self},
types::{self},
utils::{self},
MAX_KEY, MAX_LEN,
};

const TREE_LEAF_SIZE: usize = size_of::<ArchivedTreeLeaf>();

/// Returns true or false if the note is owned by the index
/// if its true then nullifier of that note if sent with it
#[no_mangle]
pub fn check_note_ownership(args: i32, len: i32) -> i64 {
// we just use BalanceArgs again as we don't want to add more cluter types
// when the data you want is the same
let types::CheckNoteOwnershipArgs { note, seed } =
match utils::take_args(args, len) {
Some(a) => a,
None => return utils::fail(),
};
let args = utils::take_args_raw(args, len);

let seed = &args[..64];
let leaves: &[u8] = &args[64..];

let seed = match utils::sanitize_seed(seed) {
let seed = match seed.try_into().ok() {
Some(s) => s,
None => return utils::fail(),
};

let note: Note = match rkyv::from_bytes(&note) {
Ok(n) => n,
Err(_) => return utils::fail(),
};

let mut is_owned: bool = false;
let mut nullifier_found = BlsScalar::default();
let mut psk_found: Option<dusk_pki::PublicSpendKey> = None;
let mut leaf_chunk = leaves.chunks_exact(TREE_LEAF_SIZE);
let mut last_pos = 0;

for idx in 0..=MAX_KEY {
let idx = idx as u64;
let view_key = key::derive_vk(&seed, idx);
let mut notes = Vec::new();
let mut nullifiers = Vec::new();
let mut block_heights = Vec::new();
let mut public_spend_keys = Vec::new();

if view_key.owns(&note) {
let ssk = key::derive_ssk(&seed, idx);
let nullifier = note.gen_nullifier(&ssk);

nullifier_found = nullifier;
is_owned = true;
psk_found = Some(ssk.public_spend_key());
for leaf_bytes in leaf_chunk.by_ref() {
let TreeLeaf { block_height, note } =
match rkyv::from_bytes(leaf_bytes).ok() {
Some(a) => a,
None => {
return utils::fail();
}
};

break;
last_pos = core::cmp::max(last_pos, *note.pos());

for idx in 0..=MAX_KEY {
let idx = idx as u64;
let view_key = key::derive_vk(&seed, idx);

if view_key.owns(&note) {
let ssk: dusk_pki::SecretSpendKey = key::derive_ssk(&seed, idx);
let nullifier = note.gen_nullifier(&ssk);

let nullifier_found =
match rkyv::to_bytes::<BlsScalar, MAX_LEN>(&nullifier).ok()
{
Some(n) => n.to_vec(),
None => return utils::fail(),
};

let psk_found = bs58::encode(ssk.public_spend_key().to_bytes())
.into_string();

let raw_note: Vec<u8> =
match rkyv::to_bytes::<Note, MAX_LEN>(&note).ok() {
Some(n) => n.to_vec(),
None => return utils::fail(),
};

notes.push(raw_note.to_owned());
block_heights.push(block_height);
public_spend_keys.push(psk_found);
nullifiers.push(nullifier_found);
}
}
}

let psk_found =
psk_found.map(|psk| bs58::encode(psk.to_bytes()).into_string());

let nullifier_found =
match rkyv::to_bytes::<BlsScalar, MAX_LEN>(&nullifier_found).ok() {
Some(n) => n.to_vec(),
None => return utils::fail(),
};
let block_heights = block_heights
.iter()
.map(|x| x.to_string())
.collect::<Vec<_>>()
.join(",");

utils::into_ptr(types::CheckNoteOwnershipResponse {
is_owned,
nullifier: nullifier_found,
public_spend_key: psk_found,
notes,
block_heights,
public_spend_keys,
nullifiers,
last_pos,
})
}

Expand Down
29 changes: 1 addition & 28 deletions src/compat/rkyv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::{

use dusk_bls12_381::BlsScalar;
use dusk_bls12_381_sign::PublicKey;
use phoenix_core::{transaction::TreeLeaf, Note};
use phoenix_core::Note;

use alloc::vec::Vec;

Expand All @@ -28,33 +28,6 @@ pub fn rkyv_u64(args: i32, len: i32) -> i64 {
utils::rkyv_into_ptr(value)
}

/// Get block_heignt a rkyv serialized note from a tree leaf
#[no_mangle]
pub fn rkyv_tree_leaf(args: i32, len: i32) -> i64 {
let types::RkyvTreeLeaf { bytes } = match utils::take_args(args, len) {
Some(a) => a,
None => return utils::fail(),
};

let TreeLeaf { block_height, note } = match rkyv::from_bytes(&bytes) {
Ok(n) => n,
Err(_) => return utils::fail(),
};

let last_pos = *note.pos();

let note = match rkyv::to_bytes::<_, MAX_LEN>(&note).ok() {
Some(t) => t.into_vec(),
None => return utils::fail(),
};

utils::into_ptr(types::RkyvTreeLeafResponse {
block_height,
note,
last_pos,
})
}

/// Convert a Vec<Note> (where note is a U8initArray into a rkyv serialized
/// Vec<Note>
#[no_mangle]
Expand Down
Loading

0 comments on commit 8355ef0

Please sign in to comment.