Skip to content

Commit

Permalink
deeplink
Browse files Browse the repository at this point in the history
  • Loading branch information
humanagent committed Jan 24, 2025

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 80a4de8 commit 74235f3
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// encryptDecryptExample.js

const { PrivateKeyBundleV1, SignedPublicKeyBundle } = require("@xmtp/proto");
const { InMemoryKeystore, InMemoryPersistence } = require("./path/to/your/keystore");
const { newWallet, dateToNs } = require("./path/to/your/utils");

async function runExample() {
// Step 1: Generate key bundles for sender and recipient
const senderKeys = await PrivateKeyBundleV1.generate(newWallet());
const recipientKeys = await PrivateKeyBundleV1.generate(newWallet());

// Create keystores for sender and recipient
const senderKeystore = await InMemoryKeystore.create(
senderKeys,
InMemoryPersistence.create(),
);
const recipientKeystore = await InMemoryKeystore.create(
recipientKeys,
InMemoryPersistence.create(),
);

// Step 2: Create an invite to establish a shared topic
const recipientPublicKeyBundle = SignedPublicKeyBundle.fromLegacyBundle(
recipientKeys.getPublicKeyBundle(),
);
const createdNs = dateToNs(new Date());
const inviteResponse = await senderKeystore.createInvite({
recipient: recipientPublicKeyBundle,
createdNs,
context: undefined,
consentProof: undefined,
});

// Step 3: Encrypt the message
const payload = new TextEncoder().encode("Hello, world!");
const headerBytes = new Uint8Array(10);
const { responses: [encrypted] } = await senderKeystore.encryptV2({
requests: [
{
contentTopic: inviteResponse.conversation!.topic,

Check failure on line 40 in index.js

GitHub Actions / Lint

Forbidden non-null assertion

Check failure on line 40 in index.js

GitHub Actions / Lint

Parsing error: Unexpected token, expected ","
payload,
headerBytes,
},
],
});

if (encrypted.error) {
throw encrypted.error;
}

// Step 4: Decrypt the message on the recipient's side
const { responses: [decrypted] } = await recipientKeystore.decryptV2({
requests: [
{
payload: encrypted.result?.encrypted,
headerBytes,
contentTopic: inviteResponse.conversation!.topic,

Check failure on line 57 in index.js

GitHub Actions / Lint

Forbidden non-null assertion
},
],
});

if (decrypted.error) {
throw decrypted.error;
}

// Verify the decrypted message
console.log(new TextDecoder().decode(decrypted.result!.decrypted)); // "Hello, world!"

Check failure on line 67 in index.js

GitHub Actions / Lint

Forbidden non-null assertion
}

runExample().catch(console.error);

0 comments on commit 74235f3

Please sign in to comment.