-
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 1 commit
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,18 @@ | ||
| export interface JWT { | ||
| kty: string | ||
| d: string | ||
| crv: string | ||
| kid: string | ||
| x: string | ||
| } | ||
|
|
||
| export interface IssuerKey { | ||
| type: string | ||
| jwk: JWT | ||
| } | ||
|
|
||
| export interface SignedCredential { | ||
| jws: string | ||
| header: Record<string, any> | ||
| issuer: string | ||
| } |
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,87 @@ | ||
| import { base64url, importJWK, JWTPayload, SignJWT } from 'jose' | ||
| import axios from 'axios' | ||
| import { ethers } from 'ethers' | ||
| import { IssuerKey, 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 {string} privateKey - The private 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, | ||
| privateKey: string | ||
| ): Promise<SignedCredential> { | ||
| try { | ||
| const wallet = new ethers.Wallet(privateKey) | ||
| const privateKeyBuffer = Buffer.from(privateKey.substring(2), 'hex') | ||
| const publicKeyHex = wallet._signingKey().publicKey | ||
| const publicKeyBuffer = Buffer.from(publicKeyHex.substring(2), 'hex') | ||
|
|
||
| // Extract x and y coordinates from the public key buffer | ||
| const xBuffer = publicKeyBuffer.slice(1, 33) | ||
| const yBuffer = publicKeyBuffer.slice(33, 65) | ||
|
|
||
| // Base64url-encode the values | ||
| const d = base64url.encode(privateKeyBuffer as any as Uint8Array) | ||
| const x = base64url.encode(xBuffer as any as Uint8Array) | ||
| const y = base64url.encode(yBuffer as any as Uint8Array) | ||
|
|
||
| const privateJwk = { | ||
| kty: 'EC', | ||
| crv: 'secp256k1', | ||
| d, | ||
| x, | ||
| y, | ||
| alg: 'ES256K', | ||
| use: 'sig' | ||
| } | ||
|
|
||
| const key = await importJWK(privateJwk, 'ES256K') | ||
|
|
||
| const jws = await new SignJWT(verifiableCredential as unknown as JWTPayload) | ||
| .setProtectedHeader({ alg: 'ES256K' }) | ||
| .setIssuedAt() | ||
| .setIssuer(publicKeyHex) | ||
| .sign(key) | ||
| const header = { alg: 'ES256K' } | ||
|
|
||
| return { jws, header, issuer: publicKeyHex } | ||
| } catch (error) { | ||
| console.error('Error signing credential:', 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
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?