-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace Send/Receive Stores with a Database table
- Loading branch information
Showing
5 changed files
with
86 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,3 +23,6 @@ impl Database { | |
Ok(was_seen_before) | ||
} | ||
} | ||
|
||
#[cfg(feature = "v2")] | ||
mod v2; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
use bitcoincore_rpc::jsonrpc::serde_json; | ||
use payjoin::receive::v2::Enrolled; | ||
use payjoin::send::RequestContext; | ||
use redb::ReadableTable; | ||
|
||
use super::*; | ||
|
||
pub const SEND_SESSIONS: TableDefinition<&[u8], &str> = TableDefinition::new("send_sessions"); | ||
pub const RECV_SESSIONS: TableDefinition<&[u8], &str> = TableDefinition::new("recv_sessions"); | ||
|
||
impl Database { | ||
pub fn insert_recv_session(&self, session: Enrolled) -> Result<()> { | ||
let write_txn = self.0.begin_write()?; | ||
let key = &session.public_key().serialize(); | ||
let value = serde_json::to_string(&session)?; | ||
write_txn.open_table(RECV_SESSIONS)?.insert(key.as_slice(), value.as_str())?; | ||
write_txn.commit()?; | ||
Ok(()) | ||
} | ||
|
||
pub fn get_recv_session(&self) -> Result<Option<Enrolled>> { | ||
let read_txn = self.0.begin_read()?; | ||
if let Some((_, session)) = read_txn.open_table(RECV_SESSIONS)?.first()? { | ||
let session: Enrolled = serde_json::from_str(session.value())?; | ||
Ok(Some(session)) | ||
} else { | ||
Ok(None) | ||
} | ||
} | ||
|
||
pub fn clear_recv_session(&self) -> Result<()> { | ||
let write_txn = self.0.begin_write()?; | ||
let _ = write_txn.open_table(RECV_SESSIONS)?.pop_first()?; | ||
write_txn.commit()?; | ||
Ok(()) | ||
} | ||
|
||
pub fn insert_send_session(&self, session: &mut RequestContext) -> Result<()> { | ||
let write_txn = self.0.begin_write()?; | ||
let key = &session.public_key().serialize(); | ||
let value = serde_json::to_string(session)?; | ||
write_txn.open_table(SEND_SESSIONS)?.insert(key.as_slice(), value.as_str())?; | ||
write_txn.commit()?; | ||
Ok(()) | ||
} | ||
|
||
pub fn get_send_session(&self) -> Result<Option<RequestContext>> { | ||
let read_txn = self.0.begin_read()?; | ||
if let Some((_, session)) = read_txn.open_table(SEND_SESSIONS)?.first()? { | ||
let session: RequestContext = serde_json::from_str(session.value())?; | ||
Ok(Some(session)) | ||
} else { | ||
Ok(None) | ||
} | ||
} | ||
|
||
pub fn clear_send_session(&self) -> Result<()> { | ||
let write_txn = self.0.begin_write()?; | ||
let _ = write_txn.open_table(SEND_SESSIONS)?.pop_first()?; | ||
write_txn.commit()?; | ||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters