Customizable diversified addresses

This commit is contained in:
Hanh 2022-12-21 03:55:12 +08:00
parent d1e59be7ad
commit 9ae96e7d88
3 changed files with 30 additions and 21 deletions

View File

@ -91,7 +91,7 @@ int8_t is_valid_key(uint8_t coin, char *key);
bool valid_address(uint8_t coin, char *address); bool valid_address(uint8_t coin, char *address);
struct CResult_____c_char new_diversified_address(void); struct CResult_____c_char new_diversified_address(uint8_t ua_type);
struct CResult_u32 get_latest_height(void); struct CResult_u32 get_latest_height(void);

View File

@ -185,7 +185,11 @@ pub fn import_transparent_secret_key(coin: u8, id_account: u32, sk: &str) -> any
} }
/// Generate a new diversified address /// Generate a new diversified address
pub fn new_diversified_address() -> anyhow::Result<String> { pub fn new_diversified_address(ua_type: u8) -> anyhow::Result<String> {
let ua_type = ua_type & 6; // don't include transparent component
if ua_type == 0 {
anyhow::bail!("Must include a shielded receiver");
}
let c = CoinConfig::get_active(); let c = CoinConfig::get_active();
let db = c.db()?; let db = c.db()?;
let AccountData { fvk, .. } = db.get_account_info(c.id_account)?; let AccountData { fvk, .. } = db.get_account_info(c.id_account)?;
@ -202,23 +206,28 @@ pub fn new_diversified_address() -> anyhow::Result<String> {
db.store_diversifier(c.id_account, &new_diversifier_index)?; db.store_diversifier(c.id_account, &new_diversifier_index)?;
let orchard_keys = db.get_orchard(c.id_account)?; let orchard_keys = db.get_orchard(c.id_account)?;
let address = match orchard_keys { if ua_type == 2 || orchard_keys.is_none() { // sapling only
Some(orchard_keys) => { return Ok(encode_payment_address(c.chain.network().hrp_sapling_payment_address(), &pa));
let orchard_fvk = FullViewingKey::from_bytes(&orchard_keys.fvk).unwrap(); }
let index = diversifier_index.0; // any sapling index is fine for orchard
let orchard_address = orchard_fvk.address_at(index, Scope::External); let orchard_keys = orchard_keys.unwrap();
let unified_address = UA(vec![ let mut receivers = vec![];
Receiver::Sapling(pa.to_bytes()), if ua_type & 2 != 0 {
Receiver::Orchard(orchard_address.to_raw_address_bytes()), receivers.push(Receiver::Sapling(pa.to_bytes()));
]); }
let address = ZcashAddress::from_unified( if ua_type & 4 != 0 {
c.chain.network().address_network().unwrap(), let orchard_fvk = FullViewingKey::from_bytes(&orchard_keys.fvk).unwrap();
unified_address, let index = diversifier_index.0; // any sapling index is fine for orchard
); let orchard_address = orchard_fvk.address_at(index, Scope::External);
address.encode() receivers.push(Receiver::Orchard(orchard_address.to_raw_address_bytes()));
} }
None => encode_payment_address(c.chain.network().hrp_sapling_payment_address(), &pa),
}; let unified_address = UA(receivers);
let address = ZcashAddress::from_unified(
c.chain.network().address_network().unwrap(),
unified_address,
);
let address = address.encode();
Ok(address) Ok(address)
} }

View File

@ -320,8 +320,8 @@ pub unsafe extern "C" fn valid_address(coin: u8, address: *mut c_char) -> bool {
} }
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn new_diversified_address() -> CResult<*mut c_char> { pub unsafe extern "C" fn new_diversified_address(ua_type: u8) -> CResult<*mut c_char> {
let res = || crate::api::account::new_diversified_address(); let res = || crate::api::account::new_diversified_address(ua_type);
to_cresult_str(res()) to_cresult_str(res())
} }