Skip to content

Commit

Permalink
Don't return error with custom remainder address and ledger nano (#1272)
Browse files Browse the repository at this point in the history
* Don't return error with custom remainder address and ledger nano

* Review comments

* Update sdk/src/client/api/block_builder/input_selection/core/remainder.rs

* Change get_remainder_address() to return a Result

---------

Co-authored-by: Thibault Martinez <[email protected]>
  • Loading branch information
Thoralf-M and thibault-martinez authored Sep 25, 2023
1 parent 1a09bbe commit 5ac334d
Show file tree
Hide file tree
Showing 11 changed files with 49 additions and 24 deletions.
2 changes: 1 addition & 1 deletion bindings/nodejs-old/examples/ledger_nano.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ require('dotenv').config({ path: path.resolve(__dirname, '.env') });
const { AccountManager, CoinType } = require('@iota/wallet');

// In this example we will create addresses with a ledger nano hardware wallet
// To use the ledger nano simulator clone https://github.com/iotaledger/ledger-shimmer-app, run `git submodule init && git submodule update --recursive`,
// To use the ledger nano simulator clone https://github.com/iotaledger/ledger-iota-app, run `git submodule init && git submodule update --recursive`,
// then `./build.sh -m nanos|nanox|nanosplus -s` and use `true` for `LedgerNano`.

