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

Make ord wallet inscriptions output JSON #1224

Merged
merged 2 commits into from
Jan 16, 2023
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: 9 additions & 0 deletions src/sat_point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ impl Decodable for SatPoint {
}
}

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

impl FromStr for SatPoint {
type Err = Error;

Expand Down
19 changes: 16 additions & 3 deletions src/subcommand/wallet/inscriptions.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,30 @@
use super::*;

#[derive(Serialize)]
struct Output {
inscription: InscriptionId,
location: SatPoint,
}

pub(crate) fn run(options: Options) -> Result {
let index = Index::open(&options)?;
index.update()?;

let inscriptions = index.get_inscriptions(None)?;
let unspent_outputs = get_unspent_outputs(&options)?;

for (satpoint, inscription_id) in inscriptions {
if unspent_outputs.contains_key(&satpoint.outpoint) {
println!("{}\t{}", inscription_id, satpoint);
let mut output = Vec::new();

for (location, inscription) in inscriptions {
if unspent_outputs.contains_key(&location.outpoint) {
output.push(Output {
location,
inscription,
});
}
}

serde_json::to_writer_pretty(io::stdout(), &output)?;

Ok(())
}
34 changes: 24 additions & 10 deletions tests/wallet/inscriptions.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
use super::*;

#[derive(Deserialize)]
struct Inscription {
inscription: String,
location: String,
}

#[test]
fn inscriptions() {
let rpc_server = test_bitcoincore_rpc::spawn();
Expand All @@ -12,10 +18,14 @@ fn inscriptions() {
..
} = inscribe(&rpc_server);

CommandBuilder::new("wallet inscriptions")
let inscriptions = CommandBuilder::new("wallet inscriptions")
.rpc_server(&rpc_server)
.expected_stdout(format!("{inscription}\t{reveal}:0:0\n"))
.run();
.output::<Vec<Inscription>>();

assert_eq!(inscriptions.len(), 1);
assert_eq!(inscriptions[0].inscription, inscription);
assert_eq!(inscriptions[0].location, format!("{reveal}:0:0"));

let stdout = CommandBuilder::new("wallet receive")
.rpc_server(&rpc_server)
Expand All @@ -35,12 +45,13 @@ fn inscriptions() {

let txid = Txid::from_str(stdout.trim()).unwrap();

let outpoint = OutPoint::new(txid, 0);

CommandBuilder::new("wallet inscriptions")
let inscriptions = CommandBuilder::new("wallet inscriptions")
.rpc_server(&rpc_server)
.expected_stdout(format!("{inscription}\t{outpoint}:0\n"))
.run();
.output::<Vec<Inscription>>();

assert_eq!(inscriptions.len(), 1);
assert_eq!(inscriptions[0].inscription, inscription);
assert_eq!(inscriptions[0].location, format!("{txid}:0:0"));
}

#[test]
Expand All @@ -63,8 +74,11 @@ fn inscriptions_includes_locked_utxos() {
vout: 0,
});

CommandBuilder::new("wallet inscriptions")
let inscriptions = CommandBuilder::new("wallet inscriptions")
.rpc_server(&rpc_server)
.expected_stdout(format!("{inscription}\t{reveal}:0:0\n"))
.run();
.output::<Vec<Inscription>>();

assert_eq!(inscriptions.len(), 1);
assert_eq!(inscriptions[0].inscription, inscription);
assert_eq!(inscriptions[0].location, format!("{reveal}:0:0"));
}