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

Output inscription ID from ord wallet inscribe #1208

Merged
merged 8 commits into from
Jan 12, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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: 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}"))
}
}
33 changes: 11 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,23 @@ 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 {
reveal: Txid,
inscription: String,
}

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