Bitcoin

taproot – non-required-script-verify-flag (invalid Schnorr signature)

async function inscribeTransfer(tick, amount, network = 'testnet') {
    const seed = bip39.mnemonicToSeedSync(mnemonic);
    const root = bip32.fromSeed(seed);
    const child = root.derivePath(`m/86'/$network == "testnet" ? 1 : 0'/0'/0/0`);
    const pubkey = toXOnly(child.publicKey);
    const (tpubkey) = Tap.getPubKey(pubkey);
    let address = Address.p2tr.encode(tpubkey, network);
    console.log("Address:", address);

    let utxos = (await fetchNonInscriptionUtxo(address, network)).data.utxo;
    let utxo;
    for (let u of utxos) 
        if (u.satoshi > 546 + fee) 
            utxo = u;
            break;
        
    

    const ec = new TextEncoder();
    const content = ec.encode(`"p":"brc-20","op":"transfer","tick":$tick,"amt":"$amount"`);
    const mimetype = ec.encode("text/plain;charset=utf-8");

    const script = (
        pubkey.toString("hex"),
        "OP_CHECKSIG",
        "OP_0",
        "OP_IF",
        toHexString(ec.encode("ord")),
        "01",
        toHexString(mimetype),
        "OP_0",
        toHexString(content),
        "OP_ENDIF",
    ).join(" ");
    const leafScript = bitcoin.script.fromASM(script);
    const scriptTree = 
        output: leafScript,
    ;
    const redeem = 
        output: leafScript,
        redeemVersion: 0xc0 // LEAF_VERSION_TAPSCRIPT,
    ;
    const  output, witness, hash  = bitcoin.payments.p2tr(
        internalPubkey: pubkey,
        scriptTree,
        redeem,
        network: network == "testnet" ? bitcoin.networks.testnet : bitcoin.networks.bitcoin
    );
    const psbt = new bitcoin.Psbt( network: network == "testnet" ? bitcoin.networks.testnet : bitcoin.networks.bitcoin );
    const tapLeafScript = 
        leafVersion: redeem.redeemVersion,
        script: redeem.output,
        controlBlock: witness(witness.length - 1),
    ;
    psbt.addInput(
        hash: utxo.txid,
        index: utxo.vout,
        witnessUtxo: 
            script: output,
            value: utxo.satoshi
        ,
        tapInternalKey: pubkey,
        tapMerkleRoot: hash,
        tapLeafScript: (
            tapLeafScript
        ),
    );
    psbt.addOutput(
        value: 546,
        address: address,
        // tapInternalKey: pubkey,
        tapTree:  leaves: tapTreeToList(scriptTree) ,
    );
    psbt.addOutput(
        value: utxo.satoshi - fee - 546,
        address: address,
    );

    const tweakedSigner = child.tweak(
        bitcoin.crypto.taggedHash(
            'TapTweak',
            Buffer.concat((pubkey, hash)),
        ),
    );
    await psbt.signInputAsync(0, tweakedSigner);

    psbt.finalizeAllInputs();
    const tx = psbt.extractTransaction();
    const rawTx = tx.toBuffer();
    const txHex = rawTx.toString('hex');
    const txHash = await broadcastTx(txHex, network);
    console.log(txHash); // ERROR: sendrawtransaction RPC error: "code":-26,"message":"non-mandatory-script-verify-flag (Invalid Schnorr signature)"

I’ve already done a line-by-line comparison using various examples and made a few adjustments, but Invalid Schnorr signature error. I was able to perform a normal UTXO transaction, but the Taproot script failed. I want to log transmissions of BRC-20 from a single address. Is this possible?

Related Articles

Back to top button