segregated witness – I get an Invalid Schnorr signature when making an attempt to broadcast a taproot transaction

0
88


I’ve 0.00036134 testnet bitcoin on the segwit v1 handle: tb1p3au39skpdd8suuhunr4ymp6ca6akv9keq20v5twuagpmyqntqaasl22f4w

I wrote this script to realize 2 issues: archive a hash in a transaction and ship the cash in tb1p3au39skpdd8suuhunr4ymp6ca6akv9keq20v5twuagpmyqntqaasl22f4w to a brand new handle.

import random
from io import BytesIO

from util import CTxInWitness, CTxOut, create_spending_transaction_nonode
from helpers.handle import program_to_witness_testnet
from helpers.key import ECKey, generate_key_pair, generate_bip340_key_pair, generate_schnorr_nonce, int_or_bytes
from helpers.segwit_addr2 import Encoding, decode, encode, segwit_scriptpubkey, bech32_decode
from helpers.script import CScript, CTransaction, OP_RETURN, SIGHASH_ALL, SIGHASH_ALL_TAPROOT, TaprootSignatureHash, tagged_hash


# Sending pockets creation
sendPrivKeyParsed1 = ECKey().set(int('SENDING PRIVATE KEY IN INTEGER'))
sendPubKeyObj = sendPrivKeyParsed1.get_pubkey()
recPubKeyBytes = sendPubKeyObj.get_bytes()

if sendPubKeyObj.get_y()%2 != 0:
    sendPrivKeyParsed1.negate()
    sendPubKeyObj.negate()

# Receiving pockets creation
recPrivKeyParsed1 = ECKey().set(int('RECEIVING ADDRESS PRIVATE KEY IN INTEGER'))
recPubKeyObj = recPrivKeyParsed1.get_pubkey()
recPubKeyBytes = recPubKeyObj.get_bytes()

if recPubKeyObj.get_y()%2 != 0:
    recPrivKeyParsed1.negate()
    recPubKeyObj.negate()

# Instance tweak
taptweak = bytes.fromhex('3e75c57bb5493f932d418f8ff604f96a5d0565a056c0f0f6a9c1c07f25e2a403')

# Tweak the personal key
# Technique: ECKey.add()
tweaked_privkey = recPrivKeyParsed1.add(taptweak)

# Tweak the general public key
# Technique: use tweak_add()
taproot_pubkey = recPubKeyObj.tweak_add(taptweak)
taproot_pubkey_b = taproot_pubkey.get_bytes()

# Derive the bech32 handle
# Use program_to_witness(version_int, pubkey_bytes)
handle = program_to_witness_testnet(0x01, taproot_pubkey_b)

hrp = "bc"
witver, witprog = decode(hrp, handle)
if witver is None:
    hrp = "tb"
    witver, witprog = decode(hrp, handle)
scriptpubkey = segwit_scriptpubkey(witver, witprog)
print("the scriptPublicKey is: {}".format(scriptpubkey.hex()))

# Create a spending transaction
spending_tx = create_spending_transaction_nonode(txid='3f75f413db8ba76d7e26a9e93b9fe94990d58922cd0aaba09eac4b46b74437c2', scriptpubkey=scriptpubkey, amount_c=0.00032134)
print("Spending transaction:n{}".format(spending_tx))

# Signal transaction with tweaked personal key
# Technique: TaprootSignatureHash(tx, output_list, hash_type=int, input_index=int, scriptpath=bool)
txout1 = CTxOut(nValue=36134, scriptPubKey=scriptpubkey)
sighash = TaprootSignatureHash(spending_tx, [txout1], SIGHASH_ALL, 0, False)
sig = sendPrivKeyParsed1.sign_schnorr(sighash)

# Add witness to transaction
spending_tx.wit.vtxinwit.append(CTxInWitness([sig]))
print(spending_tx.serialize().hex())

print("Success!")

I acquired this when making an attempt to broadcast the transaction:

sendrawtransaction RPC error: {"code":-26,"message":"non-mandatory-script-verify-flag (Invalid Schnorr signature)"}  

So why will not the community settle for this transaction and the way do I modify this transaction so it’s accepted?

LEAVE A REPLY

Please enter your comment!
Please enter your name here