Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add groupkey in init msg #62

Merged
merged 3 commits into from
Feb 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 39 additions & 38 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,30 +143,31 @@ module.exports = {
})
}

function secretKeyFromPurpose(purpose) {
return new SecretKey(Buffer.from(purpose, 'base64'))
function secretKeyFromString(string) {
return new SecretKey(Buffer.from(string, 'base64'))
}

function createGroupWithoutMembers(myRoot, cb) {
const content = {
type: 'group/init',
tangles: {
group: { root: null, previous: null },
},
}
if (!initSpec(content)) return cb(new Error(initSpec.errorsString))

findOrCreateGroupFeed(null, function gotGroupFeed(err, groupFeed) {
// prettier-ignore
if (err) return cb(clarify(err, 'Failed to find or create group feed when creating a group'))

const secret = secretKeyFromPurpose(groupFeed.purpose)
const secret = secretKeyFromString(groupFeed.purpose)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a big deal, but a small suggestion:

Suggested change
const secret = secretKeyFromString(groupFeed.purpose)
const groupKey = secretKeyFromString(groupFeed.purpose)

We seem to be going back and forth with this naming, "secret" or "group key", but we could try to be at least internally consistent (all namings of this thing in ssb-tribes2 are the same).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Problem here is the spec calls it groupKey and the tribes2 api says secret. I think the proper way for fixing is a separate issue/pr.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this pr is just for conforming to the spec

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay!


const recps = [
{ key: secret.toBuffer(), scheme: keySchemes.private_group },
myRoot.id,
]

const content = {
type: 'group/init',
groupKey: secret.toString('base64'),
tangles: {
group: { root: null, previous: null },
},
}
if (!initSpec(content)) return cb(new Error(initSpec.errorsString))

ssb.db.create(
{
keys: groupFeed.keys,
Expand Down Expand Up @@ -257,37 +258,37 @@ module.exports = {
function create(opts = {}, cb) {
if (cb === undefined) return promisify(create)(opts)

findOrCreateGroupWithoutMembers(
(err, { groupInitMsg, groupFeed, myRoot }) => {
// prettier-ignore
if (err) return cb(clarify(err, 'Failed to create group init message when creating a group'))

const secret = secretKeyFromPurpose(groupFeed.purpose)

const data = {
id: buildGroupId({
groupInitMsg,
groupKey: secret.toBuffer(),
}),
secret: secret.toBuffer(),
root: fromMessageSigil(groupInitMsg.key),
subfeed: groupFeed.keys,
}
findOrCreateGroupWithoutMembers((err, group) => {
// prettier-ignore
if (err) return cb(clarify(err, 'Failed to create group init message when creating a group'))

ssb.box2.addGroupInfo(data.id, {
key: data.secret,
root: data.root,
})
const { groupInitMsg, groupFeed, myRoot } = group

// Adding myself for recovery reasons
addMembers(data.id, [myRoot.id], {}, (err) => {
// prettier-ignore
if (err) return cb(clarify(err, 'Failed to add myself to the group when creating a group'))
const secret = secretKeyFromString(groupFeed.purpose)

return cb(null, data)
})
const data = {
id: buildGroupId({
groupInitMsg,
groupKey: secret.toBuffer(),
}),
secret: secret.toBuffer(),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly, could name this groupKey

root: fromMessageSigil(groupInitMsg.key),
subfeed: groupFeed.keys,
}
)

ssb.box2.addGroupInfo(data.id, {
key: data.secret,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Case in point: the key is the secret is the key is the secret.

root: data.root,
})

// Adding myself for recovery reasons
addMembers(data.id, [myRoot.id], {}, (err) => {
// prettier-ignore
if (err) return cb(clarify(err, 'Failed to add myself to the group when creating a group'))

return cb(null, data)
})
})
}

function get(id, cb) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"fast-deep-equal": "^3.1.3",
"lodash.get": "^4.4.2",
"lodash.set": "^4.3.2",
"private-group-spec": "^3.0.0",
"private-group-spec": "^4.0.0",
"pull-paramap": "^1.2.2",
"pull-stream": "^3.7.0",
"ssb-bfe": "^3.7.0",
Expand Down
1 change: 1 addition & 0 deletions test/create.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ test('create more', async (t) => {
msgVal.content,
{
type: 'group/init',
groupKey: group.secret.toString('base64'),
tangles: {
group: { root: null, previous: null },
},
Expand Down