Skip to content

Commit

Permalink
remove unused public spend key wallet method
Browse files Browse the repository at this point in the history
  • Loading branch information
artifex11 committed Aug 27, 2023
1 parent 0ec45f1 commit e7f8dc8
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 94 deletions.
63 changes: 36 additions & 27 deletions assets/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -139,33 +139,6 @@
}
}
},
"PublicSpendKeyArgs": {
"description": "The arguments of the public_spend_key function",
"type": "object",
"required": [
"idx",
"seed"
],
"properties": {
"idx": {
"description": "The index of the public spend key",
"type": "integer",
"format": "uint64",
"minimum": 0
},
"seed": {
"description": "Seed used to derive the keys of the wallet",
"type": "array",
"items": {
"type": "integer",
"format": "uint8",
"minimum": 0
},
"maxItems": 64,
"minItems": 64
}
}
},
"ExecuteArgs": {
"description": "The arguments of the execute function",
"type": "object",
Expand Down Expand Up @@ -326,6 +299,42 @@
}
}
},
"PublicSpendKeysArgs": {
"description": "The arguments of the public_spend_keys function",
"type": "object",
"required": [
"seed"
],
"properties": {
"seed": {
"description": "Seed used to derive the keys of the wallet",
"type": "array",
"items": {
"type": "integer",
"format": "uint8",
"minimum": 0
},
"maxItems": 64,
"minItems": 64
}
}
},
"PublicSpendKeysResponse": {
"description": "The response of the public_spend_keys function",
"type": "object",
"required": [
"keys"
],
"properties": {
"keys": {
"description": "The Base58 public spend keys of the wallet.",
"type": "array",
"items": {
"type": "string"
}
}
}
},
"ViewKeysArgs": {
"description": "The arguments of the view_keys function",
"type": "object",
Expand Down
4 changes: 3 additions & 1 deletion build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ fn main() {
use alloc::vec::Vec;
use alloc::string::String;
use serde::{Serialize, Deserialize};"#;
use serde::{Serialize, Deserialize};
"#;

let contents = header.to_owned() + &contents;

Expand Down
58 changes: 28 additions & 30 deletions src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,36 +60,6 @@ pub fn seed(args: i32, len: i32) -> i64 {
utils::compose(true, ptr, len)
}

/// Computes the public spend key from the given seed/index pair.
///
/// Expects as argument a fat pointer to a JSON string representing
/// [types::PublicSpendKeyArgs].
///
/// Will return a triplet (status, ptr, len) pointing to the Base58
/// representation of the public spend key.
#[no_mangle]
pub fn public_spend_key(args: i32, len: i32) -> i64 {
let types::PublicSpendKeyArgs { idx, seed } =
match utils::take_args(args, len) {
Some(a) => a,
None => return utils::fail(),
};

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

let psk = key::derive_psk(&seed, idx);
let psk = bs58::encode(psk.to_bytes()).into_string();

let ptr = psk.as_ptr() as u32;
let len = psk.len() as u32;

mem::forget(psk);
utils::compose(true, ptr, len)
}

/// Computes the total balance of the given notes.
///
/// Expects as argument a fat pointer to a JSON string representing
Expand Down Expand Up @@ -344,6 +314,34 @@ pub fn filter_notes(args: i32, len: i32) -> i64 {
utils::rkyv_into_ptr(notes)
}

/// Returns a list of [PublicSpendKey] that belongs to this wallet.
///
/// Expects as argument a fat pointer to a JSON string representing
/// [types::PublicSpendKeysArgs].
///
/// Will return a triplet (status, ptr, len) pointing to JSON string
/// representing [types::PublicSpendKeysResponse].
#[no_mangle]
pub fn public_spend_keys(args: i32, len: i32) -> i64 {
let types::PublicSpendKeysArgs { seed } = match utils::take_args(args, len)
{
Some(a) => a,
None => return utils::fail(),
};

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

let keys = (0..=MAX_KEY)
.map(|idx| key::derive_psk(&seed, idx as u64))
.map(|psk| bs58::encode(psk.to_bytes()).into_string())
.collect();

utils::into_ptr(types::PublicSpendKeysResponse { keys })
}

/// Returns a list of [ViewKey] that belongs to this wallet.
///
/// Expects as argument a fat pointer to a JSON string representing
Expand Down
17 changes: 11 additions & 6 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@

#![allow(missing_docs)]

use alloc::string::String;
use alloc::vec::Vec;
use serde::{Deserialize, Serialize};
use alloc::string::String;
use serde::{Serialize, Deserialize};

#[doc = " The arguments of the balance function"]
#[derive(Clone, PartialEq, Debug, Deserialize, Serialize)]
pub struct BalanceArgs {
Expand Down Expand Up @@ -116,14 +117,18 @@ pub enum OutputType {
Transparent,
Obfuscated,
}
#[doc = " The arguments of the public_spend_key function"]
#[doc = " The arguments of the public_spend_keys function"]
#[derive(Clone, PartialEq, Debug, Deserialize, Serialize)]
pub struct PublicSpendKeyArgs {
#[doc = " The index of the public spend key"]
pub idx: u64,
pub struct PublicSpendKeysArgs {
#[doc = " Seed used to derive the keys of the wallet"]
pub seed: Vec<u8>,
}
#[doc = " The response of the public_spend_keys function"]
#[derive(Clone, PartialEq, Debug, Deserialize, Serialize)]
pub struct PublicSpendKeysResponse {
#[doc = " The Base58 public spend keys of the wallet."]
pub keys: Vec<String>,
}
#[doc = " The arguments of the seed function"]
#[derive(Clone, PartialEq, Debug, Deserialize, Serialize)]
pub struct SeedArgs {
Expand Down
61 changes: 31 additions & 30 deletions tests/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

use dusk_bytes::Serializable;
use dusk_pki::PublicSpendKey;
use dusk_wallet_core::{tx, types, utils, MAX_LEN, RNG_SEED};
use dusk_wallet_core::{tx, types, utils, MAX_KEY, MAX_LEN, RNG_SEED};
use rusk_abi::ContractId;
use serde::{Deserialize, Serialize};
use serde_json::json;
Expand Down Expand Up @@ -45,29 +45,6 @@ fn balance_works() {
assert_eq!(maximum, 359);
}

#[test]
fn public_spend_key_works() {
let seed = [0xfa; RNG_SEED];

let mut wallet = Wallet::default();

let psk = wallet
.call(
"public_spend_key",
json!({
"seed": seed.to_vec(),
"idx": 3
}),
)
.take_memory();

let psk = bs58::decode(psk).into_vec().unwrap();
let mut psk_array = [0u8; PublicSpendKey::SIZE];

psk_array.copy_from_slice(&psk);
PublicSpendKey::from_bytes(&psk_array).unwrap();
}

#[test]
fn execute_works() {
let seed = [0xfa; RNG_SEED];
Expand All @@ -76,16 +53,15 @@ fn execute_works() {

let mut wallet = Wallet::default();

let psk = wallet
let types::PublicSpendKeysResponse { keys } = wallet
.call(
"public_spend_key",
"public_spend_keys",
json!({
"seed": seed.to_vec(),
"idx":5
}),
)
.take_memory();
let psk = String::from_utf8(psk).unwrap();
.take_contents();
let psk = &keys[0];

let mut contract = ContractId::uninitialized();
contract.as_bytes_mut().iter_mut().for_each(|b| *b = 0xfa);
Expand All @@ -105,7 +81,7 @@ fn execute_works() {
"openings": openings,
"output": {
"note_type": "Transparent",
"receiver": psk.clone(),
"receiver": psk,
"ref_id": 15,
"value": 10,
},
Expand Down Expand Up @@ -183,6 +159,31 @@ fn filter_notes_works() {
assert_eq!(notes, filtered);
}

#[test]
fn public_spend_keys_works() {
let seed = [0xfa; RNG_SEED];

let mut wallet = Wallet::default();

let types::PublicSpendKeysResponse { keys } = wallet
.call(
"public_spend_keys",
json!({
"seed": seed.to_vec(),
}),
)
.take_contents();

for key in &keys {
let key = bs58::decode(key).into_vec().unwrap();
let mut key_array = [0u8; PublicSpendKey::SIZE];
key_array.copy_from_slice(&key);
PublicSpendKey::from_bytes(&key_array).unwrap();
}

assert_eq!(keys.len(), MAX_KEY + 1);
}

#[test]
fn view_keys_works() {
let seed = [0xfa; RNG_SEED];
Expand Down

0 comments on commit e7f8dc8

Please sign in to comment.