Skip to content

Commit

Permalink
adjust func
Browse files Browse the repository at this point in the history
  • Loading branch information
grod220 committed Oct 16, 2024
1 parent 1f90f2a commit 593fae5
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 24 deletions.
36 changes: 22 additions & 14 deletions packages/wasm/crate/src/keys.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::str::FromStr;

use anyhow;
use penumbra_keys::keys::{AddressIndex, Bip44Path, SeedPhrase, SpendKey};
use penumbra_keys::{Address, FullViewingKey};
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 @@ -137,17 +136,34 @@ 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_forwarding_address_for_sequence(
pub fn get_noble_forwarding_addr(
sequence: u16,
full_viewing_key: &[u8],
channel: &str,
account: Option<u32>,
) -> WasmResult<Vec<u8>> {
) -> WasmResult<ForwardingAddrResponse> {
let fvk: FullViewingKey = FullViewingKey::decode(full_viewing_key)?;
let addr = forwarding_addr_inner(sequence, account, &fvk);
Ok(addr.encode_to_vec())
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)
Expand All @@ -165,11 +181,3 @@ pub fn forwarding_addr_inner(sequence: u16, account: Option<u32>, fvk: &FullView

address
}

/// Generates Bech32m noble address
#[wasm_bindgen]
pub fn generate_noble_addr(address: &[u8], channel: &str) -> WasmResult<String> {
let address: Address = Address::decode(address)?;
let forwarding_addr = address.noble_forwarding_address(channel);
Ok(forwarding_addr.to_string())
}
30 changes: 20 additions & 10 deletions packages/wasm/src/keys.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import {
generate_noble_addr,
generate_spend_key,
get_address_by_index,
get_ephemeral_address,
get_forwarding_address_for_sequence,
get_full_viewing_key,
get_noble_forwarding_addr,
get_wallet_id,
} from '../wasm/index.js';
import {
Expand Down Expand Up @@ -33,15 +32,26 @@ export const getEphemeralByIndex = (fullViewingKey: FullViewingKey, index: numbe
export const getWalletId = (fullViewingKey: FullViewingKey) =>
WalletId.fromBinary(get_wallet_id(fullViewingKey.toBinary()));

export const getForwardingAddressForSequence = (
export interface NobleAddrResponse {
// A noble address that will be used for registration on the noble network
nobleAddrBech32: string;
// Byte representation of the noble forwarding address. Used for broadcasting cosmos message.
nobleAddrBytes: Uint8Array;
// The penumbra address that a deposit to the noble address with forward to
penumbraAddr: Address;
}

// Generates an address that can be used as a forwarding address for Noble
export const getNobleForwardingAddr = (
sequence: number,
fvk: FullViewingKey,
channel: string,
account?: number,
): Address => {
const res = get_forwarding_address_for_sequence(sequence, fvk.toBinary(), account);
return Address.fromBinary(res);
};

export const generateNobleAddr = (address: Address, channel: string): string => {
return generate_noble_addr(address.toBinary(), channel);
): NobleAddrResponse => {
const res = get_noble_forwarding_addr(sequence, fvk.toBinary(), channel, account);
return {
nobleAddrBech32: res.noble_addr_bech32,
nobleAddrBytes: res.noble_addr_bytes,
penumbraAddr: Address.fromBinary(res.penumbra_addr_bytes),
};
};

0 comments on commit 593fae5

Please sign in to comment.