Ordinals 開發者指南

Ordinals 協議開發教學

Ordinals 開發者完整指南

概述

Ordinals 協議允許在比特幣區塊鏈上刻錄不可替代的數據,每個 satoshi(比特幣的最小單位)被賦予唯一的序號,成為可以追蹤和交易的數位製品。

核心概念

Satoshi 序號

比特幣的最小單位是 satoshi(1 BTC = 100,000,000 satoshis)。Ordinals 協議為每個 satoshi 分配一個序號:

區塊高度 + 區塊內交易索引 + UTXO 內輸出索引

銘文(Inscription)

銘文是刻錄在 satoshi 上的內容:

{
  "p": "ord",
  "op": "inscribe",
  "s": "image/png",
  "t": "Ordinal Punk #1",
  "i": 0,
  "data": "base64 encoded content..."
}

開發環境設置

1. 運行 Ordinals 節點

# 使用 ord 錢包(比特幣索引器)
git clone https://github.com/ordinals/ord.git
cd ord
cargo build --release

# 運行 ord 服務器
./ord server --cookie-file ~/.bitcoin/.cookie

2. 配置比特幣節點

# bitcoin.conf 配置
server=1
txindex=1
prune=550
blockfilterindex=1

3. Python 開發環境

# 安裝必要的庫
pip install python-bitcoinlib
pip install ecdsa
pip install base58

銘文創建流程

1. 準備內容

import base64
import json

class Inscription:
    def __init__(self, content_type, body):
        self.content_type = content_type
        self.body = body
        self.protocol = "ord"

    def to_json(self):
        return json.dumps({
            "p": self.protocol,
            "op": "inscribe",
            "s": self.content_type,
            "t": self.body.get("name", ""),
            "i": self.body.get("index", 0),
            "data": base64.b64encode(
                self.body.get("content", b"")
            ).decode('utf-8')
        })

2. 創建刻錄交易

import struct

def create_inscription_tx(
    utxo,           # 未花費的交易輸出
    inscription,    # 銘文內容
    fee_rate        # 費用率 (sat/vB)
):
    """
    創建刻錄交易
    """
    tx = CTransaction()

    # 輸入:花費 UTXO
    tx.vin.append(CTxIn(
        prevout=COutPoint(
            int(utxo.txid, 16),
            utxo.vout
        ),
        scriptSig=CScript()
    ))

    # 啟用設施(Envelop)
    # 這是刻錄的關鍵:OP_FALSE OP_IF ... OP_ENDIF
    envelop = CScript([
        OP_FALSE,
        OP_IF,
        b'ord',
        OP_1,  # 協議版本
        struct.pack_varint(len(inscription)),
        inscription,
        OP_ENDIF
    ])

    # 輸出:刻錄內容 + 找零
    # 銘文刻在首個輸出的首個 satoshi 上
    tx.vout.append(CTxOut(
        nValue=1,  # 1 satoshi
        scriptPubKey=envelop
    ))

    tx.vout.append(CTxOut(
        nValue=utxo.amount - 1000 - fee_rate * estimated_vsize,
        scriptPubKey=wallet.get_change_script()
    ))

    return tx

3. 簽名與廣播

def sign_and_broadcast(tx, wallet):
    """
    簽名並廣播交易
    """
    # 獲取私鑰
    private_key = wallet.get_private_key()

    # 簽名
    sighash = SIGHASH_ALL | SIGHASH_ANYONECANPAY
    tx.sign(private_key, sighash)

    # 廣播
    return wallet.broadcast(tx)

索引與查詢

1. Ord HTTP API

# 獲取銘文信息
curl http://localhost:8080/inscription/6fb976ab49dcec017f1c201590eab97be7b3c00e8d00c0f7e10c1b5b87b2c92ai0

# 獲取比特率信息
curl http://localhost:8080/rate

# 獲取銘文內容
curl http://localhost:8080/content/6fb976ab49dcec017f1c201590eab97be7b3c00e8d00c0f7e10c1b5b87b2c92ai0

2. Python 客戶端

import requests

class OrdinalsClient:
    def __init__(self, base_url="http://localhost:8080"):
        self.base_url = base_url

    def get_inscription(self, inscription_id):
        """獲取銘文詳情"""
        response = requests.get(
            f"{self.base_url}/inscription/{inscription_id}"
        )
        return response.json()

    def get_content(self, inscription_id):
        """獲取銘文內容(二進制)"""
        response = requests.get(
            f"{self.base_url}/content/{inscription_id}"
        )
        return response.content

    def get_inscriptions_by_address(self, address):
        """獲取地址的所有銘文"""
        response = requests.get(
            f"{self.base_url}/inscriptions/{address}"
        )
        return response.json()

