From 593fae5c36df2f0b0003fc16a3d4b8db201b55e3 Mon Sep 17 00:00:00 2001 From: Gabe Rodriguez Date: Fri, 11 Oct 2024 15:53:51 +0200 Subject: [PATCH] adjust func --- packages/wasm/crate/src/keys.rs | 36 ++++++++++++++++++++------------- packages/wasm/src/keys.ts | 30 ++++++++++++++++++--------- 2 files changed, 42 insertions(+), 24 deletions(-) diff --git a/packages/wasm/crate/src/keys.rs b/packages/wasm/crate/src/keys.rs index a7cf03159..1838ff75c 100644 --- a/packages/wasm/crate/src/keys.rs +++ b/packages/wasm/crate/src/keys.rs @@ -1,5 +1,3 @@ -use std::str::FromStr; - use anyhow; use penumbra_keys::keys::{AddressIndex, Bip44Path, SeedPhrase, SpendKey}; use penumbra_keys::{Address, FullViewingKey}; @@ -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; @@ -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, + /// The penumbra address that a deposit to the noble address with forward to + /// Vec encoded `pb::Address` + pub penumbra_addr_bytes: Vec, +} + /// 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, -) -> WasmResult> { +) -> WasmResult { 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) @@ -165,11 +181,3 @@ pub fn forwarding_addr_inner(sequence: u16, account: Option, fvk: &FullView address } - -/// Generates Bech32m noble address -#[wasm_bindgen] -pub fn generate_noble_addr(address: &[u8], channel: &str) -> WasmResult { - let address: Address = Address::decode(address)?; - let forwarding_addr = address.noble_forwarding_address(channel); - Ok(forwarding_addr.to_string()) -} diff --git a/packages/wasm/src/keys.ts b/packages/wasm/src/keys.ts index 0afd60bed..24340e210 100644 --- a/packages/wasm/src/keys.ts +++ b/packages/wasm/src/keys.ts @@ -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 { @@ -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), + }; };