Skip to content
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

Add Record class, DID Manager, and Stabilize API #43

Merged
merged 19 commits into from
Apr 21, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
8 changes: 3 additions & 5 deletions examples/simple-agent/.gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# Agent Specific

BLOCKSTORE/
DATASTORE/
INDEX/
DATASTORE-*
INDEX-*
MESSAGESTORE-*
6 changes: 3 additions & 3 deletions examples/simple-agent/etc/did.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"content": {
"publicKeys": [
{
"id": "key-1",
"id": "dwn",
"type": "JsonWebKey2020",
"purposes": [
"authentication"
Expand Down Expand Up @@ -68,9 +68,9 @@
],
"keys": [
{
"id": "key-1",
"id": "dwn",
"type": "JsonWebKey2020",
"keypair": {
"keyPair": {
"publicJwk": {
"kty": "EC",
"crv": "secp256k1",
Expand Down
14 changes: 10 additions & 4 deletions examples/simple-agent/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
21 changes: 15 additions & 6 deletions examples/simple-agent/src/utils.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
import getRawBody from 'raw-body';
import { DataStoreLevel, Dwn, 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 messageStore = new MessageStoreLevel({
blockstoreLocation : `MESSAGESTORE-${port}`,
indexLocation : `INDEX-${port}`,
});
frankhinek marked this conversation as resolved.
Show resolved Hide resolved

const dwnNode = await Dwn.create({ messageStore, dataStore });

const web5 = new Web5({ dwn: { node: dwnNode }});

const etcPath = './etc';
const didStoragePath = `${etcPath}/did.json`;
Expand Down Expand Up @@ -42,11 +54,10 @@ 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: didState.keys['#dwn'].keyPair,
});
}

Expand Down Expand Up @@ -99,8 +110,6 @@ async function receiveHttp(ctx) {

const data = await getRawBody(ctx.req);

console.log('TRACE - receiveHttp() - message:', message);

return await web5.send(target, {
author,
data,
Expand Down
Loading