This repository has been archived by the owner on Jan 1, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: update indexdb node example to use [email protected]
- Loading branch information
1 parent
38b90a6
commit aa73504
Showing
2 changed files
with
84 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
})(); | ||
|
||
|
||
|
||
|
||
|