diff --git a/lib/index.js b/lib/index.js index c2d4e2f..32b414a 100644 --- a/lib/index.js +++ b/lib/index.js @@ -25,16 +25,16 @@ const p = require('promisify-tuple') * @typedef {`tunnel-connect/${string}/${string}`} TunnelConnectCommandStr * @typedef {{ * type: 'promise.follow', - * issuerID: string, + * issuer: ['account' | 'pubkey', string], * token: string, * }} PromiseFollowCommand - * @typedef {`promise.follow/account.${string}/${string}`} PromiseFollowCommandStr + * @typedef {`promise.follow/${string}/${'account' | 'pubkey'}.${string}`} PromiseFollowCommandStr * @typedef {{ * type: 'promise.account-add', - * issuerID: string, + * issuer: ['account' | 'pubkey', string], * token: string, * }} PromiseAccountAddCommand - * @typedef {`promise.account-add/account.${string}/${string}`} PromiseAccountAddCommandStr + * @typedef {`promise.account-add/${string}/${'account' | 'pubkey'}.${string}`} PromiseAccountAddCommandStr * @typedef {| JoinCommand * | FollowCommand * | TunnelConnectCommand @@ -152,24 +152,28 @@ function parseTunnelConnectCommand(pieces, uri) { * @returns {PromiseFollowCommand} */ function parsePromiseFollowCommand(pieces, uri) { - const [label, issuerAndType, token] = pieces - if (!issuerAndType) { - // prettier-ignore - throw new Error(`Invalid URI "${uri}" for invite.parse, missing promise.follow issuer`) - } + const [label, token, issuerAndType] = pieces if (!token) { // prettier-ignore throw new Error(`Invalid URI "${uri}" for invite.parse, missing promise.follow token`) } + if (!issuerAndType) { + // prettier-ignore + throw new Error(`Invalid URI "${uri}" for invite.parse, missing promise.follow issuer`) + } pieces.shift() pieces.shift() pieces.shift() const [issuerType, issuerID] = issuerAndType.split('.') - if (issuerType !== 'account') { + if (issuerType !== 'pubkey') { // prettier-ignore throw new Error(`Invalid URI "${uri}" for invite.parse, invalid promise.follow issuer type "${issuerType}"`) } - return { type: 'promise.follow', issuerID, token } + return { + type: 'promise.follow', + issuer: [issuerType, issuerID], + token, + } } /** @@ -178,24 +182,28 @@ function parsePromiseFollowCommand(pieces, uri) { * @returns {PromiseAccountAddCommand} */ function parsePromiseAccountAddCommand(pieces, uri) { - const [label, issuerAndType, token] = pieces - if (!issuerAndType) { - // prettier-ignore - throw new Error(`Invalid URI "${uri}" for invite.parse, missing promise.account-add issuer`) - } + const [label, token, issuerAndType] = pieces if (!token) { // prettier-ignore throw new Error(`Invalid URI "${uri}" for invite.parse, missing promise.account-add token`) } + if (!issuerAndType) { + // prettier-ignore + throw new Error(`Invalid URI "${uri}" for invite.parse, missing promise.account-add issuer`) + } pieces.shift() pieces.shift() pieces.shift() const [issuerType, issuerID] = issuerAndType.split('.') - if (issuerType !== 'account') { + if (issuerType !== 'pubkey') { // prettier-ignore throw new Error(`Invalid URI "${uri}" for invite.parse, invalid promise.account-add issuer type "${issuerType}"`) } - return { type: 'promise.account-add', issuerID, token } + return { + type: 'promise.account-add', + issuer: [issuerType, issuerID], + token, + } } /** @@ -372,7 +380,7 @@ function initInvite(peer, config) { }) if (err2) return cb(err2) /** @type {PromiseFollowCommandStr} */ - const promiseCommand = `promise.follow/account.${opts.id}/${token}` + const promiseCommand = `promise.follow/${token}/pubkey.${peer.shse.pubkey}` // Create follow command /** @type {FollowCommandStr} */ @@ -421,7 +429,7 @@ function initInvite(peer, config) { const [err3, token] = await p(peer.promise.create)(promise) if (err3) return cb(err3) /** @type {PromiseAccountAddCommandStr} */ - const promiseCommand = `promise.account-add/account.${opts.id}/${token}` + const promiseCommand = `promise.account-add/${token}/pubkey.${peer.shse.pubkey}` // Create tunnel-connect command /** @type {TunnelConnectCommandStr} */ diff --git a/test/createForFriend.test.js b/test/createForFriend.test.js index 5227c34..a4840db 100644 --- a/test/createForFriend.test.js +++ b/test/createForFriend.test.js @@ -89,11 +89,11 @@ test('createForFriend()', async (t) => { }) assert.equal( uri, - `ppppp://invite/join/dns/example.com/tcp/8008/shse/HUB_PUBKEY.MOCK_TOKEN/follow/MOCK_ID/promise.follow/account.MOCK_ID/MOCK_PROMISE` + `ppppp://invite/join/dns/example.com/tcp/8008/shse/HUB_PUBKEY.MOCK_TOKEN/follow/MOCK_ID/promise.follow/MOCK_PROMISE/pubkey.${keypair.public}` ) assert.equal( url, - `https://example.com/invite#ppppp%3A%2F%2Finvite%2Fjoin%2Fdns%2Fexample.com%2Ftcp%2F8008%2Fshse%2FHUB_PUBKEY.MOCK_TOKEN%2Ffollow%2FMOCK_ID%2Fpromise.follow%2Faccount.MOCK_ID%2FMOCK_PROMISE` + `https://example.com/invite#ppppp%3A%2F%2Finvite%2Fjoin%2Fdns%2Fexample.com%2Ftcp%2F8008%2Fshse%2FHUB_PUBKEY.MOCK_TOKEN%2Ffollow%2FMOCK_ID%2Fpromise.follow%2FMOCK_PROMISE%2Fpubkey.${keypair.public}` ) assert.ok(connectCalled) diff --git a/test/createForMyself.test.js b/test/createForMyself.test.js index 2fbff45..be7b5ad 100644 --- a/test/createForMyself.test.js +++ b/test/createForMyself.test.js @@ -87,11 +87,11 @@ test('createForMyself()', async (t) => { }) assert.equal( uri, - `ppppp://invite/join/dns/example.com/tcp/8008/shse/HUB_PUBKEY.MOCK_TOKEN/tunnel-connect/HUB_PUBKEY/${local.shse.pubkey}/promise.account-add/account.MOCK_ID/MOCK_PROMISE` + `ppppp://invite/join/dns/example.com/tcp/8008/shse/HUB_PUBKEY.MOCK_TOKEN/tunnel-connect/HUB_PUBKEY/${local.shse.pubkey}/promise.account-add/MOCK_PROMISE/pubkey.${keypair.public}` ) assert.equal( url, - `https://example.com/invite#ppppp%3A%2F%2Finvite%2Fjoin%2Fdns%2Fexample.com%2Ftcp%2F8008%2Fshse%2FHUB_PUBKEY.MOCK_TOKEN%2Ftunnel-connect%2FHUB_PUBKEY%2F${local.shse.pubkey}%2Fpromise.account-add%2Faccount.MOCK_ID%2FMOCK_PROMISE` + `https://example.com/invite#ppppp%3A%2F%2Finvite%2Fjoin%2Fdns%2Fexample.com%2Ftcp%2F8008%2Fshse%2FHUB_PUBKEY.MOCK_TOKEN%2Ftunnel-connect%2FHUB_PUBKEY%2F${local.shse.pubkey}%2Fpromise.account-add%2FMOCK_PROMISE%2Fpubkey.${keypair.public}` ) assert.ok(connectCalled) diff --git a/test/parse.test.js b/test/parse.test.js index 7892131..a5e3ba9 100644 --- a/test/parse.test.js +++ b/test/parse.test.js @@ -16,7 +16,7 @@ test('parse() error cases', (t) => { test('parse() good friend invite', (t) => { const commands = plugin.parse( - 'ppppp://invite/join/dns/example.com/tcp/8080/shse/PUBKEY.TOKEN/follow/ALICE/promise.follow/account.ALICE/ALICE_TOKEN' + 'ppppp://invite/join/dns/example.com/tcp/8080/shse/PUBKEY.TOKEN/follow/ALICE/promise.follow/ALICE_TOKEN/pubkey.ALICE' ) assert.deepEqual(commands, [ { @@ -29,7 +29,7 @@ test('parse() good friend invite', (t) => { }, { type: 'promise.follow', - issuerID: 'ALICE', + issuer: ['pubkey', 'ALICE'], token: 'ALICE_TOKEN', }, ]) @@ -37,7 +37,7 @@ test('parse() good friend invite', (t) => { test('parse() good myself invite', (t) => { const commands = plugin.parse( - 'ppppp://invite/join/dns/example.com/tcp/8080/shse/PUBKEY.TOKEN/tunnel-connect/HUB_PUBKEY/OLD_PUBKEY/promise.account-add/account.ACCOUNT_ID/OLD_TOKEN' + 'ppppp://invite/join/dns/example.com/tcp/8080/shse/PUBKEY.TOKEN/tunnel-connect/HUB_PUBKEY/OLD_PUBKEY/promise.account-add/OLD_TOKEN/pubkey.PUBKEY' ) assert.deepEqual(commands, [ { @@ -50,7 +50,7 @@ test('parse() good myself invite', (t) => { }, { type: 'promise.account-add', - issuerID: 'ACCOUNT_ID', + issuer: ['pubkey', 'PUBKEY'], token: 'OLD_TOKEN', }, ])