Skip to content

Commit

Permalink
Merge pull request #10 from bitfinity-network/improve-inscription-par…
Browse files Browse the repository at this point in the history
…sing

Fix: Implement `Inscription::content_type` for `Nft`
  • Loading branch information
kobby-pentangeli authored Mar 15, 2024
2 parents d72d482 + 4d348fe commit 9fad379
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 29 deletions.
63 changes: 45 additions & 18 deletions src/inscription.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,64 @@ pub mod nft;

use bitcoin::script::PushBytesBuf;
use serde::de::DeserializeOwned;
use serde::Serialize;

use crate::OrdResult;
use crate::{OrdError, OrdResult};

/// The inscription trait is used to write data to the redeem script of a commit and reveal transaction.
/// The `Inscription` trait defines the behavior necessary for handling
/// Bitcoin Ordinal inscriptions within the context of commit and reveal transactions.
///
/// These are methods for encoding, decoding, and managing
/// the inscriptions, tailored to specific types (e.g. `Brc20`, `Nft`).
pub trait Inscription: DeserializeOwned {
/// Returns the content type of the inscription.
fn content_type(&self) -> String;
/// Encodes the inscription object into a JSON string.
///
/// # Errors
///
/// May return an `OrdError` if serialization fails.
fn encode(&self) -> OrdResult<String>
where
Self: Serialize,
{
serde_json::to_string(self).map_err(OrdError::from)
}

/// Returns the inscription as to be pushed to the redeem script.
/// Returns the MIME content type of the inscription.
///
/// This data follows the header of the inscription:
/// It should provide the MIME type string that best represents the
/// data format of the inscription (e.g., "text/plain;charset=utf-8", "application/json").
fn content_type(&self) -> String;

/// Returns the body of the inscription as to be pushed to the redeem script.
///
/// - public key
/// - OP_CHECKSIG
/// - OP_FALSE
/// - OP_IF
/// - ord
/// - 0x01
/// - {inscription.content_type()}
/// - 0x00
/// This body must follow the header of the inscription as presented below:
///
/// then it comes your data
/// Header:
/// - Public key
/// - OP_CHECKSIG
/// - OP_FALSE
/// - OP_IF
/// - "ord" (the opcode or marker indicating an ordinal inscription)
/// - 0x01 (version byte)
/// - {self.content_type()}
/// - 0x00 (separator byte)
///
/// and then the footer:
/// Next is the inscription data/body (payload)
///
/// - OP_ENDIF
/// Then comes the footer:
/// - OP_ENDIF
///
/// So for example in case of a BRC20, this function must return the JSON encoded BRC20 operation as `PushBytes`.
fn data(&self) -> OrdResult<PushBytesBuf>;

/// Returns the inscription data from the serialized inscription bytes in the witness script.
/// Parses inscription data from the serialized bytes found in the witness script.
///
/// Decodes the inscription data embedded within the witness script of
/// a Bitcoin transaction, reconstructing the original inscription object.
///
/// # Errors
///
/// May return an `OrdError` if parsing fails.
fn parse(data: &[u8]) -> OrdResult<Self>
where
Self: Sized;
Expand Down
5 changes: 0 additions & 5 deletions src/inscription/brc20.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,6 @@ impl Brc20 {
amt,
})
}

/// Encode the BRC-20 operation as a JSON string
pub fn encode(&self) -> OrdResult<String> {
serde_json::to_string(self).map_err(OrdError::from)
}
}

impl FromStr for Brc20 {
Expand Down
10 changes: 4 additions & 6 deletions src/inscription/nft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,10 @@ pub struct Nft {

impl Inscription for Nft {
fn content_type(&self) -> String {
unimplemented!()
match self.content_type() {
Some(t) => t.to_string(),
None => "".to_string(),
}
}

fn data(&self) -> OrdResult<PushBytesBuf> {
Expand Down Expand Up @@ -165,11 +168,6 @@ impl Nft {
builder.push_opcode(opcodes::all::OP_ENDIF)
}

/// Encodes `Self` as a JSON string.
pub fn encode(&self) -> OrdResult<String> {
Ok(serde_json::to_string(self)?)
}

/// Creates a new `Nft` from JSON-encoded string.
pub fn from_json_str(data: &str) -> OrdResult<Self> {
Self::from_str(data)?.validate_content_type()
Expand Down

0 comments on commit 9fad379

Please sign in to comment.