Пример #1
0
    def _build_witnesses(self, tx_aux_hash: str):
        witnesses = []
        for input in self.inputs:
            _, node = derive_address_and_node(self.keychain, input.address_n)
            message = (b"\x01" + cbor.encode(self.protocol_magic) +
                       b"\x58\x20" + tx_aux_hash)
            signature = ed25519.sign_ext(node.private_key(),
                                         node.private_key_ext(), message)
            extended_public_key = (remove_ed25519_prefix(node.public_key()) +
                                   node.chain_code())
            witnesses.append([
                (input.type or 0),
                cbor.Tagged(24, cbor.encode([extended_public_key, signature])),
            ])

        return witnesses
Пример #2
0
def cborize_certificate(
    keychain: seed.Keychain, certificate: CardanoTxCertificateType
) -> CborSequence:
    if certificate.type in (
        CardanoCertificateType.STAKE_REGISTRATION,
        CardanoCertificateType.STAKE_DEREGISTRATION,
    ):
        return (
            certificate.type,
            (0, get_public_key_hash(keychain, certificate.path)),
        )
    elif certificate.type == CardanoCertificateType.STAKE_DELEGATION:
        return (
            certificate.type,
            (0, get_public_key_hash(keychain, certificate.path)),
            certificate.pool,
        )
    elif certificate.type == CardanoCertificateType.STAKE_POOL_REGISTRATION:
        pool_parameters = certificate.pool_parameters

        assert pool_parameters is not None

        return (
            certificate.type,
            pool_parameters.pool_id,
            pool_parameters.vrf_key_hash,
            pool_parameters.pledge,
            pool_parameters.cost,
            cbor.Tagged(
                30,
                (
                    pool_parameters.margin_numerator,
                    pool_parameters.margin_denominator,
                ),
            ),
            # this relies on pool_parameters.reward_account being validated beforehand
            # in _validate_pool_parameters
            get_address_bytes_unsafe(pool_parameters.reward_account),
            _cborize_pool_owners(keychain, pool_parameters.owners),
            _cborize_pool_relays(pool_parameters.relays),
            _cborize_pool_metadata(pool_parameters.metadata),
        )
    else:
        raise INVALID_CERTIFICATE
Пример #3
0
    def serialise_tx(self):

        self._process_outputs()

        inputs_cbor = []
        for input in self.inputs:
            inputs_cbor.append(
                [
                    (input.type or 0),
                    cbor.Tagged(24, cbor.encode([input.prev_hash, input.prev_index])),
                ]
            )

        inputs_cbor = cbor.IndefiniteLengthArray(inputs_cbor)

        outputs_cbor = []
        for index, address in enumerate(self.output_addresses):
            outputs_cbor.append(
                [cbor.Raw(base58.decode(address)), self.outgoing_coins[index]]
            )

        for index, address in enumerate(self.change_addresses):
            outputs_cbor.append(
                [cbor.Raw(base58.decode(address)), self.change_coins[index]]
            )

        outputs_cbor = cbor.IndefiniteLengthArray(outputs_cbor)

        tx_aux_cbor = [inputs_cbor, outputs_cbor, self.attributes]
        tx_hash = hashlib.blake2b(data=cbor.encode(tx_aux_cbor), outlen=32).digest()

        witnesses = self._build_witnesses(tx_hash)
        tx_body = cbor.encode([tx_aux_cbor, witnesses])

        self.fee = self.compute_fee(
            self.input_coins_sum, self.outgoing_coins, self.change_coins
        )

        return tx_body, tx_hash
Пример #4
0
def cborize_initial_pool_registration_certificate_fields(
    certificate: CardanoTxCertificate, ) -> CborSequence:
    assert certificate.type == CardanoCertificateType.STAKE_POOL_REGISTRATION

    pool_parameters = certificate.pool_parameters
    assert pool_parameters is not None

    return (
        certificate.type,
        pool_parameters.pool_id,
        pool_parameters.vrf_key_hash,
        pool_parameters.pledge,
        pool_parameters.cost,
        cbor.Tagged(
            30,
            (
                pool_parameters.margin_numerator,
                pool_parameters.margin_denominator,
            ),
        ),
        # this relies on pool_parameters.reward_account being validated beforehand
        # in _validate_pool_parameters
        get_address_bytes_unsafe(pool_parameters.reward_account),
    )
Пример #5
0
def _encode_address_raw(address_data_encoded) -> bytes:
    return cbor.encode([
        cbor.Tagged(24, address_data_encoded),
        crc.crc32(address_data_encoded)
    ])
Пример #6
0
def _encode_address_raw(address_data_encoded):
    return base58.encode(
        cbor.encode([
            cbor.Tagged(24, address_data_encoded),
            crc.crc32(address_data_encoded)
        ]))