refactor: use ethers for typed data signing

This commit is contained in:
fryorcraken.eth 2022-08-29 15:16:41 +10:00
parent 6508403912
commit e541ce1744
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
5 changed files with 32 additions and 245 deletions

View File

@ -4,10 +4,11 @@
"private": true,
"homepage": "/eth-pm",
"dependencies": {
"@ethersproject/providers": "^5.6.8",
"@ethersproject/abstract-signer": "5.7.0",
"@ethersproject/providers": "5.7.0",
"@material-ui/core": "^4.12.3",
"@material-ui/icons": "^4.11.2",
"ethers": "^5.7.0",
"ethers": "5.7.0",
"fontsource-roboto": "^4.0.0",
"js-waku": "0.24.0-cdd0752",
"protobufjs": "^6.11.2",

View File

@ -1,7 +1,8 @@
lockfileVersion: 5.4
specifiers:
'@ethersproject/providers': ^5.6.8
'@ethersproject/abstract-signer': 5.7.0
'@ethersproject/providers': 5.7.0
'@ethersproject/shims': ^5.5.0
'@material-ui/core': ^4.12.3
'@material-ui/icons': ^4.11.2
@ -11,7 +12,7 @@ specifiers:
'@types/react-dom': ^17.0.11
cspell: ^6.0.0
eslint: ^8.9.0
ethers: ^5.7.0
ethers: 5.7.0
fontsource-roboto: ^4.0.0
js-waku: 0.24.0-cdd0752
npm-run-all: ^4.1.5
@ -24,7 +25,8 @@ specifiers:
uint8arrays: ^3.1.0
dependencies:
'@ethersproject/providers': 5.6.8
'@ethersproject/abstract-signer': 5.7.0
'@ethersproject/providers': 5.7.0
'@material-ui/core': 4.12.4_sk3eihvpffgp52mstba5zhq3vu
'@material-ui/icons': 4.11.3_ylmshrmshuypthvvj7eslpzdla
ethers: 5.7.0
@ -1945,18 +1947,6 @@ packages:
'@ethersproject/strings': 5.7.0
dev: false
/@ethersproject/abstract-provider/5.6.1:
resolution: {integrity: sha512-BxlIgogYJtp1FS8Muvj8YfdClk3unZH0vRMVX791Z9INBNT/kuACZ9GzaY1Y4yFq+YSy6/w4gzj3HCRKrK9hsQ==}
dependencies:
'@ethersproject/bignumber': 5.6.2
'@ethersproject/bytes': 5.6.1
'@ethersproject/logger': 5.6.0
'@ethersproject/networks': 5.6.4
'@ethersproject/properties': 5.6.0
'@ethersproject/transactions': 5.6.2
'@ethersproject/web': 5.6.1
dev: false
/@ethersproject/abstract-provider/5.7.0:
resolution: {integrity: sha512-R41c9UkchKCpAqStMYUpdunjo3pkEvZC3FAwZn5S5MGbXoMQOHIdHItezTETxAO5bevtMApSyEhn9+CHcDsWBw==}
dependencies:
@ -1969,16 +1959,6 @@ packages:
'@ethersproject/web': 5.7.0
dev: false
/@ethersproject/abstract-signer/5.6.2:
resolution: {integrity: sha512-n1r6lttFBG0t2vNiI3HoWaS/KdOt8xyDjzlP2cuevlWLG6EX0OwcKLyG/Kp/cuwNxdy/ous+R/DEMdTUwWQIjQ==}
dependencies:
'@ethersproject/abstract-provider': 5.6.1
'@ethersproject/bignumber': 5.6.2
'@ethersproject/bytes': 5.6.1
'@ethersproject/logger': 5.6.0
'@ethersproject/properties': 5.6.0
dev: false
/@ethersproject/abstract-signer/5.7.0:
resolution: {integrity: sha512-a16V8bq1/Cz+TGCkE2OPMTOUDLS3grCpdjoJCYNnVBbdYEMSgKrU0+B90s8b6H+ByYTBZN7a3g76jdIJi7UfKQ==}
dependencies:
@ -1989,16 +1969,6 @@ packages:
'@ethersproject/properties': 5.7.0
dev: false
/@ethersproject/address/5.6.1:
resolution: {integrity: sha512-uOgF0kS5MJv9ZvCz7x6T2EXJSzotiybApn4XlOgoTX0xdtyVIJ7pF+6cGPxiEq/dpBiTfMiw7Yc81JcwhSYA0Q==}
dependencies:
'@ethersproject/bignumber': 5.6.2
'@ethersproject/bytes': 5.6.1
'@ethersproject/keccak256': 5.6.1
'@ethersproject/logger': 5.6.0
'@ethersproject/rlp': 5.6.1
dev: false
/@ethersproject/address/5.7.0:
resolution: {integrity: sha512-9wYhYt7aghVGo758POM5nqcOMaE168Q6aRLJZwUmiqSrAungkG74gSSeKEIR7ukixesdRZGPgVqme6vmxs1fkA==}
dependencies:
@ -2009,25 +1979,12 @@ packages:
'@ethersproject/rlp': 5.7.0
dev: false
/@ethersproject/base64/5.6.1:
resolution: {integrity: sha512-qB76rjop6a0RIYYMiB4Eh/8n+Hxu2NIZm8S/Q7kNo5pmZfXhHGHmS4MinUainiBC54SCyRnwzL+KZjj8zbsSsw==}
dependencies:
'@ethersproject/bytes': 5.6.1
dev: false
/@ethersproject/base64/5.7.0:
resolution: {integrity: sha512-Dr8tcHt2mEbsZr/mwTPIQAf3Ai0Bks/7gTw9dSqk1mQvhW3XvRlmDJr/4n+wg1JmCl16NZue17CDh8xb/vZ0sQ==}
dependencies:
'@ethersproject/bytes': 5.7.0
dev: false
/@ethersproject/basex/5.6.1:
resolution: {integrity: sha512-a52MkVz4vuBXR06nvflPMotld1FJWSj2QT0985v7P/emPZO00PucFAkbcmq2vpVU7Ts7umKiSI6SppiLykVWsA==}
dependencies:
'@ethersproject/bytes': 5.6.1
'@ethersproject/properties': 5.6.0
dev: false
/@ethersproject/basex/5.7.0:
resolution: {integrity: sha512-ywlh43GwZLv2Voc2gQVTKBoVQ1mti3d8HK5aMxsfu/nRDnMmNqaSJ3r3n85HBByT8OpoY96SXM1FogC533T4zw==}
dependencies:
@ -2035,14 +1992,6 @@ packages:
'@ethersproject/properties': 5.7.0
dev: false
/@ethersproject/bignumber/5.6.2:
resolution: {integrity: sha512-v7+EEUbhGqT3XJ9LMPsKvXYHFc8eHxTowFCG/HgJErmq4XHJ2WR7aeyICg3uTOAQ7Icn0GFHAohXEhxQHq4Ubw==}
dependencies:
'@ethersproject/bytes': 5.6.1
'@ethersproject/logger': 5.6.0
bn.js: 5.2.1
dev: false
/@ethersproject/bignumber/5.7.0:
resolution: {integrity: sha512-n1CAdIHRWjSucQO3MC1zPSVgV/6dy/fjL9pMrPP9peL+QxEg9wOsVqwD4+818B6LUEtaXzVHQiuivzRoxPxUGw==}
dependencies:
@ -2051,24 +2000,12 @@ packages:
bn.js: 5.2.1
dev: false
/@ethersproject/bytes/5.6.1:
resolution: {integrity: sha512-NwQt7cKn5+ZE4uDn+X5RAXLp46E1chXoaMmrxAyA0rblpxz8t58lVkrHXoRIn0lz1joQElQ8410GqhTqMOwc6g==}
dependencies:
'@ethersproject/logger': 5.6.0
dev: false
/@ethersproject/bytes/5.7.0:
resolution: {integrity: sha512-nsbxwgFXWh9NyYWo+U8atvmMsSdKJprTcICAkvbBffT75qDocbuggBU0SJiVK2MuTrp0q+xvLkTnGMPK1+uA9A==}
dependencies:
'@ethersproject/logger': 5.7.0
dev: false
/@ethersproject/constants/5.6.1:
resolution: {integrity: sha512-QSq9WVnZbxXYFftrjSjZDUshp6/eKp6qrtdBtUCm0QxCV5z1fG/w3kdlcsjMCQuQHUnAclKoK7XpXMezhRDOLg==}
dependencies:
'@ethersproject/bignumber': 5.6.2
dev: false
/@ethersproject/constants/5.7.0:
resolution: {integrity: sha512-DHI+y5dBNvkpYUMiRQyxRBYBefZkJfo70VUkUAsRjcPs47muV9evftfZ0PJVCXYbAiCgght0DtcF9srFQmIgWA==}
dependencies:
@ -2090,19 +2027,6 @@ packages:
'@ethersproject/transactions': 5.7.0
dev: false
/@ethersproject/hash/5.6.1:
resolution: {integrity: sha512-L1xAHurbaxG8VVul4ankNX5HgQ8PNCTrnVXEiFnE9xoRnaUcgfD12tZINtDinSllxPLCtGwguQxJ5E6keE84pA==}
dependencies:
'@ethersproject/abstract-signer': 5.6.2
'@ethersproject/address': 5.6.1
'@ethersproject/bignumber': 5.6.2
'@ethersproject/bytes': 5.6.1
'@ethersproject/keccak256': 5.6.1
'@ethersproject/logger': 5.6.0
'@ethersproject/properties': 5.6.0
'@ethersproject/strings': 5.6.1
dev: false
/@ethersproject/hash/5.7.0:
resolution: {integrity: sha512-qX5WrQfnah1EFnO5zJv1v46a8HW0+E5xuBBDTwMFZLuVTx0tbU2kkx15NqdjxecrLGatQN9FGQKpb1FKdHCt+g==}
dependencies:
@ -2152,13 +2076,6 @@ packages:
scrypt-js: 3.0.1
dev: false
/@ethersproject/keccak256/5.6.1:
resolution: {integrity: sha512-bB7DQHCTRDooZZdL3lk9wpL0+XuG3XLGHLh3cePnybsO3V0rdCAOQGpn/0R3aODmnTOOkCATJiD2hnL+5bwthA==}
dependencies:
'@ethersproject/bytes': 5.6.1
js-sha3: 0.8.0
dev: false
/@ethersproject/keccak256/5.7.0:
resolution: {integrity: sha512-2UcPboeL/iW+pSg6vZ6ydF8tCnv3Iu/8tUmLLzWWGzxWKFFqOBQFLo6uLUv6BDrLgCDfN28RJ/wtByx+jZ4KBg==}
dependencies:
@ -2166,20 +2083,10 @@ packages:
js-sha3: 0.8.0
dev: false
/@ethersproject/logger/5.6.0:
resolution: {integrity: sha512-BiBWllUROH9w+P21RzoxJKzqoqpkyM1pRnEKG69bulE9TSQD8SAIvTQqIMZmmCO8pUNkgLP1wndX1gKghSpBmg==}
dev: false
/@ethersproject/logger/5.7.0:
resolution: {integrity: sha512-0odtFdXu/XHtjQXJYA3u9G0G8btm0ND5Cu8M7i5vhEcE8/HmF4Lbdqanwyv4uQTr2tx6b7fQRmgLrsnpQlmnig==}
dev: false
/@ethersproject/networks/5.6.4:
resolution: {integrity: sha512-KShHeHPahHI2UlWdtDMn2lJETcbtaJge4k7XSjDR9h79QTd6yQJmv6Cp2ZA4JdqWnhszAOLSuJEd9C0PRw7hSQ==}
dependencies:
'@ethersproject/logger': 5.6.0
dev: false
/@ethersproject/networks/5.7.0:
resolution: {integrity: sha512-MG6oHSQHd4ebvJrleEQQ4HhVu8Ichr0RDYEfHzsVAVjHNM+w36x9wp9r+hf1JstMXtseXDtkiVoARAG6M959AA==}
dependencies:
@ -2193,46 +2100,12 @@ packages:
'@ethersproject/sha2': 5.7.0
dev: false
/@ethersproject/properties/5.6.0:
resolution: {integrity: sha512-szoOkHskajKePTJSZ46uHUWWkbv7TzP2ypdEK6jGMqJaEt2sb0jCgfBo0gH0m2HBpRixMuJ6TBRaQCF7a9DoCg==}
dependencies:
'@ethersproject/logger': 5.6.0
dev: false
/@ethersproject/properties/5.7.0:
resolution: {integrity: sha512-J87jy8suntrAkIZtecpxEPxY//szqr1mlBaYlQ0r4RCaiD2hjheqF9s1LVE8vVuJCXisjIP+JgtK/Do54ej4Sw==}
dependencies:
'@ethersproject/logger': 5.7.0
dev: false
/@ethersproject/providers/5.6.8:
resolution: {integrity: sha512-Wf+CseT/iOJjrGtAOf3ck9zS7AgPmr2fZ3N97r4+YXN3mBePTG2/bJ8DApl9mVwYL+RpYbNxMEkEp4mPGdwG/w==}
dependencies:
'@ethersproject/abstract-provider': 5.6.1
'@ethersproject/abstract-signer': 5.6.2
'@ethersproject/address': 5.6.1
'@ethersproject/base64': 5.6.1
'@ethersproject/basex': 5.6.1
'@ethersproject/bignumber': 5.6.2
'@ethersproject/bytes': 5.6.1
'@ethersproject/constants': 5.6.1
'@ethersproject/hash': 5.6.1
'@ethersproject/logger': 5.6.0
'@ethersproject/networks': 5.6.4
'@ethersproject/properties': 5.6.0
'@ethersproject/random': 5.6.1
'@ethersproject/rlp': 5.6.1
'@ethersproject/sha2': 5.6.1
'@ethersproject/strings': 5.6.1
'@ethersproject/transactions': 5.6.2
'@ethersproject/web': 5.6.1
bech32: 1.1.4
ws: 7.4.6
transitivePeerDependencies:
- bufferutil
- utf-8-validate
dev: false
/@ethersproject/providers/5.7.0:
resolution: {integrity: sha512-+TTrrINMzZ0aXtlwO/95uhAggKm4USLm1PbeCBR/3XZ7+Oey+3pMyddzZEyRhizHpy1HXV0FRWRMI1O3EGYibA==}
dependencies:
@ -2261,13 +2134,6 @@ packages:
- utf-8-validate
dev: false
/@ethersproject/random/5.6.1:
resolution: {integrity: sha512-/wtPNHwbmng+5yi3fkipA8YBT59DdkGRoC2vWk09Dci/q5DlgnMkhIycjHlavrvrjJBkFjO/ueLyT+aUDfc4lA==}
dependencies:
'@ethersproject/bytes': 5.6.1
'@ethersproject/logger': 5.6.0
dev: false
/@ethersproject/random/5.7.0:
resolution: {integrity: sha512-19WjScqRA8IIeWclFme75VMXSBvi4e6InrUNuaR4s5pTF2qNhcGdCUwdxUVGtDDqC00sDLCO93jPQoDUH4HVmQ==}
dependencies:
@ -2275,13 +2141,6 @@ packages:
'@ethersproject/logger': 5.7.0
dev: false
/@ethersproject/rlp/5.6.1:
resolution: {integrity: sha512-uYjmcZx+DKlFUk7a5/W9aQVaoEC7+1MOBgNtvNg13+RnuUwT4F0zTovC0tmay5SmRslb29V1B7Y5KCri46WhuQ==}
dependencies:
'@ethersproject/bytes': 5.6.1
'@ethersproject/logger': 5.6.0
dev: false
/@ethersproject/rlp/5.7.0:
resolution: {integrity: sha512-rBxzX2vK8mVF7b0Tol44t5Tb8gomOHkj5guL+HhzQ1yBh/ydjGnpw6at+X6Iw0Kp3OzzzkcKp8N9r0W4kYSs9w==}
dependencies:
@ -2289,14 +2148,6 @@ packages:
'@ethersproject/logger': 5.7.0
dev: false
/@ethersproject/sha2/5.6.1:
resolution: {integrity: sha512-5K2GyqcW7G4Yo3uenHegbXRPDgARpWUiXc6RiF7b6i/HXUoWlb7uCARh7BAHg7/qT/Q5ydofNwiZcim9qpjB6g==}
dependencies:
'@ethersproject/bytes': 5.6.1
'@ethersproject/logger': 5.6.0
hash.js: 1.1.7
dev: false
/@ethersproject/sha2/5.7.0:
resolution: {integrity: sha512-gKlH42riwb3KYp0reLsFTokByAKoJdgFCwI+CCiX/k+Jm2mbNs6oOaCjYQSlI1+XBVejwH2KrmCbMAT/GnRDQw==}
dependencies:
@ -2309,17 +2160,6 @@ packages:
resolution: {integrity: sha512-PcTzqc3XIWT2JHuRpG/VYDJoxLoJlOnh+EtZspdCkCDbdTXMovqu9JHmLHgrq3Yz1AmPC1KoKVYBt7vff5VTQg==}
dev: true
/@ethersproject/signing-key/5.6.2:
resolution: {integrity: sha512-jVbu0RuP7EFpw82vHcL+GP35+KaNruVAZM90GxgQnGqB6crhBqW/ozBfFvdeImtmb4qPko0uxXjn8l9jpn0cwQ==}
dependencies:
'@ethersproject/bytes': 5.6.1
'@ethersproject/logger': 5.6.0
'@ethersproject/properties': 5.6.0
bn.js: 5.2.1
elliptic: 6.5.4
hash.js: 1.1.7
dev: false
/@ethersproject/signing-key/5.7.0:
resolution: {integrity: sha512-MZdy2nL3wO0u7gkB4nA/pEf8lu1TlFswPNmy8AiYkfKTdO6eXBJyUdmHO/ehm/htHw9K/qF8ujnTyUAD+Ry54Q==}
dependencies:
@ -2342,14 +2182,6 @@ packages:
'@ethersproject/strings': 5.7.0
dev: false
/@ethersproject/strings/5.6.1:
resolution: {integrity: sha512-2X1Lgk6Jyfg26MUnsHiT456U9ijxKUybz8IM1Vih+NJxYtXhmvKBcHOmvGqpFSVJ0nQ4ZCoIViR8XlRw1v/+Cw==}
dependencies:
'@ethersproject/bytes': 5.6.1
'@ethersproject/constants': 5.6.1
'@ethersproject/logger': 5.6.0
dev: false
/@ethersproject/strings/5.7.0:
resolution: {integrity: sha512-/9nu+lj0YswRNSH0NXYqrh8775XNyEdUQAuf3f+SmOrnVewcJ5SBNAjF7lpgehKi4abvNNXyf+HX86czCdJ8Mg==}
dependencies:
@ -2358,20 +2190,6 @@ packages:
'@ethersproject/logger': 5.7.0
dev: false
/@ethersproject/transactions/5.6.2:
resolution: {integrity: sha512-BuV63IRPHmJvthNkkt9G70Ullx6AcM+SDc+a8Aw/8Yew6YwT51TcBKEp1P4oOQ/bP25I18JJr7rcFRgFtU9B2Q==}
dependencies:
'@ethersproject/address': 5.6.1
'@ethersproject/bignumber': 5.6.2
'@ethersproject/bytes': 5.6.1
'@ethersproject/constants': 5.6.1
'@ethersproject/keccak256': 5.6.1
'@ethersproject/logger': 5.6.0
'@ethersproject/properties': 5.6.0
'@ethersproject/rlp': 5.6.1
'@ethersproject/signing-key': 5.6.2
dev: false
/@ethersproject/transactions/5.7.0:
resolution: {integrity: sha512-kmcNicCp1lp8qanMTC3RIikGgoJ80ztTyvtsFvCYpSCfkjhD0jZ2LOrnbcuxuToLIUYYf+4XwD1rP+B/erDIhQ==}
dependencies:
@ -2414,16 +2232,6 @@ packages:
'@ethersproject/wordlists': 5.7.0
dev: false
/@ethersproject/web/5.6.1:
resolution: {integrity: sha512-/vSyzaQlNXkO1WV+RneYKqCJwualcUdx/Z3gseVovZP0wIlOFcCE1hkRhKBH8ImKbGQbMl9EAAyJFrJu7V0aqA==}
dependencies:
'@ethersproject/base64': 5.6.1
'@ethersproject/bytes': 5.6.1
'@ethersproject/logger': 5.6.0
'@ethersproject/properties': 5.6.0
'@ethersproject/strings': 5.6.1
dev: false
/@ethersproject/web/5.7.0:
resolution: {integrity: sha512-ApHcbbj+muRASVDSCl/tgxaH2LBkRMEYfLOLVa0COipx0+nlu0QKet7U2lEg0vdkh8XRSLf2nd1f1Uk9SrVSGA==}
dependencies:
@ -3316,7 +3124,7 @@ packages:
'@libp2p/interface-connection': 3.0.1_undici@5.9.1
'@libp2p/interface-peer-id': 1.0.4
'@libp2p/interface-pubsub': 2.0.1_undici@5.9.1
'@libp2p/interface-registrar': 2.0.2_undici@5.9.1
'@libp2p/interface-registrar': 2.0.3_undici@5.9.1
'@libp2p/interfaces': 3.0.3
'@libp2p/logger': 2.0.1
'@libp2p/peer-collections': 2.0.0
@ -3342,7 +3150,7 @@ packages:
engines: {node: '>=16.0.0', npm: '>=7.0.0'}
dependencies:
'@libp2p/interface-peer-id': 1.0.4
'@libp2p/interface-registrar': 2.0.2_undici@5.9.1
'@libp2p/interface-registrar': 2.0.3_undici@5.9.1
'@libp2p/logger': 2.0.1
'@multiformats/multiaddr': 10.3.3_undici@5.9.1
err-code: 3.0.1
@ -8719,7 +8527,7 @@ packages:
dependencies:
'@chainsafe/libp2p-gossipsub': 3.5.1_2khuc3oodhgq7dgyspt6akiwne
'@chainsafe/libp2p-noise': 7.0.3
'@ethersproject/rlp': 5.6.1
'@ethersproject/rlp': 5.7.0
'@libp2p/crypto': 1.0.3_uint8arraylist@2.3.2
'@libp2p/interface-connection': 3.0.1_undici@5.9.1
'@libp2p/interface-peer-discovery': 1.0.1_undici@5.9.1

View File

@ -254,7 +254,7 @@ function App() {
address={address}
EncryptionKeyPair={encryptionKeyPair}
waku={waku}
providerRequest={provider?.provider?.request}
signer={provider?.getSigner()}
/>
</fieldset>
<fieldset>

View File

@ -8,21 +8,20 @@ import {
import { PublicKeyMessage } from "./messaging/wire";
import { WakuMessage, Waku } from "js-waku";
import { PublicKeyContentTopic } from "./waku";
import type { TypedDataSigner } from "@ethersproject/abstract-signer";
interface Props {
EncryptionKeyPair: KeyPair | undefined;
waku: Waku | undefined;
address: string | undefined;
providerRequest:
| ((request: { method: string; params?: Array<any> }) => Promise<any>)
| undefined;
signer: TypedDataSigner | undefined;
}
export default function BroadcastPublicKey({
EncryptionKeyPair,
waku,
address,
providerRequest,
signer,
}: Props) {
const [publicKeyMsg, setPublicKeyMsg] = useState<PublicKeyMessage>();
@ -30,7 +29,7 @@ export default function BroadcastPublicKey({
if (!EncryptionKeyPair) return;
if (!address) return;
if (!waku) return;
if (!providerRequest) return;
if (!signer) return;
if (publicKeyMsg) {
encodePublicKeyWakuMessage(publicKeyMsg)
@ -43,11 +42,7 @@ export default function BroadcastPublicKey({
console.log("Failed to encode Public Key Message in Waku Message", e);
});
} else {
createPublicKeyMessage(
address,
EncryptionKeyPair.publicKey,
providerRequest
)
createPublicKeyMessage(address, EncryptionKeyPair.publicKey, signer)
.then((msg) => {
setPublicKeyMsg(msg);
encodePublicKeyWakuMessage(msg)
@ -77,7 +72,7 @@ export default function BroadcastPublicKey({
variant="contained"
color="primary"
onClick={broadcastPublicKey}
disabled={!EncryptionKeyPair || !waku || !address || !providerRequest}
disabled={!EncryptionKeyPair || !waku || !address || !signer}
>
Broadcast Encryption Public Key
</Button>

View File

@ -5,6 +5,7 @@ import { generatePrivateKey, getPublicKey, utils } from "js-waku";
import { PublicKeyContentTopic } from "./waku";
import { keccak256, _TypedDataEncoder, recoverAddress } from "ethers/lib/utils";
import { equals } from "uint8arrays/equals";
import type { TypedDataSigner } from "@ethersproject/abstract-signer";
export const PublicKeyMessageEncryptionKey = utils.hexToBytes(
keccak256(utils.utf8ToBytes(PublicKeyContentTopic))
@ -32,15 +33,12 @@ export async function generateEncryptionKeyPair(): Promise<KeyPair> {
export async function createPublicKeyMessage(
address: string,
encryptionPublicKey: Uint8Array,
providerRequest: (request: {
method: string;
params?: Array<any>;
}) => Promise<any>
signer: TypedDataSigner
): Promise<PublicKeyMessage> {
const signature = await signEncryptionKey(
encryptionPublicKey,
address,
providerRequest
signer
);
console.log("Asking wallet to sign Public Key Message");
@ -59,7 +57,7 @@ function buildMsgParams(encryptionPublicKey: Uint8Array, fromAddress: string) {
name: "Ethereum Private Message over Waku",
version: "1",
},
message: {
value: {
message:
"By signing this message you certify that messages addressed to `ownerAddress` must be encrypted with `encryptionPublicKey`",
encryptionPublicKey: utils.bytesToHex(encryptionPublicKey),
@ -68,10 +66,6 @@ function buildMsgParams(encryptionPublicKey: Uint8Array, fromAddress: string) {
// Refers to the keys of the *types* object below.
primaryType: "PublishEncryptionPublicKey",
types: {
EIP712Domain: [
{ name: "name", type: "string" },
{ name: "version", type: "string" },
],
PublishEncryptionPublicKey: [
{ name: "message", type: "string" },
{ name: "encryptionPublicKey", type: "string" },
@ -84,21 +78,14 @@ function buildMsgParams(encryptionPublicKey: Uint8Array, fromAddress: string) {
export async function signEncryptionKey(
encryptionPublicKey: Uint8Array,
fromAddress: string,
providerRequest: (request: {
method: string;
params?: Array<any>;
from?: string;
}) => Promise<any>
signer: TypedDataSigner
): Promise<Uint8Array> {
const msgParams = JSON.stringify(
buildMsgParams(encryptionPublicKey, fromAddress)
const { domain, types, value } = buildMsgParams(
encryptionPublicKey,
fromAddress
);
const result = await providerRequest({
method: "eth_signTypedData_v4",
params: [fromAddress, msgParams],
from: fromAddress,
});
const result = await signer._signTypedData(domain, types, value);
console.log("TYPED SIGNED:" + JSON.stringify(result));
@ -109,25 +96,21 @@ export async function signEncryptionKey(
* Validate that the Encryption Public Key was signed by the holder of the given Ethereum address.
*/
export function validatePublicKeyMessage(msg: PublicKeyMessage): boolean {
const typedData = buildMsgParams(
const { domain, types, value } = buildMsgParams(
msg.encryptionPublicKey,
"0x" + utils.bytesToHex(msg.ethAddress)
);
const hash = _TypedDataEncoder.hash(
typedData.domain,
typedData.types,
typedData.message
);
try {
const hash = _TypedDataEncoder.hash(domain, types, value);
const recovered = recoverAddress(hash, msg.signature);
console.log("Recovered", recovered);
console.log("ethAddress", "0x" + utils.bytesToHex(msg.ethAddress));
return equals(utils.hexToBytes(recovered), msg.ethAddress);
return equals(utils.hexToBytes(recovered.toLowerCase()), msg.ethAddress);
} catch (e) {
console.error("Could not recover public key from signature");
console.error("Could not recover public key from signature", e);
return false;
}
}