okx / go-wallet-sdk

Multi-chain golang signature sdk, supports bitcoin, ethereum, aptos, cosmos, etc.
MIT License
363 stars 130 forks source link

BItcoin Brc20 sendrawtransaction Error(mandatory-script-verify-flag-failed (Witness program hash mismatch)) #58

Closed arvin-lau closed 1 month ago

arvin-lau commented 2 months ago
package main
import (
"fmt"
"github.com/btcsuite/btcd/btcutil"
"github.com/btcsuite/btcd/chaincfg"
"github.com/okx/go-wallet-sdk/coins/bitcoin/brc20"
"github.com/rs/zerolog/log"
)
func main() {

builder := brc20.NewTxBuildV1(&chaincfg.MainNetParams)

contentType := "text/plain;charset=utf-8"
body := []byte(fmt.Sprintf(`{"p":"brc-20","op":"%s","tick":"%s","amt":"%s"}`, "transfer", "sats", "1"))

inscription := brc20.NewInscription(contentType, body)

privatekeyStr := "wif key"
privateKeyWif, err := btcutil.DecodeWIF(privatekeyStr)
if err != nil {
    log.Error().Msg("DecodeWIF err: " + err.Error())
    return
}
builder.AddInput("434876ec149a94971a630ebb3a70ffa4869ab1a992f1500b4b035a21cbbe7c80",
    1,
    privateKeyWif.PrivKey.Key.String(),
    "bc1pg3zt4vv6a8hg7y935xjw26rvyc879wcwxj5fxzyd8m2mvwqpleqszlwqm7",
    "81024",
    inscription,
)
builder.AddOutput("bc1pg3zt4vv6a8hg7y935xjw26rvyc879wcwxj5fxzyd8m2mvwqpleqszlwqm7", "546")
builder.AddOutput("bc1pg3zt4vv6a8hg7y935xjw26rvyc879wcwxj5fxzyd8m2mvwqpleqszlwqm7", "77000")
tx, err := builder.Build()
if err != nil {
    log.Error().Msg("Build err: " + err.Error())
    return
}
fmt.Println("tx: ", tx)
}

ERROR:

{
    "result": null,
    "error": {
        "code": -26,
        "message": "mandatory-script-verify-flag-failed (Witness program hash mismatch)"
    },
    "id": "getblock.io"
}

How do I fix this?

evieyee commented 2 months ago

To help you get started quickly, here are some code examples: https://github.com/okx/go-wallet-sdk/blob/main/coins/bitcoin/inscribe_test.go#L10.
You also need to understand the two stages of inscriptions: Commit and Reveal. Here's something you might want to know: Taproot Assets.