Skip to content

Commit

Permalink
feat: add asn schemas for ED
Browse files Browse the repository at this point in the history
  • Loading branch information
microshine committed Mar 4, 2022
1 parent 7272d97 commit 45a9045
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/asn1/ed_private_key.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { AsnIntegerConverter, AsnProp, AsnPropTypes, AsnSerializer, AsnType, AsnTypeTypes } from "@peculiar/asn1-schema";
import { IJsonConvertible } from "@peculiar/json-schema";
import { Convert } from "pvtsutils";

@AsnType({ type: AsnTypeTypes.Choice })
export class EdPrivateKey implements IJsonConvertible {

@AsnProp({ type: AsnPropTypes.OctetString })
public value = new ArrayBuffer(0);

public fromJSON(json: any): this {
if (!json.d) {
throw new Error("d: Missing required property");
}
this.value = Convert.FromBase64Url(json.d);

return this;
}
public toJSON() {
const jwk: JsonWebKey = {
d: Convert.ToBase64Url(this.value),
};

return jwk;
}
}
41 changes: 41 additions & 0 deletions src/asn1/ed_public_key.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { AsnProp, AsnPropTypes, AsnType, AsnTypeTypes } from "@peculiar/asn1-schema";
import { IJsonConvertible } from "@peculiar/json-schema";
import { Convert } from "pvtsutils";
import { CryptoError } from "../errors";

// RFC 8410
// https://datatracker.ietf.org/doc/html/rfc8410
//
// PublicKey ::= BIT STRING

@AsnType({ type: AsnTypeTypes.Choice })
export class EdPublicKey implements IJsonConvertible {

@AsnProp({ type: AsnPropTypes.BitString })
public value = new ArrayBuffer(0);

constructor(value?: ArrayBuffer) {
if (value) {
this.value = value;
}
}

public toJSON() {
const json = {
x: Convert.ToBase64Url(this.value),
};

return json;
}

public fromJSON(json: any): this {
if (!("x" in json)) {
throw new Error("x: Missing required property");
}

this.value = Convert.FromBase64Url(json.x);

return this;
}

}
3 changes: 3 additions & 0 deletions src/asn1/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,8 @@ export * from "./rsa_public_key";
export * from "./ec_private_key";
export * from "./ec_public_key";
export * from "./ec_signature";
export * from "./one_asymmetric_key";
export * from "./ed_private_key";
export * from "./ed_public_key";
export * from "./rfc8410";
export * as converters from "./converters";
26 changes: 26 additions & 0 deletions src/asn1/one_asymmetric_key.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { AsnProp, AsnPropTypes } from "@peculiar/asn1-schema";
import { PrivateKeyInfo } from "./private_key_info";

/**
* ```asn
* OneAsymmetricKey ::= SEQUENCE {
* version Version,
* privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
* privateKey PrivateKey,
* attributes [0] IMPLICIT Attributes OPTIONAL,
* ...,
* [[2: publicKey [1] IMPLICIT PublicKey OPTIONAL ]],
* ...
* }
*
* PrivateKey ::= OCTET STRING
*
* PublicKey ::= BIT STRING
* ```
*/
export class OneAsymmetricKey extends PrivateKeyInfo {

@AsnProp({ context: 1, implicit: true, type: AsnPropTypes.BitString, optional: true })
public publicKey?: ArrayBuffer;

}

0 comments on commit 45a9045

Please sign in to comment.