101 lines
2.8 KiB
TypeScript
Raw Normal View History

2022-02-04 14:12:00 +11:00
import "@ethersproject/shims";
2021-08-12 15:26:06 +10:00
2022-02-04 14:12:00 +11:00
import { PublicKeyMessage } from "./messaging/wire";
import { hexToBuf, equalByteArrays, bufToHex } from "js-waku/lib/utils";
import * as sigUtil from "eth-sig-util";
2021-08-12 15:26:06 +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-08-12 15:26:06 +10:00
* Ethereum Address holder.
*/
export async function createPublicKeyMessage(
address: string,
2021-08-12 16:36:28 +10:00
encryptionPublicKey: Uint8Array,
providerRequest: (request: {
method: string;
params?: Array<any>;
}) => Promise<any>
2021-08-12 15:26:06 +10:00
): Promise<PublicKeyMessage> {
2021-08-12 16:36:28 +10:00
const signature = await signEncryptionKey(
encryptionPublicKey,
address,
providerRequest
2021-08-12 15:26:06 +10:00
);
2021-08-12 16:36:28 +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");
2021-08-12 15:26:06 +10:00
return new PublicKeyMessage({
encryptionPublicKey: encryptionPublicKey,
ethAddress: hexToBuf(address),
2021-08-12 15:26:06 +10:00
signature: hexToBuf(signature),
});
}
2021-08-12 16:36:28 +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",
2021-08-12 16:36:28 +10:00
},
message: {
encryptionPublicKey: bufToHex(encryptionPublicKey),
ownerAddress: fromAddress,
},
// Refers to the keys of the *types* object below.
2022-02-04 14:12:00 +11:00
primaryType: "PublishEncryptionPublicKey",
2021-08-12 16:36:28 +10:00
types: {
EIP712Domain: [
2022-02-04 14:12:00 +11:00
{ name: "name", type: "string" },
{ name: "version", type: "string" },
{ name: "chainId", type: "uint256" },
2021-08-12 16:36:28 +10:00
],
PublishEncryptionPublicKey: [
2022-02-04 14:12:00 +11:00
{ name: "encryptionPublicKey", type: "string" },
{ name: "ownerAddress", type: "string" },
2021-08-12 16:36:28 +10:00
],
},
});
}
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",
2021-08-12 16:36:28 +10:00
params: [fromAddress, msgParams],
from: fromAddress,
});
2022-02-04 14:12:00 +11:00
console.log("TYPED SIGNED:" + JSON.stringify(result));
2021-08-12 16:36:28 +10:00
return hexToBuf(result);
2021-08-12 15:26:06 +10:00
}
/**
2021-08-12 16:36:28 +10:00
* Validate that the Encryption Public Key was signed by the holder of the given Ethereum address.
2021-08-12 15:26:06 +10:00
*/
2021-08-12 16:36:28 +10:00
export function validatePublicKeyMessage(msg: PublicKeyMessage): boolean {
const recovered = sigUtil.recoverTypedSignature_v4({
data: JSON.parse(
2022-02-04 14:12:00 +11:00
buildMsgParams(msg.encryptionPublicKey, "0x" + bufToHex(msg.ethAddress))
2021-08-12 16:36:28 +10:00
),
2022-02-04 14:12:00 +11:00
sig: "0x" + bufToHex(msg.signature),
2021-08-12 15:26:06 +10:00
});
2021-08-12 16:36:28 +10:00
2022-02-04 14:12:00 +11:00
console.log("Recovered", recovered);
console.log("ethAddress", "0x" + bufToHex(msg.ethAddress));
2021-08-12 16:36:28 +10:00
return equalByteArrays(recovered, msg.ethAddress);
2021-08-12 15:26:06 +10:00
}