Skip to content

Commit

Permalink
Merge pull request #67 from ssbc/box2-pickWrite
Browse files Browse the repository at this point in the history
Update box2 and adapt to new get format
  • Loading branch information
Powersource authored Mar 12, 2023
2 parents 3b04c72 + 54cb105 commit 495519f
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 34 deletions.
37 changes: 23 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const {
validator: {
group: { addMember: isAddMember, content: isContent },
},
keySchemes,
} = require('private-group-spec')
const { fromMessageSigil, isBendyButtV1FeedSSBURI } = require('ssb-uri2')
const buildGroupId = require('./lib/build-group-id')
Expand Down Expand Up @@ -62,13 +63,19 @@ module.exports = {
groupInitMsg,
groupKey: secret.toBuffer(),
}),
secret: secret.toBuffer(),
writeKey: {
key: secret.toBuffer(),
scheme: keySchemes.private_group,
},
readKeys: [
{ key: secret.toBuffer(), scheme: keySchemes.private_group },
],
root: fromMessageSigil(groupInitMsg.key),
subfeed: groupFeed.keys,
}

ssb.box2.addGroupInfo(data.id, {
key: data.secret,
key: data.writeKey.key,
root: data.root,
})

Expand All @@ -85,15 +92,14 @@ module.exports = {
function get(id, cb) {
if (cb === undefined) return promisify(get)(id)

ssb.box2.getGroupKeyInfo(id, (err, info) => {
ssb.box2.getGroupInfo(id, (err, info) => {
if (err) return cb(clarify(err, 'Failed to get group details'))

if (!info) return cb(new Error(`Couldn't find group with id ${id}`))

cb(null, {
...info,
id,
secret: info.key,
root: info.root,
})
})
}
Expand All @@ -116,7 +122,7 @@ module.exports = {
return cb(new Error('addMembers only supports bendybutt-v1 feed IDs'))
}

get(groupId, (err, { secret, root }) => {
get(groupId, (err, { writeKey, root }) => {
// prettier-ignore
if (err) return cb(clarify(err, `Failed to get group details when adding members`))

Expand All @@ -127,7 +133,7 @@ module.exports = {
const content = {
type: 'group/add-member',
version: 'v2',
groupKey: secret.toString('base64'),
groupKey: writeKey.key.toString('base64'),
root,
creator: rootAuthorId,
recps: [groupId, ...feedIds],
Expand Down Expand Up @@ -170,11 +176,11 @@ module.exports = {

if (!isContent(content)) return cb(new Error(isContent.errorsString))

get(groupId, (err, { secret }) => {
get(groupId, (err, { writeKey }) => {
// prettier-ignore
if (err) return cb(clarify(err, 'Failed to get group details when publishing to a group'))

findOrCreateGroupFeed(secret, (err, groupFeed) => {
findOrCreateGroupFeed(writeKey.key, (err, groupFeed) => {
// prettier-ignore
if (err) return cb(clarify(err, 'Failed to find or create group feed when publishing to a group'))

Expand Down Expand Up @@ -232,12 +238,15 @@ module.exports = {
)
),
pull.map((msg) => {
const key = Buffer.from(
lodashGet(msg, 'value.content.groupKey'),
'base64'
)
const scheme = keySchemes.private_group
return {
id: lodashGet(msg, 'value.content.recps[0]'),
secret: Buffer.from(
lodashGet(msg, 'value.content.groupKey'),
'base64'
),
writeKey: { key, scheme },
readKeys: [{ key, scheme }],
root: lodashGet(msg, 'value.content.root'),
}
})
Expand Down Expand Up @@ -266,7 +275,7 @@ module.exports = {
ssb.box2.addGroupInfo(
groupInfo.id,
{
key: groupInfo.secret,
key: groupInfo.writeKey.key,
root: groupInfo.root,
},
(err) => {
Expand Down
2 changes: 1 addition & 1 deletion lib/get-tangle.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ module.exports = function GetTangle(server, tangle) {
return cb(new Error(`get-tangle expects valid groupId, got: ${groupId}`))
}

server.box2.getGroupKeyInfo(groupId, (err, info) => {
server.box2.getGroupInfo(groupId, (err, info) => {
// prettier-ignore
if (err) return cb(clarify(err, 'Failed to get group key info when getting a tangle'))

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"pull-paramap": "^1.2.2",
"pull-stream": "^3.7.0",
"ssb-bfe": "^3.7.0",
"ssb-box2": "^5.0.0",
"ssb-box2": "^6.0.0",
"ssb-crut": "^4.6.1",
"ssb-db2": "^6.3.0",
"ssb-meta-feeds": "^0.39.0",
Expand Down
8 changes: 4 additions & 4 deletions test/add-member.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ test('get added to a group', async (t) => {

const {
id: groupId,
secret,
writeKey,
root,
} = await alice.tribes2.create().catch((err) => {
console.error('alice failed to create group', err)
Expand Down Expand Up @@ -67,8 +67,8 @@ test('get added to a group', async (t) => {
pull.collect((err, bobList) => {
t.equal(bobList.length, 1, 'bob is a member of a group now')
const group = bobList[0]
t.equal(group.id, groupId)
t.true(group.secret.equals(secret))
t.equal(group.id, groupId, 'group id is correct')
t.true(group.writeKey.key.equals(writeKey.key))
t.equal(group.root, root)
res()
})
Expand Down Expand Up @@ -123,7 +123,7 @@ test('add member', async (t) => {
const expected = {
type: 'group/add-member',
version: 'v2',
groupKey: group.secret.toString('base64'),
groupKey: group.writeKey.key.toString('base64'),
root: group.root,
creator: kaitiakiRoot.id,

Expand Down
11 changes: 6 additions & 5 deletions test/create.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ const Testbot = require('./helpers/testbot')
test('create', async (t) => {
const ssb = Testbot()

const { id, subfeed, secret, root } = await ssb.tribes2
const { id, subfeed, writeKey, readKeys, root } = await ssb.tribes2
.create()
.catch(t.error)

t.true(isIdentityGroupSSBURI(id), 'has group id')
t.true(Buffer.isBuffer(secret), 'has secret')
t.true(Buffer.isBuffer(writeKey.key), 'has writeKey')
t.true(Buffer.isBuffer(readKeys[0].key), 'has readKey')
t.true(isClassicMessageSSBURI(root), 'has root')
t.true(Ref.isFeed(subfeed.id), 'has subfeed')

Expand All @@ -37,7 +38,7 @@ test('create more', async (t) => {

t.true(isIdentityGroupSSBURI(group.id), 'returns group identifier - groupId')
t.true(
Buffer.isBuffer(group.secret) && group.secret.length === 32,
Buffer.isBuffer(group.writeKey.key) && group.writeKey.key.length === 32,
'returns group symmetric key - groupKey'
)

Expand All @@ -50,7 +51,7 @@ test('create more', async (t) => {
{
type: 'group/init',
version: 'v2',
groupKey: group.secret.toString('base64'),
groupKey: group.writeKey.key.toString('base64'),
tangles: {
group: { root: null, previous: null },
members: { root: null, previous: null },
Expand All @@ -69,7 +70,7 @@ test('create more', async (t) => {
{
type: 'group/add-member',
version: 'v2',
groupKey: group.secret.toString('base64'),
groupKey: group.writeKey.key.toString('base64'),
creator: rootFeed.id,
root: group.root,
recps: [group.id, root.id], // me being added to the group
Expand Down
6 changes: 5 additions & 1 deletion test/invites.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ test('lists correct group invite and accepting actually does something', async (

const invite = invites[0]
t.equal(invite.id, group.id, 'correct group id in invite')
t.true(invite.secret.equals(group.secret), 'correct secret')
t.true(invite.writeKey.key.equals(group.writeKey.key), 'correct writeKey')
t.true(
invite.readKeys[0].key.equals(group.readKeys[0].key),
'correct readKey'
)
t.equal(invite.root, group.root, 'correct root')

const msgEnc = await p(bob.db.get)(group.root).catch(t.fail)
Expand Down
20 changes: 12 additions & 8 deletions test/list-and-get.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ test('tribes.list + tribes.get', (t) => {
t.error(err, 'no error')

const expectedGroup = {
secret: group.secret,
root: group.root,
id: group.id,
writeKey: group.writeKey,
readKeys: group.readKeys,
root: group.root,
}

t.deepEqual(list, [expectedGroup], 'lists group ids')
Expand Down Expand Up @@ -63,14 +64,17 @@ test('tribes.list + tribes.get', (t) => {
test('get', async (t) => {
const ssb = Testbot()

const { id, secret, root } = await ssb.tribes2.create().catch(t.error)
const { id, writeKey, readKeys, root } = await ssb.tribes2
.create()
.catch(t.error)

const group = await ssb.tribes2.get(id)

t.equal(id, group.id)
t.true(isIdentityGroupSSBURI(group.id))
t.true(Buffer.isBuffer(group.secret))
t.equal(secret, group.secret)
t.true(Buffer.isBuffer(group.writeKey.key), 'writeKey has key buffer')
t.equal(writeKey.key, group.writeKey.key)
t.equal(readKeys[0].key, group.readKeys[0].key)
t.true(isClassicMessageSSBURI(group.root), 'has root')
t.equal(root, group.root)

Expand All @@ -82,7 +86,7 @@ test('list', (t) => {

ssb.tribes2
.create()
.then(({ id: id1, secret: secret1 }) => {
.then(({ id: id1, writeKey: writeKey1 }) => {
t.true(isIdentityGroupSSBURI(id1), 'has id')

pull(
Expand All @@ -101,7 +105,7 @@ test('list', (t) => {

t.equal(groups2.length, 2)
t.equal(groups2[0].id, id1)
t.equal(groups2[0].secret, secret1)
t.equal(groups2[0].writeKey.key, writeKey1.key)
t.equal(groups2[1].id, id2)

ssb.close(true, t.end)
Expand Down Expand Up @@ -173,7 +177,7 @@ test('live list groups', async (t) => {
t.equal(groups.length, 1, 'bob now finds the group in the group list')
t.equal(groups[0].id, group.id, 'id matches')
t.equal(groups[0].root, group.root, 'root matches')
t.true(groups[0].secret.equals(group.secret), 'secret matches')
t.true(groups[0].writeKey.key.equals(group.writeKey.key), 'secret matches')

await p(alice.close)(true)
await p(bob.close)(true)
Expand Down
5 changes: 5 additions & 0 deletions test/prune-publish.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ test('publish many messages that might need pruning', (t) => {
const publishArray = new Array(n).fill().map((item, i) => i)

ssb.tribes2.create(null, (err, group) => {
if (err) t.fail(err)

const publishes = publishArray.map(
(value) =>
new Promise((res, rej) => {
Expand All @@ -78,11 +80,14 @@ test('publish many messages that might need pruning', (t) => {
})
)

//console.log('publishing', n)
//console.time('publish')
Promise.all(publishes)
.then(async () => {
//console.timeEnd('publish')

t.pass('published all the messages')

ssb.close(true, t.end)
})
.catch(t.error)
Expand Down

0 comments on commit 495519f

Please sign in to comment.