feat: Monero invoice/receipt frames, wallet RPC, /pay /tip
All checks were successful
ci / test (push) Successful in 2m50s
All checks were successful
ci / test (push) Successful in 2m50s
Signed inv/rcp codecs keyed by identity, payments table with unverified incoming receipts, optional JSON-RPC sidecar (loopback/.onion HTTP only), and TUI /pay /tip. OnionWire still holds no spend keys.
This commit is contained in:
parent
d304cc4c3b
commit
1a9b54d138
11 changed files with 1318 additions and 14 deletions
108
src/store.rs
108
src/store.rs
|
|
@ -67,6 +67,29 @@ pub struct Message {
|
|||
pub plaintext: Vec<u8>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Payment {
|
||||
pub id: i64,
|
||||
pub dir: String,
|
||||
pub kind: String,
|
||||
pub amount_atomic: String,
|
||||
pub address: String,
|
||||
pub memo: String,
|
||||
pub txid: Option<String>,
|
||||
pub verified: bool,
|
||||
pub created_at: i64,
|
||||
}
|
||||
|
||||
pub struct PaymentWrite<'a> {
|
||||
pub dir: &'a str,
|
||||
pub kind: &'a str,
|
||||
pub amount_atomic: &'a str,
|
||||
pub address: &'a str,
|
||||
pub memo: &'a str,
|
||||
pub txid: Option<&'a str>,
|
||||
pub verified: bool,
|
||||
}
|
||||
|
||||
impl Store {
|
||||
pub fn open() -> Result<Self> {
|
||||
Self::open_at(&home_dir()?)
|
||||
|
|
@ -126,6 +149,18 @@ impl Store {
|
|||
xmr_addr TEXT NOT NULL DEFAULT '',
|
||||
updated_at INTEGER NOT NULL
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS payments (
|
||||
id INTEGER PRIMARY KEY,
|
||||
friend_id INTEGER NOT NULL REFERENCES friends(id),
|
||||
dir TEXT NOT NULL CHECK(dir IN ('in','out')),
|
||||
kind TEXT NOT NULL CHECK(kind IN ('invoice','receipt')),
|
||||
amount_atomic TEXT NOT NULL,
|
||||
address TEXT NOT NULL,
|
||||
memo TEXT NOT NULL DEFAULT '',
|
||||
txid TEXT,
|
||||
verified INTEGER NOT NULL DEFAULT 0,
|
||||
created_at INTEGER NOT NULL
|
||||
);
|
||||
INSERT OR IGNORE INTO self_profile (id) VALUES (1);
|
||||
",
|
||||
)?;
|
||||
|
|
@ -421,6 +456,65 @@ impl Store {
|
|||
.map_err(Into::into)
|
||||
}
|
||||
|
||||
pub fn insert_payment(&self, friend_pk: &[u8], p: PaymentWrite<'_>) -> Result<i64> {
|
||||
if p.dir != "in" && p.dir != "out" {
|
||||
return Err(Error("dir must be in or out".into()));
|
||||
}
|
||||
if p.kind != "invoice" && p.kind != "receipt" {
|
||||
return Err(Error("kind must be invoice or receipt".into()));
|
||||
}
|
||||
crate::pay::parse_atomic(p.amount_atomic).map_err(|e| Error(e.to_string()))?;
|
||||
crate::pay::check_address(p.address).map_err(|e| Error(e.to_string()))?;
|
||||
let friend_id: i64 = self
|
||||
.conn
|
||||
.query_row(
|
||||
"SELECT id FROM friends WHERE pubkey = ?1",
|
||||
params![friend_pk],
|
||||
|row| row.get(0),
|
||||
)
|
||||
.map_err(|_| Error("friend not found".into()))?;
|
||||
self.conn.execute(
|
||||
"INSERT INTO payments (friend_id, dir, kind, amount_atomic, address, memo, txid, verified, created_at)
|
||||
VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9)",
|
||||
params![
|
||||
friend_id,
|
||||
p.dir,
|
||||
p.kind,
|
||||
p.amount_atomic,
|
||||
p.address,
|
||||
p.memo,
|
||||
p.txid,
|
||||
i64::from(p.verified),
|
||||
unix_now()
|
||||
],
|
||||
)?;
|
||||
Ok(self.conn.last_insert_rowid())
|
||||
}
|
||||
|
||||
pub fn list_payments(&self, friend_pk: &[u8]) -> Result<Vec<Payment>> {
|
||||
let mut stmt = self.conn.prepare(
|
||||
"SELECT p.id, p.dir, p.kind, p.amount_atomic, p.address, p.memo, p.txid, p.verified, p.created_at
|
||||
FROM payments p
|
||||
JOIN friends f ON f.id = p.friend_id
|
||||
WHERE f.pubkey = ?1
|
||||
ORDER BY p.id",
|
||||
)?;
|
||||
let rows = stmt.query_map(params![friend_pk], payment_from_row)?;
|
||||
let mut out = Vec::new();
|
||||
for row in rows {
|
||||
out.push(row?);
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
pub fn mark_verified(&self, id: i64) -> Result<bool> {
|
||||
let n = self.conn.execute(
|
||||
"UPDATE payments SET verified = 1 WHERE id = ?1",
|
||||
params![id],
|
||||
)?;
|
||||
Ok(n > 0)
|
||||
}
|
||||
|
||||
pub fn append_message(&self, friend_pk: &[u8], dir: &str, plaintext: &[u8]) -> Result<()> {
|
||||
if dir != "in" && dir != "out" {
|
||||
return Err(Error("dir must be in or out".into()));
|
||||
|
|
@ -480,6 +574,20 @@ impl Store {
|
|||
}
|
||||
}
|
||||
|
||||
fn payment_from_row(row: &rusqlite::Row<'_>) -> rusqlite::Result<Payment> {
|
||||
Ok(Payment {
|
||||
id: row.get(0)?,
|
||||
dir: row.get(1)?,
|
||||
kind: row.get(2)?,
|
||||
amount_atomic: row.get(3)?,
|
||||
address: row.get(4)?,
|
||||
memo: row.get(5)?,
|
||||
txid: row.get(6)?,
|
||||
verified: row.get::<_, i64>(7)? != 0,
|
||||
created_at: row.get(8)?,
|
||||
})
|
||||
}
|
||||
|
||||
fn profile_from_row(row: &rusqlite::Row<'_>) -> rusqlite::Result<FriendProfile> {
|
||||
Ok(FriendProfile {
|
||||
display_name: row.get(0)?,
|
||||
|
|
|
|||
Loading…
Reference in a new issue