rename identity to account

This commit is contained in:
Andre Staltz 2023-08-10 10:42:34 +03:00
parent 9e98e36752
commit 207f6a4692
No known key found for this signature in database
GPG Key ID: 9EDE23EA7E8A4890
3 changed files with 28 additions and 28 deletions

View File

@ -6,7 +6,7 @@ const bs58 = require('bs58')
const b4a = require('b4a') const b4a = require('b4a')
/** /**
* @typedef {import('ppppp-db/msg-v3').IdentityAdd} IdentityAdd * @typedef {import('ppppp-db/msg-v3').AccountAdd} AccountAdd
*/ */
/** /**
@ -20,8 +20,8 @@ const b4a = require('b4a')
/** /**
* @typedef {{type: 'follow'}} FollowPromise * @typedef {{type: 'follow'}} FollowPromise
* @typedef {{type: 'identity-add', identity: string}} IdentityAddPromise * @typedef {{type: 'account-add', account: string}} AccountAddPromise
* @typedef {FollowPromise | IdentityAddPromise} PPromise * @typedef {FollowPromise | AccountAddPromise} PPromise
*/ */
const FILENAME = 'promises.json' const FILENAME = 'promises.json'
@ -34,11 +34,11 @@ module.exports = {
revoke: 'async', revoke: 'async',
// promises // promises
follow: 'async', follow: 'async',
identityAdd: 'async', accountAdd: 'async',
}, },
permissions: { permissions: {
anonymous: { anonymous: {
allow: ['follow', 'identityAdd'], allow: ['follow', 'accountAdd'],
}, },
}, },
@ -85,16 +85,16 @@ module.exports = {
if ( if (
typeof promise !== 'object' || typeof promise !== 'object' ||
typeof promise.type !== 'string' || typeof promise.type !== 'string' ||
(promise.type !== 'follow' && promise.type !== 'identity-add') (promise.type !== 'follow' && promise.type !== 'account-add')
) { ) {
return Error('Invalid promise created: ' + JSON.stringify(promise)) return Error('Invalid promise created: ' + JSON.stringify(promise))
} }
if ( if (
promise.type === 'identity-add' && promise.type === 'account-add' &&
typeof promise.identity !== 'string' typeof promise.account !== 'string'
) { ) {
// prettier-ignore // prettier-ignore
return Error('Invalid identity-add promise missing "identity" field: ' + JSON.stringify(promise)) return Error('Invalid account-add promise missing "account" field: ' + JSON.stringify(promise))
} }
return null return null
} }
@ -156,12 +156,12 @@ module.exports = {
/** /**
* @param {string} token * @param {string} token
* @param {IdentityAdd} addition * @param {AccountAdd} addition
* @param {CB<boolean>} cb * @param {CB<boolean>} cb
*/ */
function identityAdd(token, addition, cb) { function accountAdd(token, addition, cb) {
if (!loaded) { if (!loaded) {
setTimeout(() => identityAdd(token, addition, cb), 100) setTimeout(() => accountAdd(token, addition, cb), 100)
return return
} }
if ( if (
@ -180,21 +180,21 @@ module.exports = {
cb(new Error('Invalid token')) cb(new Error('Invalid token'))
return return
} }
const promise = /** @type {IdentityAddPromise} */ (promises.get(token)) const promise = /** @type {AccountAddPromise} */ (promises.get(token))
const { type, identity } = promise const { type, account } = promise
if (type !== 'identity-add') { if (type !== 'account-add') {
cb(new Error('Invalid token')) cb(new Error('Invalid token'))
return return
} }
const keypair = { curve: 'ed25519', public: addition.key.bytes } const keypair = { curve: 'ed25519', public: addition.key.bytes }
if (local.db.identity.has({ identity, keypair })) { if (local.db.account.has({ account, keypair })) {
cb(null, false) cb(null, false)
return return
} }
local.db.identity.add( local.db.account.add(
{ identity, keypair, consent: addition.consent }, { account, keypair, consent: addition.consent },
/** /**
* @param {Error | null} err * @param {Error | null} err
* @param {any} rec * @param {any} rec
@ -223,6 +223,6 @@ module.exports = {
save(cb) save(cb)
} }
return { create, revoke, follow, identityAdd } return { create, revoke, follow, accountAdd }
}, },
} }

View File

@ -33,13 +33,13 @@
"c8": "^7.11.0", "c8": "^7.11.0",
"husky": "^4.3.0", "husky": "^4.3.0",
"ppppp-caps": "github:staltz/ppppp-caps", "ppppp-caps": "github:staltz/ppppp-caps",
"ppppp-db": "file:../db", "ppppp-db": "github:staltz/ppppp-db",
"ppppp-keypair": "github:staltz/ppppp-keypair", "ppppp-keypair": "github:staltz/ppppp-keypair",
"prettier": "^2.6.2", "prettier": "^2.6.2",
"pretty-quick": "^3.1.3", "pretty-quick": "^3.1.3",
"rimraf": "^5.0.1", "rimraf": "^5.0.1",
"secret-handshake-ext": "~0.0.8", "secret-handshake-ext": "~0.0.8",
"secret-stack": "ssbc/secret-stack#bare-mode", "secret-stack": "~7.1.0",
"typescript": "^5.0.2" "typescript": "^5.0.2"
}, },
"scripts": { "scripts": {

View File

@ -67,16 +67,16 @@ test('follow()', async (t) => {
await p(local.close)() await p(local.close)()
}) })
test('identityAdd()', async (t) => { test('accountAdd()', async (t) => {
const { local, path, keypair } = setup() const { local, path, keypair } = setup()
assert.rejects(() => p(local.promise.identityAdd)('randomnottoken', {})) assert.rejects(() => p(local.promise.accountAdd)('randomnottoken', {}))
const identity = await p(local.db.identity.findOrCreate)({ const account = await p(local.db.account.findOrCreate)({
domain: 'account', domain: 'account',
}) })
const promise = { type: 'identity-add', identity } const promise = { type: 'account-add', account }
const token = await p(local.promise.create)(promise) const token = await p(local.promise.create)(promise)
const file = Path.join(path, 'promises.json') const file = Path.join(path, 'promises.json')
@ -92,8 +92,8 @@ test('identityAdd()', async (t) => {
assert(dbBefore[0].add.nonce) assert(dbBefore[0].add.nonce)
const keypair2 = Keypair.generate('ed25519', 'bob') const keypair2 = Keypair.generate('ed25519', 'bob')
const consent = local.db.identity.consent({ identity, keypair: keypair2 }) const consent = local.db.account.consent({ account, keypair: keypair2 })
const result1 = await p(local.promise.identityAdd)(token, { const result1 = await p(local.promise.accountAdd)(token, {
key: { key: {
purpose: 'sig', purpose: 'sig',
algorithm: 'ed25519', algorithm: 'ed25519',
@ -119,7 +119,7 @@ test('identityAdd()', async (t) => {
const contentsAfter = fs.readFileSync(file, 'utf-8') const contentsAfter = fs.readFileSync(file, 'utf-8')
assert.strictEqual(contentsAfter, '[]') assert.strictEqual(contentsAfter, '[]')
assert.rejects(() => p(local.promise.identityAdd)(token, {})) assert.rejects(() => p(local.promise.accountAdd)(token, {}))
await p(local.close)() await p(local.close)()
}) })