Skip to content
This repository has been archived by the owner on Dec 12, 2024. It is now read-only.

Validate rfq claims against offering #35

Merged
merged 2 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/src/protocol/exceptions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ enum TbdexExceptionCode {
resourceSignatureMissing,
resourceSignatureMismatch,
resourceUnknownKind,
rfqClaimsInsufficient,
rfqClaimsHashMismatch,
rfqClaimsMissing,
rfqOfferingIdMismatch,
Expand Down
7 changes: 4 additions & 3 deletions lib/src/protocol/models/resource_data.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:json_schema/json_schema.dart';
import 'package:web5/web5.dart';

abstract class Data {
Map<String, dynamic> toJson() {
Expand All @@ -13,14 +14,14 @@ class OfferingData extends ResourceData {
final String payoutUnitsPerPayinUnit;
final PayinDetails payin;
final PayoutDetails payout;
// final PresentationDefinitionV2? requiredClaims;
final PresentationDefinition? requiredClaims;

OfferingData({
required this.description,
required this.payoutUnitsPerPayinUnit,
required this.payin,
required this.payout,
// this.requiredClaims,
this.requiredClaims,
});

factory OfferingData.fromJson(Map<String, dynamic> json) {
Expand All @@ -29,7 +30,7 @@ class OfferingData extends ResourceData {
payoutUnitsPerPayinUnit: json['payoutUnitsPerPayinUnit'],
payin: PayinDetails.fromJson(json['payin']),
payout: PayoutDetails.fromJson(json['payout']),
// requiredClaims: json['requiredClaims'] != null ? PresentationDefinitionV2.fromJson(json['requiredClaims']) : null,
requiredClaims: json['requiredClaims'] != null ? PresentationDefinition.fromJson(json['requiredClaims']) : null,
);
}

Expand Down
25 changes: 23 additions & 2 deletions lib/src/protocol/models/rfq.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:tbdex/src/protocol/models/message.dart';
import 'package:tbdex/src/protocol/models/message_data.dart';
import 'package:tbdex/src/protocol/models/offering.dart';
import 'package:tbdex/src/protocol/validator.dart';
import 'package:tbdex/tbdex.dart';
import 'package:web5/web5.dart';

class Rfq extends Message {
Expand Down Expand Up @@ -336,8 +337,28 @@ class Rfq extends Message {
}
}

// TODO(ethan-tbd): verify claims
// offering.data.requiredClaims?.forEach(verifyClaims);
verifyClaims(offering);
}

void verifyClaims(Offering offering) {
if (offering.data.requiredClaims == null) {
return;
}
final presentationDefinition = offering.data.requiredClaims!;

final credentials =
presentationDefinition.selectCredentials(privateData?.claims ?? []);

if (credentials.isEmpty) {
throw TbdexVerifyOfferingRequirementsException(
TbdexExceptionCode.rfqClaimsInsufficient,
"Rfq claims were insufficient to satisfy Offering's required claims",
);
}

for (final credential in credentials) {
DecodedVcJwt.decode(credential).verify();
}
}

factory Rfq.fromJson(Map<String, dynamic> json) {
Expand Down
36 changes: 34 additions & 2 deletions test/helpers/test_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ class TestData {
pfiDid = await DidJwk.create(keyManager: _pfiKeyManager);
}

static Offering getOffering() {
static Offering getOffering({
PresentationDefinition? requiredClaims,
}) {
return Offering.create(
pfiDid.uri,
OfferingData(
Expand All @@ -57,17 +59,47 @@ class TestData {
),
],
),
requiredClaims: requiredClaims,
),
);
}

static PresentationDefinition getRequiredClaims() {
// From web5-spec test vectors
const json = r'''
{
"id": "7ce4004c-3c38-4853-968b-e411bafcd945",
"input_descriptors": [
{
"id": "bbdb9b7c-5754-4f46-b63b-590bada959e0",
"constraints": {
"fields": [
{
"path": ["$.vc.type[*]"],
"filter": {
"type": "string",
"pattern": "^YoloCredential$"
}
}
]
}
}
]
}
''';

return PresentationDefinition.fromJson(jsonDecode(json));
}

static Rfq getRfq({
String? offeringId,
String? amount,
String? payinKind,
String? payoutKind,
String? to,
List<String>? claims,
}) {
claims ??= [];
return Rfq.create(
to ?? pfiDid.uri,
aliceDid.uri,
Expand All @@ -92,7 +124,7 @@ class TestData {
'cvv': '123',
}),
),
claims: [],
claims: claims,
),
);
}
Expand Down
51 changes: 47 additions & 4 deletions test/protocol/models/rfq_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:tbdex/src/protocol/models/resource.dart';
import 'package:tbdex/src/protocol/models/rfq.dart';
import 'package:test/test.dart';
import 'package:typeid/typeid.dart';
import 'package:web5/web5.dart';

import '../../helpers/test_data.dart';

Expand Down Expand Up @@ -352,10 +353,52 @@ void main() async {
});
});

test('can verify offering requirements', () {
final offering = TestData.getOffering();
final rfq = TestData.getRfq(offeringId: offering.metadata.id);
expect(() => rfq.verifyOfferingRequirements(offering), returnsNormally);
group('verifyOfferingRequirements', () {
test('can verify offering requirements', () {
final offering = TestData.getOffering();
final rfq = TestData.getRfq(offeringId: offering.metadata.id);
expect(() => rfq.verifyOfferingRequirements(offering), returnsNormally);
});

test('succeeds if claims satisfy offering required claims', () async {
final vc = VerifiableCredential.create(
// this credential fulfills the offering's required claims
type: ['YoloCredential', 'VerifiableCredential',],
issuer: TestData.aliceDid.uri,
subject: TestData.aliceDid.uri,
data: {
'beep': 'boop',
},
);
final vcJwt = await vc.sign(TestData.aliceDid);

final offering =
TestData.getOffering(requiredClaims: TestData.getRequiredClaims());
final rfq =
TestData.getRfq(offeringId: offering.metadata.id, claims: [vcJwt]);

expect(() => rfq.verifyOfferingRequirements(offering), returnsNormally);
});

test('fails if offering has no required claims', () async {
final vc = VerifiableCredential.create(
// this credential fulfills the offering's required claims
type: ['NotYoloCredential', 'VerifiableCredential',],
issuer: TestData.aliceDid.uri,
subject: TestData.aliceDid.uri,
data: {
'beep': 'boop',
},
);
final vcJwt = await vc.sign(TestData.aliceDid);

final offering =
TestData.getOffering(requiredClaims: TestData.getRequiredClaims());
final rfq =
TestData.getRfq(offeringId: offering.metadata.id, claims: [vcJwt]);

expect(() => rfq.verifyOfferingRequirements(offering), throwsException);
});
});

test('should throw exception if rfq offeringId differs from offering id',
Expand Down
Loading