-
Notifications
You must be signed in to change notification settings - Fork 23
extract JsonWebKey from @transmute/json-web-signature #617
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ac8d892
extract JsonWebKey from @transmute/json-web-signature
maycon-mello 6dac66e
extract JsonWebKey from @transmute/json-web-signature
maycon-mello ee6101f
Fix lint errors in JsonWebKey and delegation-engine type resolution
maycon-mello ad86bd6
Fix Node ESM interop for @transmute CJS imports in JsonWebKey
maycon-mello 39498ea
Pin did-jwt-cjs to 8.0.17 to keep CJS-compatible @scure/base for cheq…
maycon-mello a46844d
handle PR review
maycon-mello 84a5168
handle PR feedback
maycon-mello e728088
update infura API key
maycon-mello File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,154 @@ | ||
| // Local copy of JsonWebKey from @transmute/json-web-signature@0.7.0-unstable.82, | ||
| // extracted to drop that package's broken jsonld@5 -> @digitalbazaar/http-client@1 | ||
| // -> esm dependency chain (JsonWebKey itself never touches it). | ||
| // 0.7.0-unstable.82 is the `latest` npm dist-tag for all @transmute packages — | ||
| // no stable release exists, so there is no newer version to upgrade to. The | ||
| // four @transmute deps are pinned exactly to keep them in sync with this copy. | ||
| // These @transmute packages are CommonJS. Named ESM imports off them break | ||
| // under Node's native loader (the examples-with-node CI job); a default import | ||
| // breaks under babel/jest. `import * as` resolves to a namespace exposing the | ||
| // named members in both, so destructure off that. | ||
| // Deviations from upstream: getSigner reads `alg` from the exported JWK | ||
| // (matching getVerifier) instead of `k.alg`, and useJwa restores the original | ||
| // signer/verifier factories so it can be re-applied with new options. | ||
| import crypto from 'crypto'; | ||
| import * as ed25519 from '@transmute/ed25519-key-pair'; | ||
| import * as secp256k1 from '@transmute/secp256k1-key-pair'; | ||
| import * as joseLd from '@transmute/jose-ld'; | ||
| import * as webCrypto from '@transmute/web-crypto-key-pair'; | ||
|
|
||
| const { Ed25519KeyPair } = ed25519; | ||
| const { Secp256k1KeyPair } = secp256k1; | ||
| const { JWS } = joseLd; | ||
| const { WebCryptoKey } = webCrypto; | ||
|
|
||
| const getKeyPairForKtyAndCrv = (kty, crv) => { | ||
| if (kty === 'OKP' && crv === 'Ed25519') { | ||
| return Ed25519KeyPair; | ||
| } | ||
| if (kty === 'EC' && crv === 'secp256k1') { | ||
| return Secp256k1KeyPair; | ||
| } | ||
| if (kty === 'EC' && ['P-256', 'P-384', 'P-521'].includes(crv)) { | ||
| return WebCryptoKey; | ||
| } | ||
| throw new Error(`getKeyPairForKtyAndCrv does not support: ${kty} and ${crv}`); | ||
| }; | ||
|
|
||
| // Maps a JWK (kty, crv, alg) to the [keySuite, jwsAlg] pair passed to jose-ld. | ||
| const jwaFor = (kty, crv, alg) => { | ||
| if (kty === 'OKP' && crv === 'Ed25519') { | ||
| return ['EdDsa', 'EdDSA']; | ||
| } | ||
| if (kty === 'EC' && crv === 'secp256k1') { | ||
| return alg === 'ES256K-R' | ||
| ? ['EcRecover', 'ES256K-R'] | ||
| : ['Ecdsa', 'ES256K']; | ||
| } | ||
| if (kty === 'EC' && crv === 'P-256') return ['Ecdsa', 'ES256']; | ||
| if (kty === 'EC' && crv === 'P-384') return ['Ecdsa', 'ES384']; | ||
| if (kty === 'EC' && crv === 'P-521') return ['Ecdsa', 'ES512']; | ||
| return null; | ||
| }; | ||
|
|
||
| const getKeyPairForType = (k) => { | ||
| if (k.type === 'JsonWebKey2020') { | ||
| return getKeyPairForKtyAndCrv(k.publicKeyJwk.kty, k.publicKeyJwk.crv); | ||
| } | ||
| if (k.type === 'Ed25519VerificationKey2018') { | ||
| return Ed25519KeyPair; | ||
| } | ||
| if (k.type === 'EcdsaSecp256k1VerificationKey2019') { | ||
| return Secp256k1KeyPair; | ||
| } | ||
|
|
||
| if (['P256Key2021', 'P384Key2021', 'P521Key2021'].includes(k.type)) { | ||
| return WebCryptoKey; | ||
| } | ||
|
|
||
| throw new Error(`getKeyPairForType does not support type: ${k.type}`); | ||
| }; | ||
|
|
||
| const getVerifier = async (k, options = { detached: true }) => { | ||
| const { publicKeyJwk } = await k.export({ type: 'JsonWebKey2020' }); | ||
| const { kty, crv, alg } = publicKeyJwk; | ||
| const jwa = jwaFor(kty, crv, alg); | ||
| if (!jwa) { | ||
| throw new Error( | ||
| `getVerifier does not support ${JSON.stringify(publicKeyJwk, null, 2)}`, | ||
| ); | ||
| } | ||
| const [keySuite, jwsAlg] = jwa; | ||
| return JWS.createVerifier(k.verifier(keySuite), jwsAlg, options); | ||
| }; | ||
|
|
||
| const getSigner = async (k, options = { detached: true }) => { | ||
| const { publicKeyJwk } = await k.export({ type: 'JsonWebKey2020' }); | ||
| const { kty, crv, alg } = publicKeyJwk; | ||
| const jwa = jwaFor(kty, crv, alg); | ||
| if (!jwa) { | ||
| throw new Error( | ||
| `getSigner does not support ${JSON.stringify(publicKeyJwk, null, 2)}`, | ||
| ); | ||
| } | ||
| const [keySuite, jwsAlg] = jwa; | ||
| return JWS.createSigner(k.signer(keySuite), jwsAlg, options); | ||
| }; | ||
|
|
||
| const applyJwa = async (k, options) => { | ||
| const verifier = await getVerifier(k, options); | ||
| // eslint-disable-next-line no-param-reassign | ||
| k.verifier = () => verifier; | ||
| if (k.privateKey) { | ||
| const signer = await getSigner(k, options); | ||
| // eslint-disable-next-line no-param-reassign | ||
| k.signer = () => signer; | ||
| } | ||
| return k; | ||
| }; | ||
|
|
||
| const useJwa = async (k, options) => { | ||
| // applyJwa replaces `verifier`/`signer` with 0-arg wrappers, so keep the | ||
| // original factories and restore them before re-applying with new options. | ||
| const { verifier, signer } = k; | ||
| // eslint-disable-next-line no-param-reassign | ||
| k.useJwa = async (opts) => { | ||
| // eslint-disable-next-line no-param-reassign | ||
| k.verifier = verifier; | ||
| // eslint-disable-next-line no-param-reassign | ||
| k.signer = signer; | ||
| return applyJwa(k, opts); | ||
| }; | ||
| return applyJwa(k, options); | ||
| }; | ||
|
|
||
| export class JsonWebKey { | ||
| static generate = async ( | ||
| options = { | ||
| kty: 'OKP', | ||
| crv: 'Ed25519', | ||
| detached: true, | ||
| }, | ||
| ) => { | ||
| const KeyPair = getKeyPairForKtyAndCrv(options.kty, options.crv); | ||
| const secureRandom = options.secureRandom || (() => crypto.randomBytes(32)); | ||
| const kp = await KeyPair.generate({ | ||
| kty: options.kty, | ||
| crvOrSize: options.crv, | ||
| secureRandom, | ||
| }); | ||
| const { detached } = options; | ||
| return useJwa(kp, { detached }); | ||
| }; | ||
|
maycon-mello marked this conversation as resolved.
|
||
|
|
||
| static from = async (k, options = { detached: true }) => { | ||
| const KeyPair = getKeyPairForType(k); | ||
| const kp = await KeyPair.from(k); | ||
| let { detached } = options; | ||
| const { header } = options; | ||
| if (detached === undefined) { | ||
| detached = true; | ||
| } | ||
| return useJwa(kp, { detached, header }); | ||
| }; | ||
| } | ||
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.