示例#1
0
 def serialize(self):
     r = b"".join(
         (struct.pack("<i",
                      self.nVersion), struct.pack("<Q", self.nServices),
          struct.pack("<q", self.nTime), self.addrTo.serialize(),
          self.addrFrom.serialize(), struct.pack("<Q", self.nNonce),
          ser_string(self.strSubVer), struct.pack("<i",
                                                  self.nStartingHeight),
          struct.pack("<b",
                      self.nRelay), struct.pack("<Q", self.nExtraEntropy)))
     return r
def create_invalid_coinbase(height, outputValue=50):
    coinbase = CTransaction()
    coinbase.vin.append(CTxIn(COutPoint(0, 0xffffffff),
                              ser_string(serialize_script_num(height)), 0xffffffff))
    coinbaseoutput = CTxOut()
    coinbaseoutput.nValue = outputValue * COIN
    halvings = int(height / 150)  # regtest
    coinbaseoutput.nValue >>= halvings
    coinbaseoutput.scriptPubKey = CScript([OP_FALSE])
    coinbase.vout = [coinbaseoutput]
    coinbase.calc_sha256()
    return coinbase
示例#3
0
def SignatureHashForkId(script, txTo, inIdx, hashtype, amount):

    hashPrevouts = 0
    hashSequence = 0
    hashOutputs = 0

    if not (hashtype & SIGHASH_ANYONECANPAY):
        serialize_prevouts = bytes()
        for i in txTo.vin:
            serialize_prevouts += i.prevout.serialize()
        hashPrevouts = uint256_from_str(hash256(serialize_prevouts))

    if (not (hashtype & SIGHASH_ANYONECANPAY)
            and (hashtype & 0x1f) != SIGHASH_SINGLE
            and (hashtype & 0x1f) != SIGHASH_NONE):
        serialize_sequence = bytes()
        for i in txTo.vin:
            serialize_sequence += struct.pack("<I", i.nSequence)
        hashSequence = uint256_from_str(hash256(serialize_sequence))

    if ((hashtype & 0x1f) != SIGHASH_SINGLE
            and (hashtype & 0x1f) != SIGHASH_NONE):
        serialize_outputs = bytes()
        for o in txTo.vout:
            serialize_outputs += o.serialize()
        hashOutputs = uint256_from_str(hash256(serialize_outputs))
    elif ((hashtype & 0x1f) == SIGHASH_SINGLE and inIdx < len(txTo.vout)):
        serialize_outputs = txTo.vout[inIdx].serialize()
        hashOutputs = uint256_from_str(hash256(serialize_outputs))

    ss = bytes()
    ss += struct.pack("<i", txTo.nVersion)
    ss += ser_uint256(hashPrevouts)
    ss += ser_uint256(hashSequence)
    ss += txTo.vin[inIdx].prevout.serialize()
    ss += ser_string(script)
    ss += struct.pack("<q", amount)
    ss += struct.pack("<I", txTo.vin[inIdx].nSequence)
    ss += ser_uint256(hashOutputs)
    ss += struct.pack("<i", txTo.nLockTime)
    ss += struct.pack("<I", hashtype)

    return hash256(ss)
