Skip to content

Commit

Permalink
dynamically select up to 4 free dwn hosts for every did
Browse files Browse the repository at this point in the history
  • Loading branch information
mistermoe authored and frankhinek committed May 19, 2023
1 parent 9bc7bcd commit c0f5aab
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
6 changes: 6 additions & 0 deletions packages/web5/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,12 @@ export function pascalToKebabCase(str) {
.toLowerCase();
}

export function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min)) + min;
}

/**
* Credit for toType() function:
* Angus Croll
Expand Down
43 changes: 39 additions & 4 deletions packages/web5/src/web5.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ import type { DidState, DidMethodApi, DidResolverCache } from '@tbd54566975/dids
import type { Web5Agent } from '@tbd54566975/web5-agent';

// import { Web5ProxyAgent } from '@tbd54566975/web5-proxy-agent';
import { DidIonApi, DidKeyApi } from '@tbd54566975/dids';
import { DidIonApi, DidKeyApi, utils as didUtils } from '@tbd54566975/dids';
import { Web5UserAgent, ProfileApi } from '@tbd54566975/web5-user-agent';

import { DwnApi } from './dwn-api.js';
import { DidApi } from './did-api.js';
import { AppStorage } from './app-storage.js';
import { getRandomInt } from './utils.js';
import { DwnServiceEndpoint } from '@tbd54566975/dwn-sdk-js';

// TODO: discuss what other options we want
export type Web5ConnectOptions = {
Expand Down Expand Up @@ -66,9 +68,20 @@ export class Web5 {
// create enc. keys

if (!profile) {
// TODO: add good samaritan nodes here when they're ready

const defaultProfileDid = await this.did.create('ion');
const dwnUrls = await Web5.getDwnHosts();
let ionCreateOptions;

if (dwnUrls.length > 0) {
ionCreateOptions = {
services: [{
id : 'dwn',
type : 'DecentralizedWebNode',
serviceEndpoint : { nodes: dwnUrls }
}]
};
}

const defaultProfileDid = await this.did.create('ion', ionCreateOptions);
// setting id & name as the app's did to make migration easier
profile = await profileApi.createProfile({
name : appDidState.id,
Expand All @@ -84,4 +97,26 @@ export class Web5 {

return { web5, did: connectedDid };
}

/**
* dynamically selects up to 4 dwn hosts
*/
static async getDwnHosts() {
const response = await fetch('https://dwn.tbddev.org/.well-known/did.json');

const didDoc = await response.json();
const [ service ] = didUtils.getServices(didDoc, { id: '#dwn', type: 'DecentralizedWebNode' });
const { nodes } = <DwnServiceEndpoint>service.serviceEndpoint;

// allocate up to 4 nodes for a user.
const numNodesToAllocate = Math.min(Math.floor(nodes.length / 2), 4);
const dwnUrls = [];

for (let i = 0; i < numNodesToAllocate; i += 1) {
const nodeIdx = getRandomInt(0, nodes.length);
dwnUrls.push(nodes[nodeIdx]);
}

return dwnUrls;
}
}

0 comments on commit c0f5aab

Please sign in to comment.