Skip to content

Commit

Permalink
Output inscription ID from ord wallet inscribe (ordinals#1208)
Browse files Browse the repository at this point in the history
  • Loading branch information
casey authored Jan 12, 2023
1 parent d48dbf6 commit ab59f47
Show file tree
Hide file tree
Showing 9 changed files with 178 additions and 277 deletions.
9 changes: 9 additions & 0 deletions src/inscription_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ impl<'de> Deserialize<'de> for InscriptionId {
}
}

impl Serialize for InscriptionId {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.collect_str(self)
}
}

impl Display for InscriptionId {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "{}i{}", self.txid, self.index)
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ use {
html_escaper::{Escape, Trusted},
lazy_static::lazy_static,
regex::Regex,
serde::{Deserialize, Deserializer, Serialize},
serde::{Deserialize, Deserializer, Serialize, Serializer},
std::{
cmp,
collections::{BTreeMap, HashSet, VecDeque},
Expand Down
22 changes: 18 additions & 4 deletions src/subcommand/wallet/inscribe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ use {
std::collections::BTreeSet,
};

#[derive(Serialize)]
struct Output {
commit: Txid,
inscription: InscriptionId,
reveal: Txid,
}

#[derive(Debug, Parser)]
pub(crate) struct Inscribe {
#[clap(long, help = "Inscribe <SATPOINT>")]
Expand Down Expand Up @@ -69,16 +76,23 @@ impl Inscribe {
.sign_raw_transaction_with_wallet(&unsigned_commit_tx, None, None)?
.hex;

let commit_txid = client
let commit = client
.send_raw_transaction(&signed_raw_commit_tx)
.context("Failed to send commit transaction")?;

let reveal_txid = client
let reveal = client
.send_raw_transaction(&reveal_tx)
.context("Failed to send reveal transaction")?;

println!("commit\t{commit_txid}");
println!("reveal\t{reveal_txid}");
serde_json::to_writer_pretty(
io::stdout(),
&Output {
commit,
reveal,
inscription: reveal.into(),
},
)?;

Ok(())
}

Expand Down
6 changes: 6 additions & 0 deletions tests/command_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,10 @@ impl CommandBuilder {

stdout.into()
}

pub(crate) fn output<T: DeserializeOwned>(self) -> T {
let stdout = self.stdout_regex(".*").run();
serde_json::from_str(&stdout)
.unwrap_or_else(|err| panic!("Failed to deserialize JSON: {err}\n{stdout}"))
}
}
35 changes: 13 additions & 22 deletions tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use {
pretty_assertions::assert_eq as pretty_assert_eq,
regex::Regex,
reqwest::{StatusCode, Url},
serde::{de::DeserializeOwned, Deserialize},
std::{
fs,
net::TcpListener,
Expand Down Expand Up @@ -35,35 +36,25 @@ 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()
#[derive(Deserialize)]
struct Inscribe {
#[allow(dead_code)]
commit: Txid,
inscription: String,
reveal: Txid,
}

fn create_inscription(rpc_server: &test_bitcoincore_rpc::Handle, filename: &str) -> String {
fn inscribe(rpc_server: &test_bitcoincore_rpc::Handle) -> Inscribe {
rpc_server.mine_blocks(1);

let stdout = CommandBuilder::new(format!(
"--chain {} wallet inscribe {filename}",
rpc_server.network()
))
.write(filename, "HELLOWORLD")
.rpc_server(rpc_server)
.stdout_regex("commit\t[[:xdigit:]]{64}\nreveal\t[[:xdigit:]]{64}\n")
.run();

let reveal_txid = reveal_txid_from_inscribe_stdout(&stdout);
let output = CommandBuilder::new("wallet inscribe foo.txt")
.write("foo.txt", "FOO")
.rpc_server(rpc_server)
.output();

rpc_server.mine_blocks(1);

format!("{reveal_txid}i0")
output
}

fn create_wallet(rpc_server: &test_bitcoincore_rpc::Handle) {
Expand Down
Loading

0 comments on commit ab59f47

Please sign in to comment.