*/
import { exec, ExecOptions } from 'child_process'
-import { randomBytes } from 'crypto'
+import { ED25519KeyPairOptions, generateKeyPair, randomBytes, RSAKeyPairOptions } from 'crypto'
import { truncate } from 'lodash'
-import { createPrivateKey as createPrivateKey_1, getPublicKey as getPublicKey_1 } from 'pem'
import { pipeline } from 'stream'
import { URL } from 'url'
import { promisify } from 'util'
return process.env.NODE_ENV === 'test'
}
+function isDevInstance () {
+ return process.env.NODE_ENV === 'dev'
+}
+
+function isTestOrDevInstance () {
+ return isTestInstance() || isDevInstance()
+}
+
function isProdInstance () {
return process.env.NODE_ENV === 'production'
}
// ---------------------------------------------------------------------------
+function generateRSAKeyPairPromise (size: number) {
+ return new Promise<{ publicKey: string, privateKey: string }>((res, rej) => {
+ const options: RSAKeyPairOptions<'pem', 'pem'> = {
+ modulusLength: size,
+ publicKeyEncoding: {
+ type: 'spki',
+ format: 'pem'
+ },
+ privateKeyEncoding: {
+ type: 'pkcs1',
+ format: 'pem'
+ }
+ }
+
+ generateKeyPair('rsa', options, (err, publicKey, privateKey) => {
+ if (err) return rej(err)
+
+ return res({ publicKey, privateKey })
+ })
+ })
+}
+
+function generateED25519KeyPairPromise () {
+ return new Promise<{ publicKey: string, privateKey: string }>((res, rej) => {
+ const options: ED25519KeyPairOptions<'pem', 'pem'> = {
+ publicKeyEncoding: {
+ type: 'spki',
+ format: 'pem'
+ },
+ privateKeyEncoding: {
+ type: 'pkcs8',
+ format: 'pem'
+ }
+ }
+
+ generateKeyPair('ed25519', options, (err, publicKey, privateKey) => {
+ if (err) return rej(err)
+
+ return res({ publicKey, privateKey })
+ })
+ })
+}
+
+// ---------------------------------------------------------------------------
+
function promisify0<A> (func: (cb: (err: any, result: A) => void) => void): () => Promise<A> {
return function promisified (): Promise<A> {
return new Promise<A>((resolve: (arg: A) => void, reject: (err: any) => void) => {
}
const randomBytesPromise = promisify1<number, Buffer>(randomBytes)
-const createPrivateKey = promisify1<number, { key: string }>(createPrivateKey_1)
-const getPublicKey = promisify1<string, { publicKey: string }>(getPublicKey_1)
const execPromise2 = promisify2<string, any, string>(exec)
const execPromise = promisify1<string, string>(exec)
const pipelinePromise = promisify(pipeline)
export {
isTestInstance,
+ isTestOrDevInstance,
isProdInstance,
getAppNumber,
promisify2,
randomBytesPromise,
- createPrivateKey,
- getPublicKey,
+
+ generateRSAKeyPairPromise,
+ generateED25519KeyPairPromise,
+
execPromise2,
execPromise,
pipelinePromise,