Skip to content

Commit 6566fc1

Browse files
authored
Add mwixnet code to master (#726)
* add all comsig code, update to master branch * refactor code into libwallet, remove some redundancy and update type exports * added api calls and unit test for comsig request creation * Addition of JSON-RPC mwixnet function call * attempt to debug invalid params * additions to support json doctesting for comsig creation * tweaks to testing and documentation updates * dependencies for tests
1 parent b7104cd commit 6566fc1

File tree

21 files changed

+2057
-47
lines changed

21 files changed

+2057
-47
lines changed

Cargo.lock

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

api/src/owner.rs

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
1717
use chrono::prelude::*;
1818
use ed25519_dalek::SecretKey as DalekSecretKey;
19+
use grin_wallet_libwallet::mwixnet::{MixnetReqCreationParams, SwapReq};
1920
use grin_wallet_libwallet::RetrieveTxQueryArgs;
2021
use uuid::Uuid;
2122

@@ -33,7 +34,7 @@ use crate::libwallet::{
3334
TxLogEntry, ViewWallet, WalletInfo, WalletInst, WalletLCProvider,
3435
};
3536
use crate::util::logger::LoggingConfig;
36-
use crate::util::secp::key::SecretKey;
37+
use crate::util::secp::{key::SecretKey, pedersen::Commitment};
3738
use crate::util::{from_hex, static_secp_instance, Mutex, ZeroingString};
3839
use grin_wallet_util::OnionV3Address;
3940
use std::convert::TryFrom;
@@ -2423,6 +2424,71 @@ where
24232424
let w = w_lock.lc_provider()?.wallet_inst()?;
24242425
owner::build_output(&mut **w, keychain_mask, features, amount)
24252426
}
2427+
2428+
// MWIXNET
2429+
2430+
/// Creates an mwixnet request [SwapReq](../grin_wallet_libwallet/api_impl/types/struct.SwapReq.html)
2431+
/// from a given output commitment under this wallet's control.
2432+
///
2433+
/// # Arguments
2434+
/// * `keychain_mask` - Wallet secret mask to XOR against the stored wallet seed before using, if
2435+
/// being used.
2436+
/// * `params` - A [MixnetReqCreationParams](../grin_wallet_libwallet/api_impl/types/struct.MixnetReqCreationParams.html)
2437+
/// struct containing the parameters for the request, which include:
2438+
/// `server_keys` - The public keys of the servers participating in the mixnet (each encoded internally as a `SecretKey`)
2439+
/// `fee_per_hop` - The fee to be paid to each server for each hop in the mixnet
2440+
/// * `commitment` - The commitment of the output to be mixed
2441+
/// * `lock_output` - Whether to lock the referenced output after creating the request
2442+
///
2443+
/// # Returns
2444+
/// * Ok([SwapReq](../grin_wallet_libwallet/api_impl/types/struct.SwapReq.html)) if successful
2445+
/// * or [`libwallet::Error`](../grin_wallet_libwallet/struct.Error.html) if an error is encountered
2446+
///
2447+
/// # Example
2448+
/// Set up as in [`new`](struct.Owner.html#method.new) method above.
2449+
/// ```
2450+
/// # grin_wallet_api::doctest_helper_setup_doc_env!(wallet, wallet_config);
2451+
///
2452+
/// let api_owner = Owner::new(wallet.clone(), None);
2453+
/// let keychain_mask = None;
2454+
/// let params = MixnetReqCreationParams {
2455+
/// server_keys: vec![], // Public keys here in secret key representation
2456+
/// fee_per_hop: 100,
2457+
/// };
2458+
///
2459+
/// let commitment = Commitment::from_vec(vec![0; 32]);
2460+
/// let lock_output = true;
2461+
///
2462+
/// let result = api_owner.create_mwixnet_req(
2463+
/// keychain_mask,
2464+
/// &params,
2465+
/// &commitment,
2466+
/// lock_output,
2467+
/// );
2468+
///
2469+
/// if let Ok(req) = result {
2470+
/// //...
2471+
/// }
2472+
/// ```
2473+
2474+
pub fn create_mwixnet_req(
2475+
&self,
2476+
keychain_mask: Option<&SecretKey>,
2477+
params: &MixnetReqCreationParams,
2478+
commitment: &Commitment,
2479+
lock_output: bool, // use_test_rng: bool,
2480+
) -> Result<SwapReq, Error> {
2481+
let mut w_lock = self.wallet_inst.lock();
2482+
let w = w_lock.lc_provider()?.wallet_inst()?;
2483+
owner::create_mwixnet_req(
2484+
&mut **w,
2485+
keychain_mask,
2486+
params,
2487+
commitment,
2488+
lock_output,
2489+
self.doctest_mode,
2490+
)
2491+
}
24262492
}
24272493

