-
Notifications
You must be signed in to change notification settings - Fork 239
feat(cheqd): Add Token StatusList Service #2325
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
import { AgentContext, CredoError, getKeyFromVerificationMethod, Buffer, utils } from '@credo-ts/core' | ||
Check failure on line 1 in packages/cheqd/src/tokenStatusList/CheqdTokenStatusListService.ts
|
||
import { StatusListPayload, StatusListToken } from './types' | ||
import { createEmptyBitmap, decodeBitmap, encodeBitmap, isBitSet, setBit } from './utils/bitmap' | ||
import { CheqdCreateResourceOptions, CheqdDidResolver } from '../dids' | ||
Check failure on line 4 in packages/cheqd/src/tokenStatusList/CheqdTokenStatusListService.ts
|
||
import { TokenStatusList } from './types/tokenStatusList' | ||
Check failure on line 5 in packages/cheqd/src/tokenStatusList/CheqdTokenStatusListService.ts
|
||
import { CheqdApi } from '../CheqdApi' | ||
import { parseCheqdDid } from '../anoncreds/utils/identifiers' | ||
import base64url from 'base64url' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We have base64url support in the |
||
|
||
export class CheqdTokenStatusListService implements TokenStatusList { | ||
private async loadStatusList( | ||
agentContext: AgentContext, | ||
statusListId: string | ||
): Promise<{ | ||
metadata: any | ||
bitmap: Buffer | ||
jwt: string | ||
}> { | ||
const api = agentContext.dependencyManager.resolve(CheqdApi) | ||
const resource = await api.resolveResource(statusListId) | ||
const jwt = resource.resource?.data.toString() | ||
const payload = JSON.parse(Buffer.from(jwt!.split('.')[1], 'base64').toString()) as StatusListPayload | ||
Comment on lines
+21
to
+22
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. WE have tools like the Also should we not verify it first? |
||
return { | ||
metadata: resource.resourceMetadata, | ||
bitmap: decodeBitmap(payload.status_list.bits), | ||
jwt, | ||
} | ||
} | ||
|
||
async createStatusList( | ||
agentContext: AgentContext, | ||
did: string, | ||
name: string, | ||
tag: string, | ||
size: number, | ||
signer: any // what could be passed as the signer | ||
): Promise<StatusListToken> { | ||
const api = agentContext.dependencyManager.resolve(CheqdApi) | ||
const bitmap = createEmptyBitmap(size) | ||
const payload: StatusListPayload = { | ||
iss: did, | ||
iat: Math.floor(Date.now() / 1000), | ||
status_list: { | ||
encoding: 'bitstring', | ||
bits: encodeBitmap(bitmap), | ||
}, | ||
} | ||
|
||
const jwt = await signer.signJWT(payload) | ||
|
||
const resource = { | ||
collectionId: did.split(':')[3], | ||
id: utils.uuid(), | ||
name: name, | ||
resourceType: 'StatusList', | ||
data: jwt, | ||
version: tag || utils.uuid(), | ||
} satisfies CheqdCreateResourceOptions | ||
|
||
await api.createResource(did, resource) | ||
|
||
return { | ||
jwt, | ||
metadata: { | ||
statusListId: resource.id, | ||
issuedAt: payload.iat, | ||
size: size, | ||
}, | ||
} | ||
Comment on lines
+40
to
+69
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We have quite some utils also to create JWTs. See the Jwt service, also we should probably make a generic status list service that creates the status list (like we have an SD-JWT VC service) which works with a Making a cheqd focused status list service will just result in duplication. So I don't think we can merge the PR like this, and it would first need to be refactored to a generic status list service. Then i see two options for the cheqd integration:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @TimoGlastra Is this the right way to create JWTs?
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @TimoGlastra to publish status lists in different registries. How about following the pattern similar to registrar
|
||
} | ||
|
||
async revokeIndex( | ||
agentContext: AgentContext, | ||
statusListId: string, | ||
index: number, | ||
tag: string, | ||
signer: string | ||
): Promise<StatusListToken> { | ||
const api = agentContext.dependencyManager.resolve(CheqdApi) | ||
const parsedDid = parseCheqdDid(statusListId) | ||
if (!parsedDid) throw new CredoError(`Invalid statusListId: ${statusListId}`) | ||
|
||
const { bitmap, metadata } = await this.loadStatusList(agentContext, statusListId) | ||
setBit(bitmap, index) | ||
|
||
// Build payload | ||
const payload: StatusListPayload = { | ||
iss: parsedDid.did, | ||
iat: Math.floor(Date.now() / 1000), | ||
status_list: { | ||
encoding: 'bitstring', | ||
bits: encodeBitmap(bitmap), | ||
}, | ||
} | ||
|
||
const resolverService = agentContext.dependencyManager.resolve(CheqdDidResolver) | ||
const { didDocument } = await resolverService.resolve(agentContext, parsedDid.did, parsedDid) | ||
if (!didDocument || !didDocument.verificationMethod || !didDocument.verificationMethod.length) | ||
throw new Error('Did is not valid') | ||
const method = didDocument.verificationMethod[0] | ||
|
||
// Build header | ||
const header = { | ||
alg: 'ES256', | ||
kid: method.id, // e.g. 'did:cheqd:testnet:xyz123#key-1' | ||
} | ||
|
||
// encode | ||
const encodedHeader = base64url.encode(JSON.stringify(header)) | ||
const encodedPayload = base64url.encode(JSON.stringify(payload)) | ||
const signingInput = `${encodedHeader}.${encodedPayload}` | ||
// sign payload | ||
const key = getKeyFromVerificationMethod(method) | ||
const signature = await agentContext.wallet.sign({ data: Buffer.from(signingInput), key }) | ||
|
||
// construct jwt | ||
const jwt = `${signingInput}.${base64url.encode(signature.toLocaleString())}` | ||
|
||
const resource = { | ||
collectionId: parsedDid.did, | ||
id: utils.uuid(), | ||
name: metadata.name, | ||
resourceType: metadata.type, | ||
data: jwt, | ||
version: tag || utils.uuid(), | ||
} satisfies CheqdCreateResourceOptions | ||
|
||
await api.createResource(parsedDid.did, resource) | ||
|
||
return { | ||
jwt, | ||
metadata: { | ||
statusListId, | ||
issuedAt: payload.iat, | ||
size: bitmap.length * 8, | ||
}, | ||
} | ||
} | ||
|
||
async isRevoked(agentContext: AgentContext, statusListId: string, index: number): Promise<boolean> { | ||
const { bitmap } = await this.loadStatusList(agentContext, statusListId) | ||
return isBitSet(bitmap, index) | ||
} | ||
|
||
async getStatusListToken(agentContext: AgentContext, statusListId: string): Promise<StatusListToken> { | ||
const { jwt, metadata, bitmap } = await this.loadStatusList(agentContext, statusListId) | ||
const parsedDid = parseCheqdDid(statusListId) | ||
if (!parsedDid) throw new CredoError(`Invalid statusListId: ${statusListId}`) | ||
|
||
return { | ||
jwt, | ||
metadata: { | ||
statusListId, | ||
issuedAt: metadata.createdAt, | ||
size: bitmap.length * 8, | ||
}, | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
export type StatusListToken = { | ||
jwt: string // Signed JWT | ||
metadata: { | ||
statusListId: string | ||
issuedAt: number | ||
size: number | ||
} | ||
} | ||
|
||
export type StatusListPayload = { | ||
iss: string | ||
iat: number | ||
status_list: { | ||
encoding: 'bitstring' | ||
bits: string // base64url-encoded compressed bitmap | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { AgentContext } from '@credo-ts/core' | ||
import { StatusListToken } from './index' | ||
|
||
export interface TokenStatusList { | ||
createStatusList( | ||
agentContext: AgentContext, | ||
did: string, | ||
name: string, | ||
tag: string, | ||
size: number, | ||
signer: any | ||
): Promise<StatusListToken> | ||
revokeIndex( | ||
agentContext: AgentContext, | ||
statusListId: string, | ||
index: number, | ||
tag: string, | ||
signer: any | ||
): Promise<StatusListToken> | ||
isRevoked(agentContext: AgentContext, statusListId: string, index: number): Promise<boolean> | ||
getStatusListToken(agentContext: AgentContext, statusListId: string): Promise<StatusListToken> | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import zlib from 'zlib' | ||
import base64url from 'base64url' | ||
import { Buffer } from '@credo-ts/core' | ||
|
||
export function createEmptyBitmap(size: number): Buffer { | ||
const byteLength = Math.ceil(size / 8) | ||
return Buffer.alloc(byteLength) // All bits = 0 | ||
} | ||
|
||
export function setBit(bitmap: Buffer, index: number): Buffer { | ||
const byteIndex = Math.floor(index / 8) | ||
const bitIndex = index % 8 | ||
bitmap[byteIndex] |= 1 << (7 - bitIndex) | ||
return bitmap | ||
} | ||
|
||
export function isBitSet(bitmap: Buffer, index: number): boolean { | ||
const byteIndex = Math.floor(index / 8) | ||
const bitIndex = index % 8 | ||
return (bitmap[byteIndex] & (1 << (7 - bitIndex))) !== 0 | ||
} | ||
|
||
export function encodeBitmap(bitmap: Buffer): string { | ||
const compressed = zlib.deflateSync(bitmap) | ||
return base64url.encode(compressed) | ||
} | ||
|
||
export function decodeBitmap(encoded: string): Buffer { | ||
const compressed = base64url.toBuffer(encoded) | ||
return Buffer.from(zlib.inflateSync(compressed)) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SD-JWT VC library already has utils to create status lists.
Or updating an existing one: