@tidecloak/js
Version:
TideCloak client side JS SDK
157 lines • 5.19 kB
JavaScript
import { etc, Point } from "../../../Ed25519.js";
import { SHA512_Digest } from "../../../Hashing/Hash.js";
import { mod, RandomBigInt } from "../../../Math.js";
import { BigIntFromByteArray, BigIntToByteArray, StringToUint8Array } from "../../../Serialization.js";
import { Public, Private, BaseComponent, Seed, BaseSeedComponent, BasePrivateComponent, BasePublicComponent } from "../../BaseComponent.js";
import Ed25519Scheme from "./Ed25519Scheme.js";
export class Ed25519PublicComponent extends BasePublicComponent {
get Scheme() { return Ed25519Scheme; }
get ComponentType() { return Public; }
;
constructor(rawData) {
super();
/**@type {Uint8Array} */
this.pb = undefined;
/**@type {Point} */
this.p = undefined;
if (rawData instanceof Point) {
this.p = rawData;
}
else if (rawData instanceof Uint8Array) {
this.pb = rawData;
}
else {
throw Error("unexpected type;");
}
}
get public() {
if (!this.p && this.pb)
this.p = Point.fromBytes(this.pb);
else if (!this.p && !this.pb)
throw Error("empty object");
return this.p;
}
get rawBytes() {
if (!this.pb && this.p)
this.pb = this.p.toRawBytes();
else if (!this.pb && !this.p)
throw Error("empty object");
return this.pb;
}
AddComponent(component) {
if (component instanceof Ed25519PublicComponent) {
return new Ed25519PublicComponent(this.public.add(component.public));
}
throw Error("Mismatch with components");
}
MultiplyComponent(component) {
if (component instanceof Ed25519PrivateComponent) {
return new Ed25519PublicComponent(this.public.mul(component.priv));
}
throw Error("Mismatch with components");
}
MinusComponent(component) {
if (component instanceof Ed25519PublicComponent) {
return new Ed25519PublicComponent(this.public.add(component.public.negate()));
}
throw Error("Mismatch with components");
}
EqualsComponent(component) {
if (component instanceof Ed25519PublicComponent) {
return this.public.equals(component.public);
}
throw Error("Mismatch with components");
}
SerializeComponent() {
return this.rawBytes.slice();
}
}
Ed25519PublicComponent.Name = "Ed25519PublicComponent";
Ed25519PublicComponent.Version = "1";
export class Ed25519PrivateComponent extends BasePrivateComponent {
get Scheme() { return Ed25519Scheme; }
get ComponentType() { return Private; }
;
get priv() {
if (!this.p && this.rB)
this.p = BigIntFromByteArray(this.rB);
else if (!this.p && !this.rB)
throw Error("Empty object");
return this.p;
}
get rawBytes() {
if (!this.rB && this.p)
this.rB = BigIntToByteArray(this.p);
else if (!this.rB && !this.p)
throw Error("Empty object");
return this.rB;
}
constructor(rawData) {
super();
/**@type {bigint} */
this.p = undefined;
/**@type {Uint8Array} */
this.rB = undefined;
if (typeof rawData == "bigint") {
this.p = rawData;
}
else if (rawData instanceof Uint8Array) {
this.rB = rawData;
}
else {
throw Error("unexpected type;");
}
}
SerializeComponent() {
return this.rawBytes.slice();
}
GetPublic() {
return new Ed25519PublicComponent(Point.BASE.mul(this.priv));
}
static New() {
return Ed25519SeedComponent.New().GetPrivate();
}
}
Ed25519PrivateComponent.Name = "Ed25519PrivateComponent";
Ed25519PrivateComponent.Version = "1";
export class Ed25519SeedComponent extends BaseSeedComponent {
get Scheme() { return Ed25519Scheme; }
get ComponentType() { return Seed; }
;
get rawBytes() {
return this.rB;
}
constructor(rawData) {
super();
/**@type {Uint8Array} */
this.rB = undefined;
if (rawData instanceof Uint8Array)
this.rB = rawData.slice();
else if (!rawData)
this.rB = Ed25519SeedComponent.GenerateSeed(); // if nothing provided - self instanciate
else
throw Error("Expecting Uint8Array or nothing for constructor");
}
SerializeComponent() {
return this.rB.slice();
}
static GenerateSeed() {
const head = etc.randomBytes(32);
head[0] &= 248; // Clamp bits: 0b1111_1000,
head[31] &= 127; // 0b0111_1111,
head[31] |= 64; // 0b0100_0000
return mod(BigIntFromByteArray(head));
}
GetPrivate() {
return new Ed25519PrivateComponent(this.rawBytes);
}
GetPublic() {
return this.GetPrivate().GetPublic();
}
static async New() {
return new Ed25519SeedComponent(this.GenerateSeed());
}
}
Ed25519SeedComponent.Name = "Ed25519SeedComponent";
Ed25519SeedComponent.Version = "1";
//# sourceMappingURL=Ed25519Components.js.map