From 3e4fe864f7e139aa17815e9d352ab3167b79f0aa Mon Sep 17 00:00:00 2001 From: Andre Staltz Date: Thu, 16 Nov 2023 14:09:55 +0200 Subject: [PATCH] align impl and spec regarding account key purposes --- lib/index.js | 3 ++- lib/msg-v3/index.js | 18 +++++++++++------- protospec.md | 14 +++++++------- test/account-create.test.js | 4 ++-- test/add.test.js | 2 +- test/msg-v3/create.test.js | 20 ++++++++++---------- 6 files changed, 33 insertions(+), 28 deletions(-) diff --git a/lib/index.js b/lib/index.js index a0c9911..e6b14af 100644 --- a/lib/index.js +++ b/lib/index.js @@ -311,7 +311,8 @@ function initDB(peer, config) { /** @type {AccountData} */ const data = msg.data if (data.action !== 'add') continue - if (data.key.purpose !== 'sig') continue + const purpose = data.key?.purpose + if (purpose !== 'sig' && purpose !== 'shs-and-sig') continue if (data.key.algorithm !== 'ed25519') continue pubkeys.add(data.key.bytes) } diff --git a/lib/msg-v3/index.js b/lib/msg-v3/index.js index 8ba58c4..6363068 100644 --- a/lib/msg-v3/index.js +++ b/lib/msg-v3/index.js @@ -61,21 +61,25 @@ const { isEmptyObject } = require('./util') * * @typedef {AccountAdd | AccountDel} AccountData * - * @typedef {'add' | 'del' | 'box'} AccountPower + * @typedef {'add' | 'del' | 'internal-encryption' | 'external-encryption'} AccountPower * * @typedef {{ + * purpose: 'shs-and-sig'; + * algorithm: 'ed25519'; + * bytes: string; + * }} ShsAndSigKey + * @typedef {{ * purpose: 'sig'; * algorithm: 'ed25519'; * bytes: string; * }} SigKey - * * @typedef {{ - * purpose: 'box'; + * purpose: 'external-encryption'; * algorithm: 'x25519-xsalsa20-poly1305'; * bytes: string; - * }} BoxKey; + * }} ExternalEncryptionKey; * - * @typedef {SigKey | BoxKey} AccountKey + * @typedef {ShsAndSigKey | SigKey | ExternalEncryptionKey} AccountKey * * @typedef {{ * action: 'add', @@ -234,12 +238,12 @@ function createAccount(keypair, domain, nonce = getRandomNonce) { const data = { action: 'add', key: { - purpose: 'sig', + purpose: 'shs-and-sig', algorithm: 'ed25519', bytes: keypair.public, }, nonce: typeof nonce === 'function' ? nonce() : nonce, - powers: ['add', 'del', 'box'], + powers: ['add', 'del', 'external-encryption', 'internal-encryption'], } return create({ diff --git a/protospec.md b/protospec.md index d455bb3..808d31d 100644 --- a/protospec.md +++ b/protospec.md @@ -70,10 +70,10 @@ interface Msg { type AccountData = AccountAdd | AccountDel -// "add" means this shs peer can validly add more keys to the account tangle -// "del" means this shs peer can validly revoke keys from the account tangle -// "internal-encryption" means this shs peer should get access to symmetric key -// "external-encryption" means this shs peer should get access to asymmetric key +// (if key is sig) "add" means this key can validly add more keys to the account +// (if key is sig) "del" means this key can validly revoke keys from the account +// (if key is shs) "internal-encryption" means this peer can get symmetric key +// (if key is shs) "external-encryption" means this peer can get asymmetric key type AccountPower = 'add' | 'del' | 'internal-encryption' | 'external-encryption' type AccountAdd = { @@ -91,7 +91,7 @@ type AccountDel = { type Key = | { - purpose: 'shs-and-external-signature' // secret-handshake and digital signatures + purpose: 'shs-and-sig' // secret-handshake and digital signatures algorithm: 'ed25519' // libsodium crypto_sign_detached bytes: string // base58 encoded string for the public key } @@ -101,8 +101,8 @@ type Key = bytes: string // base58 encoded string of the public key } | { - purpose: 'internal-signature', // digital signatures of internal msgs - algorithm: 'ed25519', // libsodium crypto_sign_detached + purpose: 'sig' // secret-handshake and digital signatures + algorithm: 'ed25519' // libsodium crypto_sign_detached bytes: string // base58 encoded string for the public key } ``` diff --git a/test/account-create.test.js b/test/account-create.test.js index 82fa663..7b06ae8 100644 --- a/test/account-create.test.js +++ b/test/account-create.test.js @@ -31,12 +31,12 @@ test('account.create() ', async (t) => { { action: 'add', key: { - purpose: 'sig', + purpose: 'shs-and-sig', algorithm: 'ed25519', bytes: keypair.public, }, nonce: 'MYNONCE', - powers: ['add', 'del', 'box'], + powers: ['add', 'del', 'external-encryption', 'internal-encryption'], }, 'msg.data' ) diff --git a/test/add.test.js b/test/add.test.js index 5c20375..9c263f4 100644 --- a/test/add.test.js +++ b/test/add.test.js @@ -50,7 +50,7 @@ test('add()', async (t) => { await p(peer.db._getLog().onDrain)() const stats = await p(peer.db.logStats)() - assert.deepEqual(stats, { totalBytes: 897, deletedBytes: 0 }) + assert.deepEqual(stats, { totalBytes: 943, deletedBytes: 0 }) await p(peer.close)(true) }) diff --git a/test/msg-v3/create.test.js b/test/msg-v3/create.test.js index 9001e08..a5698b8 100644 --- a/test/msg-v3/create.test.js +++ b/test/msg-v3/create.test.js @@ -15,17 +15,17 @@ test('MsgV3.createAccount()', (t) => { { action: 'add', key: { - purpose: 'sig', + purpose: 'shs-and-sig', algorithm: 'ed25519', bytes: keypair.public, }, nonce: 'MYNONCE', - powers: ['add', 'del', 'box'], + powers: ['add', 'del', 'external-encryption', 'internal-encryption'], }, 'data' ) - assert.equal(accountMsg0.metadata.dataHash, 'DQCPxgzni6UTZ5DSCms9Y', 'hash') - assert.equal(accountMsg0.metadata.dataSize, 164, 'size') + assert.equal(accountMsg0.metadata.dataHash, 'NxJZecVcVUWmUkk6cAn9JV', 'hash') + assert.equal(accountMsg0.metadata.dataSize, 210, 'size') assert.equal(accountMsg0.metadata.account, 'self', 'account') assert.equal(accountMsg0.metadata.accountTips, null, 'accountTips') assert.deepEqual(accountMsg0.metadata.tangles, {}, 'tangles') @@ -34,7 +34,7 @@ test('MsgV3.createAccount()', (t) => { assert.equal(accountMsg0.pubkey, keypair.public, 'pubkey') account = MsgV3.getMsgID(accountMsg0) - assert.equal(account, 'Hx9Fuitrg3WQCCcBaPqpeo', 'account ID') + assert.equal(account, 'UQN1Qmxr4rr9nCMQKs9u8P', 'account ID') }) let moot = null @@ -56,7 +56,7 @@ test('MsgV3.createMoot()', (t) => { assert.equal(moot.pubkey, keypair.public, 'pubkey') mootID = MsgV3.getMsgID(moot) - assert.equal(mootID, 'YYrum2aUPGLarrVnjM5o93', 'moot ID') + assert.equal(mootID, 'AP2rJSfm9TwpNcMmbUsnRa', 'moot ID') }) test('MsgV3.create()', (t) => { @@ -116,11 +116,11 @@ test('MsgV3.create()', (t) => { ) assert.equal( msg1.sig, - '5wrhPju22NHuq1qFK9qMrNafUMAhCHnLurGfASCVhPTjQTVQE4SqdV9G3zmUTesxFmynn7a1P6nJFgfvWGuSw86h', + 'rh8bc8QY7ju7yi4rt6y9njCyS3TVV1SBjn5dWGpKKRrC3XDMBc9KeNJgVCJLK8b8uiU5F49avAWt35P9kNaWZYH', 'sig' ) - const msgID1 = '7qfYPwQ1qYHYHLSXzGQCCy' + const msgID1 = 'MUvfNDk3gMPRy9CpTDEuvW' assert.equal(MsgV3.getMsgID(msg1), msgID1, 'getMsgID') @@ -180,11 +180,11 @@ test('MsgV3.create()', (t) => { ) assert.equal( msg2.sig, - '2xsdFCPsUzmaGzoQaANJSJHkCAZt3qyVUDW88RBV3r1PCspzU3BbKdQxHoxKYKcwLrpxxi4cSd5eyfcEt3DV61ge', + '3NscyRLJZP8mtq4DhhNPfwtw8yzoWsFytxGxD2QAqjW64RMeRLP5czN5mMYm4nCqRtXvzRgRhqgN1qtz9hWW14S4', 'sig' ) - assert.deepEqual(MsgV3.getMsgID(msg2), '38GT4SxtEqfZffkCrNYLtY', 'getMsgID') + assert.deepEqual(MsgV3.getMsgID(msg2), 'XMeQ6sbW3mjLYRLR4dAmKD', 'getMsgID') }) test('MsgV3.create() handles DAG tips correctly', (t) => {