Skip to content

Instantly share code, notes, and snippets.

@ALRubinger
Created April 20, 2023 23:58
Show Gist options
  • Save ALRubinger/0f5abb0f4012d906e581682869fd4819 to your computer and use it in GitHub Desktop.
Save ALRubinger/0f5abb0f4012d906e581682869fd4819 to your computer and use it in GitHub Desktop.
Web5 Quickstart
import { Web5 } from '@tbd54566975/web5';
const web5 = new Web5;
const did = await web5.did.create('ion');
console.log('did', did);
await web5.did.register({
connected: true,
did: did.id,
endpoint: 'app://dwn',
keys: did.keys[0].keypair,
});
const data = 'Hello Web5';
console.log('write data', data);
const writeResult = await web5.dwn.records.write(did.id, {
author: did.id,
data,
message: {
dataFormat: 'text/plain',
},
});
console.log('write result', writeResult);
const queryResult = await web5.dwn.records.query(did.id, {
author: did.id,
message: {
filter: {
dataFormat: 'text/plain',
},
},
});
console.log('query result', queryResult);
const readResult = await web5.dwn.records.read(did.id, {
author: did.id,
message: {
recordId: queryResult.entries[0].recordId,
},
});
console.log('read result', readResult);
console.log('read data', web5.dwn.SDK.Encoder.bytesToString(await web5.dwn.SDK.DataStream.toBytes(readResult.data)));
const deleteResult = await web5.dwn.records.delete(did.id, {
author: did.id,
message: {
recordId: queryResult.entries[0].recordId,
},
});
console.log('delete result', deleteResult);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment