def test_serialize(self):
     self.assertEqual(
         ser.serialize(helpers.MSIG_2_2['redeem_script']),
         helpers.MSIG_2_2['ser_script'])
     self.assertEqual(
         ser.serialize('OP_IF'),
         bytes([99]))
Example #2
0
def make_script_sig(stack_script: str, redeem_script: str) -> bytes:
    '''
    str, str -> bytearray
    '''
    script_sig = '{} {}'.format(stack_script,
                                serialization.hex_serialize(redeem_script))
    return serialization.serialize(script_sig)
Example #3
0
def make_script_sig(stack_script, redeem_script):
    '''
    str, str -> bytearray
    '''
    stack_script += ' {}'.format(
        serialization.hex_serialize(redeem_script))
    return serialization.serialize(stack_script)
Example #4
0
def make_script_sig(stack_script: str, redeem_script: str) -> bytes:
    '''
    Make a serialized script sig from a human-readable stack script and redeem
    script.
    '''
    script_sig = '{} {}'.format(stack_script,
                                serialization.hex_serialize(redeem_script))
    return serialization.serialize(script_sig)
Example #5
0
def make_sh_address(script_string: str,
                    witness: bool = False,
                    cashaddr: bool = True) -> str:
    '''Turns a human-readable script into an address'''
    script_bytes = script_ser.serialize(script_string)

    return _ser_script_to_sh_address(script_bytes=script_bytes,
                                     witness=witness,
                                     cashaddr=cashaddr)
Example #6
0
def make_sh_output_script(script_string: str, witness: bool = False) -> bytes:
    '''
    str -> bytearray
    '''
    if witness and not riemann.network.SEGWIT:
        raise ValueError('Network {} does not support witness scripts.'.format(
            riemann.get_current_network_name()))

    script_bytes = serialization.serialize(script_string)
    return make_sh_script_pubkey(script_bytes=script_bytes, witness=witness)
Example #7
0
 def _parse_script_sig(script_sig: bytes) -> Tuple[bytes, bytes]:
     '''
     byte_string -> (byte_string, byte_string)
     '''
     # Is there a better way to do this?
     stack_script = script_sig
     redeem_script = b''
     try:
         # If the last entry deserializes, it's a p2sh input
         # There is a vanishingly small edge case where the pubkey
         #   forms a deserializable script.
         # Edge case: serialization errors on CODESEPARATOR
         deserialized = serialization.deserialize(script_sig)
         items = deserialized.split()
         serialization.hex_deserialize(items[-1])
         stack_script = serialization.serialize(' '.join(items[:-1]))
         redeem_script = serialization.serialize(items[-1])
     except (IndexError, ValueError, NotImplementedError):
         pass
     return stack_script, redeem_script
Example #8
0
def make_sh_output_script(script_string: str, witness: bool = False) -> bytes:
    '''
    Make a P2SH or P2WSH script pubkey from a human-readable script. Does not
    support Compatibility p2wsh-via-p2sh output scripts.

    Args:
        script_string: The human-readable redeem script or witness script.
        witness: Pass True to make a P2WSH script pubkey.
    Returns:
        The script pubkey containing the hash of the serialized script.
    '''
    if witness and not riemann.network.SEGWIT:
        raise ValueError('Network {} does not support witness scripts.'.format(
            riemann.get_current_network_name()))

    script_bytes = serialization.serialize(script_string)
    return make_sh_script_pubkey(script_bytes=script_bytes, witness=witness)
Example #9
0
def createMultiSigAddress(pubkey0, pubkey1, verbose=None):
    msig_script = examples.msig_two_two
    msig_scriptpubkey = msig_script.format(pk0=pubkey0, pk1=pubkey1)

    if verbose: print("Multi sig script: ", msig_scriptpubkey)

    riemann.select_network(NETWORK)
    if ZCASH in NETWORK:
        msig_address = addresses.make_p2sh_address(msig_scriptpubkey)
    elif BITCOIN in NETWORK:
        msig_address = addresses.make_p2wsh_address(msig_scriptpubkey)
    else:
        sys.exit("%s not supported" % NETWORK)
    # TODO: add BOLT opcode here for channel opening?
    redeem_script = msig_scriptpubkey
    script = serialization.serialize(redeem_script)

    return msig_address, script.hex()
Example #10
0
def make_sh_address(script_string: str,
                    witness: bool = False,
                    cashaddr: bool = True) -> str:
    '''
    Turns a human-readable script into an address. Prefers Cashaddrs to legacy
    addresses whenever supported.

    Args:
        script_string: The human-readable script
        witness:       Pass True to generate a witness address if supported.
                       Default False.
        cashaddr:      Pass False to prefer legacy to cashaddr. Default True

    Returns:
        The encoded address
    '''
    script_bytes = script_ser.serialize(script_string)

    return _ser_script_to_sh_address(script_bytes=script_bytes,
                                     witness=witness,
                                     cashaddr=cashaddr)
    def test_serialize_error(self):
        with self.assertRaises(NotImplementedError) as context:
            ser.serialize('00' * 65999)
        self.assertIn(
            'Hex string too long to serialize.',
            str(context.exception))

        with self.assertRaises(NotImplementedError) as context:
            ser.serialize('OP_PUSHDATA4')
        self.assertIn(
            'OP_PUSHDATA4 is a bad idea.',
            str(context.exception))

        with self.assertRaises(NotImplementedError) as context:
            ser.serialize('OP_CODESEPARATOR')
        self.assertIn(
            'OP_CODESEPARATOR is a bad idea.',
            str(context.exception))

        with self.assertRaises(ValueError) as context:
            ser.serialize('OP_NOTAREALOPCODE')
        self.assertIn(
            'non-hexadecimal number found',
            str(context.exception))
Example #12
0
# Calculate the sighashes
sighash_1 = unsigned_tx.sighash_all(index=0,
                                    script_code=script_code_1,
                                    prevout_value=utils.i2le_padded(
                                        1845001, 8))
sighash_2 = unsigned_tx.sighash_all(index=1,
                                    script_code=script_code_2,
                                    prevout_value=utils.i2le_padded(2000, 8))

# Sign the transaction with your private keys elsewhere
pubkey_1 = ''
pubkey_2 = ''

sig_1 = ''
sig_2 = ''

# Build the stack scripts
# For P2PKH this is the signature, then the pubkey
stack_script_1 = '{} {}'.format(sig_1, pubkey_1)
stack_script_2 = '{} {}'.format(sig_2, pubkey_2)

# Add serialized stack scripts to the inputs
signed_tx_in_1 = tx_in_1.copy(stack_script=ser.serialize(stack_script_1))
signed_tx_in_2 = tx_in_2.copy(stack_script=ser.serialize(stack_script_2))

# Add the signed inputs
signed_tx = unsigned_tx.copy(tx_ins=[signed_tx_in_1, signed_tx_in_2])

# Print the hex-serialized transaction, ready for broadcast
print(signed_tx.hex())
Example #13
0
def build_serialized_eris_witness_script(
        synod: Synod) -> bytes:  # pragma: nocover
    '''
    Builds a serialized eris script given a synod
    '''
    return ser.serialize(build_eris_witness_script(synod))