Sharing records
Use a configured protocol
Create the message record
Send the message record to recipients
Reading the message record from an author
Create a DID for Alice
const alice = await sdk.did.create({
sync: 'off' | '10s'
});
Create a Protocol
const protocolDef = {
'protocol' : 'https://message.xyz/protocol',
'published' : true,
'types' : {
'message': {
'schema' : 'https://message.xyz/protocol/schema',
'dataFormats' : [
'application/json'
]
}
},
'structure': {
'message': {
'$actions': [
{
'who' : 'anyone',
'can' : 'write'
},
{
'who' : 'author',
'of' : 'message',
'can' : 'read'
},
{
'who' : 'recipient',
'of' : 'message',
'can' : 'read'
}
]
}
}
}
Configure Protocol
const { status: configureStatus, protocol } = await sdk.dwn.protocols.configure({
message: {
definition: protocolDef
}
});
console.log('configure protocol local status', configureStatus);
Create a Message from Alice to Bob
const { record, status } = await sdk.dwn.records.create({
data,
message : {
protocol,
protocolPath,
schema,
recipient: did:ion:bob
}
});
if (status.code !== 202) {
console.error(`${status.code} - ${status.detail}`);
return;
}
const shortDid = did.substr(0, 22);
console.info(`Record written! sending to ${shortDid}...`);
const { status: sendStatus } = await record.send(bob);
if (sendStatus.code !== 202) {
console.error(`${sendStatus.code} - ${sendStatus.detail}`);
return;
}
console.info(`Sent to ${shortDid}!`);
Reading a Message to Bob from Alice
const { records, status } = await sdk.dwn.records.query({
message: {
filter: {
protocol: pingerProtocolDefinition.protocol
},
dateSort: 'createdDescending'
}
});
if (status.code !== 200) {
console.error('Failed to query', status);
return;
}
for (let record of records) {
const { author, note } = await record.data.json();
console.info(`${author.substr(0, 22)}... ${note}`);
}