24282494
/// attempt to send slate synchronously with TOR
@@ -2524,13 +2590,17 @@ macro_rules! doctest_helper_setup_doc_env {
25242590
use keychain::ExtKeychain;
25252591
use tempfile::tempdir;
25262592

2593+
use grin_util::secp::pedersen::Commitment;
25272594
use std::sync::Arc;
25282595
use util::{Mutex, ZeroingString};
25292596

25302597
use api::{Foreign, Owner};
25312598
use config::WalletConfig;
25322599
use impls::{DefaultLCProvider, DefaultWalletImpl, HTTPNodeClient};
2533-
use libwallet::{BlockFees, InitTxArgs, IssueInvoiceTxArgs, Slate, WalletInst};
2600+
use libwallet::{
2601+
mwixnet::MixnetReqCreationParams, BlockFees, InitTxArgs, IssueInvoiceTxArgs, Slate,
2602+
WalletInst,
2603+
};
25342604

25352605
use uuid::Uuid;
25362606

api/src/owner_rpc.rs

Lines changed: 102 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,23 @@
1414

1515
//! JSON-RPC Stub generation for the Owner API
1616
use grin_wallet_libwallet::RetrieveTxQueryArgs;
17+
use libwallet::mwixnet::SwapReq;
1718
use uuid::Uuid;
1819

1920
use crate::config::{TorConfig, WalletConfig};
2021
use crate::core::core::OutputFeatures;
2122
use crate::core::global;
2223
use crate::keychain::{Identifier, Keychain};
2324
use crate::libwallet::{
24-
AcctPathMapping, Amount, BuiltOutput, Error, InitTxArgs, IssueInvoiceTxArgs, NodeClient,
25-
NodeHeightResult, OutputCommitMapping, PaymentProof, Slate, SlateVersion, Slatepack,
26-
SlatepackAddress, StatusMessage, TxLogEntry, VersionedSlate, ViewWallet, WalletInfo,
27-
WalletLCProvider,
25+
mwixnet::MixnetReqCreationParams, AcctPathMapping, Amount, BuiltOutput, Error, InitTxArgs,
26+
IssueInvoiceTxArgs, NodeClient, NodeHeightResult, OutputCommitMapping, PaymentProof, Slate,
27+
SlateVersion, Slatepack, SlatepackAddress, StatusMessage, TxLogEntry, VersionedSlate,
28+
ViewWallet, WalletInfo, WalletLCProvider,
2829
};
2930
use crate::util::logger::LoggingConfig;
3031
use crate::util::secp::key::{PublicKey, SecretKey};
31-
use crate::util::{static_secp_instance, Mutex, ZeroingString};
32+
use crate::util::secp::pedersen::Commitment;
33+
use crate::util::{from_hex, static_secp_instance, Mutex, ZeroingString};
3234
use crate::{ECDHPubkey, Ed25519SecretKey, Owner, Token};
3335
use easy_jsonrpc_mw;
3436
use grin_wallet_util::OnionV3Address;
@@ -1963,6 +1965,63 @@ pub trait OwnerRpc {
19631965
features: OutputFeatures,
19641966
amount: Amount,
19651967
) -> Result<BuiltOutput, Error>;
1968+
1969+
/**
1970+
Networked version of [Owner::build_output](struct.Owner.html#method.create_mwixnet_req).
1971+
```
1972+
# grin_wallet_api::doctest_helper_json_rpc_owner_assert_response!(
1973+
# r#"
1974+
{
1975+
"jsonrpc": "2.0",
1976+
"method": "create_mwixnet_req",
1977+
"params": {
1978+
"token": "d202964900000000d302964900000000d402964900000000d502964900000000",
1979+
"commitment": "08e1da9e6dc4d6e808a718b2f110a991dd775d65ce5ae408a4e1f002a4961aa9e7",
1980+
"fee_per_hop": "5000000",
1981+
"lock_output": true,
1982+
"server_keys": [
1983+
"97444ae673bb92c713c1a2f7b8882ffbfc1c67401a280a775dce1a8651584332",
1984+
"0c9414341f2140ed34a5a12a6479bf5a6404820d001ab81d9d3e8cc38f049b4e",
1985+
"b58ece97d60e71bb7e53218400b0d67bfe6a3cb7d3b4a67a44f8fb7c525cbca5"
1986+
]
1987+
},
1988+
"id": 1
1989+
}
1990+
# "#
1991+
# ,
1992+
# r#"
1993+
{
1994+
"id": 1,
1995+
"jsonrpc": "2.0",
1996+
"result": {
1997+
"Ok": {
1998+
"comsig": "099561ed0be59f6502ee358ee4f6760cd16d6be04d58d7a2c1bf2fd09dd7fd2d291beaae5483c6f18d1ceaae6321f06f9ba129a1ee9e7d15f152c67397a621538b5c10bbeb95140dee815c02657c91152939afe389458dc59af095e8e8e5c81a08",
1999+
"onion": {
2000+
"commit": "08e1da9e6dc4d6e808a718b2f110a991dd775d65ce5ae408a4e1f002a4961aa9e7",
2001+
"data": [
2002+
"37f68116475e1aa6b58fc911addbd0e04e7aa19ab3e82e7b5cfcaf57d82cf35e7388ce51711cc5ef8cf7630f7dc7229878f91c7ec85991a7fc0051a7bbc66569db3a3aa89ef490055f3c",
2003+
"b9ff8c0c1699808efce46d581647c65764a28e813023ae677d688282422a07505ae1a051037d7ba58f3279846d0300800fc1c5bfcc548dab815e9fd2f29df9515170c41fa6e4e44b8bcb",
2004+
"62ea6b8369686a0415e1e752b9b4d6e66cf5b6066a2d3c60d8818890a55f3adff4601466f4c6e6b646568b99ae93549a3595b7a7b4be815ced87d9297cabbd69518d7b2ed6edd14007528fd346aaea765a1165fe886666627ebcab9588b8ee1c9e98395ae67913c48eb6e924581b40182fce807f97312fb07fd5e216d99941f2b488babce4078a50cd66b28b30a66c4f54fcc127437408a99b30ffd6c3d0d8c7d39e864fc04e321b8c10138c8852d4cad0a4f2780412b9dadcc6e0f2657b7803a81bccb809ca392464be2e01755be7377d0e815698ad6ea51d4617cc92c3ccf852f038e33cc9c90992438ba5c49cca7cc188b682da684e2f4c9733a84a7b64ac5c2216ebf5926f0ee67b664fb5bab799109cbee755ce1aebc8cd352fea51cd84c333cb958093c53544c3f3ab05dba64d8f041c3b179796b476ec04b11044e39db6994ab767315e52cc0ef023432ec88ade2911612db7e74e0923889f765b58b00e3869c5072a4e882c1b721913f63bda986b8c97b7ae575f0d4be596a1ac3cd0db96ce6074ee000b32018b3bda16d7dba34a13ba9c3ce983946414c16e278351a3411cb8ef2cb8ef5b6e1667c4c58bc797c0324ae4fec8960d684e561c0e833ee4c3331c6c439b59042a62993535e23cc8a8a4cf705c0f9b1d62db4e3d76c22c01138800414b143ddff471e4df4413e842a1b41f43cc9647e47145fd6c86d4d1a34fb2f62f5a55b31c9353ee34743c548eff955f2d2143c1a86cbcb452104f96d0142db31153021bbeed995c71a92de8fb1f97269533a508085c543fcb3ee57000bb265e74187b858403aa97b6c7b085e5d5b6025cbfe5f6926d33c835f90e60fc62013e80bbe0a855da5938b4b8f83ac29c5e8251827795356222079a6d1612e2fdf93bd7836d1613c7a353ada48ce256f880bbbb3108e037e3b5647101bd4d549101b0ee73d2248a932a802a3b1beb0b69d777c4285d57e91d83e96fe2f8a1a2f182fe2c6ca37b18460cf8d7f56c201147b9be19f1d01f8ad305c1e9c4dd79b5d8719d6550432352cf737082b1e9de7a083ffbe1"
2005+
],
2006+
"pubkey": "e7ee7d51b11d09f268ade98bc9d7ae9be3c4ac124ce1c3a40e50d34460fa5f08"
2007+
}
2008+
}
2009+
}
2010+
}
2011+
# "#
2012+
# , 5, true, true, false, false);
2013+
```
2014+
*
2015+
*/
2016+
2017+
fn create_mwixnet_req(
2018+
&self,
2019+
token: Token,
2020+
commitment: String,
2021+
fee_per_hop: String,
2022+
lock_output: bool,
2023+
server_keys: Vec<String>,
2024+
) -> Result<SwapReq, Error>;
19662025
}
19672026

