-
Notifications
You must be signed in to change notification settings - Fork 2
Experimental Inscription Support #3
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
base: master
Are you sure you want to change the base?
Changes from 1 commit
4d96350
caa7f0a
eca8c68
c607588
403390a
3f23950
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,14 +17,23 @@ | |
//! Translates a script into a reversed sequence of tokens | ||
//! | ||
|
||
use bitcoin::blockdata::{opcodes, script}; | ||
use bitcoin::{blockdata::{ | ||
opcodes::{ | ||
self, | ||
all::{OP_ENDIF, OP_IF}, | ||
OP_FALSE, | ||
}, | ||
script::{self, Instruction}, | ||
}, Script}; | ||
|
||
use std::fmt; | ||
use std::{fmt, sync::Arc}; | ||
|
||
use crate::ord::{PROTOCOL_ID}; | ||
|
||
use super::Error; | ||
|
||
/// Atom of a tokenized version of a script | ||
#[derive(Debug, Copy, Clone, PartialEq, Eq)] | ||
#[derive(Debug, Clone, PartialEq, Eq)] | ||
#[allow(missing_docs)] | ||
pub enum Token<'s> { | ||
BoolAnd, | ||
|
@@ -60,18 +69,23 @@ pub enum Token<'s> { | |
Bytes32(&'s [u8]), | ||
Bytes33(&'s [u8]), | ||
Bytes65(&'s [u8]), | ||
Inscription(Arc<Script>), | ||
} | ||
|
||
impl<'s> fmt::Display for Token<'s> { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
match *self { | ||
match self.clone() { | ||
Token::Num(n) => write!(f, "#{}", n), | ||
Token::Hash20(b) | Token::Bytes33(b) | Token::Bytes32(b) | Token::Bytes65(b) => { | ||
for ch in &b[..] { | ||
write!(f, "{:02x}", *ch)?; | ||
} | ||
Ok(()) | ||
} | ||
Token::Inscription(ref ins) => { | ||
write!(f, "Inscribe({})", ins.asm())?; | ||
Ok(()) | ||
} | ||
x => write!(f, "{:?}", x), | ||
} | ||
} | ||
|
@@ -117,7 +131,8 @@ impl<'s> Iterator for TokenIter<'s> { | |
pub fn lex<'s>(script: &'s script::Script) -> Result<Vec<Token<'s>>, Error> { | ||
let mut ret = Vec::with_capacity(script.len()); | ||
|
||
for ins in script.instructions_minimal() { | ||
let mut it = script.instructions_minimal(); | ||
while let Some(ins) = it.next() { | ||
match ins.map_err(Error::Script)? { | ||
script::Instruction::Op(opcodes::all::OP_BOOLAND) => { | ||
ret.push(Token::BoolAnd); | ||
|
@@ -182,7 +197,50 @@ pub fn lex<'s>(script: &'s script::Script) -> Result<Vec<Token<'s>>, Error> { | |
ret.push(Token::Add); | ||
} | ||
script::Instruction::Op(opcodes::all::OP_IF) => { | ||
ret.push(Token::If); | ||
if ret.last() == Some(&Token::Num(0)) { | ||
// Inscription Detected | ||
ret.pop(); | ||
if let Some(Ok(Instruction::PushBytes(b"ord"))) = it.next() { | ||
|
||
|
||
} else { | ||
return Err(Error::InscriptionError("Unknown Protocol Version".into())); | ||
} | ||
let mut scan = script::Builder::new() | ||
.push_opcode(OP_FALSE) | ||
.push_opcode(OP_IF) | ||
.push_slice(&PROTOCOL_ID); | ||
'scan_inscription: while let Some(ins) = it.next() { | ||
let instr = ins.map_err(Error::Script)?; | ||
|
||
match instr { | ||
script::Instruction::Op(OP_ENDIF) => { | ||
scan = scan.push_opcode(OP_ENDIF); | ||
break 'scan_inscription; | ||
} | ||
script::Instruction::PushBytes(p) => { | ||
scan = scan.push_slice(p); | ||
} | ||
script::Instruction::Op(a) | ||
if matches!( | ||
a.classify( | ||
opcodes::ClassifyContext::TapScript, /*Either Works */ | ||
), | ||
opcodes::Class::PushNum(_) | ||
) => | ||
{ | ||
scan = scan.push_opcode(a); | ||
} | ||
_ => { | ||
return Err(Error::InscriptionError( | ||
"Inscription must be Push Only".into(), | ||
)) | ||
} | ||
} | ||
} | ||
ret.push(Token::Inscription(Arc::new(scan.into_script()))) | ||
} else { | ||
ret.push(Token::If); | ||
} | ||
} | ||
script::Instruction::Op(opcodes::all::OP_IFDUP) => { | ||
ret.push(Token::IfDup); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be useful to you if we exported the
inscriptions
module inord
and made more types public so you can use it as a crate?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, I think it's a good idea. but also not really for sapio unfortunately since we have like the entire rust-bitcoin ecosystem forked with patches.
this patch set though should be upstreamable!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would love that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have a look if this is useful: ordinals/ord#3042