Skip to content

Commit

Permalink
fix: Including examples for cjs, esm, typescript and browser.
Browse files Browse the repository at this point in the history
  • Loading branch information
elribonazo committed Dec 8, 2023
1 parent c475b5b commit ee72e91
Show file tree
Hide file tree
Showing 16 changed files with 8,965 additions and 402 deletions.
2,754 changes: 2,754 additions & 0 deletions demos/node-cjs/package-lock.json

Large diffs are not rendered by default.

18 changes: 18 additions & 0 deletions demos/node-cjs/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "cjs",
"version": "1.0.0",
"description": "",
"main": "./src/index.js",
"type": "commonjs",
"scripts": {
"start": "node ./src/index.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"@atala/prism-wallet-sdk": "file:../..",
"@pluto-encrypted/database": "^0.0.8",
"@pluto-encrypted/inmemory": "^0.0.7",
"@pluto-encrypted/leveldb": "^0.0.6"
}
}
85 changes: 85 additions & 0 deletions demos/node-cjs/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
const SDK = require("@atala/prism-wallet-sdk");
const InMemory = require("@pluto-encrypted/inmemory");
const Database = require("@pluto-encrypted/database").Database;

const defaultPassword = new Uint8Array(32).fill(1);

let database;

async function createTestScenario(mediatorDID) {
database = await Database.createEncrypted(
{
name: `my-db`,
encryptionKey: defaultPassword,
storage: InMemory,
}
);
const apollo = new SDK.Apollo();
const api = new SDK.ApiImpl();
const castor = new SDK.Castor(apollo);
const pluto = database;
const didcomm = new SDK.DIDCommWrapper(apollo, castor, pluto);
const mercury = new SDK.Mercury(castor, didcomm, api);
const store = new SDK.PublicMediatorStore(pluto);
const handler = new SDK.BasicMediatorHandler(mediatorDID, mercury, store);
const manager = new SDK.ConnectionsManager(castor, mercury, pluto, handler);
const seed = apollo.createRandomSeed();
const agent = new SDK.Agent(
apollo,
castor,
pluto,
mercury,
handler,
manager,
seed.seed,
);
return {
apollo,
seed,
agent,
mercury,
pluto,
castor,
};
}

(async () => {
const mediatorDID = SDK.Domain.DID.fromString(
"did:peer:2.Ez6LSghwSE437wnDE1pt3X6hVDUQzSjsHzinpX3XFvMjRAm7y.Vz6Mkhh1e5CEYYq6JBUcTZ6Cp2ranCWRrv7Yax3Le4N59R6dd.SeyJ0IjoiZG0iLCJzIjoiaHR0cHM6Ly9iZXRhLW1lZGlhdG9yLmF0YWxhcHJpc20uaW8iLCJyIjpbXSwiYSI6WyJkaWRjb21tL3YyIl19"
);

const { seed, agent } = await createTestScenario(mediatorDID);

agent.addListener(SDK.ListenerKey.MESSAGE, async (message) => {
console.log("Got new message", message);
if (database) {
const content = await database.backup();
console.log(content);
}
});

await agent.start();
console.log(
`Welcome to PrismEdge Agent, state ${agent.state
} with mnemonics ${seed.mnemonics.join(", ")}`,
);

try {
const secondaryDID = await agent.createNewPeerDID([], true);
const message = new SDK.BasicMessage(
{ content: "Test Message" },
secondaryDID,
secondaryDID,
);

await agent.sendMessage(message.makeMessage());
await agent.sendMessage(message.makeMessage());
console.log("Sent");


} catch (err) {
console.log(
"Safe to ignore, mediator returns null on successfully receiving the message, unpack fails.",
);
}
})();
Loading

0 comments on commit ee72e91

Please sign in to comment.