Skip to content

Commit

Permalink
Skip outputs with non pushdata opcodes
Browse files Browse the repository at this point in the history
  • Loading branch information
casey committed Mar 20, 2024
1 parent c53af97 commit 3dcab13
Showing 1 changed file with 44 additions and 20 deletions.
64 changes: 44 additions & 20 deletions src/runes/runestone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,22 +226,29 @@ impl Runestone {
}

fn payload(transaction: &Transaction) -> Result<Option<Vec<u8>>, script::Error> {
for output in &transaction.output {
// search transaction outputs for payload
'outer: for output in &transaction.output {
let mut instructions = output.script_pubkey.instructions();

// payload starts with OP_RETURN
if instructions.next().transpose()? != Some(Instruction::Op(opcodes::all::OP_RETURN)) {
continue;
}

// followed by the protocol identifier
if instructions.next().transpose()? != Some(Instruction::Op(MAGIC_NUMBER)) {
continue;
}

// construct the payload by concatinating remaining data pushes
let mut payload = Vec::new();

for result in instructions {
if let Instruction::PushBytes(push) = result? {
payload.extend_from_slice(push.as_bytes());
} else {
// if we encounter a non data push opcode, continue to the next output
continue 'outer;
}
}

Expand Down Expand Up @@ -438,35 +445,52 @@ mod tests {
}

#[test]
fn non_push_opcodes_in_runestone_are_ignored() {
fn outputs_with_non_pushdata_opcodes_are_skipped() {
assert_eq!(
Runestone::decipher(&Transaction {
input: Vec::new(),
output: vec![TxOut {
script_pubkey: script::Builder::new()
.push_opcode(opcodes::all::OP_RETURN)
.push_opcode(MAGIC_NUMBER)
.push_opcode(opcodes::all::OP_VERIFY)
.push_slice([0])
.push_slice::<&script::PushBytes>(
varint::encode(rune_id(1).into())
.as_slice()
.try_into()
.unwrap()
)
.push_slice([2, 0])
.into_script(),
value: 0,
}],
output: vec![
TxOut {
script_pubkey: script::Builder::new()
.push_opcode(opcodes::all::OP_RETURN)
.push_opcode(MAGIC_NUMBER)
.push_opcode(opcodes::all::OP_VERIFY)
.push_slice([0])
.push_slice::<&script::PushBytes>(
varint::encode(rune_id(1).into())
.as_slice()
.try_into()
.unwrap()
)
.push_slice([2, 0])
.into_script(),
value: 0,
},
TxOut {
script_pubkey: script::Builder::new()
.push_opcode(opcodes::all::OP_RETURN)
.push_opcode(MAGIC_NUMBER)
.push_slice([0])
.push_slice::<&script::PushBytes>(
varint::encode(rune_id(2).into())
.as_slice()
.try_into()
.unwrap()
)
.push_slice([3, 0])
.into_script(),
value: 0,
},
],
lock_time: LockTime::ZERO,
version: 2,
})
.unwrap()
.unwrap(),
Runestone {
edicts: vec![Edict {
id: rune_id(1),
amount: 2,
id: rune_id(2),
amount: 3,
output: 0,
}],
..Default::default()
Expand Down

0 comments on commit 3dcab13

Please sign in to comment.