Skip to content

Commit

Permalink
crypto: truncated addresses are renamed to transparent addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
redshiftzero committed Dec 10, 2024
1 parent 8a33f27 commit 00e4bc7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
8 changes: 4 additions & 4 deletions crates/core/keys/src/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub use view::AddressView;

use crate::{fmd, ka, keys::Diversifier};

pub const TRUNCATED_ADDRESS_BECH32_PREFIX: &str = "ptrunc";
pub const TRANSPARENT_ADDRESS_BECH32_PREFIX: &str = "tpenumbra";

/// The length of an [`Address`] in bytes.
pub const ADDRESS_LEN_BYTES: usize = 80;
Expand Down Expand Up @@ -321,11 +321,11 @@ impl std::str::FromStr for Address {
type Err = anyhow::Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
if s.starts_with(TRUNCATED_ADDRESS_BECH32_PREFIX) {
if s.starts_with(TRANSPARENT_ADDRESS_BECH32_PREFIX) {
let dzero = Diversifier([0u8; 16]);

let pk_dzero_bytes: [u8; 32] =
bech32str::decode(s, TRUNCATED_ADDRESS_BECH32_PREFIX, bech32str::Bech32)?
bech32str::decode(s, TRANSPARENT_ADDRESS_BECH32_PREFIX, bech32str::Bech32)?
.try_into()
.map_err(|bytes: Vec<u8>| {
anyhow::anyhow!("wrong length {}, expected 32", bytes.len())
Expand All @@ -335,7 +335,7 @@ impl std::str::FromStr for Address {
let ck_id = fmd::ClueKey([0u8; 32]);

Self::from_components(dzero, pk_dzero, ck_id)
.ok_or_else(|| anyhow::anyhow!("could not reconstruct truncated address"))
.ok_or_else(|| anyhow::anyhow!("could not reconstruct transparent address"))
} else if s.starts_with(bech32str::compat_address::BECH32_PREFIX) {
pb::Address {
inner: bech32str::decode(
Expand Down
18 changes: 9 additions & 9 deletions crates/core/keys/src/keys/ivk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,19 @@ impl IncomingViewingKey {
)
}

/// Derive the (encoding of the) truncated address for the given IVK.
/// Derive the (encoding of the) transparent address for the given IVK.
///
/// This intentionally returns a `String` rather than an `Address`, as it's not
/// safe to truncate arbitrary addresses.
pub fn truncated_address(&self) -> String {
// The truncated address uses an all-zero diversifier.
pub fn transparent_address(&self) -> String {
// The transparent address uses an all-zero diversifier.
let dzero = Diversifier([0u8; 16]);
let g_dzero = dzero.diversified_generator();
let pk_dzero = self.ivk.diversified_public(&g_dzero);

let encoding = bech32str::encode(
&pk_dzero.0,
crate::address::TRUNCATED_ADDRESS_BECH32_PREFIX,
crate::address::TRANSPARENT_ADDRESS_BECH32_PREFIX,
bech32str::Bech32,
);

Expand Down Expand Up @@ -206,17 +206,17 @@ mod test {
use super::*;

#[test]
fn truncated_address_generation_and_parsing() {
fn transparent_address_generation_and_parsing() {
let rng = rand::rngs::OsRng;
let spend_key =
SpendKey::from_seed_phrase_bip44(SeedPhrase::generate(rng), &Bip44Path::new(0));
let ivk = spend_key.full_viewing_key().incoming();

let truncated_address_str = ivk.truncated_address();
let transparent_address_str = ivk.transparent_address();

let reconstructed: Address = truncated_address_str
let reconstructed: Address = transparent_address_str
.parse()
.expect("can parse truncated address");
.expect("can parse transparent address");

assert!(ivk.views_address(&reconstructed));

Expand All @@ -234,7 +234,7 @@ mod test {
// The clue keys should not match, as the clue key is zeroed out
assert_ne!(reconstructed.clue_key(), actual_address.clue_key());

println!("Truncated address: {}", truncated_address_str);
println!("Transparent address: {}", transparent_address_str);
println!("Reconstructed address: {}", reconstructed);
println!("Address index: {:?}", address_index);
println!("Actual address for index: {}", actual_address);
Expand Down

0 comments on commit 00e4bc7

Please sign in to comment.