|
| 1 | +import { type CborDecodeOptions, cborDecode, cborEncode } from '../../cbor' |
| 2 | +import type { MdocContext } from '../../context' |
| 3 | +import { Handover } from './handover' |
| 4 | + |
| 5 | +export type Oid4vpDraft18HandoverStructure = [Uint8Array, Uint8Array, string] |
| 6 | + |
| 7 | +export type Oid4vpDraft18HandoverOptions = { |
| 8 | + mdocGeneratedNonce?: string |
| 9 | + clientId?: string |
| 10 | + responseUri?: string |
| 11 | + nonce: string |
| 12 | + |
| 13 | + clientIdHash?: Uint8Array |
| 14 | + responseUriHash?: Uint8Array |
| 15 | +} |
| 16 | + |
| 17 | +/** |
| 18 | + * |
| 19 | + * @note this will be removed as it is already a legacy handover structure |
| 20 | + * |
| 21 | + */ |
| 22 | +export class Oid4vpDraft18Handover extends Handover { |
| 23 | + public mdocGeneratedNonce?: string |
| 24 | + public clientId?: string |
| 25 | + public responseUri?: string |
| 26 | + public nonce: string |
| 27 | + |
| 28 | + public clientIdHash?: Uint8Array |
| 29 | + public responseUriHash?: Uint8Array |
| 30 | + |
| 31 | + public constructor(options: Oid4vpDraft18HandoverOptions) { |
| 32 | + super() |
| 33 | + this.mdocGeneratedNonce = options.mdocGeneratedNonce |
| 34 | + this.clientId = options.clientId |
| 35 | + this.responseUri = options.responseUri |
| 36 | + this.nonce = options.nonce |
| 37 | + |
| 38 | + this.clientIdHash = options.clientIdHash |
| 39 | + this.responseUriHash = options.responseUriHash |
| 40 | + } |
| 41 | + |
| 42 | + public async prepare(ctx: Pick<MdocContext, 'crypto'>) { |
| 43 | + if ( |
| 44 | + (!this.mdocGeneratedNonce || !this.clientId || !this.responseUri) && |
| 45 | + (!this.clientIdHash || !this.responseUriHash) |
| 46 | + ) { |
| 47 | + throw new Error( |
| 48 | + 'Either the responseUriHash and clientIdHash must be set or the clientId, responseUri and mdocGeneratedNonce' |
| 49 | + ) |
| 50 | + } |
| 51 | + |
| 52 | + if (this.clientId && this.mdocGeneratedNonce) { |
| 53 | + this.clientIdHash = await ctx.crypto.digest({ |
| 54 | + digestAlgorithm: 'SHA-256', |
| 55 | + bytes: cborEncode([this.clientId, this.mdocGeneratedNonce]), |
| 56 | + }) |
| 57 | + } |
| 58 | + |
| 59 | + if (this.responseUri && this.mdocGeneratedNonce) { |
| 60 | + this.responseUriHash = await ctx.crypto.digest({ |
| 61 | + digestAlgorithm: 'SHA-256', |
| 62 | + bytes: cborEncode([this.responseUri, this.mdocGeneratedNonce]), |
| 63 | + }) |
| 64 | + } |
| 65 | + |
| 66 | + if (!this.clientIdHash || !this.responseUriHash) { |
| 67 | + throw new Error( |
| 68 | + 'Could not hash the client id and/or the response uri. Make sure the properties are set on the class, or manually provide the hashed client id and response uri with the mdoc generated nonce' |
| 69 | + ) |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + public encodedStructure(): Oid4vpDraft18HandoverStructure { |
| 74 | + if (!this.clientIdHash || !this.responseUriHash) { |
| 75 | + throw new Error('Call `prepare` first to create the hash over the client id and response uri') |
| 76 | + } |
| 77 | + |
| 78 | + return [this.clientIdHash, this.responseUriHash, this.nonce] |
| 79 | + } |
| 80 | + |
| 81 | + public static override fromEncodedStructure(encodedStructure: Oid4vpDraft18HandoverStructure): Oid4vpDraft18Handover { |
| 82 | + return new Oid4vpDraft18Handover({ |
| 83 | + clientIdHash: encodedStructure[0], |
| 84 | + responseUriHash: encodedStructure[1], |
| 85 | + nonce: encodedStructure[2], |
| 86 | + }) |
| 87 | + } |
| 88 | + |
| 89 | + public static override decode(bytes: Uint8Array, options?: CborDecodeOptions): Oid4vpDraft18Handover { |
| 90 | + const structure = cborDecode<Oid4vpDraft18HandoverStructure>(bytes, { ...(options ?? {}), mapsAsObjects: false }) |
| 91 | + return Oid4vpDraft18Handover.fromEncodedStructure(structure) |
| 92 | + } |
| 93 | + |
| 94 | + public static isCorrectHandover(structure: unknown): structure is Oid4vpDraft18HandoverStructure { |
| 95 | + return ( |
| 96 | + Array.isArray(structure) && |
| 97 | + structure[0] instanceof Uint8Array && |
| 98 | + structure[1] instanceof Uint8Array && |
| 99 | + typeof structure[2] === 'string' |
| 100 | + ) |
| 101 | + } |
| 102 | +} |
0 commit comments