收藏品標準

1. 文本銘文

{
  "p": "ord",
  "op": "inscribe",
  "s": "text/plain",
  "t": "Hello, Ordinals!",
  "i": 0
}

2. 圖像銘文

{
  "p": "ord",
  "op": "inscribe",
  "s": "image/png",
  "t": "Ordinal Punk #1",
  "i": 0,
  "data": "<base64 encoded image>"
}

3. HTML 銘文

{
  "p": "ord",
  "op": "inscribe",
  "s": "text/html",
  "t": "My Web3 Site",
  "i": 0
}

4. JSON 集合

{
  "p": "ord",
  "op": "inscribe",
  "s": "application/json",
  "t": "My Collection",
  "i": 0,
  "attr": {
    "collection": "My NFT Collection",
    "attributes": [
      {"trait_type": "Background", "value": "Blue"},
      {"trait_type": "Eyes", "value": "Laser"}
    ]
  }
}

錢包集成

1. Sparrow Wallet

# 配置 Sparrow 連接 ord 服務器
# Settings > Ordinals > Enable Ordinals
# Server URL: http://localhost:8080

2. Xverse Wallet

# Xverse 錢包集成
from xverse import XverseWallet

wallet = XverseWallet()

# 創建 ord 兼容地址
address = wallet.get_ordinals_address()

3. Leather Wallet

// Leather 錢包 JavaScript API
const wallet = await Leather.connect();

const inscription = await wallet.inscribe({
  content: imageData,
  contentType: 'image/png',
  recipient: 'bc1p...'
});

批量 mint 優化

1. 父子銘文

父子銘文允許將多個相關銘文組合:

# 父子銘文結構
parent_inscription = {
    "p": "ord",
    "op": "inscribe",
    "s": "application/json",
    "t": "Collection Parent",
    "i": None  # 將由索引器填充
}

child_inscription = {
    "p": "ord",
    "op": "inscribe",
    "s": "image/png",
    "t": "Item #1",
    "i": None,
    "p_i": "parent_inscription_id"  # 引用父銘文
}

2. 遞歸銘文

遞歸銘文引用其他銘文的內容:

{
  "p": "ord",
  "op": "inscribe",
  "s": "text/html",
  "<svg xmlns='http://www.w3.org/2000/svg'>
    <image href='ordinals://6fb976ab...i0' />
  </svg>"
}

3. 費用優化

def estimate_inscription_fee(content_size, fee_rate=10):
    """
    估算刻錄費用

    費用計算:
    - 見證數據:銘文內容 + 啟用設施
    - 基礎交易大小
    """
    base_vsize = 200  # 基礎交易大小
    content_vsize = len(content) // 4  # 內容大小(壓縮估算)

    # 啟用設施開銷
    envelop_overhead = 20

    total_vsize = base_vsize + content_vsize + envelop_overhead

    return total_vsize * fee_rate

常見問題排查

1. 交易卡住

# 加速交易
./ord wallet rbf <txid> --fee-rate 20

# 取消交易
./ord wallet cancel <txid>

2. 銘文未顯示

# 重新索引
./ord server --reindex

3. 余額顯示錯誤

# 重新掃描錢包
./ord wallet rescan

測試網絡

# 使用比特幣測試網絡
./ord server --chain testnet

# 獲取測試網 BTC
# 使用比特幣水龍頭

最佳實踐

1. 費用管理

2. 內容大小

3. 錢包安全

未來改進

1. 比特幣升級

2. 索引器改進

3. 工具生態

-更好的市場平台

總結

Ordinals 為比特幣開啟了 NFT 時代,讓每個 satoshi 都可以承載獨特的數位內容。雖然開發難度比傳統區塊鏈更高,但其去中心化特性和比特幣網路的安全性使其成為獨特的選擇。開發者應熟悉比特幣腳本、Taproot 和 Ordinals 協議規範,才能構建高質量的應用。

風險提示

延伸閱讀與來源

這篇文章對您有幫助嗎?

評論

發表評論

注意:由於這是靜態網站,您的評論將儲存在本地瀏覽器中,不會公開顯示。

目前尚無評論,成為第一個發表評論的人吧!