Skip to content

Commit

Permalink
Merge branch 'main' into pow
Browse files Browse the repository at this point in the history
* main:
  Removed the need for createProfile()
  • Loading branch information
thehenrytsai committed Jan 3, 2024
2 parents 0ddefcd + 8849a5c commit b303da4
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 56 deletions.
5 changes: 2 additions & 3 deletions examples/node/stream-data.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import { RecordsRead } from '@tbd54566975/dwn-sdk-js';
import { RecordsRead, DidKeyResolver } from '@tbd54566975/dwn-sdk-js';

import fetch from 'node-fetch';
import { v4 as uuidv4 } from 'uuid';

import { createJsonRpcRequest } from '../../dist/src/lib/json-rpc.js';
import {
createProfile,
createRecordsWriteMessage,
getFileAsReadStream,
} from '../../dist/tests/utils.js';

const alice = await createProfile();
const alice = await DidKeyResolver();
const { stream, size, cid } = await getFileAsReadStream('fixtures/test.jpeg');
const { recordsWrite } = await createRecordsWriteMessage(alice, {
dataCid: cid,
Expand Down
88 changes: 84 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"url": "https://github.com/TBD54566975/dwn-server/issues"
},
"dependencies": {
"@tbd54566975/dwn-sdk-js": "0.2.9",
"@tbd54566975/dwn-sdk-js": "0.2.10",
"@tbd54566975/dwn-sql-store": "0.2.5",
"better-sqlite3": "^8.5.0",
"body-parser": "^1.20.2",
Expand Down
6 changes: 4 additions & 2 deletions tests/dwn-process-message.spec.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import { DidKeyResolver } from '@tbd54566975/dwn-sdk-js';

import { expect } from 'chai';
import { v4 as uuidv4 } from 'uuid';

import { handleDwnProcessMessage } from '../src/json-rpc-handlers/dwn/process-message.js';
import type { RequestContext } from '../src/lib/json-rpc-router.js';
import { createJsonRpcRequest } from '../src/lib/json-rpc.js';
import { getTestDwn } from './test-dwn.js';
import { createProfile, createRecordsWriteMessage } from './utils.js';
import { createRecordsWriteMessage } from './utils.js';

describe('handleDwnProcessMessage', function () {
it('returns a JSON RPC Success Response when DWN returns a 2XX status code', async function () {
const alice = await createProfile();
const alice = await DidKeyResolver.generate();

// Construct a well-formed DWN Request that will be successfully processed.
const { recordsWrite, dataStream } = await createRecordsWriteMessage(alice);
Expand Down
30 changes: 15 additions & 15 deletions tests/http-api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import {
Cid,
DataStream,
DidKeyResolver,
RecordsQuery,
RecordsRead,
Time,
Expand Down Expand Up @@ -32,7 +33,6 @@ import type { RegisteredTenantGate } from '../src/registered-tenant-gate.js';
import { getTestDwn } from './test-dwn.js';
import type { Profile } from './utils.js';
import {
createProfile,
createRecordsWriteMessage,
getFileAsReadStream,
streamHttpRequest,
Expand Down Expand Up @@ -65,7 +65,7 @@ describe('http api', function () {
httpApi = new HttpApi(dwn, tenantGate);

await tenantGate.initialize();
profile = await createProfile();
profile = await DidKeyResolver.generate();
await tenantGate.authorizeTenantPOW(profile.did);
await tenantGate.authorizeTenantTermsOfService(profile.did);
});
Expand Down Expand Up @@ -113,7 +113,7 @@ describe('http api', function () {
response = generateNonce(5);
}

const p = await createProfile();
const p = await DidKeyResolver.generate();
const submitResponse = await fetch('http://localhost:3000/register/pow', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
Expand Down Expand Up @@ -171,7 +171,7 @@ describe('http api', function () {
response = generateNonce(5);
}

const p = await createProfile();
const p = await DidKeyResolver.generate();
const submitResponse = await fetch('http://localhost:3000/register/pow', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
Expand All @@ -187,10 +187,10 @@ describe('http api', function () {

it('increase complexity as more challenges are completed', async function () {
for (let i = 1; i <= 60; i++) {
tenantGate.authorizeTenantPOW((await createProfile()).did);
tenantGate.authorizeTenantPOW((await DidKeyResolver.generate()).did);
}

const p = await createProfile();
const p = await DidKeyResolver.generate();
const challengeResponse = await fetch(
'http://localhost:3000/register/pow',
);
Expand Down Expand Up @@ -263,7 +263,7 @@ describe('http api', function () {
response = generateNonce(5);
}

const p = await createProfile();
const p = await DidKeyResolver.generate();
const submitResponse = await fetch('http://localhost:3000/register/pow', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
Expand All @@ -286,7 +286,7 @@ describe('http api', function () {
response = generateNonce(5);
}

const p = await createProfile();
const p = await DidKeyResolver.generate();
const submitResponse = await fetch('http://localhost:3000/register/pow', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
Expand All @@ -301,7 +301,7 @@ describe('http api', function () {
});

it('rejects tenants that have not accepted the terms of use and have not completed POW', async function () {
const unauthorized = await createProfile();
const unauthorized = await DidKeyResolver.generate();
const recordsQuery = await RecordsQuery.create({
filter: { schema: 'woosa' },
signer: unauthorized.signer,
Expand All @@ -324,7 +324,7 @@ describe('http api', function () {
});

it('rejects tenants that have accepted the terms of use but not completed POW', async function () {
const unauthorized = await createProfile();
const unauthorized = await DidKeyResolver.generate();
await tenantGate.authorizeTenantTermsOfService(unauthorized.did);
const recordsQuery = await RecordsQuery.create({
filter: { schema: 'woosa' },
Expand Down Expand Up @@ -364,7 +364,7 @@ describe('http api', function () {
const hash = createHash('sha256');
hash.update(terms);

const p = await createProfile();
const p = await DidKeyResolver.generate();

const acceptResponse = await fetch(
'http://localhost:3000/register/terms-of-service',
Expand Down Expand Up @@ -403,7 +403,7 @@ describe('http api', function () {
});

it('rejects tenants that have completed POW but have not accepted the terms of use', async function () {
const unauthorized = await createProfile();
const unauthorized = await DidKeyResolver.generate();
await tenantGate.authorizeTenantPOW(unauthorized.did);
const recordsQuery = await RecordsQuery.create({
filter: { schema: 'woosa' },
Expand All @@ -430,7 +430,7 @@ describe('http api', function () {
const hash = createHash('sha256');
hash.update('i do not agree');

const p = await createProfile();
const p = await DidKeyResolver.generate();

const acceptResponse = await fetch(
'http://localhost:3000/register/terms-of-service',
Expand Down Expand Up @@ -618,7 +618,7 @@ describe('http api', function () {
});

it('handles RecordsWrite overwrite that does not mutate data', async function () {
const p = await createProfile();
const p = await DidKeyResolver.generate();
await tenantGate.authorizeTenantPOW(p.did);
await tenantGate.authorizeTenantTermsOfService(p.did);

Expand Down Expand Up @@ -899,7 +899,7 @@ describe('http api', function () {
});

it('returns a 404 for invalid or unauthorized did', async function () {
const unauthorized = await createProfile();
const unauthorized = await DidKeyResolver.generate();
const { recordsWrite } = await createRecordsWriteMessage(unauthorized);

const response = await fetch(
Expand Down
25 changes: 1 addition & 24 deletions tests/utils.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
import type { PrivateJwk, PublicJwk, Signer } from '@tbd54566975/dwn-sdk-js';
import {
Cid,
DataStream,
DidKeyResolver,
PrivateKeySigner,
RecordsWrite,
} from '@tbd54566975/dwn-sdk-js';
import { Cid, DataStream, RecordsWrite } from '@tbd54566975/dwn-sdk-js';

import { createHash } from 'crypto';
import type { ReadStream } from 'node:fs';
Expand All @@ -29,23 +23,6 @@ export type Profile = {
signer: Signer;
};

export async function createProfile(): Promise<Profile> {
const { did, keyPair, keyId } = await DidKeyResolver.generate();

// signer is required by all dwn message classes. it's used to sign messages
const signer = new PrivateKeySigner({
privateJwk: keyPair.privateJwk,
algorithm: keyPair.privateJwk.alg,
keyId: `${did}#${keyId}`,
});

return {
did,
keyPair,
signer,
};
}

export type CreateRecordsWriteOverrides =
| ({
dataCid?: string;
Expand Down
10 changes: 3 additions & 7 deletions tests/ws-api.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DataStream } from '@tbd54566975/dwn-sdk-js';
import { DataStream, DidKeyResolver } from '@tbd54566975/dwn-sdk-js';

import { expect } from 'chai';
import { base64url } from 'multiformats/bases/base64';
Expand All @@ -12,11 +12,7 @@ import {
} from '../src/lib/json-rpc.js';
import { WsApi } from '../src/ws-api.js';
import { getTestDwn } from './test-dwn.js';
import {
createProfile,
createRecordsWriteMessage,
sendWsMessage,
} from './utils.js';
import { createRecordsWriteMessage, sendWsMessage } from './utils.js';

let server: http.Server;
let wsServer: WebSocketServer;
Expand Down Expand Up @@ -57,7 +53,7 @@ describe('websocket api', function () {
});

it('handles RecordsWrite messages', async function () {
const alice = await createProfile();
const alice = await DidKeyResolver.generate();

const { recordsWrite, dataStream } = await createRecordsWriteMessage(alice);
const dataBytes = await DataStream.toBytes(dataStream);
Expand Down

0 comments on commit b303da4

Please sign in to comment.