Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(anoncreds): use legacy prover did #1374

Merged

Conversation

genaris
Copy link
Contributor

@genaris genaris commented Mar 8, 2023

Adds useLegacyProverDid flag for credential request creation, which is always set when dealing with legacy indy identifiers. Some checks were added in LegacyIndyCredentialFormatService to make sure we are using legacy ones.

When using IndySdk, only legacy prover_did can be used, while in AnonCredsRs through this flag we can select between prover_did and entropy, unless we are using new identifiers where only entropy can be generated.

)
}

if (!credentialDefinitionId.match(legacyIndyCredentialDefinitionIdRegex)) {

Check failure

Code scanning / CodeQL

Polynomial regular expression used on uncontrolled data

This [regular expression](1) that depends on [library input](2) may run slow on strings with many repetitions of 'a:.:a'. This [regular expression](1) that depends on [library input](3) may run slow on strings with many repetitions of 'a:.:a'.
if (!credOffer.schema_id || !credOffer.cred_def_id) {
if (
!credOffer.schema_id.match(legacyIndySchemaIdRegex) ||
!credOffer.cred_def_id.match(legacyIndyCredentialDefinitionIdRegex)

Check failure

Code scanning / CodeQL

Polynomial regular expression used on uncontrolled data

This [regular expression](1) that depends on [library input](2) may run slow on strings with many repetitions of 'a:.:a'. This [regular expression](1) that depends on [library input](3) may run slow on strings with many repetitions of 'a:.:a'.
@@ -226,6 +234,11 @@

const credentialOffer = offerAttachment.getDataAsJson<AnonCredsCredentialOffer>()

if (!credentialOffer.cred_def_id.match(legacyIndyCredentialDefinitionIdRegex)) {

Check failure

Code scanning / CodeQL

Polynomial regular expression used on uncontrolled data

This [regular expression](1) that depends on [library input](2) may run slow on strings with many repetitions of 'a:.:a'.
credentialDefinition: CredentialDefinition.load(JSON.stringify(credentialDefinition)),
credentialOffer: CredentialOffer.load(JSON.stringify(credentialOffer)),
masterSecret: MasterSecret.load(JSON.stringify({ value: { ms: linkSecretRecord.value } })),
const isLegacyIdentifier = credentialOffer.cred_def_id.match(legacyIndyCredentialDefinitionIdRegex)

Check failure

Code scanning / CodeQL

Polynomial regular expression used on uncontrolled data

This [regular expression](1) that depends on [library input](2) may run slow on strings with many repetitions of 'a:.:a'.
@codecov-commenter
Copy link

codecov-commenter commented Mar 8, 2023

Codecov Report

Merging #1374 (5f17ee8) into main (0351eec) will decrease coverage by 0.03%.
The diff coverage is 68.18%.

@@            Coverage Diff             @@
##             main    #1374      +/-   ##
==========================================
- Coverage   87.23%   87.20%   -0.03%     
==========================================
  Files         780      780              
  Lines       18728    18747      +19     
  Branches     3211     3221      +10     
==========================================
+ Hits        16337    16349      +12     
- Misses       2384     2391       +7     
  Partials        7        7              
Impacted Files Coverage Δ
...sdk/src/anoncreds/services/IndySdkHolderService.ts 8.40% <0.00%> (-0.15%) ⬇️
...ncreds/src/formats/LegacyIndyProofFormatService.ts 72.83% <50.00%> (-0.39%) ⬇️
...s/src/formats/LegacyIndyCredentialFormatService.ts 74.86% <75.00%> (-0.14%) ⬇️
...ncreds-rs/src/services/AnonCredsRsHolderService.ts 91.72% <85.71%> (-0.42%) ⬇️
packages/anoncreds/src/index.ts 100.00% <100.00%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@@ -499,6 +505,10 @@
const credentialDefinitions: { [key: string]: AnonCredsCredentialDefinition } = {}

for (const credentialDefinitionId of credentialDefinitionIds) {
if (!credentialDefinitionId.match(legacyIndyCredentialDefinitionIdRegex)) {

Check failure

Code scanning / CodeQL

Polynomial regular expression used on uncontrolled data

This [regular expression](1) that depends on [library input](2) may run slow on strings with many repetitions of 'a:.:a'. This [regular expression](1) that depends on [library input](3) may run slow on strings with many repetitions of 'a:.:a'.
@TimoGlastra
Copy link
Contributor

What's the blocker for this PR @genaris?

@genaris
Copy link
Contributor Author

genaris commented Mar 15, 2023

What's the blocker for this PR @genaris?

It was hyperledger/anoncreds-rs#137

Now it's merged but waiting for the release in npm integrating it.

@TimoGlastra
Copy link
Contributor

createReturnObj = CredentialRequest.create({
entropy: anoncreds.generateNonce(), // FIXME: find a better source of entropy
entropy: !useLegacyProverDid || !isLegacyIdentifier ? anoncreds.generateNonce() : undefined, // FIXME: find a better source of entropy
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another option for generating entropy can be calling agentContext.wallet.generateWalletKey, which in AskarWallet creates an ephemeral key and converts its private key to a base58 string (I guess IndySdkWallet does more or less the same thing). However I'm not sure if it's good to rely on wallet implementation for that (unless we strictly specify the requirements for such generateWalletKey in API).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It feels like we're misusing the generateWalletKey in that case. I think a nonce should provide enough randomness right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO the nonce generated by anoncreds, apart from being convenient for us, is enough for the purposes of giving some entropy in credential generation. But just trying to find an alternative that provides a true 'alphanumeric' random string as stated in the spec.

@genaris genaris marked this pull request as ready for review March 15, 2023 14:03
@genaris genaris requested a review from a team as a code owner March 15, 2023 14:03
createReturnObj = CredentialRequest.create({
entropy: anoncreds.generateNonce(), // FIXME: find a better source of entropy
entropy: !useLegacyProverDid || !isLegacyIdentifier ? anoncreds.generateNonce() : undefined, // FIXME: find a better source of entropy
proverDid: useLegacyProverDid ? generateLegacyProverDidLikeString() : undefined,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As it should be random, do wr need to make this a secure ranom generation? E.g. nonce and then encode it as base58?

We could also add a getRandomBytes method to the wallet?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I copied it from anoncreds/indy-sdk package implementation, but also thought that could be a good idea to rely in anoncreds native library instead of uuid as it's being done currently in generateLegacyProverDidLikeString.

Any 22-bytes long base58 string will be good for that purpose, right?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As it's used for entropy, it may be needed for the string to be generated with actual secure random bytes instead of a random string?


const legacySchemaId = getLegacySchemaId(legacyIssuerId, options.schema.name, options.schema.version)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The old implementation supported both for convenience, why is this needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If schema issuerId is an unqualified did, parseIndyDid threw an error. Maybe I can do it 'automatic' (i.e. without config flag and a try-catch?)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's for tests, so whatever works
Is fine. Just curious

@genaris genaris force-pushed the feat/use-legacy-prover-did branch from c7d385a to cc03c05 Compare March 15, 2023 20:48
@TimoGlastra
Copy link
Contributor

LGTM!!

@TimoGlastra TimoGlastra enabled auto-merge (squash) March 17, 2023 13:13
@TimoGlastra TimoGlastra merged commit c17013c into openwallet-foundation:main Mar 17, 2023
karimStekelenburg pushed a commit to karimStekelenburg/aries-framework-javascript that referenced this pull request Mar 17, 2023
@genaris genaris deleted the feat/use-legacy-prover-did branch August 17, 2023 17:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants