mirror of https://codeberg.org/pzp/pzp-promise.git
rename identity to account
This commit is contained in:
parent
9e98e36752
commit
207f6a4692
38
lib/index.js
38
lib/index.js
|
@ -6,7 +6,7 @@ const bs58 = require('bs58')
|
|||
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: 'identity-add', identity: string}} IdentityAddPromise
|
||||
* @typedef {FollowPromise | IdentityAddPromise} PPromise
|
||||
* @typedef {{type: 'account-add', account: string}} AccountAddPromise
|
||||
* @typedef {FollowPromise | AccountAddPromise} PPromise
|
||||
*/
|
||||
|
||||
const FILENAME = 'promises.json'
|
||||
|
@ -34,11 +34,11 @@ module.exports = {
|
|||
revoke: 'async',
|
||||
// promises
|
||||
follow: 'async',
|
||||
identityAdd: 'async',
|
||||
accountAdd: 'async',
|
||||
},
|
||||
permissions: {
|
||||
anonymous: {
|
||||
allow: ['follow', 'identityAdd'],
|
||||
allow: ['follow', 'accountAdd'],
|
||||
},
|
||||
},
|
||||
|
||||
|
@ -85,16 +85,16 @@ module.exports = {
|
|||
if (
|
||||
typeof promise !== 'object' ||
|
||||
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))
|
||||
}
|
||||
if (
|
||||
promise.type === 'identity-add' &&
|
||||
typeof promise.identity !== 'string'
|
||||
promise.type === 'account-add' &&
|
||||
typeof promise.account !== 'string'
|
||||
) {
|
||||
// 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
|
||||
}
|
||||
|
@ -156,12 +156,12 @@ module.exports = {
|
|||
|
||||
/**
|
||||
* @param {string} token
|
||||
* @param {IdentityAdd} addition
|
||||
* @param {AccountAdd} addition
|
||||
* @param {CB<boolean>} cb
|
||||
*/
|
||||
function identityAdd(token, addition, cb) {
|
||||
function accountAdd(token, addition, cb) {
|
||||
if (!loaded) {
|
||||
setTimeout(() => identityAdd(token, addition, cb), 100)
|
||||
setTimeout(() => accountAdd(token, addition, cb), 100)
|
||||
return
|
||||
}
|
||||
if (
|
||||
|
@ -180,21 +180,21 @@ module.exports = {
|
|||
cb(new Error('Invalid token'))
|
||||
return
|
||||
}
|
||||
const promise = /** @type {IdentityAddPromise} */ (promises.get(token))
|
||||
const { type, identity } = promise
|
||||
if (type !== 'identity-add') {
|
||||
const promise = /** @type {AccountAddPromise} */ (promises.get(token))
|
||||
const { type, account } = promise
|
||||
if (type !== 'account-add') {
|
||||
cb(new Error('Invalid token'))
|
||||
return
|
||||
}
|
||||
|
||||
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)
|
||||
return
|
||||
}
|
||||
|
||||
local.db.identity.add(
|
||||
{ identity, keypair, consent: addition.consent },
|
||||
local.db.account.add(
|
||||
{ account, keypair, consent: addition.consent },
|
||||
/**
|
||||
* @param {Error | null} err
|
||||
* @param {any} rec
|
||||
|
@ -223,6 +223,6 @@ module.exports = {
|
|||
save(cb)
|
||||
}
|
||||
|
||||
return { create, revoke, follow, identityAdd }
|
||||
return { create, revoke, follow, accountAdd }
|
||||
},
|
||||
}
|
||||
|
|
|
@ -33,13 +33,13 @@
|
|||
"c8": "^7.11.0",
|
||||
"husky": "^4.3.0",
|
||||
"ppppp-caps": "github:staltz/ppppp-caps",
|
||||
"ppppp-db": "file:../db",
|
||||
"ppppp-db": "github:staltz/ppppp-db",
|
||||
"ppppp-keypair": "github:staltz/ppppp-keypair",
|
||||
"prettier": "^2.6.2",
|
||||
"pretty-quick": "^3.1.3",
|
||||
"rimraf": "^5.0.1",
|
||||
"secret-handshake-ext": "~0.0.8",
|
||||
"secret-stack": "ssbc/secret-stack#bare-mode",
|
||||
"secret-stack": "~7.1.0",
|
||||
"typescript": "^5.0.2"
|
||||
},
|
||||
"scripts": {
|
||||
|
|
|
@ -67,16 +67,16 @@ test('follow()', async (t) => {
|
|||
await p(local.close)()
|
||||
})
|
||||
|
||||
test('identityAdd()', async (t) => {
|
||||
test('accountAdd()', async (t) => {
|
||||
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',
|
||||
})
|
||||
|
||||
const promise = { type: 'identity-add', identity }
|
||||
const promise = { type: 'account-add', account }
|
||||
const token = await p(local.promise.create)(promise)
|
||||
|
||||
const file = Path.join(path, 'promises.json')
|
||||
|
@ -92,8 +92,8 @@ test('identityAdd()', async (t) => {
|
|||
assert(dbBefore[0].add.nonce)
|
||||
|
||||
const keypair2 = Keypair.generate('ed25519', 'bob')
|
||||
const consent = local.db.identity.consent({ identity, keypair: keypair2 })
|
||||
const result1 = await p(local.promise.identityAdd)(token, {
|
||||
const consent = local.db.account.consent({ account, keypair: keypair2 })
|
||||
const result1 = await p(local.promise.accountAdd)(token, {
|
||||
key: {
|
||||
purpose: 'sig',
|
||||
algorithm: 'ed25519',
|
||||
|
@ -119,7 +119,7 @@ test('identityAdd()', async (t) => {
|
|||
const contentsAfter = fs.readFileSync(file, 'utf-8')
|
||||
assert.strictEqual(contentsAfter, '[]')
|
||||
|
||||
assert.rejects(() => p(local.promise.identityAdd)(token, {}))
|
||||
assert.rejects(() => p(local.promise.accountAdd)(token, {}))
|
||||
|
||||
await p(local.close)()
|
||||
})
|
||||
|
|
Loading…
Reference in New Issue