Skip to content

Commit

Permalink
Fix charms and add op code parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
onchainguy-btc committed Dec 9, 2023
1 parent 3e60231 commit 4c69287
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 26 deletions.
31 changes: 18 additions & 13 deletions src/index/updater/inscription_updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,10 @@ impl<'a, 'db, 'tx> InscriptionUpdater<'a, 'db, 'tx> {
}

// if the inscription was sent to an OP_RETURN it was burned
let is_burned = tx.output.get(input_index).unwrap().script_pubkey.is_op_return();
let is_burned = tx.output
.get(input_index)
.map(|output| output.script_pubkey.is_op_return())
.unwrap_or(false);

// find existing inscriptions on input (transfers of inscriptions)
for (old_satpoint, inscription_id) in Index::inscriptions_on_output(
Expand Down Expand Up @@ -379,20 +382,22 @@ impl<'a, 'db, 'tx> InscriptionUpdater<'a, 'db, 'tx> {
.unwrap()
.value();

let mut inscription_entry = InscriptionEntry::load(
self
.sequence_number_to_entry
.get(sequence_number)?
.unwrap()
.value(),
);
if flotsam.burned {
let mut inscription_entry = InscriptionEntry::load(
self
.sequence_number_to_entry
.get(sequence_number)?
.unwrap()
.value(),
);

Charm::Burned.set(&mut inscription_entry.charms);
Charm::Burned.set(&mut inscription_entry.charms);

self.sequence_number_to_entry.insert(
sequence_number,
&inscription_entry.store(),
)?;
self.sequence_number_to_entry.insert(
sequence_number,
&inscription_entry.store(),
)?;
}

(
false,
Expand Down
25 changes: 15 additions & 10 deletions src/subcommand/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use {
bitcoin::blockdata::script::Instruction::{
Op, PushBytes
},
bitcoin::blockdata::opcodes::all::OP_RETURN,
bitcoin::blockdata::opcodes::all::{OP_PUSHNUM_NEG1,OP_RETURN},
rust_embed::RustEmbed,
rustls_acme::{
acme::{LETS_ENCRYPT_PRODUCTION_DIRECTORY, LETS_ENCRYPT_STAGING_DIRECTORY},
Expand Down Expand Up @@ -1247,15 +1247,20 @@ impl Server {

// Check if the first instruction is OP_RETURN
if let Some(Ok(Op(OP_RETURN))) = instructions.next() {
is_burned = true;
// Extract the payload if it exists
instructions.filter_map(|instr| {
if let Ok(PushBytes(data)) = instr {
String::from_utf8(data.as_bytes().to_vec()).ok()
} else {
None
}
}).next()
// Check if the second instruction is OP_1NEGATE
if let Some(Ok(Op(OP_PUSHNUM_NEG1))) = instructions.next() {
is_burned = true;
// Extract the payload if it exists
instructions.filter_map(|instr| {
if let Ok(PushBytes(data)) = instr {
String::from_utf8(data.as_bytes().to_vec()).ok()
} else {
None
}
}).next()
} else {
None
}
} else {
None
}
Expand Down
14 changes: 11 additions & 3 deletions src/subcommand/wallet/send.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use bitcoin::script::Builder;
use {
super::*,
bitcoin::{
blockdata::opcodes::all::{OP_PUSHNUM_NEG1,OP_RETURN},
script::PushBytesBuf,
},
crate::{
Expand Down Expand Up @@ -138,9 +140,15 @@ impl Send {
let address = address.clone().require_network(options.chain().network())?;
Ok(ScriptBuf::from(address))
} else if let Some(msg) = &self.burn {
Ok(ScriptBuf::new_op_return(
&PushBytesBuf::try_from(Vec::from(msg.clone())).expect("burn payload too large")
))
let push_data_buf = PushBytesBuf::try_from(Vec::from(msg.clone()))
.expect("burn payload too large");

Ok(Builder::new()
.push_opcode(OP_RETURN)
.push_opcode(OP_PUSHNUM_NEG1)
.push_slice(&push_data_buf)
.into_script()
)
} else {
bail!("no valid output given")
}
Expand Down
4 changes: 4 additions & 0 deletions tests/wallet/send.rs
Original file line number Diff line number Diff line change
Expand Up @@ -569,4 +569,8 @@ fn burn_inscribed_sat() {
inscription_json.charms.eq(&Some(1u16)),
"inscription should have burned charm"
);
assert!(
inscription_json.burn_payload.eq(&Some("begone".into())),
"inscription should have burn payload"
)
}

0 comments on commit 4c69287

Please sign in to comment.