Skip to content

Commit

Permalink
Add custodial wallet info to boosts table
Browse files Browse the repository at this point in the history
  • Loading branch information
ericpp committed Oct 24, 2024
1 parent 9d39656 commit 0402edc
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 26 deletions.
73 changes: 52 additions & 21 deletions dbif/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ pub struct BoostRecord {
pub remote_podcast: Option<String>,
pub remote_episode: Option<String>,
pub reply_sent: bool,
pub custom_key: Option<u64>,
pub custom_value: Option<String>,
pub payment_info: Option<PaymentRecord>,
}

Expand Down Expand Up @@ -182,7 +184,9 @@ pub fn create_database(filepath: &String) -> Result<bool, Box<dyn Error>> {
episode text,
tlv text,
remote_podcast text,
remote_episode text
remote_episode text,
custom_key integer,
custom_value text
)",
[],
) {
Expand All @@ -208,6 +212,10 @@ pub fn create_database(filepath: &String) -> Result<bool, Box<dyn Error>> {
println!("Boosts reply sent column added.");
}

if conn.execute_batch("ALTER TABLE boosts ADD COLUMN custom_key integer; ALTER TABLE boosts ADD COLUMN custom_value text;").is_ok() {
println!("Boosts custom key/value added.");
}

//Create the node info table
match conn.execute(
"CREATE TABLE IF NOT EXISTS node_info (
Expand Down Expand Up @@ -518,22 +526,30 @@ pub fn add_node_info_to_db(filepath: &String, info: NodeInfoRecord) -> Result<bo
pub fn add_invoice_to_db(filepath: &String, boost: &BoostRecord) -> Result<bool, Box<dyn Error>> {
let conn = connect_to_database(false, filepath)?;

match conn.execute("INSERT INTO boosts (idx, time, value_msat, value_msat_total, action, sender, app, message, podcast, episode, tlv, remote_podcast, remote_episode, reply_sent) \
VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12, ?13, ?14)",
params![boost.index,
boost.time,
boost.value_msat,
boost.value_msat_total,
boost.action,
boost.sender,
boost.app,
boost.message,
boost.podcast,
boost.episode,
boost.tlv,
boost.remote_podcast,
boost.remote_episode,
boost.reply_sent]
match conn.execute(
"INSERT INTO boosts
(idx, time, value_msat, value_msat_total, action, sender, app, message, podcast, episode, tlv, remote_podcast, remote_episode, reply_sent, custom_key, custom_value)
VALUES
(?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12, ?13, ?14, ?15, ?16)
",
params![
boost.index,
boost.time,
boost.value_msat,
boost.value_msat_total,
boost.action,
boost.sender,
boost.app,
boost.message,
boost.podcast,
boost.episode,
boost.tlv,
boost.remote_podcast,
boost.remote_episode,
boost.reply_sent,
boost.custom_key,
boost.custom_value
]
) {
Ok(_) => {
Ok(true)
Expand Down Expand Up @@ -570,7 +586,7 @@ pub fn get_invoices_from_db(filepath: &String, invtype: &str, index: u64, max: u

//Query for boosts and automated boosts
let sqltxt = format!("
SELECT idx, time, value_msat, value_msat_total, action, sender, app, message, podcast, episode, tlv, remote_podcast, remote_episode, reply_sent
SELECT idx, time, value_msat, value_msat_total, action, sender, app, message, podcast, episode, tlv, remote_podcast, remote_episode, reply_sent, custom_key, custom_value
FROM boosts
WHERE
idx {} :index
Expand All @@ -597,6 +613,8 @@ pub fn get_invoices_from_db(filepath: &String, invtype: &str, index: u64, max: u
remote_podcast: row.get(11).ok(),
remote_episode: row.get(12).ok(),
reply_sent: row.get(13).unwrap_or(false),
custom_key: row.get(14).ok(),
custom_value: row.get(15).ok(),
payment_info: None,
})
}).unwrap();
Expand Down Expand Up @@ -656,9 +674,18 @@ pub fn get_last_boost_index_from_db(filepath: &String) -> Result<u64, Box<dyn Er
let max = 1;

//Prepare and execute the query
let mut stmt = conn.prepare("SELECT idx, time, value_msat, value_msat_total, action, sender, app, message, podcast, episode, tlv, remote_podcast, remote_episode, reply_sent \
FROM boosts \
ORDER BY idx DESC LIMIT :max")?;
let mut stmt = conn.prepare(
"SELECT
idx, time, value_msat, value_msat_total, action, sender, app, message, podcast, episode, tlv, remote_podcast, remote_episode, reply_sent, custom_key, custom_value
FROM
boosts
ORDER BY
idx DESC
LIMIT
:max
"
)?;

let rows = stmt.query_map(&[(":max", max.to_string().as_str())], |row| {
Ok(BoostRecord {
index: row.get(0)?,
Expand All @@ -675,6 +702,8 @@ pub fn get_last_boost_index_from_db(filepath: &String) -> Result<u64, Box<dyn Er
remote_podcast: row.get(11).ok(),
remote_episode: row.get(12).ok(),
reply_sent: row.get(13).unwrap_or(false),
custom_key: row.get(14).ok(),
custom_value: row.get(15).ok(),
payment_info: None,
})
}).unwrap();
Expand Down Expand Up @@ -790,6 +819,8 @@ pub fn get_payments_from_db(filepath: &String, index: u64, max: u64, direction:
remote_podcast: row.get(11).ok(),
remote_episode: row.get(12).ok(),
reply_sent: false,
custom_key: None,
custom_value: None,
payment_info: Some(PaymentRecord {
payment_hash: row.get(13)?,
pubkey: row.get(14)?,
Expand Down
30 changes: 25 additions & 5 deletions src/lightning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,10 +368,25 @@ pub async fn parse_boost_from_invoice(invoice: Invoice, remote_cache: &mut podca
remote_podcast: None,
remote_episode: None,
reply_sent: false,
custom_key: None,
custom_value: None,
payment_info: None,
};

parse_podcast_tlv(&mut boost, &htlc.custom_records[&TLV_PODCASTING20], remote_cache).await;
// Parse boost and custodial wallet TLVs
for (key, val) in htlc.custom_records {
if key == TLV_PODCASTING20 {
// Parse boost TLV
parse_podcast_tlv(&mut boost, &val, remote_cache).await;
}
else if key == TLV_WALLET_KEY || key == TLV_WALLET_ID || key == TLV_HIVE_ACCOUNT || key == TLV_FOUNTAIN_KEY {
// Parse custodial wallet info
let custom_value = std::str::from_utf8(&val).unwrap().to_string();
boost.custom_key = Some(key);
boost.custom_value = Some(custom_value);
break;
}
}

return Some(boost);
}
Expand Down Expand Up @@ -410,6 +425,8 @@ pub async fn parse_boost_from_payment(payment: Payment, remote_cache: &mut podca
tlv: "".to_string(),
remote_podcast: None,
remote_episode: None,
custom_key: None,
custom_value: None,
reply_sent: false,
payment_info: Some(dbif::PaymentRecord {
payment_hash: payment.payment_hash.clone(),
Expand All @@ -421,17 +438,20 @@ pub async fn parse_boost_from_payment(payment: Payment, remote_cache: &mut podca
}),
};

for (idx, val) in hop.custom_records {
if idx == TLV_PODCASTING20 {
// Parse boost and custodial wallet TLVs
for (key, val) in hop.custom_records {
if key == TLV_PODCASTING20 {
// Parse boost TLV
parse_podcast_tlv(&mut boost, &val, remote_cache).await;
}
else if idx == TLV_WALLET_KEY || idx == TLV_WALLET_ID || idx == TLV_HIVE_ACCOUNT || idx == TLV_FOUNTAIN_KEY {
else if key == TLV_WALLET_KEY || key == TLV_WALLET_ID || key == TLV_HIVE_ACCOUNT || key == TLV_FOUNTAIN_KEY {
// Parse custodial wallet info
let custom_value = std::str::from_utf8(&val).unwrap().to_string();

boost.payment_info = Some(dbif::PaymentRecord {
payment_hash: payment.payment_hash.clone(),
pubkey: hop.pub_key.clone(),
custom_key: idx,
custom_key: key,
custom_value,
fee_msat: payment.fee_msat,
reply_to_idx: None,
Expand Down

0 comments on commit 0402edc

Please sign in to comment.