const KeetRegistryRpcClient=require('@holepunchto/keet-registry-client'),ReadyResource=require('ready-resource');
const KeetRegistry=require('@holepunchto/keet-registry'),b4a=require('b4a');
const {
createUsernameReservationRequest}
=require('../invitations'),{
Readable}
=require('streamx'),KeetRegistryError=require('@holepunchto/keet-registry/lib/errors');
class UserRegistry extends ReadyResource{
constructor(_0x2408a1,{db:_0x21937c,reserved:_0x551668,blobs:_0x14d63b,rpc:_0x975952,rateLimit:rateLimit={}}={}){
_0x2544ae={
_0xa408ab:0x2cf}
;
super(),this.dbKey=_0x21937c,this.reservedKey=_0x551668,this.blobsKey=_0x14d63b,this.rpcKeys=_0x975952;
this.keet=_0x2408a1,this.store=null,this.swarm=null,this.swarmWorker=null,this.db=null,this.reserved=null,this.blobs=null,this._keetRegistry=null,this._rpcClient=null,this._registryRpcOpts={
'rateLimit':rateLimit}
;
this._subs=[],this._watchingReservation=![],this._watchingRecord=![],this._boundEmit=this._emit.bind(this),this._boundCatch=this._catch.bind(this),this._boundLookupOwn=null;
}
async._open(){
_0x12ac5d={
_0x3b1a42:0x7e}
;
this.store=this.keet.store.session(),this.swarmWorker=this.keet.swarmWorker,this.db=this.store.get({'key':this.dbKey}),this.reserved=this.store.get({'key':this.reservedKey}),this.blobs=this.store.get({'key':this.blobsKey}),this._keetRegistry=new KeetRegistry(this.db,this.reserved,this.blobs,{'extension':![]}),this._rpcClient=new KeetRegistryRpcClient(this.rpcKeys,this.keet.rpcClient,this._registryRpcOpts),await this._keetRegistry.ready();
const _0x42290f=await this._getIdentity();
this._boundLookupOwn=this._safeLookupOwn.bind(this,_0x42290f);
this._lookupOwn(_0x42290f).catch(this.keet.onerror),this._keetRegistry.reserved.core.once('append',this._boundLookupOwn);
}
async._emit(_0x4a5abd,_0x5bfe13=null,_0x21332c=null){
_0x3ccee8={
_0x5157e6:0x28d}
;
for(const _0x504229 of this._subs){
_0x504229.push({'status':fullStatus(_0x4a5abd),'reservation':_0x5bfe13,'record':this._encode(_0x21332c)});
}
}
async._catch(_0x3985b6){
_0x41b979={
_0x28a2fa:0xc7,_0x52a363:0xf5},_0x38bda4={
_0x189fe5:0x12e,_0xe2e4c4:0xf,_0x5bba31:0x1c0}
;
this._unwatch();
for(const _0x458f9d of this._subs){
_0x458f9d.push({'status':fullStatus({'hasError':!![]}),'error':_0x3985b6.code??_0x3985b6.message});
}
}
async._close(){
_0x364564={
_0x1ccdc4:0x124,_0x231d0b:0xb4},_0x418a20={
_0x5c12df:0x36,_0x5c004f:0x131,_0x12965b:0x197}
;
this._unwatch(),this._rpcClient.destroy(),await this._keetRegistry.close(),await this.store.close();
}
async._getIdentity(){
_0x5049ae={
_0x589e00:0x255,_0x27097b:0x1bf,_0x10dce6:0xb5},_0x462487={
_0x3c8f93:0xc3,_0x2c8295:0x4b,_0x1154e2:0xb3}
;
const _0x5e3915=await this.keet.local.get("@local/identity");
if(!_0x5e3915)throw new Error("Identity\x20is\x20required");
return _0x5e3915;
}
._encode(_0x625ef3){
_0x3fa4fb={
_0x371160:0x3a7,_0x55c70f:0x15f,_0x110c99:0x13a},_0x275239={
_0x2c8ce8:0xd3,_0x5f3f70:0x5c,_0x2e2021:0x11c}
;
if(!_0x625ef3)return null;
return{
..._0x625ef3,'contactInfo':this.keet.internalEncodeContactInfo(_0x625ef3)}
;
}
async._safeLookupOwn(_0x481ae6){
_0x3eed65={
_0x1d48ab:0x18c,_0x4bb6d3:0xdd},_0x5151d4={
_0x528945:0xf0,_0x355cc3:0x122}
;
try{
return await this._lookupOwn(_0x481ae6);
}catch(_0x9621d2){
this.keet.onerror(_0x9621d2);
}
}
async._lookupOwn(_0x35aca9){
_0x4ea195={
_0x4d5e28:0x31e,_0x2bf03a:0xc5,_0x2d334a:0x100},_0x2e379b={
_0x194d67:0x1e,_0x5dd97a:0x1f4},[_0x57a316,_0x56aa40]=await Promise.all([this._keetRegistry.getByKey(_0x35aca9.publicKey),this._keetRegistry.getReservationsByKey(_0x35aca9.publicKey)]);
if(!_0x57a316){
if(_0x56aa40.length&&!this._watchingRecord){
const _0x2a1030=_0x56aa40.pop();
this._boundEmit({'isReserved':!![]},{'username':_0x2a1030.username}),this._claimReservation(_0x2a1030);
}
return null;
}
return this._encode(_0x57a316);
}
async.lookup(_0x5ade1a){
_0x42f284={
_0x38f3fa:0x188,_0x5a56e8:0xa7,_0x2e8ea3:0xce},_0x3ccc06={
_0x3c3212:0x1c8}
;
if(!_0x5ade1a)throw new Error("Username required");
if(!this.opened)await this.ready();
this.swarmWorker.startDiscoverySwarming(this.db.discoveryKey,{'deferSwarming':![],'limit':0x1});
const _0x4b44ea=await this._keetRegistry.get(_0x5ade1a);
return this._encode(_0x4b44ea);
}
async.lookupByKey(_0x2be35a){
_0x47bb08={
_0x29bcea:0x312,_0x54f625:0x62,_0x46a1e9:0x12b},_0x52e52f={
_0x33fbe2:0x1a0,_0x17cbf2:0xdc,_0x1e349e:0x43b}
;
if(!_0x2be35a)throw new Error("Public key required");
if(!this.opened)await this.ready();
this.swarmWorker.startDiscoverySwarming(this.db.discoveryKey,{'deferSwarming':![],'limit':0x1});
const _0x15db19=await this._getIdentity();
if(b4a.equals(_0x15db19.publicKey,_0x2be35a))return this._lookupOwn(_0x15db19);
const _0x4f4ff8=await this._keetRegistry.getByKey(_0x2be35a);
return this._encode(_0x4f4ff8);
}
async.search(_0x382176,_0x3f9362){
_0xa7d871={
_0x3b172a:0x11a},_0x349dc3={
_0x58b994:0x25a,_0x58623e:0x199}
;
if(!_0x382176)return[];
if(!this.opened)await this.ready();
this.swarmWorker.startDiscoverySwarming(this.db.discoveryKey,{'deferSwarming':![],'limit':0x1});
const _0x59dcf3=[];
for await(const _0x3ad822 of this._keetRegistry.search(_0x382176,_0x3f9362)){
_0x59dcf3.push(this._encode(_0x3ad822));
}
return _0x59dcf3;
}
async.isAvailable(_0x15ab9f){
_0x540a26={
_0x2a6f76:0x164},_0x159940={
_0x35bda9:0xfa,_0x6d3c7b:0x187}
;
if(!_0x15ab9f)throw new Error("Username\x20required");
if(!this.opened)await this.ready();
return this.swarmWorker.startDiscoverySwarming(this.db.discoveryKey,{'deferSwarming':![],'limit':0x1}),this._keetRegistry.isAvailable(_0x15ab9f);
}
async.getReservationsByKey(_0x8803b4){
_0x59492f={
_0xb55557:0x1be},_0x37aea5={
_0x1785b6:0x7d,_0x160e80:0x63}
;
if(!_0x8803b4)throw new Error("Public key required");
if(!this.opened)await this.ready();
this.swarmWorker.startDiscoverySwarming(this.db.discoveryKey,{'deferSwarming':![],'limit':0x1});
return this._keetRegistry.getReservationsByKey(_0x8803b4);
}
async.internalUpdateRegistryMember(_0x16e907){
_0x24cbb6={
_0x44e4d2:0x9f},_0x38d9a8={
_0x713ea9:0x1c,_0x139653:0x3e4}
;
try{
await KeetRegistry.verifyPut(this._keetRegistry,_0x16e907),await this._rpcClient.putRecord(_0x16e907);
}catch(_0xe56896){
this._boundCatch(_0xe56896);
}
}
async._pushOwnRecord(){
_0x357021={
_0x175260:0x10e,_0x2cdd38:0x61,_0x3095e5:0x1f0},_0x2b5944={
_0x10b1a0:0x101,_0x36f433:0x1c,_0x23c40f:0x4c}
;
const {
publicKey:_0x32a888}
=await this._getIdentity(),_0x4b117f=await this._keetRegistry.getByKey(_0x32a888);
if(_0x4b117f)this._boundEmit({'hasRecord':!![]},null,_0x4b117f);
}
.subscribeUsername(){
_0x2e4ecd={
_0x27306e:0x3ae},_0x50373e={
_0x3c3c93:0x43a,_0x5ca635:0x3e5,_0x16cb77:0x3e0,_0x43bdce:0x3f7,_0x3342df:0x410,_0x7339e0:0x158,_0x1229b8:0x151,_0x2f675d:0x406,_0x4080d2:0x3f8,_0x1a3708:0x402,_0x3da9a2:0x3b0,_0x3f947d:0x3c0,_0x2652dd:0x3da,_0x2dcd05:0x3c5},_0xe33b01={
_0x195173:0x266},_0x2f187e=this;
return new Readable({'open'(_0x2a0041){_0x2f187e._subs.push(this),_0x2f187e._pushOwnRecord().catch(_0x2f187e.keet.onerror),_0x2a0041(null);},'destroy'(_0x224042){_0x5db750=_0x2f187e._subs.indexOf(this);if(_0x5db750!==-(0x1))_0x2f187e._subs.splice(_0x5db750,0x1);_0x224042(null);}});
}
async.update(_0x46fc49){
_0x3271f6={
_0xb95eed:0x16,_0xbcadd1:0x75},_0xbd75b7={
_0x28256a:0xca,_0xf65160:0x15e}
;
if(!_0x46fc49)throw new Error("Username required");
if(!this.opened)await this.ready();
if(this._watchingReservation)throw new Error("Registration already in progress");
const {
publicKey:_0xdf82f4,deviceKeyPair:_0xf1e04b,deviceProof:_0x54ea9e}
=await this._getIdentity();
this.swarmWorker.startDiscoverySwarming(this.db.discoveryKey,{'deferSwarming':![],'limit':0x1}),this._watchReservation(_0x46fc49,_0xdf82f4);
try{
this._boundEmit({'isReserving':!![]},{'username':_0x46fc49});
const _0x47eac4=createUsernameReservationRequest(_0x46fc49,{'publicKey':_0xdf82f4,'deviceKeyPair':_0xf1e04b,'deviceProof':_0x54ea9e});
await KeetRegistry.verifyReservation(_0x47eac4),await this._rpcClient.reserve(_0x47eac4);
}catch(_0x5091a){
return this._boundCatch(_0x5091a),![];
}
return!![];
}
._unwatch(){
_0x2e5b31={
_0x2bb3fc:0x336,_0x27dcbd:0x4f,_0xf15494:0x1b9},_0xbf2c9={
_0x35f28d:0x2d8,_0x315d70:0xda}
;
this._keetRegistry.unwatch(),this._watchingRecord=![];
this._watchingReservation=![];
}
async._claimReservation({username:_0xb0aad0,memberKey:_0x36c517}){
_0x548482={
_0x3fadb0:0x13d,_0x2bd366:0x1e5,_0x1e5ee1:0xf9},_0xc8f2ca={
_0x24b444:0x4ca,_0x14d8e5:0x1d4}
;
this._keetRegistry.reserved.core.removeListener('append',this._boundLookupOwn),this._boundEmit({'isReserved':!![]},{'username':_0xb0aad0,'memberKey':_0x36c517});
this._watchRecord(_0xb0aad0,_0x36c517),await this.keet.internalUpdateProfile({'username':_0xb0aad0},{'forceUpdateRegistry':!![]}).catch(this._boundCatch);
}
._watchReservation(_0x1327ea,_0x12e73a){
_0x3737ed={
_0x26e5f2:0x26f,_0x4c5dde:0x27f,_0x1ab47f:0x263,_0x5d9a09:0x276,_0x463349:0x285,_0x4dbbc8:0x272,_0x6d7ad3:0x28f,_0x4ed0a6:0x265,_0x5df6fa:0x258,_0x210d57:0x276,_0x52d4b6:0x24b,_0x2450e2:0x26e,_0x5a6723:0x254,_0x2e30ab:0x290,_0x22b317:0x27c,_0xfb8664:0x2a8,_0x272125:0x289,_0x53221e:0x277,_0x1ba670:0x26d},_0x556c77={
_0x57c0fe:0x163},_0x32a304={
_0x473720:0x7b},_0x5da153=this;
this._watchingReservation=!![];
this._keetRegistry.watchReservation(_0x1327ea).then(_0x556da5,this._boundCatch);
async function _0x556da5(_0x128202){
_0x2eb642={
_0x5ec45f:0x8d,_0x4cf138:0x285,_0x324bd4:0x1e3}
;
_0x5da153._watchingReservation=![];
if(_0x128202===null)return;
if(!b4a.equals(_0x128202.memberKey,_0x12e73a)){
_0x5da153._boundCatch(KeetRegistryError.NAME_UNAVAILABLE());
return;
}
await _0x5da153._claimReservation(_0x128202);
}
}
._watchRecord(_0x24d121,_0x266993){
_0xc857bc={
_0x323b4:0x40,_0x502b87:0x30,_0x52fe69:0x27,_0x4c0581:0x21,_0x5ac2b7:0x10f,_0x56f9cb:0xfe,_0x3667c1:0x113,_0x33b367:0xd9},_0x3f16c2={
_0x53055a:0x191,_0x526be1:0x60,_0x13f066:0x502},_0x38c6c7={
_0x159d9d:0x1d,_0x53f208:0xe3},_0x15890b={
_0x51478c:0x3b8,_0xf56216:0x3c6,_0x54933:0x3d1,_0x34b21d:0x2a1,_0x120a7c:0x2d3},_0x4f6518={
_0x39b095:0x1a3,_0x1fd835:0x29f},_0x168533={
_0x5c822d:0xc0},_0x3ddd9f={
_0x4d3aff:0x1},_0x34d551=this;
const _0x3a5d21={
'attestation':{
'memberKey':_0x266993}
}
;
this._watchingRecord=!![],this._keetRegistry.watchRecord(_0x3a5d21,_0x31c4ed).then(_0xe5189d,this._boundCatch);
function _0x31c4ed(_0x240c8b){
return _0x240c8b.profile.username===_0x24d121;
}
function _0xe5189d(_0x169dca){
_0x34d551._watchingRecord=![];
if(_0x169dca===null)return;
_0x34d551._boundEmit({'hasRecord':!![]},null,_0x169dca);
}
}
}
module.exports=UserRegistry;
function fullStatus(_0x4b0ee3){
_0x30e1aa={
_0xa1789:0x3a,_0x37cf80:0x1ad,_0x15ad1f:0x449},_0x20b811={
_0x33d52f:0xdf,_0x5a03e8:0x11b,_0x18a4d2:0x8a}
;
return{
'hasRecord':_0x4b0ee3.hasRecord===!![],'hasError':_0x4b0ee3.hasError===!![],'isReserving':_0x4b0ee3.isReserving===!![],'isReserved':_0x4b0ee3.isReserved===!![]||_0x4b0ee3.hasRecord===!![]}
;
}