Skip to content

Commit

Permalink
separate encoding and creation of disclosures
Browse files Browse the repository at this point in the history
Signed-off-by: Berend Sliedrecht <[email protected]>
  • Loading branch information
berendsliedrecht committed Oct 6, 2023
1 parent 6cdcbf5 commit 7afb1d0
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 17 deletions.
22 changes: 13 additions & 9 deletions src/createDisclosure.ts → src/disclosures.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import { SdJwtError } from './error'
import { Base64url } from './base64url'
import { SdJwtError } from './error'

export type DisclosureItem = [string, string, unknown] | [string, unknown]

export const encodeDisclosure = (item: DisclosureItem) =>
Base64url.encode(JSON.stringify(item))

export const createObjectDisclosure = (
salt: string,
key: string,
value: unknown
) => {
): DisclosureItem => {
if (typeof value === 'number' && isNaN(value)) {
throw new SdJwtError('NaN is not allowed to be used in a disclosure.')
}
Expand All @@ -16,12 +21,13 @@ export const createObjectDisclosure = (
)
}

const disclosure = [salt, key, value]

return Base64url.encode(JSON.stringify(disclosure))
return [salt, key, value]
}

export const createArrayDisclosure = (salt: string, value: unknown) => {
export const createArrayDisclosure = (
salt: string,
value: unknown
): DisclosureItem => {
if (typeof value === 'number' && isNaN(value)) {
throw new SdJwtError('NaN is not allowed to be used in a disclosure.')
}
Expand All @@ -32,7 +38,5 @@ export const createArrayDisclosure = (salt: string, value: unknown) => {
)
}

const disclosure = [salt, value]

return Base64url.encode(JSON.stringify(disclosure))
return [salt, value]
}
5 changes: 3 additions & 2 deletions src/sdJwt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { SdJwtError } from './error'
import { HasherAlgorithm } from './hasherAlgorithm'
import { deleteByPath } from './util'
import { SaltGenerator, createDecoys } from './createDecoys'
import { createObjectDisclosure } from './createDisclosure'
import { createObjectDisclosure, encodeDisclosure } from './disclosures'
import { HasherAndAlgorithm, hashDisclosure } from './hashDisclosure'

type ReturnSdJwtWithHeaderAndPayload<T extends SdJwt> = MakePropertyRequired<
Expand Down Expand Up @@ -220,8 +220,9 @@ export class SdJwt<

const salt = await this.saltGenerator!()
const disclosure = createObjectDisclosure(salt, key, value)
const encodedDisclosure = encodeDisclosure(disclosure)

return disclosure
return encodedDisclosure
}

private async hashDisclosure(disclosure: string): Promise<string> {
Expand Down
17 changes: 11 additions & 6 deletions tests/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import assert from 'node:assert'
import { hashDisclosure } from '../src/hashDisclosure'
import {
createArrayDisclosure,
createObjectDisclosure
} from '../src/createDisclosure'
createObjectDisclosure,
encodeDisclosure
} from '../src/disclosures'

export const prelude = () => {
const oldStringify = JSON.stringify
Expand All @@ -23,9 +24,11 @@ export const testCreateDisclosureObjectAndHash = async (
) => {
const disclosure = createObjectDisclosure(...input)

assert.strictEqual(disclosure, expectedDisclosure)
const encodedDisclosure = encodeDisclosure(disclosure)

const hash = await hashDisclosure(disclosure, hasher)
assert.strictEqual(encodedDisclosure, expectedDisclosure)

const hash = await hashDisclosure(encodedDisclosure, hasher)

assert.strictEqual(hash, expectedHash)
}
Expand All @@ -37,9 +40,11 @@ export const testCreateDisclosureArrayAndHash = async (
) => {
const disclosure = createArrayDisclosure(...input)

assert.strictEqual(disclosure, expectedDisclosure)
const encodedDisclosure = encodeDisclosure(disclosure)

assert.strictEqual(encodedDisclosure, expectedDisclosure)

const hash = await hashDisclosure(disclosure, hasher)
const hash = await hashDisclosure(encodedDisclosure, hasher)

assert.strictEqual(hash, expectedHash)
}

0 comments on commit 7afb1d0

Please sign in to comment.