Skip to content

Instantly share code, notes, and snippets.

@frankhinek
Created March 10, 2023 02:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save frankhinek/21c15ae7a143ca987fb5204e2075869a to your computer and use it in GitHub Desktop.
Save frankhinek/21c15ae7a143ca987fb5204e2075869a to your computer and use it in GitHub Desktop.
DWN Experiments
import { DataStream, Dwn, MessageStoreLevel, RecordsQuery, RecordsWrite } from '@tbd54566975/dwn-sdk-js';
import { createProfile, randomBytes } from './utils.js';
const dwn = await Dwn.create({messageStore});
const alice = await createProfile();
const dataBytes = randomBytes(32);
let dataStream = DataStream.fromBytes(dataBytes);
const recordsWrite = await RecordsWrite.create({
data: dataBytes,
dataFormat: 'application/json',
authorizationSignatureInput: alice.signatureMaterial
});
const writeReply = await dwn.processMessage(alice.did, recordsWrite.message, dataStream);
const recordsQuery = await RecordsQuery.create({
filter : { recordId: recordsWrite.message.recordId },
authorizationSignatureInput : alice.signatureMaterial
});
const queryReply = await dwn.processMessage(alice.did, recordsQuery.message);
console.log('QUERY REPLY:', JSON.stringify(queryReply, null, 2));
const recordsWriteNew = await RecordsWrite.create({
dataCid: queryReply.entries[0].descriptor.dataCid,
dataFormat: 'application/json',
authorizationSignatureInput: alice.signatureMaterial
});
console.log('WRITE 2:', recordsWriteNew);
const writeReplyNew = await dwn.processMessage(alice.did, recordsWriteNew.message);
console.log(writeReplyNew);
import { DIDKey } from './did-key.js';
import { ProtocolsConfigure } from '@tbd54566975/dwn-sdk-js';
export async function createProfile() {
const { did, publicJWK, privateJWK } = await DIDKey.generate();
const { alg, kid } = publicJWK;
const signatureMaterial = {
protectedHeader: { alg, kid },
privateJwk: privateJWK
};
const response = {
connected: true,
did,
keyChain: true,
endpoint: 'app://dwn',
keypair: { publicJwk: publicJWK, privateJwk: privateJWK },
signatureMaterial
};
return response;
}
export async function createProtocol(profile, name, definition) {
return await ProtocolsConfigure.create({
protocol: name,
target: profile.did,
authorizationSignatureInput: profile.signatureMaterial,
definition,
});
}
/**
* Generates a random alpha-numeric string.
*/
export function randomString(length) {
const charset = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
// pick characters randomly
let randomString = '';
for (let i = 0; i < length; i++) {
randomString += charset.charAt(Math.floor(Math.random() * charset.length));
}
return randomString;
};
/**
* Generates a random byte array of given length.
*/
export function randomBytes(length) {
const randomBytes = new Uint8Array(length);
for (let i = 0; i < length; i++) {
randomBytes[i] = Math.floor(Math.random() * 256);
}
return randomBytes;
};
/**
* The maximum is exclusive and the minimum is inclusive
* @param {number} min
* @param {number} max
* @returns {number}
*/
export function randomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min) + min);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment