-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
doc: add examples of json serialization
- Loading branch information
Showing
3 changed files
with
131 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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,57 @@ | ||
import { FlattenJSON, SDJwtInstance } from '@sd-jwt/core'; | ||
import type { DisclosureFrame } from '@sd-jwt/types'; | ||
import { createSignerVerifier, digest, generateSalt, ES256 } from './utils'; | ||
|
||
(async () => { | ||
const { signer, verifier } = await createSignerVerifier(); | ||
|
||
// Create SDJwt instance for use | ||
const sdjwt = new SDJwtInstance({ | ||
signer, | ||
signAlg: ES256.alg, | ||
verifier, | ||
hasher: digest, | ||
saltGenerator: generateSalt, | ||
kbSigner: signer, | ||
kbSignAlg: ES256.alg, | ||
kbVerifier: verifier, | ||
}); | ||
const claims = { | ||
firstname: 'John', | ||
lastname: 'Doe', | ||
ssn: '123-45-6789', | ||
id: '1234', | ||
}; | ||
const disclosureFrame: DisclosureFrame<typeof claims> = { | ||
_sd: ['firstname', 'id'], | ||
}; | ||
|
||
const kbPayload = { | ||
iat: Math.floor(Date.now() / 1000), | ||
aud: 'https://example.com', | ||
nonce: '1234', | ||
custom: 'data', | ||
}; | ||
|
||
const encodedSdjwt = await sdjwt.issue(claims, disclosureFrame); | ||
console.log('encodedSdjwt:', encodedSdjwt); | ||
|
||
const flattenJSON = FlattenJSON.fromEncode(encodedSdjwt); | ||
console.log('flattenJSON(credential): ', flattenJSON.toJson()); | ||
|
||
const presentedSdJwt = await sdjwt.present<typeof claims>( | ||
encodedSdjwt, | ||
{ id: true }, | ||
{ | ||
kb: { | ||
payload: kbPayload, | ||
}, | ||
}, | ||
); | ||
|
||
const flattenPresentationJSON = FlattenJSON.fromEncode(presentedSdJwt); | ||
console.log('flattenJSON(presentation): ', flattenPresentationJSON.toJson()); | ||
|
||
const verified = await sdjwt.verify(presentedSdJwt, ['id', 'ssn'], true); | ||
console.log(verified); | ||
})(); |
This file contains 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,71 @@ | ||
import { GeneralJSON, SDJwtInstance } from '@sd-jwt/core'; | ||
import type { DisclosureFrame } from '@sd-jwt/types'; | ||
import { createSignerVerifier, digest, generateSalt, ES256 } from './utils'; | ||
|
||
(async () => { | ||
const { signer, verifier } = await createSignerVerifier(); | ||
|
||
// Create SDJwt instance for use | ||
const sdjwt = new SDJwtInstance({ | ||
signer, | ||
signAlg: ES256.alg, | ||
verifier, | ||
hasher: digest, | ||
saltGenerator: generateSalt, | ||
kbSigner: signer, | ||
kbSignAlg: ES256.alg, | ||
kbVerifier: verifier, | ||
}); | ||
const claims = { | ||
firstname: 'John', | ||
lastname: 'Doe', | ||
ssn: '123-45-6789', | ||
id: '1234', | ||
}; | ||
const disclosureFrame: DisclosureFrame<typeof claims> = { | ||
_sd: ['firstname', 'id'], | ||
}; | ||
|
||
const kbPayload = { | ||
iat: Math.floor(Date.now() / 1000), | ||
aud: 'https://example.com', | ||
nonce: '1234', | ||
custom: 'data', | ||
}; | ||
|
||
const encodedSdjwt = await sdjwt.issue(claims, disclosureFrame); | ||
console.log('encodedSdjwt:', encodedSdjwt); | ||
|
||
const generalJSON = GeneralJSON.fromEncode(encodedSdjwt); | ||
console.log('flattenJSON(credential): ', generalJSON.toJson()); | ||
|
||
const presentedSdJwt = await sdjwt.present<typeof claims>( | ||
encodedSdjwt, | ||
{ id: true }, | ||
{ | ||
kb: { | ||
payload: kbPayload, | ||
}, | ||
}, | ||
); | ||
|
||
const generalPresentationJSON = GeneralJSON.fromEncode(presentedSdJwt); | ||
|
||
await generalPresentationJSON.addSignature( | ||
{ | ||
alg: 'ES256', | ||
typ: 'sd+jwt', | ||
kid: 'key-1', | ||
}, | ||
signer, | ||
'key-1', | ||
); | ||
|
||
console.log( | ||
'flattenJSON(presentation): ', | ||
JSON.stringify(generalPresentationJSON.toJson(), null, 2), | ||
); | ||
|
||
const verified = await sdjwt.verify(presentedSdJwt, ['id', 'ssn'], true); | ||
console.log(verified); | ||
})(); |
This file contains 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