Add db.account.del (#27)

This commit is contained in:
Powersource 2024-03-25 16:45:08 +01:00 committed by GitHub
parent 3c236581fa
commit d332308104
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 73 additions and 22 deletions

View File

@ -957,6 +957,74 @@ function initDB(peer, config) {
}) })
} }
/**
* Remove the given `keypair` (or the implicit config.global.keypair) from
* the given `account`.
*
* @param {{
* account: string;
* keypair: KeypairPublicSlice;
* }} opts
* @param {CB<RecPresent>} cb
*/
function delFromAccount(opts, cb) {
if (!opts) return cb(new Error('account.del() requires an `opts`'))
// prettier-ignore
if (!opts.account) return cb(new Error('account.del() requires a `account`'))
// prettier-ignore
if (!opts.keypair) return cb(new Error('account.del() requires a `keypair`'))
// prettier-ignore
if (!opts.keypair.public) return cb(new Error('account.del() requires a `keypair` with `public`'))
const deldKeypair = opts.keypair
const signingKeypair = config.global.keypair
// Verify powers of the signingKeypair:
const accountTangle = new DBTangle(opts.account, records(), get)
const signingPowers = getAccountPowers(accountTangle, signingKeypair)
if (!signingPowers.has('del')) {
// prettier-ignore
return cb(new Error('account.del() failed because the signing keypair does not have the "del" power'))
}
const accountRoot = get(opts.account)
if (!accountRoot) {
// prettier-ignore
return cb(new Error(`account.del() failed because the account root "${opts.account}" is unknown`))
}
let msg
try {
msg = MsgV4.create({
account: ACCOUNT_SELF,
accountTips: null,
domain: accountRoot.metadata.domain,
keypair: signingKeypair,
tangles: {
[opts.account]: accountTangle,
},
data: {
action: 'del',
key: {
purpose: 'sig',
algorithm: 'ed25519',
bytes: deldKeypair.public,
},
},
})
} catch (err) {
return cb(new Error('account.del() failed', { cause: err }))
}
const msgID = MsgV4.getMsgID(msg)
logAppend(msgID, msg, (err, rec) => {
// prettier-ignore
if (err) return cb(new Error('account.del() failed to append the log', { cause: err }))
queueMicrotask(() => onRecordAdded.set(rec))
cb(null, rec)
})
}
/** /**
* @param {{ * @param {{
* keypair?: Keypair; * keypair?: Keypair;
@ -1277,6 +1345,7 @@ function initDB(peer, config) {
create: createAccount, create: createAccount,
findOrCreate: findOrCreateAccount, findOrCreate: findOrCreateAccount,
add: addToAccount, add: addToAccount,
del: delFromAccount,
consent: consentToAccount, consent: consentToAccount,
has: accountHas, has: accountHas,
}, },

View File

@ -243,28 +243,10 @@ test('account.add()', async (t) => {
}) })
const postMootRec = peer.db.feed.findMoot(account, 'post') const postMootRec = peer.db.feed.findMoot(account, 'post')
const tangle = new MsgV4.Tangle(account) const delRec = await p(peer.db.account.del)({
tangle.add(account, accountMsg0) account,
tangle.add(accountRec1.id, accountRec1.msg) keypair: keypair2,
// can't publish() account msgs. and creating this manually for now until we have a .del() fn
const delMsg = MsgV4.create({
account: 'self',
accountTips: null,
domain: accountMsg0.metadata.domain,
keypair: keypair1,
tangles: {
[account]: tangle,
},
data: {
action: 'del',
key: {
purpose: 'sig',
algorithm: 'ed25519',
bytes: keypair2.public,
},
},
}) })
await p(peer.db.add)(delMsg, account)
const badRec = await p(peer.db.feed.publish)({ const badRec = await p(peer.db.feed.publish)({
account, account,
@ -285,7 +267,7 @@ test('account.add()', async (t) => {
await p(carol.db.add)(accountRec1.msg, account) await p(carol.db.add)(accountRec1.msg, account)
await p(carol.db.add)(postMootRec.msg, postMootRec.id) await p(carol.db.add)(postMootRec.msg, postMootRec.id)
await p(carol.db.add)(goodRec.msg, postMootRec.id) await p(carol.db.add)(goodRec.msg, postMootRec.id)
await p(carol.db.add)(delMsg, account) await p(carol.db.add)(delRec.msg, account)
await assert.rejects( await assert.rejects(
p(carol.db.add)(badRec.msg, postMootRec.id), p(carol.db.add)(badRec.msg, postMootRec.id),
/add\(\) failed to verify msg/, /add\(\) failed to verify msg/,