예제 #1
0
 def to_create_vote_hash_script(self):
     return ''.join([
         script.CScript(AsimovOpCode.OP_VOTE.to_bytes(1, 'little')).hex(),
         script.CScript(AsimovOpCode.OP_DATA_21.to_bytes(1,
                                                         'little')).hex(),
         remove_0x_prefix(Web3.toHex(hexstr=self.address))
     ])
예제 #2
0
    def from_scriptPubKey(cls,
                          scriptPubKey,
                          accept_non_canonical_pushdata=True,
                          accept_bare_checksig=True):
        """Convert a scriptPubKey to a P2PKH address

        Raises CBitcoinAddressError if the scriptPubKey isn't of the correct
        form.

        accept_non_canonical_pushdata - Allow non-canonical pushes (default True)

        accept_bare_checksig          - Treat bare-checksig as P2PKH scriptPubKeys (default True)
        """
        if accept_non_canonical_pushdata:
            # Canonicalize script pushes
            scriptPubKey = script.CScript(
                scriptPubKey)  # in case it's not a CScript instance yet

            try:
                scriptPubKey = script.CScript(
                    tuple(scriptPubKey))  # canonicalize
            except bitcointx.core.script.CScriptInvalidError:
                raise CBitcoinAddressError(
                    'not a P2PKH scriptPubKey: script is invalid')

        if scriptPubKey.is_witness_v0_keyhash():
            return cls.from_bytes(scriptPubKey[2:22])
        elif scriptPubKey.is_witness_v0_nested_keyhash():
            return cls.from_bytes(scriptPubKey[3:23])
        elif (len(scriptPubKey) == 25 and scriptPubKey[0] == script.OP_DUP
              and scriptPubKey[1] == script.OP_HASH160
              and scriptPubKey[2] == 0x14
              and scriptPubKey[23] == script.OP_EQUALVERIFY
              and scriptPubKey[24] == script.OP_CHECKSIG):
            return cls.from_bytes(scriptPubKey[3:23])

        elif accept_bare_checksig:
            pubkey = None

            # We can operate on the raw bytes directly because we've
            # canonicalized everything above.
            if (len(scriptPubKey) == 35  # compressed
                    and scriptPubKey[0] == 0x21
                    and scriptPubKey[34] == script.OP_CHECKSIG):

                pubkey = scriptPubKey[1:34]

            elif (len(scriptPubKey) == 67  # uncompressed
                  and scriptPubKey[0] == 0x41
                  and scriptPubKey[66] == script.OP_CHECKSIG):

                pubkey = scriptPubKey[1:66]

            if pubkey is not None:
                return cls.from_pubkey(pubkey, accept_invalid=True)

        raise CBitcoinAddressError('not a P2PKH scriptPubKey')
예제 #3
0
def test_vote_hash_script():
    expected = 'c6153197eb7cd1538b26cf2398caa5986f1744934fb43e0b7f2a71c947bba0da3b48'
    hex_value = ''.join([
        script.CScript(AsimovOpCode.OP_VOTE.to_bytes(1, 'little')).hex(),
        script.CScript(AsimovOpCode.OP_DATA_21.to_bytes(1, 'little')).hex(),
        remove_0x_prefix(
            Web3.toHex(
                hexstr=
                "0x3197eb7cd1538b26cf2398caa5986f1744934fb43e0b7f2a71c947bba0da3b48"
            ))
    ])
    assert hex_value == expected
예제 #4
0
 def to_scriptPubKey(self, nested=False):
     """Convert an address to a scriptPubKey"""
     assert self.nVersion == bitcointx.params.BASE58_PREFIXES['PUBKEY_ADDR']
     return script.CScript([
         script.OP_DUP, script.OP_HASH160, self, script.OP_EQUALVERIFY,
         script.OP_CHECKSIG
     ])
예제 #5
0
def test_call_contract_hash_script():
    expected = 'c2203197eb7cd1538b26cf2398caa5986f1744934fb43e0b7f2a71c947bba0da3b48'
    hex_value = script.CScript(AsimovOpCode.OP_CALL.to_bytes(1, 'little')).hex() + \
        len(
            Web3.toBytes(hexstr="0x3197eb7cd1538b26cf2398caa5986f1744934fb43e0b7f2a71c947bba0da3b48")
        ).to_bytes(1, 'little', signed=False).hex() + \
        remove_0x_prefix(
            Web3.toHex(hexstr="0x3197eb7cd1538b26cf2398caa5986f1744934fb43e0b7f2a71c947bba0da3b48"))
    assert hex_value == expected
