122 lines
3.4 KiB
TypeScript
Raw Normal View History

2022-02-04 14:12:00 +11:00
import "@ethersproject/shims";
2021-06-11 15:33:29 +10:00
2022-02-04 14:12:00 +11:00
import { PublicKeyMessage } from "./messaging/wire";
2022-02-14 10:50:02 +11:00
import { hexToBytes, equalByteArrays, bytesToHex } from "js-waku/lib/utils";
2022-02-04 14:12:00 +11:00
import { generatePrivateKey, getPublicKey } from "js-waku";
import * as sigUtil from "eth-sig-util";
import { PublicKeyContentTopic } from "./waku";
import { keccak256 } from "ethers/lib/utils";
2022-02-14 10:50:02 +11:00
export const PublicKeyMessageEncryptionKey = hexToBytes(
2022-02-04 14:12:00 +11:00
keccak256(Buffer.from(PublicKeyContentTopic, "utf-8"))
);
2021-05-28 15:35:50 +10:00
2021-06-11 15:33:29 +10:00
export interface KeyPair {
privateKey: Uint8Array;
publicKey: Uint8Array;
2021-06-11 15:33:29 +10:00
}
2021-05-28 15:35:50 +10:00
/**
* Generate new encryption keypair.
2021-05-28 15:35:50 +10:00
*/
export async function generateEncryptionKeyPair(): Promise<KeyPair> {
const privateKey = generatePrivateKey();
const publicKey = getPublicKey(privateKey);
return { privateKey, publicKey };
2021-05-28 15:35:50 +10:00
}
/**
* Sign the encryption public key with Web3. This can then be published to let other
* users know to use this encryption public key to encrypt messages for the
2021-06-22 10:48:54 +10:00
* Ethereum Address holder.
2021-05-28 15:35:50 +10:00
*/
2021-06-11 15:33:29 +10:00
export async function createPublicKeyMessage(
address: string,
encryptionPublicKey: Uint8Array,
providerRequest: (request: {
method: string;
params?: Array<any>;
}) => Promise<any>
2021-06-11 15:33:29 +10:00
): Promise<PublicKeyMessage> {
const signature = await signEncryptionKey(
encryptionPublicKey,
address,
providerRequest
2021-06-11 15:33:29 +10:00
);
2022-02-04 14:12:00 +11:00
console.log("Asking wallet to sign Public Key Message");
console.log("Public Key Message signed");
return new PublicKeyMessage({
encryptionPublicKey: encryptionPublicKey,
2022-02-14 10:50:02 +11:00
ethAddress: hexToBytes(address),
signature: hexToBytes(signature),
});
2021-05-28 15:35:50 +10:00
}
function buildMsgParams(encryptionPublicKey: Uint8Array, fromAddress: string) {
return JSON.stringify({
domain: {
chainId: 1,
2022-02-04 14:12:00 +11:00
name: "Ethereum Private Message over Waku",
version: "1",
},
message: {
2022-02-14 10:50:02 +11:00
encryptionPublicKey: bytesToHex(encryptionPublicKey),
ownerAddress: fromAddress,
},
// Refers to the keys of the *types* object below.
2022-02-04 14:12:00 +11:00
primaryType: "PublishEncryptionPublicKey",
types: {
EIP712Domain: [
2022-02-04 14:12:00 +11:00
{ name: "name", type: "string" },
{ name: "version", type: "string" },
{ name: "chainId", type: "uint256" },
],
PublishEncryptionPublicKey: [
2022-02-04 14:12:00 +11:00
{ name: "encryptionPublicKey", type: "string" },
{ name: "ownerAddress", type: "string" },
],
},
});
}
export async function signEncryptionKey(
encryptionPublicKey: Uint8Array,
fromAddress: string,
providerRequest: (request: {
method: string;
params?: Array<any>;
from?: string;
}) => Promise<any>
): Promise<Uint8Array> {
const msgParams = buildMsgParams(encryptionPublicKey, fromAddress);
const result = await providerRequest({
2022-02-04 14:12:00 +11:00
method: "eth_signTypedData_v3",
params: [fromAddress, msgParams],
from: fromAddress,
});
2022-02-04 14:12:00 +11:00
console.log("TYPED SIGNED:" + JSON.stringify(result));
2022-02-14 10:50:02 +11:00
return hexToBytes(result);
2021-05-28 15:35:50 +10:00
}
/**
* Validate that the Encryption Public Key was signed by the holder of the given Ethereum address.
2021-05-28 15:35:50 +10:00
*/
export function validatePublicKeyMessage(msg: PublicKeyMessage): boolean {
const recovered = sigUtil.recoverTypedSignature_v4({
data: JSON.parse(
2022-02-14 10:50:02 +11:00
buildMsgParams(msg.encryptionPublicKey, "0x" + bytesToHex(msg.ethAddress))
),
2022-02-14 10:50:02 +11:00
sig: "0x" + bytesToHex(msg.signature),
2021-06-11 15:33:29 +10:00
});
2022-02-04 14:12:00 +11:00
console.log("Recovered", recovered);
2022-02-14 10:50:02 +11:00
console.log("ethAddress", "0x" + bytesToHex(msg.ethAddress));
return equalByteArrays(recovered, msg.ethAddress);
2021-06-24 16:05:22 +10:00
}