Refactor response handling
MODIFY
WalletWasabi.Tests/UnitTests/Wabisabi/CredentialTests.cs
WalletWasabi.Tests/UnitTests/Wabisabi/CredentialTests.cs
@@ -24,8 +24,7 @@ namespace WalletWasabi.Tests.UnitTests.Wabisabi
{
// Null request. This requests `numberOfCredentials` zero-value credentials.
- var requestFactory = client.GetCredentialRegistrationFactory();
- var (credentialRequest, validationData) = requestFactory.CreateRequestForZeroAmount();
+ var (credentialRequest, validationData) = client.CreateRequestForZeroAmount();
Assert.True(credentialRequest.IsNullRequest);
Assert.Equal(numberOfCredentials, credentialRequest.Requested.Count());
@@ -47,9 +46,8 @@ namespace WalletWasabi.Tests.UnitTests.Wabisabi
}
{
- var requestFactory = client.GetCredentialRegistrationFactory();
var present = client.Credentials.ZeroValue.Take(numberOfCredentials);
- var (credentialRequest, validationData) = requestFactory.CreateRequest(new[] { Money.Coins(1) }, present);
+ var (credentialRequest, validationData) = client.CreateRequest(new[] { Money.Coins(1) }, present);
Assert.False(credentialRequest.IsNullRequest);
var credentialRequested = credentialRequest.Requested.ToArray();
@@ -70,10 +68,9 @@ namespace WalletWasabi.Tests.UnitTests.Wabisabi
}
{
- var requestFactory = client.GetCredentialRegistrationFactory();
var valuableCredential = client.Credentials.Valuable.Take(1);
var amounts = Enumerable.Repeat(Money.Coins(0.5m), 2);
- var (credentialRequest, validationData) = requestFactory.CreateRequest(amounts, valuableCredential);
+ var (credentialRequest, validationData) = client.CreateRequest(amounts, valuableCredential);
Assert.False(credentialRequest.IsNullRequest);
var requested = credentialRequest.Requested.ToArray();
@@ -98,20 +95,18 @@ namespace WalletWasabi.Tests.UnitTests.Wabisabi
{
var client0 = new WabiSabiClient(sk.ComputeCoordinatorParameters(), numberOfCredentials, rnd);
- var requestFactory = client0.GetCredentialRegistrationFactory();
-
- var (credentialRequest, validationData) = requestFactory.CreateRequestForZeroAmount();
+ var (credentialRequest, validationData) = client0.CreateRequestForZeroAmount();
var issuer = new CredentialIssuer(sk, numberOfCredentials, rnd);
var credentialResponse = issuer.HandleRequest(credentialRequest);
client0.HandleResponse(credentialResponse, validationData);
- (credentialRequest, validationData) = requestFactory.CreateRequest(new[] { Money.Coins(1m) }, Enumerable.Empty<Credential>());
+ (credentialRequest, validationData) = client0.CreateRequest(new[] { Money.Coins(1m) }, Enumerable.Empty<Credential>());
credentialResponse = issuer.HandleRequest(credentialRequest);
client0.HandleResponse(credentialResponse, validationData);
- (credentialRequest, validationData) = requestFactory.CreateRequest(new Money[0], client0.Credentials.Valuable);
+ (credentialRequest, validationData) = client0.CreateRequest(new Money[0], client0.Credentials.Valuable);
credentialResponse = issuer.HandleRequest(credentialRequest);
client0.HandleResponse(credentialResponse, validationData);
@@ -133,13 +128,12 @@ namespace WalletWasabi.Tests.UnitTests.Wabisabi
var client = new WabiSabiClient(sk.ComputeCoordinatorParameters(), numberOfCredentials, rnd);
// Null request. This requests `numberOfCredentials` zero-value credentials.
- var factory = client.GetCredentialRegistrationFactory();
- var (credentialRequest, validationData) = factory.CreateRequestForZeroAmount();
+ var (credentialRequest, validationData) = client.CreateRequestForZeroAmount();
var credentialResponse = issuer.HandleRequest(credentialRequest);
client.HandleResponse(credentialResponse, validationData);
- var (validCredentialRequest, _) = factory.CreateRequest(new Money[0], client.Credentials.ZeroValue.Take(1));
+ var (validCredentialRequest, _) = client.CreateRequest(new Money[0], client.Credentials.ZeroValue.Take(1));
// Test incorrect number of presentations (one instead of 3)
var presented = validCredentialRequest.Presented.ToArray();
@@ -165,7 +159,7 @@ namespace WalletWasabi.Tests.UnitTests.Wabisabi
Assert.Equal(WabiSabiErrorCode.InvalidNumberOfPresentedCredentials, ex.ErrorCode);
Assert.Equal("3 credential presentations were expected but 0 were received.", ex.Message);
- (validCredentialRequest, _) = factory.CreateRequest(new Money[0], client.Credentials.All);
+ (validCredentialRequest, _) = client.CreateRequest(new Money[0], client.Credentials.All);
// Test incorrect number of credential requests
invalidCredentialRequest = new RegistrationRequest(
@@ -204,8 +198,7 @@ namespace WalletWasabi.Tests.UnitTests.Wabisabi
{
var client = new WabiSabiClient(sk.ComputeCoordinatorParameters(), numberOfCredentials, rnd);
- var factory = client.GetCredentialRegistrationFactory();
- var (validCredentialRequest, validationData) = factory.CreateRequestForZeroAmount();
+ var (validCredentialRequest, validationData) = client.CreateRequestForZeroAmount();
// Test invalid proofs
var proofs = validCredentialRequest.Proofs.ToArray();
@@ -222,13 +215,12 @@ namespace WalletWasabi.Tests.UnitTests.Wabisabi
{
var client = new WabiSabiClient(sk.ComputeCoordinatorParameters(), numberOfCredentials, rnd);
- var factory = client.GetCredentialRegistrationFactory();
- var (validCredentialRequest, validationData) = factory.CreateRequestForZeroAmount();
+ var (validCredentialRequest, validationData) = client.CreateRequestForZeroAmount();
var credentialResponse = issuer.HandleRequest(validCredentialRequest);
client.HandleResponse(credentialResponse, validationData);
- (validCredentialRequest, validationData) = factory.CreateRequest(Enumerable.Empty<Money>(), client.Credentials.All);
+ (validCredentialRequest, validationData) = client.CreateRequest(Enumerable.Empty<Money>(), client.Credentials.All);
issuer.HandleRequest(validCredentialRequest);
var ex = Assert.Throws<WabiSabiException>(() => issuer.HandleRequest(validCredentialRequest));

MODIFY
WalletWasabi/Wabisabi/CredentialRegistrationFactory.cs
WalletWasabi/Wabisabi/CredentialRegistrationFactory.cs
@@ -13,25 +13,24 @@ using WalletWasabi.Helpers;
namespace WalletWasabi.Wabisabi
{
- public class CredentialRegistrationFactory
+ public class WabiSabiClient
{
- internal CredentialRegistrationFactory(
- int numberOfCredentials,
- CredentialPool credentialPool,
+ public WabiSabiClient(
CoordinatorParameters coordinatorParameters,
+ int numberOfCredentials,
WasabiRandom randomNumberGenerator)
{
- NumberOfCredentials = numberOfCredentials;
- RandomNumberGenerator = randomNumberGenerator;
- CoordinatorParameters = coordinatorParameters;
- CredentialPool = credentialPool;
+ RandomNumberGenerator = Guard.NotNull(nameof(randomNumberGenerator), randomNumberGenerator);
+ NumberOfCredentials = Guard.InRangeAndNotNull(nameof(numberOfCredentials), numberOfCredentials, 1, 100);
+ CoordinatorParameters = Guard.NotNull(nameof(coordinatorParameters), coordinatorParameters);
+ Credentials = new CredentialPool();
}
private int NumberOfCredentials { get; }
private CoordinatorParameters CoordinatorParameters { get; }
- private CredentialPool CredentialPool { get; }
+ public CredentialPool Credentials { get; }
private WasabiRandom RandomNumberGenerator { get; }
@@ -81,7 +80,7 @@ namespace WalletWasabi.Wabisabi
var missingCredentialPresent = NumberOfCredentials - credentialsToPresent.Count();
var alreadyPresentedZeroCredentials = credentialsToPresent.Where(x => x.Amount.IsZero);
- var availableZeroCredentials = CredentialPool.ZeroValue.Except(alreadyPresentedZeroCredentials);
+ var availableZeroCredentials = Credentials.ZeroValue.Except(alreadyPresentedZeroCredentials);
// This should not be possible
var availableZeroCredentialCount = availableZeroCredentials.Count();
@@ -152,6 +151,40 @@ namespace WalletWasabi.Wabisabi
validationData));
}
+ public void HandleResponse(RegistrationResponse registrationResponse, RegistrationValidationData registrationValidationData)
+ {
+ Guard.NotNull(nameof(registrationResponse), registrationResponse);
+ Guard.NotNull(nameof(registrationValidationData), registrationValidationData);
+
+ var issuedCredentialCount = registrationResponse.IssuedCredentials.Count();
+ var requestedCredentialCount = registrationValidationData.Requested.Count();
+ if (issuedCredentialCount != NumberOfCredentials)
+ {
+ throw new WabiSabiException(
+ WabiSabiErrorCode.IssuedCredentialNumberMismatch,
+ $"{issuedCredentialCount} issued but {requestedCredentialCount} were requested.");
+ }
+
+ var credentials = Enumerable
+ .Zip(registrationValidationData.Requested, registrationResponse.IssuedCredentials)
+ .Select(x => (Requested: x.First, Issued: x.Second))
+ .ToArray();
+
+ var statements = credentials
+ .Select(x => ProofSystem.IssuerParameters(CoordinatorParameters, x.Issued, x.Requested.Ma));
+
+ var areCorrectlyIssued = Verifier.Verify(registrationValidationData.Transcript, statements, registrationResponse.Proofs);
+ if (!areCorrectlyIssued)
+ {
+ throw new WabiSabiException(WabiSabiErrorCode.ClientReceivedInvalidProofs);
+ }
+
+ var credentialReceived = credentials.Select(x =>
+ new Credential(new Scalar((ulong)x.Requested.Amount.Satoshi), x.Requested.Randomness, x.Issued));
+
+ Credentials.UpdateCredentials(credentialReceived, registrationValidationData.Presented);
+ }
+
private Transcript BuildTransnscript(bool isNullRequest)
{
var label = $"UnifiedRegistration/{NumberOfCredentials}/{isNullRequest}";

Refactor response handling