Skip to content

Commit

Permalink
Store exchange key in well known location (#285)
Browse files Browse the repository at this point in the history
* Store exchange key in well known location

* Fix test

* Remove console.log

* Make contents an empty object

* Update CHANGELOG

* Add hasPublicExchangeKey function

* Mention hasPublicExchangeKey in changelog

* Store as directory instead
  • Loading branch information
icidasset committed Sep 8, 2021
1 parent 26fe276 commit ed5ee65
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 1 deletion.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
42 changes: 41 additions & 1 deletion src/fs/filesystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -53,6 +54,16 @@ type MutationOptions = {
}


// CONSTANTS


export const EXCHANGE_PATH: DirectoryPath = pathing.directory(
pathing.Branch.Public,
".well-known",
"exchange"
)


// CLASS


Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -212,6 +223,7 @@ export class FileSystem {
return this
}


// POSIX INTERFACE (FILES)
// -----------------------

Expand Down Expand Up @@ -247,6 +259,7 @@ export class FileSystem {
return this.add(path, content, options)
}


// POSIX INTERFACE (GENERAL)
// -------------------------

Expand Down Expand Up @@ -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<void> {
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<boolean> {
const publicDid = await did.exchange()

return this.exists(
pathing.combine(EXCHANGE_PATH, pathing.directory(publicDid))
)
}


// INTERNAL
// --------
Expand Down
22 changes: 22 additions & 0 deletions tests/fs/exchange.node.test.ts
Original file line number Diff line number Diff line change
@@ -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)
})

})

0 comments on commit ed5ee65

Please sign in to comment.