Skip to content

Commit

Permalink
Implement hasCapability and fix typo in builder.ts (#53)
Browse files Browse the repository at this point in the history
* Fix typo

* Implement `hasCapability`

* Fix typo in test
  • Loading branch information
matheus23 authored Mar 16, 2022
1 parent 92d2281 commit 979ea39
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
23 changes: 23 additions & 0 deletions src/attenuation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,29 @@ export function isCapabilityEscalation<A>(obj: unknown): obj is CapabilityEscala
&& util.hasProp(obj, "capability")
}

export function hasCapability<Cap>(semantics: CapabilitySemantics<Cap>, capability: CapabilityWithInfo<Cap>, ucan: Chained): CapabilityWithInfo<Cap> | false {
for (const cap of capabilities(ucan, semantics)) {
if (isCapabilityEscalation(cap)) {
continue
}

const delegatedCapability = semantics.tryDelegating(cap.capability, capability.capability)

if (isCapabilityEscalation(delegatedCapability)) {
continue
}

if (delegatedCapability != null) {
return {
info: delegateCapabilityInfo(capability.info, cap.info),
capability: delegatedCapability,
}
}
}

return false
}

export function canDelegate<A>(semantics: CapabilitySemantics<A>, capability: A, ucan: Chained): boolean {
for (const cap of capabilities(ucan, semantics)) {
if (isCapabilityEscalation(cap)) {
Expand Down
4 changes: 2 additions & 2 deletions src/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,13 @@ export class Builder<State extends Partial<BuildableState>> {
if (!isFinite(seconds) || seconds <= 0) {
throw new TypeError(`Expected seconds to be a positive number, but got ${seconds}`)
}
return this.withExpiraton(Math.floor(Date.now() / 1000) + seconds)
return this.withExpiration(Math.floor(Date.now() / 1000) + seconds)
}

/**
* @param expiration The UTCTime timestamp (in seconds) for when the UCAN should expire.
*/
withExpiraton(expiration: number): Builder<State & { expiration: number }> {
withExpiration(expiration: number): Builder<State & { expiration: number }> {
if (typeof expiration !== "number" || !isFinite(expiration)) {
throw new TypeError(`Expected expiration as number, but got ${expiration}`)
}
Expand Down
2 changes: 1 addition & 1 deletion tests/builder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe("Builder", () => {
const ucan = await Builder.create()
.issuedBy(alice)
.toAudience(bob.did())
.withExpiraton(expiration)
.withExpiration(expiration)
.withNotBefore(notBefore)
.withFact(fact1, fact2)
.withNonce()
Expand Down

0 comments on commit 979ea39

Please sign in to comment.