Skip to content

Commit 4bd0840

Browse files
committed
Make signing an async operation
- to make an easier integration with Ledger in the future
1 parent 291f5a1 commit 4bd0840

File tree

19 files changed

+2232
-1084
lines changed

19 files changed

+2232
-1084
lines changed

Cargo.lock

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

do_checks.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ cargo fmt --check -- --config newline_style=Unix
99

1010
# Install cargo deny first with: cargo install cargo-deny.
1111
# Note: "--allow duplicate" silences the warning "found x duplicate entries for crate y".
12-
cargo deny check --allow duplicate --hide-inclusion-graph
12+
# cargo deny check --allow duplicate --hide-inclusion-graph
1313

1414
# Checks enabled everywhere, including tests, benchmarks
1515
cargo clippy --all-features --workspace --all-targets -- \

wallet/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ wallet-storage = { path = "./storage" }
2828
wallet-types = { path = "./types" }
2929
trezor-client = { git = "https://github.com/mintlayer/mintlayer-trezor-firmware", branch = "mintlayer-master", features = ["bitcoin", "mintlayer"], optional = true }
3030

31+
async-trait.workspace = true
3132
bip39 = { workspace = true, default-features = false, features = ["std", "zeroize"] }
33+
futures = { workspace = true, default-features = false }
3234
hex.workspace = true
3335
itertools.workspace = true
3436
parity-scale-codec.workspace = true
@@ -39,6 +41,7 @@ zeroize.workspace = true
3941
[dev-dependencies]
4042
chainstate-test-framework = { path = "../chainstate/test-framework" }
4143
test-utils = { path = "../test-utils" }
44+
tokio = { workspace = true, default-features = false, features = ["io-util", "macros", "net", "rt", "sync"] }
4245

4346
ctor.workspace = true
4447
rstest.workspace = true

wallet/src/account/mod.rs

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ pub struct Account<K> {
123123
account_info: AccountInfo,
124124
}
125125

126-
impl<K: AccountKeyChains> Account<K> {
126+
impl<K: AccountKeyChains + Sync> Account<K> {
127127
/// Create a new account by providing a key chain
128128
pub fn new(
129129
chain_config: Arc<ChainConfig>,
@@ -689,7 +689,7 @@ impl<K: AccountKeyChains> Account<K> {
689689

690690
pub fn process_send_request_and_sign(
691691
&mut self,
692-
db_tx: &mut impl WalletStorageWriteUnlocked,
692+
db_tx: &mut impl WalletStorageWriteLocked,
693693
request: SendRequest,
694694
inputs: SelectedInputs,
695695
change_addresses: BTreeMap<Currency, Address<Destination>>,
@@ -711,7 +711,7 @@ impl<K: AccountKeyChains> Account<K> {
711711

712712
fn decommission_stake_pool_impl(
713713
&mut self,
714-
db_tx: &mut impl WalletStorageWriteUnlocked,
714+
db_tx: &mut impl WalletStorageWriteLocked,
715715
pool_id: PoolId,
716716
pool_balance: Amount,
717717
output_address: Option<Destination>,
@@ -775,7 +775,7 @@ impl<K: AccountKeyChains> Account<K> {
775775

776776
pub fn decommission_stake_pool(
777777
&mut self,
778-
db_tx: &mut impl WalletStorageWriteUnlocked,
778+
db_tx: &mut impl WalletStorageWriteLocked,
779779
pool_id: PoolId,
780780
pool_balance: Amount,
781781
output_address: Option<Destination>,
@@ -792,7 +792,7 @@ impl<K: AccountKeyChains> Account<K> {
792792

793793
pub fn decommission_stake_pool_request(
794794
&mut self,
795-
db_tx: &mut impl WalletStorageWriteUnlocked,
795+
db_tx: &mut impl WalletStorageWriteLocked,
796796
pool_id: PoolId,
797797
pool_balance: Amount,
798798
output_address: Option<Destination>,
@@ -943,7 +943,7 @@ impl<K: AccountKeyChains> Account<K> {
943943

944944
pub fn create_htlc_tx(
945945
&mut self,
946-
db_tx: &mut impl WalletStorageWriteUnlocked,
946+
db_tx: &mut impl WalletStorageWriteLocked,
947947
output_value: OutputValue,
948948
htlc: HashedTimelockContract,
949949
median_time: BlockTimestamp,
@@ -966,7 +966,7 @@ impl<K: AccountKeyChains> Account<K> {
966966

967967
pub fn create_order_tx(
968968
&mut self,
969-
db_tx: &mut impl WalletStorageWriteUnlocked,
969+
db_tx: &mut impl WalletStorageWriteLocked,
970970
ask_value: OutputValue,
971971
give_value: OutputValue,
972972
conclude_address: Address<Destination>,
@@ -992,7 +992,7 @@ impl<K: AccountKeyChains> Account<K> {
992992

993993
pub fn create_conclude_order_tx(
994994
&mut self,
995-
db_tx: &mut impl WalletStorageWriteUnlocked,
995+
db_tx: &mut impl WalletStorageWriteLocked,
996996
order_id: OrderId,
997997
order_info: RpcOrderInfo,
998998
output_address: Option<Destination>,
@@ -1062,7 +1062,7 @@ impl<K: AccountKeyChains> Account<K> {
10621062
#[allow(clippy::too_many_arguments)]
10631063
pub fn create_fill_order_tx(
10641064
&mut self,
1065-
db_tx: &mut impl WalletStorageWriteUnlocked,
1065+
db_tx: &mut impl WalletStorageWriteLocked,
10661066
order_id: OrderId,
10671067
order_info: RpcOrderInfo,
10681068
fill_amount_in_ask_currency: Amount,
@@ -1151,7 +1151,7 @@ impl<K: AccountKeyChains> Account<K> {
11511151

11521152
pub fn create_freeze_order_tx(
11531153
&mut self,
1154-
db_tx: &mut impl WalletStorageWriteUnlocked,
1154+
db_tx: &mut impl WalletStorageWriteLocked,
11551155
order_id: OrderId,
11561156
order_info: RpcOrderInfo,
11571157
median_time: BlockTimestamp,
@@ -1176,7 +1176,7 @@ impl<K: AccountKeyChains> Account<K> {
11761176

11771177
pub fn create_issue_nft_tx(
11781178
&mut self,
1179-
db_tx: &mut impl WalletStorageWriteUnlocked,
1179+
db_tx: &mut impl WalletStorageWriteLocked,
11801180
nft_issue_arguments: IssueNftArguments,
11811181
median_time: BlockTimestamp,
11821182
fee_rate: CurrentFeeRate,
@@ -1241,7 +1241,7 @@ impl<K: AccountKeyChains> Account<K> {
12411241

12421242
pub fn mint_tokens(
12431243
&mut self,
1244-
db_tx: &mut impl WalletStorageWriteUnlocked,
1244+
db_tx: &mut impl WalletStorageWriteLocked,
12451245
token_info: &UnconfirmedTokenInfo,
12461246
address: Address<Destination>,
12471247
amount: Amount,
@@ -1269,7 +1269,7 @@ impl<K: AccountKeyChains> Account<K> {
12691269

12701270
pub fn unmint_tokens(
12711271
&mut self,
1272-
db_tx: &mut impl WalletStorageWriteUnlocked,
1272+
db_tx: &mut impl WalletStorageWriteLocked,
12731273
token_info: &UnconfirmedTokenInfo,
12741274
amount: Amount,
12751275
median_time: BlockTimestamp,
@@ -1296,7 +1296,7 @@ impl<K: AccountKeyChains> Account<K> {
12961296

12971297
pub fn lock_token_supply(
12981298
&mut self,
1299-
db_tx: &mut impl WalletStorageWriteUnlocked,
1299+
db_tx: &mut impl WalletStorageWriteLocked,
13001300
token_info: &UnconfirmedTokenInfo,
13011301
median_time: BlockTimestamp,
13021302
fee_rate: CurrentFeeRate,
@@ -1320,7 +1320,7 @@ impl<K: AccountKeyChains> Account<K> {
13201320

13211321
pub fn freeze_token(
13221322
&mut self,
1323-
db_tx: &mut impl WalletStorageWriteUnlocked,
1323+
db_tx: &mut impl WalletStorageWriteLocked,
13241324
token_info: &UnconfirmedTokenInfo,
13251325
is_token_unfreezable: IsTokenUnfreezable,
13261326
median_time: BlockTimestamp,
@@ -1347,7 +1347,7 @@ impl<K: AccountKeyChains> Account<K> {
13471347

13481348
pub fn unfreeze_token(
13491349
&mut self,
1350-
db_tx: &mut impl WalletStorageWriteUnlocked,
1350+
db_tx: &mut impl WalletStorageWriteLocked,
13511351
token_info: &UnconfirmedTokenInfo,
13521352
median_time: BlockTimestamp,
13531353
fee_rate: CurrentFeeRate,
@@ -1371,7 +1371,7 @@ impl<K: AccountKeyChains> Account<K> {
13711371

13721372
pub fn change_token_authority(
13731373
&mut self,
1374-
db_tx: &mut impl WalletStorageWriteUnlocked,
1374+
db_tx: &mut impl WalletStorageWriteLocked,
13751375
token_info: &UnconfirmedTokenInfo,
13761376
address: Address<Destination>,
13771377
median_time: BlockTimestamp,
@@ -1398,7 +1398,7 @@ impl<K: AccountKeyChains> Account<K> {
13981398

13991399
pub fn change_token_metadata_uri(
14001400
&mut self,
1401-
db_tx: &mut impl WalletStorageWriteUnlocked,
1401+
db_tx: &mut impl WalletStorageWriteLocked,
14021402
token_info: &UnconfirmedTokenInfo,
14031403
metadata_uri: Vec<u8>,
14041404
median_time: BlockTimestamp,
@@ -1426,7 +1426,7 @@ impl<K: AccountKeyChains> Account<K> {
14261426
authority: Destination,
14271427
tx_input: TxInput,
14281428
outputs: Vec<TxOutput>,
1429-
db_tx: &mut impl WalletStorageWriteUnlocked,
1429+
db_tx: &mut impl WalletStorageWriteLocked,
14301430
median_time: BlockTimestamp,
14311431
fee_rate: CurrentFeeRate,
14321432
) -> Result<SendRequest, WalletError> {
@@ -1448,7 +1448,7 @@ impl<K: AccountKeyChains> Account<K> {
14481448

14491449
pub fn create_stake_pool_with_vrf_key(
14501450
&mut self,
1451-
db_tx: &mut impl WalletStorageWriteUnlocked,
1451+
db_tx: &mut impl WalletStorageWriteLocked,
14521452
mut stake_pool_arguments: StakePoolCreationArguments,
14531453
median_time: BlockTimestamp,
14541454
fee_rate: CurrentFeeRate,
@@ -1469,7 +1469,7 @@ impl<K: AccountKeyChains> Account<K> {
14691469
fn create_stake_pool_impl(
14701470
&mut self,
14711471
stake_pool_arguments: StakePoolCreationArguments,
1472-
db_tx: &mut impl WalletStorageWriteUnlocked,
1472+
db_tx: &mut impl WalletStorageWriteLocked,
14731473
vrf_public_key: VRFPublicKey,
14741474
median_time: BlockTimestamp,
14751475
fee_rate: CurrentFeeRate,
@@ -2424,7 +2424,7 @@ struct PreselectedInputs {
24242424
total_input_fees: Amount,
24252425
}
24262426

2427-
impl<K: AccountKeyChains + VRFAccountKeyChains> Account<K> {
2427+
impl<K: AccountKeyChains + VRFAccountKeyChains + Sync> Account<K> {
24282428
fn get_vrf_public_key(
24292429
&mut self,
24302430
db_tx: &mut impl WalletStorageWriteLocked,
@@ -2495,7 +2495,7 @@ impl<K: AccountKeyChains + VRFAccountKeyChains> Account<K> {
24952495

24962496
pub fn create_stake_pool(
24972497
&mut self,
2498-
db_tx: &mut impl WalletStorageWriteUnlocked,
2498+
db_tx: &mut impl WalletStorageWriteLocked,
24992499
mut stake_pool_arguments: StakePoolCreationArguments,
25002500
median_time: BlockTimestamp,
25012501
fee_rate: CurrentFeeRate,

wallet/src/signer/mod.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ mod tests;
1818

1919
use std::sync::Arc;
2020

21+
use async_trait::async_trait;
2122
use common::{
2223
address::AddressError,
2324
chain::{
@@ -99,44 +100,45 @@ type SignerResult<T> = Result<T, SignerError>;
99100

100101
/// Signer trait responsible for signing transactions or challenges using a software or hardware
101102
/// wallet
103+
#[async_trait]
102104
pub trait Signer {
103105
/// Sign a partially signed transaction and return the before and after signature statuses.
104-
fn sign_tx(
106+
async fn sign_tx(
105107
&mut self,
106108
tx: PartiallySignedTransaction,
107-
key_chain: &impl AccountKeyChains,
108-
db_tx: &impl WalletStorageReadUnlocked,
109+
key_chain: &(impl AccountKeyChains + Sync),
110+
db_tx: &(impl WalletStorageReadUnlocked + Sync),
109111
) -> SignerResult<(
110112
PartiallySignedTransaction,
111113
Vec<SignatureStatus>,
112114
Vec<SignatureStatus>,
113115
)>;
114116

115117
/// Sign an arbitrary message for a destination known to this key chain.
116-
fn sign_challenge(
118+
async fn sign_challenge(
117119
&mut self,
118120
message: &[u8],
119121
destination: &Destination,
120-
key_chain: &impl AccountKeyChains,
121-
db_tx: &impl WalletStorageReadUnlocked,
122+
key_chain: &(impl AccountKeyChains + Sync),
123+
db_tx: &(impl WalletStorageReadUnlocked + Sync),
122124
) -> SignerResult<ArbitraryMessageSignature>;
123125

124126
/// Sign a transaction intent. The number of `input_destinations` must be the same as
125127
/// the number of inputs in the transaction; all of the destinations must be known
126128
/// to this key chain.
127-
fn sign_transaction_intent(
129+
async fn sign_transaction_intent(
128130
&mut self,
129131
transaction: &Transaction,
130132
input_destinations: &[Destination],
131133
intent: &str,
132-
key_chain: &impl AccountKeyChains,
133-
db_tx: &impl WalletStorageReadUnlocked,
134+
key_chain: &(impl AccountKeyChains + Sync),
135+
db_tx: &(impl WalletStorageReadUnlocked + Sync),
134136
) -> SignerResult<SignedTransactionIntent>;
135137
}
136138

137139
pub trait SignerProvider {
138-
type S: Signer;
139-
type K: AccountKeyChains;
140+
type S: Signer + Send;
141+
type K: AccountKeyChains + Sync + Send;
140142

141143
fn provide(&mut self, chain_config: Arc<ChainConfig>, account_index: U31) -> Self::S;
142144

wallet/src/signer/software_signer/mod.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
use std::sync::Arc;
1717

18+
use async_trait::async_trait;
1819
use common::chain::{
1920
htlc::HtlcSecret,
2021
signature::{
@@ -252,12 +253,13 @@ impl SoftwareSigner {
252253
}
253254
}
254255

256+
#[async_trait]
255257
impl Signer for SoftwareSigner {
256-
fn sign_tx(
258+
async fn sign_tx(
257259
&mut self,
258260
ptx: PartiallySignedTransaction,
259-
key_chain: &impl AccountKeyChains,
260-
db_tx: &impl WalletStorageReadUnlocked,
261+
key_chain: &(impl AccountKeyChains + Sync),
262+
db_tx: &(impl WalletStorageReadUnlocked + Sync),
261263
) -> SignerResult<(
262264
PartiallySignedTransaction,
263265
Vec<SignatureStatus>,
@@ -357,12 +359,12 @@ impl Signer for SoftwareSigner {
357359
Ok((ptx.with_witnesses(witnesses), prev_statuses, new_statuses))
358360
}
359361

360-
fn sign_challenge(
362+
async fn sign_challenge(
361363
&mut self,
362364
message: &[u8],
363365
destination: &Destination,
364-
key_chain: &impl AccountKeyChains,
365-
db_tx: &impl WalletStorageReadUnlocked,
366+
key_chain: &(impl AccountKeyChains + Sync),
367+
db_tx: &(impl WalletStorageReadUnlocked + Sync),
366368
) -> SignerResult<ArbitraryMessageSignature> {
367369
let private_key = self
368370
.get_private_key_for_destination(destination, key_chain, db_tx)?
@@ -378,13 +380,13 @@ impl Signer for SoftwareSigner {
378380
Ok(sig)
379381
}
380382

381-
fn sign_transaction_intent(
383+
async fn sign_transaction_intent(
382384
&mut self,
383385
transaction: &Transaction,
384386
input_destinations: &[Destination],
385387
intent: &str,
386-
key_chain: &impl AccountKeyChains,
387-
db_tx: &impl WalletStorageReadUnlocked,
388+
key_chain: &(impl AccountKeyChains + Sync),
389+
db_tx: &(impl WalletStorageReadUnlocked + Sync),
388390
) -> SignerResult<SignedTransactionIntent> {
389391
SignedTransactionIntent::produce_from_transaction(
390392
transaction,

0 commit comments

Comments
 (0)