Exemplo n.º 1
0
        def create_fund_and_spend_tx(spend, multi=False, sig='schnorr'):
            if multi:
                script = CScript([OP_1, public_key, OP_1, OP_CHECKMULTISIG])
            else:
                script = CScript([public_key, OP_CHECKSIG])

            # Fund transaction
            txfund = create_transaction(spend.tx, spend.n, b'', 50 * COIN,
                                        script)
            txfund.rehash()

            # Spend transaction
            txspend = CTransaction()
            txspend.vout.append(CTxOut(50 * COIN - 1000, CScript([OP_TRUE])))
            txspend.vin.append(CTxIn(COutPoint(txfund.sha256, 0), b''))

            # Sign the transaction
            sighashtype = SIGHASH_ALL | SIGHASH_FORKID
            hashbyte = bytes([sighashtype & 0xff])
            sighash = SignatureHashForkId(script, txspend, 0, sighashtype,
                                          50 * COIN)
            if sig == 'schnorr':
                txsig = schnorr.sign(privkeybytes, sighash) + hashbyte
            elif sig == 'ecdsa':
                txsig = private_key.sign(sighash) + hashbyte
            elif isinstance(sig, bytes):
                txsig = sig + hashbyte
            if multi:
                txspend.vin[0].scriptSig = CScript([b'', txsig])
            else:
                txspend.vin[0].scriptSig = CScript([txsig])
            txspend.rehash()

            return txfund, txspend
        def create_fund_and_spend_tx(dummy=OP_0, sigtype='ecdsa'):
            spendfrom = spendable_outputs.pop()

            script = CScript([OP_1, public_key, OP_1, OP_CHECKMULTISIG])

            value = spendfrom.vout[0].nValue

            # Fund transaction
            txfund = create_tx_with_script(spendfrom, 0, b'', value, script)
            txfund.rehash()
            fundings.append(txfund)

            # Spend transaction
            txspend = CTransaction()
            txspend.vout.append(CTxOut(value - 1000, CScript([OP_TRUE])))
            txspend.vin.append(CTxIn(COutPoint(txfund.sha256, 0), b''))

            # Sign the transaction
            sighashtype = SIGHASH_ALL | SIGHASH_FORKID
            hashbyte = bytes([sighashtype & 0xff])
            sighash = SignatureHashForkId(script, txspend, 0, sighashtype,
                                          value)
            if sigtype == 'schnorr':
                txsig = schnorr.sign(privkeybytes, sighash) + hashbyte
            elif sigtype == 'ecdsa':
                txsig = private_key.sign_ecdsa(sighash) + hashbyte
            txspend.vin[0].scriptSig = CScript([dummy, txsig])
            txspend.rehash()

            return txspend
Exemplo n.º 3
0
    def basicSchnorrSigning(self):
        # First try a canned sig (taken from schnorr.py)
        privkey = bytes.fromhex(
            "12b004fff7f4b69ef8650e767f18f11ede158148b425660723b9f9a66e61f747")

        pubkey = schnorr.getpubkey(privkey, compressed=True)
        assert pubkey == bytes.fromhex(
            "030b4c866585dd868a9d62348a9cd008d6a312937048fff31670e7e920cfc7a744"
        )

        msg = b"Very deterministic message"
        msghash = hash256(msg)
        assert msghash == bytes.fromhex(
            "5255683da567900bfd3e786ed8836a4e7763c221bf1ac20ece2a5171b9199e8a")

        sig = schnorr.sign(privkey, msghash)
        assert sig == bytes.fromhex(
            "2c56731ac2f7a7e7f11518fc7722a166b02438924ca9d8b4d111347b81d0717571846de67ad3d913a8fdf9d8f3f73161a4c48ae81cb183b214765feb86e255ce"
        )
        sig2 = cashlib.signHashSchnorr(privkey, msghash)
        assert sig2 == sig

        logging.info("random Schnorr signature comparison")
        # Next try random signatures
        for i in range(1, 1000):
            privkey = cashlib.randombytes(32)
            pubkey = schnorr.getpubkey(privkey, compressed=True)
            pubkey2 = cashlib.pubkey(privkey)
            assert pubkey == pubkey2

            msg = cashlib.randombytes(random.randint(0, 10000))
            hsh = cashlib.hash256(msg)

            sigpy = schnorr.sign(privkey, hsh)
            sigcashlib = cashlib.signHashSchnorr(privkey, hsh)
            assert sigpy == sigcashlib
