mirror of
https://github.com/Blah-IM/typescript-core.git
synced 2025-06-23 16:31:08 +00:00
feat: Add Identity class documentation and signPayload method
This commit is contained in:
parent
46f489bfee
commit
481da78cae
1 changed files with 26 additions and 6 deletions
|
@ -2,12 +2,23 @@ import {
|
|||
BlahKeyPair,
|
||||
BlahPublicKey,
|
||||
type BlahSignedPayload,
|
||||
type SignOrVerifyOptions,
|
||||
} from "../crypto/mod.ts";
|
||||
import { type ActKeyUpdate, BlahActKey } from "./actKey.ts";
|
||||
import { blahIdentityDescriptionSchema } from "./identityDescription.ts";
|
||||
import type { BlahIdentityDescription } from "./mod.ts";
|
||||
import { type BlahProfile, blahProfileSchema } from "./profile.ts";
|
||||
|
||||
/**
|
||||
* Object representing an identity.
|
||||
*
|
||||
* This object manages the identity's ID key and acting keys, as well as the profile.
|
||||
*
|
||||
* There are 3 major configurations this object can be in:
|
||||
* - *Full Access*: Private key of ID key is available. All methods are available.
|
||||
* - *Limited Access*: Private key of (at least) one act key is avilable. Key management methods are unavailable.
|
||||
* - *Read Only*: No private keys are available. Only inspection & integrity verification are available.
|
||||
*/
|
||||
export class BlahIdentity {
|
||||
private internalIdKey: BlahPublicKey | BlahKeyPair;
|
||||
private internalActKeys: BlahActKey[];
|
||||
|
@ -44,6 +55,10 @@ export class BlahIdentity {
|
|||
return this.internalActKeys;
|
||||
}
|
||||
|
||||
get actingKey(): BlahActKey | undefined {
|
||||
return this.internalActKeys.find((k) => k.canSign);
|
||||
}
|
||||
|
||||
static async fromIdentityDescription(
|
||||
identityDesc: unknown,
|
||||
idKeyPair?: BlahKeyPair,
|
||||
|
@ -142,13 +157,18 @@ export class BlahIdentity {
|
|||
await key.update(update, this.internalIdKey);
|
||||
}
|
||||
|
||||
async updateProfile(profile: BlahProfile) {
|
||||
const signingActKey = this.internalActKeys.find((k) => k.canSign);
|
||||
if (!signingActKey) {
|
||||
throw new Error("No act key to sign profile with.");
|
||||
}
|
||||
/** Sign a payload with the acting key. */
|
||||
signPayload<P>(
|
||||
payload: P,
|
||||
options?: Omit<SignOrVerifyOptions, "identityKeyId">,
|
||||
): Promise<BlahSignedPayload<P>> {
|
||||
if (!this.actingKey) throw new Error("No act key to sign payload with.");
|
||||
|
||||
this.rawProfile = await signingActKey.signPayload(profile);
|
||||
return this.actingKey.signPayload(payload, options);
|
||||
}
|
||||
|
||||
async updateProfile(profile: BlahProfile) {
|
||||
this.rawProfile = await this.signPayload(profile);
|
||||
this.internalProfileSigValid = true;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue