diff --git a/CHANGELOG.md b/CHANGELOG.md index acc4d0a4f..6a03ea8d2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # Changelog + +### v0.28.0 + +Added the `fs.addPublicExchangeKey()` function which adds the public exchange key of that domain/browser/device to your filesystem at `/public/.well-known/exchange/DID_KEY`. Along with the `fs.hasPublicExchangeKey()` to check if it's there. + + + ### v0.27.0 - Fixed `webnative.apps.index()`, and now returns a list of domains, along with their `insertedAt` and `modifiedAt` ISO8601 timestamps. diff --git a/src/fs/filesystem.ts b/src/fs/filesystem.ts index 1a67b1ee7..175d5e3e4 100644 --- a/src/fs/filesystem.ts +++ b/src/fs/filesystem.ts @@ -14,6 +14,7 @@ import * as cidLog from "../common/cid-log.js" import * as dataRoot from "../data-root.js" import * as debug from "../common/debug.js" import * as crypto from "../crypto/index.js" +import * as did from "../did/index.js" import * as pathing from "../path.js" import * as typeCheck from "./types/check.js" import * as ucan from "../ucan/index.js" @@ -53,6 +54,16 @@ type MutationOptions = { } +// CONSTANTS + + +export const EXCHANGE_PATH: DirectoryPath = pathing.directory( + pathing.Branch.Public, + ".well-known", + "exchange" +) + + // CLASS @@ -121,7 +132,7 @@ export class FileSystem { if (!this.localOnly) { // Publish when coming back online globalThis.addEventListener("online", this._whenOnline) - + // Show an alert when leaving the page while updating the data root globalThis.addEventListener("beforeunload", this._beforeLeaving) } @@ -212,6 +223,7 @@ export class FileSystem { return this } + // POSIX INTERFACE (FILES) // ----------------------- @@ -247,6 +259,7 @@ export class FileSystem { return this.add(path, content, options) } + // POSIX INTERFACE (GENERAL) // ------------------------- @@ -330,6 +343,33 @@ export class FileSystem { } + // COMMON + // ------ + + /** + * Stores the public part of the exchange key in the DID format, + * in the `/public/.well-known/exchange/DID_GOES_HERE/` directory. + */ + async addPublicExchangeKey(): Promise { + const publicDid = await did.exchange() + + await this.mkdir( + pathing.combine(EXCHANGE_PATH, pathing.directory(publicDid)) + ) + } + + /** + * Checks if the public exchange key was added in the well-known location. + * See `addPublicExchangeKey()` for the exact details. + */ + async hasPublicExchangeKey(): Promise { + const publicDid = await did.exchange() + + return this.exists( + pathing.combine(EXCHANGE_PATH, pathing.directory(publicDid)) + ) + } + // INTERNAL // -------- diff --git a/tests/fs/exchange.node.test.ts b/tests/fs/exchange.node.test.ts new file mode 100644 index 000000000..cb4babfbb --- /dev/null +++ b/tests/fs/exchange.node.test.ts @@ -0,0 +1,22 @@ +import expect from "expect" + +import "../../src/setup/node.js" + +import { EXCHANGE_PATH } from "../../src/fs/filesystem.js" +import { emptyFilesystem } from "../helpers/filesystem.js" + +import * as did from "../../src/did/index.js" +import * as path from "../../src/path.js" + + +describe("the filesystem", () => { + + it("adds public exchange key to well-known location", async function() { + const fs = await emptyFilesystem() + await fs.addPublicExchangeKey() + const exists = await fs.hasPublicExchangeKey() + + expect(exists).toEqual(true) + }) + +})