Write Records
+ ++ +
Query Records
+ ++ +
Read Records
+ ++ +
Delete Records
+ ++ +
Protocols
+ ++ +
Custom Data Formats
+ ++ +
New Tests
+ +diff --git a/examples/simple-agent/.gitignore b/examples/simple-agent/.gitignore index 0467ea0eb..9cda4e2f9 100644 --- a/examples/simple-agent/.gitignore +++ b/examples/simple-agent/.gitignore @@ -1,5 +1,4 @@ -# Agent Specific - -BLOCKSTORE/ -DATASTORE/ -INDEX/ \ No newline at end of file +DATASTORE-* +EVENTLOG-* +INDEX-* +MESSAGESTORE-* \ No newline at end of file diff --git a/examples/simple-agent/etc/did.json b/examples/simple-agent/etc/did.json index b3ddb677d..d18a28ca7 100644 --- a/examples/simple-agent/etc/did.json +++ b/examples/simple-agent/etc/did.json @@ -7,7 +7,7 @@ "content": { "publicKeys": [ { - "id": "key-1", + "id": "dwn", "type": "JsonWebKey2020", "purposes": [ "authentication" @@ -68,9 +68,9 @@ ], "keys": [ { - "id": "key-1", + "id": "dwn", "type": "JsonWebKey2020", - "keypair": { + "keyPair": { "publicJwk": { "kty": "EC", "crv": "secp256k1", diff --git a/examples/simple-agent/package.json b/examples/simple-agent/package.json index 7f4055f1e..581a17e32 100644 --- a/examples/simple-agent/package.json +++ b/examples/simple-agent/package.json @@ -16,7 +16,7 @@ "dependencies": { "@koa/cors": "4.0.0", "@koa/router": "12.0.0", - "@tbd54566975/web5": "0.3.1-unstable.e2dfe57-2023.3.23-19-07-37", + "@tbd54566975/web5": "0.5.0", "koa": "2.14.1", "koa-body": "6.0.1", "mkdirp": "2.1.5", diff --git a/examples/simple-agent/src/index.js b/examples/simple-agent/src/index.js index 33a72f75f..c148bb011 100644 --- a/examples/simple-agent/src/index.js +++ b/examples/simple-agent/src/index.js @@ -20,10 +20,16 @@ router.post('/dwn', async (ctx, _next) => { try { const response = await receiveHttp(ctx); - // Normalize DWN MessageReply and HTTP Reponse - ctx.status = response?.status?.code ?? response?.status; - ctx.statusText = response?.status?.detail ?? response?.statusText; - ctx.body = 'entries' in response ? { entries: response.entries } : response.body; + console.log('SIMPLE AGENT receiveHTTP response:', response); + + // // All DWN MessageReply responses contain a `status` object. + // // DWN RecordsQuery responses contain an `entries` array of query results. + // // DWN RecordsRead responses contain a data property which is a Readable stream. + // const { message, ...retainedResponse } = response; + + // ctx.body = retainedResponse; + // ctx.status = retainedResponse?.status?.code; + // ctx.statusText = retainedResponse?.status?.detail; } catch(err) { console.error(err); diff --git a/examples/simple-agent/src/utils.js b/examples/simple-agent/src/utils.js index 27784bc2f..d2782cfc5 100644 --- a/examples/simple-agent/src/utils.js +++ b/examples/simple-agent/src/utils.js @@ -1,10 +1,23 @@ import getRawBody from 'raw-body'; +import { DataStoreLevel, Dwn, EventLogLevel, MessageStoreLevel } from '@tbd54566975/dwn-sdk-js'; import { Web5 } from '@tbd54566975/web5'; import fs from 'node:fs'; import mkdirp from 'mkdirp'; import { createRequire } from 'node:module'; -const web5 = new Web5(); +// Use custom names for the block, message, and data stores to make it possible to launch multiple simple agents +// in the same directory. If you don't do this, you will get LevelDB lock errors. +const port = await getPort(process.argv); +const dataStore = new DataStoreLevel({ blockstoreLocation: `DATASTORE-${port}` }); +const eventLog = new EventLogLevel({ location: `EVENTLOG-${port}` }); +const messageStore = new MessageStoreLevel({ + blockstoreLocation : `MESSAGESTORE-${port}`, + indexLocation : `INDEX-${port}`, +}); + +const dwnNode = await Dwn.create({ dataStore, eventLog, messageStore }); + +export const web5 = new Web5({ dwn: { node: dwnNode }}); const etcPath = './etc'; const didStoragePath = `${etcPath}/did.json`; @@ -14,7 +27,7 @@ const require = createRequire(import.meta.url); const testProtocol = require('../resources/test-protocol.json'); const protocols = [testProtocol]; -async function getPort(processArgv) { +export async function getPort(processArgv) { const defaultPort = 8080; // Find the -p option and get the port number @@ -27,7 +40,7 @@ async function getPort(processArgv) { return port; } -async function loadConfig() { +export async function loadConfig() { if (!fs.existsSync(etcPath)) { // ensure that directory for persistent storage exists mkdirp.sync(etcPath); @@ -42,15 +55,14 @@ async function loadConfig() { didState = await initOperatorDid(); fs.writeFileSync(didStoragePath, JSON.stringify(didState, null, 2)); } - web5.did.register({ + web5.did.manager.set(didState.id, { connected: true, - did: didState.id, endpoint: 'app://dwn', - keys: didState.keys[0].keypair, + keys: { ['#dwn']: { keyPair: didState.keys[0].keyPair} }, }); } -async function initOperatorDid() { +export async function initOperatorDid() { const operatorDid = await web5.did.create('ion', { services: [ { @@ -65,7 +77,7 @@ async function initOperatorDid() { return operatorDid; } -async function initializeProtocols() { +export async function initializeProtocols() { for (let { protocol, definition } of protocols) { const queryResponse = await web5.dwn.protocols.query(didState.id, { author: didState.id, @@ -87,7 +99,7 @@ async function initializeProtocols() { } } -async function receiveHttp(ctx) { +export async function receiveHttp(ctx) { const encodedMessage = ctx.get(web5.transports.http.ENCODED_MESSAGE_HEADER); if (!encodedMessage) throw 'Message is missing or malformed'; @@ -99,20 +111,9 @@ async function receiveHttp(ctx) { const data = await getRawBody(ctx.req); - console.log('TRACE - receiveHttp() - message:', message); - return await web5.send(target, { author, data, message, }); } - -export { - getPort, - initializeProtocols, - initOperatorDid, - loadConfig, - receiveHttp, - web5, -}; diff --git a/examples/test-dashboard/desktop-agent-original.html b/examples/test-dashboard/desktop-agent-original.html new file mode 100644 index 000000000..df1527f12 --- /dev/null +++ b/examples/test-dashboard/desktop-agent-original.html @@ -0,0 +1,937 @@ + + + +
+ + +Write Records
+ +Query Records
+ +Read Records
+ +Delete Records
+ +Protocols
+ +Custom Data Formats
+ +New Tests
+ +