mirror of
https://github.com/logos-messaging/lab.waku.org.git
synced 2026-01-03 22:33:09 +00:00
1 line
4.4 KiB
JavaScript
1 line
4.4 KiB
JavaScript
(self.webpackChunk_N_E=self.webpackChunk_N_E||[]).push([[990],{4033:function(e,t,n){e.exports=n(94)},5136:function(e,t,n){"use strict";n.d(t,{UF:function(){return I},Mf:function(){return x},oQ:function(){return m}});var r=n(9746),i=n(5867),o=n(5210),a=n(9527),c=n(7761),s=n(4755),u=n(9710),l=n(6398);let f={keySize:32,ivSize:12,algorithm:{name:"AES-GCM",length:128}},y=BigInt(1e6),h={node:c,web:"object"==typeof self&&"crypto"in self?self.crypto:void 0};function p(){if(h.web)return h.web.subtle;if(h.node)return h.node.webcrypto.subtle;throw Error("The environment doesn't have Crypto Subtle API (if in the browser, be sure to use to be in a secure context, ie, https)")}let d=s.P6.randomBytes;function m(){return d(f.keySize)}async function g(e,t){let[n,r]=await s.Xx(e,t,{recovered:!0,der:!1});return(0,u.zo)([n,new Uint8Array([r])],n.length+1)}function w(e){return new Uint8Array(l.keccak256.arrayBuffer(e))}s.P6.sha256,s.$3;var b=n(1363);class v extends r.lY{signature;signaturePublicKey;_decodedPayload;constructor(e,t,n,r,i){super(e,t),this.signature=r,this.signaturePublicKey=i,this._decodedPayload=n}get payload(){return this._decodedPayload}verifySignature(e){return!!this.signaturePublicKey&&(0,b.f)(this.signaturePublicKey,e)}}async function P(e,t,n){return p().importKey("raw",t,f.algorithm,!1,["encrypt"]).then(t=>p().encrypt({iv:e,...f.algorithm},t,n)).then(e=>new Uint8Array(e))}async function K(e,t,n){return p().importKey("raw",t,f.algorithm,!1,["decrypt"]).then(t=>p().decrypt({iv:e,...f.algorithm},t,n)).then(e=>new Uint8Array(e))}async function z(e,t){let n=d(f.ivSize),r=await P(n,(0,u.nr)(t),e);return(0,u.zo)([r,n])}async function T(e,t){let n=e.length-f.ivSize,r=e.slice(0,n),i=e.slice(n);return K(i,(0,u.nr)(t),r)}function S(e){let t=1;for(let n=e.length;n>=256;n/=256)t++;return t}async function U(e,t){let n=new Uint8Array([0]);n=function(e,t){let n=S(t),r=new Uint8Array(4),i=new DataView(r.buffer);return i.setUint32(0,t.length,!0),r=r.slice(0,n),e=(0,u.zo)([e,r]),e[0]|=n,e}(n,e),n=(0,u.zo)([n,e]);let r=1+S(e)+e.length;t&&(r+=65);let i=r%256,o=256-i,a=d(o);if(a.length!==o||!(o<=3)&&-1===a.findIndex(e=>0!==e))throw Error("failed to generate random padding of size "+o);if(n=(0,u.zo)([n,a]),t){n[0]|=4;let e=w(n),r=await g(e,t);n=(0,u.zo)([n,r])}return n}let A=new a.Yd("message-encryption:symmetric");class k{pubsubTopic;contentTopic;symKey;sigPrivKey;ephemeral;metaSetter;constructor(e,t,n,r,i=!1,o){if(this.pubsubTopic=e,this.contentTopic=t,this.symKey=n,this.sigPrivKey=r,this.ephemeral=i,this.metaSetter=o,!t||""===t)throw Error("Content topic must be specified")}async toWire(e){let t=await this.toProtoObj(e);if(t)return o.HE.encode(t)}async toProtoObj(e){let t=e.timestamp??new Date,n=await U(e.payload,this.sigPrivKey),r=await z(n,this.symKey),i={payload:r,version:1,contentTopic:this.contentTopic,timestamp:BigInt(t.valueOf())*y,meta:void 0,rateLimitProof:e.rateLimitProof,ephemeral:this.ephemeral};if(this.metaSetter){let e=this.metaSetter(i);return{...i,meta:e}}return i}}function x({pubsubTopic:e=i.nf,pubsubTopicShardInfo:t,contentTopic:n,symKey:r,sigPrivKey:o,ephemeral:c=!1,metaSetter:s}){return new k((0,a.IY)(n,e??t),n,r,o,c,s)}class E extends r.h{symKey;constructor(e,t,n){super(e,t),this.symKey=n}async fromProtoObj(e,t){let n;let r=t.payload;if(1!==t.version){A.error("Failed to decrypt due to incorrect version, expected:",1,", actual:",t.version);return}try{n=await T(r,this.symKey)}catch(e){A.error(`Failed to decrypt message using asymmetric decryption for contentTopic: ${this.contentTopic}`,e);return}if(!n){A.error(`Failed to decrypt payload for contentTopic ${this.contentTopic}`);return}let i=function(e){let t;let n=function(e){let t=new DataView(e.buffer);return 3&t.getUint8(0)}(e);if(0===n)return;let r=function(e,t){let n=e.slice(1,1+t);t<4&&(n=(0,u.zo)([n,new Uint8Array(4-t)],4));let r=new DataView(n.buffer);return r.getInt32(0,!0)}(e,n),i=1+n,o=e.slice(i,i+r),a=function(e){let t=new DataView(e.buffer);return(4&t.getUint8(0))==4}(e);if(a){let n=e.slice(e.length-65,e.length),r=a?w(e.slice(0,e.length-65)):w(e),i=function(e,t){let n=new DataView(t.slice(64).buffer),r=n.getUint8(0),i=s.Pc.fromCompact(t.slice(0,64));return s.LO(e,i,r,!1)}(r,n);t={signature:n,publicKey:i}}return{payload:o,sig:t}}(n);if(!i){A.error(`Failed to decode payload for contentTopic ${this.contentTopic}`);return}return A.info("Message decrypted",t),new v(e,t,i.payload,i.sig?.signature,i.sig?.publicKey)}}function I(e,t,n=i.nf){return new E((0,a.IY)(e,n),e,t)}}}]); |