Search in sources :

Example 1 with ReceiptSerial

use of org.signal.zkgroup.receipts.ReceiptSerial in project Signal-Android by signalapp.

the class SubscriptionReceiptRequestResponseJob method generateRequestContext.

private static ReceiptCredentialRequestContext generateRequestContext() {
    Log.d(TAG, "Generating request credentials context for token redemption...", true);
    SecureRandom secureRandom = new SecureRandom();
    byte[] randomBytes = Util.getSecretBytes(ReceiptSerial.SIZE);
    try {
        ReceiptSerial receiptSerial = new ReceiptSerial(randomBytes);
        ClientZkReceiptOperations operations = ApplicationDependencies.getClientZkReceiptOperations();
        return operations.createReceiptCredentialRequestContext(secureRandom, receiptSerial);
    } catch (InvalidInputException | VerificationFailedException e) {
        Log.e(TAG, "Failed to create credential.", e);
        throw new AssertionError(e);
    }
}
Also used : InvalidInputException(org.signal.zkgroup.InvalidInputException) ReceiptSerial(org.signal.zkgroup.receipts.ReceiptSerial) ClientZkReceiptOperations(org.signal.zkgroup.receipts.ClientZkReceiptOperations) SecureRandom(java.security.SecureRandom) VerificationFailedException(org.signal.zkgroup.VerificationFailedException)

Example 2 with ReceiptSerial

use of org.signal.zkgroup.receipts.ReceiptSerial in project Signal-Android by WhisperSystems.

the class SubscriptionReceiptRequestResponseJob method generateRequestContext.

private static ReceiptCredentialRequestContext generateRequestContext() {
    Log.d(TAG, "Generating request credentials context for token redemption...", true);
    SecureRandom secureRandom = new SecureRandom();
    byte[] randomBytes = Util.getSecretBytes(ReceiptSerial.SIZE);
    try {
        ReceiptSerial receiptSerial = new ReceiptSerial(randomBytes);
        ClientZkReceiptOperations operations = ApplicationDependencies.getClientZkReceiptOperations();
        return operations.createReceiptCredentialRequestContext(secureRandom, receiptSerial);
    } catch (InvalidInputException | VerificationFailedException e) {
        Log.e(TAG, "Failed to create credential.", e);
        throw new AssertionError(e);
    }
}
Also used : InvalidInputException(org.signal.zkgroup.InvalidInputException) ReceiptSerial(org.signal.zkgroup.receipts.ReceiptSerial) ClientZkReceiptOperations(org.signal.zkgroup.receipts.ClientZkReceiptOperations) SecureRandom(java.security.SecureRandom) VerificationFailedException(org.signal.zkgroup.VerificationFailedException)

Example 3 with ReceiptSerial

use of org.signal.zkgroup.receipts.ReceiptSerial in project Signal-Android by WhisperSystems.

the class BoostReceiptRequestResponseJob method onRun.

@Override
protected void onRun() throws Exception {
    if (requestContext == null) {
        Log.d(TAG, "Creating request context..");
        SecureRandom secureRandom = new SecureRandom();
        byte[] randomBytes = new byte[ReceiptSerial.SIZE];
        secureRandom.nextBytes(randomBytes);
        ReceiptSerial receiptSerial = new ReceiptSerial(randomBytes);
        ClientZkReceiptOperations operations = ApplicationDependencies.getClientZkReceiptOperations();
        requestContext = operations.createReceiptCredentialRequestContext(secureRandom, receiptSerial);
    } else {
        Log.d(TAG, "Reusing request context from previous run", true);
    }
    Log.d(TAG, "Submitting credential to server", true);
    ServiceResponse<ReceiptCredentialResponse> response = ApplicationDependencies.getDonationsService().submitBoostReceiptCredentialRequest(paymentIntentId, requestContext.getRequest()).blockingGet();
    if (response.getApplicationError().isPresent()) {
        handleApplicationError(context, response);
    } else if (response.getResult().isPresent()) {
        ReceiptCredential receiptCredential = getReceiptCredential(response.getResult().get());
        if (!isCredentialValid(receiptCredential)) {
            DonationError.routeDonationError(context, DonationError.genericBadgeRedemptionFailure(DonationErrorSource.BOOST));
            throw new IOException("Could not validate receipt credential");
        }
        Log.d(TAG, "Validated credential. Handing off to redemption job.", true);
        ReceiptCredentialPresentation receiptCredentialPresentation = getReceiptCredentialPresentation(receiptCredential);
        setOutputData(new Data.Builder().putBlobAsString(DonationReceiptRedemptionJob.INPUT_RECEIPT_CREDENTIAL_PRESENTATION, receiptCredentialPresentation.serialize()).build());
    } else {
        Log.w(TAG, "Encountered a retryable exception: " + response.getStatus(), response.getExecutionError().orNull(), true);
        throw new RetryableException();
    }
}
Also used : ReceiptCredential(org.signal.zkgroup.receipts.ReceiptCredential) ReceiptSerial(org.signal.zkgroup.receipts.ReceiptSerial) ReceiptCredentialPresentation(org.signal.zkgroup.receipts.ReceiptCredentialPresentation) ClientZkReceiptOperations(org.signal.zkgroup.receipts.ClientZkReceiptOperations) ReceiptCredentialResponse(org.signal.zkgroup.receipts.ReceiptCredentialResponse) SecureRandom(java.security.SecureRandom) IOException(java.io.IOException)

