UPDATE tutorial with recoverPublicKey

state-channel
pubkey 7 years ago
parent bcbd5e2a17
commit bef80387d0
  1. 32
      test/tutorials/encrypted-message.test.js
  2. 22
      tutorials/encrypted-message.md

@ -21,8 +21,7 @@ describe('encrypted-message.md', () => {
const payload = {
message: secretMessage,
signature,
sender: alice.publicKey
signature
};
const encrypted = await EthCrypto.encryptWithPublicKey(
bob.publicKey,
@ -37,28 +36,16 @@ describe('encrypted-message.md', () => {
encrypted
);
const decryptedPayload = JSON.parse(decrypted);
const senderAddress = EthCrypto.addressByPublicKey(decryptedPayload.sender);
// console.log('decryptedPayload:');
// console.dir(decryptedPayload);
// check signature
const signer = EthCrypto.recover(
const senderAddress = EthCrypto.recover(
decryptedPayload.signature,
EthCrypto.hash.keccak256(payload.message)
);
if (signer !== senderAddress)
throw new Error('signature not valid');
/* console.log(
'Got message from ' +
senderAddress +
': "' +
decryptedPayload.message + '"'
);
*/
assert.equal(senderAddress, alice.address);
assert.equal(decryptedPayload.message, secretMessage);
// answer
const answerMessage = 'Roger dad';
const answerSignature = EthCrypto.sign(
@ -67,11 +54,18 @@ describe('encrypted-message.md', () => {
);
const answerPayload = {
message: answerMessage,
signature: answerSignature,
sender: bob.publicKey
signature: answerSignature
};
const alicePublicKey = EthCrypto.recoverPublicKey(
decryptedPayload.signature,
EthCrypto.hash.keccak256(payload.message)
);
assert.equal(alicePublicKey, alice.publicKey);
const encryptedAnswer = await EthCrypto.encryptWithPublicKey(
decryptedPayload.sender,
alicePublicKey,
JSON.stringify(answerPayload)
);
assert.ok(encryptedAnswer);

@ -30,8 +30,7 @@ const signature = EthCrypto.sign(
);
const payload = {
message: secretMessage,
signature,
sender: alice.publicKey
signature
};
const encrypted = await EthCrypto.encryptWithPublicKey(
bob.publicKey, // by encryping with bobs publicKey, only bob can decrypt the payload with his privateKey
@ -57,15 +56,12 @@ const decrypted = await EthCrypto.decryptWithPrivateKey(
encrypted
);
const decryptedPayload = JSON.parse(decrypted);
const senderAddress = EthCrypto.addressByPublicKey(decryptedPayload.sender);
// check signature
const signer = EthCrypto.recover(
const senderAddress = EthCrypto.recover(
decryptedPayload.signature,
EthCrypto.hash.keccak256(payload.message)
);
if (signer !== senderAddress)
throw new Error('signature not valid');
console.log(
'Got message from ' +
@ -79,20 +75,26 @@ console.log(
## Creating an answer
Now that `Bob` got the message, he can also answer back to alice.
To to this he has to recover the publicKey of alice with `recoverPublicKey()`.
```javascript
const answerMessage = 'Roger dad';
const answerMessage = 'And I am Bob Kelso';
const answerSignature = EthCrypto.sign(
bob.privateKey,
EthCrypto.hash.keccak256(answerMessage)
);
const answerPayload = {
message: answerMessage,
signature: answerSignature,
sender: bob.publicKey
signature: answerSignature
};
const alicePublicKey = EthCrypto.recoverPublicKey(
decryptedPayload.signature,
EthCrypto.hash.keccak256(payload.message)
);
const encryptedAnswer = await EthCrypto.encryptWithPublicKey(
decryptedPayload.sender,
alicePublicKey,
JSON.stringify(answerPayload)
);
// now we send the encryptedAnswer to alice over the internet.. *bieb, bieb, blob*

Loading…
Cancel
Save