Skip to content
This repository was archived by the owner on Sep 12, 2023. It is now read-only.

Commit a98bdb5

Browse files
committed
fixup! feat(taker): import seed
1 parent 84a510c commit a98bdb5

File tree

10 files changed

+37
-36
lines changed

10 files changed

+37
-36
lines changed

crates/daemon/src/seed.rs

+8
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ pub const APP_SEED_SIZE: usize = 32;
4242
pub trait Seed {
4343
fn seed(&self) -> Vec<u8>;
4444

45+
fn is_managed(&self) -> bool;
46+
4547
fn derive_extended_priv_key(&self, network: Network) -> Result<ExtendedPrivKey> {
4648
let mut ext_priv_key_seed = [0u8; 64];
4749

@@ -97,6 +99,9 @@ impl Seed for RandomSeed {
9799
fn seed(&self) -> Vec<u8> {
98100
self.0.to_vec()
99101
}
102+
fn is_managed(&self) -> bool {
103+
true
104+
}
100105
}
101106

102107
impl Debug for RandomSeed {
@@ -164,6 +169,9 @@ impl Seed for AppSeed {
164169
fn seed(&self) -> Vec<u8> {
165170
self.0.to_vec()
166171
}
172+
fn is_managed(&self) -> bool {
173+
false
174+
}
167175
}
168176

169177
impl From<[u8; APP_SEED_SIZE]> for AppSeed {

crates/daemon/src/wallet.rs

+5
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,15 @@ pub struct Actor<B, DB> {
111111
used_utxos: LockedUtxos,
112112
sender: watch::Sender<Option<WalletInfo>>,
113113
db: Option<Db>,
114+
managed_wallet: bool,
114115
}
115116

116117
impl Actor<ElectrumBlockchain, Tree> {
117118
pub fn spawn(
118119
electrum_rpc_url: &str,
119120
ext_priv_key: ExtendedPrivKey,
120121
db_path: PathBuf,
122+
managed_wallet: bool,
121123
) -> Result<(xtra::Address<Self>, watch::Receiver<Option<WalletInfo>>)> {
122124
let client = electrum_client::Client::new(electrum_rpc_url)
123125
.context("Failed to initialize Electrum RPC client")?;
@@ -147,6 +149,7 @@ impl Actor<ElectrumBlockchain, Tree> {
147149
used_utxos: LockedUtxos::new(time_to_lock),
148150
blockchain_client: ElectrumBlockchain::from(client),
149151
db: Some(db),
152+
managed_wallet,
150153
};
151154

152155
let (addr, fut) = actor.create(None).run();
@@ -267,6 +270,7 @@ where
267270
address,
268271
last_updated_at: Timestamp::now(),
269272
transactions,
273+
managed_wallet: self.managed_wallet,
270274
};
271275

272276
tracing::trace!(target : "wallet", sync_time_sec = %now.elapsed().as_secs(), "Wallet sync done");
@@ -588,6 +592,7 @@ mod tests {
588592
},
589593
blockchain_client: (),
590594
db: None,
595+
managed_wallet: true,
591596
})
592597
}
593598
}

crates/maker/src/main.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,12 @@ async fn main() -> Result<()> {
8585
let mut wallet_dir = data_dir.clone();
8686

8787
wallet_dir.push(MAKER_WALLET_ID);
88-
let (wallet, wallet_feed_receiver) =
89-
wallet::Actor::spawn(opts.network.electrum(), ext_priv_key, wallet_dir)?;
88+
let (wallet, wallet_feed_receiver) = wallet::Actor::spawn(
89+
opts.network.electrum(),
90+
ext_priv_key,
91+
wallet_dir,
92+
wallet_seed.is_managed(),
93+
)?;
9094

9195
if let Some(Withdraw::Withdraw {
9296
amount,

crates/model/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,7 @@ pub struct WalletInfo {
456456
pub address: Address,
457457
pub last_updated_at: Timestamp,
458458
pub transactions: Vec<TransactionDetails>,
459+
pub managed_wallet: bool,
459460
}
460461

461462
#[derive(Debug, Copy, Clone, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]

crates/shared-bin/src/to_sse_event.rs

+2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ pub struct WalletInfo {
2929
address: String,
3030
last_updated_at: Timestamp,
3131
transactions: Vec<TransactionDetails>,
32+
managed_wallet: bool,
3233
}
3334

3435
#[derive(Serialize, Debug, Clone, PartialEq, Eq, Default)]
@@ -75,6 +76,7 @@ impl ToSseEvent for Option<model::WalletInfo> {
7576
address: wallet_info.address.to_string(),
7677
last_updated_at: wallet_info.last_updated_at,
7778
transactions: transaction_details,
79+
managed_wallet: wallet_info.managed_wallet,
7880
}
7981
});
8082

crates/taker/src/lib.rs

+11-8
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ pub async fn run(opts: Opts) -> Result<()> {
385385
let mut wallet_dir = data_dir.clone();
386386
wallet_dir.push(TAKER_WALLET_ID);
387387
let (wallet, wallet_feed_receiver) =
388-
wallet::Actor::spawn(network.electrum(), ext_priv_key, wallet_dir)?;
388+
wallet::Actor::spawn(network.electrum(), ext_priv_key, wallet_dir, wallet_seed.is_managed())?;
389389

390390
if let Some(Withdraw::Withdraw {
391391
amount,
@@ -497,7 +497,7 @@ pub async fn run(opts: Opts) -> Result<()> {
497497
let rocket_auth_db_connection = RocketAuthDbConnection::new(db.clone());
498498
let users = Users::new(Box::new(rocket_auth_db_connection));
499499

500-
let mission_success = rocket::custom(figment)
500+
let mut rocket = rocket::custom(figment)
501501
.manage(feed_receivers)
502502
.manage(wallet_feed_receiver)
503503
.manage(identity_info)
@@ -513,8 +513,6 @@ pub async fn run(opts: Opts) -> Result<()> {
513513
routes::post_cfd_action,
514514
routes::post_withdraw_request,
515515
routes::put_sync_wallet,
516-
routes::get_export_seed,
517-
routes::put_import_seed,
518516
shared_bin::routes::get_health_check,
519517
shared_bin::routes::get_metrics,
520518
shared_bin::routes::get_version,
@@ -527,16 +525,21 @@ pub async fn run(opts: Opts) -> Result<()> {
527525
.register("/api", default_catchers())
528526
.manage(users)
529527
.manage(data_dir)
530-
.manage(environment)
531528
.manage(network)
532529
.mount("/", rocket::routes![routes::dist, routes::index])
533530
.register("/", default_catchers())
534531
.attach(fairings::log_launch())
535532
.attach(fairings::log_requests())
536-
.attach(fairings::ui_browser_launch(!opts.headless))
537-
.launch()
538-
.await?;
533+
.attach(fairings::ui_browser_launch(!opts.headless));
539534

535+
if wallet_seed.is_managed() {
536+
rocket = rocket.mount(
537+
"/api",
538+
rocket::routes![routes::get_export_seed, routes::put_import_seed],
539+
);
540+
}
541+
542+
let mission_success = rocket.launch().await?;
540543
tracing::trace!(?mission_success, "Rocket has landed");
541544

542545
db.close().await;

crates/taker/src/routes.rs

+1-20
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ use daemon::projection::FeedReceivers;
1414
use daemon::seed;
1515
use daemon::seed::RANDOM_SEED_SIZE;
1616
use daemon::wallet;
17-
use daemon::Environment;
1817
use daemon::TakerActorSystem;
1918
use http_api_problem::HttpApiProblem;
2019
use http_api_problem::StatusCode;
@@ -71,7 +70,6 @@ pub async fn feed(
7170
rx_maker_status: &State<watch::Receiver<ConnectionStatus>>,
7271
rx_maker_identity: &State<watch::Receiver<Option<identify::PeerInfo>>>,
7372
identity_info: &State<IdentityInfo>,
74-
environment: &State<Environment>,
7573
_user: User,
7674
) -> EventStream![] {
7775
let rx = rx.inner();
@@ -84,7 +82,6 @@ pub async fn feed(
8482
let identity = identity_info.inner().clone();
8583
let mut heartbeat =
8684
tokio::time::interval(std::time::Duration::from_secs(HEARTBEAT_INTERVAL_SECS));
87-
let environment = environment.inner().clone();
8885

8986
EventStream! {
9087

@@ -99,8 +96,6 @@ pub async fn feed(
9996

10097
yield Event::json(&identity).event("identity");
10198

102-
yield Event::json(&environment).event("environment");
103-
10499
let offers = rx_offers.borrow().clone();
105100
yield Event::json(&offers.btcusd_long).event("btcusd_long_offer");
106101
yield Event::json(&offers.btcusd_short).event("btcusd_short_offer");
@@ -316,15 +311,8 @@ pub async fn put_sync_wallet(taker: &State<Taker>, _user: User) -> Result<(), Ht
316311
#[instrument(name = "GET /export", skip_all)]
317312
pub async fn get_export_seed<'a>(
318313
data_dir: &'a State<PathBuf>,
319-
environment: &'a State<Environment>,
320314
_user: User,
321315
) -> Result<DownloadResponsePro<'a>, HttpApiProblem> {
322-
if environment.inner().as_string() == "umbrel" {
323-
return Err(HttpApiProblem::new(StatusCode::BAD_REQUEST)
324-
.title("Could not export seed file")
325-
.detail("Cannot export seed file for umbrel environment"));
326-
}
327-
328316
let seed = tokio::fs::read(data_dir.join(seed::TAKER_WALLET_SEED_FILE))
329317
.await
330318
.map_err(|e| {
@@ -335,7 +323,7 @@ pub async fn get_export_seed<'a>(
335323

336324
let resp = DownloadResponsePro::from_vec(
337325
seed,
338-
Some("taker_seed"),
326+
Some(seed::TAKER_WALLET_SEED_FILE),
339327
Some(mime::APPLICATION_OCTET_STREAM),
340328
);
341329
Ok(resp)
@@ -348,14 +336,7 @@ pub async fn put_import_seed(
348336
taker: &State<Taker>,
349337
data_dir: &State<PathBuf>,
350338
network: &State<Network>,
351-
environment: &State<Environment>,
352339
) -> Result<(), HttpApiProblem> {
353-
if environment.inner().as_string() == "umbrel" {
354-
return Err(HttpApiProblem::new(StatusCode::BAD_REQUEST)
355-
.title("Could not import seed file")
356-
.detail("Cannot import seed file for umbrel environment"));
357-
}
358-
359340
// fetch seed from upload stream as bytes. we only support the random seed.
360341
let seed = seed
361342
.open(RANDOM_SEED_SIZE.bytes())

taker-frontend/src/App.tsx

+1-3
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,6 @@ export const App = () => {
141141

142142
const identityOrUndefined = useLatestEvent<IdentityInfo>(source, "identity");
143143

144-
const environment = useLatestEvent<string>(source, "environment");
145-
146144
const shortOffer = makerOfferToTakerOffer(makerLong);
147145
const longOffer = makerOfferToTakerOffer(makerShort);
148146

@@ -289,7 +287,7 @@ export const App = () => {
289287
>
290288
<Route
291289
path="/wallet"
292-
element={<Wallet walletInfo={walletInfo} cfds={cfds.length > 0} environment={environment} />}
290+
element={<Wallet walletInfo={walletInfo} cfds={cfds.length > 0} />}
293291
/>
294292
<Route
295293
element={

taker-frontend/src/components/Wallet.tsx

+1-3
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ import Timestamp from "./Timestamp";
4747
interface WalletProps {
4848
walletInfo: WalletInfo | null;
4949
cfds: boolean;
50-
environment: string | null;
5150
}
5251

5352
interface SeedFile {
@@ -59,7 +58,6 @@ export default function Wallet(
5958
{
6059
walletInfo,
6160
cfds,
62-
environment,
6361
}: WalletProps,
6462
) {
6563
const toast = useToast();
@@ -282,7 +280,7 @@ export default function Wallet(
282280
</HStack>
283281
</VStack>
284282

285-
{environment !== "umbrel" && (
283+
{walletInfo?.managed_wallet && (
286284
<Box>
287285
<Divider marginTop={3} marginBottom={4} />
288286
<Center>

taker-frontend/src/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export interface WalletInfo {
1111
address: string;
1212
last_updated_at: number;
1313
transactions: Transaction[];
14+
managed_wallet: boolean;
1415
}
1516

1617
export interface Transaction {

0 commit comments

Comments
 (0)