-
Notifications
You must be signed in to change notification settings - Fork 4
Feat/core id creation logic #99
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
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
dcd1b00
feat: create w3id builder
coodos 57b44bc
fix: w3id builder
coodos 454b7ea
feat: add global config var for w3id
coodos 9e0580a
chore: add docs
coodos d2fca54
chore: change rand to crng
coodos 9e2f093
chore: add ts type again
coodos 9791ea6
chore: fix lint and format
coodos e8ed604
chore: add w3id tests github workflow
coodos File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 +1,121 @@ | ||
export default {}; | ||
import { IDLogManager } from "./logs/log-manager"; | ||
import type { LogEvent, Signer } from "./logs/log.types"; | ||
import type { StorageSpec } from "./logs/storage/storage-spec"; | ||
import { generateRandomAlphaNum } from "./utils/rand"; | ||
import { v4 as uuidv4 } from "uuid"; | ||
import { generateUuid } from "./utils/uuid"; | ||
|
||
export class W3ID { | ||
constructor( | ||
public id: string, | ||
public logs?: IDLogManager, | ||
) {} | ||
} | ||
|
||
export class W3IDBuilder { | ||
private signer?: Signer; | ||
private repository?: StorageSpec<LogEvent, LogEvent>; | ||
private entropy?: string; | ||
private namespace?: string; | ||
private nextKeyHash?: string; | ||
private global?: boolean = false; | ||
|
||
/** | ||
* Specify entropy to create the identity with | ||
* | ||
* @param {string} str | ||
*/ | ||
public withEntropy(str: string): W3IDBuilder { | ||
this.entropy = str; | ||
return this; | ||
} | ||
|
||
/** | ||
* Specify namespace to use to generate the UUIDv5 | ||
* | ||
* @param {string} uuid | ||
*/ | ||
public withNamespace(uuid: string): W3IDBuilder { | ||
this.namespace = uuid; | ||
return this; | ||
} | ||
|
||
/** | ||
* Specify whether to create a global identifier or a local identifer | ||
* | ||
* According to the project specification there are supposed to be 2 main types of | ||
* W3ID's ones which are tied to more permanent entities | ||
* | ||
* A global identifer is expected to live at the registry and starts with an \`@\` | ||
* | ||
* @param {boolean} isGlobal | ||
*/ | ||
public withGlobal(isGlobal: boolean): W3IDBuilder { | ||
this.global = isGlobal; | ||
return this; | ||
} | ||
|
||
/** | ||
* Add a logs repository to the W3ID, a rotateble key attached W3ID would need a | ||
* repository in which the logs would be stored | ||
* | ||
* @param {StorageSpec<LogEvent, LogEvent>} storage | ||
*/ | ||
public withRepository(storage: StorageSpec<LogEvent, LogEvent>): W3IDBuilder { | ||
this.repository = storage; | ||
return this; | ||
} | ||
|
||
/** | ||
* Attach a keypair to the W3ID, a key attached W3ID would also need a repository | ||
* to be added. | ||
* | ||
* @param {Signer} signer | ||
*/ | ||
public withSigner(signer: Signer): W3IDBuilder { | ||
this.signer = signer; | ||
return this; | ||
} | ||
|
||
/** | ||
* Specify the SHA256 hash of the next key which will sign the next log entry after | ||
* rotation of keys | ||
* | ||
* @param {string} hash | ||
*/ | ||
public withNextKeyHash(hash: string): W3IDBuilder { | ||
this.nextKeyHash = hash; | ||
return this; | ||
} | ||
|
||
/** | ||
* Build the W3ID with provided builder options | ||
* | ||
* @returns Promise<W3ID> | ||
*/ | ||
public async build(): Promise<W3ID> { | ||
this.entropy = this.entropy ?? generateRandomAlphaNum(); | ||
this.namespace = this.namespace ?? uuidv4(); | ||
const id = `${ | ||
this.global ? "@" : "" | ||
}${generateUuid(this.entropy, this.namespace)}`; | ||
if (!this.signer) { | ||
return new W3ID(id); | ||
} | ||
if (!this.repository) | ||
throw new Error( | ||
"Repository is required, pass with `withRepository` method", | ||
); | ||
|
||
if (!this.nextKeyHash) | ||
throw new Error( | ||
"NextKeyHash is required pass with `withNextKeyHash` method", | ||
); | ||
const logs = new IDLogManager(this.repository, this.signer); | ||
await logs.createLogEvent({ | ||
id, | ||
nextKeyHashes: [this.nextKeyHash], | ||
}); | ||
return new W3ID(id, logs); | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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,19 @@ | ||
/** | ||
* Generate a random alphanumeric sequence with set length | ||
* | ||
* @param {number} length length of the alphanumeric string you want | ||
* @returns {string} | ||
*/ | ||
|
||
export function generateRandomAlphaNum(length = 16): string { | ||
const chars = | ||
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; | ||
let result = ""; | ||
const charsLength = chars.length; | ||
|
||
for (let i = 0; i < length; i++) { | ||
result += chars.charAt(Math.floor(Math.random() * charsLength)); | ||
} | ||
|
||
return result; | ||
} | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.