Skip to content

Commit 9b3e38a

Browse files
author
Ludo Galabru
committed
feat: draft naive inscription detection
1 parent ee9a345 commit 9b3e38a

File tree

3 files changed

+78
-14
lines changed

3 files changed

+78
-14
lines changed

components/chainhook-event-observer/src/indexer/bitcoin/mod.rs

+58-14
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,32 @@ pub fn standardize_bitcoin_block(
107107
) -> Result<BitcoinBlockData, String> {
108108
let mut transactions = vec![];
109109

110-
let expected_magic_bytes = get_canonical_magic_bytes(&indexer_config.bitcoin_network);
110+
let expected_magic_bytes = get_stacks_canonical_magic_bytes(&indexer_config.bitcoin_network);
111111
let pox_config = get_canonical_pox_config(&indexer_config.bitcoin_network);
112112

113-
for mut tx in block.txdata.into_iter() {
114-
let txid = tx.txid().to_string();
113+
ctx.try_log(|logger| slog::info!(logger, "Start processing Bitcoin block {}", block.hash,));
114+
115+
for mut tx in block.tx.into_iter() {
116+
let txid = tx.txid.to_string();
117+
118+
ctx.try_log(|logger| slog::info!(logger, "Start processing Bitcoin transaction {txid}"));
119+
120+
let mut stacks_operations = vec![];
121+
if let Some(op) = try_parse_stacks_operation(
122+
&tx.vout,
123+
&pox_config,
124+
&expected_magic_bytes,
125+
block_height,
126+
ctx,
127+
) {
128+
stacks_operations.push(op);
129+
}
130+
131+
let mut ordinal_operations = vec![];
132+
if let Some(op) = try_parse_ordinal_operation(&tx, block_height, ctx) {
133+
ordinal_operations.push(op);
134+
}
135+
115136
let mut inputs = vec![];
116137
for input in tx.vin.drain(..) {
117138
if input.is_coinbase() {
@@ -141,17 +162,6 @@ pub fn standardize_bitcoin_block(
141162
}
142163

143164
let mut outputs = vec![];
144-
let mut stacks_operations = vec![];
145-
146-
if let Some(op) = try_parse_stacks_operation(
147-
&tx.output,
148-
&pox_config,
149-
&expected_magic_bytes,
150-
block_height,
151-
ctx,
152-
) {
153-
stacks_operations.push(op);
154-
}
155165
for output in tx.vout.drain(..) {
156166
outputs.push(TxOut {
157167
value: output.value.to_sat(),
@@ -168,6 +178,7 @@ pub fn standardize_bitcoin_block(
168178
inputs,
169179
outputs,
170180
stacks_operations,
181+
ordinal_operations,
171182
proof: None,
172183
},
173184
};
@@ -189,6 +200,39 @@ pub fn standardize_bitcoin_block(
189200
})
190201
}
191202

203+
fn try_parse_ordinal_operation(
204+
tx: &GetRawTransactionResult,
205+
block_height: u64,
206+
ctx: &Context,
207+
) -> Option<OrdinalOperation> {
208+
let (pos, magic_bytes) = get_ordinal_canonical_magic_bytes();
209+
let limit = pos + magic_bytes.len();
210+
211+
for input in tx.vin.iter() {
212+
if let Some(ref witnesses) = input.txinwitness {
213+
for witness in witnesses.iter() {
214+
if witness.len() > limit && witness[pos..limit] == magic_bytes {
215+
ctx.try_log(|logger| {
216+
slog::info!(
217+
logger,
218+
"Ordinal operation detected in transaction {}",
219+
tx.txid,
220+
)
221+
});
222+
return Some(OrdinalOperation::InscriptionReveal(
223+
OrdinalInscriptionRevealData {
224+
satoshi_point: "".into(),
225+
content_type: "".into(),
226+
content: vec![],
227+
},
228+
));
229+
}
230+
}
231+
}
232+
}
233+
None
234+
}
235+
192236
fn try_parse_stacks_operation(
193237
outputs: &Vec<GetRawTransactionResultVout>,
194238
pox_config: &PoxConfig,

components/chainhook-event-observer/src/indexer/bitcoin/tests.rs

+19
Original file line numberDiff line numberDiff line change
@@ -204,3 +204,22 @@ fn test_bitcoin_vector_040() {
204204
// fn test_bitcoin_vector_041() {
205205
// process_bitcoin_blocks_and_check_expectations(helpers::shapes::get_vector_041());
206206
// }
207+
208+
#[test]
209+
fn test_ordinal_inscription_parsing() {
210+
use clarity_repl::clarity::util::hash::hex_bytes;
211+
212+
let witness = hex_bytes("208737bc46923c3e64c7e6768c0346879468bf3aba795a5f5f56efca288f50ed2aac0063036f7264010118746578742f706c61696e3b636861727365743d7574662d38004c9948656c6c6f2030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030300a68").unwrap();
213+
214+
let (pos, magic_bytes) = super::get_ordinal_canonical_magic_bytes();
215+
let limit = pos + magic_bytes.len();
216+
217+
println!("{:?}", &magic_bytes);
218+
219+
// println!("{:?}", &witness);
220+
println!("{:?}", &witness[pos..limit]);
221+
if witness.len() > limit && witness[pos..limit] == magic_bytes {
222+
} else {
223+
panic!();
224+
}
225+
}

components/chainhook-event-observer/src/indexer/tests/helpers/transactions.rs

+1
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ pub fn generate_test_tx_bitcoin_p2pkh_transfer(
104104
metadata: BitcoinTransactionMetadata {
105105
inputs: vec![],
106106
outputs,
107+
ordinal_operations: vec![],
107108
stacks_operations: vec![],
108109
proof: None,
109110
},

0 commit comments

Comments
 (0)