cmdruid / tapscript

A humble library for working with Tapscript and Bitcoin Transactions.
https://www.npmjs.com/package/@cmdcode/tapscript
Creative Commons Zero v1.0 Universal
188 stars 49 forks source link

Witness program hash mismatch #26

Closed HuangCaiJin closed 7 months ago

HuangCaiJin commented 9 months ago

test/example/taproot/inscribe.test.ts ` import { Buff } from '@cmdcode/buff' import { taproot } from '../../../src/lib/sig/index.js' import { tap_pubkey } from '../../../src/lib/tap/index.js' import { assert } from '../../../src/index.js'

import { get_seckey, get_pubkey } from '@cmdcode/crypto-tools/keys'

import { parse_addr, P2TR } from '../../../src/lib/addr/index.js'

import { encode_tx, parse_tx } from '../../../src/lib/tx/index.js'

import { Buffer } from 'buffer/'

const VERBOSE = false

function inscription() {

const ret_addr = "tb1ptv0luvng53jlddlae7y2898pgs9cqfxsaw0qsrcz4k0kvjsdtnsqvkvyzg" const encoder = new TextEncoder() try { const text = {"p":"brc-20","op":"deploy","tick":"hffp","max":"210000","lim":"1"} const imgdata = Buffer.from(encoder.encode(text)) const marker = Buff.encode('ord') const mimetype = Buffer.from(encoder.encode('text/plain;charset=utf-8')) const secret = 'your secret' const seckey = get_seckey(secret) const pubkey = get_pubkey(seckey, true) const script = [pubkey, 'OP_CHECKSIG', 'OP_0', 'OP_IF', marker, '01', mimetype, 'OP_0', imgdata, 'OP_ENDIF'] const { tapkey, cblock } = tap_pubkey(pubkey, { script }) const address = P2TR.create(tapkey, 'testnet') const txinput = { "txid": "201d025a29b20985a329947910c293ce9640c639c834c416dcf6440d3ad4697d", "vout": 0, "satoshi": 4000000, "scriptType": "5120", "scriptPk": "51206602f211fb1a7a56c3c45422fdc5509876af35c095c4973029ae57ea7974edbc", "codeType": 9, "address": "tb1pvcp0yy0mrfa9ds7y2s30m32snpm27dwqjhzfwvpf4et757t5ak7qq66r4h", "height": 2543743, "idx": 67, "isOpInRBF": false, "inscriptions": [] }

const txdata = parse_tx({
  vin: [{
    // Use the txid of the funding transaction used to send the sats.
    txid: txinput.txid,
    // Specify the index value of the output that you are going to spend from.
    vout: txinput.vout,
    // Also include the value and script of that ouput.
    prevout: {
      // Feel free to change this if you sent a different amount.
      value: txinput.satoshi,
      // This is what our address looks like in script form.
      scriptPubKey: ["OP_1", tapkey],
    },
  }],
  vout: [{
    value: 5000,
    scriptPubKey: parse_addr(ret_addr).asm
  }]
})

const sig = taproot.sign_tx(seckey, txdata, { script, txindex: 0 })

txdata.vin[0].witness = [sig, script, cblock]

const is_valid = taproot.verify_tx(txdata, { txindex: 0, pubkey, throws: true })
assert.ok(is_valid, 'Transaction failed validation.')
const txhex = encode_tx(txdata)
console.log(txhex.toString())

} catch (err) { } }

inscription() `

020000000001017d69d43a0d44f6dc16c434c839c64096ce93c210799429a38509b2295a021d200000000000fdffffff0188130000000000002251205b1ffe3268a465f6b7fdcf88a394e1440b8024d0eb9e080f02ad9f664a0d5ce00340477a3553bc207d380298787fa597e40cd67fa92b5304f1e3826477c9e7792259e75cf72132c0c834d72717aa60ea019146b241ba77a4d08cfb24a24fc8f55cf18820fcf61b477e3ba7ed2fe14521da13960b61332f660204f6089bae65c6ba145685ac0063036f72645118746578742f706c61696e3b636861727365743d7574662d3800437b2270223a226272632d3230222c226f70223a226465706c6f79222c227469636b223a2268666670222c226d6178223a22323130303030222c226c696d223a2231227d6821c0fcf61b477e3ba7ed2fe14521da13960b61332f660204f6089bae65c6ba14568500000000

sendrawtransaction RPC error: {"code":-26,"message":"non-mandatory-script-verify-flag (Witness program hash mismatch)"}

LightRider5 commented 8 months ago

I had the same issue and was not able to fix it: But also see here and here and here

As it seems like many people have this issue.

cmdruid commented 8 months ago

What version of the library are you using? Is this the development version?