Skip to content

Commit 0349929

Browse files
committed
read wallet config from KVStore instead of files
1 parent 4ee2df6 commit 0349929

1 file changed

Lines changed: 56 additions & 43 deletions

File tree

lightning/src/rgb_utils/mod.rs

Lines changed: 56 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -33,24 +33,23 @@ use tokio::runtime::Handle;
3333
use crate::io;
3434
use core::ops::Deref;
3535
use std::collections::HashMap;
36-
use std::fs;
37-
use std::path::{Path, PathBuf};
36+
use std::path::Path;
3837
use std::str::FromStr;
3938
use std::sync::Arc;
4039

4140
/// Static blinding constant (will be removed in the future)
4241
pub const STATIC_BLINDING: u64 = 777;
43-
/// Name of the file containing the bitcoin network
42+
/// KVStore key for the bitcoin network
4443
pub const BITCOIN_NETWORK_FNAME: &str = "bitcoin_network";
45-
/// Name of the file containing the electrum URL
44+
/// KVStore key for the electrum URL
4645
pub const INDEXER_URL_FNAME: &str = "indexer_url";
47-
/// Name of the file containing the wallet fingerprint
46+
/// KVStore key for the wallet fingerprint
4847
pub const WALLET_FINGERPRINT_FNAME: &str = "wallet_fingerprint";
49-
/// Name of the file containing the account-level xPub of the vanilla-side of the wallet
48+
/// KVStore key for the account-level xPub of the vanilla-side of the wallet
5049
pub const WALLET_ACCOUNT_XPUB_VANILLA_FNAME: &str = "wallet_account_xpub_vanilla";
51-
/// Name of the file containing the account-level xPub of the colored-side of the wallet
50+
/// KVStore key for the account-level xPub of the colored-side of the wallet
5251
pub const WALLET_ACCOUNT_XPUB_COLORED_FNAME: &str = "wallet_account_xpub_colored";
53-
/// Name of the file containing the master fingerprint of the wallet
52+
/// KVStore key for the master fingerprint of the wallet
5453
pub const WALLET_MASTER_FINGERPRINT_FNAME: &str = "wallet_master_fingerprint";
5554
const VANILLA_SYNC_LOOKBACK: u32 = 20;
5655

@@ -69,6 +68,8 @@ pub const RGB_PAYMENT_INFO_OUTBOUND_NS: &str = "payment_info_outbound";
6968
pub const RGB_TRANSFER_INFO_NS: &str = "transfer_info";
7069
/// Secondary namespace for consignment data
7170
pub const RGB_CONSIGNMENT_NS: &str = "consignment";
71+
/// Secondary namespace for wallet config values
72+
pub const RGB_WALLET_CONFIG_NS: &str = "wallet_config";
7273

7374
/// RGB channel info
7475
#[derive(Debug, Clone, Deserialize, Serialize)]
@@ -136,38 +137,32 @@ mod contract_id_serde {
136137
}
137138
}
138139