19682027
impl<L, C, K> OwnerRpc for Owner<L, C, K>
@@ -2372,6 +2431,44 @@ where
23722431
) -> Result<BuiltOutput, Error> {
23732432
Owner::build_output(self, (&token.keychain_mask).as_ref(), features, amount.0)
23742433
}
2434+
2435+
fn create_mwixnet_req(
2436+
&self,
2437+
token: Token,
2438+
commitment: String,
2439+
fee_per_hop: String,
2440+
lock_output: bool,
2441+
server_keys: Vec<String>,
2442+
) -> Result<SwapReq, Error> {
2443+
let commit =
2444+
Commitment::from_vec(from_hex(&commitment).map_err(|e| Error::CommitDeser(e))?);
2445+
2446+
let secp_inst = static_secp_instance();
2447+
let secp = secp_inst.lock();
2448+
2449+
let mut keys = vec![];
2450+
for key in server_keys {
2451+
keys.push(SecretKey::from_slice(
2452+
&secp,
2453+
&grin_util::from_hex(&key).map_err(|e| Error::ServerKeyDeser(e))?,
2454+
)?)
2455+
}
2456+
2457+
let req_params = MixnetReqCreationParams {
2458+
server_keys: keys,
2459+
fee_per_hop: fee_per_hop
2460+
.parse::<u64>()
2461+
.map_err(|_| Error::U64Deser(fee_per_hop))?,
2462+
};
2463+
2464+
Owner::create_mwixnet_req(
2465+
self,
2466+
(&token.keychain_mask).as_ref(),
2467+
&req_params,
2468+
&commit,
2469+
lock_output,
2470+
)
2471+
}
23752472
}
23762473