예제 #6
0
def test_pub_key_script():
    expected = "76a9203197eb7cd1538b26cf2398caa5986f1744934fb43e0b7f2a71c947bba0da3b48c5ac"
    hex_value = script.CScript([
        script.OP_DUP, script.OP_HASH160,
        Web3.toBytes(
            hexstr=
            "0x3197eb7cd1538b26cf2398caa5986f1744934fb43e0b7f2a71c947bba0da3b48"
        ), AsimovOpCode.OP_IFLAG_EQUALVERIFY, script.OP_CHECKSIG
    ]).hex()
    assert hex_value == expected
예제 #7
0
def create_funding_output(
    taker_pubkey,
    maker_pubkey,
    premium_amount,
    fund_amount,
):
    # References: https://git.io/J3HsI (LND) https://git.io/J3Hs4 (BIP69)
    if list(maker_pubkey) > list(taker_pubkey):
        pk1, pk2 = taker_pubkey, maker_pubkey
    else:
        pk1, pk2 = maker_pubkey, taker_pubkey

    msig_script = bs.CScript([bs.OP_2, pk1, pk2, bs.OP_2, bs.OP_CHECKMULTISIG])

    # Convert to P2WSH
    script_pubkey = bs.CScript([bs.OP_0, hashlib.sha256(msig_script).digest()])

    if not script_pubkey.is_witness_v0_scripthash():
        raise RuntimeError("Funding transaction is not using P2WSH.")

    funding_output = bc.CTxOut(premium_amount + fund_amount, script_pubkey)
    return funding_output
예제 #8
0
 def to_scriptPubKey(self, nested=False):
     """Convert an address to a scriptPubKey"""
     return script.CScript([
         script.OP_DUP, script.OP_HASH160, self, script.OP_EQUALVERIFY,
         script.OP_CHECKSIG
     ])
예제 #9
0
 def to_scriptPubKey(self):
     """Convert an address to a scriptPubKey"""
     return script.CScript([script.OP_HASH160, self, script.OP_EQUAL])
예제 #10
0
def test_scrip_op():
    expected = "c5"
    hex_value = script.CScript([script.CScriptOp(197)]).hex()
    assert hex_value == expected
예제 #11
0
 def to_contract_hash_script(self):
     return script.CScript(AsimovOpCode.OP_CALL.to_bytes(1, 'little')).hex() + \
            len(Web3.toBytes(hexstr=self.address)).to_bytes(1, 'little', signed=False).hex() + \
            remove_0x_prefix(Web3.toHex(hexstr=self.address))
예제 #12
0
 def to_create_template_hash_script():
     return script.CScript(AsimovOpCode.OP_TEMPLATE.to_bytes(
         1, 'little')).hex()
예제 #13
0
 def to_create_contract_hash_script():
     return script.CScript(AsimovOpCode.OP_CREATE.to_bytes(1,
                                                           'little')).hex()
예제 #14
0
 def to_script_pub_key(self) -> str:
     return script.CScript([
         script.OP_DUP, script.OP_HASH160,
         Web3.toBytes(hexstr=self.address),
         AsimovOpCode.OP_IFLAG_EQUALVERIFY, script.OP_CHECKSIG
     ]).hex()
예제 #15
0
 def to_scriptPubKey(self):
     """Convert an address to a scriptPubKey"""
     assert self.witver == 0
     return script.CScript([0, self])
예제 #16
0
def test_create_contract_hash_script():
    expected = 'c1'
    hex_value = script.CScript(AsimovOpCode.OP_CREATE.to_bytes(
        1, 'little')).hex()
    assert hex_value == expected
예제 #17
0
 def to_redeemScript(self):
     return script.CScript([
         script.OP_DUP, script.OP_HASH160, self, script.OP_EQUALVERIFY,
         script.OP_CHECKSIG
     ])
예제 #18
0
 def to_scriptPubKey(self):
     """Convert an address to a scriptPubKey"""
     assert self.nVersion == bitcointx.params.BASE58_PREFIXES['SCRIPT_ADDR']
     return script.CScript([script.OP_HASH160, self, script.OP_EQUAL])