Commit 8ad93c0e authored by Daniel Edgecumbe's avatar Daniel Edgecumbe

backend: Add submitpsbt

parent f072bb89
Pipeline #310 passed with stages
in 1 minute and 2 seconds
......@@ -65,3 +65,36 @@ pub fn decode_psbt(rpc_auth: &str, rpc_addr: &str, psbt: &str) -> BoxFut {
return Box::new(proxied);
}
pub fn process_psbt(rpc_auth: &str, rpc_addr: &str, psbt: &str) -> BoxFut {
let proxied = bitcoin_rpc::get_resp_future_rpc(
"walletprocesspsbt".to_string(),
Some(vec![BitcoinRPCParameter::Str(psbt.to_string())]),
rpc_auth,
rpc_addr,
);
return Box::new(proxied);
}
pub fn finalize_psbt(rpc_auth: &str, rpc_addr: &str, psbt: &str) -> BoxFut {
let proxied = bitcoin_rpc::get_resp_future_rpc(
"finalizepsbt".to_string(),
Some(vec![BitcoinRPCParameter::Str(psbt.to_string())]),
rpc_auth,
rpc_addr,
);
return Box::new(proxied);
}
pub fn sendrawtransaction(rpc_auth: &str, rpc_addr: &str, hex: &str) -> BoxFut {
let proxied = bitcoin_rpc::get_resp_future_rpc(
"sendrawtransaction".to_string(),
Some(vec![BitcoinRPCParameter::Str(hex.to_string())]),
rpc_auth,
rpc_addr,
);
return Box::new(proxied);
}
......@@ -58,6 +58,23 @@ struct RPCCreatePSBT {
changepos: u32,
}
#[derive(Serialize, Deserialize, Debug)]
struct RPCResp<T> {
result: T,
}
#[derive(Serialize, Deserialize, Debug)]
struct RPCPSBT {
psbt: String,
complete: bool,
}
#[derive(Serialize, Deserialize, Debug)]
struct RPCRawTx {
hex: String,
complete: bool,
}
fn add_headers(response: &mut Response<Body>, cache_secs: usize) {
let hn = HeaderName::from_lowercase(b"cache-control").unwrap();
let hv = HeaderValue::from_str(
......@@ -491,6 +508,81 @@ pub fn server_get(x: &str, pool: &PgPool, rpc_auth: &str, rpc_addr: &str) -> Box
return Box::new(proxied);
}
if method == "submitpsbt" {
if raw_args.len() != 3 {
eprintln!("{:#?}", raw_args);
*response.body_mut() = Body::from(
"{\"result\": null, \"error\": \"submitpsbt/psbt\", \"id\": \"na\"}\n"
);
return Box::new(futures::future::ok(response));
}
let rpsbt: &str = raw_args[2];
// "decode" from urlsafe base64 to standard
let mut s = rpsbt.replace("-","+").replace("_","/").to_string();
let padding = (4 - (s.len() % 4)) % 4;
for _ in 0..padding {
s.push('=');
}
let rpc_auth = rpc_auth.to_string();
let rpc_addr = rpc_addr.to_string();
add_headers(&mut response, 1);
let proxied = createpsbt::process_psbt(
&rpc_auth, &rpc_addr, &s
)
.and_then(move |res| {
res
.into_body()
.concat2()
.and_then(move |c| {
let r: RPCResp<RPCPSBT> =
match serde_json::from_slice(&c) {
Ok(x) => x,
Err(y) => {
// TODO: distinctly suboptimal
eprintln!("{:#?}", c);
panic!("oh for fuck's sake {}", y);
},
};
createpsbt::finalize_psbt(
&rpc_auth, &rpc_addr, &r.result.psbt
)
.and_then(move |res| {
res
.into_body()
.concat2()
.and_then(move |c| {
let r: RPCResp<RPCRawTx> =
match serde_json::from_slice(&c) {
Ok(x) => x,
Err(y) => {
// TODO: distinctly suboptimal
panic!("oh for fuck's sake {}", y);
},
};
createpsbt::sendrawtransaction(
&rpc_auth, &rpc_addr, &r.result.hex
)
.and_then(move |res| {
res
.into_body()
.concat2()
.map(move |c| {
*response.body_mut() = Body::from(c);
response
})
})
})
})
})
});
return Box::new(proxied);
}
// done with weird custom methods now
let (cache_secs, argument_cap, _) = match WHITELIST.get(method) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment