Skip to content

Commit

Permalink
readme
Browse files Browse the repository at this point in the history
  • Loading branch information
OR13 committed Feb 3, 2024
1 parent 54a23bd commit 0b048fa
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 1 deletion.
62 changes: 62 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,68 @@ npm i @transmute/verifiable-credentials@latest --save
import * as transmute from "@transmute/verifiable-credentials";
```

```ts
const privateKey = await transmute.key.generate({
alg: "ES384",
cty: "application/jwk+json",
});

const publicKey = await transmute.key.publicFromPrivate({
cty: "application/jwk+json",
content: privateKey,
});

const vc = await transmute
.issuer({
alg: "ES384",
iss: "https://university.example/issuers/565049",
kid: "key-42",
cty: "application/vc+ld+json+jwt",
privateKey: {
cty: "application/jwk+json",
content: privateKey,
},
})
.issue({
claimset: new TextEncoder().encode(
JSON.stringify({
"@context": [
"https://www.w3.org/ns/credentials/v2",
"https://www.w3.org/ns/credentials/examples/v2",
],
id: "https://university.example/credentials/1872",
type: ["VerifiableCredential", "ExampleAlumniCredential"],
issuer: {
id: "https://university.example/issuers/565049",
},
validFrom: "2010-01-01T19:23:24Z",
credentialSubject: {
id: "did:example:ebfeb1f712ebc6f1c276e12ec21",
alumniOf: {
id: "did:example:c276e12ec21ebfeb1f712ebc6f1",
name: "Example University",
},
},
})
),
});
const verified = await cr1
.verifier({
resolver: {
resolve: async () => {
return {
cty: "application/jwk+json",
content: publicKey,
};
},
},
})
.verify<transmute.VerifiableCredentialWithIssuerObject>({
cty: "application/vc+ld+json+jwt",
content: vc,
});
```

## Develop

```bash
Expand Down
4 changes: 3 additions & 1 deletion src/cr1/key/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export * from './generate'
export * from './importJWK'
export * from './importKeyLike'
export * from './importKeyLike'

export * from './publicFromPrivate'
29 changes: 29 additions & 0 deletions src/cr1/key/publicFromPrivate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@



import * as cose from '@transmute/cose'
import * as jose from 'jose'

import { SupportedKeyFormats } from '../types'

import { encoder, decoder } from '../text'

export const publicFromPrivate = async (
key: {
cty: SupportedKeyFormats,
content: Uint8Array
}) => {
if (key.cty === 'application/jwk+json') {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { d, ...publicKeyJwk } = JSON.parse(decoder.decode(key.content))
return encoder.encode(JSON.stringify(publicKeyJwk))
} else if (key.cty === 'application/cose-key') {
const coseKey = cose.cbor.decode(key.content)
const jwk = await cose.key.convertCoseKeyToJsonWebKey<jose.JWK>(coseKey)
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { d, ...publicKeyJwk } = jwk
const publicKey = cose.key.convertJsonWebKeyToCoseKey(publicKeyJwk)
return cose.cbor.encode(publicKey)
}
throw new Error('Unsupported key type.')
}
15 changes: 15 additions & 0 deletions test/w3c-cr-1/0-keys.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,21 @@ it('has version', () => {
expect(cr1.version).toBe('https://www.w3.org/TR/2024/CR-vc-data-model-2.0-20240201/')
})

it('publicFromPrivate', async () => {
const privateKey = await cr1.key.generate({
alg: 'ES384',
cty: 'application/jwk+json'
})
const publicKey = await cr1.key.publicFromPrivate({
cty: 'application/jwk+json',
content: privateKey
})
const parsed0 = JSON.parse(decoder.decode(privateKey))
delete parsed0.d
const parsed1 = JSON.parse(decoder.decode(publicKey))
expect(parsed0).toEqual(parsed1)
})

describe.skip('key generation', () => {
it('application/jwk+json', async () => {
const k1 = await cr1.key.generate({
Expand Down

0 comments on commit 0b048fa

Please sign in to comment.