You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于bitcoinjs-lib实现DRC-20铭文写入DOGE失败问题排查

DRC-20铭文刻录失败问题排查

我尝试用bitcoinjs-lib在DOGE链上刻录DRC-20格式的JSON铭文,按照序数理论手册规范构造了铭文脚本并生成PSBT,但最终铭文无法被索引器识别、钱包扩展看不到,交易输出的tx_hex里也没有相关内容,请求排查代码问题。

DRC-20铭文格式

{
  "p": "drc-20",
  "op": "deploy",
  "tick": "woof",
  "max": "21000000",
  "lim": "1000"
}

序数理论手册铭文脚本示例

OP_FALSE
OP_IF
  OP_PUSH "ord"
  OP_PUSH 1
  OP_PUSH "text/plain;charset=utf-8"
  OP_PUSH 0
  OP_PUSH "Hello, world!"
OP_ENDIF

我的bitcoinjs-lib代码

/**
 * Creates a PSBT with the given inputs and outputs, and adds an OP_RETURN output with the given text based inscription.
 * @param psbt The PSBT add inscription to.
 * @param inscription The inscription to add to the transaction.
 * @returns The PSBT in bitcoin.Psbt format, with an inscription added.
 */
function createPsbtWithInscriptionRaw(psbt: bitcoin.Psbt, receive_address: string, inscription: string): bitcoin.Psbt {
    // Ensure the inscription is within the allowed byte size
    const data = Buffer.from(inscription, 'utf8');
    if (data.length > 80) {
        throw new Error('Inscription is too long. Must be under 80 bytes.');
    }

    // make the script output
    var script = bitcoin.script.compile([
        bitcoin.opcodes.OP_FALSE,
        bitcoin.opcodes.OP_IF,
        bitcoin.opcodes.OP_PUSH,
        Buffer.from('ordQ', 'utf8'), // doge labs uses this to identify the inscription
        bitcoin.opcodes.OP_PUSH,
        Buffer.from('1', 'utf8'),
        bitcoin.opcodes.OP_PUSH,
        Buffer.from('text/plain;charset=utf-8', 'utf8'),
        bitcoin.opcodes.OP_PUSH,
        Buffer.from('0', 'utf8'),
        bitcoin.opcodes.OP_PUSH,
        Buffer.from(inscription, 'utf8'),  // our actual text inscription
        bitcoin.opcodes.OP_ENDIF,
    ]);


    psbt.addOutput({
        address: receive_address,
        script: script,
        value: 100_000, // minimum value on output, 0.001 DOGE
    });

    // Return the base64 encoded PSBT
    return psbt;
}

问题排查与修正方案

1. OP_PUSH使用错误

bitcoinjs-lib中不存在bitcoin.opcodes.OP_PUSH这个有效操作码,正确做法是直接传入Buffer,bitcoin.script.compile会自动根据Buffer长度匹配对应的PUSH操作码(如OP_0、OP_PUSHDATA1等)。手动添加OP_PUSH会导致脚本无效。

2. 铭文标识错误

DOGE链的DRC-20铭文标识应为ord(与比特币序数一致),而非你代码中的ordQ,这个错误会直接导致索引器无法识别铭文。

3. 输出参数冲突

你同时指定了address和script参数,bitcoinjs-lib会优先使用address对应的脚本,完全覆盖你自定义的铭文脚本——这就是tx_hex中看不到铭文内容的核心原因。必须移除address参数,仅保留script和value。

4. 内容类型错误

DRC-20是JSON格式,Content-Type需设为application/json,而非text/plain;charset=utf-8,否则索引器无法正确解析为DRC-20铭文。

修正后的代码

/**
 * 创建包含DRC-20铭文的PSBT
 * @param psbt 待添加铭文的PSBT
 * @param inscription DRC-20铭文JSON字符串
 * @returns 添加铭文后的PSBT
 */
function createPsbtWithInscriptionRaw(psbt: bitcoin.Psbt, inscription: string): bitcoin.Psbt {
    const data = Buffer.from(inscription, 'utf8');
    if (data.length > 80) {
        throw new Error('铭文过长,必须小于80字节');
    }

    // 正确构造铭文脚本
    const script = bitcoin.script.compile([
        bitcoin.opcodes.OP_FALSE,
        bitcoin.opcodes.OP_IF,
        Buffer.from('ord', 'utf8'), // DOGE链标准铭文标识
        Buffer.from('1', 'utf8'), // 协议版本
        Buffer.from('application/json', 'utf8'), // DRC-20对应内容类型
        Buffer.from('0', 'utf8'), // 偏移量
        data, // 铭文内容
        bitcoin.opcodes.OP_ENDIF,
    ]);

    // 仅指定脚本和金额,避免地址覆盖脚本
    psbt.addOutput({
        script: script,
        value: 100_000, // DOGE最小输出金额0.001 DOGE
    });

    return psbt;
}

验证步骤

  1. 生成PSBT后导出tx_hex,检查输出脚本是否包含铭文内容
  2. 使用Wonky Ord工具验证铭文格式有效性
  3. 交易广播后等待区块确认,再用DRC-20索引器查询

内容的提问来源于stack exchange,提问作者user23343243

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.30 14:18:31