From 13a3f49f80841c28e8e358be7f69f79c58e70109 Mon Sep 17 00:00:00 2001 From: rooooooooob Date: Wed, 7 Aug 2019 11:23:13 -0700 Subject: [PATCH] Testnet address generation to wallet-wasm. The motivation to change this part of the deprecated `wallet-wasm` library was the simple introduction of testnet address support in yoroi-mobile. The alternative is to migrate all of yoroi-mobile away to use the new `cardano-wallet` module, but this would be significant work in comparasin and can be delayed. Directly adapating `react-native-cardano` to use `cardano-wallet` was not feasable either as the API is quite different and would be exceptionally awkward to try and force into the `react-native-cardano` API that was built on top of `wallet-wasm` with its API in mind. Only `xwallet_account()` was changed, as the other function lacking support for testnet, `wallet_public_to_address()` does not seem to be used anywhere in react-native-cardano, which was the motivation for this change. Changing `wallet_public_to_address()` would also be more difficult as it requires a hard API change or an additional function to allow overloading. On the other hand, `xwallet_account()` supports a JSON API, which conveniently allows us to be backwards compatable, as the absence of the `protocol_magic` field will revert to the previous behavior of default (mainnet) protocol magic. --- wallet-wasm/src/lib.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/wallet-wasm/src/lib.rs b/wallet-wasm/src/lib.rs index 8b11809..946092e 100644 --- a/wallet-wasm/src/lib.rs +++ b/wallet-wasm/src/lib.rs @@ -1058,6 +1058,8 @@ struct GenAddressesInput { account: Bip44Account, address_type: bip44::AddrType, indices: Vec, + // For backwards compatability, absence of this field means mainnet + protocol_magic: Option, } #[no_mangle] @@ -1072,6 +1074,10 @@ pub extern "C" fn xwallet_addresses( output_ptr, account.change(input.account.derivation_scheme, input.address_type) ); + let network_magic = match input.protocol_magic { + Some(pm) => cardano::config::NetworkMagic::from(pm), + None => default_network_magic(), + }; let mut addresses: Vec = Vec::with_capacity(input.indices.len()); for index in input.indices.into_iter() { @@ -1079,7 +1085,7 @@ pub extern "C" fn xwallet_addresses( output_ptr, changelevel.index(input.account.derivation_scheme, index) ); - let addr = address::ExtendedAddr::new_simple(*xpub, default_network_magic()); + let addr = address::ExtendedAddr::new_simple(*xpub, network_magic); addresses.push(addr); } jrpc_ok!(output_ptr, addresses)