From c6e004806aecef54c640119439406b5308fbacd0 Mon Sep 17 00:00:00 2001 From: Antoine Poinsot Date: Sat, 1 Oct 2022 02:22:25 +0200 Subject: [PATCH] commands: add a 'list_spend' command (and 'listspendtxs' RPC) --- doc/API.md | 25 ++++++++++++ src/commands/mod.rs | 21 ++++++++++ src/database/mod.rs | 10 +++++ src/database/sqlite/mod.rs | 10 +++++ src/jsonrpc/api.rs | 1 + src/testutils.rs | 10 +++++ tests/test_rpc.py | 84 +++++++++++++++++++++++++++++++++++++- 7 files changed, 159 insertions(+), 2 deletions(-) diff --git a/doc/API.md b/doc/API.md index f126fffb..15994554 100644 --- a/doc/API.md +++ b/doc/API.md @@ -10,6 +10,7 @@ Commands must be sent as valid JSONRPC 2.0 requests, ending with a `\n`. | [`stop`](#stop) | Stops the minisafe daemon | | [`getinfo`](#getinfo) | Get general information about the daemon | | [`getnewaddress`](#getnewaddress) | Get a new receiving address | +| [`listspendtxs`](#listspendtxs) | List all stored Spend transactions | # Reference @@ -130,3 +131,27 @@ This command does not return anything for now. | Field | Type | Description | | -------------- | --------- | ---------------------------------------------------- | + + +### `listspendtxs` + +List stored Spend transactions. + +#### Request + +This command does not take any parameter for now. + +| Field | Type | Description | +| ------------- | ----------------- | ----------------------------------------------------------- | + +#### Response + +| Field | Type | Description | +| -------------- | ------------- | ---------------------------------------------------------------- | +| `spend_txs` | array | Array of Spend tx entries | + +##### Spend tx entry + +| Field | Type | Description | +| ------------- | ----------------- | ----------------------------------------------------------- | +| `psbt` | string | Base64-encoded PSBT of the Spend transaction. | diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 4a364f19..94380a95 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -418,6 +418,16 @@ impl DaemonControl { Ok(()) } + + pub fn list_spend(&self) -> ListSpendResult { + let mut db_conn = self.db.connection(); + let spend_txs = db_conn + .list_spend() + .into_iter() + .map(|psbt| ListSpendEntry { psbt }) + .collect(); + ListSpendResult { spend_txs } + } } #[derive(Debug, Clone, Serialize, Deserialize)] @@ -462,6 +472,17 @@ pub struct CreateSpendResult { pub psbt: Psbt, } +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct ListSpendEntry { + #[serde(serialize_with = "ser_base64", deserialize_with = "deser_psbt_base64")] + pub psbt: Psbt, +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct ListSpendResult { + pub spend_txs: Vec, +} + #[cfg(test)] mod tests { use super::*; diff --git a/src/database/mod.rs b/src/database/mod.rs index 2bf5214b..e5b4c9de 100644 --- a/src/database/mod.rs +++ b/src/database/mod.rs @@ -76,6 +76,9 @@ pub trait DatabaseConnection { /// Insert a new Spend transaction or replace an existing one. fn store_spend(&mut self, psbt: &Psbt); + + /// List all existing Spend transactions. + fn list_spend(&mut self) -> Vec; } // FIXME: if possible, avoid reallocating. @@ -171,6 +174,13 @@ impl DatabaseConnection for SqliteConn { fn store_spend(&mut self, psbt: &Psbt) { self.store_spend(psbt) } + + fn list_spend(&mut self) -> Vec { + self.list_spend() + .into_iter() + .map(|db_spend| db_spend.psbt) + .collect() + } } #[derive(Debug, Clone, PartialEq, Eq)] diff --git a/src/database/sqlite/mod.rs b/src/database/sqlite/mod.rs index 55b76171..a144e84e 100644 --- a/src/database/sqlite/mod.rs +++ b/src/database/sqlite/mod.rs @@ -382,6 +382,16 @@ impl SqliteConn { }) .expect("Db must not fail"); } + + pub fn list_spend(&mut self) -> Vec { + db_query( + &mut self.conn, + "SELECT * FROM spend_transactions", + rusqlite::params![], + |row| row.try_into(), + ) + .expect("Db must not fail") + } } #[cfg(test)] diff --git a/src/jsonrpc/api.rs b/src/jsonrpc/api.rs index 90f224c6..b905bf7d 100644 --- a/src/jsonrpc/api.rs +++ b/src/jsonrpc/api.rs @@ -72,6 +72,7 @@ pub fn handle_request(control: &DaemonControl, req: Request) -> Result serde_json::json!(&control.get_info()), "getnewaddress" => serde_json::json!(&control.get_new_address()), "listcoins" => serde_json::json!(&control.list_coins()), + "listspendtxs" => serde_json::json!(&control.list_spend()), "stop" => serde_json::json!({}), "updatespend" => { let params = req diff --git a/src/testutils.rs b/src/testutils.rs index 5cefaf93..563baec4 100644 --- a/src/testutils.rs +++ b/src/testutils.rs @@ -170,6 +170,16 @@ impl DatabaseConnection for DummyDbConn { fn spend_tx(&mut self, txid: &bitcoin::Txid) -> Option { self.db.read().unwrap().spend_txs.get(txid).cloned() } + + fn list_spend(&mut self) -> Vec { + self.db + .read() + .unwrap() + .spend_txs + .values() + .cloned() + .collect() + } } pub struct DummyMinisafe { diff --git a/tests/test_rpc.py b/tests/test_rpc.py index 84e077fa..9d8ac87e 100644 --- a/tests/test_rpc.py +++ b/tests/test_rpc.py @@ -95,6 +95,41 @@ def test_create_spend(minisafed, bitcoind): bitcoind.rpc.sendrawtransaction(signed_tx_hex) +def test_list_spend(minisafed, bitcoind): + # Start by creating two conflicting Spend PSBTs + addr = minisafed.rpc.getnewaddress()["address"] + bitcoind.rpc.sendtoaddress(addr, 0.2567) + wait_for(lambda: len(minisafed.rpc.listcoins()["coins"]) == 1) + outpoints = [c["outpoint"] for c in minisafed.rpc.listcoins()["coins"]] + destinations = { + bitcoind.rpc.getnewaddress(): 200_000, + } + res = minisafed.rpc.createspend(outpoints, destinations, 6) + assert "psbt" in res + + addr = minisafed.rpc.getnewaddress()["address"] + bitcoind.rpc.sendtoaddress(addr, 0.0987) + wait_for(lambda: len(minisafed.rpc.listcoins()["coins"]) == 2) + outpoints = [c["outpoint"] for c in minisafed.rpc.listcoins()["coins"]] + destinations = { + bitcoind.rpc.getnewaddress(): 400_000, + } + res_b = minisafed.rpc.createspend(outpoints, destinations, 2) + assert "psbt" in res_b + + # Store them both in DB. + assert len(minisafed.rpc.listspendtxs()["spend_txs"]) == 0 + minisafed.rpc.updatespend(res["psbt"]) + minisafed.rpc.updatespend(res_b["psbt"]) + + # Listing all Spend transactions will list them both. + list_res = minisafed.rpc.listspendtxs()["spend_txs"] + assert len(list_res) == 2 + all_psbts = [entry["psbt"] for entry in list_res] + assert res["psbt"] in all_psbts + assert res_b["psbt"] in all_psbts + + def test_update_spend(minisafed, bitcoind): # Start by creating a Spend PSBT addr = minisafed.rpc.getnewaddress()["address"] @@ -108,7 +143,52 @@ def test_update_spend(minisafed, bitcoind): assert "psbt" in res # Now update it + assert len(minisafed.rpc.listspendtxs()["spend_txs"]) == 0 minisafed.rpc.updatespend(res["psbt"]) + list_res = minisafed.rpc.listspendtxs()["spend_txs"] + assert len(list_res) == 1 + assert list_res[0]["psbt"] == res["psbt"] - # TODO: check it's stored once we implement 'listspendtxs' - # TODO: check with added signatures once we implement 'listspendtxs' + # Keep a copy for later. + psbt_no_sig = PSBT() + psbt_no_sig.deserialize(res["psbt"]) + + # We can add a signature and update it + psbt_sig_a = PSBT() + psbt_sig_a.deserialize(res["psbt"]) + dummy_pk_a = bytes.fromhex( + "0375e00eb72e29da82b89367947f29ef34afb75e8654f6ea368e0acdfd92976b7c" + ) + dummy_sig_a = bytes.fromhex( + "304402202b925395cfeaa0171a7a92982bb4891acc4a312cbe7691d8375d36796d5b570a0220378a8ab42832848e15d1aedded5fb360fedbdd6c39226144e527f0f1e19d5398" + ) + psbt_sig_a.inputs[0].partial_sigs[dummy_pk_a] = dummy_sig_a + psbt_sig_a_ser = psbt_sig_a.serialize() + minisafed.rpc.updatespend(psbt_sig_a_ser) + + # We'll get it when querying + list_res = minisafed.rpc.listspendtxs()["spend_txs"] + assert len(list_res) == 1 + assert list_res[0]["psbt"] == psbt_sig_a_ser + + # We can add another signature to the empty PSBT and update it again + psbt_sig_b = PSBT() + psbt_sig_b.deserialize(res["psbt"]) + dummy_pk_b = bytes.fromhex( + "03a1b26313f430c4b15bb1fdce663207659d8cac749a0e53d70eff01874496feff" + ) + dummy_sig_b = bytes.fromhex( + "3044022005aebcd649fb8965f0591710fb3704931c3e8118ee60dd44917479f63ceba6d4022018b212900e5a80e9452366894de37f0d02fb9c89f1e94f34fb6ed7fd71c15c41" + ) + psbt_sig_b.inputs[0].partial_sigs[dummy_pk_b] = dummy_sig_b + psbt_sig_b_ser = psbt_sig_b.serialize() + minisafed.rpc.updatespend(psbt_sig_b_ser) + + # It will have merged both. + list_res = minisafed.rpc.listspendtxs()["spend_txs"] + assert len(list_res) == 1 + psbt_merged = PSBT() + psbt_merged.deserialize(list_res[0]["psbt"]) + assert len(psbt_merged.inputs[0].partial_sigs) == 2 + assert psbt_merged.inputs[0].partial_sigs[dummy_pk_a] == dummy_sig_a + assert psbt_merged.inputs[0].partial_sigs[dummy_pk_b] == dummy_sig_b