Move most EthCrypto usage to crypto.ts

This commit is contained in:
Franck Royer 2021-06-24 16:05:22 +10:00
parent 93665feac8
commit 452f4285eb
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
3 changed files with 38 additions and 17 deletions

View File

@ -7,11 +7,12 @@ import { ethers } from 'ethers';
import { Web3Provider } from '@ethersproject/providers';
import {
createPublicKeyMessage,
decryptMessage,
generateEthDmKeyPair,
KeyPair,
recoverKeysFromPrivateKey,
validatePublicKeyMessage,
} from './crypto';
import * as EthCrypto from 'eth-crypto';
import { decode, DirectMessage, encode, PublicKeyMessage } from './messages';
import { Message, Messages } from './Messages';
import 'fontsource-roboto';
@ -225,10 +226,7 @@ async function handleDirectMessage(
console.log('Waku Message received:', wakuMsg);
if (!wakuMsg.payload) return;
const directMessage: DirectMessage = decode(wakuMsg.payload);
const text = await EthCrypto.decryptWithPrivateKey(
privateKey,
directMessage.encMessage
);
const text = await decryptMessage(privateKey, directMessage);
const timestamp = wakuMsg.timestamp ? wakuMsg.timestamp : new Date();
@ -251,13 +249,7 @@ function saveKeysToStorage(ethDmKeyPair: KeyPair) {
function retrieveKeysFromStorage() {
const privateKey = window.localStorage.getItem(EthDmKeyStorageKey);
if (privateKey) {
const publicKey = EthCrypto.publicKeyByPrivateKey(privateKey);
const address = EthCrypto.publicKey.toAddress(publicKey);
return {
privateKey,
publicKey,
address,
};
return recoverKeysFromPrivateKey(privateKey);
}
return;
}

View File

@ -8,9 +8,9 @@ import {
} from '@material-ui/core';
import React, { ChangeEvent, useState, KeyboardEvent } from 'react';
import { Waku, WakuMessage } from 'js-waku';
import * as EthCrypto from 'eth-crypto';
import { DirectMessage, encode } from './messages';
import { DirectMessageContentTopic } from './App';
import { encryptMessage } from './crypto';
const useStyles = makeStyles((theme) => ({
formControl: {
@ -111,7 +111,7 @@ async function encodeEncryptedWakuMessage(
publicKey: string,
address: string
): Promise<WakuMessage> {
const encryptedMsg = await EthCrypto.encryptWithPublicKey(publicKey, message);
const encryptedMsg = await encryptMessage(publicKey, message);
const directMsg: DirectMessage = {
toAddress: address,

View File

@ -3,7 +3,7 @@ import '@ethersproject/shims';
import * as EthCrypto from 'eth-crypto';
import { ethers } from 'ethers';
import { Signer } from '@ethersproject/abstract-signer';
import { PublicKeyMessage } from './messages';
import { DirectMessage, PublicKeyMessage } from './messages';
export interface KeyPair {
privateKey: string;
@ -59,8 +59,37 @@ export function validatePublicKeyMessage(msg: PublicKeyMessage): boolean {
* context.
*/
function formatPublicKeyForSignature(ethDmPublicKey: string): string {
const txt = JSON.stringify({
return JSON.stringify({
ethDmPublicKey,
});
return txt;
}
/**
* Decrypt a Direct Message using the private key.
*/
export function decryptMessage(
privateKey: string,
directMessage: DirectMessage
) {
return EthCrypto.decryptWithPrivateKey(privateKey, directMessage.encMessage);
}
/**
* Recover Public Key and address from Private Key
*/
export function recoverKeysFromPrivateKey(privateKey: string) {
const publicKey = EthCrypto.publicKeyByPrivateKey(privateKey);
const address = EthCrypto.publicKey.toAddress(publicKey);
return {
privateKey,
publicKey,
address,
};
}
/**
* Encrypt message with given Public Key
*/
export async function encryptMessage(publicKey: string, message: string) {
return await EthCrypto.encryptWithPublicKey(publicKey, message);
}