-
Notifications
You must be signed in to change notification settings - Fork 71
chore: signDDO #1877
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
Closed
Closed
chore: signDDO #1877
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
4e975a6
chore: signDDO
AdriGeorge 367babb
chore: add tests
AdriGeorge 890c697
fix: tests
AdriGeorge fe8950a
fix: tests
AdriGeorge d0436c6
fix: tests
AdriGeorge fbb57b3
fix: tests
AdriGeorge 8789c05
fix: params
AdriGeorge f1ef7e9
fix: params
AdriGeorge 1250703
fix: params
AdriGeorge a221a83
fix: types
AdriGeorge File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| /** | ||
| * Represents a JSON Web Token (JWT) used in cryptographic operations. | ||
| */ | ||
| export interface JWT { | ||
| kty: string // Key type (e.g., 'EC' for Elliptic Curve) | ||
| d: string // Private key (base64url encoded) | ||
| crv: string // Cryptographic curve (e.g., 'secp256k1') | ||
| kid: string // Key ID | ||
| x: string // X-coordinate of the public key (base64url encoded) | ||
| } | ||
|
|
||
| /** | ||
| * Represents a key used by an issuer to sign credentials. | ||
| */ | ||
| export interface IssuerKey { | ||
| type: string // Type of the key (e.g., 'JWK') | ||
| jwk: JWT // The JSON Web Token associated with the issuer's key | ||
| } | ||
|
|
||
| /** | ||
| * Represents the result of signing a credential. | ||
| */ | ||
| export interface SignedCredential { | ||
| jws: string // JSON Web Signature (JWS) of the credential | ||
| header: Record<string, any> // Protected header used in the JWS | ||
| issuer: string // DID or public key of the issuer | ||
| } | ||
|
|
||
| /** | ||
| * Represents the common properties of a JSON Web Key (JWK). | ||
| */ | ||
| interface BaseJWK { | ||
| kty: string // Key type (e.g., 'EC' for Elliptic Curve) | ||
| crv: string // Cryptographic curve (e.g., 'secp256k1') | ||
| x: string // X-coordinate of the public key (base64url encoded) | ||
| y: string // Y-coordinate of the public key (base64url encoded) | ||
| alg: string // Algorithm used (e.g., 'ES256K') | ||
| use: string // Intended use of the key (e.g., 'sig' for signing) | ||
| } | ||
|
|
||
| /** | ||
| * Represents a JSON Web Key (JWK) for private signing operations. | ||
| */ | ||
| export interface IssuerKeyJWK extends BaseJWK { | ||
| d: string // Private key (base64url encoded) | ||
| } | ||
|
|
||
| /** | ||
| * Represents a JSON Web Key (JWK) for public verification operations. | ||
| */ | ||
| export interface IssuerPublicKeyJWK extends BaseJWK {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| import { importJWK, JWTPayload, jwtVerify, SignJWT } from 'jose' | ||
| import axios from 'axios' | ||
| import { | ||
| IssuerKey, | ||
| IssuerKeyJWK, | ||
| IssuerPublicKeyJWK, | ||
| SignedCredential | ||
| } from '../@types/IssuerSignature' | ||
|
|
||
| /** | ||
| * Signs a verifiable credential using Walt.id's issuer API. | ||
| * @param {any} verifiableCredential - The verifiable credential to sign. | ||
| * @param {string} waltIdIssuerApi - URL of the Walt.id issuer API. | ||
| * @param {string} issuerDid - DID of the issuer. | ||
| * @param {IssuerKey} issuerKey - Issuer's key for signing. | ||
| * @returns {Promise<SignedCredential>} - The signed credential's JWS, header, and issuer information. | ||
| * @throws {Error} If the signing process fails. | ||
| */ | ||
| export async function signCredentialWithWaltId( | ||
| verifiableCredential: any, | ||
| waltIdIssuerApi: string, | ||
| issuerDid: string, | ||
| issuerKey: IssuerKey | ||
| ): Promise<SignedCredential> { | ||
| try { | ||
| const response = await axios.post(waltIdIssuerApi, { | ||
| credentialData: verifiableCredential, | ||
| issuerDid, | ||
| issuerKey, | ||
| subjectDid: verifiableCredential.credentialSubject.id | ||
| }) | ||
| const jws = response.data | ||
| const header = { alg: issuerKey.jwk.kty } | ||
| return { jws, header, issuer: issuerDid } | ||
| } catch (error) { | ||
| console.error('Error signing credential with WaltId:', error) | ||
| throw error | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Signs a verifiable credential locally using a private key. | ||
| * @param {any} verifiableCredential - The verifiable credential to sign. | ||
| * @param {IssuerKeyJWK} issuerKeyJWK - the JWK from private key. | ||
| * @param {string} publicKeyHex - the public key | ||
| * @returns {Promise<SignedCredential>} - The signed credential's JWS, header, and issuer information. | ||
| * @throws {Error} If the signing process fails. | ||
| */ | ||
| export async function signCredential( | ||
| verifiableCredential: any, | ||
| issuerKeyJWK: IssuerKeyJWK, | ||
| publicKeyHex: string | ||
| ): Promise<SignedCredential> { | ||
| try { | ||
| const key = await importJWK(issuerKeyJWK, issuerKeyJWK.alg) | ||
|
|
||
| const jws = await new SignJWT(verifiableCredential as unknown as JWTPayload) | ||
| .setProtectedHeader({ alg: issuerKeyJWK.alg }) | ||
| .setIssuedAt() | ||
| .setIssuer(publicKeyHex) | ||
| .sign(key) | ||
| const header = { alg: issuerKeyJWK.alg } | ||
|
|
||
| return { jws, header, issuer: publicKeyHex } | ||
| } catch (error) { | ||
| console.error('Error signing credential:', error) | ||
| throw error | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Verifies a verifiable credential's JWS using the issuer's public key. | ||
| * @param {string} jws - The JSON Web Signature (JWS) to verify. | ||
| * @param {IssuerPublicKeyJWK} issuerPublicKeyJWK - The public key JWK of the issuer. | ||
| * @returns {Promise<JWTPayload>} - The verified payload of the credential. | ||
| * @throws {Error} If the verification fails. | ||
| */ | ||
| export async function verifyCredential( | ||
| jws: string, | ||
| issuerPublicKeyJWK: IssuerPublicKeyJWK | ||
| ): Promise<JWTPayload> { | ||
| const key = await importJWK(issuerPublicKeyJWK, 'ES256K') | ||
| try { | ||
| const { payload } = await jwtVerify(jws, key) | ||
| return payload | ||
| } catch (error) { | ||
| console.error('Verification failed:', error) | ||
| throw error | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Why have you added axios when we already have cross-fetch?