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

View File

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

View File

@ -3,7 +3,7 @@ import '@ethersproject/shims';
import * as EthCrypto from 'eth-crypto'; import * as EthCrypto from 'eth-crypto';
import { ethers } from 'ethers'; import { ethers } from 'ethers';
import { Signer } from '@ethersproject/abstract-signer'; import { Signer } from '@ethersproject/abstract-signer';
import { PublicKeyMessage } from './messages'; import { DirectMessage, PublicKeyMessage } from './messages';
export interface KeyPair { export interface KeyPair {
privateKey: string; privateKey: string;
@ -59,8 +59,37 @@ export function validatePublicKeyMessage(msg: PublicKeyMessage): boolean {
* context. * context.
*/ */
function formatPublicKeyForSignature(ethDmPublicKey: string): string { function formatPublicKeyForSignature(ethDmPublicKey: string): string {
const txt = JSON.stringify({ return JSON.stringify({
ethDmPublicKey, 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);
} }