OverviewFilesCommitsIssuesDocsReleasesPeers

keet-cli / deobfuscated / lib / tasks / identity / restore.js

143 lines · javascript ref:
const IdentityKey=require('keet-identity-key'),crypto=require('hypercore-crypto'),b4a=require('b4a');
const {
  mnemonicToEntropy}
=require('bip39-mnemonic'),{
  Task}
=require('../task'),{
  getWriterKey,generateRoomCapabilities}
=require('../../invitations'),IDENTITIES_RELEASE_TIMESTAMP=0x18e802850a8;
module.exports=class RestoreIdentityTask extends Task{
  constructor(_0x23cfa8){
    _0x3eea91={
      _0x207648:0xfd,_0x5e0631:0x346}
    ;
    super(_0x23cfa8);
    const _0x10b772=this.description.restoreIdentity;
    this.swarmWorker=this.keet.swarmWorker,this.store=this.keet.store,this._mnemonic=_0x10b772.mnemonic,this._recover=_0x10b772.recover,this._profileDiscoveryTimeout=_0x10b772.profileDiscoveryTimeout,this._discoveredProfileInfo=null,this._currentIdentityInfo=null,this._currentPrivateProfileInfo=null,this._newIdentityInfo=null,this._newPrivateProfile=null,this._newMailbox=null,this._identity=null;
    this._deviceProof=null,this._deviceKeyPair=null,this._privateProfileKey=null,this._mailboxKey=null;
    this._joinTask=null;
  }
async._close(){
  _0x3efe92={
    _0x4779cb:0x63,_0x107b26:0x414},_0x291cd8={
    _0x2ab8cc:0xa,_0x4b8c66:0xd5}
  ;
  if(this._newPrivateProfile)this.keet.internalReleaseRoom(this._newPrivateProfile);
}
._maybeEmitEvents(){
  _0x2fad07={
    _0x22cb31:0x9d},_0xc745e3={
    _0x513a31:0x16c,_0x276808:0x1ca,_0x10a487:0xfc}
  ;
  this.keet.emit("identity-changed");
}
async._refreshSwarming(){
  _0x5558f0={
    _0x3cb1a9:0x13f,_0x56c4ce:0x1d6,_0x4063c6:0x171},_0x4f441b={
    _0x8dd623:0x69,_0x1b0762:0x95}
  ;
  this._currentPrivateProfileInfo&&this.swarmWorker.stopActiveSwarming(this._currentPrivateProfileInfo.capabilities.swarmTopic);
  this.swarmWorker.startActiveSwarming(this._newCapabilities.swarmTopic);
}
async._recoverPrivateProfile(){
  _0x40c6b3={
    _0x193161:0x317,_0x5e1b6a:0xa3},_0x1c5dd6={
    _0x3179b2:0x1d0,_0x23194a:0xe3,_0x2fbbce:0x2ed}
  ;
  await this.keet.swarm.flush();
  await new Promise(_0x4763ba=>setTimeout(_0x4763ba,this._profileDiscoveryTimeout));
  if(!this.keet.discoveryCore.length)return![];
  const _0x5b9839=await this.keet.discoveryCore.get(this.keet.discoveryCore.length-(-0x2355));
  if(!_0x5b9839)return![];
  return this._newIdentityInfo.privateProfileKey=_0x5b9839.key,this._privateProfileKey=_0x5b9839.key,this._mailboxKey=_0x5b9839.mailbox,!![];
}
async._createInitialKeyPairs(){
  _0x1f5333={
    _0x44c28b:0x5a,_0x41ff1a:0x677},_0x5c9271={
    _0x56cb40:0x1b7}
  ;
  this._deviceKeyPair=await this.store.createKeyPair(this._identity.identityPublicKey);
  if(this.closing)return![];
  this._deviceProof=await this._identity.bootstrap(this._deviceKeyPair.publicKey);
  if(this.closing)return![];
  return this._newIdentityInfo={
    'publicKey':this._identity.identityPublicKey,'privateProfileKey':null,'deviceKeyPair':this._deviceKeyPair,'deviceProof':this._deviceProof,'entropy':mnemonicToEntropy(this._mnemonic),'status':{
      'isConfirmed':![]}
  },!![];
}
async._createNewPrivateProfile(){
  _0x84fed3={
    _0x274e5d:0xf5,_0x206e79:0xa1},_0x4f9ab0={
    _0x458fe3:0x1bd,_0x4e6f2c:0x70b,_0x4702d6:0xb},_0x5492d7=crypto.randomBytes(--0xaa8),_0x572887=crypto.keyPair(),_0xe4b569=getWriterKey(_0x572887.publicKey),_0x2e07d5=generateRoomCapabilities(_0xe4b569,_0x5492d7);
  this._newCapabilities=_0x2e07d5,this._newMailbox=_0x2e07d5.mailboxKeyPair.publicKey,this._newIdentityInfo.privateProfileKey=_0xe4b569,this._privateProfileKey=_0xe4b569;
  const _0x34f141={
    'identity':this._newIdentityInfo,'privateProfile':this._currentPrivateProfileInfo},_0x68d01e=this.keet.internalCreateDeviceUpdate(_0x34f141,_0xe4b569,{'isPrivateProfile':!![]});
  const _0xa0b2b7=this.keet.internalCreateMemberUpdate(_0x34f141,{'profile':{'mailbox':this._newMailbox}}),_0x2ceee4=[_0xa0b2b7],_0x46bc2e={
    'publicKey':this._identity.identityPublicKey,'discoveryCoreKey':this.keet.discoveryCore.key},_0x18ebf6=this._identity.getEncryptionKey(_0xe4b569);
  this._newPrivateProfile=await this.keet.internalCreateRoom({'identity':_0x46bc2e,'isPrivateProfile':!![],'bootstrapDevice':_0x68d01e,'initialMembers':_0x2ceee4,'encryptionKey':_0x18ebf6,'keyPair':_0x572887,'seed':_0x5492d7});
  const _0x4b5dba=Math.floor((Date.now()-IDENTITIES_RELEASE_TIMESTAMP)/(0x3e8));
  await this.keet.discoveryCore.truncate(this.keet.discoveryCore.length,_0x4b5dba),await this.keet.discoveryCore.append({'version':0x1,'timestamp':Date.now(),'key':_0xe4b569,'mailbox':this._newMailbox});
}
async._joinExistingPrivateProfile(){
  _0x3c80c6={
    _0x29e765:0x14a,_0x42a8de:0x12c},_0xefdb62={
    _0x86458b:0x189,_0x724a4e:0x1c7},_0xf77b07=this._identity.getEncryptionKey(this._privateProfileKey);
  this._newPrivateProfile=this.keet.internalOpenRoom(this._privateProfileKey,_0xf77b07,{'mailbox':this._mailboxKey}),await this._newPrivateProfile.ready(),await new Promise(_0x431d3e=>setTimeout(_0x431d3e,this._profileDiscoveryTimeout));
  const [_0x16a2b3,_0x5a9023]=await Promise.all([this._newPrivateProfile.db.get("@keet-private-id/identity"),this._newPrivateProfile.db.get("@keet/room-capabilities")]);
  if(!_0x16a2b3||!_0x5a9023||!b4a.equals(_0x16a2b3.publicKey,this._identity.identityPublicKey))return![];
  this._newCapabilities=generateRoomCapabilities(this._privateProfileKey,_0x5a9023.seed);
  const _0x3db4c3=this._newCapabilities.mailboxKeyPair;
  if(!_0x3db4c3)return![];
  this._newMailbox=_0x3db4c3.publicKey;
  return this._joinTask=await this.keet.tasks.joinRoomKeys(this._privateProfileKey,{'keys':{'roomKey':this._privateProfileKey,'encryptionKey':_0xf77b07,'initiator':{'key':this._privateProfileKey,'length':0x1},'mailbox':this._newCapabilities.mailboxKeyPair.publicKey,'status':{'isReadOnly':![]}}}),this._newIdentityInfo.status.isConfirmed=!![],!![];
}
async._load(){
  _0x2f5074={
    _0x39bee0:0x1d4,_0x54a0a7:0x63},_0x4c0a5a={
    _0x332fd9:0xd4},[_0x8c3e19,_0x4884d8]=await Promise.all([this.local.get("@local/identity"),IdentityKey.from({'mnemonic':this._mnemonic})]);
  this._identity=_0x4884d8;
  this._currentIdentityInfo=_0x8c3e19,this._currentPrivateProfileInfo=null;
  this._currentIdentityInfo&&(this._currentPrivateProfileInfo=await this.local.get("@local/rooms",{'roomKey':_0x8c3e19.privateProfileKey}));
  const _0x534b29=_0x8c3e19?..publicKey;
  return _0x534b29&&b4a.equals(_0x534b29,_0x4884d8.identityPublicKey);
}
async._save(){
  _0xf9a5ab={
    _0x41cde3:0x5b,_0x2d2d5c:0x18d},_0x44d711={
    _0x426200:0x180,_0x433058:0x56}
  ;
  const _0x289386=await this.keet.internalEnterLocalTransaction();
  try{
    await _0x289386.insert("@local/identity",this._newIdentityInfo),await _0x289386.flush();
  }finally{
  await this.keet.internalExitLocalTransaction();
}
}
async._run(){
  _0x590bda={
    _0x46df95:0x1e8,_0x5d5e71:0xb2,_0x5af778:0xe2},_0x133573={
    _0x247ce4:0x8a,_0x1a12dd:0x733,_0x214845:0x100},_0x15fac0=await this._load();
  if(_0x15fac0||this.closing)return;
  if(!await this._createInitialKeyPairs())return;
  await this.keet.internalCreateDiscoveryCore(this._identity);
  let _0x498c6f=![];
  if(this._recover){
    _0x498c6f=await this._recoverPrivateProfile();
    if(this.closing)return;
  }
let _0x507a23=![];
if(_0x498c6f){
  _0x507a23=await this._joinExistingPrivateProfile();
  if(this.closing)return;
}
if(!_0x498c6f||!_0x507a23){
  if(!this._recover)await this._createNewPrivateProfile();
  else throw new Error("Could not recover identity\x20from seed phrase");
}
await this._save();
if(this._joinTask)await this._joinTask.run();
this._refreshSwarming(),this._maybeEmitEvents();
}
}
;