UNPKG

wowok

Version:

Wowok Blockchain TypeScript API

1 lines 30.9 kB
import{createHash}from'crypto';import*as a184a from'fs';import*as a184b from'path';import{MessengerSession,DecryptionEngine}from'./session.js';import{MessengerServerClient}from'./server.js';import{MessageStorage,SessionStateStorage}from'./storage.js';import{hashPlaintext,verifyMessage,bytesToBase64}from'./crypto.js';import{canonicalizeJson}from'./utils.js';import{DEFAULT_MESSENGER_CONFIG,MessengerError,MessengerErrorCode,MessageDirection,MessageStatus,WTS_FILE_BYTES_LIMIT,NORMAL_MESSAGE_BYTES_LIMIT,CHAIN_PROOF_TYPE}from'./types.js';import{isValidWowAddress}from'../../utils/wow-types.js';async function getAccount(){const {Account:a}=await import('../local/account.js');return a['Instance']();}export class Messenger{['session'];['serverClient'];['config'];['userAddress']=null;['onMessageCallback']=null;['pollingTimer']=null;['messageConsecutiveEmptyPulls']=0x0;['prekeyConsecutiveOkCount']=0x0;['currentMessageInterval'];['isPollingRunning']=![];['messageStorage'];['sessionStateStorage'];['decryptionEngine'];['waitingMessages']=new Map();['failedMessages']=new Map();['waitingMessageRetries']=new Map();constructor(a,b){this['userAddress']=a,this['config']={...DEFAULT_MESSENGER_CONFIG,...b},this['session']=new MessengerSession(a,this['config']),this['serverClient']=new MessengerServerClient(this['config']),this['messageStorage']=new MessageStorage(a),this['sessionStateStorage']=new SessionStateStorage(a),this['decryptionEngine']=new DecryptionEngine(this['session']['store']),this['currentMessageInterval']=this['config']['message_poll_default_interval_ms']??0x6*0x3c*0x3e8;}['getUserAddress'](){if(!this['userAddress'])throw new MessengerError(MessengerErrorCode['IDENTITY_NOT_FOUND'],'User\x20address\x20not\x20set');return this['userAddress'];}async['getPublicKey'](){if(!this['userAddress'])throw new MessengerError(MessengerErrorCode['IDENTITY_NOT_FOUND'],'User\x20address\x20not\x20set');const a=await(await getAccount())['get'](this['userAddress'],![]);return a?.['pubkey']||'';}['isValidGuardMessage'](a,b){return Boolean(a&&b&&isValidWowAddress(a)&&isValidWowAddress(b));}async['generateSignatureParams'](a){if(!this['userAddress'])throw new MessengerError(MessengerErrorCode['IDENTITY_NOT_FOUND'],'User\x20address\x20not\x20set');const b=await(await getAccount())['get'](this['userAddress'],![]);if(!b?.['secret'])throw new MessengerError(MessengerErrorCode['IDENTITY_NOT_FOUND'],'Account\x20not\x20found\x20or\x20no\x20secret\x20key\x20for\x20'+this['userAddress']);const {Falcon512Keypair:c}=await import('../../keypairs/falcon512/index.js'),d=c['fromSecretKey'](b['secret']),e=d['getPublicKey'](),f=e['toWPublicKey'](),g=Date['now'](),h=this['generateNonce'](),i=a+':'+f+':'+g+':'+h,j=await d['sign'](new TextEncoder()['encode'](i));return{'signatureScheme':'Falcon512','signature':Buffer['from'](j)['toString']('base64'),'timestamp':g,'nonce':h};}['generateNonce'](){const a=new Uint8Array(0x10);return crypto['getRandomValues'](a),Array['from'](a,c=>c['toString'](0x10)['padStart'](0x2,'0'))['join']('');}async['initialize'](){if(!this['userAddress'])throw new MessengerError(MessengerErrorCode['IDENTITY_NOT_FOUND'],'User\x20address\x20not\x20set');await this['session']['ensureIdentity'](this['userAddress']),await this['session']['registerDevice'](this['userAddress']),await this['session']['ensurePreKeys'](this['userAddress']),await this['checkAndRefillPrekeys'](),this['startPollingTimer']();}async['checkAndRefillPrekeys'](){if(!this['userAddress'])return;await this['session']['ensurePreKeys'](this['userAddress'],![]);}['startPollingTimer'](){this['pollingTimer']&&clearTimeout(this['pollingTimer']),this['isPollingRunning']=!![],this['scheduleNextPoll']();}['stopPollingTimer'](){this['isPollingRunning']=![],this['pollingTimer']&&(clearTimeout(this['pollingTimer']),this['pollingTimer']=null);}['scheduleNextPoll'](){if(!this['isPollingRunning'])return;const a=this['getNextPollInterval']();this['pollingTimer']=setTimeout(()=>{this['poll']()['catch'](b=>{})['finally'](()=>{this['scheduleNextPoll']();});},a);}['getNextPollInterval'](){if(this['waitingMessages']['size']>0x0)return this['config']['message_poll_waiting_interval_ms']??0x3*0x3e8;return this['currentMessageInterval'];}async['poll'](){if(!this['userAddress'])return;try{const a=await this['pullMessages']();a['messages']['length']>0x0?(this['messageConsecutiveEmptyPulls']=0x0,this['currentMessageInterval']=this['config']['message_poll_fast_interval_ms']??0x6*0x3e8,this['onMessageCallback']&&this['onMessageCallback'](a['messages'])):(this['messageConsecutiveEmptyPulls']++,this['messageConsecutiveEmptyPulls']>=(this['config']['message_poll_consecutive_empty_limit']??0x3)&&(this['currentMessageInterval']=this['config']['message_poll_default_interval_ms']??0x6*0x3c*0x3e8,this['messageConsecutiveEmptyPulls']=0x0));if(a['prekey_status']){const b=a['prekey_status'];b['shouldRefill']?(this['prekeyConsecutiveOkCount']=0x0,await this['checkAndRefillPrekeys']()):(this['prekeyConsecutiveOkCount']++,this['prekeyConsecutiveOkCount']>=(this['config']['prekey_poll_consecutive_ok_limit']??0x3)&&(this['prekeyConsecutiveOkCount']=0x0));}}catch(c){}}['triggerFastPoll'](){this['currentMessageInterval']=this['config']['message_poll_fast_interval_ms']??0x6*0x3e8,this['messageConsecutiveEmptyPulls']=0x0,this['isPollingRunning']&&(this['pollingTimer']&&clearTimeout(this['pollingTimer']),this['scheduleNextPoll']());}async['sendMessage'](a,b,c){if(!this['userAddress'])throw new MessengerError(MessengerErrorCode['IDENTITY_NOT_FOUND'],'User\x20address\x20not\x20set');const d=Date['now'](),e=Date['now'](),f=this['generateNonce'](),g=new TextEncoder()['encode'](b)['length'];if(g>NORMAL_MESSAGE_BYTES_LIMIT)throw new MessengerError(MessengerErrorCode['INVALID_INPUT'],'Message\x20size\x20('+g+'\x20bytes)\x20exceeds\x20maximum\x20allowed\x20size\x20('+NORMAL_MESSAGE_BYTES_LIMIT+'\x20bytes).\x20Consider\x20using\x20sendZipFile\x20for\x20large\x20content.');await this['pullMessages']();const h=this['messageStorage']['getLastReceivedLeafIndex'](this['userAddress'],a),i=await this['session']['encryptMessage'](this['userAddress'],a,b),j=Buffer['from'](new Uint8Array(i['body']))['toString']('base64'),k=hashPlaintext(b,d,c?.['guardAddress'],c?.['passportAddress'],h),l=await(await getAccount())['get'](this['userAddress'],![]);if(!l)throw new MessengerError(MessengerErrorCode['IDENTITY_NOT_FOUND'],'User\x20account\x20not\x20found');const m=l['pubkey'],n=c?.['guardAddress']||'',o=c?.['passportAddress']||'',p=h>=0x0?h['toString']():'',q=c?.['force']?'true':'false',r='send_message:'+this['userAddress']+':'+a+':'+n+':'+o+':'+k+':'+p+':'+m+':'+e+':'+f+':'+q,s=await(await getAccount())['signData'](this['userAddress'],r),t=Buffer['from'](s['signature']['slice'](0x2),'hex'),u=bytesToBase64(new Uint8Array(t)),v=await this['serverClient']['sendMessage']({'sender':s['publicKey'],'recipient':a,'ciphertext':j,'plaintextHash':k,'clientTimestamp':d,'msgType':i['type'],'signatureScheme':'Falcon512','signature':u,'timestamp':e,'nonce':f,'guardAddress':c?.['guardAddress'],'passportAddress':c?.['passportAddress'],'force':c?.['force'],'lastReceivedLeafIndex':h>=0x0?h:undefined}),w={'messageId':v['messageId'],'fromAddress':this['userAddress'],'toAddress':a,'plaintextHash':k,'plaintext':b,'guardAddress':c?.['guardAddress'],'passportAddress':c?.['passportAddress'],'lastReceivedLeafIndex':h,'direction':MessageDirection['SENT'],'status':v['status']==='confirmed'?MessageStatus['CONFIRMED']:MessageStatus['PENDING'],'msgType':i['type'],'createdAt':d};v['merkleData']&&(w['leafIndex']=v['merkleData']['leafIndex'],w['prevRoot']=v['merkleData']['prevRoot'],w['newRoot']=v['merkleData']['newRoot'],w['serverSignature']=v['merkleData']['serverSignature'],w['serverTimestamp']=v['merkleData']['serverTimestamp'],w['serverPublicKey']=v['merkleData']['serverPublicKey'],this['sessionStateStorage']['updateSessionState'](this['userAddress'],a,{'currentRoot':v['merkleData']['newRoot'],'prevRoot':v['merkleData']['prevRoot'],'lastLeafIndex':v['merkleData']['leafIndex'],'lastSyncAt':Date['now']()}));this['messageStorage']['saveMessage'](w);if(v['pendingMerkleData'])for(const [x,y]of Object['entries'](v['pendingMerkleData'])){const z=this['messageStorage']['getMessage'](x);z&&z['status']===MessageStatus['PENDING']&&this['messageStorage']['updateMessageStatus'](x,MessageStatus['CONFIRMED'],{'leafIndex':y['leafIndex'],'newRoot':y['newRoot'],'serverSignature':y['serverSignature'],'serverTimestamp':y['serverTimestamp'],'serverPublicKey':y['serverPublicKey']});}return this['triggerFastPoll'](),{'messageId':v['messageId'],'status':w['status'],'merkleData':v['merkleData'],'guardList':v['guard_list'],'lastReceivedLeafIndex':h>=0x0?h:undefined};}async['pullMessages'](a){if(!this['userAddress'])throw new MessengerError(MessengerErrorCode['IDENTITY_NOT_FOUND'],'User\x20address\x20not\x20set');const b=Date['now'](),c=this['generateNonce'](),d='fetch_messages:'+this['userAddress']+':'+(a||0x64)+':'+b+':'+c,e=await(await getAccount())['get'](this['userAddress'],![]),f=e?.['pubkey']||'',g=await(await getAccount())['signData'](this['userAddress'],d),h=Buffer['from'](g['signature']['slice'](0x2),'hex'),i={'signatureScheme':'Falcon512','signature':bytesToBase64(h),'timestamp':b,'nonce':c},j=await this['serverClient']['pullMessages'](this['userAddress'],f,i,a);if(j['messages']['length']===0x0)return{'messages':[],'prekey_status':j['prekey_status']};const k=[],l=[],m=[...j['messages']]['sort'](function(n,o){return(n['clientTimestamp']||0x0)-(o['clientTimestamp']||0x0);});await this['processWaitingMessages'](k,l);for(const n of m){if(this['waitingMessages']['has'](n['id'])){l['push'](n['id']);continue;}if(this['failedMessages']['has'](n['id'])){l['push'](n['id']);continue;}const o=this['messageStorage']['getMessage'](n['id']);if(o){l['push'](n['id']);continue;}const p=await this['tryDecryptMessage'](n);if(p['success']&&p['message']&&p['decryptedData'])this['messageStorage']['saveMessage'](p['message']),k['push'](p['decryptedData']),l['push'](n['id']),this['waitingMessages']['delete'](n['id']);else p['shouldRetry']?this['waitingMessages']['set'](n['id'],n):(this['failedMessages']['set'](n['id'],{'message':n,'error':p['error']||'Unknown\x20error'}),await this['saveFailedMessage'](n,p['error']||'Unknown\x20error'),l['push'](n['id']));}this['waitingMessages']['size']>0x0&&this['isPollingRunning']&&this['reschedulePolling']();if(l['length']>0x0&&this['userAddress']){const q=Date['now'](),r=this['generateNonce'](),s=await(await getAccount())['get'](this['userAddress'],![]);if(s?.['pubkey']){const t='ack_messages:'+this['userAddress']+':'+l['join'](',')+':'+s['pubkey']+':'+q+':'+r,u=await(await getAccount())['signData'](this['userAddress'],t),v=Buffer['from'](u['signature']['slice'](0x2),'hex'),w=bytesToBase64(new Uint8Array(v));await this['serverClient']['acknowledgeMessages'](l,u['publicKey'],{'publicKey':u['publicKey'],'signatureScheme':'Falcon512','signature':w,'timestamp':q,'nonce':r});}}return{'messages':k,'prekey_status':j['prekey_status']};}['getLastSentLeafIndex'](a){if(!this['userAddress'])return-0x1;const b=this['messageStorage']['getMessagesBySession'](this['userAddress'],a),c=b['filter'](d=>d['direction']===MessageDirection['SENT']&&d['leafIndex']!==undefined);if(c['length']===0x0)return-0x1;return Math['max'](...c['map'](d=>d['leafIndex']));}['getSessionMessages'](a){if(!this['userAddress'])return[];return this['messageStorage']['getMessagesBySession'](this['userAddress'],a);}['getSessionState'](a){if(!this['userAddress'])return undefined;return this['sessionStateStorage']['getSessionState'](this['userAddress'],a);}async['submitChainProof'](a,b,c){if(!this['userAddress'])throw new MessengerError(MessengerErrorCode['IDENTITY_NOT_FOUND'],'User\x20address\x20not\x20set');const d=this['sessionStateStorage']['getSessionState'](this['userAddress'],b);if(!d||!d['currentRoot'])throw new MessengerError(MessengerErrorCode['SESSION_NOT_ESTABLISHED'],'No\x20session\x20state\x20found');const e=this['messageStorage']['getMessagesBySession'](this['userAddress'],b),f=e[e['length']-0x1];if(!f||!f['serverPublicKey']||!f['serverSignature'])throw new MessengerError(MessengerErrorCode['INVALID_PROOF'],'No\x20temporal\x20proof\x20(missing\x20server\x20public\x20key\x20or\x20signature),\x20cannot\x20submit\x20Proof\x20to\x20WoWok\x20blockchain');const g=f['serverPublicKey'],h=f['serverSignature'],i={'sessionId':d['sessionId'],'merkleRoot':d['currentRoot'],'messageCount':d['messageCount'],'timestamp':Date['now']()},{CallProof:j}=await import('../call/proof.js'),k=new j({'proof':JSON['stringify'](i),'server_pubkey':g,'server_signature':h,'proof_type':CHAIN_PROOF_TYPE,'description':c||'Messenger\x20session\x20proof\x20for\x20'+this['userAddress']+'\x20<->\x20'+b,'item_count':d['messageCount'],'about_address':d['sessionId']}),l=await k['call'](a),m=l?.['objectChanges'],n=m?.['find'](o=>o['objectType']?.['includes']('Proof'))?.['objectId'];return{'proofAddress':n||'','txHash':l?.['digest']||''};}['destroy'](){this['stopPollingTimer']();}['disconnect'](){this['destroy']();}['setOnMessageCallback'](a){this['onMessageCallback']=a;}async['sendZipFile'](a,b,c){if(!this['userAddress'])throw new MessengerError(MessengerErrorCode['IDENTITY_NOT_FOUND'],'User\x20address\x20not\x20set');const d=await this['loadFileData'](b),{ZipWriter:e,BlobWriter:f,Uint8ArrayReader:g}=await import('@zip.js/zip.js'),h=c?.['fileName']||a184b['basename'](b)||'file.zip',i=c?.['contentType']||this['detectContentType'](h),j=new f('application/zip'),k=new e(j);await k['add'](h,new g(d)),await k['close']();const l=await j['getData'](),m=new Uint8Array(await l['arrayBuffer']());if(m['length']>WTS_FILE_BYTES_LIMIT)throw new MessengerError(MessengerErrorCode['INVALID_INPUT'],'ZIP\x20file\x20size\x20('+m['length']+'\x20bytes)\x20exceeds\x20local\x20maximum\x20allowed\x20size\x20('+WTS_FILE_BYTES_LIMIT+'\x20bytes)');const n='0x'+createHash('sha256')['update'](m)['digest']('hex'),o=Buffer['from'](m)['toString']('base64'),p={'fileName':h+'.zip','fileSize':m['length'],'fileHash':n,'contentType':i};await this['pullMessages']();const q=this['messageStorage']['getLastReceivedLeafIndex'](this['userAddress'],a),r=Date['now'](),s=await this['session']['encryptMessage'](this['userAddress'],a,o),t=Buffer['from'](new Uint8Array(s['body']))['toString']('base64'),u=c?.['guardAddress']||'',v=c?.['passportAddress']||'',w=hashPlaintext(o,r,u,v,q),x=Date['now'](),y=this['generateNonce'](),z=await(await getAccount())['get'](this['userAddress'],![]);if(!z)throw new MessengerError(MessengerErrorCode['IDENTITY_NOT_FOUND'],'User\x20account\x20not\x20found');const A=z['pubkey'],B=q>=0x0?q['toString']():'',C=c?.['force']?'true':'false',D='send_message:'+this['userAddress']+':'+a+':'+u+':'+v+':'+w+':'+B+':'+A+':'+x+':'+y+':'+C,E=await(await getAccount())['signData'](this['userAddress'],D),F=Buffer['from'](E['signature']['slice'](0x2),'hex'),G=bytesToBase64(new Uint8Array(F)),H=await this['serverClient']['sendMessage']({'sender':E['publicKey'],'recipient':a,'ciphertext':t,'plaintextHash':w,'clientTimestamp':r,'msgType':s['type'],'zipMetadata':p,'guardAddress':c?.['guardAddress'],'passportAddress':c?.['passportAddress'],'force':c?.['force'],'lastReceivedLeafIndex':q>=0x0?q:undefined,'signatureScheme':'Falcon512','signature':G,'timestamp':x,'nonce':y}),I={'messageId':H['messageId'],'fromAddress':this['userAddress'],'toAddress':a,'plaintextHash':w,'plaintext':o,'guardAddress':c?.['guardAddress'],'passportAddress':c?.['passportAddress'],'lastReceivedLeafIndex':q,'direction':MessageDirection['SENT'],'status':H['status']==='confirmed'?MessageStatus['CONFIRMED']:MessageStatus['PENDING'],'msgType':s['type'],'zipMetadata':p,'createdAt':r};H['merkleData']&&(I['leafIndex']=H['merkleData']['leafIndex'],I['prevRoot']=H['merkleData']['prevRoot'],I['newRoot']=H['merkleData']['newRoot'],I['serverSignature']=H['merkleData']['serverSignature'],I['serverTimestamp']=H['merkleData']['serverTimestamp'],I['serverPublicKey']=H['merkleData']['serverPublicKey'],this['sessionStateStorage']['updateSessionState'](this['userAddress'],a,{'currentRoot':H['merkleData']['newRoot'],'prevRoot':H['merkleData']['prevRoot'],'lastLeafIndex':H['merkleData']['leafIndex'],'lastSyncAt':Date['now']()}));this['messageStorage']['saveMessage'](I);if(H['pendingMerkleData'])for(const [J,K]of Object['entries'](H['pendingMerkleData'])){const L=this['messageStorage']['getMessage'](J);L&&L['status']===MessageStatus['PENDING']&&this['messageStorage']['updateMessageStatus'](J,MessageStatus['CONFIRMED'],{'leafIndex':K['leafIndex'],'newRoot':K['newRoot'],'serverSignature':K['serverSignature'],'serverTimestamp':K['serverTimestamp'],'serverPublicKey':K['serverPublicKey']});}return this['triggerFastPoll'](),{'messageId':H['messageId'],'status':I['status'],'merkleData':H['merkleData'],'guardList':H['guard_list'],'lastReceivedLeafIndex':q>=0x0?q:undefined};}async['loadFileData'](a){if(a['startsWith']('http://')||a['startsWith']('https://')){const d=await fetch(a);if(!d['ok'])throw new MessengerError(MessengerErrorCode['NETWORK_ERROR'],'Failed\x20to\x20fetch\x20file\x20from\x20'+a+':\x20'+d['statusText']);const e=await d['arrayBuffer']();return new Uint8Array(e);}const b=a184b['resolve'](a);if(!a184a['existsSync'](b))throw new MessengerError(MessengerErrorCode['FILE_NOT_FOUND'],'File\x20not\x20found:\x20'+b);const c=a184a['readFileSync'](b);return new Uint8Array(c);}['detectContentType'](a){const b=a184b['extname'](a)['toLowerCase']();switch(b){case'.wts':return'wts';case'.wip':return'wip';default:return'zip';}}async['_createSignedRequest'](a){if(!this['userAddress'])throw new MessengerError(MessengerErrorCode['IDENTITY_NOT_FOUND'],'User\x20address\x20not\x20set');const b=Date['now'](),c=this['generateNonce'](),d=canonicalizeJson(a),e=b+':'+c+':'+d,f=await(await getAccount())['signData'](this['userAddress'],e),g=Buffer['from'](f['signature']['slice'](0x2),'hex'),h=bytesToBase64(new Uint8Array(g)),i=await(await getAccount())['get'](this['userAddress'],![]);if(!i||!i['pubkey'])throw new MessengerError(MessengerErrorCode['IDENTITY_NOT_FOUND'],'Account\x20public\x20key\x20not\x20found');return{'userAddress':this['userAddress'],'publicKey':i['pubkey'],'signatureScheme':'Falcon512','signature':h,'timestamp':b,'nonce':c,'data':a};}['_validateAddresses'](a){const b=[],c=[];for(const d of a){isValidWowAddress(d)?b['push'](d):c['push'](d);}return{'valid':b,'invalid':c};}async['addToBlacklist'](a){if(!this['userAddress'])throw new MessengerError(MessengerErrorCode['IDENTITY_NOT_FOUND'],'User\x20address\x20not\x20set');const {valid:b,invalid:c}=this['_validateAddresses'](a);if(c['length']>0x0)return{'success':![],'operation':'add','modifiedCount':0x0,'currentCount':0x0,'maxCount':0x0,'invalidAddresses':c,'message':'Invalid\x20addresses:\x20'+c['join'](',\x20')};const d={'addresses':b},e=await this['_createSignedRequest'](d);return this['serverClient']['addToBlacklist'](this['userAddress'],e);}async['removeFromBlacklist'](a){if(!this['userAddress'])throw new MessengerError(MessengerErrorCode['IDENTITY_NOT_FOUND'],'User\x20address\x20not\x20set');const {valid:b,invalid:c}=this['_validateAddresses'](a);if(c['length']>0x0)return{'success':![],'operation':'remove','modifiedCount':0x0,'currentCount':0x0,'maxCount':0x0,'invalidAddresses':c,'message':'Invalid\x20addresses:\x20'+c['join'](',\x20')};const d={'addresses':b},e=await this['_createSignedRequest'](d);return this['serverClient']['removeFromBlacklist'](this['userAddress'],e);}async['clearBlacklist'](){if(!this['userAddress'])throw new MessengerError(MessengerErrorCode['IDENTITY_NOT_FOUND'],'User\x20address\x20not\x20set');const a={},b=await this['_createSignedRequest'](a);return this['serverClient']['clearBlacklist'](this['userAddress'],b);}async['getBlacklist'](){if(!this['userAddress'])throw new MessengerError(MessengerErrorCode['IDENTITY_NOT_FOUND'],'User\x20address\x20not\x20set');const a=await this['_createSignedRequest']({});return this['serverClient']['getBlacklist'](this['userAddress'],a);}async['existInBlacklist'](a){if(!this['userAddress'])throw new MessengerError(MessengerErrorCode['IDENTITY_NOT_FOUND'],'User\x20address\x20not\x20set');const {valid:b,invalid:c}=this['_validateAddresses'](a);if(c['length']>0x0)return{'success':![],'operation':'exist','modifiedCount':0x0,'currentCount':0x0,'maxCount':0x0,'invalidAddresses':c,'message':'Invalid\x20addresses:\x20'+c['join'](',\x20')};const d={'addresses':b},e=await this['_createSignedRequest'](d);return this['serverClient']['existInBlacklist'](this['userAddress'],e);}async['addToFriendsList'](a){if(!this['userAddress'])throw new MessengerError(MessengerErrorCode['IDENTITY_NOT_FOUND'],'User\x20address\x20not\x20set');const {valid:b,invalid:c}=this['_validateAddresses'](a);if(c['length']>0x0)return{'success':![],'operation':'add','modifiedCount':0x0,'currentCount':0x0,'maxCount':0x0,'invalidAddresses':c,'message':'Invalid\x20addresses:\x20'+c['join'](',\x20')};const d={'addresses':b},e=await this['_createSignedRequest'](d);return this['serverClient']['addToFriendsList'](this['userAddress'],e);}async['removeFromFriendsList'](a){if(!this['userAddress'])throw new MessengerError(MessengerErrorCode['IDENTITY_NOT_FOUND'],'User\x20address\x20not\x20set');const {valid:b,invalid:c}=this['_validateAddresses'](a);if(c['length']>0x0)return{'success':![],'operation':'remove','modifiedCount':0x0,'currentCount':0x0,'maxCount':0x0,'invalidAddresses':c,'message':'Invalid\x20addresses:\x20'+c['join'](',\x20')};const d={'addresses':b},e=await this['_createSignedRequest'](d);return this['serverClient']['removeFromFriendsList'](this['userAddress'],e);}async['clearFriendsList'](){if(!this['userAddress'])throw new MessengerError(MessengerErrorCode['IDENTITY_NOT_FOUND'],'User\x20address\x20not\x20set');const a={},b=await this['_createSignedRequest'](a);return this['serverClient']['clearFriendsList'](this['userAddress'],b);}async['getFriendsList'](){if(!this['userAddress'])throw new MessengerError(MessengerErrorCode['IDENTITY_NOT_FOUND'],'User\x20address\x20not\x20set');const a=await this['_createSignedRequest']({});return this['serverClient']['getFriendsList'](this['userAddress'],a);}async['existInFriendsList'](a){if(!this['userAddress'])throw new MessengerError(MessengerErrorCode['IDENTITY_NOT_FOUND'],'User\x20address\x20not\x20set');const {valid:b,invalid:c}=this['_validateAddresses'](a);if(c['length']>0x0)return{'success':![],'operation':'exist','modifiedCount':0x0,'currentCount':0x0,'maxCount':0x0,'invalidAddresses':c,'message':'Invalid\x20addresses:\x20'+c['join'](',\x20')};const d={'addresses':b},e=await this['_createSignedRequest'](d);return this['serverClient']['existInFriendsList'](this['userAddress'],e);}async['addToGuardList'](a){if(!this['userAddress'])throw new MessengerError(MessengerErrorCode['IDENTITY_NOT_FOUND'],'User\x20address\x20not\x20set');const b={'guards':a},c=await this['_createSignedRequest'](b);return this['serverClient']['addToGuardList'](this['userAddress'],c);}async['removeFromGuardList'](a){if(!this['userAddress'])throw new MessengerError(MessengerErrorCode['IDENTITY_NOT_FOUND'],'User\x20address\x20not\x20set');const {valid:b,invalid:c}=this['_validateAddresses'](a);if(c['length']>0x0)return{'success':![],'operation':'remove','modifiedCount':0x0,'currentCount':0x0,'maxCount':0x0,'invalidAddresses':c,'message':'Invalid\x20addresses:\x20'+c['join'](',\x20')};const d={'addresses':b},e=await this['_createSignedRequest'](d);return this['serverClient']['removeFromGuardList'](this['userAddress'],e);}async['getGuardList'](){if(!this['userAddress'])throw new MessengerError(MessengerErrorCode['IDENTITY_NOT_FOUND'],'User\x20address\x20not\x20set');const a=await this['_createSignedRequest']({});return this['serverClient']['getGuardList'](this['userAddress'],a);}async['getSettings'](){if(!this['userAddress'])throw new MessengerError(MessengerErrorCode['IDENTITY_NOT_FOUND'],'User\x20address\x20not\x20set');const a=await this['_createSignedRequest']({});return this['serverClient']['getSettings'](this['userAddress'],a);}async['setSettings'](a){if(!this['userAddress'])throw new MessengerError(MessengerErrorCode['IDENTITY_NOT_FOUND'],'User\x20address\x20not\x20set');const b={'allowStrangerMessages':a['allowStrangerMessages'],'maxInboxSize':a['maxInboxSize']},c=await this['_createSignedRequest'](b);return this['serverClient']['updateSettings'](this['userAddress'],c);}async['tryDecryptMessage'](a){try{const b=Uint8Array['from'](Buffer['from'](a['bodyB64'],'base64')),c=await this['decryptionEngine']['decryptMessage'](this['userAddress'],a['from'],b['buffer'],a['msgType']);if(!c['success']){const h=this['isRetryableError'](c['error']||'');return{'success':![],'shouldRetry':h,'error':c['error'],'sessionUpdated':c['sessionUpdated']};}const d=c['plaintext'];if(!a['clientTimestamp'])throw new Error('Missing\x20clientTimestamp\x20for\x20message\x20'+a['id']);if(!a['plaintextHash'])throw new Error('Missing\x20plaintextHash\x20for\x20message\x20'+a['id']);const e=verifyMessage({'messageId':a['id'],'plaintext':d,'plaintextHash':a['plaintextHash'],'createdAt':a['clientTimestamp'],'guardAddress':a['guardAddress'],'passportAddress':a['passportAddress'],'lastReceivedLeafIndex':a['lastReceivedLeafIndex'],'serverSignature':a['merkleMetadata']?.['serverSignature'],'serverPublicKey':a['merkleMetadata']?.['serverPublicKey'],'merkleMetadata':a['merkleMetadata']?{'prevRoot':a['merkleMetadata']['prevRoot'],'newRoot':a['merkleMetadata']['newRoot'],'serverTimestamp':a['merkleMetadata']['serverTimestamp'],'leafIndex':a['merkleMetadata']['leafIndex'],'proofSiblings':a['merkleMetadata']['proofSiblings'],'proofIndices':a['merkleMetadata']['proofIndices']}:undefined});if(!e['valid'])throw new Error(e['error']);const f={'messageId':a['id'],'fromAddress':a['from'],'toAddress':this['userAddress'],'plaintextHash':a['plaintextHash'],'plaintext':d,'guardAddress':a['guardAddress'],'passportAddress':a['passportAddress'],'lastReceivedLeafIndex':a['lastReceivedLeafIndex'],'direction':MessageDirection['RECEIVED'],'status':MessageStatus['DECRYPTED'],'msgType':a['msgType'],'leafIndex':a['merkleMetadata']?.['leafIndex'],'prevRoot':a['merkleMetadata']?.['prevRoot'],'newRoot':a['merkleMetadata']?.['newRoot'],'serverSignature':a['merkleMetadata']?.['serverSignature'],'serverTimestamp':a['merkleMetadata']?.['serverTimestamp'],'serverPublicKey':a['merkleMetadata']?.['serverPublicKey'],'createdAt':a['clientTimestamp'],'receivedAt':Date['now'](),'zipMetadata':a['zipMetadata']};a['merkleMetadata']&&this['sessionStateStorage']['updateSessionState'](this['userAddress'],a['from'],{'currentRoot':a['merkleMetadata']['newRoot'],'prevRoot':a['merkleMetadata']['prevRoot'],'lastLeafIndex':a['merkleMetadata']['leafIndex'],'lastSyncAt':Date['now']()});const g=a['merkleMetadata']?.['serverTimestamp']||a['clientTimestamp']||Date['now']();if(a['lastReceivedLeafIndex']!==undefined&&a['lastReceivedLeafIndex']>=0x0){const i=this['messageStorage']['getMessagesBySession'](this['userAddress'],a['from'])['filter'](j=>j['direction']===MessageDirection['SENT']&&j['status']===MessageStatus['CONFIRMED']&&j['leafIndex']!==undefined&&j['leafIndex']<=a['lastReceivedLeafIndex']);for(const j of i){this['messageStorage']['updateMessageStatus'](j['messageId'],MessageStatus['READ']);}}return{'success':!![],'shouldRetry':![],'message':f,'decryptedData':{'id':a['id'],'from':a['from'],'plaintext':d,'timestamp':g,'merkleVerified':!!a['merkleMetadata'],'merkleData':a['merkleMetadata']?{'leafIndex':a['merkleMetadata']['leafIndex'],'rootHash':a['merkleMetadata']['newRoot']}:undefined},'sessionUpdated':c['sessionUpdated']};}catch(k){const l=k instanceof Error?k['message']:String(k),m=this['isRetryableError'](l);return{'success':![],'shouldRetry':m,'error':l};}}['isRetryableError'](a){const b=[/message number/i,/chain key/i,/session not found/i,/prekey not found/i,/PREKEY 竞争:我的地址较小,保留我的会话/i,/The operation failed for an operation-specific reason/i,/DOMException/i,/收到 WHISPER_MESSAGE 但无现有会话,需要等待 PREKEY_MESSAGE/i];return b['some'](c=>c['test'](a));}async['processWaitingMessages'](a,b){if(this['waitingMessages']['size']===0x0)return;const c=[],d=Array['from'](this['waitingMessages']['values']())['sort'](function(e,f){return(e['clientTimestamp']||0x0)-(f['clientTimestamp']||0x0);});for(const e of d){const f=e['id'],g=await this['tryDecryptMessage'](e);if(g['success']&&g['message']&&g['decryptedData'])this['messageStorage']['saveMessage'](g['message']),a['push'](g['decryptedData']),b['push'](f),c['push'](f),this['waitingMessageRetries']['delete'](f);else{if(!g['shouldRetry'])this['failedMessages']['set'](f,{'message':e,'error':g['error']||'Unknown\x20error'}),await this['saveFailedMessage'](e,g['error']||'Unknown\x20error'),b['push'](f),c['push'](f),this['waitingMessageRetries']['delete'](f);else{const h=this['waitingMessageRetries']['get'](f)||0x0,i=h+0x1;i>=0x3?(this['failedMessages']['set'](f,{'message':e,'error':g['error']||'Retry\x20limit\x20exceeded'}),await this['saveFailedMessage'](e,g['error']||'Retry\x20limit\x20exceeded'),b['push'](f),c['push'](f),this['waitingMessageRetries']['delete'](f)):this['waitingMessageRetries']['set'](f,i);}}}for(const j of c){this['waitingMessages']['delete'](j);}if(c['length']>0x0){}}async['saveFailedMessage'](a,b){const c={'messageId':a['id'],'fromAddress':a['from'],'toAddress':this['userAddress'],'plaintextHash':a['plaintextHash']||'','guardAddress':a['guardAddress'],'passportAddress':a['passportAddress'],'lastReceivedLeafIndex':a['lastReceivedLeafIndex'],'direction':MessageDirection['RECEIVED'],'status':MessageStatus['DECRYPT_FAILED'],'msgType':a['msgType'],'leafIndex':a['merkleMetadata']?.['leafIndex'],'prevRoot':a['merkleMetadata']?.['prevRoot'],'newRoot':a['merkleMetadata']?.['newRoot'],'serverSignature':a['merkleMetadata']?.['serverSignature'],'serverTimestamp':a['merkleMetadata']?.['serverTimestamp'],'serverPublicKey':a['merkleMetadata']?.['serverPublicKey'],'createdAt':a['clientTimestamp']||Date['now'](),'receivedAt':Date['now'](),'zipMetadata':a['zipMetadata'],'decryptError':b,'decryptAttempts':0x1,'lastDecryptAttemptAt':Date['now']()};this['messageStorage']['saveMessage'](c);}['reschedulePolling'](){if(!this['isPollingRunning']||!this['pollingTimer'])return;clearTimeout(this['pollingTimer']),this['pollingTimer']=null,this['scheduleNextPoll']();}['getFailedMessages'](){return Array['from'](this['failedMessages']['entries']())['map'](([a,b])=>({'messageId':a,'from':b['message']['from'],'error':b['error'],'leafIndex':b['message']['merkleMetadata']?.['leafIndex']}));}['clearFailedMessages'](a){a?this['failedMessages']['delete'](a):this['failedMessages']['clear']();}['getWaitingMessageCount'](){return this['waitingMessages']['size'];}}