Skip to content
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

feat(ExportableKey): adding ExportableKey protocol and implementation #131

Merged
merged 1 commit into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 17 additions & 20 deletions src/apollo/utils/Ed25519PrivateKey.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
import ApolloBaseAsymmetricEncryption from "@atala/apollo";
import { Curve, KeyTypes, PrivateKey } from "../../domain";
import { KeyProperties } from "../../domain/models/KeyProperties";
import { SignableKey } from "../../domain/models/keyManagement/SignableKey";
import ApolloPkg from "@atala/apollo";
import { Ed25519PublicKey } from "./Ed25519PublicKey";
import {
Curve,
ExportableKey,
ImportableKey,
KeyProperties,
KeyTypes,
PrivateKey,
SignableKey
} from "../../domain";


/**
* @ignore
*/
export class Ed25519PrivateKey extends PrivateKey implements SignableKey {
export class Ed25519PrivateKey extends PrivateKey implements ExportableKey, SignableKey {
public keySpecification: Map<string, string> = new Map();
public raw: Buffer;
public size: number;
public type: KeyTypes = KeyTypes.EC;

public readonly to = ExportableKey.factory(this, { pemLabel: "PRIVATE KEY" });
static from = ImportableKey.factory(Ed25519PrivateKey, { pemLabel: "PRIVATE KEY" });

constructor(bytes: Int8Array | Uint8Array) {
super();

Expand All @@ -36,27 +46,14 @@ export class Ed25519PrivateKey extends PrivateKey implements SignableKey {

private getInstance(
value?: Int8Array | Uint8Array
): ApolloBaseAsymmetricEncryption.io.iohk.atala.prism.apollo.utils.KMMEdPrivateKey {
): ApolloPkg.io.iohk.atala.prism.apollo.utils.KMMEdPrivateKey {
// eslint-disable-next-line no-extra-boolean-cast
const bytes = !!value ? Buffer.from(value) : this.raw;
const instance =
new ApolloBaseAsymmetricEncryption.io.iohk.atala.prism.apollo.utils.KMMEdPrivateKey(
new ApolloPkg.io.iohk.atala.prism.apollo.utils.KMMEdPrivateKey(
Int8Array.from(bytes)
);

return instance;
}

public readonly to = {
Buffer: () => Buffer.from(this.raw),
Hex: () => this.to.Buffer().toString("hex"),
};

static from = {
Buffer: (value: Buffer) => new Ed25519PrivateKey(Int8Array.from(value)),
Hex: (value: string) =>
Ed25519PrivateKey.from.Buffer(Buffer.from(value, "hex")),
String: (value: string) =>
Ed25519PrivateKey.from.Buffer(Buffer.from(value)),
};
}
23 changes: 16 additions & 7 deletions src/apollo/utils/Ed25519PublicKey.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
import ApolloBaseAsymmetricEncryption from "@atala/apollo";
import { Curve, KeyTypes, PublicKey } from "../../domain";
import { KeyProperties } from "../../domain/models/KeyProperties";
import { VerifiableKey } from "../../domain/models/keyManagement/VerifiableKey";
import ApolloPkg from "@atala/apollo";
import {
Curve,
ExportableKey,
ImportableKey,
KeyProperties,
KeyTypes,
PublicKey,
VerifiableKey
} from "../../domain";

/**
* @ignore
*/
export class Ed25519PublicKey extends PublicKey implements VerifiableKey {
export class Ed25519PublicKey extends PublicKey implements ExportableKey, VerifiableKey {
public keySpecification: Map<string, string> = new Map();
public raw: Buffer;
public size: number;
public type: KeyTypes = KeyTypes.EC;

public readonly to = ExportableKey.factory(this, { pemLabel: "PUBLIC KEY" });
static from = ImportableKey.factory(Ed25519PublicKey, { pemLabel: "PUBLIC KEY" });

constructor(bytes: Int8Array | Uint8Array) {
super();

Expand All @@ -33,11 +42,11 @@ export class Ed25519PublicKey extends PublicKey implements VerifiableKey {

private getInstance(
value?: Int8Array | Uint8Array
): ApolloBaseAsymmetricEncryption.io.iohk.atala.prism.apollo.utils.KMMEdPublicKey {
): ApolloPkg.io.iohk.atala.prism.apollo.utils.KMMEdPublicKey {
// eslint-disable-next-line no-extra-boolean-cast
const bytes = !!value ? Buffer.from(value) : this.raw;
const instance =
new ApolloBaseAsymmetricEncryption.io.iohk.atala.prism.apollo.utils.KMMEdPublicKey(
new ApolloPkg.io.iohk.atala.prism.apollo.utils.KMMEdPublicKey(
Int8Array.from(bytes)
);

Expand Down
46 changes: 13 additions & 33 deletions src/apollo/utils/Secp256k1PrivateKey.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,35 @@
import ApolloPkg from "@atala/apollo";
import BN from "bn.js";

import * as ECConfig from "../../config/ECConfig";
import { Secp256k1PublicKey } from "./Secp256k1PublicKey";
import { ApolloError } from "../../domain/models/Errors";
import { SignableKey } from "../../domain/models/keyManagement/SignableKey";
import { KeyProperties } from "../../domain/models/KeyProperties";
import { Curve, DerivableKey, KeyTypes, PrivateKey } from "../../domain";

import * as ApolloPKG from "@atala/apollo";
import { Curve, DerivableKey, ExportableKey, ImportableKey, KeyTypes, PrivateKey } from "../../domain";
import { DerivationPath } from "./derivation/DerivationPath";

const ApolloSDK = ApolloPKG.io.iohk.atala.prism.apollo;

const HDKey = ApolloSDK.derivation.HDKey;
const BigIntegerWrapper = ApolloSDK.derivation.BigIntegerWrapper;
const {
io: {
iohk: {
atala: {
prism: { apollo },
},
},
},
} = ApolloPKG;
const Apollo = ApolloPkg.io.iohk.atala.prism.apollo;
const HDKey = Apollo.derivation.HDKey;
const BigIntegerWrapper = Apollo.derivation.BigIntegerWrapper;

/**
* @ignore
*/
export class Secp256k1PrivateKey
extends PrivateKey
implements SignableKey, DerivableKey {
public type: KeyTypes = KeyTypes.EC;
implements DerivableKey, ExportableKey, SignableKey
{
public keySpecification: Map<string, string> = new Map();
public raw: Uint8Array;
public size: number;
public type: KeyTypes = KeyTypes.EC;

public readonly to = ExportableKey.factory(this, { pemLabel: "EC PRIVATE KEY" });
static from = ImportableKey.factory(Secp256k1PrivateKey, { pemLabel: "EC PRIVATE KEY" });

private get native() {
return apollo.utils.KMMECSecp256k1PrivateKey.Companion.secp256k1FromByteArray(
return Apollo.utils.KMMECSecp256k1PrivateKey.Companion.secp256k1FromByteArray(
Int8Array.from(this.raw)
);
}
Expand Down Expand Up @@ -112,18 +106,4 @@ export class Secp256k1PrivateKey
const bnprv = new BN(encoded);
return new Secp256k1PrivateKey(Uint8Array.from(bnprv.toArray()));
}

public readonly to = {
Buffer: () => Buffer.from(this.getEncoded()),
Hex: () => this.to.Buffer().toString("hex"),
};

static from = {
Buffer: (value: Buffer) =>
Secp256k1PrivateKey.secp256k1FromBytes(new Uint8Array(value)),
Hex: (value: string) =>
Secp256k1PrivateKey.from.Buffer(Buffer.from(value, "hex")),
String: (value: string) =>
Secp256k1PrivateKey.from.Buffer(Buffer.from(value)),
};
}
41 changes: 21 additions & 20 deletions src/apollo/utils/Secp256k1PublicKey.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
import ApolloPkg from "@atala/apollo";
import BN from "bn.js";
import BigInteger from "bn.js";
import * as ApolloPKG from "@atala/apollo";

import * as ECConfig from "../../config/ECConfig";
import { ECPoint } from "./ec/ECPoint";
import { VerifiableKey } from "../../domain/models/keyManagement/VerifiableKey";
import { KeyProperties } from "../../domain/models/KeyProperties";
import { ApolloError } from "../../domain/models/Errors";
import { Curve, KeyTypes, PublicKey } from "../../domain";

const {
io: {
iohk: {
atala: {
prism: { apollo },
},
},
},
} = ApolloPKG;
import {
Curve,
ExportableKey,
ImportableKey,
KeyProperties,
KeyTypes,
PublicKey,
VerifiableKey
} from "../../domain";

/**
* @ignore
*/
export class Secp256k1PublicKey extends PublicKey implements VerifiableKey {
public type: KeyTypes = KeyTypes.EC;
export class Secp256k1PublicKey extends PublicKey implements ExportableKey, VerifiableKey {
public keySpecification: Map<KeyProperties | string, string> = new Map();
public size;
public size: number;
public raw: Uint8Array;
public type: KeyTypes = KeyTypes.EC;

public readonly to = ExportableKey.factory(this, { pemLabel: "EC PUBLIC KEY" });
static from = ImportableKey.factory(Secp256k1PublicKey, { pemLabel: "EC PUBLIC KEY" });

public get isCompressed() {
return (
this.keySpecification.has("compressed") &&
Expand All @@ -35,10 +35,11 @@ export class Secp256k1PublicKey extends PublicKey implements VerifiableKey {
}

private get native() {
return apollo.utils.KMMECSecp256k1PublicKey.Companion.secp256k1FromBytes(
return ApolloPkg.io.iohk.atala.prism.apollo.utils.KMMECSecp256k1PublicKey.Companion.secp256k1FromBytes(
Int8Array.from(this.raw)
);
}

constructor(nativeValue: Uint8Array) {
super();

Expand Down Expand Up @@ -142,7 +143,7 @@ export class Secp256k1PublicKey extends PublicKey implements VerifiableKey {
static secp256k1FromBytes(encoded: Uint8Array): Secp256k1PublicKey {
return new Secp256k1PublicKey(
Uint8Array.from(
ApolloPKG.io.iohk.atala.prism.apollo.utils.KMMECSecp256k1PublicKey.Companion.secp256k1FromBytes(
ApolloPkg.io.iohk.atala.prism.apollo.utils.KMMECSecp256k1PublicKey.Companion.secp256k1FromBytes(
Int8Array.from(encoded)
).raw
)
Expand Down Expand Up @@ -177,7 +178,7 @@ export class Secp256k1PublicKey extends PublicKey implements VerifiableKey {
const xCoord = Buffer.from(x.toArray());
const yCoord = Buffer.from(y.toArray());
const publicKey =
apollo.utils.KMMECSecp256k1PublicKey.Companion.secp256k1FromByteCoordinates(
ApolloPkg.io.iohk.atala.prism.apollo.utils.KMMECSecp256k1PublicKey.Companion.secp256k1FromByteCoordinates(
Int8Array.from(xCoord),
Int8Array.from(yCoord)
);
Expand Down
31 changes: 14 additions & 17 deletions src/apollo/utils/X25519PrivateKey.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
import ApolloBaseAsymmetricEncryption from "@atala/apollo";
import { Curve, KeyTypes, PrivateKey } from "../../domain";
import { KeyProperties } from "../../domain/models/KeyProperties";
import ApolloPkg from "@atala/apollo";
import { X25519PublicKey } from "./X25519PublicKey";
import {
Curve,
ExportableKey,
ImportableKey,
KeyProperties,
KeyTypes,
PrivateKey
} from "../../domain";

/**
* @ignore
*/
export class X25519PrivateKey extends PrivateKey {
export class X25519PrivateKey extends PrivateKey implements ExportableKey {
public keySpecification: Map<string, string> = new Map();
public raw: Buffer;
public size: number;
public type: KeyTypes = KeyTypes.EC;

public readonly to = ExportableKey.factory(this, { pemLabel: "PRIVATE KEY" });
static from = ImportableKey.factory(X25519PrivateKey, { pemLabel: "PRIVATE KEY" });

constructor(bytes: Int8Array | Uint8Array) {
super();

Expand All @@ -32,22 +41,10 @@ export class X25519PrivateKey extends PrivateKey {
// eslint-disable-next-line no-extra-boolean-cast
const bytes = !!value ? Buffer.from(value) : this.raw;
const instance =
new ApolloBaseAsymmetricEncryption.io.iohk.atala.prism.apollo.utils.KMMX25519PrivateKey(
new ApolloPkg.io.iohk.atala.prism.apollo.utils.KMMX25519PrivateKey(
Int8Array.from(bytes)
);

return instance;
}

public readonly to = {
Buffer: () => Buffer.from(this.raw),
Hex: () => this.to.Buffer().toString("hex"),
};

static from = {
Buffer: (value: Buffer) => new X25519PrivateKey(new Uint8Array(value)),
Hex: (value: string) =>
X25519PrivateKey.from.Buffer(Buffer.from(value, "hex")),
String: (value: string) => X25519PrivateKey.from.Buffer(Buffer.from(value)),
};
}
19 changes: 14 additions & 5 deletions src/apollo/utils/X25519PublicKey.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
import ApolloBaseAsymmetricEncryption from "@atala/apollo";
import { Curve, KeyTypes, PublicKey } from "../../domain";
import { KeyProperties } from "../../domain/models/KeyProperties";
import ApolloPkg from "@atala/apollo";
import {
Curve,
ExportableKey,
ImportableKey,
KeyProperties,
KeyTypes,
PublicKey
} from "../../domain";

/**
* @ignore
*/
export class X25519PublicKey extends PublicKey {
export class X25519PublicKey extends PublicKey implements ExportableKey {
public keySpecification: Map<string, string> = new Map();
public raw: Buffer;
public size: number;
public type: KeyTypes = KeyTypes.EC;

public readonly to = ExportableKey.factory(this, { pemLabel: "PUBLIC KEY" });
static from = ImportableKey.factory(X25519PublicKey, { pemLabel: "PUBLIC KEY" });

constructor(bytes: Int8Array | Uint8Array) {
super();

Expand All @@ -27,7 +36,7 @@ export class X25519PublicKey extends PublicKey {
// eslint-disable-next-line no-extra-boolean-cast
const bytes = !!value ? Buffer.from(value) : this.raw;
const instance =
new ApolloBaseAsymmetricEncryption.io.iohk.atala.prism.apollo.utils.KMMX25519PublicKey(
new ApolloPkg.io.iohk.atala.prism.apollo.utils.KMMX25519PublicKey(
Int8Array.from(bytes)
);

Expand Down
7 changes: 7 additions & 0 deletions src/domain/models/keyManagement/Key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { VerifiableKey } from "./VerifiableKey";
import { KeyCurve } from "../KeyCurve";
import { Curve } from "./Curve";
import { KeyTypes } from "./KeyTypes";
import { ExportableKey } from "./exportable";

export function getKeyCurveByNameAndIndex(
name: string,
Expand All @@ -29,9 +30,15 @@ export abstract class Key {
abstract keySpecification: Map<KeyProperties | string, string>;
abstract size: number;
abstract raw: Uint8Array;
abstract to: ExportableKey.Common["to"];

abstract getEncoded(): Uint8Array;

get curve() {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
return this.getProperty(KeyProperties.curve)!;
}

isExportable(): this is StorableKey {
return "export" in this;
}
Expand Down
Loading
Loading