mirror of https://codeberg.org/pzp/pzp-db.git
account msgs have "account__" domain prefix
This commit is contained in:
parent
1ff84756bc
commit
fc47a4006b
26
lib/index.js
26
lib/index.js
|
@ -11,6 +11,7 @@ const {
|
||||||
SIGNATURE_TAG_ACCOUNT_ADD,
|
SIGNATURE_TAG_ACCOUNT_ADD,
|
||||||
ACCOUNT_SELF,
|
ACCOUNT_SELF,
|
||||||
ACCOUNT_ANY,
|
ACCOUNT_ANY,
|
||||||
|
ACCOUNT_DOMAIN_PREFIX,
|
||||||
} = require('./msg-v3/constants')
|
} = require('./msg-v3/constants')
|
||||||
const ReadyGate = require('./utils/ready-gate')
|
const ReadyGate = require('./utils/ready-gate')
|
||||||
const Ghosts = require('./ghosts')
|
const Ghosts = require('./ghosts')
|
||||||
|
@ -487,20 +488,21 @@ function initDB(peer, config) {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Find the account that contains this `keypair` (or the implicit
|
* 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
|
* @public
|
||||||
* @param {{
|
* @param {{
|
||||||
* keypair?: KeypairPublicSlice;
|
* keypair?: KeypairPublicSlice;
|
||||||
* domain: string;
|
* subdomain: string;
|
||||||
* }} opts
|
* }} opts
|
||||||
* @param {CB<string>} cb
|
* @param {CB<string>} cb
|
||||||
*/
|
*/
|
||||||
function findAccount(opts, cb) {
|
function findAccount(opts, cb) {
|
||||||
// prettier-ignore
|
// 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 keypair = opts?.keypair ?? config.keypair
|
||||||
const domain = opts.domain
|
const domain = ACCOUNT_DOMAIN_PREFIX + opts.subdomain
|
||||||
|
|
||||||
for (let i = 0; i < recs.length; i++) {
|
for (let i = 0; i < recs.length; i++) {
|
||||||
const rec = recs[i]
|
const rec = recs[i]
|
||||||
|
@ -522,7 +524,7 @@ function initDB(peer, config) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// prettier-ignore
|
// 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)
|
cb(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -556,21 +558,22 @@ function initDB(peer, config) {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create an account (root msg) for the given `keypair` (or the implicit
|
* 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
|
* @public
|
||||||
* @param {{
|
* @param {{
|
||||||
* keypair?: Keypair,
|
* keypair?: Keypair,
|
||||||
* domain: string,
|
* subdomain: string,
|
||||||
* _nonce?: string
|
* _nonce?: string
|
||||||
* }} opts
|
* }} opts
|
||||||
* @param {CB<string>} cb
|
* @param {CB<string>} cb
|
||||||
*/
|
*/
|
||||||
function createAccount(opts, cb) {
|
function createAccount(opts, cb) {
|
||||||
// prettier-ignore
|
// 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 keypair = opts?.keypair ?? config.keypair
|
||||||
const domain = opts.domain
|
const domain = ACCOUNT_DOMAIN_PREFIX + opts.subdomain
|
||||||
|
|
||||||
let msg
|
let msg
|
||||||
try {
|
try {
|
||||||
|
@ -590,12 +593,13 @@ function initDB(peer, config) {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Find or create an account (root msg) for the given `keypair` (or the
|
* 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
|
* @public
|
||||||
* @param {{
|
* @param {{
|
||||||
* keypair?: Keypair,
|
* keypair?: Keypair,
|
||||||
* domain: string,
|
* subdomain: string,
|
||||||
* _nonce?: string
|
* _nonce?: string
|
||||||
* }} opts
|
* }} opts
|
||||||
* @param {CB<string>} cb
|
* @param {CB<string>} cb
|
||||||
|
|
|
@ -5,6 +5,8 @@ module.exports = {
|
||||||
/** @type {'any'} */
|
/** @type {'any'} */
|
||||||
ACCOUNT_ANY: 'any',
|
ACCOUNT_ANY: 'any',
|
||||||
|
|
||||||
|
ACCOUNT_DOMAIN_PREFIX: 'account__',
|
||||||
|
|
||||||
SIGNATURE_TAG_MSG_V3: ':msg-v3:',
|
SIGNATURE_TAG_MSG_V3: ':msg-v3:',
|
||||||
SIGNATURE_TAG_ACCOUNT_ADD: ':account-add:',
|
SIGNATURE_TAG_ACCOUNT_ADD: ':account-add:',
|
||||||
}
|
}
|
||||||
|
|
|
@ -55,7 +55,7 @@ interface Msg {
|
||||||
accountTips: null // MUST be null
|
accountTips: null // MUST be null
|
||||||
dataHash: DataHash
|
dataHash: DataHash
|
||||||
dataSize: number
|
dataSize: number
|
||||||
domain: string // alphanumeric string, at least 3 chars, max 100 chars
|
domain: string // alphanumeric string, must start with "account__"
|
||||||
tangles: {
|
tangles: {
|
||||||
[accountTangleID: string]: {
|
[accountTangleID: string]: {
|
||||||
depth: number // maximum distance (positive integer) from this msg to the root
|
depth: number // maximum distance (positive integer) from this msg to the root
|
||||||
|
|
|
@ -24,7 +24,7 @@ test('account.add()', async (t) => {
|
||||||
await peer.db.loaded()
|
await peer.db.loaded()
|
||||||
const account = await p(peer.db.account.create)({
|
const account = await p(peer.db.account.create)({
|
||||||
keypair: keypair1,
|
keypair: keypair1,
|
||||||
domain: 'person',
|
subdomain: 'person',
|
||||||
})
|
})
|
||||||
|
|
||||||
assert.equal(peer.db.account.has({ account, keypair: keypair2 }), false)
|
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.account, 'self', 'msg.metadata.account')
|
||||||
assert.equal(msg.metadata.accountTips, null, 'msg.metadata.accountTips')
|
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(
|
assert.deepEqual(
|
||||||
msg.metadata.tangles,
|
msg.metadata.tangles,
|
||||||
{ [account]: { depth: 1, prev: [account] } },
|
{ [account]: { depth: 1, prev: [account] } },
|
||||||
|
@ -83,7 +83,7 @@ test('account.add()', async (t) => {
|
||||||
await peer1.db.loaded()
|
await peer1.db.loaded()
|
||||||
const id = await p(peer1.db.account.create)({
|
const id = await p(peer1.db.account.create)({
|
||||||
keypair: keypair1,
|
keypair: keypair1,
|
||||||
domain: 'account',
|
subdomain: 'account',
|
||||||
})
|
})
|
||||||
const msg1 = peer1.db.get(id)
|
const msg1 = peer1.db.get(id)
|
||||||
|
|
||||||
|
@ -164,7 +164,7 @@ test('account.add()', async (t) => {
|
||||||
|
|
||||||
const account = await p(peer.db.account.create)({
|
const account = await p(peer.db.account.create)({
|
||||||
keypair: keypair1,
|
keypair: keypair1,
|
||||||
domain: 'person',
|
subdomain: 'person',
|
||||||
})
|
})
|
||||||
const accountMsg0 = peer.db.get(account)
|
const accountMsg0 = peer.db.get(account)
|
||||||
|
|
||||||
|
|
|
@ -21,7 +21,7 @@ test('account.create() ', async (t) => {
|
||||||
|
|
||||||
await peer.db.loaded()
|
await peer.db.loaded()
|
||||||
const account = await p(peer.db.account.create)({
|
const account = await p(peer.db.account.create)({
|
||||||
domain: 'person',
|
subdomain: 'person',
|
||||||
_nonce: 'MYNONCE',
|
_nonce: 'MYNONCE',
|
||||||
})
|
})
|
||||||
assert.ok(account, 'accountRec0 exists')
|
assert.ok(account, 'accountRec0 exists')
|
||||||
|
@ -64,7 +64,7 @@ test('account.create() ', async (t) => {
|
||||||
await peer.db.loaded()
|
await peer.db.loaded()
|
||||||
const account = await p(peer.db.account.create)({
|
const account = await p(peer.db.account.create)({
|
||||||
keypair,
|
keypair,
|
||||||
domain: 'person',
|
subdomain: 'person',
|
||||||
})
|
})
|
||||||
assert.ok(account, 'account created')
|
assert.ok(account, 'account created')
|
||||||
const msg = peer.db.get(account)
|
const msg = peer.db.get(account)
|
||||||
|
@ -84,7 +84,7 @@ test('account.create() ', async (t) => {
|
||||||
await t.test('account.find() can find', async (t) => {
|
await t.test('account.find() can find', async (t) => {
|
||||||
rimraf.sync(DIR)
|
rimraf.sync(DIR)
|
||||||
const keypair = Keypair.generate('ed25519', 'alice')
|
const keypair = Keypair.generate('ed25519', 'alice')
|
||||||
const domain = 'person'
|
const subdomain = 'person'
|
||||||
|
|
||||||
const peer = SecretStack({ appKey: caps.shse })
|
const peer = SecretStack({ appKey: caps.shse })
|
||||||
.use(require('../lib'))
|
.use(require('../lib'))
|
||||||
|
@ -92,10 +92,10 @@ test('account.create() ', async (t) => {
|
||||||
.call(null, { keypair, path: DIR })
|
.call(null, { keypair, path: DIR })
|
||||||
|
|
||||||
await peer.db.loaded()
|
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')
|
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')
|
assert.equal(found, account, 'found')
|
||||||
|
|
||||||
await p(peer.close)()
|
await p(peer.close)()
|
||||||
|
@ -104,7 +104,7 @@ test('account.create() ', async (t) => {
|
||||||
await t.test('account.findOrCreate() can find', async (t) => {
|
await t.test('account.findOrCreate() can find', async (t) => {
|
||||||
rimraf.sync(DIR)
|
rimraf.sync(DIR)
|
||||||
const keypair = Keypair.generate('ed25519', 'alice')
|
const keypair = Keypair.generate('ed25519', 'alice')
|
||||||
const domain = 'person'
|
const subdomain = 'person'
|
||||||
|
|
||||||
const peer = SecretStack({ appKey: caps.shse })
|
const peer = SecretStack({ appKey: caps.shse })
|
||||||
.use(require('../lib'))
|
.use(require('../lib'))
|
||||||
|
@ -112,10 +112,10 @@ test('account.create() ', async (t) => {
|
||||||
.call(null, { keypair, path: DIR })
|
.call(null, { keypair, path: DIR })
|
||||||
|
|
||||||
await peer.db.loaded()
|
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')
|
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')
|
assert.equal(found, account, 'found')
|
||||||
|
|
||||||
await p(peer.close)()
|
await p(peer.close)()
|
||||||
|
@ -124,7 +124,7 @@ test('account.create() ', async (t) => {
|
||||||
await t.test('account.findOrCreate() can create', async (t) => {
|
await t.test('account.findOrCreate() can create', async (t) => {
|
||||||
rimraf.sync(DIR)
|
rimraf.sync(DIR)
|
||||||
const keypair = Keypair.generate('ed25519', 'alice')
|
const keypair = Keypair.generate('ed25519', 'alice')
|
||||||
const domain = 'person'
|
const subdomain = 'person'
|
||||||
|
|
||||||
const peer = SecretStack({ appKey: caps.shse })
|
const peer = SecretStack({ appKey: caps.shse })
|
||||||
.use(require('../lib'))
|
.use(require('../lib'))
|
||||||
|
@ -134,13 +134,13 @@ test('account.create() ', async (t) => {
|
||||||
await peer.db.loaded()
|
await peer.db.loaded()
|
||||||
|
|
||||||
let gotError = false
|
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')
|
assert.equal(err.cause, 'ENOENT')
|
||||||
gotError = true
|
gotError = true
|
||||||
})
|
})
|
||||||
assert.ok(gotError, 'account not found')
|
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')
|
assert.ok(account, 'account created')
|
||||||
const msg = peer.db.get(account)
|
const msg = peer.db.get(account)
|
||||||
assert.equal(msg.data.key.bytes, keypair.public, 'msg.data')
|
assert.equal(msg.data.key.bytes, keypair.public, 'msg.data')
|
||||||
|
|
|
@ -20,7 +20,7 @@ test('del()', async (t) => {
|
||||||
|
|
||||||
await peer.db.loaded()
|
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 = []
|
const msgIDs = []
|
||||||
for (let i = 0; i < 5; i++) {
|
for (let i = 0; i < 5; i++) {
|
||||||
|
|
|
@ -21,7 +21,7 @@ test('erase()', async (t) => {
|
||||||
|
|
||||||
await peer.db.loaded()
|
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 = []
|
const msgIDs = []
|
||||||
for (let i = 0; i < 5; i++) {
|
for (let i = 0; i < 5; i++) {
|
||||||
|
|
|
@ -21,7 +21,7 @@ test('feed.getID()', async (t) => {
|
||||||
|
|
||||||
await peer.db.loaded()
|
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 moot = MsgV3.createMoot(id, 'post', keypair)
|
||||||
const mootID = MsgV3.getMsgID(moot)
|
const mootID = MsgV3.getMsgID(moot)
|
||||||
|
|
||||||
|
|
|
@ -29,7 +29,7 @@ test('feed.publish()', async (t) => {
|
||||||
|
|
||||||
await peer.db.loaded()
|
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)
|
moot = MsgV3.createMoot(id, 'post', keypair)
|
||||||
mootID = MsgV3.getMsgID(moot)
|
mootID = MsgV3.getMsgID(moot)
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,7 +21,7 @@ test('get()', async (t) => {
|
||||||
|
|
||||||
await peer.db.loaded()
|
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)({
|
const rec1 = await p(peer.db.feed.publish)({
|
||||||
account: id,
|
account: id,
|
||||||
|
|
|
@ -29,7 +29,7 @@ test('getTangle()', async (t) => {
|
||||||
|
|
||||||
await peer.db.loaded()
|
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
|
// Slow down append so that we can trigger msg creation in parallel
|
||||||
const originalAppend = peer.db._getLog().append
|
const originalAppend = peer.db._getLog().append
|
||||||
|
|
|
@ -20,7 +20,7 @@ test('ghosts.add, ghosts.get, ghosts.getMinDepth', async (t) => {
|
||||||
|
|
||||||
await peer.db.loaded()
|
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
|
const SPAN = 5
|
||||||
|
|
||||||
let msgIDs = []
|
let msgIDs = []
|
||||||
|
@ -66,7 +66,7 @@ test('ghosts.add queues very-concurrent calls', async (t) => {
|
||||||
|
|
||||||
await peer.db.loaded()
|
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
|
const SPAN = 5
|
||||||
|
|
||||||
let msgIDs = []
|
let msgIDs = []
|
||||||
|
|
|
@ -19,7 +19,7 @@ test('msgs() iterator', async (t) => {
|
||||||
|
|
||||||
await peer.db.loaded()
|
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++) {
|
for (let i = 0; i < 6; i++) {
|
||||||
await p(peer.db.feed.publish)({
|
await p(peer.db.feed.publish)({
|
||||||
|
|
|
@ -19,7 +19,7 @@ test('onRecordAdded', async (t) => {
|
||||||
|
|
||||||
await peer.db.loaded()
|
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 = []
|
const listened = []
|
||||||
var remove = peer.db.onRecordAdded((ev) => {
|
var remove = peer.db.onRecordAdded((ev) => {
|
||||||
|
|
|
@ -19,7 +19,7 @@ test('publish some msgs, close, re-open', async (t) => {
|
||||||
.call(null, { keypair, path: DIR })
|
.call(null, { keypair, path: DIR })
|
||||||
|
|
||||||
await peer.db.loaded()
|
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')
|
// t.pass('opened db')
|
||||||
|
|
||||||
const msgIDs = []
|
const msgIDs = []
|
||||||
|
|
|
@ -18,7 +18,7 @@ test('records() iterator', async (t) => {
|
||||||
.call(null, { keypair, path: DIR })
|
.call(null, { keypair, path: DIR })
|
||||||
|
|
||||||
await peer.db.loaded()
|
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++) {
|
for (let i = 0; i < 6; i++) {
|
||||||
await p(peer.db.feed.publish)({
|
await p(peer.db.feed.publish)({
|
||||||
|
|
Loading…
Reference in New Issue