Big News: Socket raises $60M Series C at a $1B valuation to secure software supply chains for AI-driven development.Announcement
Sign In

dids-threads

Package Overview
Dependencies
Maintainers
1
Versions
32
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

dids-threads - npm Package Compare versions

Comparing version
1.0.5
to
1.0.6
+12
dist/common.d.ts
import { DIDResolutionResult } from "did-resolver";
import { CreateJWSOptions, DagJWS, DagJWSResult, GeneralJWS, VerifyJWSOptions, VerifyJWSResult } from "dids";
export interface DidVerification {
createJWS<T extends string | Record<string, any>>(payload: T, options?: CreateJWSOptions): Promise<DagJWS>;
createDagJWS(payload: Record<string, any>, options?: CreateJWSOptions): Promise<DagJWSResult>;
verifyJWS(jws: string | DagJWS, options?: VerifyJWSOptions): Promise<VerifyJWSResult>;
}
export interface DidResolver {
resolve(url: string): Promise<DIDResolutionResult>;
request<T extends string | Record<string, any>>(options: CreateJWSOptions, payload: T): Promise<GeneralJWS>;
}
//# sourceMappingURL=common.d.ts.map
{"version":3,"file":"common.d.ts","sourceRoot":"","sources":["../src/common.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,MAAM,cAAc,CAAA;AAClD,OAAO,EAAE,gBAAgB,EAAE,MAAM,EAAE,YAAY,EAAE,UAAU,EAAE,gBAAgB,EAAE,eAAe,EAAE,MAAM,MAAM,CAAA;AAE5G,MAAM,WAAW,eAAe;IAC5B,SAAS,CAAC,CAAC,SAAS,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAAE,OAAO,EAAE,CAAC,EAAE,OAAO,CAAC,EAAE,gBAAgB,GAAG,OAAO,CAAC,MAAM,CAAC,CAAA;IAC1G,YAAY,CAAC,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAAE,OAAO,CAAC,EAAE,gBAAgB,GAAG,OAAO,CAAC,YAAY,CAAC,CAAA;IAC7F,SAAS,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,EAAE,OAAO,CAAC,EAAE,gBAAgB,GAAG,OAAO,CAAC,eAAe,CAAC,CAAA;CACxF;AAED,MAAM,WAAW,WAAW;IACxB,OAAO,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,mBAAmB,CAAC,CAAA;IAClD,OAAO,CAAC,CAAC,SAAS,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAAE,OAAO,EAAE,gBAAgB,EAAE,OAAO,EAAE,CAAC,GAAG,OAAO,CAAC,UAAU,CAAC,CAAA;CAC9G"}
import { SpawnedWorker } from "threads-esm";
import { CreateJWSOptions, DagJWS, DagJWSResult, DID, VerifyJWSOptions, VerifyJWSResult } from "dids";
import * as common from "./common";
export * from "./common";
export declare class DIDVerifier {
readonly did: DID;
readonly didVerifier: common.DidResolver;
private verification?;
constructor(did: DID);
init(): Promise<SpawnedWorker<common.DidVerification>>;
createJWS<T extends string | Record<string, any>>(payload: T, options?: CreateJWSOptions): Promise<DagJWS>;
createDagJWS(payload: Record<string, any>, options?: CreateJWSOptions): Promise<DagJWSResult>;
verify(jws: string | DagJWS, options?: VerifyJWSOptions): Promise<VerifyJWSResult>;
shutdown(): Promise<void>;
}
//# sourceMappingURL=index.d.ts.map
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAqB,aAAa,EAAC,MAAM,aAAa,CAAA;AAC7D,OAAO,EAAC,gBAAgB,EAAE,MAAM,EAAE,YAAY,EAAE,GAAG,EAAc,gBAAgB,EAAE,eAAe,EAAC,MAAM,MAAM,CAAA;AAC/G,OAAO,KAAK,MAAM,MAAM,UAAU,CAAA;AAGlC,cAAc,UAAU,CAAA;AAExB,qBAAa,WAAW;IACpB,QAAQ,CAAC,GAAG,EAAE,GAAG,CAAA;IACjB,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC,WAAW,CAAA;IACxC,OAAO,CAAC,YAAY,CAAC,CAAmD;gBAE5D,GAAG,EAAE,GAAG;IAcP,IAAI,IAAI,OAAO,CAAC,aAAa,CAAC,MAAM,CAAC,eAAe,CAAC,CAAC;IAWtD,SAAS,CAAC,CAAC,SAAS,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAAE,OAAO,EAAE,CAAC,EAAE,OAAO,CAAC,EAAE,gBAAgB,GAAG,OAAO,CAAC,MAAM,CAAC;IAK1G,YAAY,CAAC,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAAE,OAAO,CAAC,EAAE,gBAAgB,GAAG,OAAO,CAAC,YAAY,CAAC;IAK7F,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,EAAE,OAAO,CAAC,EAAE,gBAAgB,GAAG,OAAO,CAAC,eAAe,CAAC;IAKlF,QAAQ,IAAI,OAAO,CAAC,IAAI,CAAC;CAOzC"}
export {};
//# sourceMappingURL=sign.d.ts.map
{"version":3,"file":"sign.d.ts","sourceRoot":"","sources":["../src/sign.ts"],"names":[],"mappings":"AAoDA,OAAO,EAAE,CAAA"}
+1
-47

@@ -1,47 +0,1 @@

import { toString } from 'uint8arrays/to-string';
import { decode } from 'codeco';
import { uint8ArrayAsBase64pad, uint8ArrayAsBase64url } from '@didtools/codecs';
export function encodeBase64(bytes) {
return uint8ArrayAsBase64pad.encode(bytes);
}
export function encodeBase64Url(bytes) {
return uint8ArrayAsBase64url.encode(bytes);
}
export function decodeBase64(s) {
return decode(uint8ArrayAsBase64pad, s);
}
export function base64urlToJSON(s) {
const decoded = decode(uint8ArrayAsBase64url, s);
return JSON.parse(toString(decoded));
}
export function fromDagJWS(jws) {
if (jws.signatures.length > 1) throw new Error('Cant convert to compact jws');
return `${jws.signatures[0].protected}.${jws.payload}.${jws.signatures[0].signature}`;
}
/**
* Make DID URL from DID and timestamp (= versionTime query)
*/ export function didWithTime(did, atTime) {
if (atTime) {
const versionTime = atTime.toISOString().split('.')[0] + 'Z';
return `${did}?versionTime=${versionTime}`;
} else {
return did;
}
}
/**
* `controller` field of DID Document can be one or multiple strings, if defined.
* Here we transform it into array of strings.
* Potentially it can be an empty array.
*/ export function extractControllers(controllerProperty) {
if (controllerProperty) {
if (Array.isArray(controllerProperty)) {
return controllerProperty;
} else {
return [
controllerProperty
];
}
} else {
return [];
}
}
export { };

@@ -14,10 +14,9 @@ function _define_property(obj, key, value) {

}
import { spawn } from "threads-esm";
import * as common from "./common.mjs";
import { Cacao } from "@didtools/cacao";
import { getEIP191Verifier } from '@didtools/pkh-ethereum';
import { spawnBidirectional } from "threads-esm";
export * from "./common";
export class DIDVerifier {
async init() {
if (!this.verification) {
this.verification = await spawn(new URL('./sign', import.meta.url));
this.verification = await spawnBidirectional(this.didVerifier, new URL('./sign', import.meta.url));
console.log('Verification ready');
}

@@ -28,12 +27,11 @@ return this.verification;

const verification = await this.init();
return await verification.createJWS(this.did, payload, options);
return verification.createJWS(payload, options);
}
async createDagJWS(payload, options) {
const verification = await this.init();
return await verification.createDagJWS(this.did, payload, options);
return verification.createDagJWS(payload, options);
}
async verify(jws, options) {
const verification = await this.init();
const res = await verifyJWS(verification.verifyJWS, this.did, jws, options);
return res;
return verification.verifyJWS(jws, options);
}

@@ -49,79 +47,16 @@ async shutdown() {

_define_property(this, "did", void 0);
_define_property(this, "didVerifier", void 0);
_define_property(this, "verification", undefined);
this.did = did;
}
}
const verifiers = {
...getEIP191Verifier()
};
async function verifyJWS(func, did, jws, options) {
options = Object.assign({
verifiers
}, options);
if (typeof jws !== 'string') jws = common.fromDagJWS(jws);
const kid = common.base64urlToJSON(jws.split('.')[0]).kid;
if (!kid) throw new Error('No "kid" found in jws');
const didResolutionResult = await did.resolve(kid);
const timecheckEnabled = !options.disableTimecheck;
if (timecheckEnabled) {
const nextUpdate = didResolutionResult.didDocumentMetadata?.nextUpdate;
if (nextUpdate) {
// This version of the DID document has been revoked. Check if the JWS
// was signed before the revocation happened.
const phaseOutMS = options.revocationPhaseOutSecs ? options.revocationPhaseOutSecs * 1000 : 0;
const revocationTime = new Date(nextUpdate).valueOf() + phaseOutMS;
const isEarlier = options.atTime && options.atTime.getTime() < revocationTime;
const isLater = !isEarlier;
if (isLater) {
// Do not allow using a key _after_ it is being revoked
throw new Error(`invalid_jws: signature authored with a revoked DID version: ${kid}`);
this.didVerifier = {
resolve (url) {
console.log(`resolve ${url}`);
return did.resolve(url);
},
request (options, payload) {
console.log(`request ${JSON.stringify(payload)}`);
return did.requestJWS(options, payload);
}
}
// Key used before `updated` date
const updated = didResolutionResult.didDocumentMetadata?.updated;
if (updated && options.atTime && options.atTime.getTime() < new Date(updated).valueOf()) {
throw new Error(`invalid_jws: signature authored before creation of DID version: ${kid}`);
}
};
}
const signerDid = didResolutionResult.didDocument?.id;
if (options.issuer && options.issuer === options.capability?.p.iss && signerDid === options.capability.p.aud) {
if (!options.verifiers) throw new Error('Registered verifiers needed for CACAO');
await Cacao.verify(options.capability, {
disableExpirationCheck: options.disableTimecheck,
atTime: options.atTime ? options.atTime : undefined,
revocationPhaseOutSecs: options.revocationPhaseOutSecs,
verifiers: options.verifiers ?? {}
});
} else if (options.issuer && options.issuer !== signerDid) {
const issuerUrl = common.didWithTime(options.issuer, options.atTime);
const issuerResolution = await did.resolve(issuerUrl);
const controllerProperty = issuerResolution.didDocument?.controller;
const controllers = common.extractControllers(controllerProperty);
if (options.capability?.s && options.capability.p.aud === signerDid && controllers.includes(options.capability.p.iss)) {
await Cacao.verify(options.capability, {
atTime: options.atTime ? options.atTime : undefined,
revocationPhaseOutSecs: options.revocationPhaseOutSecs,
verifiers: options.verifiers ?? {}
});
} else {
const signerIsController = signerDid ? controllers.includes(signerDid) : false;
if (!signerIsController) {
throw new Error(`invalid_jws: not a valid verificationMethod for issuer: ${kid}`);
}
}
}
const publicKeys = didResolutionResult.didDocument?.verificationMethod || [];
// verifyJWS will throw an error if the signature is invalid
await func(jws, publicKeys);
let payload;
try {
payload = common.base64urlToJSON(jws.split('.')[1]);
} catch (e) {
// If an error is thrown it means that the payload is a CID.
}
return {
kid,
payload,
didResolutionResult
};
}

@@ -1,13 +0,31 @@

import { expose } from "threads-esm";
import { verifyJWS } from 'did-jwt';
expose({
createJWS: (did, payload, options)=>{
return did.createJWS(payload, options);
import { exposeBidirectional } from "threads-esm";
import { createDagJWSUsing, createJWSUsing, verifyJWSUsing } from "dids";
let resolver;
resolver = await exposeBidirectional({
createJWS: (payload, options, capability)=>{
console.log('CreateJWS');
return createJWSUsing({
request: resolver.request,
capability,
payload,
options: options ?? {}
});
},
createDagJWS: (did, payload, options)=>{
return did.createDagJWS(payload, options);
createDagJWS: (payload, options, capability)=>{
console.log('CreateDagJWS');
return createDagJWSUsing({
request: resolver.request,
capability,
payload,
options: options ?? {}
});
},
verifyJWS: (jws, pubKeys)=>{
return verifyJWS(jws, pubKeys);
verifyJWS: (jws, options)=>{
console.log('VerifyJWS');
return verifyJWSUsing({
resolve: resolver.resolve,
jws,
options: options ?? {}
});
}
});
{
"name": "dids-threads",
"version": "1.0.5",
"version": "1.0.6",
"description": "DIDs Threads",

@@ -32,16 +32,10 @@ "packageManager": "pnpm@8.5.0",

"dependencies": {
"@didtools/cacao": "^2.1.0",
"@didtools/codecs": "^1.0.1",
"@didtools/pkh-ethereum": "^0.4.1",
"codeco": "^1.2.0",
"dag-jose-utils": "^3.0.0",
"did-jwt": "^7.4.0",
"@didtools/cacao": "2.1.1-next.0",
"did-resolver": "^4.1.0",
"dids": "^4.0.4",
"multiformats": "^12.1.2",
"threads-esm": "^2.0.0",
"uint8arrays": "^4.0.6"
"dids": "4.0.5-next.0",
"threads-esm": "^2.0.0"
},
"devDependencies": {
"@changesets/cli": "^2.26.1",
"@didtools/codecs": "^1.0.1",
"@jest/globals": "^29.5.0",

@@ -55,2 +49,4 @@ "@skypack/package-check": "^0.2.2",

"@typescript-eslint/parser": "^5.59.7",
"codeco": "^1.2.0",
"dag-jose-utils": "^3.0.0",
"del-cli": "^5.0.0",

@@ -67,3 +63,4 @@ "eslint": "^8.41.0",

"typedoc-plugin-markdown": "^3.15.3",
"typescript": "^5.0.4"
"typescript": "^5.0.4",
"uint8arrays": "^4.0.6"
},

@@ -70,0 +67,0 @@ "engines": {

import { VerificationMethod } from "did-resolver";
import { CreateJWSOptions, DagJWSResult, DID, DIDProviderClient } from "dids";
import { DagJWS } from "dids";
import { Cacao } from "@didtools/cacao";
export type DidThreads = {
client: DIDProviderClient;
id: string;
capability?: Cacao;
};
export type CreateJWS<T extends string | Record<string, any>> = (did: DID, payload: T, options?: CreateJWSOptions) => Promise<DagJWS>;
export type CreateDagJWS = (did: DID, payload: Record<string, any>, options?: CreateJWSOptions) => Promise<DagJWSResult>;
export type JWSVerification = (jws: string, pubKeys: VerificationMethod | VerificationMethod[]) => Promise<VerificationMethod>;
export interface DidVerification {
createJWS<T extends string | Record<string, any>>(did: DID, payload: T, options?: CreateJWSOptions): Promise<DagJWS>;
createDagJWS(did: DID, payload: Record<string, any>, options?: CreateJWSOptions): Promise<DagJWSResult>;
verifyJWS(jws: string, pubKeys: VerificationMethod | VerificationMethod[]): VerificationMethod;
}
export declare function encodeBase64(bytes: Uint8Array): string;
export declare function encodeBase64Url(bytes: Uint8Array): string;
export declare function decodeBase64(s: string): Uint8Array;
export declare function base64urlToJSON(s: string): Record<string, any>;
export declare function fromDagJWS(jws: DagJWS): string;
/**
* Make DID URL from DID and timestamp (= versionTime query)
*/
export declare function didWithTime(did: string, atTime?: Date): string;
/**
* `controller` field of DID Document can be one or multiple strings, if defined.
* Here we transform it into array of strings.
* Potentially it can be an empty array.
*/
export declare function extractControllers(controllerProperty: string | Array<string> | undefined): Array<string>;
//# sourceMappingURL=common.d.mts.map
{"version":3,"file":"common.d.mts","sourceRoot":"","sources":["../src/common.mts"],"names":[],"mappings":"AAAA,OAAO,EAAC,kBAAkB,EAAC,MAAM,cAAc,CAAA;AAC/C,OAAO,EAAC,gBAAgB,EAAE,YAAY,EAAE,GAAG,EAAE,iBAAiB,EAAC,MAAM,MAAM,CAAA;AAI3E,OAAO,EAAE,MAAM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ACjG;AAED,wBAAgB,YAAY,CAAC,KAAK,EAAE,UAAU,GAAG,MAAM,CAEtD;AAED,wBAAgB,eAAe,CAAC,KAAK,EAAE,UAAU,GAAG,MAAM,CAEzD;AAED,wBAAgB,YAAY,CAAC,CAAC,EAAE,MAAM,GAAG,UAAU,CAElD;AAED,wBAAgB,eAAe,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAG9D;AAED,wBAAgB,UAAU,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAG9C;AAED;;GAEG;AACH,wBAAgB,WAAW,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,IAAI,GAAG,MAAM,CAO9D;AAED;;;;GAIG;AACH,wBAAgB,kBAAkB,CAC9B,kBAAkB,EAAE,MAAM,GAAG,KAAK,CAAC,MAAM,CAAC,GAAG,SAAS,GACvD,KAAK,CAAC,MAAM,CAAC,CAUf"}
import { SpawnedWorker } from "threads-esm";
import { CreateJWSOptions, DagJWS, DagJWSResult, DID, VerifyJWSOptions, VerifyJWSResult } from "dids";
import * as common from "./common.mjs";
export declare class DIDVerifier {
readonly did: DID;
private verification?;
constructor(did: DID);
init(): Promise<SpawnedWorker<common.DidVerification>>;
createJWS<T extends string | Record<string, any>>(payload: T, options?: CreateJWSOptions): Promise<DagJWS>;
createDagJWS(payload: Record<string, any>, options?: CreateJWSOptions): Promise<DagJWSResult>;
verify(jws: string | DagJWS, options?: VerifyJWSOptions): Promise<VerifyJWSResult>;
shutdown(): Promise<void>;
}
//# sourceMappingURL=index.d.mts.map
{"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../src/index.mts"],"names":[],"mappings":"AAAA,OAAO,EAAQ,aAAa,EAAC,MAAM,aAAa,CAAA;AAChD,OAAO,EAAC,gBAAgB,EAAE,MAAM,EAAE,YAAY,EAAE,GAAG,EAAE,gBAAgB,EAAE,eAAe,EAAC,MAAM,MAAM,CAAA;AACnG,OAAO,KAAK,MAAM,MAAM,cAAc,CAAA;AAItC,qBAAa,WAAW;IACpB,QAAQ,CAAC,GAAG,EAAE,GAAG,CAAA;IACjB,OAAO,CAAC,YAAY,CAAC,CAAmD;gBAE5D,GAAG,EAAE,GAAG;IAIP,IAAI,IAAI,OAAO,CAAC,aAAa,CAAC,MAAM,CAAC,eAAe,CAAC,CAAC;IAOtD,SAAS,CAAC,CAAC,SAAS,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAAE,OAAO,EAAE,CAAC,EAAE,OAAO,CAAC,EAAE,gBAAgB,GAAG,OAAO,CAAC,MAAM,CAAC;IAK1G,YAAY,CAAC,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAAE,OAAO,CAAC,EAAE,gBAAgB,GAAG,OAAO,CAAC,YAAY,CAAC;IAK7F,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,EAAE,OAAO,CAAC,EAAE,gBAAgB,GAAG,OAAO,CAAC,eAAe,CAAC;IAMlF,QAAQ,IAAI,OAAO,CAAC,IAAI,CAAC;CAOzC"}
export {};
//# sourceMappingURL=sign.d.mts.map
{"version":3,"file":"sign.d.mts","sourceRoot":"","sources":["../src/sign.mts"],"names":[],"mappings":""}