Skip to content

Commit bea846b

Browse files
authored
refactor: split async getData method on x509 certificate (#2180)
1 parent 589fc40 commit bea846b

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed

.changeset/eleven-deers-rush.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@credo-ts/core': minor
3+
---
4+
5+
refactor: split async `getData` method on x509 certificate to sync `.data` getter and async `getThumbprint` method

packages/core/src/modules/mdoc/MdocContext.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,10 @@ export const getMdocContext = (agentContext: AgentContext): MdocContext => {
105105
getCertificateData: async (input) => {
106106
const { certificate } = input
107107
const x509Certificate = X509Certificate.fromRawCertificate(certificate)
108-
return x509Certificate.getData(crypto)
108+
return {
109+
...x509Certificate.data,
110+
thumbprint: await x509Certificate.getThumprint(agentContext),
111+
}
109112
},
110113
} satisfies X509Context,
111114
}

packages/core/src/modules/x509/X509Certificate.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { X509CreateSelfSignedCertificateOptions } from './X509ServiceOptions'
2-
import type { CredoWebCrypto } from '../../crypto/webcrypto'
2+
import type { AgentContext } from '../../agent'
33

44
import { AsnParser } from '@peculiar/asn1-schema'
55
import {
@@ -14,7 +14,7 @@ import * as x509 from '@peculiar/x509'
1414
import { Key } from '../../crypto/Key'
1515
import { KeyType } from '../../crypto/KeyType'
1616
import { compress } from '../../crypto/jose/jwk/ecCompression'
17-
import { CredoWebCryptoKey } from '../../crypto/webcrypto'
17+
import { CredoWebCrypto, CredoWebCryptoKey } from '../../crypto/webcrypto'
1818
import { credoKeyTypeIntoCryptoKeyAlgorithm, spkiAlgorithmIntoCredoKeyType } from '../../crypto/webcrypto/utils'
1919
import { TypedArrayEncoder } from '../../utils'
2020

@@ -271,16 +271,30 @@ export class X509Certificate {
271271
}
272272
}
273273

274-
public async getData(crypto?: CredoWebCrypto) {
274+
/**
275+
* Get the thumprint of the X509 certificate in hex format.
276+
*/
277+
public async getThumprint(agentContext: AgentContext) {
275278
const certificate = new x509.X509Certificate(this.rawCertificate)
276279

277-
const thumbprint = await certificate.getThumbprint(crypto)
280+
const thumbprint = await certificate.getThumbprint(new CredoWebCrypto(agentContext))
278281
const thumbprintHex = TypedArrayEncoder.toHex(new Uint8Array(thumbprint))
282+
283+
return thumbprintHex
284+
}
285+
286+
/**
287+
* Get the data elements of the x509 certificate
288+
*/
289+
public get data() {
290+
const certificate = new x509.X509Certificate(this.rawCertificate)
291+
279292
return {
280293
issuerName: certificate.issuerName.toString(),
294+
issuer: certificate.issuer,
281295
subjectName: certificate.subjectName.toString(),
296+
subject: certificate.subject,
282297
serialNumber: certificate.serialNumber,
283-
thumbprint: thumbprintHex,
284298
pem: certificate.toString(),
285299
notBefore: certificate.notBefore,
286300
notAfter: certificate.notAfter,

0 commit comments

Comments
 (0)