-
Notifications
You must be signed in to change notification settings - Fork 57
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
Always set alg and kid on expansion #502
Conversation
|
TBDocs Report ✅ No errors or warnings @web5/api
@web5/crypto
@web5/crypto-aws-kms
@web5/dids
@web5/credentials
TBDocs Report Updated at 2024-05-07T21:17:36Z |
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #502 +/- ##
==========================================
- Coverage 91.03% 90.81% -0.23%
==========================================
Files 116 116
Lines 29562 29439 -123
Branches 2177 2161 -16
==========================================
- Hits 26912 26734 -178
- Misses 2615 2669 +54
- Partials 35 36 +1
|
Co-authored-by: Gabe <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks legit but no need for tests? 😆
@@ -1025,6 +1035,16 @@ export class DidDhtDocument { | |||
// Convert the public key from a byte array to JWK format. | |||
let publicKey = await DidDhtUtils.keyConverter(namedCurve).bytesToPublicKey({ publicKeyBytes }); | |||
|
|||
// Always set the algorithm on did:dht expansion. | |||
publicKey.alg = parsedAlg || getJoseSignatureAlgorithmFromPublicKey(publicKey); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sanity: getJoseSignatureAlgorithmFromPublicKey()
does not seem to return Ed25519
that will be expected of vector 3.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
am I using this correctly?
export function getJoseSignatureAlgorithmFromPublicKey(publicKey: Jwk): string {
const curveToJoseAlgorithm: Record<string, string> = {
'Ed25519' : 'EdDSA',
'P-256' : 'ES256',
'P-384' : 'ES384',
'P-521' : 'ES512',
'secp256k1' : 'ES256K',
};
// If the key contains an `alg` property that matches a JOSE registered algorithm identifier,
// return its value.
if (publicKey.alg && Object.values(curveToJoseAlgorithm).includes(publicKey.alg)) {
return publicKey.alg;
}
// If the key contains a `crv` property, return the corresponding algorithm.
if (publicKey.crv && Object.keys(curveToJoseAlgorithm).includes(publicKey.crv)) {
return curveToJoseAlgorithm[publicKey.crv];
}
throw new Error(
`Unable to determine algorithm based on provided input: alg=${publicKey.alg}, crv=${publicKey.crv}. ` +
`Supported 'alg' values: ${Object.values(curveToJoseAlgorithm).join(', ')}. ` +
`Supported 'crv' values: ${Object.keys(curveToJoseAlgorithm).join(', ')}.`
);
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the alg is always there for our jwks, but is that not necessarily the case for outside jwks inside the did dht??
if the alg is always there we can just do
if(parsedAlg) {
publicKey.alg = parsedAlg
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't get if(parsedAlg) { publicKey.alg = parsedAlg }
, if parsedAlg
is always defined, then there is nothing do right? since:
const { id, t, k, c, a: parsedAlg } = DidDhtUtils.parseTxtDataToObject(answer.data);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
parsed alg will not always be defined, but publicKey.alg is always defined
packages/dids/src/methods/did-dht.ts
Outdated
publicKey.alg = parsedAlg || getJoseSignatureAlgorithmFromPublicKey(publicKey); | ||
|
||
// Determine the Key ID (kid): '0' for the identity key or JWK thumbprint for others. Always set alg on expansion. | ||
if (id !== '0' && publicKey.kid === undefined) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For my understanding: why would kid
be defined when id
is not 0
? May have opportunity to simplify the condition here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
simplifying to:
if (id === '0') {
publicKey.kid = '0';
} else if (publicKey.kid === undefined) {
publicKey.kid = await computeJwkThumbprint({ jwk: publicKey });
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the publicKey.kid should be defined, but if it is not, we can recompute it deterministically with computeJwkThumbprint
Co-authored-by: Henry Tsai <[email protected]>
Co-authored-by: Henry Tsai <[email protected]>
Co-authored-by: Henry Tsai <[email protected]>
Co-authored-by: Henry Tsai <[email protected]>
Co-authored-by: Henry Tsai <[email protected]>
packages/dids/src/methods/did-dht.ts
Outdated
if (id === '0') { | ||
publicKey.kid = '0'; | ||
} else if (publicKey.kid === undefined) { | ||
publicKey.kid = await computeJwkThumbprint({ jwk: publicKey }); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@decentralgabe just confirmed that id
MUST be 0
for identity key, and undefined
otherwise, (we should have tests if they don't exist to make sure these are true in implementation), so it seems to me the condition can simply be:
if (id === '0') { | |
publicKey.kid = '0'; | |
} else if (publicKey.kid === undefined) { | |
publicKey.kid = await computeJwkThumbprint({ jwk: publicKey }); | |
} | |
if (id === undefined) { | |
publicKey.kid = await computeJwkThumbprint({ jwk: publicKey }); | |
} |
Happy to discuss if I am not making sense!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i'm going to update the spec language to always remove the id property
@thehenrytsai correctly pointed out we can discern the 0 key based on the did identifier
all other keys will use the thumbprint
This pr resolves #3 from this issue:
#497
Always set alg and kid on expansion (to the values in the registry), support overriding of alg values
toDnsPacket:
fromDnsPacket: