mirror of https://codeberg.org/pzp/pzp-db.git
new identity.has() API
This commit is contained in:
parent
7dea4b2328
commit
54f00d1944
25
lib/index.js
25
lib/index.js
|
@ -385,6 +385,30 @@ function initDB(peer, config) {
|
||||||
cb(err)
|
cb(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {{
|
||||||
|
* keypair?: KeypairPublicSlice;
|
||||||
|
* identity: string;
|
||||||
|
* }} opts
|
||||||
|
*/
|
||||||
|
function identityHas(opts) {
|
||||||
|
const keypair = opts?.keypair ?? config.keypair
|
||||||
|
|
||||||
|
const identityTangle = new DBTangle(opts.identity, records())
|
||||||
|
for (const msgHash of identityTangle.topoSort()) {
|
||||||
|
const msg = get(msgHash)
|
||||||
|
if (!msg?.data) continue
|
||||||
|
/** @type {IdentityData} */
|
||||||
|
const data = msg.data
|
||||||
|
if (data.action !== 'add') continue
|
||||||
|
if (data.add.key.algorithm !== keypair.curve) continue
|
||||||
|
if (data.add.key.bytes === keypair.public) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {{
|
* @param {{
|
||||||
* keypair?: Keypair,
|
* keypair?: Keypair,
|
||||||
|
@ -722,6 +746,7 @@ function initDB(peer, config) {
|
||||||
findOrCreate: findOrCreateIdentity,
|
findOrCreate: findOrCreateIdentity,
|
||||||
add: addToIdentity,
|
add: addToIdentity,
|
||||||
consent: consentToIdentity,
|
consent: consentToIdentity,
|
||||||
|
has: identityHas,
|
||||||
},
|
},
|
||||||
feed: {
|
feed: {
|
||||||
publish: publishToFeed,
|
publish: publishToFeed,
|
||||||
|
|
|
@ -26,6 +26,8 @@ test('identity.add()', async (t) => {
|
||||||
domain: 'person',
|
domain: 'person',
|
||||||
})
|
})
|
||||||
|
|
||||||
|
assert.equal(peer.db.identity.has({ identity: id, keypair: keypair2 }), false)
|
||||||
|
|
||||||
const consent = peer.db.identity.consent({ identity: id, keypair: keypair2 })
|
const consent = peer.db.identity.consent({ identity: id, keypair: keypair2 })
|
||||||
|
|
||||||
const identityRec1 = await p(peer.db.identity.add)({
|
const identityRec1 = await p(peer.db.identity.add)({
|
||||||
|
@ -60,6 +62,8 @@ test('identity.add()', async (t) => {
|
||||||
)
|
)
|
||||||
assert.equal(msg.pubkey, keypair1.public, 'msg.pubkey OLD KEY')
|
assert.equal(msg.pubkey, keypair1.public, 'msg.pubkey OLD KEY')
|
||||||
|
|
||||||
|
assert.equal(peer.db.identity.has({ identity: id, keypair: keypair2 }), true)
|
||||||
|
|
||||||
await p(peer.close)()
|
await p(peer.close)()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue