Esempio n. 1
0
    def verify_signtures_on_payments(inputs, outputs, signatures, verifier,
                                     *extra_fields_for_signing):
        correct_sigs_from = []
        for inp, sig in zip(inputs, signatures):
            try:
                sig = base58.b58decode(sig)
            except Exception as ex:
                raise InvalidSignatureFormat from ex

            new_data = [inp, outputs]
            new_data.extend(extra_fields_for_signing)
            idr = inp[ADDRESS]
            ser = serialize_msg_for_signing(new_data)
            try:
                verkey = address_to_verkey(idr)
            except ValueError:
                continue

            vr = verifier(verkey)
            if vr.verify(sig, ser):
                correct_sigs_from.append(idr)

        if len(correct_sigs_from) != len(inputs):
            # All inputs should have signatures present
            raise InsufficientCorrectSignatures(len(correct_sigs_from),
                                                len(inputs))
        return correct_sigs_from
Esempio n. 2
0
    def authenticate_multi(self,
                           msg: Dict,
                           signatures: Dict[str, str],
                           threshold: int = None,
                           verifier: Verifier = DidVerifier):
        num_sigs = len(signatures)
        if threshold is not None:
            if num_sigs < threshold:
                raise InsufficientSignatures(num_sigs, threshold)
        else:
            threshold = num_sigs
        correct_sigs_from = []
        for idr, sig in signatures.items():
            try:
                sig = base58.b58decode(sig)
            except Exception as ex:
                raise InvalidSignatureFormat from ex

            ser = self.serializeForSig(msg, identifier=idr)
            verkey = self.getVerkey(idr)

            if verkey is None:
                raise CouldNotAuthenticate(
                    'Can not find verkey for {}'.format(idr))

            vr = verifier(verkey, identifier=idr)
            if vr.verify(sig, ser):
                correct_sigs_from.append(idr)
                if len(correct_sigs_from) == threshold:
                    break
        else:
            raise InsufficientCorrectSignatures(len(correct_sigs_from),
                                                threshold)
        return correct_sigs_from
Esempio n. 3
0
def test_friendly_exception_formatting_multiple_exceptions():
    """
        Check if the `friendyEx` is formatting multiple exceptions with and without reason field correctly
    """
    ex1 = InsufficientCorrectSignatures(1, 2, {
        '6ouriXMZkLeHsuXrN1X1fd':
        '3GoEPiwhJUjALzrXmmE9tFTXAi7Emv8Y8jjSxQyQB'
    })
    ex2 = InsufficientSignatures(1, 3)
    ex2.__cause__ = ex1
    ex3 = SigningException()
    ex3.__cause__ = ex2

    expected = '{} [caused by {} [caused by {}]]'.format(
        ex3, ex2.reason, ex1.reason)
    formatted_exception = friendlyEx(ex3)

    assert formatted_exception == expected