Beispiel #1
0
def generate_multisig_address(
        redeem_script: bytes,
        version_byte: bytes = settings.MULTISIG_VERSION_BYTE) -> str:
    """ Generate a multisig address for the multisig wallet

        <version_byte> <redeem_script_hash> <checksum>
        version_byte: MULTISIG_VERSION_BYTE
        redeem_script_hash: RIPEMD160(SHA256(redeem_script))
        checksum: first four bytes of the double SHA256 hash of the version and hash

        :param redeem_script: Redeem script to spend the multisig output
        :type redeem_script: bytes

        :param version_byte: Byte to be preppended in the address that represents the version
        :type version_byte: bytes

        :return: The multisig address
        :rtype: str(base58)
    """
    address = bytearray()

    address.extend(version_byte)

    redeem_script_hash = get_hash160(redeem_script)
    address.extend(redeem_script_hash)

    checksum = hashlib.sha256(hashlib.sha256(address).digest()).digest()[:4]
    address.extend(checksum)

    baddress = bytes(address)
    return base58.b58encode(baddress).decode('utf-8')
Beispiel #2
0
    def test_hash160(self):
        with self.assertRaises(MissingStackItems):
            op_hash160([], log=[], extras=None)

        elem = b'aaaaaaaa'
        hash160 = get_hash160(elem)
        stack = [elem]
        op_hash160(stack, log=[], extras=None)
        self.assertEqual(hash160, stack.pop())
Beispiel #3
0
def op_hash160(stack: Stack, log: List[str], extras: ScriptExtras) -> None:
    """Top stack item is hashed twice: first with SHA-256 and then with RIPEMD-160.
    Result is pushed back to stack.

    :param stack: the stack used when evaluating the script
    :type stack: List[]

    :raises MissingStackItems: if there's no element on stack
    """
    if not len(stack):
        raise MissingStackItems('OP_HASH160: empty stack')
    elem1 = stack.pop()
    assert isinstance(elem1, bytes)
    new_elem = get_hash160(elem1)
    stack.append(new_elem)
Beispiel #4
0
def main():
    from hathor.cli.util import create_parser
    from hathor.crypto.util import get_hash160, get_private_key_bytes, get_public_key_bytes_compressed

    parser = create_parser()

    parser.add_argument('filepath', help='Create a new private key in the given file')
    args = parser.parse_args()

    new_key = ec.generate_private_key(ec.SECP256K1(), default_backend())
    private_key_bytes = get_private_key_bytes(new_key)
    with open(args.filepath, 'w') as key_file:
        key_file.write(base64.b64encode(private_key_bytes).decode('utf-8'))
        print('key created!')
    public_key_bytes = get_public_key_bytes_compressed(new_key.public_key())
    print('base64 pubkey hash:', base64.b64encode(get_hash160(public_key_bytes)).decode('utf-8'))
def main():
    from hathor.cli.util import create_parser
    from hathor.crypto.util import get_hash160, get_private_key_from_bytes, get_public_key_bytes_compressed

    parser = create_parser()

    parser.add_argument('filepath',
                        help='Get public key hash given the private key file')
    args = parser.parse_args()

    with open(args.filepath, 'r') as key_file:
        private_key_bytes = base64.b64decode(key_file.read())
    private_key = get_private_key_from_bytes(private_key_bytes)
    public_key_bytes = get_public_key_bytes_compressed(
        private_key.public_key())
    print('base64:', base64.b64encode(public_key_bytes).decode('utf-8'))
    print('hash base64:',
          base64.b64encode(get_hash160(public_key_bytes)).decode('utf-8'))