示例#4
0
def SignatureHash(script, txTo, inIdx, hashtype, amount, consensusBranchId):
    """Consensus-correct SignatureHash"""
    if inIdx >= len(txTo.vin):
        raise ValueError("inIdx %d out of range (%d)" % (inIdx, len(txTo.vin)))

    if consensusBranchId != 0:
        # ZIP 243
        hashPrevouts = b'\x00' * 32
        hashSequence = b'\x00' * 32
        hashOutputs = b'\x00' * 32
        hashJoinSplits = b'\x00' * 32
        hashShieldedSpends = b'\x00' * 32
        hashShieldedOutputs = b'\x00' * 32

        if not (hashtype & SIGHASH_ANYONECANPAY):
            hashPrevouts = getHashPrevouts(txTo)

        if (not (hashtype & SIGHASH_ANYONECANPAY)) and \
            (hashtype & 0x1f) != SIGHASH_SINGLE and \
            (hashtype & 0x1f) != SIGHASH_NONE:
            hashSequence = getHashSequence(txTo)

        if (hashtype & 0x1f) != SIGHASH_SINGLE and \
            (hashtype & 0x1f) != SIGHASH_NONE:
            hashOutputs = getHashOutputs(txTo)
        elif (hashtype & 0x1f) == SIGHASH_SINGLE and \
            0 <= inIdx and inIdx < len(txTo.vout):
            digest = blake2b(digest_size=32, person=b'ZcashOutputsHash')
            digest.update(txTo.vout[inIdx].serialize())
            hashOutputs = digest.digest()

        if len(txTo.vJoinSplit) > 0:
            hashJoinSplits = getHashJoinSplits(txTo)

        if len(txTo.shieldedSpends) > 0:
            hashShieldedSpends = getHashShieldedSpends(txTo)

        if len(txTo.shieldedOutputs) > 0:
            hashShieldedOutputs = getHashShieldedOutputs(txTo)

        digest = blake2b(
            digest_size=32,
            person=b'ZcashSigHash' + struct.pack('<I', consensusBranchId),
        )

        digest.update(
            struct.pack('<I', (int(txTo.fOverwintered) << 31) | txTo.nVersion))
        digest.update(struct.pack('<I', txTo.nVersionGroupId))
        digest.update(hashPrevouts)
        digest.update(hashSequence)
        digest.update(hashOutputs)
        digest.update(hashJoinSplits)
        digest.update(hashShieldedSpends)
        digest.update(hashShieldedOutputs)
        digest.update(struct.pack('<I', txTo.nLockTime))
        digest.update(struct.pack('<I', txTo.nExpiryHeight))
        digest.update(struct.pack('<Q', txTo.valueBalance))
        digest.update(struct.pack('<I', hashtype))

        if inIdx is not None:
            digest.update(txTo.vin[inIdx].prevout.serialize())
            digest.update(ser_string(script))
            digest.update(struct.pack('<Q', amount))
            digest.update(struct.pack('<I', txTo.vin[inIdx].nSequence))

        return (digest.digest(), None)
    else:
        # Pre-Overwinter
        txtmp = CTransaction(txTo)

        for txin in txtmp.vin:
            txin.scriptSig = b''
        txtmp.vin[inIdx].scriptSig = script

        if (hashtype & 0x1f) == SIGHASH_NONE:
            txtmp.vout = []

            for i in range(len(txtmp.vin)):
                if i != inIdx:
                    txtmp.vin[i].nSequence = 0

        elif (hashtype & 0x1f) == SIGHASH_SINGLE:
            outIdx = inIdx
            if outIdx >= len(txtmp.vout):
                raise ValueError("outIdx %d out of range (%d)" %
                                 (outIdx, len(txtmp.vout)))

            tmp = txtmp.vout[outIdx]
            txtmp.vout = []
            for i in range(outIdx):
                txtmp.vout.append(CTxOut())
            txtmp.vout.append(tmp)

            for i in range(len(txtmp.vin)):
                if i != inIdx:
                    txtmp.vin[i].nSequence = 0

        if hashtype & SIGHASH_ANYONECANPAY:
            tmp = txtmp.vin[inIdx]
            txtmp.vin = []
            txtmp.vin.append(tmp)

        s = txtmp.serialize()
        s += struct.pack(b"<I", hashtype)

        hash = hash256(s)

        return (hash, None)
示例#5
0
from test_framework.mininode import hash256, ser_string
from test_framework.mc_test.mc_test import *

from binascii import a2b_hex, b2a_hex
from hashlib import sha256
from decimal import Decimal
from struct import unpack, pack

import binascii
import codecs

# this is defined in src/primitives/block.h
SC_CERTIFICATE_BLOCK_VERSION = 3

# this is defined in src/primitives/block.cpp
SC_NULL_HASH = hash256(ser_string("Horizen ScTxsCommitment null hash string"))

SC_EPOCH_LENGTH = 5
SC_CREATION_AMOUNT = Decimal("1.0")
SC_CERT_AMOUNT = Decimal("0.5")

DEBUG_MODE = 1

NODE_LIST = []


def check_array_result(object_array, to_match, expected):
    """
    Pass in array of JSON objects, a dictionary with key/value pairs
    to match against, and another dictionary with expected key/value
    pairs.