Example 4 with ReceiptSerial

use of org.signal.zkgroup.receipts.ReceiptSerial in project Signal-Android by signalapp.

the class BoostReceiptRequestResponseJob method onRun.

@Override
protected void onRun() throws Exception {
    if (requestContext == null) {
        Log.d(TAG, "Creating request context..");
        SecureRandom secureRandom = new SecureRandom();
        byte[] randomBytes = new byte[ReceiptSerial.SIZE];
        secureRandom.nextBytes(randomBytes);
        ReceiptSerial receiptSerial = new ReceiptSerial(randomBytes);
        ClientZkReceiptOperations operations = ApplicationDependencies.getClientZkReceiptOperations();
        requestContext = operations.createReceiptCredentialRequestContext(secureRandom, receiptSerial);
    } else {
        Log.d(TAG, "Reusing request context from previous run", true);
    }
    Log.d(TAG, "Submitting credential to server", true);
    ServiceResponse<ReceiptCredentialResponse> response = ApplicationDependencies.getDonationsService().submitBoostReceiptCredentialRequest(paymentIntentId, requestContext.getRequest()).blockingGet();
    if (response.getApplicationError().isPresent()) {
        handleApplicationError(context, response);
    } else if (response.getResult().isPresent()) {
        ReceiptCredential receiptCredential = getReceiptCredential(response.getResult().get());
        if (!isCredentialValid(receiptCredential)) {
            DonationError.routeDonationError(context, DonationError.genericBadgeRedemptionFailure(DonationErrorSource.BOOST));
            throw new IOException("Could not validate receipt credential");
        }
        Log.d(TAG, "Validated credential. Handing off to redemption job.", true);
        ReceiptCredentialPresentation receiptCredentialPresentation = getReceiptCredentialPresentation(receiptCredential);
        setOutputData(new Data.Builder().putBlobAsString(DonationReceiptRedemptionJob.INPUT_RECEIPT_CREDENTIAL_PRESENTATION, receiptCredentialPresentation.serialize()).build());
    } else {
        Log.w(TAG, "Encountered a retryable exception: " + response.getStatus(), response.getExecutionError().orNull(), true);
        throw new RetryableException();
    }
}
Also used : ReceiptCredential(org.signal.zkgroup.receipts.ReceiptCredential) ReceiptSerial(org.signal.zkgroup.receipts.ReceiptSerial) ReceiptCredentialPresentation(org.signal.zkgroup.receipts.ReceiptCredentialPresentation) ClientZkReceiptOperations(org.signal.zkgroup.receipts.ClientZkReceiptOperations) ReceiptCredentialResponse(org.signal.zkgroup.receipts.ReceiptCredentialResponse) SecureRandom(java.security.SecureRandom) IOException(java.io.IOException)

Aggregations

SecureRandom (java.security.SecureRandom)4 ClientZkReceiptOperations (org.signal.zkgroup.receipts.ClientZkReceiptOperations)4 ReceiptSerial (org.signal.zkgroup.receipts.ReceiptSerial)4 IOException (java.io.IOException)2 InvalidInputException (org.signal.zkgroup.InvalidInputException)2 VerificationFailedException (org.signal.zkgroup.VerificationFailedException)2 ReceiptCredential (org.signal.zkgroup.receipts.ReceiptCredential)2 ReceiptCredentialPresentation (org.signal.zkgroup.receipts.ReceiptCredentialPresentation)2 ReceiptCredentialResponse (org.signal.zkgroup.receipts.ReceiptCredentialResponse)2