Skip to content

Commit

Permalink
Show inscription on reveal transaction page (#954)
Browse files Browse the repository at this point in the history
  • Loading branch information
casey authored Dec 12, 2022
1 parent 6841837 commit 7e1ff2b
Show file tree
Hide file tree
Showing 16 changed files with 98 additions and 68 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ axum = "0.6.1"
axum-server = "0.4.0"
base64 = "0.13.1"
bitcoin = { version = "0.29.1", features = ["rand"] }
boilerplate = { version = "0.2.2", features = ["axum"] }
boilerplate = { version = "0.2.3", features = ["axum"] }
chrono = "0.4.19"
clap = { version = "3.1.0", features = ["derive"] }
ctrlc = "3.2.1"
Expand Down
4 changes: 4 additions & 0 deletions src/inscription.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ impl Inscription {
_ => None,
}
}

pub(crate) fn content_html(&self) -> Trusted<ContentHtml> {
Trusted(ContentHtml(self.content()))
}
}

#[derive(Debug, PartialEq)]
Expand Down
3 changes: 2 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use {
ordinal::Ordinal,
rarity::Rarity,
sat_point::SatPoint,
subcommand::Subcommand,
subcommand::{server::templates::ContentHtml, Subcommand},
tally::Tally,
},
anyhow::{anyhow, bail, Context, Error},
Expand All @@ -41,6 +41,7 @@ use {
chrono::{NaiveDateTime, TimeZone, Utc},
clap::Parser,
derive_more::{Display, FromStr},
html_escaper::{Escape, Trusted},
regex::Regex,
serde::{Deserialize, Serialize},
std::{
Expand Down
2 changes: 1 addition & 1 deletion src/subcommand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ mod index;
mod info;
mod list;
mod parse;
mod server;
pub(crate) mod server;
mod subsidy;
mod supply;
mod traits;
Expand Down
12 changes: 11 additions & 1 deletion src/subcommand/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use {
};

mod deserialize_from_str;
mod templates;
pub(crate) mod templates;

enum BlockQuery {
Height(u64),
Expand Down Expand Up @@ -475,6 +475,15 @@ impl Server {
Extension(chain): Extension<Chain>,
Path(txid): Path<Txid>,
) -> ServerResult<PageHtml> {
let inscription = index
.get_inscription_by_inscription_id(txid)
.map_err(|err| {
ServerError::Internal(anyhow!(
"failed to retrieve inscription from txid {txid} from index: {err}"
))
})?
.map(|(inscription, _satpoint)| inscription);

Ok(
TransactionHtml::new(
index
Expand All @@ -485,6 +494,7 @@ impl Server {
))
})?
.ok_or_else(|| ServerError::NotFound(format!("transaction {txid} unknown")))?,
inscription,
chain,
)
.page(
Expand Down
9 changes: 3 additions & 6 deletions src/subcommand/server/templates.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
use {
super::*,
boilerplate::Boilerplate,
html_escaper::{Escape, Trusted},
};
use {super::*, boilerplate::Boilerplate};

pub(crate) use {
block::BlockHtml, clock::ClockSvg, home::HomeHtml, input::InputHtml,
block::BlockHtml, clock::ClockSvg, content::ContentHtml, home::HomeHtml, input::InputHtml,
inscription::InscriptionHtml, ordinal::OrdinalHtml, output::OutputHtml, range::RangeHtml,
rare::RareTxt, transaction::TransactionHtml,
};

mod block;
mod clock;
mod content;
mod home;
mod input;
mod inscription;
Expand Down
17 changes: 17 additions & 0 deletions src/subcommand/server/templates/content.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use super::*;

pub(crate) struct ContentHtml<'a>(pub(crate) Option<Content<'a>>);

impl<'a> Display for ContentHtml<'a> {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match self.0 {
Some(Content::Text(text)) => text.escape(f, false),
Some(Content::Png(png)) => write!(
f,
"<img src='data:image/png;base64,{}'>",
base64::encode(png)
),
None => write!(f, "UNKNOWN"),
}
}
}
2 changes: 1 addition & 1 deletion src/subcommand/server/templates/inscription.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ mod tests {
<dt>satpoint</dt>
<dd>1111111111111111111111111111111111111111111111111111111111111111:1:0</dd>
</dl>
<img src=\"data:image/png;base64,AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQ==\">
<img src='data:image/png;base64,AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQ=='>
"
.unindent()
);
Expand Down
16 changes: 11 additions & 5 deletions src/subcommand/server/templates/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,23 @@ use super::*;

#[derive(Boilerplate)]
pub(crate) struct TransactionHtml {
txid: Txid,
transaction: Transaction,
chain: Chain,
inscription: Option<Inscription>,
transaction: Transaction,
txid: Txid,
}

impl TransactionHtml {
pub(crate) fn new(transaction: Transaction, chain: Chain) -> Self {
pub(crate) fn new(
transaction: Transaction,
inscription: Option<Inscription>,
chain: Chain,
) -> Self {
Self {
txid: transaction.txid(),
transaction,
chain,
inscription,
transaction,
}
}
}
Expand Down Expand Up @@ -49,7 +55,7 @@ mod tests {
};

pretty_assert_eq!(
TransactionHtml::new(transaction, Chain::Mainnet).to_string(),
TransactionHtml::new(transaction, None, Chain::Mainnet).to_string(),
"
<h1>Transaction <span class=monospace>9108ec7cbe9f1231dbf6374251b7267fb31cb23f36ed5a1d7344f5635b17dfe9</span></h1>
<h2>2 Outputs</h2>
Expand Down
12 changes: 1 addition & 11 deletions templates/inscription.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,4 @@ <h1>Inscription</h1>
<dt>satpoint</dt>
<dd>{{ self.satpoint }}</dd>
</dl>
%% match self.inscription.content() {
%% Some(Content::Text(text)) => {
{{ text }}
%% },
%% Some(Content::Png(png)) => {
<img src="data:image/png;base64,{{ base64::encode(png) }}">
%% },
%% None => {
UNKNOWN
%% },
%% }
{{ self.inscription.content_html() }}
12 changes: 1 addition & 11 deletions templates/ordinal.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,7 @@ <h1>Ordinal {{ self.ordinal.n() }}</h1>
<dt>time</dt><dd>{{ self.blocktime }}</dd>
%% if let Some(inscription) = &self.inscription {
<dt>inscription</dt>
%% match inscription.content() {
%% Some(Content::Text(text)) => {
<dd>{{ text }}</dd>
%% },
%% Some(Content::Png(png)) => {
<dd><img src="data:image/png;base64,{{ base64::encode(png) }}"></dd>
%% },
%% None => {
<dd>UNKNOWN</d>
%% }
%% }
<dd>{{ inscription.content_html() }}</dd>
%% }
</dl>
%% if self.ordinal.n() > 0 {
Expand Down
4 changes: 4 additions & 0 deletions templates/transaction.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
<h1>Transaction <span class=monospace>{{self.txid}}</span></h1>
%% if let Some(inscription) = &self.inscription {
<h2>Inscription</h2>
{{ inscription.content_html() }}
%% }
<h2>{{"Output".tally(self.transaction.output.len())}}</h2>
<ul class=monospace>
%% for (vout, output) in self.transaction.output.iter().enumerate() {
Expand Down
12 changes: 12 additions & 0 deletions tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@ macro_rules! assert_regex_match {
};
}

fn reveal_txid_from_inscribe_stdout(stdout: &str) -> Txid {
stdout
.lines()
.nth(1)
.unwrap()
.split('\t')
.nth(1)
.unwrap()
.parse()
.unwrap()
}

mod command_builder;
mod epochs;
mod expected;
Expand Down
41 changes: 26 additions & 15 deletions tests/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,11 @@ fn inscription_page() {
.stdout_regex("commit\t[[:xdigit:]]{64}\nreveal\t[[:xdigit:]]{64}\n")
.run();

let reveal_tx = stdout.split("reveal\t").collect::<Vec<&str>>()[1].trim();
let reveal_tx = reveal_txid_from_inscribe_stdout(&stdout);

rpc_server.mine_blocks(1);

let ord_server = TestServer::spawn_with_args(&rpc_server, &[]);
ord_server.assert_response_regex(
TestServer::spawn_with_args(&rpc_server, &[]).assert_response_regex(
&format!("/inscription/{}", reveal_tx),
&format!(
".*<h1>Inscription</h1>
Expand All @@ -63,19 +62,31 @@ fn inscription_page() {
HELLOWORLD.*",
),
);
}

let ord_server = TestServer::spawn_with_args(&rpc_server, &[]);
ord_server.assert_response_regex(
&format!("/inscription/{}", reveal_tx),
&format!(
".*<h1>Inscription</h1>
<dl>
<dt>satpoint</dt>
<dd>{reveal_tx}:0:0</dd>
</dl>
#[test]
fn inscription_appears_on_reveal_transaction_page() {
let rpc_server = test_bitcoincore_rpc::spawn_with(Network::Regtest, "ord");
let txid = rpc_server.mine_blocks(1)[0].txdata[0].txid();

let stdout = CommandBuilder::new(format!(
"--chain regtest wallet inscribe --satpoint {txid}:0:0 --file hello.txt"
))
.write("hello.txt", "HELLOWORLD")
.rpc_server(&rpc_server)
.stdout_regex("commit\t[[:xdigit:]]{64}\nreveal\t[[:xdigit:]]{64}\n")
.run();

let reveal_tx = reveal_txid_from_inscribe_stdout(&stdout);

rpc_server.mine_blocks(1);

TestServer::spawn_with_args(&rpc_server, &[]).assert_response_regex(
&format!("/tx/{}", reveal_tx),
".*<h1>Transaction .*</h1>.*
<h2>Inscription</h2>
HELLOWORLD.*",
),
)
);
}

#[test]
Expand All @@ -91,7 +102,7 @@ fn inscription_page_after_send() {
.stdout_regex("commit\t[[:xdigit:]]{64}\nreveal\t[[:xdigit:]]{64}\n")
.run();

let reveal_txid = stdout.split("reveal\t").collect::<Vec<&str>>()[1].trim();
let reveal_txid = reveal_txid_from_inscribe_stdout(&stdout);

rpc_server.mine_blocks(1);

Expand Down
14 changes: 1 addition & 13 deletions tests/wallet.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,5 @@
use {super::*, std::str::FromStr};

fn reveal_txid_from_inscribe_stdout(stdout: &str) -> Txid {
stdout
.lines()
.nth(1)
.unwrap()
.split('\t')
.nth(1)
.unwrap()
.parse()
.unwrap()
}

#[test]
fn satoshis() {
let rpc_server = test_bitcoincore_rpc::spawn();
Expand Down Expand Up @@ -509,7 +497,7 @@ fn inscriptions_cannot_be_sent_by_satpoint() {
.stdout_regex("commit\t[[:xdigit:]]{64}\nreveal\t[[:xdigit:]]{64}\n")
.run();

let reveal_txid = stdout.split("reveal\t").collect::<Vec<&str>>()[1].trim();
let reveal_txid = reveal_txid_from_inscribe_stdout(&stdout);

rpc_server.mine_blocks(1);

Expand Down

0 comments on commit 7e1ff2b

Please sign in to comment.