8000 Use stricter types for path creation by icidasset · Pull Request #466 · oddsdk/ts-odd · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Use stricter types for path creation #466

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jan 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
- Adds `program.accountDID(username)` shorthand method to retrieve the root DID of a given account username.
- Adds the file system data functions as shorthand methods.

#### Other

- Introduces stricter types for paths to restrict the paths you can use with various functions and also to guide you a bit more.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've put this here since it shouldn't be a breaking change.
Unless you've been using the file system incorrectly 🙈



### v0.35.2

Expand Down
8 changes: 4 additions & 4 deletions src/capabilities.ts
10000
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export type Capabilities = {

export type FileSystemSecret = {
bareNameFilter: string
path: Path.DistinctivePath
path: Path.Distinctive<Path.Segments>
readKey: Uint8Array
}

Expand Down Expand Up @@ -77,7 +77,7 @@ export async function collectSecret({ accountDID, bareNameFilter, crypto, path,
accountDID: string
bareNameFilter: string
crypto: Crypto.Implementation
path: Path.DistinctivePath
path: Path.DistinctivePath<Path.Segments>
readKey: Uint8Array
storage: Storage.Implementation
}) {
Expand Down Expand Up @@ -166,10 +166,10 @@ export async function validateSecrets(
permissions: Permissions.Permissions
): Promise<boolean> {
return Permissions.permissionPaths(permissions).reduce(
(acc: Promise<boolean>, path: Path.DistinctivePath): Promise<boolean> =>
(acc: Promise<boolean>, path: Path.Distinctive<Path.Partitioned<Path.Partition>>): Promise<boolean> =>
acc.then(async (bool: boolean) => {
if (bool === false) return bool
if (Path.isBranch(Path.Branch.Public, path)) return bool
if (Path.isPartition(Path.RootBranch.Public, path)) return bool

const keyName = await Identifiers.readKey({ accountDID, crypto, path })
return crypto.keystore.keyExists(keyName)
Expand Down
4 changes: 2 additions & 2 deletions src/common/identifiers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import * as Path from "../path/index.js"
type Arguments = {
crypto: Crypto.Implementation
accountDID: string
path: DistinctivePath
path: DistinctivePath<Path.Segments>
}


Expand All @@ -30,7 +30,7 @@ export async function readKey(
// 🛠


async function pathHash(crypto: Crypto.Implementation, path: DistinctivePath): Promise<string> {
async function pathHash(crypto: Crypto.Implementation, path: DistinctivePath<Path.Segments>): Promise<string> {
return Uint8Arrays.toString(
await crypto.hash.sha256(
Uint8Arrays.fromString("/" + Path.unwrap(path).join("/"), "utf8")
Expand Down
2 changes: 1 addition & 1 deletion src/common/root-key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,6 @@ export function toString(a: Uint8Array): string {


function identifier(crypto: Crypto.Implementation, accountDID: string): Promise<string> {
const path = Path.directory(Path.Branch.Private)
const path = Path.directory(Path.RootBranch.Private)
return Identifiers.readKey({ crypto, path, accountDID })
}
6 changes: 3 additions & 3 deletions src/filesystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import * as Ucan from "./ucan/index.js"
import * as Versions from "./fs/versions.js"

import { AuthenticationStrategy } from "./index.js"
import { Branch } from "./path/index.js"
import { RootBranch } from "./path/index.js"
import { Configuration } from "./configuration.js"
import { Dependencies } from "./fs/filesystem.js"
import { Maybe, decodeCID, EMPTY_CID } from "./common/index.js"
Expand Down Expand Up @@ -194,12 +194,12 @@ export async function checkFileSystemVersion(
const links = await Protocol.basic.getSimpleLinks(depot, filesystemCID)
// if there's no version link, we assume it's from a 1.0.0-compatible version
// (from before ~ November 2020)
const versionStr = links[ Branch.Version ] == null
const versionStr = links[ RootBranch.Version ] == null
? "1.0.0"
: new TextDecoder().decode(
await Protocol.basic.getFile(
depot,
decodeCID(links[ Branch.Version ].cid)
decodeCID(links[ RootBranch.Version ].cid)
)
)

Expand Down
2 changes: 1 addition & 1 deletion src/fs/bare/tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import * as Link from "../link.js"
import { HardLinks, BaseLinks, Tree, File, Puttable, UpdateCallback, PuttableUnixTree } from "../types.js"
import { Maybe, decodeCID } from "../../common/index.js"
import { isObject, hasProp } from "../../common/type-checks.js"
import { Path } from "../../path/index.js"
import { Segments as Path } from "../../path/index.js"

import BareFile from "../bare/file.js"
import BaseTree from "../base/tree.js"
Expand Down
2 changes: 1 addition & 1 deletion src/fs/base/tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as Check from "../types/check.js"
import * as Pathing from "../../path/index.js"

import { Maybe } from "../../common/index.js"
import { Path } from "../../path/index.js"
import { Segments as Path } from "../../path/index.js"
import { PutResult } from "../../components/depot/implementation.js"
import { Tree, File, UnixTree, Links, UpdateCallback } from "../types.js"

Expand Down
14 changes: 6 additions & 8 deletions src/fs/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,20 @@ import * as FileSystem from "../fs/types.js"
import * as Path from "../path/index.js"
import * as Sharing from "./share.js"

import { Branch } from "../path/index.js"


/**
* Adds some sample to the file system.
*/
export async function addSampleData(fs: FileSystem.API): Promise<void> {
await fs.mkdir(Path.directory(Branch.Private, "Apps"))
await fs.mkdir(Path.directory(Branch.Private, "Audio"))
await fs.mkdir(Path.directory(Branch.Private, "Documents"))
await fs.mkdir(Path.directory(Branch.Private, "Photos"))
await fs.mkdir(Path.directory(Branch.Private, "Video"))
await fs.mkdir(Path.directory("private", "Apps"))
await fs.mkdir(Path.directory("private", "Audio"))
await fs.mkdir(Path.directory("private", "Documents"))
await fs.mkdir(Path.directory("private", "Photos"))
await fs.mkdir(Path.directory("private", "Video"))

// Files
await fs.write(
Path.file(Branch.Private, "Welcome.txt"),
Path.file("private", "Welcome.txt"),
new TextEncoder().encode("Welcome to your personal transportable encrypted file system 👋")
)
}
Expand Down
Loading
0