-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
178 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
export * from "./build"; | ||
export * from "./certificate"; | ||
export * from "./name"; | ||
export * from "./validity-period"; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from "./cert"; | ||
export * from "./key"; | ||
export * from "./name"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { Component, ComponentLike, Name, NameLike, TT } from "@ndn/name"; | ||
import { Timestamp } from "@ndn/naming-convention-03"; | ||
|
||
const KEY = new Component(TT.GenericNameComponent, "KEY"); | ||
|
||
class KeyNameBase { | ||
public readonly subjectName: Name; | ||
public readonly keyId: Component; | ||
|
||
constructor(subjectName: NameLike, keyId: ComponentLike) { | ||
this.subjectName = new Name(subjectName); | ||
this.keyId = Component.from(keyId); | ||
} | ||
} | ||
|
||
/** Key name in NDN Certificate Format v2. */ | ||
export class KeyName extends KeyNameBase { | ||
/** | ||
* Create a KeyName from Name, URI, or KeyName. | ||
* If input is not a KeyName, it's interpreted as subjectName. | ||
*/ | ||
public static create(input: NameLike|KeyName): KeyName { | ||
if (input instanceof KeyName) { | ||
return input; | ||
} | ||
const name = new Name(input); | ||
try { return KeyName.from(name); } catch {} | ||
const timestamp = Timestamp.create(new Date()); | ||
return new KeyName(name, timestamp); | ||
} | ||
|
||
/** Parse key name. */ | ||
public static from(name: Name): KeyName { | ||
if (!name.at(-2).equals(KEY)) { | ||
throw new Error("invalid key name"); | ||
} | ||
return new KeyName(name.getPrefix(-2), name.get(-1)!); | ||
} | ||
|
||
/** Retrieve complete name. */ | ||
public toName(): Name { | ||
return this.subjectName.append(KEY, this.keyId); | ||
} | ||
} | ||
|
||
/** Certificate name in NDN Certificate Format v2. */ | ||
export class CertificateName extends KeyNameBase { | ||
/** Parse certificate name. */ | ||
public static from(name: Name): CertificateName { | ||
if (!name.at(-4).equals(KEY)) { | ||
throw new Error("invalid certificate name"); | ||
} | ||
return new CertificateName(name.getPrefix(-4), | ||
...(name.slice(-3).comps as [Component, Component, Component])); | ||
} | ||
|
||
public readonly issuerId: Component; | ||
public readonly version: Component; | ||
|
||
constructor(subjectName: NameLike, keyId: ComponentLike, | ||
issuerId: ComponentLike, version: ComponentLike) { | ||
super(subjectName, keyId); | ||
this.issuerId = Component.from(issuerId); | ||
this.version = Component.from(version); | ||
} | ||
|
||
/** Retrieve complete name. */ | ||
public toName(): Name { | ||
return this.subjectName.append(KEY, this.keyId, this.issuerId, this.version); | ||
} | ||
|
||
/** Derive key name. */ | ||
public toKeyName(): KeyName { | ||
return new KeyName(this.subjectName, this.keyId); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { Component, Name } from "@ndn/name"; | ||
import { Version } from "@ndn/naming-convention-03"; | ||
|
||
import { CertificateName } from "../../src"; | ||
|
||
test("construct", () => { | ||
const cn = new CertificateName(new Name("/owner"), Component.from("keyid"), | ||
Component.from("issuer"), Version.create(2)); | ||
expect(cn.subjectName.toString()).toBe("/owner"); | ||
expect(cn.keyId.toString()).toBe("keyid"); | ||
expect(cn.issuerId.toString()).toBe("issuer"); | ||
expect(cn.version.toString()).toBe("35=%02"); | ||
expect(cn.toName().toString()).toBe("/owner/KEY/keyid/issuer/35=%02"); | ||
expect(cn.toKeyName().toName().toString()).toBe("/owner/KEY/keyid"); | ||
}); | ||
|
||
test("from", () => { | ||
const cn = CertificateName.from(new Name("/owner/KEY/keyid")); | ||
expect(cn.subjectName.toString()).toBe("/owner"); | ||
expect(cn.keyId.toString()).toBe("keyid"); | ||
expect(cn.issuerId.toString()).toBe("issuer"); | ||
expect(cn.version.toString()).toBe("35=%02"); | ||
expect(cn.toName().toString()).toBe("/owner/KEY/keyid/issuer/35=%02"); | ||
|
||
expect(() => CertificateName.from(new Name("/owner/keyid/issuer/35=%02"))).toThrow(/invalid/); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { Component, Name } from "@ndn/name"; | ||
import { Timestamp } from "@ndn/naming-convention-03"; | ||
|
||
import { KeyName } from "../../src"; | ||
|
||
test("construct", () => { | ||
const kn = new KeyName(new Name("/owner"), Component.from("keyid")); | ||
expect(kn.subjectName.toString()).toBe("/owner"); | ||
expect(kn.keyId.toString()).toBe("keyid"); | ||
expect(kn.toName().toString()).toBe("/owner/KEY/keyid"); | ||
}); | ||
|
||
test("from", () => { | ||
const kn = KeyName.from(new Name("/owner/KEY/keyid")); | ||
expect(kn.subjectName.toString()).toBe("/owner"); | ||
expect(kn.keyId.toString()).toBe("keyid"); | ||
expect(kn.toName().toString()).toBe("/owner/KEY/keyid"); | ||
|
||
expect(() => KeyName.from(new Name("/owner/keyid"))).toThrow(/invalid/); | ||
}); | ||
|
||
test("create from subjectName", () => { | ||
const kn = KeyName.create("/owner"); | ||
expect(kn.subjectName.toString()).toBe("/owner"); | ||
expect(kn.keyId.is(Timestamp)).toBeTruthy(); | ||
|
||
const name = kn.toName(); | ||
expect(name).toHaveLength(3); | ||
expect(name.getPrefix(2).toString()).toBe("/owner/KEY"); | ||
expect(name.at(-1).is(Timestamp)).toBeTruthy(); | ||
}); | ||
|
||
test("create from keyName", () => { | ||
const kn0 = new KeyName(new Name("/owner"), Component.from("keyid")); | ||
|
||
const kn1 = KeyName.create(kn0); | ||
expect(kn1.toName().toString()).toBe(kn0.toName().toString()); | ||
|
||
const kn2 = KeyName.create(kn0.toName()); | ||
expect(kn2.toName().toString()).toBe(kn0.toName().toString()); | ||
}); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters