ordinals-parser 0.1.0

A lightweight parser for Bitcoin Ordinals inscriptions
Documentation
# Ordinals Parser

[![Crates.io](https://img.shields.io/crates/v/ordinals-parser.svg)](https://crates.io/crates/ordinals-parser)
[![Documentation](https://docs.rs/ordinals-parser/badge.svg)](https://docs.rs/ordinals-parser)
[![MIT License](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE)

A lightweight Rust library for parsing Bitcoin Ordinals inscriptions without requiring the full Ord codebase.

## Features

- Parse Ordinals inscriptions from Bitcoin transactions
- Support for both classic and modern inscription formats
- Extract content type, body, and other metadata
- Detect JSON in text/plain content
- Low dependency footprint
- Simple and easy-to-use API

## Installation

Add this to your `Cargo.toml`:

```toml
[dependencies]
ordinals-parser = "0.1.0"
```

## Usage

### Basic Example

```rust
use ordinals_parser::{parse_inscriptions_from_transaction, Inscription};
use bitcoin::consensus::deserialize;
use std::fs::read;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Load a Bitcoin transaction
    let tx_bytes = hex::decode("your_transaction_hex_here")?;
    let transaction: bitcoin::Transaction = deserialize(&tx_bytes)?;
    
    // Parse inscriptions
    let inscriptions = parse_inscriptions_from_transaction(&transaction);
    
    for (idx, inscription) in inscriptions.iter().enumerate() {
        println!("Inscription #{}", idx + 1);
        
        if let Some(content_type) = inscription.content_type() {
            println!("Content Type: {}", content_type);
        }
        
        if let Some(content_length) = inscription.content_length() {
            println!("Content Length: {} bytes", content_length);
        }
        
        if let Some(body) = inscription.body() {
            // Work with inscription content
            if let Ok(text) = std::str::from_utf8(body) {
                println!("Content: {}", text);
            }
        }
    }
    
    Ok(())
}
```

### Working with JSON Inscriptions

The library can detect and parse JSON content in both `application/json` and `text/plain` inscriptions:

```rust
use ordinals_parser::{parse_inscriptions_from_transaction};
use bitcoin::consensus::deserialize;
use serde_json::Value;

fn main() {
    // ... load and parse transaction as above
    
    for inscription in inscriptions {
        if let (Some(content_type), Some(body)) = (inscription.content_type(), inscription.body()) {
            // Check if it's JSON content
            if content_type == "application/json" || 
               (content_type.starts_with("text/plain") && is_json(body)) {
                if let Ok(text) = std::str::from_utf8(body) {
                    if let Ok(json) = serde_json::from_str::<Value>(text) {
                        println!("JSON content: {:?}", json);
                        // Process BRC-20, Ordinals Collections, etc.
                    }
                }
            }
        }
    }
}

fn is_json(data: &[u8]) -> bool {
    if let Ok(text) = std::str::from_utf8(data) {
        let trimmed = text.trim();
        (trimmed.starts_with('{') && trimmed.ends_with('}')) || 
        (trimmed.starts_with('[') && trimmed.ends_with(']'))
    } else {
        false
    }
}
```

## CLI Tool

The package also includes a command-line tool for parsing inscriptions:

```bash
# Install the CLI tool
cargo install ordinals-parser --features cli

# Parse inscriptions in a transaction
ordinals-parser-cli parse-tx <TRANSACTION_ID>

# Parse inscriptions from a file containing transaction hex
ordinals-parser-cli parse-file <FILE_PATH>
```

## License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

## Acknowledgments

This project is a derivative of the [ord](https://github.com/ordinals/ord) project, simplified and adapted for use as a standalone library.