23772474
/// helper to set up a real environment to run integrated doctests

controller/tests/invoice.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -82,16 +82,16 @@ fn invoice_tx_impl(test_dir: &'static str) -> Result<(), libwallet::Error> {
8282
wallet_inst!(wallet1, w);
8383
w.set_parent_key_id_by_name("mining")?;
8484
}
85-
let mut bh = 10u64;
85+
let mut _bh = 10u64;
8686
let _ =
87-
test_framework::award_blocks_to_wallet(&chain, wallet1.clone(), mask1, bh as usize, false);
87+
test_framework::award_blocks_to_wallet(&chain, wallet1.clone(), mask1, _bh as usize, false);
8888

8989
// Sanity check wallet 1 contents
9090
wallet::controller::owner_single_use(Some(wallet1.clone()), mask1, None, |api, m| {
9191
let (wallet1_refreshed, wallet1_info) = api.retrieve_summary_info(m, true, 1)?;
9292
assert!(wallet1_refreshed);
93-
assert_eq!(wallet1_info.last_confirmed_height, bh);
94-
assert_eq!(wallet1_info.total, bh * reward);
93+
assert_eq!(wallet1_info.last_confirmed_height, _bh);
94+
assert_eq!(wallet1_info.total, _bh * reward);
9595
Ok(())
9696
})?;
9797

@@ -138,10 +138,10 @@ fn invoice_tx_impl(test_dir: &'static str) -> Result<(), libwallet::Error> {
138138
api.post_tx(m, &slate, false)?;
139139
Ok(())
140140
})?;
141-
bh += 1;
141+
_bh += 1;
142142

143143
let _ = test_framework::award_blocks_to_wallet(&chain, wallet1.clone(), mask1, 3, false);
144-
bh += 3;
144+
_bh += 3;
145145

146146
// Check transaction log for wallet 2
147147
wallet::controller::owner_single_use(Some(wallet2.clone()), mask2, None, |api, m| {
@@ -151,7 +151,7 @@ fn invoice_tx_impl(test_dir: &'static str) -> Result<(), libwallet::Error> {
151151
assert!(txs.len() == 1);
152152
println!(
153153
"last confirmed height: {}, bh: {}",
154-
wallet2_info.last_confirmed_height, bh
154+
wallet2_info.last_confirmed_height, _bh
155155
);
156156
assert!(refreshed);
157157
Ok(())
@@ -163,10 +163,10 @@ fn invoice_tx_impl(test_dir: &'static str) -> Result<(), libwallet::Error> {
163163
let (_, wallet1_info) = api.retrieve_summary_info(m, true, 1)?;
164164
let (refreshed, txs) = api.retrieve_txs(m, true, None, None, None)?;
165165
assert!(refreshed);
166-
assert_eq!(txs.len() as u64, bh + 1);
166+
assert_eq!(txs.len() as u64, _bh + 1);
167167
println!(
168168
"Wallet 1: last confirmed height: {}, bh: {}",
169-
wallet1_info.last_confirmed_height, bh
169+
wallet1_info.last_confirmed_height, _bh
170170
);
171171
Ok(())
172172
})?;
@@ -248,7 +248,7 @@ fn invoice_tx_impl(test_dir: &'static str) -> Result<(), libwallet::Error> {
248248

249249
// test that payee can only cancel once
250250
let _ = test_framework::award_blocks_to_wallet(&chain, wallet1.clone(), mask1, 3, false);
251-
bh += 3;
251+
_bh += 3;
252252

253253
wallet::controller::owner_single_use(Some(wallet2.clone()), mask2, None, |api, m| {
254254
// Wallet 2 inititates an invoice transaction, requesting payment

0 commit comments

Comments
 (0)