Add API to generate keys

This commit is contained in:
Franck Royer 2021-07-09 10:37:47 +10:00
parent f123cd7e62
commit 22c716e0e6
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
3 changed files with 11 additions and 7 deletions

View File

@ -13,7 +13,7 @@ import { delay } from '../delay';
import { hexToBuf } from '../utils';
import { Waku } from '../waku';
import { getPublicKey } from './version_1';
import { generatePrivateKey, getPublicKey } from './version_1';
import { DefaultContentTopic, WakuMessage } from './index';
@ -129,9 +129,7 @@ describe('Interop: Nim', function () {
payload: Buffer.from(messageText, 'utf-8').toString('hex'),
};
const keyPair = await nimWaku.getAsymmetricKeyPair();
const privateKey = hexToBuf(keyPair.privateKey);
const publicKey = hexToBuf(keyPair.publicKey);
const privateKey = generatePrivateKey();
waku.relay.addDecryptionPrivateKey(privateKey);
@ -139,6 +137,7 @@ describe('Interop: Nim', function () {
waku.relay.addObserver(resolve);
});
const publicKey = getPublicKey(privateKey);
dbg('Post message');
await nimWaku.postAsymmetricMessage(message, publicKey);

View File

@ -1,5 +1,6 @@
import { Buffer } from 'buffer';
import { randomBytes } from 'crypto';
import * as crypto from 'crypto';
import ecies from 'ecies-parity';
import { keccak256 } from 'js-sha3';
@ -122,6 +123,13 @@ export async function decryptAsymmetric(
return ecies.decrypt(Buffer.from(privKey), Buffer.from(payload));
}
/**
* Generate a new private key
*/
export function generatePrivateKey(): Uint8Array {
return crypto.randomBytes(32);
}
/**
* Return the public key for the given private key
*/

View File

@ -1,9 +1,6 @@
// TypeScript Version: 2.1
/// <reference types="node" />
declare module 'ecies-parity' {
// Generate a new valid private key. Will use crypto.randomBytes as source.
export function generatePrivate(): Buffer;
// Compute the public key for a given private key.
export function getPublic(privateKey: Buffer): Buffer;