iotaledger / integration-services

https://demo-integration-services.iota.cafe/
Apache License 2.0
30 stars 19 forks source link

BE: Verify presentation endpoint #619

Closed dominic22 closed 2 years ago

dominic22 commented 2 years ago
dominic22 commented 2 years ago

Code snipped used to verify the VP:


  private getConfig(usePermaNode?: boolean): IClientConfig {
    const permaNode = 'https://chrysalis-chronicle.iota.org/api/mainnet/';
    const node = 'https://chrysalis-nodes.iota.org:443';

    return {
      permanodes: usePermaNode ? [{ url: permaNode }] : [],
      primaryNode: { url: node },
      network: Network.mainnet(),
      localPow: false
    };
  }

  async verifyVerifiablePresentation(signedVpJSON: any): Promise<void> {
    const challenge = '475a7984-1bb5-4c4c-a56f-822bccd46440';
    const expires = Timestamp.nowUTC().checkedAdd(Duration.seconds(10));
    const presentation = Presentation.fromJSON(signedVpJSON);

    // Declare that the challenge must match our expectation:
    const presentationVerifierOptions = new VerifierOptions({
      challenge,
      allowExpired: false
    });

    // Declare that any credential contained in the presentation are not allowed to expire within the next 10 hours:
    const earliestExpiryDate = Timestamp.nowUTC().checkedAdd(Duration.hours(10));
    const credentialValidationOptions = new CredentialValidationOptions({
      earliestExpiryDate: earliestExpiryDate
    });

    // Declare that the presentation holder's DID must match the subject ID on all credentials in the presentation.
    const subjectHolderRelationship = SubjectHolderRelationship.AlwaysSubject;

    const presentationValidationOptions = new PresentationValidationOptions({
      sharedValidationOptions: credentialValidationOptions,
      presentationVerifierOptions: presentationVerifierOptions,
      subjectHolderRelationship: subjectHolderRelationship
    });

    // In order to validate presentations and credentials one needs to resolve the DID Documents of
    // the presentation holder and of credential issuers. This is something the `Resolver` can help with.
    const resolver = await Resolver.builder().clientConfig(this.getConfig(true)).build();
    console.log('before resolve:');
    // Validate the presentation and all the credentials included in it according to the validation options
    await resolver.verifyPresentation(
      presentation,
      presentationValidationOptions,
      FailFast.FirstError
    );

    // Since no errors were thrown by `verifyPresentation` we know that the validation was successful.
    console.log(`VP successfully validated`);
  }