Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Noble forwarding wasm helpers #1855

Merged
merged 4 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/funny-knives-refuse.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@penumbra-zone/wasm': minor
---

Added noble forwarding address wasm helpers
42 changes: 21 additions & 21 deletions packages/wasm/crate/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 16 additions & 15 deletions packages/wasm/crate/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,22 @@ default = ["console_error_panic_hook"]
mock-database = []

[dependencies]
penumbra-auction = { git = "https://github.com/penumbra-zone/penumbra.git", tag = "v0.80.5", package = "penumbra-auction", default-features = false }
penumbra-asset = { git = "https://github.com/penumbra-zone/penumbra.git", tag = "v0.80.5", package = "penumbra-asset" }
penumbra-compact-block = { git = "https://github.com/penumbra-zone/penumbra.git", tag = "v0.80.5", package = "penumbra-compact-block", default-features = false }
penumbra-dex = { git = "https://github.com/penumbra-zone/penumbra.git", tag = "v0.80.5", package = "penumbra-dex", default-features = false }
penumbra-fee = { git = "https://github.com/penumbra-zone/penumbra.git", tag = "v0.80.5", package = "penumbra-fee", default-features = false }
penumbra-governance = { git = "https://github.com/penumbra-zone/penumbra.git", tag = "v0.80.5", package = "penumbra-governance", default-features = false }
penumbra-keys = { git = "https://github.com/penumbra-zone/penumbra.git", tag = "v0.80.5", package = "penumbra-keys" }
penumbra-num = { git = "https://github.com/penumbra-zone/penumbra.git", tag = "v0.80.5", package = "penumbra-num" }
penumbra-proof-params = { git = "https://github.com/penumbra-zone/penumbra.git", tag = "v0.80.5", package = "penumbra-proof-params", default-features = false }
penumbra-proto = { git = "https://github.com/penumbra-zone/penumbra.git", tag = "v0.80.5", package = "penumbra-proto", default-features = false }
penumbra-sct = { git = "https://github.com/penumbra-zone/penumbra.git", tag = "v0.80.5", package = "penumbra-sct", default-features = false }
penumbra-shielded-pool = { git = "https://github.com/penumbra-zone/penumbra.git", tag = "v0.80.5", package = "penumbra-shielded-pool", default-features = false }
penumbra-stake = { git = "https://github.com/penumbra-zone/penumbra.git", tag = "v0.80.5", package = "penumbra-stake", default-features = false }
penumbra-tct = { git = "https://github.com/penumbra-zone/penumbra.git", tag = "v0.80.5", package = "penumbra-tct" }
penumbra-transaction = { git = "https://github.com/penumbra-zone/penumbra.git", tag = "v0.80.5", package = "penumbra-transaction", default-features = false }
# Update to tag dependency once https://github.com/penumbra-zone/penumbra/pull/4878 is in a release
penumbra-auction = { git = "https://github.com/penumbra-zone/penumbra.git", rev = "5e8ea2b12777e5956db6ea5c620877e5d1aa2de5", package = "penumbra-auction", default-features = false }
penumbra-asset = { git = "https://github.com/penumbra-zone/penumbra.git", rev = "5e8ea2b12777e5956db6ea5c620877e5d1aa2de5", package = "penumbra-asset" }
penumbra-compact-block = { git = "https://github.com/penumbra-zone/penumbra.git", rev = "5e8ea2b12777e5956db6ea5c620877e5d1aa2de5", package = "penumbra-compact-block", default-features = false }
penumbra-dex = { git = "https://github.com/penumbra-zone/penumbra.git", rev = "5e8ea2b12777e5956db6ea5c620877e5d1aa2de5", package = "penumbra-dex", default-features = false }
penumbra-fee = { git = "https://github.com/penumbra-zone/penumbra.git", rev = "5e8ea2b12777e5956db6ea5c620877e5d1aa2de5", package = "penumbra-fee", default-features = false }
penumbra-governance = { git = "https://github.com/penumbra-zone/penumbra.git", rev = "5e8ea2b12777e5956db6ea5c620877e5d1aa2de5", package = "penumbra-governance", default-features = false }
penumbra-keys = { git = "https://github.com/penumbra-zone/penumbra.git", rev = "5e8ea2b12777e5956db6ea5c620877e5d1aa2de5", package = "penumbra-keys" }
penumbra-num = { git = "https://github.com/penumbra-zone/penumbra.git", rev = "5e8ea2b12777e5956db6ea5c620877e5d1aa2de5", package = "penumbra-num" }
penumbra-proof-params = { git = "https://github.com/penumbra-zone/penumbra.git", rev = "5e8ea2b12777e5956db6ea5c620877e5d1aa2de5", package = "penumbra-proof-params", default-features = false }
penumbra-proto = { git = "https://github.com/penumbra-zone/penumbra.git", rev = "5e8ea2b12777e5956db6ea5c620877e5d1aa2de5", package = "penumbra-proto", default-features = false }
penumbra-sct = { git = "https://github.com/penumbra-zone/penumbra.git", rev = "5e8ea2b12777e5956db6ea5c620877e5d1aa2de5", package = "penumbra-sct", default-features = false }
penumbra-shielded-pool = { git = "https://github.com/penumbra-zone/penumbra.git", rev = "5e8ea2b12777e5956db6ea5c620877e5d1aa2de5", package = "penumbra-shielded-pool", default-features = false }
penumbra-stake = { git = "https://github.com/penumbra-zone/penumbra.git", rev = "5e8ea2b12777e5956db6ea5c620877e5d1aa2de5", package = "penumbra-stake", default-features = false }
penumbra-tct = { git = "https://github.com/penumbra-zone/penumbra.git", rev = "5e8ea2b12777e5956db6ea5c620877e5d1aa2de5", package = "penumbra-tct" }
penumbra-transaction = { git = "https://github.com/penumbra-zone/penumbra.git", rev = "5e8ea2b12777e5956db6ea5c620877e5d1aa2de5", package = "penumbra-transaction", default-features = false }

