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

fix: encode protobuf enums correctly #254

Merged
merged 1 commit into from
May 10, 2022
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
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -176,14 +176,14 @@
"iso-random-stream": "^2.0.0",
"multiformats": "^9.4.5",
"node-forge": "^1.1.0",
"protons-runtime": "^1.0.2",
"protons-runtime": "^1.0.4",
"uint8arrays": "^3.0.0"
},
"devDependencies": {
"@types/mocha": "^9.0.0",
"aegir": "^37.0.12",
"benchmark": "^2.1.4",
"protons": "^3.0.2",
"protons": "^3.0.4",
"sinon": "^13.0.1",
"util": "^0.12.3",
"wherearewe": "^1.0.0"
Expand Down
6 changes: 4 additions & 2 deletions src/keys/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ export { keyStretcher }
export { generateEphemeralKeyPair }
export { keysPBM }

export type KeyTypes = 'RSA' | 'Ed25519' | 'secp256k1'

export const supportedKeys = {
rsa: RSA,
ed25519: Ed25519,
Expand All @@ -39,13 +41,13 @@ function typeToKey (type: string) {
}

// Generates a keypair of the given type and bitsize
export async function generateKeyPair (type: 'RSA' | 'Ed25519' | 'secp256k1', bits?: number): Promise<PrivateKey> { // eslint-disable-line require-await
export async function generateKeyPair (type: KeyTypes, 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): Promise<PrivateKey> { // eslint-disable-line require-await
export async function generateKeyPairFromSeed (type: KeyTypes, 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 Down
14 changes: 10 additions & 4 deletions src/keys/keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,32 @@
/* eslint-disable @typescript-eslint/no-namespace */

import { enumeration, encodeMessage, decodeMessage, message, bytes } from 'protons-runtime'
import type { Codec } from 'protons-runtime'

export enum KeyType {
RSA = 'RSA',
Ed25519 = 'Ed25519',
Secp256k1 = 'Secp256k1'
}

enum __KeyTypeValues {
RSA = 0,
Ed25519 = 1,
Secp256k1 = 2
}

export namespace KeyType {
export const codec = () => {
return enumeration<typeof KeyType>(KeyType)
return enumeration<typeof KeyType>(__KeyTypeValues)
}
}

export interface PublicKey {
Type: KeyType
Data: Uint8Array
}

export namespace PublicKey {
export const codec = () => {
export const codec = (): Codec<PublicKey> => {
return message<PublicKey>({
1: { name: 'Type', codec: KeyType.codec() },
2: { name: 'Data', codec: bytes }
Expand All @@ -43,7 +49,7 @@ export interface PrivateKey {
}

export namespace PrivateKey {
export const codec = () => {
export const codec = (): Codec<PrivateKey> => {
return message<PrivateKey>({
1: { name: 'Type', codec: KeyType.codec() },
2: { name: 'Data', codec: bytes }
Expand Down