Skip to content
This repository has been archived by the owner on Jan 1, 2025. It is now read-only.

Commit

Permalink
fix: update indexdb node example to use [email protected]
Browse files Browse the repository at this point in the history
  • Loading branch information
chereseeriepa committed Jul 26, 2024
1 parent 38b90a6 commit aa73504
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 67 deletions.
2 changes: 1 addition & 1 deletion examples/node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"scripts": {
"start:inmemory": "node src/inmemory.js",
"start:leveldb": "node src/leveldb.js",
"start:indexdb": "node src/leveldb.js"
"start:indexdb": "node src/indexdb.js"
},
"author": "",
"license": "ISC",
Expand Down
149 changes: 83 additions & 66 deletions examples/node/src/indexdb.js
Original file line number Diff line number Diff line change
@@ -1,80 +1,97 @@
import "fake-indexeddb/auto";
async function createTestScenario() {

const PlutoEncrypted = await import('@pluto-encrypted/database')
const { default: IndexDB } = await import("@pluto-encrypted/indexdb")
const SDK = await import("@atala/prism-wallet-sdk");
import IndexDB from "@pluto-encrypted/indexdb";
import SDK from "@atala/prism-wallet-sdk";

const { Database } = PlutoEncrypted;
const defaultPassword = new Uint8Array(32).fill(1);
const mediatorDID = SDK.Domain.DID.fromString(
"did:peer:2.Ez6LSghwSE437wnDE1pt3X6hVDUQzSjsHzinpX3XFvMjRAm7y.Vz6Mkhh1e5CEYYq6JBUcTZ6Cp2ranCWRrv7Yax3Le4N59R6dd.SeyJ0IjoiZG0iLCJzIjoiaHR0cHM6Ly9iZXRhLW1lZGlhdG9yLmF0YWxhcHJpc20uaW8iLCJyIjpbXSwiYSI6WyJkaWRjb21tL3YyIl19"
);
const apollo = new SDK.Apollo();
const api = new SDK.ApiImpl();
const castor = new SDK.Castor(apollo);
const pluto = await Database.createEncrypted(
{
name: `my-db`,
encryptionKey: defaultPassword,
storage: IndexDB,
}
);
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 {
SDK,
apollo,
seed,
agent,
mercury,
pluto,
castor,
};

// TODO: move DID to env
// https://sit-prism-mediator.atalaprism.io/ - NOTE: currently returning a 504
// const mediatorDIDString = 'did:peer:2.Ez6LSghwSE437wnDE1pt3X6hVDUQzSjsHzinpX3XFvMjRAm7y.Vz6Mkhh1e5CEYYq6JBUcTZ6Cp2ranCWRrv7Yax3Le4N59R6dd.SeyJ0IjoiZG0iLCJzIjp7InVyaSI6Imh0dHBzOi8vc2l0LXByaXNtLW1lZGlhdG9yLmF0YWxhcHJpc20uaW8iLCJhIjpbImRpZGNvbW0vdjIiXX19.SeyJ0IjoiZG0iLCJzIjp7InVyaSI6IndzczovL3NpdC1wcmlzbS1tZWRpYXRvci5hdGFsYXByaXNtLmlvL3dzIiwiYSI6WyJkaWRjb21tL3YyIl19fQ'

// local mediator did
const mediatorDIDString = 'did:peer:2.Ez6LSghwSE437wnDE1pt3X6hVDUQzSjsHzinpX3XFvMjRAm7y.Vz6Mkhh1e5CEYYq6JBUcTZ6Cp2ranCWRrv7Yax3Le4N59R6dd.SeyJ0IjoiZG0iLCJzIjp7InVyaSI6Imh0dHBzOi8vc2FuZGJveC1tZWRpYXRvci5hdGFsYXByaXNtLmlvIiwiYSI6WyJkaWRjb21tL3YyIl19fQ.SeyJ0IjoiZG0iLCJzIjp7InVyaSI6IndzczovL3NhbmRib3gtbWVkaWF0b3IuYXRhbGFwcmlzbS5pby93cyIsImEiOlsiZGlkY29tbS92MiJdfX0'

async function createAgent () {
const mediatorDID = SDK.Domain.DID.fromString(mediatorDIDString)

const store = new SDK.Store({
name: 'test',
storage: IndexDB,
password: Buffer.from("demoapp").toString("hex")
})

const apollo = new SDK.Apollo()
const seed = apollo.createRandomSeed();

const pluto = new SDK.Pluto(
store,
apollo
)

return {
agent: SDK.Agent.initialize({ mediatorDID, pluto, apollo, seed: seed.seed }),
seed
}
}

(async () => {

// create the SDK agent
const {
seed,
agent
} = await createAgent()
.catch(err => console.error('Failed to create the agent', err))
console.log('Agent created')

const { seed, agent, SDK } = await createTestScenario();
// start the SDK agent
await agent.start()
.catch(err => console.error('Failed to start the agent', err))

agent.addListener(SDK.ListenerKey.MESSAGE, (message) => {
console.log("Got new message", message);
});
console.log('Agent started and mediation has been established')

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

// listen for new messages
agent.addListener(SDK.ListenerKey.MESSAGE, async (message) => {
console.log("Got new message", message);

// await agent.stop()
// .then(() => console.log('Agent stopped'))
});

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


// Send a message
try {
console.log('Creating test message')
const secondaryDID = await agent.createNewPeerDID([], true);
const message = new SDK.BasicMessage(
{ content: "Test Message" },
secondaryDID,
secondaryDID,
);

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.",
);
await agent.sendMessage(message.makeMessage());
await agent.sendMessage(message.makeMessage());

console.log('Test message sent!')
} catch (err) {
if (/Malformed: Message is not a valid JWE, JWS or JWM/.test(err.message)) {
// NOTE: Safe to ignore, mediator returns null on successfully receiving the message, unpack fails.
console.log('Test message sent!')
} else {
throw err
}
}
})();





0 comments on commit aa73504

Please sign in to comment.