139-
fn _get_file_in_parent(ldk_data_dir: &Path, fname: &str) -> PathBuf {
140-
ldk_data_dir.parent().unwrap().join(fname)
141-
}
142-
143-
fn _read_file_in_parent(ldk_data_dir: &Path, fname: &str) -> String {
144-
fs::read_to_string(_get_file_in_parent(ldk_data_dir, fname)).unwrap()
145-
}
146-
147-
fn _get_rgb_wallet_dir(ldk_data_dir: &Path) -> PathBuf {
148-
let fingerprint = _read_file_in_parent(ldk_data_dir, WALLET_FINGERPRINT_FNAME);
149-
_get_file_in_parent(ldk_data_dir, &fingerprint)
150-
}
151-
152-
fn _get_bitcoin_network(ldk_data_dir: &Path) -> BitcoinNetwork {
153-
let bitcoin_network = _read_file_in_parent(ldk_data_dir, BITCOIN_NETWORK_FNAME);
140+
fn _get_bitcoin_network(kv_store: &dyn KVStoreSync) -> BitcoinNetwork {
141+
let bitcoin_network =
142+
kv_store.read_config(BITCOIN_NETWORK_FNAME).expect("bitcoin_network must be in KVStore");
154143
BitcoinNetwork::from_str(&bitcoin_network).unwrap()
155144
}
156145

157-
fn _get_account_xpub_colored(ldk_data_dir: &Path) -> String {
158-
_read_file_in_parent(ldk_data_dir, WALLET_ACCOUNT_XPUB_COLORED_FNAME)
146+
fn _get_account_xpub_colored(kv_store: &dyn KVStoreSync) -> String {
147+
kv_store
148+
.read_config(WALLET_ACCOUNT_XPUB_COLORED_FNAME)
149+
.expect("wallet_account_xpub_colored must be in KVStore")
159150
}
160151

161-
fn _get_account_xpub_vanilla(ldk_data_dir: &Path) -> String {
162-
_read_file_in_parent(ldk_data_dir, WALLET_ACCOUNT_XPUB_VANILLA_FNAME)
152+
fn _get_account_xpub_vanilla(kv_store: &dyn KVStoreSync) -> String {
153+
kv_store
154+
.read_config(WALLET_ACCOUNT_XPUB_VANILLA_FNAME)
155+
.expect("wallet_account_xpub_vanilla must be in KVStore")
163156
}
164157

165-
fn _get_master_fingerprint(ldk_data_dir: &Path) -> String {
166-
_read_file_in_parent(ldk_data_dir, WALLET_MASTER_FINGERPRINT_FNAME)
158+
fn _get_master_fingerprint(kv_store: &dyn KVStoreSync) -> String {
159+
kv_store
160+
.read_config(WALLET_MASTER_FINGERPRINT_FNAME)
161+
.expect("wallet_master_fingerprint must be in KVStore")
167162
}
168163

169-
fn _get_indexer_url(ldk_data_dir: &Path) -> String {
170-
_read_file_in_parent(ldk_data_dir, INDEXER_URL_FNAME)
164+
fn _get_indexer_url(kv_store: &dyn KVStoreSync) -> String {
165+
kv_store.read_config(INDEXER_URL_FNAME).expect("indexer_url must be in KVStore")
171166
}
172167

173168
fn _new_rgb_wallet(
@@ -200,18 +195,20 @@ fn _new_rgb_wallet(
200195
.expect("valid rgb-lib wallet")
201196
}
202197

203-
fn _get_wallet_data(ldk_data_dir: &Path) -> (String, BitcoinNetwork, String, String, String) {
198+
fn _get_wallet_data(
199+
ldk_data_dir: &Path, kv_store: &dyn KVStoreSync,
200+
) -> (String, BitcoinNetwork, String, String, String) {
204201
let data_dir = ldk_data_dir.parent().unwrap().to_string_lossy().to_string();
205-
let bitcoin_network = _get_bitcoin_network(ldk_data_dir);
206-
let account_xpub_vanilla = _get_account_xpub_vanilla(ldk_data_dir);
207-
let account_xpub_colored = _get_account_xpub_colored(ldk_data_dir);
208-
let master_fingerprint = _get_master_fingerprint(ldk_data_dir);
202+
let bitcoin_network = _get_bitcoin_network(kv_store);
203+
let account_xpub_vanilla = _get_account_xpub_vanilla(kv_store);
204+
let account_xpub_colored = _get_account_xpub_colored(kv_store);
205+
let master_fingerprint = _get_master_fingerprint(kv_store);
209206
(data_dir, bitcoin_network, account_xpub_vanilla, account_xpub_colored, master_fingerprint)
210207
}
211208

212-
async fn _get_rgb_wallet(ldk_data_dir: &Path) -> Wallet {
209+
async fn _get_rgb_wallet(ldk_data_dir: &Path, kv_store: &dyn KVStoreSync) -> Wallet {
213210
let (data_dir, bitcoin_network, account_xpub_vanilla, account_xpub_colored, master_fingerprint) =
214-
_get_wallet_data(ldk_data_dir);
211+
_get_wallet_data(ldk_data_dir, kv_store);
215212
tokio::task::spawn_blocking(move || {
216213
_new_rgb_wallet(
217214
data_dir,
@@ -227,11 +224,12 @@ async fn _get_rgb_wallet(ldk_data_dir: &Path) -> Wallet {
227224

228225
async fn _accept_transfer(
229226
ldk_data_dir: &Path, funding_txid: String, consignment_endpoint: RgbTransport,
227+
kv_store: &dyn KVStoreSync,
230228
) -> Result<(RgbTransfer, Vec<Assignment>), RgbLibError> {
231229
let funding_vout = 1;
232230
let (data_dir, bitcoin_network, account_xpub_vanilla, account_xpub_colored, master_fingerprint) =
233-
_get_wallet_data(ldk_data_dir);
234-
let indexer_url = _get_indexer_url(ldk_data_dir);
231+
_get_wallet_data(ldk_data_dir, kv_store);
232+
let indexer_url = _get_indexer_url(kv_store);
235233
tokio::task::spawn_blocking(move || {
236234
let mut wallet = _new_rgb_wallet(
237235
data_dir,
@@ -405,7 +403,7 @@ where
405403
let mut psbt = RgbLibPsbt::from_str(&psbt.to_string()).unwrap();
406404
let handle = Handle::current();
407405
let _ = handle.enter();
408-
let wallet = futures::executor::block_on(_get_rgb_wallet(ldk_data_dir));
406+
let wallet = futures::executor::block_on(_get_rgb_wallet(ldk_data_dir, kv_store));
409407
let (fascia, _) = wallet.color_psbt(&mut psbt, coloring_info).unwrap();
410408
let psbt = Psbt::from_str(&psbt.to_string()).unwrap();
411409
let modified_tx = match psbt.extract_tx() {
@@ -459,7 +457,7 @@ pub(crate) fn color_htlc(
459457
let mut psbt = RgbLibPsbt::from_str(&psbt.to_string()).unwrap();
460458
let handle = Handle::current();
461459
let _ = handle.enter();
462-
let wallet = futures::executor::block_on(_get_rgb_wallet(ldk_data_dir));
460+
let wallet = futures::executor::block_on(_get_rgb_wallet(ldk_data_dir, kv_store));
463461
let (fascia, _) = wallet.color_psbt(&mut psbt, coloring_info).unwrap();
464462
let psbt = Psbt::from_str(&psbt.to_string()).unwrap();
465463
let modified_tx = match psbt.extract_tx() {
@@ -521,7 +519,7 @@ pub(crate) fn color_closing(
521519
let mut psbt = RgbLibPsbt::from_str(&psbt.to_string()).unwrap();
522520
let handle = Handle::current();
523521
let _ = handle.enter();
524-
let wallet = futures::executor::block_on(_get_rgb_wallet(ldk_data_dir));
522+
let wallet = futures::executor::block_on(_get_rgb_wallet(ldk_data_dir, kv_store));
525523
let (fascia, _) = wallet.color_psbt(&mut psbt, coloring_info).unwrap();
526524
let psbt = Psbt::from_str(&psbt.to_string()).unwrap();
527525
let modified_tx = match psbt.extract_tx() {
@@ -613,6 +611,7 @@ pub(crate) fn handle_funding(
613611
ldk_data_dir,
614612
funding_txid.clone(),
615613
consignment_endpoint,
614+
kv_store,
616615
));
617616
let (consignment, remote_rgb_assignments) = match accept_res {
618617
Ok(res) => res,
@@ -737,6 +736,10 @@ pub trait RgbKvStoreExt {
737736
fn filter_first_hops(
738737
&self, payment_hash: &PaymentHash, first_hops: &mut Vec<ChannelDetails>,
739738
) -> (ContractId, u64);
739+
/// read a wallet config value from KVStore
740+
fn read_config(&self, key: &str) -> Result<String, io::Error>;
741+
/// write a wallet config value to KVStore
742+
fn write_config(&self, key: &str, value: &str);
740743
}
741744

742745
impl<K: KVStoreSync + ?Sized> RgbKvStoreExt for K {
@@ -834,4 +837,14 @@ impl<K: KVStoreSync + ?Sized> RgbKvStoreExt for K {
834837
});
835838
(contract_id, rgb_amount)
836839
}
840+
841+
fn read_config(&self, key: &str) -> Result<String, io::Error> {
842+
let data = self.read(RGB_PRIMARY_NS, RGB_WALLET_CONFIG_NS, key)?;
843+
String::from_utf8(data).map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))
844+
}
845+
846+
fn write_config(&self, key: &str, value: &str) {
847+
self.write(RGB_PRIMARY_NS, RGB_WALLET_CONFIG_NS, key, value.as_bytes().to_vec())
848+
.expect("KVStore write failed");
849+
}
837850
}

0 commit comments

Comments
 (0)