Skip to content

Instantly share code, notes, and snippets.

@afk11
Created November 9, 2019 15:19
Show Gist options
  • Save afk11/e623091081632185092aa21552c6de81 to your computer and use it in GitHub Desktop.
Save afk11/e623091081632185092aa21552c6de81 to your computer and use it in GitHub Desktop.
feature_taproot_test.py
#!/usr/bin/env python3
# Copyright (c) 2019 The Bitcoin Core developers
# Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
# Test taproot softfork.
from test_framework.blocktools import create_coinbase, create_block, create_transaction, add_witness_commitment
from test_framework.messages import CTransaction, CTxIn, CTxOut, COutPoint, CTxInWitness, uint256_from_str
from test_framework.script import CScript, TaprootSignatureHash, taproot_construct, OP_0, OP_1, OP_2, OP_HASH160, OP_CHECKSIG, OP_CHECKSIGVERIFY, OP_CHECKSIGADD, OP_IF, OP_CODESEPARATOR, OP_ELSE, OP_ENDIF, OP_DROP, DEFAULT_TAPSCRIPT_VER, SIGHASH_ALL, SIGHASH_SINGLE, is_op_success, CScriptOp, OP_RETURN, OP_VERIF, OP_RESERVED, OP_1NEGATE, OP_EQUAL, OP_SWAP, OP_CHECKMULTISIG, OP_CHECKMULTISIGVERIFY, OP_NOTIF, OP_2DROP, OP_NOT, OP_2DUP, OP_1SUB, OP_DUP, MAX_SCRIPT_ELEMENT_SIZE, LOCKTIME_THRESHOLD, ANNEX_TAG, hash160, OP_EQUALVERIFY
from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import assert_equal, assert_raises_rpc_error, hex_str_to_bytes
from test_framework.key import ECKey
from test_framework.address import program_to_witness
from binascii import hexlify, unhexlify
from collections import namedtuple
from hashlib import sha256
from io import BytesIO
import random
import struct
def testfxn():
priv1i = unhexlify('4242424242424242424242424242424242424242424242424242424242424242')
priv2i = unhexlify('4242424242424242424242424242424242424242424242424242424242424243')
priv3i = unhexlify('4242424242424242424242424242424242424242424242424242424242424244')
comp = True
priv1 = ECKey()
priv1.set(priv1i, comp)
priv2 = ECKey()
priv2.set(priv2i, comp)
priv3 = ECKey()
priv3.set(priv3i, comp)
pub1 = priv1.get_pubkey()
xonly1 = pub1.get_xonly_bytes()
pub2 = priv2.get_pubkey()
pub3 = priv3.get_pubkey()
print("priv1: %s" % (priv1.get_bytes().hex()) )
print("pub1: %s" % (pub1.get_bytes().hex()) )
print("priv2: %s" % (priv2.get_bytes().hex()) )
print("pub2: %s" % (pub2.get_bytes().hex()) )
print("priv3: %s" % (priv3.get_bytes().hex()) )
print("pub3: %s" % (pub3.get_bytes().hex()) )
outpoint = COutPoint(uint256_from_str(hex_str_to_bytes('0000000000000000000000000000000000000000000000000000000000000000')), 0)
amount = 5000000000
multisig1_2 = CScript([OP_1, pub2.get_bytes(), pub3.get_bytes(), OP_2, OP_CHECKMULTISIG])
multisig2_2 = CScript([OP_2, pub2.get_bytes(), pub3.get_bytes(), OP_2, OP_CHECKMULTISIG])
p2wpkh2 = CScript([OP_0, hash160(pub2.get_bytes())])
p2pkh2 = CScript([OP_DUP, OP_HASH160, hash160(pub2.get_bytes()), OP_EQUALVERIFY, OP_CHECKSIG])
p2pkh3 = CScript([OP_DUP, OP_HASH160, hash160(pub3.get_bytes()), OP_EQUALVERIFY, OP_CHECKSIG])
print("p2pkh3: %s" % (p2pkh3.hex()) )
scripts1 = [
p2wpkh2,
p2pkh3,
p2pkh2,
multisig1_2,
multisig2_2,
]
scripts2 = [
[
[p2wpkh2],
[
[p2pkh3],
[p2pkh2],
]
],
[
[multisig1_2],
[multisig2_2],
],
]
scripts3 = [p2wpkh2]
scripts4 = [
multisig1_2,
multisig2_2,
]
info = taproot_construct(pub1, scripts2)
print("scriptSpk: %s" % info[0].hex())
print("scriptTweak: %s" % info[1].hex())
scriptTaprootKey = CScript([OP_1, xonly1])
scriptTaprootScripts = info[0]
scriptPubKey = scriptTaprootScripts
print("p2pkh2: %s" % (p2pkh2.hex()) )
tx = CTransaction()
vin0 = CTxIn(outpoint=outpoint, scriptSig=b"", nSequence=0xffffffff)
tx.vin = [vin0]
tx.vout = [CTxOut(nValue=90000000, scriptPubKey=p2pkh2), CTxOut(nValue=4900000000, scriptPubKey=p2pkh3)]
tx_wit = tx.serialize_with_witness()
txout = CTxOut(nValue=amount, scriptPubKey=scriptPubKey)
spendUtxos = [txout]
ht = 0x00
sighash = TaprootSignatureHash(tx, spendUtxos, ht, 0, False)
sig = priv1.sign_schnorr(sighash)
verify = priv1.get_pubkey().verify_schnorr(sig, sighash)
print("Test: %s" % tx_wit.hex())
print("sighash: %s" % sighash.hex())
if verify:
print("VALID")
else:
print("INVALID")
class TAPROOTTest(BitcoinTestFramework):
def set_test_params(self):
self.num_nodes = 1
self.setup_clean_chain = True
self.extra_args = [["-whitelist=127.0.0.1", "-acceptnonstdtxn=0", "-par=1"]]
def run_test(self):
testfxn()
if __name__ == '__main__':
testfxn()
#TAPROOTTest().main()
@afk11
Copy link
Author

afk11 commented Nov 9, 2019

How to run this:

Test scripts are located in test/functional

As a python script:

./feature_taproot_test.py

With the test runner

  1. Add script to test_runner.py
  2. Comment Line 117, and uncomment line 118
  3. Run test_runner.py

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment