Skip to content
This repository has been archived by the owner on Jul 21, 2023. It is now read-only.

Commit

Permalink
fix: add return types to key methods (#252)
Browse files Browse the repository at this point in the history
Adds return types to exported key functions
  • Loading branch information
achingbrain authored Apr 14, 2022
1 parent 54ba3dd commit 8363d28
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@
"generate": "protons ./src/keys/keys.proto"
},
"dependencies": {
"@libp2p/interfaces": "^1.3.20",
"@noble/ed25519": "^1.6.0",
"@noble/secp256k1": "^1.5.4",
"err-code": "^3.0.1",
Expand Down
15 changes: 8 additions & 7 deletions src/keys/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { importer } from './importer.js'
import * as RSA from './rsa-class.js'
import * as Ed25519 from './ed25519-class.js'
import * as Secp256k1 from './secp256k1-class.js'
import type { PrivateKey, PublicKey } from '@libp2p/interfaces/keys'

export { keyStretcher }
export { generateEphemeralKeyPair }
Expand Down Expand Up @@ -38,13 +39,13 @@ function typeToKey (type: string) {
}

// Generates a keypair of the given type and bitsize
export async function generateKeyPair (type: 'RSA' | 'Ed25519' | 'secp256k1', bits?: number) { // eslint-disable-line require-await
export async function generateKeyPair (type: 'RSA' | 'Ed25519' | 'secp256k1', bits?: number): Promise<PrivateKey> { // eslint-disable-line require-await
return await typeToKey(type).generateKeyPair(bits ?? 2048)
}

// Generates a keypair of the given type and bitsize
// seed is a 32 byte uint8array
export async function generateKeyPairFromSeed (type: 'RSA' | 'Ed25519' | 'secp256k1', seed: Uint8Array, bits?: number) { // eslint-disable-line require-await
export async function generateKeyPairFromSeed (type: 'RSA' | 'Ed25519' | 'secp256k1', seed: Uint8Array, bits?: number): Promise<PrivateKey> { // eslint-disable-line require-await
if (type.toLowerCase() !== 'ed25519') {
throw errcode(new Error('Seed key derivation is unimplemented for RSA or secp256k1'), 'ERR_UNSUPPORTED_KEY_DERIVATION_TYPE')
}
Expand All @@ -54,7 +55,7 @@ export async function generateKeyPairFromSeed (type: 'RSA' | 'Ed25519' | 'secp25

// Converts a protobuf serialized public key into its
// representative object
export function unmarshalPublicKey (buf: Uint8Array) {
export function unmarshalPublicKey (buf: Uint8Array): PublicKey {
const decoded = keysPBM.PublicKey.decode(buf)
const data = decoded.Data

Expand All @@ -71,15 +72,15 @@ export function unmarshalPublicKey (buf: Uint8Array) {
}

// Converts a public key object into a protobuf serialized public key
export function marshalPublicKey (key: { bytes: Uint8Array }, type?: string) {
export function marshalPublicKey (key: { bytes: Uint8Array }, type?: string): Uint8Array {
type = (type ?? 'rsa').toLowerCase()
typeToKey(type) // check type
return key.bytes
}

// Converts a protobuf serialized private key into its
// representative object
export async function unmarshalPrivateKey (buf: Uint8Array) { // eslint-disable-line require-await
export async function unmarshalPrivateKey (buf: Uint8Array): Promise<PrivateKey> { // eslint-disable-line require-await
const decoded = keysPBM.PrivateKey.decode(buf)
const data = decoded.Data

Expand All @@ -96,7 +97,7 @@ export async function unmarshalPrivateKey (buf: Uint8Array) { // eslint-disable-
}

// Converts a private key object into a protobuf serialized private key
export function marshalPrivateKey (key: { bytes: Uint8Array }, type?: string) {
export function marshalPrivateKey (key: { bytes: Uint8Array }, type?: string): Uint8Array {
type = (type ?? 'rsa').toLowerCase()
typeToKey(type) // check type
return key.bytes
Expand All @@ -107,7 +108,7 @@ export function marshalPrivateKey (key: { bytes: Uint8Array }, type?: string) {
* @param {string} encryptedKey
* @param {string} password
*/
export async function importKey (encryptedKey: string, password: string) { // eslint-disable-line require-await
export async function importKey (encryptedKey: string, password: string): Promise<PrivateKey> { // eslint-disable-line require-await
try {
const key = await importer(encryptedKey, password)
return await unmarshalPrivateKey(key)
Expand Down

0 comments on commit 8363d28

Please sign in to comment.