Skip to content

Instantly share code, notes, and snippets.

@bnonni
Last active August 21, 2024 07:04
Show Gist options
  • Save bnonni/ecba009fc58bd78a91719bd9292a6991 to your computer and use it in GitHub Desktop.
Save bnonni/ecba009fc58bd78a91719bd9292a6991 to your computer and use it in GitHub Desktop.
import { Web5PlatformAgent } from '@web5/agent';
import { Web5 } from '@web5/api';
import { appendFile } from 'fs/promises';
const password = '<some-password>'
const { web5, did, recoveryPhrase } = await Web5.connect({
password,
sync : '0s',
techPreview : { dwnEndpoints: ['http://localhost:3000'] }
});
console.log('connected did ', did);
console.log('recoveryPhrase ', recoveryPhrase);
const agent = web5.agent as Web5PlatformAgent;
console.log('agent did ', agent.agentDid.uri);
for (let i = 0; i < 2; i++) {
const identity = await agent.identity.create({
didMethod : 'dht',
metadata : { name: 'Default' },
didOptions : {
services: [
{
id : 'dwn',
type : 'DecentralizedWebNode',
serviceEndpoint : ['http://localhost:3000'],
enc : '#enc',
sig : '#sig',
}
],
verificationMethods: [
{
algorithm : 'Ed25519',
id : 'sig',
purposes : ['assertionMethod', 'authentication']
},
{
algorithm : 'secp256k1',
id : 'enc',
purposes : ['keyAgreement']
}
]
}
});
await agent.identity.manage({ portableIdentity: await identity.export() });
await agent.sync.registerIdentity({ did: identity.did.uri });
}
const identities = await agent.identity.list();
identities.map((identity) => {
console.log('identity.did.metadata', identity.did);
identity.export().then((portableIdentity) => {
console.log('portableIdentity', portableIdentity);
appendFile('identities.json', JSON.stringify(portableIdentity, null, 2) + '\n').then(() => console.log('identity exported'));
});
});
import { Web5 } from '@web5/api';
const dwnEndpoints = ['http://localhost:3000'];
const password = '<some-password>';
const recoveryPhrase = '<some-recovery-phrase>';
const { web5, did: connectedDid } = await Web5.recover({ password, recoveryPhrase, dwnEndpoints });
console.log('web5 ', web5);
console.log('connectedDid ', connectedDid);
  1. In a clean folder, copy connect.ts and recover.ts
  2. Set a password in connect.ts
  3. Run tsx connect.ts and let it run. It'll hang at the sync, let it sit for a 10 sec or so then kill it.
  4. Copy the recovery phrase that prints out, and make note of the did identities it created.
  5. Delete the DATA folder, paste the recovery phrase into recover.ts and run tsx recover.ts

Note You'll need to checkout this branch on our repo and build locally: https://github.com/FormFree/web5-js/tree/api/web5-recover Then you'll need to start a new npm project and run npm install /path/to/FormFree/web5-js/clone/api/web5-recover/branch

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment