Skip to content

Commit

Permalink
Add metadata recursive endpoint (#2604)
Browse files Browse the repository at this point in the history
  • Loading branch information
rot13maxi authored Nov 1, 2023
1 parent 8cdcca7 commit 0a90581
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 11 deletions.
34 changes: 24 additions & 10 deletions src/subcommand/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,18 +185,11 @@ impl Server {

let router = Router::new()
.route("/", get(Self::home))
.route("/r/blockheight", get(Self::block_height))
.route("/r/blockhash", get(Self::block_hash_json))
.route(
"/r/blockhash/:height",
get(Self::block_hash_from_height_json),
)
.route("/r/blocktime", get(Self::block_time))
.route("/block/:query", get(Self::block))
.route("/blockcount", get(Self::block_count))
.route("/blockheight", get(Self::block_height))
.route("/blockhash", get(Self::block_hash))
.route("/blockhash/:height", get(Self::block_hash_from_height))
.route("/blockheight", get(Self::block_height))
.route("/blocktime", get(Self::block_time))
.route("/bounties", get(Self::bounties))
.route("/clock", get(Self::clock))
Expand All @@ -207,6 +200,8 @@ impl Server {
.route("/input/:block/:transaction/:input", get(Self::input))
.route("/inscription/:inscription_query", get(Self::inscription))
.route("/inscriptions", get(Self::inscriptions))
.route("/inscriptions/:from", get(Self::inscriptions_from))
.route("/inscriptions/:from/:n", get(Self::inscriptions_from_n))
.route(
"/inscriptions/block/:height",
get(Self::inscriptions_in_block),
Expand All @@ -215,12 +210,18 @@ impl Server {
"/inscriptions/block/:height/:page_index",
get(Self::inscriptions_in_block_from_page),
)
.route("/inscriptions/:from", get(Self::inscriptions_from))
.route("/inscriptions/:from/:n", get(Self::inscriptions_from_n))
.route("/install.sh", get(Self::install_script))
.route("/ordinal/:sat", get(Self::ordinal))
.route("/output/:output", get(Self::output))
.route("/preview/:inscription_id", get(Self::preview))
.route("/r/blockhash", get(Self::block_hash_json))
.route(
"/r/blockhash/:height",
get(Self::block_hash_from_height_json),
)
.route("/r/blockheight", get(Self::block_height))
.route("/r/blocktime", get(Self::block_time))
.route("/r/metadata/:inscription_id", get(Self::metadata))
.route("/range/:start/:end", get(Self::range))
.route("/rare.txt", get(Self::rare_txt))
.route("/rune/:rune", get(Self::rune))
Expand Down Expand Up @@ -693,6 +694,19 @@ impl Server {
)
}

async fn metadata(
Extension(index): Extension<Arc<Index>>,
Path(inscription_id): Path<InscriptionId>,
) -> ServerResult<Json<String>> {
let metadata = index
.get_inscription_by_id(inscription_id)?
.ok_or_not_found(|| format!("inscription {inscription_id}"))?
.metadata
.ok_or_not_found(|| format!("inscription {inscription_id} metadata"))?;

Ok(Json(hex::encode(metadata)))
}

async fn status(Extension(index): Extension<Arc<Index>>) -> (StatusCode, &'static str) {
if index.is_unrecoverably_reorged() {
(
Expand Down
54 changes: 53 additions & 1 deletion tests/server.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use {super::*, crate::command_builder::ToArgs, ord::subcommand::wallet::send::Output};
use {
super::*, crate::command_builder::ToArgs, ciborium::value::Integer,
ord::subcommand::wallet::send::Output,
};

#[test]
fn run() {
Expand Down Expand Up @@ -190,6 +193,55 @@ fn inscription_content() {
assert_eq!(response.bytes().unwrap(), "FOO");
}

#[test]
fn inscription_metadata() {
let metadata = r#"{"foo":"bar","baz":1}"#;
let mut encoded_metadata = Vec::new();
let cbor_map = ciborium::value::Value::Map(vec![
(
ciborium::value::Value::Text("foo".into()),
ciborium::value::Value::Text("bar".into()),
),
(
ciborium::value::Value::Text("baz".into()),
ciborium::value::Value::Integer(Integer::from(1)),
),
]);
ciborium::ser::into_writer(&cbor_map, &mut encoded_metadata).unwrap();

let rpc_server = test_bitcoincore_rpc::spawn();
create_wallet(&rpc_server);

rpc_server.mine_blocks(1);

let inscription_id = CommandBuilder::new(
"wallet inscribe --fee-rate 1 --json-metadata metadata.json --file foo.txt",
)
.write("foo.txt", "FOO")
.write("metadata.json", metadata)
.rpc_server(&rpc_server)
.run_and_deserialize_output::<Inscribe>()
.inscriptions
.get(0)
.unwrap()
.id;

rpc_server.mine_blocks(1);

let response =
TestServer::spawn_with_args(&rpc_server, &[]).request(format!("/r/metadata/{inscription_id}"));

assert_eq!(response.status(), StatusCode::OK);
assert_eq!(
response.headers().get("content-type").unwrap(),
"application/json"
);
assert_eq!(
response.text().unwrap(),
format!("\"{}\"", hex::encode(encoded_metadata))
);
}

#[test]
fn inscriptions_page() {
let rpc_server = test_bitcoincore_rpc::spawn();
Expand Down

0 comments on commit 0a90581

Please sign in to comment.