Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add decode api #3733

Merged
merged 6 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ use {
serde_hex::{SerHex, Strict},
};

pub use crate::templates::{
BlocksHtml as Blocks, RuneHtml as Rune, RunesHtml as Runes, StatusHtml as Status,
TransactionHtml as Transaction,
pub use crate::{
subcommand::decode::RawOutput as Decode,
templates::{
BlocksHtml as Blocks, RuneHtml as Rune, RunesHtml as Runes, StatusHtml as Status,
TransactionHtml as Transaction,
},
};

#[derive(Debug, PartialEq, Serialize, Deserialize)]
Expand Down
26 changes: 26 additions & 0 deletions src/subcommand/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ impl Server {
.route("/static/*path", get(Self::static_asset))
.route("/status", get(Self::status))
.route("/tx/:txid", get(Self::transaction))
.route("/decode/:txid", get(Self::decode))
.route("/update", get(Self::update))
.fallback(Self::fallback)
.layer(Extension(index))
Expand Down Expand Up @@ -914,6 +915,31 @@ impl Server {
})
}

async fn decode(
Extension(index): Extension<Arc<Index>>,
Path(txid): Path<Txid>,
AcceptJson(accept_json): AcceptJson,
) -> ServerResult {
task::block_in_place(|| {
let transaction = index
.get_transaction(txid)?
.ok_or_not_found(|| format!("transaction {txid}"))?;

let inscriptions = ParsedEnvelope::from_transaction(&transaction);
let runestone = Runestone::decipher(&transaction);

Ok(if accept_json {
Json(api::Decode {
inscriptions,
runestone,
})
.into_response()
} else {
StatusCode::NOT_FOUND.into_response()
})
})
}

async fn update(
Extension(settings): Extension<Arc<Settings>>,
Extension(index): Extension<Arc<Index>>,
Expand Down
49 changes: 48 additions & 1 deletion tests/json_api.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use {super::*, bitcoin::BlockHash};
use {
super::*,
bitcoin::BlockHash,
ord::{Envelope, Inscription},
};

#[test]
fn get_sat_without_sat_index() {
Expand Down Expand Up @@ -705,3 +709,46 @@ fn get_runes_balances() {

pretty_assert_eq!(runes_balance_json, rune_balances);
}

#[test]
fn get_decode_tx() {
let core = mockcore::builder().network(Network::Regtest).build();

let ord = TestServer::spawn_with_server_args(&core, &["--index-runes", "--regtest"], &[]);

create_wallet(&core, &ord);
core.mine_blocks(3);

let envelope = envelope(&[b"ord", &[1], b"text/plain;charset=utf-8", &[], b"bar"]);

let txid = core.broadcast_tx(TransactionTemplate {
inputs: &[(1, 0, 0, envelope.clone())],
..default()
});

let transaction = core.mine_blocks(1)[0].txdata[0].clone();

let inscriptions = vec![Envelope {
payload: Inscription {
body: Some(vec![98, 97, 114]),
content_type: Some(b"text/plain;charset=utf-8".into()),
..default()
},
input: 0,
offset: 0,
pushnum: false,
stutter: false,
}];
let runestone = Runestone::decipher(&transaction);
let response = ord.json_request(format!("/decode/{txid}"));

assert_eq!(response.status(), StatusCode::OK);

assert_eq!(
serde_json::from_str::<api::Decode>(&response.text().unwrap()).unwrap(),
api::Decode {
inscriptions,
runestone,
}
);
}
Loading