Skip to content

Commit

Permalink
fix: types
Browse files Browse the repository at this point in the history
  • Loading branch information
AdriGeorge committed Nov 20, 2024
1 parent 1250703 commit a221a83
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 44 deletions.
59 changes: 41 additions & 18 deletions src/@types/IssuerSignature.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,51 @@
/**
* Represents a JSON Web Token (JWT) used in cryptographic operations.
*/
export interface JWT {
kty: string
d: string
crv: string
kid: string
x: string
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
jwk: JWT
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
header: Record<string, any>
issuer: string
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
}

export interface IssuerKeyJWK {
kty: string
crv: string
d: string
x: string
y: string
alg: string
use: string
/**
* 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 {}
32 changes: 10 additions & 22 deletions src/utils/SignDDO.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { base64url, importJWK, JWTPayload, jwtVerify, SignJWT } from 'jose'
import { importJWK, JWTPayload, jwtVerify, SignJWT } from 'jose'
import axios from 'axios'
import { IssuerKey, IssuerKeyJWK, SignedCredential } from '../@types/IssuerSignature'
import {
IssuerKey,
IssuerKeyJWK,
IssuerPublicKeyJWK,
SignedCredential
} from '../@types/IssuerSignature'

/**
* Signs a verifiable credential using Walt.id's issuer API.
Expand Down Expand Up @@ -66,32 +71,15 @@ export async function signCredential(
/**
* Verifies a verifiable credential's JWS using the issuer's public key.
* @param {string} jws - The JSON Web Signature (JWS) to verify.
* @param {string} issuerPublicKey - The public key of the issuer in hexadecimal format.
* @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,
issuerPublicKey: string
issuerPublicKeyJWK: IssuerPublicKeyJWK
): Promise<JWTPayload> {
const publicKeyBuffer = Buffer.from(issuerPublicKey.substring(2), 'hex')
const xBuffer = publicKeyBuffer.slice(1, 33)
const yBuffer = publicKeyBuffer.slice(33, 65)

const x = base64url.encode(xBuffer as any as Uint8Array)
const y = base64url.encode(yBuffer as any as Uint8Array)

const publicJwk = {
kty: 'EC',
crv: 'secp256k1',
x,
y,
alg: 'ES256K',
use: 'sig'
}

const key = await importJWK(publicJwk, 'ES256K')

const key = await importJWK(issuerPublicKeyJWK, 'ES256K')
try {
const { payload } = await jwtVerify(jws, key)
return payload
Expand Down
31 changes: 27 additions & 4 deletions test/unit/SignDDO.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { assert } from 'chai'
import { ethers } from 'ethers'
import { base64url } from 'jose'
import { signCredential, verifyCredential } from '../../src/utils'
import { IssuerKeyJWK } from '../../src/@types/IssuerSignature'
import { IssuerKeyJWK, IssuerPublicKeyJWK } from '../../src/@types/IssuerSignature'

const mockVerifiableCredential = {
'@context': ['https://www.w3.org/2018/credentials/v1'],
Expand All @@ -18,7 +18,6 @@ describe('Credential Signing and Verification Functions', () => {
const privateKey =
'0xc494c6e5def4bab63ac29eed19a134c130355f74f019bc74b8f4389df2837a57'
const wallet = new ethers.Wallet(privateKey)
const { publicKey } = wallet._signingKey()
const privateKeyBuffer = Buffer.from(privateKey.substring(2), 'hex')
const publicKeyHex = wallet._signingKey().publicKey
const publicKeyBuffer = Buffer.from(publicKeyHex.substring(2), 'hex')
Expand All @@ -44,7 +43,16 @@ describe('Credential Signing and Verification Functions', () => {
publicKeyHex
)

const payload = await verifyCredential(jws, publicKey)
const publicJwk = {
kty: 'EC',
crv: 'secp256k1',
x,
y,
alg: 'ES256K',
use: 'sig'
}

const payload = await verifyCredential(jws, publicJwk)
assert.deepEqual(
{
type: payload.type,
Expand Down Expand Up @@ -95,8 +103,23 @@ describe('Credential Signing and Verification Functions', () => {
publicKey
)

const invalidPublicKeyBuffer = Buffer.from(invalidPublicKey.substring(2), 'hex')
const invalidXBuffer = invalidPublicKeyBuffer.slice(1, 33)
const invalidYBuffer = invalidPublicKeyBuffer.slice(33, 65)

const invalidX = base64url.encode(invalidXBuffer as any as Uint8Array)
const invalidY = base64url.encode(invalidYBuffer as any as Uint8Array)
const publicJwk: IssuerPublicKeyJWK = {
kty: 'EC',
crv: 'secp256k1',
x: invalidX,
y: invalidY,
alg: 'ES256K',
use: 'sig'
}

try {
await verifyCredential(jws, invalidPublicKey)
await verifyCredential(jws, publicJwk)
assert.fail('Expected error to be thrown')
} catch (error) {
assert.include(
Expand Down

0 comments on commit a221a83

Please sign in to comment.