Exemplo n.º 4
0
def create_fund_and_spend_tx(node, spend, multi=False):

    privkeybytes = b"Schnorr!" * 4
    private_key = CECKey()
    private_key.set_secretbytes(privkeybytes)
    # get uncompressed public key serialization
    public_key = private_key.get_pubkey()

    if multi:
        script = CScript([OP_1, public_key, OP_1, OP_CHECKMULTISIG])
    else:
        script = CScript([public_key, OP_CHECKSIG])

    # Fund transaction
    prevtx = FromHex(CTransaction(), node.getrawtransaction(spend['txid']))
    prevtx.rehash()
    fee = 500
    fund_amount = 50 * COIN - fee
    txfund = create_transaction(prevtx, spend['vout'], b'', fund_amount,
                                script)
    txfund = FromHex(CTransaction(),
                     node.signrawtransactionwithwallet(ToHex(txfund))["hex"])
    txfund.rehash()

    # Spend transaction
    txspend = CTransaction()
    txspend.vout.append(CTxOut(fund_amount - 1000, CScript([OP_TRUE])))
    txspend.vin.append(CTxIn(COutPoint(txfund.sha256, 0), b''))

    # Sign the transaction
    sighashtype = SIGHASH_ALL | SIGHASH_FORKID
    hashbyte = bytes([sighashtype & 0xff])
    sighash = SignatureHashForkId(script, txspend, 0, sighashtype, fund_amount)
    txsig = schnorr.sign(privkeybytes, sighash) + hashbyte
    if multi:
        txspend.vin[0].scriptSig = CScript([b'', txsig])
    else:
        txspend.vin[0].scriptSig = CScript([txsig])
    txspend.rehash()

    return txfund, txspend
Exemplo n.º 5
0
        def create_fund_and_spend_tx(scriptsigextra, redeemextra) -> Tuple[CTransaction, CTransaction]:
            spendfrom = spendable_txns.pop()

            redeem_script = CScript(redeemextra + [OP_1, public_key, OP_1, OP_CHECKMULTISIG])
            script_pubkey = CScript([OP_HASH160, hash160(redeem_script), OP_EQUAL])

            value = spendfrom.vout[0].nValue
            value1 = value - 500

            # Fund transaction
            txfund = create_tx_with_script(spendfrom, 0, b'', value1, script_pubkey)
            txfund.rehash()

            p2sh = script_to_p2sh(redeem_script)
            self.log.info(f"scriptPubKey {script_pubkey!r}")
            self.log.info(f"redeemScript {redeem_script!r} -> p2sh address {p2sh}")

            # Spend transaction
            value2 = value1 - 500
            txspend = CTransaction()
            txspend.vout.append(
                CTxOut(value2, CScript([OP_TRUE])))
            txspend.vin.append(
                CTxIn(COutPoint(txfund.sha256, 0), b''))

            # Sign the transaction
            sighashtype = SIGHASH_ALL | SIGHASH_FORKID
            hashbyte = bytes([sighashtype & 0xff])
            sighash = SignatureHashForkId(
                redeem_script, txspend, 0, sighashtype, value1)
            txsig = schnorr.sign(privkeybytes, sighash) + hashbyte
            dummy = OP_1  # Required for 1-of-1 schnorr sig
            txspend.vin[0].scriptSig = ss = CScript([dummy, txsig] + scriptsigextra + [redeem_script])
            self.log.info(f"scriptSig: {ss!r}")
            txspend.rehash()

            return txfund, txspend
def create_fund_and_spend_tx(node, spendfrom, dummy):

    privkeybytes = b"Schnorr!" * 4
    private_key = CECKey()
    private_key.set_secretbytes(privkeybytes)
    # get uncompressed public key serialization
    public_key = private_key.get_pubkey()

    script = CScript([OP_1, public_key, OP_1, OP_CHECKMULTISIG])

    value = spendfrom.vout[0].nValue
    value -= 1000

    # Fund transaction
    txfund = create_transaction(spendfrom, 0, b'', value, script)
    txfund = FromHex(CTransaction(), node.signrawtransactionwithwallet(ToHex(txfund))["hex"])
    txfund.rehash()
    #fundings.append(txfund)

    # Spend transaction
    txspend = CTransaction()
    txspend.vout.append(
        CTxOut(value-1000, CScript([OP_TRUE])))
    txspend.vin.append(
        CTxIn(COutPoint(txfund.sha256, 0), b''))

    # Sign the transaction
    sighashtype = SIGHASH_ALL | SIGHASH_FORKID
    hashbyte = bytes([sighashtype & 0xff])
    sighash = SignatureHashForkId(
        script, txspend, 0, sighashtype, value)
    txsig = schnorr.sign(privkeybytes, sighash) + hashbyte
    txspend.vin[0].scriptSig = CScript([dummy, txsig])
    txspend.rehash()

    return txfund, txspend
Exemplo n.º 7
0
 def send_avaresponse(self, round, votes, privkey):
     response = AvalancheResponse(round, 0, votes)
     sig = schnorr.sign(privkey, response.get_hash())
     msg = msg_tcpavaresponse()
     msg.response = TCPAvalancheResponse(response, sig)
     self.send_message(msg)