2021-08-12 15:26:06 +10:00
|
|
|
import '@ethersproject/shims';
|
|
|
|
|
|
|
|
|
|
import { ethers } from 'ethers';
|
|
|
|
|
import { Signer } from '@ethersproject/abstract-signer';
|
|
|
|
|
import { PublicKeyMessage } from './messaging/wire';
|
|
|
|
|
import { hexToBuf, equalByteArrays, bufToHex } from 'js-waku/lib/utils';
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Sign the Eth-DM public key with Web3. This can then be published to let other
|
|
|
|
|
* users know to use this Eth-DM public key to encrypt messages for the
|
|
|
|
|
* Ethereum Address holder.
|
|
|
|
|
*/
|
|
|
|
|
export async function createPublicKeyMessage(
|
|
|
|
|
web3Signer: Signer,
|
2021-08-12 15:51:18 +10:00
|
|
|
address: string,
|
2021-08-12 15:26:06 +10:00
|
|
|
encryptionPublicKey: Uint8Array
|
|
|
|
|
): Promise<PublicKeyMessage> {
|
2021-08-12 15:51:18 +10:00
|
|
|
console.log('Asking wallet to sign Public Key Message');
|
2021-08-12 15:26:06 +10:00
|
|
|
const signature = await web3Signer.signMessage(
|
|
|
|
|
formatPublicKeyForSignature(encryptionPublicKey)
|
|
|
|
|
);
|
2021-08-12 15:51:18 +10:00
|
|
|
console.log('Public Key Message signed');
|
2021-08-12 15:26:06 +10:00
|
|
|
|
|
|
|
|
return new PublicKeyMessage({
|
|
|
|
|
encryptionPublicKey: encryptionPublicKey,
|
2021-08-12 15:51:18 +10:00
|
|
|
ethAddress: hexToBuf(address),
|
2021-08-12 15:26:06 +10:00
|
|
|
signature: hexToBuf(signature),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Validate that the Encryption Public Key was signed by the holder of the given Ethereum address.
|
|
|
|
|
*/
|
|
|
|
|
export function validatePublicKeyMessage(msg: PublicKeyMessage): boolean {
|
|
|
|
|
const formattedMsg = formatPublicKeyForSignature(msg.encryptionPublicKey);
|
|
|
|
|
try {
|
|
|
|
|
const sigAddress = ethers.utils.verifyMessage(formattedMsg, msg.signature);
|
|
|
|
|
return equalByteArrays(sigAddress, msg.ethAddress);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.log(
|
|
|
|
|
'Failed to verify signature for Public Key Message',
|
|
|
|
|
formattedMsg,
|
|
|
|
|
msg
|
|
|
|
|
);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Prepare Eth-Dm Public key to be signed for publication.
|
|
|
|
|
* The public key is set in on Object `{ encryptionPublicKey: string; }`, converted
|
|
|
|
|
* to JSON and then hashed with Keccak256.
|
|
|
|
|
* The usage of the object helps ensure the signature is only used in an Eth-DM
|
|
|
|
|
* context.
|
|
|
|
|
*/
|
|
|
|
|
function formatPublicKeyForSignature(encryptionPublicKey: Uint8Array): string {
|
|
|
|
|
return JSON.stringify({
|
|
|
|
|
encryptionPublicKey: bufToHex(encryptionPublicKey),
|
|
|
|
|
});
|
|
|
|
|
}
|