Skip to content

Commit

Permalink
Convert ord wallet inscriptions to JSON (#1224)
Browse files Browse the repository at this point in the history
  • Loading branch information
casey authored Jan 16, 2023
1 parent 3a46ba0 commit b585240
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 13 deletions.
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"));
}

0 comments on commit b585240

Please sign in to comment.