anyhow = "1.0.89"
ark-ff = { version = "0.4.2", features = ["std"] }
Expand Down
51 changes: 48 additions & 3 deletions packages/wasm/crate/src/keys.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use std::str::FromStr;

use anyhow;
use penumbra_keys::keys::{Bip44Path, SeedPhrase, SpendKey};
use penumbra_keys::keys::{AddressIndex, Bip44Path, SeedPhrase, SpendKey};
use penumbra_keys::{Address, FullViewingKey};
use penumbra_proof_params::{
CONVERT_PROOF_PROVING_KEY, DELEGATOR_VOTE_PROOF_PROVING_KEY, OUTPUT_PROOF_PROVING_KEY,
Expand All @@ -10,6 +8,7 @@ use penumbra_proof_params::{
use penumbra_proto::core::keys::v1 as pb;
use penumbra_proto::DomainType;
use rand_core::OsRng;
use std::str::FromStr;
use wasm_bindgen::prelude::*;

use crate::error::WasmResult;
Expand Down Expand Up @@ -136,3 +135,49 @@ pub fn is_controlled_address(full_viewing_key: &[u8], address: &[u8]) -> WasmRes
pub fn is_controlled_inner(fvk: &FullViewingKey, address: &Address) -> bool {
fvk.address_index(address).is_some()
}

#[wasm_bindgen(getter_with_clone)]
pub struct ForwardingAddrResponse {
/// A noble address that will be used for registration on the noble network
pub noble_addr_bech32: String,
/// Byte representation of the noble forwarding address. Used for broadcasting cosmos message.
pub noble_addr_bytes: Vec<u8>,
/// The penumbra address that a deposit to the noble address with forward to
/// Vec encoded `pb::Address`
pub penumbra_addr_bytes: Vec<u8>,
}

/// Generates an address that can be used as a forwarding address for Noble
/// Returns: Uint8Array representing encoded Address
#[wasm_bindgen]
pub fn get_noble_forwarding_addr(
sequence: u16,
full_viewing_key: &[u8],
channel: &str,
account: Option<u32>,
) -> WasmResult<ForwardingAddrResponse> {
let fvk: FullViewingKey = FullViewingKey::decode(full_viewing_key)?;
let penumbra_addr = forwarding_addr_inner(sequence, account, &fvk);
let noble_addr = penumbra_addr.noble_forwarding_address(channel);
Ok(ForwardingAddrResponse {
noble_addr_bech32: noble_addr.to_string(),
noble_addr_bytes: noble_addr.bytes(),
penumbra_addr_bytes: penumbra_addr.encode_to_vec(),
})
}

/// Noble Randomizer: [0xff; 10] followed by LE16(sequence)
pub fn forwarding_addr_inner(sequence: u16, account: Option<u32>, fvk: &FullViewingKey) -> Address {
let mut randomizer: [u8; 12] = [0xff; 12]; // Initialize all 12 bytes to 0xff
let seq_bytes = sequence.to_le_bytes();
randomizer[10..].copy_from_slice(&seq_bytes); // Replace the last 2 bytes with seq_bytes

let index = AddressIndex {
account: account.unwrap_or_default(),
randomizer,
};

let (address, _dtk) = fvk.incoming().payment_address(index);

address
}
Loading
Loading