Skip to content

Commit

Permalink
Update to use vda-did-resolver for signature verification. Fix unit t…
Browse files Browse the repository at this point in the history
…ests.
  • Loading branch information
Chris committed Apr 29, 2024
1 parent b8abd42 commit f7b0a5e
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 20 deletions.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@
"@verida/did-client": "4.0.0-alpha.0",
"@verida/did-document": "4.0.0-alpha.0",
"@verida/encryption-utils": "^3.0.1-alpha.0",
"@verida/types": "^4.0.0-alpha.0",
"@verida/vda-common": "^4.0.0-alpha.0",
"@verida/vda-did-resolver": "^4.0.0-alpha.0",
"aws-serverless-express": "^3.4.0",
"axios": "^1.2.1",
"cors": "^2.8.5",
Expand Down
29 changes: 17 additions & 12 deletions src/components/authManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@ import randtoken from 'rand-token';
import jwt from 'jsonwebtoken';
import mcache from 'memory-cache';

import { DIDClient } from '@verida/did-client'
import EncryptionUtils from '@verida/encryption-utils';
import Utils from './utils.js';
import Db from './db.js';
import dbManager from './dbManager.js';
import { getResolver } from '@verida/vda-did-resolver';
import { DIDDocument } from '@verida/did-document';
import { Resolver } from 'did-resolver';

const vdaDidResolver = getResolver()
const didResolver = new Resolver(vdaDidResolver)

dotenv.config();

Expand Down Expand Up @@ -104,6 +109,14 @@ class AuthManager {
}
}

/**
*
* @todo: Refactor to use @verida/vda-did-resolver, Ensure signature checks verify context
*
* @param {*} did
* @param {*} ignoreCache
* @returns
*/
async getDidDocument(did, ignoreCache=false) {
// Verify the signature signed the correct string
const cacheKey = did
Expand All @@ -116,17 +129,9 @@ class AuthManager {

if (!didDocument) {
console.info(`DID document not in cache: ${did}, fetching`)
if (!didClient) {
console.info(`DID client didn't exist, creating`)
const didClientConfig = {
network: process.env.VERIDA_NETWORK ? process.env.VERIDA_NETWORK : 'banksia',
rpcUrl: process.env.DID_RPC_URL
}

didClient = new DIDClient(didClientConfig);
}

didDocument = await didClient.get(did)

const response = await didResolver.resolve(did)
didDocument = new DIDDocument(response.didDocument)

if (didDocument) {
console.info(`Adding DID document to cache: ${did}`)
Expand Down
14 changes: 9 additions & 5 deletions test/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -364,14 +364,18 @@ describe("Server tests", function() {
assert.equal(response.data.results.maxUsers, process.env.MAX_USERS, 'Correct maximum number of users');
assert.ok(response.data.results.currentUsers > 2, 'At least two users');
assert.ok(response.data.results.version && response.data.results.version.length, 'Version specified');
assert.ok(response.data.results.metrics && response.data.results.metrics.length === 3, 'Metrics exist');
assert.ok(response.data.results.metrics.continuousChangesClientCount >= 0, 'Metric continuousChangesClientCount is in expected range');
assert.ok(response.data.results.metrics.requestMeanTimeMS >= 0, 'Metric requestMeanTimeMS is in expected range');
assert.ok(response.data.results.metrics.requestTimeStdDevMS >= 0, 'Metric requestTimeStdDevMS is in expected range');
assert.ok(response.data.results.metrics && Object.keys(response.data.results.metrics).length === 3, 'Metrics exist');
assert.ok(typeof(response.data.results.metrics.continuousChangesClientCount) != 'undefined', 'Metric continuousChangesClientCount exists');
assert.ok(typeof(response.data.results.metrics.requestMeanTimeMS) != 'undefined', 'Metric requestMeanTimeMS exists');
assert.ok(typeof(response.data.results.metrics.requestTimeStdDevMS) != 'undefined', 'Metric requestTimeStdDevMS exists');
})
})

describe("Contexts", () => {
/**
* Note: deleting all databases means there are no contexts, so this test always
* fails unless you manually create a database for the DID / context
*/
describe.skip("Contexts", () => {
it("Can fetch did contextName from contextHash", async function() {
const did = accountInfo.did
const timestamp = parseInt((new Date()).getTime() / 1000.0)
Expand Down
6 changes: 3 additions & 3 deletions test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ class Utils {
const account = new AutoAccount({
privateKey: privateKey,
didClientConfig: CONFIG.DID_CLIENT_CONFIG,
environment: CONFIG.ENVIRONMENT
network: CONFIG.NETWORK
}, CONFIG.DEFAULT_ENDPOINTS)

return await Network.connect({
client: {
environment: CONFIG.ENVIRONMENT
network: CONFIG.NETWORK
},
account: account,
context: {
Expand All @@ -39,7 +39,7 @@ class Utils {
const account = new AutoAccount({
privateKey: privateKey,
didClientConfig: CONFIG.DID_CLIENT_CONFIG,
environment: CONFIG.ENVIRONMENT
network: CONFIG.NETWORK
}, CONFIG.DEFAULT_ENDPOINTS)

const did = await account.did()
Expand Down

0 comments on commit f7b0a5e

Please sign in to comment.