From fc47a4006bc36f73f0a9305d6a6c2d680cf3f4a5 Mon Sep 17 00:00:00 2001 From: Andre Staltz Date: Thu, 16 Nov 2023 12:09:00 +0200 Subject: [PATCH] account msgs have "account__" domain prefix --- lib/index.js | 26 +++++++++++++++----------- lib/msg-v3/constants.js | 2 ++ protospec.md | 2 +- test/account-add.test.js | 8 ++++---- test/account-create.test.js | 22 +++++++++++----------- test/del.test.js | 2 +- test/erase.test.js | 2 +- test/feed-get-id.test.js | 2 +- test/feed-publish.test.js | 2 +- test/get.test.js | 2 +- test/getTangle.test.js | 2 +- test/ghosts.tests.js | 4 ++-- test/msgs-iterator.test.js | 2 +- test/on-record-added.test.js | 2 +- test/re-open.test.js | 2 +- test/records-iterator.test.js | 2 +- 16 files changed, 45 insertions(+), 39 deletions(-) diff --git a/lib/index.js b/lib/index.js index eed7ddd..a0c9911 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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} 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} 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} cb diff --git a/lib/msg-v3/constants.js b/lib/msg-v3/constants.js index 7dca076..e8c8d53 100644 --- a/lib/msg-v3/constants.js +++ b/lib/msg-v3/constants.js @@ -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:', } diff --git a/protospec.md b/protospec.md index 65ff919..d455bb3 100644 --- a/protospec.md +++ b/protospec.md @@ -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 diff --git a/test/account-add.test.js b/test/account-add.test.js index f27797d..947db7f 100644 --- a/test/account-add.test.js +++ b/test/account-add.test.js @@ -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) diff --git a/test/account-create.test.js b/test/account-create.test.js index 36e9b67..82fa663 100644 --- a/test/account-create.test.js +++ b/test/account-create.test.js @@ -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') diff --git a/test/del.test.js b/test/del.test.js index 51d8875..23c5bd9 100644 --- a/test/del.test.js +++ b/test/del.test.js @@ -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++) { diff --git a/test/erase.test.js b/test/erase.test.js index f38e283..258a754 100644 --- a/test/erase.test.js +++ b/test/erase.test.js @@ -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++) { diff --git a/test/feed-get-id.test.js b/test/feed-get-id.test.js index 98db896..eb56540 100644 --- a/test/feed-get-id.test.js +++ b/test/feed-get-id.test.js @@ -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) diff --git a/test/feed-publish.test.js b/test/feed-publish.test.js index 4ff223d..bcdefd2 100644 --- a/test/feed-publish.test.js +++ b/test/feed-publish.test.js @@ -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) } diff --git a/test/get.test.js b/test/get.test.js index b67da23..b199339 100644 --- a/test/get.test.js +++ b/test/get.test.js @@ -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, diff --git a/test/getTangle.test.js b/test/getTangle.test.js index c988278..6ac983e 100644 --- a/test/getTangle.test.js +++ b/test/getTangle.test.js @@ -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 diff --git a/test/ghosts.tests.js b/test/ghosts.tests.js index 388a6cd..bea9624 100644 --- a/test/ghosts.tests.js +++ b/test/ghosts.tests.js @@ -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 = [] diff --git a/test/msgs-iterator.test.js b/test/msgs-iterator.test.js index d219d4a..a673c60 100644 --- a/test/msgs-iterator.test.js +++ b/test/msgs-iterator.test.js @@ -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)({ diff --git a/test/on-record-added.test.js b/test/on-record-added.test.js index cba5553..949e4d3 100644 --- a/test/on-record-added.test.js +++ b/test/on-record-added.test.js @@ -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) => { diff --git a/test/re-open.test.js b/test/re-open.test.js index ca42145..89f8f80 100644 --- a/test/re-open.test.js +++ b/test/re-open.test.js @@ -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 = [] diff --git a/test/records-iterator.test.js b/test/records-iterator.test.js index 7800d04..349021b 100644 --- a/test/records-iterator.test.js +++ b/test/records-iterator.test.js @@ -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)({