MCPcopy Create free account
hub / github.com/ElementsProject/lightning / verify_rune

Function verify_rune

plugins/rest-plugin/src/shared.rs:11–72  ·  view source on GitHub ↗
(
    plugin: &Plugin<PluginState>,
    rune_header: Option<String>,
    checkrune_params: &CheckRuneParams,
)

Source from the content-addressed store, hash-verified

9use crate::{CheckRuneParams, ClnrestMap, PluginState, structs::AppError};
10
11pub async fn verify_rune(
12 plugin: &Plugin<PluginState>,
13 rune_header: Option<String>,
14 checkrune_params: &CheckRuneParams,
15) -> Result<(), AppError> {
16 let rune = match rune_header {
17 Some(rune) => rune,
18 None => {
19 let err = RpcError {
20 code: Some(1501),
21 data: None,
22 message: "Not authorized: Missing rune".to_string(),
23 };
24 log::info!("verify_rune failed: {checkrune_params} {err}");
25 return Err(AppError::Forbidden(err));
26 }
27 };
28
29 let mut rpc_params = serde_json::Map::new();
30 rpc_params.insert("rune".to_owned(), json!(rune));
31 if let Some(nodeid) = &checkrune_params.nodeid {
32 rpc_params.insert("nodeid".to_owned(), json!(nodeid));
33 }
34 if let Some(method) = &checkrune_params.method {
35 rpc_params.insert("method".to_owned(), json!(method));
36 }
37 if let Some(params) = &checkrune_params.params {
38 rpc_params.insert("params".to_owned(), json!(params));
39 }
40 let rpc_params_value = serde_json::Value::Object(rpc_params);
41
42 let checkrune_result = match call_rpc(plugin, "checkrune", rpc_params_value).await {
43 Ok(o) => serde_json::from_value::<CheckruneResponse>(o).unwrap(),
44 Err(e) => {
45 log::info!("verify_rune failed: {checkrune_params} {e}");
46 return Err(AppError::Unauthorized(e));
47 }
48 };
49
50 if !checkrune_result.valid {
51 let err = RpcError {
52 code: Some(1502),
53 message: "Rune is not valid".to_string(),
54 data: None,
55 };
56 log::info!("verify_rune failed: {checkrune_params} {err}");
57 return Err(AppError::Unauthorized(err));
58 }
59
60 let showrunes_result = match call_rpc(plugin, "showrunes", json!({"rune": rune})).await {
61 Ok(r) => serde_json::from_value::<ShowrunesResponse>(r).unwrap(),
62 Err(e) => return Err(AppError::InternalServerError(e)),
63 };
64
65 log::info!(
66 "Authorized rune_id:`{}` access to {}",
67 showrunes_result.runes.first().unwrap().unique_id,
68 checkrune_params,

Callers 2

call_rpc_methodFunction · 0.85

Calls 4

call_rpcFunction · 0.85
to_stringMethod · 0.45
insertMethod · 0.45
unwrapMethod · 0.45

Tested by

no test coverage detected