<li>Defined in <ahref="https://github.com/waku-org/js-waku/blob/eecdb6ae/packages/message-encryption/src/index.ts#L1">packages/message-encryption/src/index.ts:1</a></li></ul></aside>
<p>Symmetric encryption uses a unique key to encrypt and decrypt messages.</p>
<pre><codeclass="language-typescript"><spanclass="hl-2">import</span><spanclass="hl-0"> {</span><br/><spanclass="hl-0"></span><spanclass="hl-3">createDecoder</span><spanclass="hl-0">,</span><br/><spanclass="hl-0"></span><spanclass="hl-3">createEncoder</span><spanclass="hl-0">,</span><br/><spanclass="hl-0"></span><spanclass="hl-3">generateSymmetricKey</span><spanclass="hl-0">,</span><br/><spanclass="hl-0">} </span><spanclass="hl-2">from</span><spanclass="hl-0"></span><spanclass="hl-4">"@waku/message-encryption/symmetric"</span><spanclass="hl-0">;</span><br/><br/><spanclass="hl-5">// Generate a random key</span><br/><spanclass="hl-6">const</span><spanclass="hl-0"></span><spanclass="hl-7">key</span><spanclass="hl-0"> = </span><spanclass="hl-1">generateSymmetricKey</span><spanclass="hl-0">();</span><br/><br/><spanclass="hl-5">// To send messages, create an encoder</span><br/><spanclass="hl-6">const</span><spanclass="hl-0"></span><spanclass="hl-7">encoder</span><spanclass="hl-0"> = </span><spanclass="hl-1">createEncoder</span><spanclass="hl-0">(</span><spanclass="hl-3">contentTopic</span><spanclass="hl-0">, </span><spanclass="hl-3">key</span><spanclass="hl-0">);</span><br/><br/><spanclass="hl-5">// For example</span><br/><spanclass="hl-3">waku</span><spanclass="hl-0">.</span><spanclass="hl-3">lightPush</span><spanclass="hl-0">.</span><spanclass="hl-1">push</span><spanclass="hl-0">(</span><spanclass="hl-3">encoder</span><spanclass="hl-0">, { </span><spanclass="hl-3">payload</span><spanclass="hl-0"> });</span><br/><br/><spanclass="hl-5">// To receive messages, create a decoder</span><br/><spanclass="hl-6">const</span><spanclass="hl-0"></span><spanclass="hl-7">decoder</span><spanclass="hl-0"> = </span><spanclass="hl-1">createDecoder</span><spanclass="hl-0">(</span><spanclass="hl-3">contentTopic</span><spanclass="hl-0">, </span><spanclass="hl-3">key</span><spanclass="hl-0">);</span><br/><br/><spanclass="hl-5">// For example</span><br/><spanclass="hl-2">await</span><spanclass="hl-0"></span><spanclass="hl-3">waku</span><spanclass="hl-0">.</span><spanclass="hl-3">store</span><spanclass="hl-0">.</span><spanclass="hl-1">queryOrderedCallback</span><spanclass="hl-0">([</span><spanclass="hl-3">decoder</span><spanclass="hl-0">], (</span><spanclass="hl-3">msg</span><spanclass="hl-0">) </span><spanclass="hl-6">=></span><spanclass="hl-0"> {</span><br/><spanclass="hl-0"></span><spanclass="hl-5">// ...</span><br/><spanclass="hl-0">});</span>
<p>ECIES encryption enables encryption for a public key and decryption using a private key.</p>
<pre><codeclass="language-typescript"><spanclass="hl-2">import</span><spanclass="hl-0"> {</span><br/><spanclass="hl-0"></span><spanclass="hl-3">createDecoder</span><spanclass="hl-0">,</span><br/><spanclass="hl-0"></span><spanclass="hl-3">createEncoder</span><spanclass="hl-0">,</span><br/><spanclass="hl-0"></span><spanclass="hl-3">generatePrivateKey</span><spanclass="hl-0">,</span><br/><spanclass="hl-0"></span><spanclass="hl-3">getPublicKey</span><spanclass="hl-0">,</span><br/><spanclass="hl-0">} </span><spanclass="hl-2">from</span><spanclass="hl-0"></span><spanclass="hl-4">"@waku/message-encryption/ecies"</span><spanclass="hl-0">;</span><br/><br/><spanclass="hl-5">// Generate a random private key</span><br/><spanclass="hl-6">const</span><spanclass="hl-0"></span><spanclass="hl-7">privateKey</span><spanclass="hl-0"> = </span><spanclass="hl-1">generatePrivateKey</span><spanclass="hl-0">();</span><br/><br/><spanclass="hl-5">// Keep the private key secure, provide the public key to the sender</span><br/><spanclass="hl-6">const</span><spanclass="hl-0"></span><spanclass="hl-7">publicKey</span><spanclass="hl-0"> = </span><spanclass="hl-1">getPublicKey</span><spanclass="hl-0">(</span><spanclass="hl-3">privateKey</span><spanclass="hl-0">);</span><br/><br/><spanclass="hl-5">// To send messages, create an encoder</span><br/><spanclass="hl-6">const</span><spanclass="hl-0"></span><spanclass="hl-7">encoder</span><spanclass="hl-0"> = </span><spanclass="hl-1">createEncoder</span><spanclass="hl-0">(</span><spanclass="hl-3">contentTopic</span><spanclass="hl-0">, </span><spanclass="hl-3">publicKey</span><spanclass="hl-0">);</span><br/><br/><spanclass="hl-5">// For example</span><br/><spanclass="hl-3">waku</span><spanclass="hl-0">.</span><spanclass="hl-3">lightPush</span><spanclass="hl-0">.</span><spanclass="hl-1">push</span><spanclass="hl-0">(</span><spanclass="hl-3">encoder</span><spanclass="hl-0">, { </span><spanclass="hl-3">payload</span><spanclass="hl-0"> });</span><br/><br/><spanclass="hl-5">// To receive messages, create a decoder</span><br/><spanclass="hl-6">const</span><spanclass="hl-0"></span><spanclass="hl-7">decoder</span><spanclass="hl-0"> = </span><spanclass="hl-1">createDecoder</span><spanclass="hl-0">(</span><spanclass="hl-3">contentTopic</span><spanclass="hl-0">, </span><spanclass="hl-3">privateKey</span><spanclass="hl-0">);</span><br/><br/><spanclass="hl-5">// For example</span><br/><spanclass="hl-2">await</span><spanclass="hl-0"></span><spanclass="hl-3">waku</span><spanclass="hl-0">.</span><spanclass="hl-3">store</span><spanclass="hl-0">.</span><spanclass="hl-1">queryOrderedCallback</span><spanclass="hl-0">([</span><spanclass="hl-3">decoder</span><spanclass="hl-0">], (</span><spanclass="hl-3">msg</span><spanclass="hl-0">) </span><spanclass="hl-6">=></span><spanclass="hl-0"> {</span><br/><spanclass="hl-0"></span><spanclass="hl-5">// ...</span><br/><spanclass="hl-0">});</span>