<sectionclass="tsd-panel tsd-typography"><aid="md:wakumessage-encryption"class="tsd-anchor"></a><h1><ahref="#md:wakumessage-encryption"><code>@waku/message-encryption</code></a></h1><p>Provide Waku Message Version 1 payload encryption as defined in <ahref="https://rfc.vac.dev/spec/26/">26/WAKU2-PAYLOAD</a>.</p>
<aid="md:symmetric-encryption"class="tsd-anchor"></a><h2><ahref="#md:symmetric-encryption">Symmetric Encryption</a></h2><p>Symmetric encryption uses a unique key to encrypt and decrypt messages.</p>
<pre><codeclass="language-typescript"><spanclass="hl-5">import</span><spanclass="hl-1"> {</span><br/><spanclass="hl-1"></span><spanclass="hl-3">createDecoder</span><spanclass="hl-1">,</span><br/><spanclass="hl-1"></span><spanclass="hl-3">createEncoder</span><spanclass="hl-1">,</span><br/><spanclass="hl-1"></span><spanclass="hl-3">generateSymmetricKey</span><br/><spanclass="hl-1">} </span><spanclass="hl-5">from</span><spanclass="hl-1"></span><spanclass="hl-2">"@waku/message-encryption/symmetric"</span><spanclass="hl-1">;</span><br/><br/><spanclass="hl-7">// Generate a random key</span><br/><spanclass="hl-6">const</span><spanclass="hl-1"></span><spanclass="hl-4">key</span><spanclass="hl-1"> = </span><spanclass="hl-0">generateSymmetricKey</span><spanclass="hl-1">();</span><br/><br/><spanclass="hl-7">// To send messages, create an encoder</span><br/><spanclass="hl-6">const</span><spanclass="hl-1"></span><spanclass="hl-4">encoder</span><spanclass="hl-1"> = </span><spanclass="hl-0">createEncoder</span><spanclass="hl-1">(</span><spanclass="hl-3">contentTopic</span><spanclass="hl-1">, </span><spanclass="hl-3">key</span><spanclass="hl-1">);</span><br/><br/><spanclass="hl-7">// For example</span><br/><spanclass="hl-3">waku</span><spanclass="hl-1">.</span><spanclass="hl-3">lightPush</span><spanclass="hl-1">.</span><spanclass="hl-0">push</span><spanclass="hl-1">(</span><spanclass="hl-3">encoder</span><spanclass="hl-1">, { </span><spanclass="hl-3">payload</span><spanclass="hl-1"> });</span><br/><br/><spanclass="hl-7">// To receive messages, create a decoder</span><br/><spanclass="hl-6">const</span><spanclass="hl-1"></span><spanclass="hl-4">decoder</span><spanclass="hl-1"> = </span><spanclass="hl-0">createDecoder</span><spanclass="hl-1">(</span><spanclass="hl-3">contentTopic</span><spanclass="hl-1">, </span><spanclass="hl-3">key</span><spanclass="hl-1">);</span><br/><br/><spanclass="hl-7">// For example</span><br/><spanclass="hl-5">await</span><spanclass="hl-1"></span><spanclass="hl-3">waku</span><spanclass="hl-1">.</span><spanclass="hl-3">store</span><spanclass="hl-1">.</span><spanclass="hl-0">queryOrderedCallback</span><spanclass="hl-1">([</span><spanclass="hl-3">decoder</span><spanclass="hl-1">], (</span><spanclass="hl-3">msg</span><spanclass="hl-1">) </span><spanclass="hl-6">=></span><spanclass="hl-1"> {</span><br/><spanclass="hl-1"></span><spanclass="hl-7">// ...</span><br/><spanclass="hl-1">});</span>
</code><button>Copy</button></pre>
<aid="md:ecies-encryption"class="tsd-anchor"></a><h2><ahref="#md:ecies-encryption">ECIES Encryption</a></h2><p>ECIES encryption enables encryption for a public key and decryption using a private key.</p>
<pre><codeclass="language-typescript"><spanclass="hl-5">import</span><spanclass="hl-1"> {</span><br/><spanclass="hl-1"></span><spanclass="hl-3">createDecoder</span><spanclass="hl-1">,</span><br/><spanclass="hl-1"></span><spanclass="hl-3">createEncoder</span><spanclass="hl-1">,</span><br/><spanclass="hl-1"></span><spanclass="hl-3">generatePrivateKey</span><spanclass="hl-1">,</span><br/><spanclass="hl-1"></span><spanclass="hl-3">getPublicKey</span><br/><spanclass="hl-1">} </span><spanclass="hl-5">from</span><spanclass="hl-1"></span><spanclass="hl-2">"@waku/message-encryption/ecies"</span><spanclass="hl-1">;</span><br/><br/><spanclass="hl-7">// Generate a random private key</span><br/><spanclass="hl-6">const</span><spanclass="hl-1"></span><spanclass="hl-4">privateKey</span><spanclass="hl-1"> = </span><spanclass="hl-0">generatePrivateKey</span><spanclass="hl-1">();</span><br/><br/><spanclass="hl-7">// Keep the private key secure, provide the public key to the sender</span><br/><spanclass="hl-6">const</span><spanclass="hl-1"></span><spanclass="hl-4">publicKey</span><spanclass="hl-1"> = </span><spanclass="hl-0">getPublicKey</span><spanclass="hl-1">(</span><spanclass="hl-3">privateKey</span><spanclass="hl-1">);</span><br/><br/><spanclass="hl-7">// To send messages, create an encoder</span><br/><spanclass="hl-6">const</span><spanclass="hl-1"></span><spanclass="hl-4">encoder</span><spanclass="hl-1"> = </span><spanclass="hl-0">createEncoder</span><spanclass="hl-1">(</span><spanclass="hl-3">contentTopic</span><spanclass="hl-1">, </span><spanclass="hl-3">publicKey</span><spanclass="hl-1">);</span><br/><br/><spanclass="hl-7">// For example</span><br/><spanclass="hl-3">waku</span><spanclass="hl-1">.</span><spanclass="hl-3">lightPush</span><spanclass="hl-1">.</span><spanclass="hl-0">push</span><spanclass="hl-1">(</span><spanclass="hl-3">encoder</span><spanclass="hl-1">, { </span><spanclass="hl-3">payload</span><spanclass="hl-1"> });</span><br/><br/><spanclass="hl-7">// To receive messages, create a decoder</span><br/><spanclass="hl-6">const</span><spanclass="hl-1"></span><spanclass="hl-4">decoder</span><spanclass="hl-1"> = </span><spanclass="hl-0">createDecoder</span><spanclass="hl-1">(</span><spanclass="hl-3">contentTopic</span><spanclass="hl-1">, </span><spanclass="hl-3">privateKey</span><spanclass="hl-1">);</span><br/><br/><spanclass="hl-7">// For example</span><br/><spanclass="hl-5">await</span><spanclass="hl-1"></span><spanclass="hl-3">waku</span><spanclass="hl-1">.</span><spanclass="hl-3">store</span><spanclass="hl-1">.</span><spanclass="hl-0">queryOrderedCallback</span><spanclass="hl-1">([</span><spanclass="hl-3">decoder</span><spanclass="hl-1">], (</span><spanclass="hl-3">msg</span><spanclass="hl-1">) </span><spanclass="hl-6">=></span><spanclass="hl-1"> {</span><br/><spanclass="hl-1"></span><spanclass="hl-7">// ...</span><br/><spanclass="hl-1">});</span>