generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9aa02be
commit 4561808
Showing
5 changed files
with
84 additions
and
10 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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,4 @@ | ||
export function getCurrentXmlSchema112Timestamp() : string { | ||
// Omit the milliseconds part from toISOString() output | ||
return new Date().toISOString().replace(/\.\d+Z$/, 'Z'); | ||
} | ||
This file contains 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 |
---|---|---|
@@ -1,16 +1,70 @@ | ||
import type { Web5Agent } from '@tbd54566975/web5-agent'; | ||
import { Web5Agent } from '@tbd54566975/web5-agent'; | ||
import type { VerifiableCredential } from '../../credentials/src/types.js'; | ||
|
||
import { getCurrentXmlSchema112Timestamp } from '../../credentials/src/utils.js'; | ||
import { v4 as uuidv4 } from 'uuid'; | ||
import { dataToBlob } from './utils.js'; | ||
import { Record } from './record.js'; | ||
|
||
import { DwnInterfaceName, DwnMethodName, RecordsWriteMessage, RecordsWriteOptions } from '@tbd54566975/dwn-sdk-js'; | ||
import { RecordsWriteResponse } from './dwn-api.js'; | ||
|
||
export type VcCreateResponse = RecordsWriteResponse & { | ||
vc: VerifiableCredential; | ||
}; | ||
|
||
export class VcApi { | ||
#agent: Web5Agent; | ||
#web5Agent: Web5Agent; | ||
#connectedDid: string; | ||
|
||
constructor(agent: Web5Agent, connectedDid: string) { | ||
this.#agent = agent; | ||
this.#web5Agent = agent; | ||
this.#connectedDid = connectedDid; | ||
} | ||
|
||
async create() { | ||
// TODO: implement | ||
throw new Error('Not implemented.'); | ||
async create(credentialSubject: any): Promise<VcCreateResponse> { | ||
if (!credentialSubject || typeof credentialSubject !== 'object') { | ||
throw new Error('credentialSubject not valid'); | ||
} | ||
|
||
const vc: VerifiableCredential = { | ||
'@context' : ['https://www.w3.org/2018/credentials/v1'], | ||
credentialSubject : credentialSubject, | ||
type : ['VerifiableCredential'], | ||
issuer : { id: this.#connectedDid }, | ||
issuanceDate : getCurrentXmlSchema112Timestamp(), | ||
id : uuidv4(), | ||
}; | ||
|
||
const messageOptions: Partial<RecordsWriteOptions> = { ...{ schema: 'vc/vc', dataFormat: 'application/json' } }; | ||
|
||
const { dataBlob, dataFormat } = dataToBlob(vc, 'application/json'); | ||
messageOptions.dataFormat = dataFormat; | ||
|
||
const agentResponse = await this.#web5Agent.processDwnRequest({ | ||
author : this.#connectedDid, | ||
dataStream : dataBlob, | ||
messageOptions, | ||
messageType : DwnInterfaceName.Records + DwnMethodName.Write, | ||
store : true, | ||
target : this.#connectedDid | ||
}); | ||
|
||
const { message, reply: { status } } = agentResponse; | ||
const responseMessage = message as RecordsWriteMessage; | ||
|
||
let record: Record; | ||
if (200 <= status.code && status.code <= 299) { | ||
const recordOptions = { | ||
author : this.#connectedDid, | ||
encodedData : dataBlob, | ||
target : this.#connectedDid, | ||
...responseMessage, | ||
}; | ||
|
||
record = new Record(this.#web5Agent, recordOptions); | ||
} | ||
|
||
return { record, status, vc }; | ||
} | ||
} |
This file contains 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