Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Receive / Apply Transaction #50

Merged
merged 7 commits into from
Jan 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions crates/core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ pub enum GrinWalletInterfaceError {
WalletLibWallet(#[from] grin_wallet_libwallet::Error),
#[error("Owner API not Instantiated")]
OwnerAPINotInstantiated,
#[error("Foreign API not Instantiated")]
ForeignAPINotInstantiated,
}

#[derive(thiserror::Error, Debug)]
Expand Down
66 changes: 60 additions & 6 deletions crates/core/src/wallet/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/// Placeholder for all wallet calls
/// Should eventually feature async calls that work via local wallet or remote owner API
use grin_wallet::cmd::wallet_args::inst_wallet;
use grin_wallet_api::Owner;
use grin_wallet_api::{Owner, Foreign};
use grin_wallet_config::{self, GlobalWalletConfig};
use grin_wallet_controller::command::InitArgs;
use grin_wallet_impls::DefaultLCProvider;
Expand All @@ -21,8 +21,8 @@ use dirs;
pub use global::ChainTypes;
pub use grin_wallet_impls::HTTPNodeClient;
pub use grin_wallet_libwallet::{
InitTxArgs, RetrieveTxQueryArgs, RetrieveTxQuerySortOrder, Slate, SlatepackAddress,
StatusMessage, TxLogEntry, TxLogEntryType, WalletInfo,
InitTxArgs, RetrieveTxQueryArgs, RetrieveTxQuerySortOrder, Slate, SlatepackAddress, Slatepack,
StatusMessage, SlateState, TxLogEntry, TxLogEntryType, WalletInfo,
};

use crate::error::GrinWalletInterfaceError;
Expand Down Expand Up @@ -92,6 +92,8 @@ where
pub config: Option<GlobalWalletConfig>,
// owner api will hold instantiated/opened wallets
pub owner_api: Option<Owner<L, C, keychain::ExtKeychain>>,
// Also need reference to foreign API
pub foreign_api: Option<Foreign<'static, L, C, keychain::ExtKeychain>>,
// Simple flag to check whether wallet has been opened
wallet_is_open: bool,
// Hold on to check node foreign API secret for now
Expand All @@ -112,6 +114,7 @@ where
chain_type: None,
config: None,
owner_api: None,
foreign_api: None,
wallet_is_open: false,
check_node_foreign_api_secret_path: None,
node_client,
Expand Down Expand Up @@ -208,7 +211,7 @@ where
Ok(wallet_inst)
}

fn inst_owner_api(
fn inst_apis(
wallet_interface: Arc<RwLock<WalletInterface<L, C>>>,
chain_type: global::ChainTypes,
top_level_directory: PathBuf,
Expand All @@ -220,6 +223,7 @@ where
)?;
let mut w = wallet_interface.write().unwrap();
w.owner_api = Some(Owner::new(wallet_inst.clone(), None));
w.foreign_api = Some(Foreign::new(wallet_inst.clone(), None, None, false));
global::set_local_chain_type(chain_type);

Ok(())
Expand All @@ -233,7 +237,7 @@ where
chain_type: global::ChainTypes,
recovery_phrase: Option<String>,
) -> Result<(String, String, String, global::ChainTypes), GrinWalletInterfaceError> {
WalletInterface::inst_owner_api(
WalletInterface::inst_apis(
wallet_interface.clone(),
chain_type,
top_level_directory.clone(),
Expand Down Expand Up @@ -305,7 +309,7 @@ where
top_level_directory: PathBuf,
chain_type: global::ChainTypes,
) -> Result<(), GrinWalletInterfaceError> {
WalletInterface::inst_owner_api(
WalletInterface::inst_apis(
wallet_interface.clone(),
chain_type,
top_level_directory.clone(),
Expand Down Expand Up @@ -369,6 +373,21 @@ where
Ok(api.create_slatepack_message(None, &unenc_slate, Some(0), recipients)?)
}

/// Attempt to decode and decrypt a given slatepack
pub fn decrypt_slatepack(
wallet_interface: Arc<RwLock<WalletInterface<L, C>>>,
slatepack: String,
) -> Result<(Slatepack, Slate), GrinWalletInterfaceError> {
let w = wallet_interface.read().unwrap();
if let Some(o) = &w.owner_api {
let sp= o.decode_slatepack_message(None, slatepack.clone(), vec![0])?;
let slate = o.slate_from_slatepack_message(None, slatepack, vec![0])?;
return Ok((sp, slate))
} else {
return Err(GrinWalletInterfaceError::OwnerAPINotInstantiated);
}
}

pub async fn get_wallet_info(
wallet_interface: Arc<RwLock<WalletInterface<L, C>>>,
) -> Result<(bool, WalletInfo), GrinWalletInterfaceError> {
Expand Down Expand Up @@ -433,6 +452,41 @@ where
}
}

pub async fn receive_tx_from_s1(
wallet_interface: Arc<RwLock<WalletInterface<L, C>>>,
slate: Slate,
dest_slatepack_address: String,
) -> Result<Option<String>, GrinWalletInterfaceError> {
let w = wallet_interface.write().unwrap();
let ret_slate;
if let Some(f) = &w.foreign_api {
ret_slate = f.receive_tx(&slate, None, None)?;
} else {
return Err(GrinWalletInterfaceError::ForeignAPINotInstantiated);
}
if let Some(o) = &w.owner_api {
let encrypted = WalletInterface::encrypt_slatepack(o, &dest_slatepack_address, &ret_slate)?;
return Ok(Some(encrypted))
} else {
return Err(GrinWalletInterfaceError::OwnerAPINotInstantiated);
}
}

pub async fn finalize_from_s2(
wallet_interface: Arc<RwLock<WalletInterface<L, C>>>,
slate: Slate,
send_to_chain: bool,
) -> Result<Option<String>, GrinWalletInterfaceError> {
let w = wallet_interface.write().unwrap();
if let Some(o) = &w.owner_api {
let ret_slate = o.finalize_tx(None, &slate)?;
o.post_tx(None, &ret_slate, true)?;
return Ok(None)
} else {
return Err(GrinWalletInterfaceError::ForeignAPINotInstantiated);
}
}

pub async fn cancel_tx(
wallet_interface: Arc<RwLock<WalletInterface<L, C>>>,
id: u32,
Expand Down
13 changes: 10 additions & 3 deletions locale/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@
"wallet-create-tx": "Send",
"wallet-apply-tx": "Receive",
"wallet-home": "Wallet - Set Name Here TBD",
"apply-tx": "Progress Transaction",
"apply-tx": "Apply Transaction",
"create-tx": "Start Transaction",
"slatepack-address-name": "This Wallet's Slatepack Address",
"address-instruction": "Provide this address to others to allow them to send you funds",
Expand All @@ -235,6 +235,13 @@
"tx-list": "Transactions",
"tx-outstanding": "Outstanding",
"tx-recent": "Most Recent",
"tx-details": "Details"

"tx-details": "Details",
"tx-slatepack-paste-transaction-here": "Paste Transaction from Clipboard",
"tx-slatepack-read-result-default": "Ensure the clipboard contains the encrypted slatepack contents and press 'Continue'",
"tx-slatepack-read-failure": "Clipboard does not contain a slatepack that can be decrypted by this wallet",
"tx-continue": "Continue",
"apply-tx-confirm": "Confirm Transaction Details",
"tx-sender-name": "Sender",
"apply-tx-amount": "Incoming amount",
"tx-state": "Transaction Stage (this will be presented better)"
}
12 changes: 10 additions & 2 deletions locale/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@
"wallet-create-tx": "Send",
"wallet-apply-tx": "Receive",
"wallet-home": "Wallet - Set Name Here TBD",
"apply-tx": "Progress Transaction",
"apply-tx": "Apply Transaction",
"create-tx": "Start Transaction",
"slatepack-address-name": "This Wallet's Slatepack Address",
"address-instruction": "Provide this address to others to allow them to send you funds",
Expand All @@ -244,5 +244,13 @@
"tx-recent": "Most Recent",
"tx-details": "Details",
"tx-create-success-title": "Encrypted Transaction",
"tx-create-success-desc": "Copy/Paste this encrypted transaction to the recipient via a channel of your choosing"
"tx-create-success-desc": "Copy/Paste this encrypted transaction to the recipient via a channel of your choosing",
"tx-slatepack-paste-transaction-here": "Paste Transaction from Clipboard",
"tx-slatepack-read-result-default": "Ensure the clipboard contains the encrypted slatepack contents and press 'Continue'",
"tx-slatepack-read-failure": "Clipboard does not contain a slatepack that can be decrypted by this wallet",
"tx-continue": "Continue",
"apply-tx-confirm": "Confirm Transaction Details",
"tx-sender-name": "Sender",
"apply-tx-amount": "Incoming amount",
"tx-state": "Transaction Stage (this will be presented better)"
}