Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions .github/workflows/examples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ jobs:
CHEQD_MNEMONIC: "steak come surprise obvious remain black trouble measure design volume retreat float coach amused match album moment radio stuff crack orphan ranch dose endorse"
CHEQD_IMAGE_TAG: 4.1.7
CHEQD_NETWORK: "testnet"
INFURA_API_KEY: ${{ secrets.INFURA_API_KEY }}
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v4
Expand All @@ -16,14 +17,15 @@ jobs:
- run: yarn install --frozen-lockfile --ignore-scripts
- run: npm install -g turbo@2.0.5
- run: turbo telemetry disable
- run: CHEQD_MNEMONIC=$CHEQD_MNEMONIC CHEQD_IMAGE_TAG=$CHEQD_IMAGE_TAG CHEQD_NETWORK=$CHEQD_NETWORK turbo run examples-with-node
- run: CHEQD_MNEMONIC=$CHEQD_MNEMONIC CHEQD_IMAGE_TAG=$CHEQD_IMAGE_TAG CHEQD_NETWORK=$CHEQD_NETWORK INFURA_API_KEY=$INFURA_API_KEY turbo run examples-with-node

mainnet:
runs-on: ubuntu-latest
env:
CHEQD_MNEMONIC: "steak come surprise obvious remain black trouble measure design volume retreat float coach amused match album moment radio stuff crack orphan ranch dose endorse"
CHEQD_IMAGE_TAG: 4.1.7
CHEQD_NETWORK: "mainnet"
INFURA_API_KEY: ${{ secrets.INFURA_API_KEY }}
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v4
Expand All @@ -32,4 +34,4 @@ jobs:
- run: yarn install --frozen-lockfile --ignore-scripts
- run: npm install -g turbo@2.0.5
- run: turbo telemetry disable
- run: CHEQD_MNEMONIC=$CHEQD_MNEMONIC CHEQD_IMAGE_TAG=$CHEQD_IMAGE_TAG CHEQD_NETWORK=$CHEQD_NETWORK turbo run examples-with-node
- run: CHEQD_MNEMONIC=$CHEQD_MNEMONIC CHEQD_IMAGE_TAG=$CHEQD_IMAGE_TAG CHEQD_NETWORK=$CHEQD_NETWORK INFURA_API_KEY=$INFURA_API_KEY turbo run examples-with-node
2 changes: 2 additions & 0 deletions examples/src/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ export const faucet = {

export const url = process.env.CHEQD_RPC_URL || 'http://localhost:26657';
export const network = process.env.CHEQD_NETWORK || CheqdNetwork.Testnet;

export const infuraApiKey = process.env.INFURA_API_KEY || '';
6 changes: 4 additions & 2 deletions examples/src/resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ import {
Ed25519Keypair,
DidKeypair,
} from '@docknetwork/credential-sdk/keypairs';
import { faucet, network, url } from './env.js';
import {
faucet, network, url, infuraApiKey,
} from './env.js';

const universalResolverUrl = 'https://uniresolver.io';

Expand All @@ -29,7 +31,7 @@ const ethereumProviderConfig = {
networks: [
{
name: 'mainnet',
rpcUrl: 'https://mainnet.infura.io/v3/05f321c3606e44599c54dbc92510e6a9',
rpcUrl: `https://mainnet.infura.io/v3/${infuraApiKey}`,
},
],
};
Expand Down
12 changes: 5 additions & 7 deletions packages/credential-sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,6 @@
"default": "./dist/esm/*"
}
},
"resolutions": {
"@digitalbazaar/http-client": "^3.4.1",
"jsonld/@digitalbazaar/http-client": "^3.4.1",
"@transmute/jsonld/jsonld/@digitalbazaar/http-client": "^3.4.1"
},
"repository": {
"type": "git",
"url": "https://github.com/docknetwork/sdk"
Expand All @@ -49,7 +44,10 @@
"@juanelas/base64": "^1.1.5",
"@sphereon/pex": "^5.0.0-unstable.28",
"@sphereon/ssi-sdk-ext.did-resolver-jwk": "^0.26.0",
"@transmute/json-web-signature": "^0.7.0-unstable.82",
"@transmute/ed25519-key-pair": "0.7.0-unstable.82",
"@transmute/jose-ld": "0.7.0-unstable.82",
"@transmute/secp256k1-key-pair": "0.7.0-unstable.82",
"@transmute/web-crypto-key-pair": "0.7.0-unstable.82",
"base64url": "^3.0.1",
"blake2b": "2.1.4",
"blakejs": "^1.2.1",
Expand All @@ -59,7 +57,7 @@
"json-canonicalize": "1.0.4",
"json-stringify-deterministic": "^1.0.12",
"jsonld": "^6.0.0",
"jsonld-signatures": "^9.3.1",
"jsonld-signatures": "^10.0.0",
"jsonschema": "1.4.1",
"key-did-resolver": "^1.4.0",
"mrklt": "^0.2.0",
Expand Down
154 changes: 154 additions & 0 deletions packages/credential-sdk/src/vc/crypto/JsonWebKey.js
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;
};
Comment thread
maycon-mello marked this conversation as resolved.

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 });
};
Comment thread
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 });
};
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { JsonWebKey } from '@transmute/json-web-signature';
import { JsonWebKey } from './JsonWebKey';
import CustomLinkedDataSignature from './common/CustomLinkedDataSignature';

const SUITE_CONTEXT_URL = 'https://w3id.org/security/suites/jws-2020/v1';
Expand Down
2 changes: 1 addition & 1 deletion packages/credential-sdk/src/vc/helpers.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import jsonld from 'jsonld';
import { JsonWebKey } from '@transmute/json-web-signature';
import { JsonWebKey } from './crypto/JsonWebKey';
import defaultDocumentLoader from './document-loader';

import {
Expand Down
2 changes: 1 addition & 1 deletion packages/vc-delegation-engine/tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"outDir": "dist/types",
"rootDir": "src",
"types": ["node"],
"typeRoots": ["./types", "./node_modules/@types"]
"typeRoots": ["./types", "./node_modules/@types", "../../node_modules/@types"]
},
"include": ["src/**/*.js", "types/**/*.d.ts"]
}
Expand Down
2 changes: 1 addition & 1 deletion turbo.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"cache": false,
"dependsOn": ["^build"],
"outputs": [],
"env": ["CHEQD_MNEMONIC", "CHEQD_IMAGE_TAG", "CHEQD_NETWORK"]
"env": ["CHEQD_MNEMONIC", "CHEQD_IMAGE_TAG", "CHEQD_NETWORK", "INFURA_API_KEY"]
},
"publish-packages": {
"dependsOn": ["^build"],
Expand Down
Loading
Loading