Skip to content

Commit

Permalink
Add PrivateKey.toBytes method
Browse files Browse the repository at this point in the history
  • Loading branch information
Comp0te committed Dec 16, 2024
1 parent 2591822 commit 91cfe66
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 41 deletions.
11 changes: 8 additions & 3 deletions src/types/keypair/PrivateKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import { PrivateKey as Ed25519PrivateKey } from './ed25519/PrivateKey';
import { PrivateKey as Secp256k1PrivateKey } from './secp256k1/PrivateKey';

/**
* Interface representing the structure of a private key, with methods for
* obtaining the public key, signing messages, and exporting to PEM format.
* Interface representing the structure and methods of a private key, including
* functions to retrieve public key bytes, sign messages, and export to PEM format.
*/
interface PrivateKeyInternal {
export interface PrivateKeyInternal {
/** Retrieves the public key bytes. */
publicKeyBytes(): Promise<Uint8Array>;
toBytes(): Uint8Array;

/**
* Signs a message using the private key.
Expand Down Expand Up @@ -57,6 +58,10 @@ export class PrivateKey {
this.priv = priv;
}

toBytes(): Uint8Array {
return this.priv.toBytes();
}

/**
* Gets the public key associated with this private key.
* @returns The associated PublicKey instance.
Expand Down
24 changes: 5 additions & 19 deletions src/types/keypair/ed25519/PrivateKey.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,5 @@
import * as ed25519 from '@noble/ed25519';

/**
* Interface representing the structure and methods of a private key, including
* functions to retrieve public key bytes, sign messages, and export to PEM format.
*/
interface PrivateKeyInternal {
/** Retrieves the public key bytes. */
publicKeyBytes(): Promise<Uint8Array>;

/**
* Signs a message using the private key.
* @param message - The message to sign.
* @returns A promise that resolves to the signature bytes.
*/
sign(message: Uint8Array): Promise<Uint8Array>;

/** Converts the private key to PEM format. */
toPem(): string;
}
import { PrivateKeyInternal } from "../PrivateKey";

/**
* Represents an Ed25519 private key, supporting key generation, signing, and PEM encoding.
Expand Down Expand Up @@ -55,6 +37,10 @@ export class PrivateKey implements PrivateKeyInternal {
return ed25519.getPublicKey(this.key);
}

toBytes(): Uint8Array {
return this.key;
}

/**
* Signs a message using the private key.
* @param message - The message to sign.
Expand Down
24 changes: 5 additions & 19 deletions src/types/keypair/secp256k1/PrivateKey.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,13 @@
import * as secp256k1 from '@noble/secp256k1';
import { sha256 } from '@noble/hashes/sha256';
import { PrivateKeyInternal } from "../PrivateKey";

/** PEM prefix for a private key. */
const PemPrivateKeyPrefix = '-----BEGIN PRIVATE KEY-----';

/** PEM suffix for a private key. */
const PemPrivateKeySuffix = '-----END PRIVATE KEY-----';

/**
* Interface representing the structure and methods of a private key, including
* functions to retrieve public key bytes, sign messages, and export to PEM format.
*/
interface PrivateKeyInternal {
/** Retrieves the public key bytes. */
publicKeyBytes(): Promise<Uint8Array>;

/**
* Signs a message using the private key.
* @param message - The message to sign.
* @returns A promise resolving to the signature bytes.
*/
sign(message: Uint8Array): Promise<Uint8Array>;

/** Converts the private key to PEM format. */
toPem(): string;
}

/**
* Represents a secp256k1 private key, supporting key generation, signing, and PEM encoding.
* The class offers static methods to create instances from bytes, hex, and PEM formats.
Expand Down Expand Up @@ -67,6 +49,10 @@ export class PrivateKey implements PrivateKeyInternal {
return secp256k1.getPublicKey(this.key, true);
}

toBytes(): Uint8Array {
return this.key;
}

/**
* Signs a message using the private key.
* The message is first hashed with SHA-256 before signing.
Expand Down

0 comments on commit 91cfe66

Please sign in to comment.