new identity.has() API

This commit is contained in:
Andre Staltz 2023-07-18 23:47:13 +03:00
parent 7dea4b2328
commit 54f00d1944
No known key found for this signature in database
GPG Key ID: 9EDE23EA7E8A4890
2 changed files with 29 additions and 0 deletions

View File

@ -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,

View File

@ -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)()
}) })