account msgs have "account__" domain prefix

This commit is contained in:
Andre Staltz 2023-11-16 12:09:00 +02:00
parent 1ff84756bc
commit fc47a4006b
No known key found for this signature in database
GPG Key ID: 9EDE23EA7E8A4890
16 changed files with 45 additions and 39 deletions

View File

@ -11,6 +11,7 @@ const {
SIGNATURE_TAG_ACCOUNT_ADD,
ACCOUNT_SELF,
ACCOUNT_ANY,
ACCOUNT_DOMAIN_PREFIX,
} = require('./msg-v3/constants')
const ReadyGate = require('./utils/ready-gate')
const Ghosts = require('./ghosts')
@ -487,20 +488,21 @@ function initDB(peer, config) {
/**
* Find the account that contains this `keypair` (or the implicit
* config.keypair) under the given `domain`.
* config.keypair) under the given `subdomain` (will be converted to an actual
* msg domain).
*
* @public
* @param {{
* keypair?: KeypairPublicSlice;
* domain: string;
* subdomain: string;
* }} opts
* @param {CB<string>} cb
*/
function findAccount(opts, cb) {
// prettier-ignore
if (!opts.domain) return cb(new Error('account.find() requires a `domain`'))
if (!opts.subdomain) return cb(new Error('account.find() requires a `subdomain`'))
const keypair = opts?.keypair ?? config.keypair
const domain = opts.domain
const domain = ACCOUNT_DOMAIN_PREFIX + opts.subdomain
for (let i = 0; i < recs.length; i++) {
const rec = recs[i]
@ -522,7 +524,7 @@ function initDB(peer, config) {
}
}
// prettier-ignore
const err = new Error(`account.find() failed for pubkey=${keypair.public} domain=${domain}`, { cause: 'ENOENT' });
const err = new Error(`account.find() failed for pubkey=${keypair.public} subdomain=${opts.subdomain}`, { cause: 'ENOENT' });
cb(err)
}
@ -556,21 +558,22 @@ function initDB(peer, config) {
/**
* Create an account (root msg) for the given `keypair` (or the implicit
* config.keypair) under this `domain`.
* config.keypair) under the given `subdomain` (will be converted to an actual
* msg domain).
*
* @public
* @param {{
* keypair?: Keypair,
* domain: string,
* subdomain: string,
* _nonce?: string
* }} opts
* @param {CB<string>} cb
*/
function createAccount(opts, cb) {
// prettier-ignore
if (!opts.domain) return cb(new Error('account.create() requires a `domain`'))
if (!opts.subdomain) return cb(new Error('account.create() requires a `subdomain`'))
const keypair = opts?.keypair ?? config.keypair
const domain = opts.domain
const domain = ACCOUNT_DOMAIN_PREFIX + opts.subdomain
let msg
try {
@ -590,12 +593,13 @@ function initDB(peer, config) {
/**
* Find or create an account (root msg) for the given `keypair` (or the
* implicit config.keypair) under this `domain`.
* implicit config.keypair) under the given `domain` (will be converted to an
* actual msg domain).
*
* @public
* @param {{
* keypair?: Keypair,
* domain: string,
* subdomain: string,
* _nonce?: string
* }} opts
* @param {CB<string>} cb

View File

@ -5,6 +5,8 @@ module.exports = {
/** @type {'any'} */
ACCOUNT_ANY: 'any',
ACCOUNT_DOMAIN_PREFIX: 'account__',
SIGNATURE_TAG_MSG_V3: ':msg-v3:',
SIGNATURE_TAG_ACCOUNT_ADD: ':account-add:',
}

View File

@ -55,7 +55,7 @@ interface Msg {
accountTips: null // MUST be null
dataHash: DataHash
dataSize: number
domain: string // alphanumeric string, at least 3 chars, max 100 chars
domain: string // alphanumeric string, must start with "account__"
tangles: {
[accountTangleID: string]: {
depth: number // maximum distance (positive integer) from this msg to the root

View File

@ -24,7 +24,7 @@ test('account.add()', async (t) => {
await peer.db.loaded()
const account = await p(peer.db.account.create)({
keypair: keypair1,
domain: 'person',
subdomain: 'person',
})
assert.equal(peer.db.account.has({ account, keypair: keypair2 }), false)
@ -56,7 +56,7 @@ test('account.add()', async (t) => {
)
assert.equal(msg.metadata.account, 'self', 'msg.metadata.account')
assert.equal(msg.metadata.accountTips, null, 'msg.metadata.accountTips')
assert.equal(msg.metadata.domain, 'person', 'msg.metadata.domain')
assert.equal(msg.metadata.domain, 'account__person', 'msg.metadata.domain')
assert.deepEqual(
msg.metadata.tangles,
{ [account]: { depth: 1, prev: [account] } },
@ -83,7 +83,7 @@ test('account.add()', async (t) => {
await peer1.db.loaded()
const id = await p(peer1.db.account.create)({
keypair: keypair1,
domain: 'account',
subdomain: 'account',
})
const msg1 = peer1.db.get(id)
@ -164,7 +164,7 @@ test('account.add()', async (t) => {
const account = await p(peer.db.account.create)({
keypair: keypair1,
domain: 'person',
subdomain: 'person',
})
const accountMsg0 = peer.db.get(account)

View File

@ -21,7 +21,7 @@ test('account.create() ', async (t) => {
await peer.db.loaded()
const account = await p(peer.db.account.create)({
domain: 'person',
subdomain: 'person',
_nonce: 'MYNONCE',
})
assert.ok(account, 'accountRec0 exists')
@ -64,7 +64,7 @@ test('account.create() ', async (t) => {
await peer.db.loaded()
const account = await p(peer.db.account.create)({
keypair,
domain: 'person',
subdomain: 'person',
})
assert.ok(account, 'account created')
const msg = peer.db.get(account)
@ -84,7 +84,7 @@ test('account.create() ', async (t) => {
await t.test('account.find() can find', async (t) => {
rimraf.sync(DIR)
const keypair = Keypair.generate('ed25519', 'alice')
const domain = 'person'
const subdomain = 'person'
const peer = SecretStack({ appKey: caps.shse })
.use(require('../lib'))
@ -92,10 +92,10 @@ test('account.create() ', async (t) => {
.call(null, { keypair, path: DIR })
await peer.db.loaded()
const account = await p(peer.db.account.create)({ keypair, domain })
const account = await p(peer.db.account.create)({ keypair, subdomain })
assert.ok(account, 'account created')
const found = await p(peer.db.account.find)({ keypair, domain })
const found = await p(peer.db.account.find)({ keypair, subdomain })
assert.equal(found, account, 'found')
await p(peer.close)()
@ -104,7 +104,7 @@ test('account.create() ', async (t) => {
await t.test('account.findOrCreate() can find', async (t) => {
rimraf.sync(DIR)
const keypair = Keypair.generate('ed25519', 'alice')
const domain = 'person'
const subdomain = 'person'
const peer = SecretStack({ appKey: caps.shse })
.use(require('../lib'))
@ -112,10 +112,10 @@ test('account.create() ', async (t) => {
.call(null, { keypair, path: DIR })
await peer.db.loaded()
const account = await p(peer.db.account.create)({ keypair, domain })
const account = await p(peer.db.account.create)({ keypair, subdomain })
assert.ok(account, 'account created')
const found = await p(peer.db.account.findOrCreate)({ keypair, domain })
const found = await p(peer.db.account.findOrCreate)({ keypair, subdomain })
assert.equal(found, account, 'found')
await p(peer.close)()
@ -124,7 +124,7 @@ test('account.create() ', async (t) => {
await t.test('account.findOrCreate() can create', async (t) => {
rimraf.sync(DIR)
const keypair = Keypair.generate('ed25519', 'alice')
const domain = 'person'
const subdomain = 'person'
const peer = SecretStack({ appKey: caps.shse })
.use(require('../lib'))
@ -134,13 +134,13 @@ test('account.create() ', async (t) => {
await peer.db.loaded()
let gotError = false
await p(peer.db.account.find)({ keypair, domain }).catch((err) => {
await p(peer.db.account.find)({ keypair, subdomain }).catch((err) => {
assert.equal(err.cause, 'ENOENT')
gotError = true
})
assert.ok(gotError, 'account not found')
const account = await p(peer.db.account.findOrCreate)({ keypair, domain })
const account = await p(peer.db.account.findOrCreate)({ keypair, subdomain })
assert.ok(account, 'account created')
const msg = peer.db.get(account)
assert.equal(msg.data.key.bytes, keypair.public, 'msg.data')

View File

@ -20,7 +20,7 @@ test('del()', async (t) => {
await peer.db.loaded()
const id = await p(peer.db.account.create)({ domain: 'person' })
const id = await p(peer.db.account.create)({ subdomain: 'person' })
const msgIDs = []
for (let i = 0; i < 5; i++) {

View File

@ -21,7 +21,7 @@ test('erase()', async (t) => {
await peer.db.loaded()
const id = await p(peer.db.account.create)({ domain: 'person' })
const id = await p(peer.db.account.create)({ subdomain: 'person' })
const msgIDs = []
for (let i = 0; i < 5; i++) {

View File

@ -21,7 +21,7 @@ test('feed.getID()', async (t) => {
await peer.db.loaded()
const id = await p(peer.db.account.create)({ domain: 'person' })
const id = await p(peer.db.account.create)({ subdomain: 'person' })
const moot = MsgV3.createMoot(id, 'post', keypair)
const mootID = MsgV3.getMsgID(moot)

View File

@ -29,7 +29,7 @@ test('feed.publish()', async (t) => {
await peer.db.loaded()
id = await p(peer.db.account.create)({ domain: 'person' })
id = await p(peer.db.account.create)({ subdomain: 'person' })
moot = MsgV3.createMoot(id, 'post', keypair)
mootID = MsgV3.getMsgID(moot)
}

View File

@ -21,7 +21,7 @@ test('get()', async (t) => {
await peer.db.loaded()
const id = await p(peer.db.account.create)({ domain: 'person' })
const id = await p(peer.db.account.create)({ subdomain: 'person' })
const rec1 = await p(peer.db.feed.publish)({
account: id,

View File

@ -29,7 +29,7 @@ test('getTangle()', async (t) => {
await peer.db.loaded()
const id = await p(peer.db.account.create)({ domain: 'person' })
const id = await p(peer.db.account.create)({ subdomain: 'person' })
// Slow down append so that we can trigger msg creation in parallel
const originalAppend = peer.db._getLog().append

View File

@ -20,7 +20,7 @@ test('ghosts.add, ghosts.get, ghosts.getMinDepth', async (t) => {
await peer.db.loaded()
const account = await p(peer.db.account.create)({ domain: 'person' })
const account = await p(peer.db.account.create)({ subdomain: 'person' })
const SPAN = 5
let msgIDs = []
@ -66,7 +66,7 @@ test('ghosts.add queues very-concurrent calls', async (t) => {
await peer.db.loaded()
const account = await p(peer.db.account.create)({ domain: 'person' })
const account = await p(peer.db.account.create)({ subdomain: 'person' })
const SPAN = 5
let msgIDs = []

View File

@ -19,7 +19,7 @@ test('msgs() iterator', async (t) => {
await peer.db.loaded()
const account = (await p(peer.db.account.create)({domain: 'person'}))
const account = (await p(peer.db.account.create)({subdomain: 'person'}))
for (let i = 0; i < 6; i++) {
await p(peer.db.feed.publish)({

View File

@ -19,7 +19,7 @@ test('onRecordAdded', async (t) => {
await peer.db.loaded()
const account = (await p(peer.db.account.create)({domain: 'person'}))
const account = (await p(peer.db.account.create)({subdomain: 'person'}))
const listened = []
var remove = peer.db.onRecordAdded((ev) => {

View File

@ -19,7 +19,7 @@ test('publish some msgs, close, re-open', async (t) => {
.call(null, { keypair, path: DIR })
await peer.db.loaded()
const account = await p(peer.db.account.create)({ domain: 'person' })
const account = await p(peer.db.account.create)({ subdomain: 'person' })
// t.pass('opened db')
const msgIDs = []

View File

@ -18,7 +18,7 @@ test('records() iterator', async (t) => {
.call(null, { keypair, path: DIR })
await peer.db.loaded()
const account = (await p(peer.db.account.create)({ domain: 'person' }))
const account = (await p(peer.db.account.create)({ subdomain: 'person' }))
for (let i = 0; i < 6; i++) {
await p(peer.db.feed.publish)({