Skip to content

Commit

Permalink
Remove toCapability & rename parse -> tryParsing
Browse files Browse the repository at this point in the history
  • Loading branch information
matheus23 committed Dec 13, 2021
1 parent 20b7478 commit e9450e9
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 34 deletions.
5 changes: 2 additions & 3 deletions src/attenuation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import * as util from "./util"


export interface CapabilitySemantics<A> {
parse(cap: Capability): A | null
toCapability(parsedCap: A): Capability
tryParsing(cap: Capability): A | null
/**
* This figures out whether a given `childCap` can be delegated from `parentCap`.
* There are three possible results with three return types respectively:
Expand Down Expand Up @@ -51,7 +50,7 @@ export function capabilities<A>(
function* findParsingCaps(ucan: Ucan<never>): Iterable<A & CapabilityInfo> {
const capInfo = parseCapabilityInfo(ucan)
for (const cap of ucan.payload.att) {
const parsedCap = capability.parse(cap)
const parsedCap = capability.tryParsing(cap)
if (parsedCap != null) yield { ...parsedCap, ...capInfo }
}
}
Expand Down
25 changes: 2 additions & 23 deletions src/capability/wnfs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export const wnfsPublicSemantics: CapabilitySemantics<WnfsPublicCapability> = {
* }
* ```
*/
parse(cap: Capability): WnfsPublicCapability | null {
tryParsing(cap: Capability): WnfsPublicCapability | null {
if (typeof cap.wnfs !== "string" || !isWnfsCap(cap.cap)) return null

// remove trailing slash
Expand All @@ -57,13 +57,6 @@ export const wnfsPublicSemantics: CapabilitySemantics<WnfsPublicCapability> = {
}
},

toCapability(parsed: WnfsPublicCapability): Capability {
return {
wnfs: `${parsed.user}/public/${parsed.publicPath.join("/")}`,
cap: parsed.cap,
}
},

tryDelegating<T extends WnfsPublicCapability>(parentCap: T, childCap: T): T | null | CapabilityEscalation<WnfsPublicCapability> {
// need to delegate the same user's file system
if (childCap.user !== parentCap.user) return null
Expand Down Expand Up @@ -118,7 +111,7 @@ const wnfsPrivateSemantics: CapabilitySemantics<WnfsPrivateCapability> = {
* }
* ```
*/
parse(cap: Capability): WnfsPrivateCapability | null {
tryParsing(cap: Capability): WnfsPrivateCapability | null {
if (typeof cap.wnfs !== "string" || !isWnfsCap(cap.cap)) return null

// split up "boris.fission.name/private/fccXmZ8HYmpwxkvPSjwW9A" into "<user>/private/<inumberBase64url>"
Expand All @@ -135,20 +128,6 @@ const wnfsPrivateSemantics: CapabilitySemantics<WnfsPrivateCapability> = {
}
},

toCapability(parsed: WnfsPrivateCapability): Capability {
const inumbers = Array.from(parsed.requiredINumbers.values())
const [inumber] = inumbers
if (inumbers.length !== 1 || inumber == null) {
// Private wnfs capabilities will only have an encoding with a single inumber.
// Multiple inumbers are the result of delegations with multiple private capabilities interacting.
throw new Error(`Can only construct a private capability with exactly one inumber.`)
}
return {
wnfs: `${parsed.user}/private/${inumber}`,
cap: parsed.cap,
}
},

tryDelegating<T extends WnfsPrivateCapability>(parentCap: T, childCap: T): T | null | CapabilityEscalation<WnfsPrivateCapability> {
// If the users don't match, these capabilities are unrelated.
if (childCap.user !== parentCap.user) return null
Expand Down
9 changes: 1 addition & 8 deletions tests/emailCapabilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export interface EmailCapability {

export const emailSemantics: CapabilitySemantics<EmailCapability> = {

parse(cap: Capability): EmailCapability | null {
tryParsing(cap: Capability): EmailCapability | null {
if (typeof cap.email === "string" && cap.cap === "SEND") {
return {
email: cap.email,
Expand All @@ -21,13 +21,6 @@ export const emailSemantics: CapabilitySemantics<EmailCapability> = {
return null
},

toCapability(parsed: EmailCapability): Capability {
return {
email: parsed.email,
cap: parsed.cap,
}
},

tryDelegating<T extends EmailCapability>(parentCap: T, childCap: T): T | null | CapabilityEscalation<EmailCapability> {
// potency is always "SEND" anyway, so doesn't need to be checked
return childCap.email === parentCap.email ? childCap : null
Expand Down

0 comments on commit e9450e9

Please sign in to comment.