async function run() {
Expand Down
5 changes: 5 additions & 0 deletions bindings/nodejs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## 1.0.12 - 2023-09-25

### Changed

- Made `TransactionOptions.allowMicroAmount` optional;

### Fixed

- Parsing of `RegularTransactionEssence.payload`;
- Don't error if custom remainder address is provided with ledger nano;

## 1.0.11 - 2023-09-14

Expand Down
2 changes: 1 addition & 1 deletion bindings/nodejs/examples/secret_manager/ledger-nano.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require('dotenv').config({ path: '.env' });
// yarn run-example ./secret_manager/ledger-nano.ts

// In this example we will get the ledger status and generate an address
// To use the ledger nano simulator clone https://github.com/iotaledger/ledger-shimmer-app, run `git submodule init && git submodule update --recursive`,
// To use the ledger nano simulator clone https://github.com/iotaledger/ledger-iota-app, run `git submodule init && git submodule update --recursive`,
// then `./build.sh -m nanos|nanox|nanosplus -s` and use `true` in `LedgerSecretManager::new(true)`.
async function run() {
initLogger();
Expand Down
2 changes: 1 addition & 1 deletion bindings/nodejs/lib/types/wallet/transaction-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export interface TransactionOptions {
/** Optional note, that is only stored locally. */
note?: string;
/** Whether to allow sending a micro amount. */
allowMicroAmount: boolean;
allowMicroAmount?: boolean;
}

/** The possible remainder value strategies. */
Expand Down
2 changes: 1 addition & 1 deletion bindings/python/examples/secret_manager/ledger_nano.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
load_dotenv()

# In this example we will get the ledger status and generate an address
# To use the ledger nano simulator clone https://github.com/iotaledger/ledger-shimmer-app, run `git submodule init && git submodule update --recursive`,
# To use the ledger nano simulator clone https://github.com/iotaledger/ledger-iota-app, run `git submodule init && git submodule update --recursive`,
# then `./build.sh -m nanos|nanox|nanosplus -s` and use `True` in
# `LedgerNanoSecretManager(True)`.

Expand Down
1 change: 1 addition & 0 deletions sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed

- Format of `milestoneIndex` query parameter of `ClientInner::event_status`;
- Don't error if custom remainder address is provided with ledger nano;

## 1.0.3 - 2023-09-07

Expand Down
2 changes: 1 addition & 1 deletion sdk/examples/client/ledger_nano.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//! In this example we will create testnet addresses with a simulated ledger nano hardware wallet.
//!
//! To use the ledger nano simulator, run the following commands:
//! 1. `clone https://github.com/iotaledger/ledger-shimmer-app`
//! 1. `clone https://github.com/iotaledger/ledger-iota-app`
//! 2. `cd ledger-shimmer-app`
//! 3. `git submodule init && git submodule update --recursive`
//! 4. `./build.sh -m nanos|nanox|nanosplus -s`
Expand Down
2 changes: 1 addition & 1 deletion sdk/examples/client/ledger_nano_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//! In this example we will create a testnet transaction with a simulated ledger nano hardware wallet.
//!
//! To use the ledger nano simulator, run the following commands:
//! 1. `clone https://github.com/iotaledger/ledger-shimmer-app`
//! 1. `clone https://github.com/iotaledger/ledger-iota-app`
//! 2. `cd ledger-shimmer-app`
//! 3. `git submodule init && git submodule update --recursive`
//! 4. `./build.sh -m nanos|nanox|nanosplus -s`
Expand Down
2 changes: 1 addition & 1 deletion sdk/examples/wallet/ledger_nano.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//! In this example we will create addresses with a ledger nano hardware wallet.
//!
//! To use the ledger nano simulator
//! * clone https://github.com/iotaledger/ledger-shimmer-app,
//! * clone https://github.com/iotaledger/ledger-iota-app,
//! * run `git submodule init && git submodule update --recursive`,
//! * run `./build.sh -m nanos|nanox|nanosplus -s`, and
//! * use `true` in `LedgerSecretManager::new(true)`.
Expand Down
33 changes: 24 additions & 9 deletions sdk/src/client/api/block_builder/input_selection/core/remainder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,26 @@ use crate::{

impl InputSelection {
// Gets the remainder address from configuration of finds one from the inputs.
fn get_remainder_address(&self) -> Option<(Address, Option<Bip44>)> {
if self.remainder_address.is_some() {
return self.remainder_address.map(|address| (address, None));
fn get_remainder_address(&self) -> Result<Option<(Address, Option<Bip44>)>, Error> {
if let Some(remainder_address) = self.remainder_address {
// Search in inputs for the Bip44 chain for the remainder address, so the ledger can regenerate it
for input in self.available_inputs.iter().chain(self.selected_inputs.iter()) {
let alias_transition = is_alias_transition(
&input.output,
*input.output_id(),
self.outputs.as_slice(),
self.burn.as_ref(),
);
let (required_address, _) =
input
.output
.required_and_unlocked_address(self.timestamp, input.output_id(), alias_transition)?;

if required_address == remainder_address {
return Ok(Some((remainder_address, input.chain)));
}
}
return Ok(Some((remainder_address, None)));
}

for input in &self.selected_inputs {
Expand All @@ -33,19 +50,17 @@ impl InputSelection {
self.outputs.as_slice(),
self.burn.as_ref(),
);
// PANIC: safe to unwrap as outputs with no address have been filtered out already.
let required_address = input
.output
.required_and_unlocked_address(self.timestamp, input.output_id(), alias_transition)
.unwrap()
.required_and_unlocked_address(self.timestamp, input.output_id(), alias_transition)?
.0;

if required_address.is_ed25519() {
return Some((required_address, input.chain));
return Ok(Some((required_address, input.chain)));
}
}

None
Ok(None)
}

pub(crate) fn remainder_amount(&self) -> Result<(u64, bool), Error> {
Expand Down Expand Up @@ -125,7 +140,7 @@ impl InputSelection {
return Ok((None, storage_deposit_returns));
}

let Some((remainder_address, chain)) = self.get_remainder_address() else {
let Some((remainder_address, chain)) = self.get_remainder_address()? else {
return Err(Error::MissingInputWithEd25519Address);
};

Expand Down
20 changes: 12 additions & 8 deletions sdk/src/client/secret/ledger_nano.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,17 +295,21 @@ impl SecretManage for LedgerSecretManager {
.map_err(Error::from)?;
} else {
// figure out the remainder address and bip32 index (if there is one)
#[allow(clippy::option_if_let_else)]
let (remainder_address, remainder_bip32): (Option<&Address>, LedgerBIP32Index) =
match &prepared_transaction.remainder {
Some(a) => {
let chain = a.chain.ok_or(Error::MissingBip32Chain)?;
(
Some(&a.address),
LedgerBIP32Index {
bip32_change: chain.change.harden().into(),
bip32_index: chain.address_index.harden().into(),
},
)
if let Some(chain) = a.chain {
(
Some(&a.address),
LedgerBIP32Index {
bip32_change: chain.change.harden().into(),
bip32_index: chain.address_index.harden().into(),
},
)
} else {
(None, LedgerBIP32Index::default())
}
}
None => (None, LedgerBIP32Index::default()),
};
Expand Down

0 comments on commit 5ac334d

Please sign in to comment.