update secret-stack to 8.1

This commit is contained in:
Andre Staltz 2024-01-08 11:53:32 +02:00
parent b08e3d7c9f
commit 149b2dd86f
No known key found for this signature in database
GPG Key ID: 9EDE23EA7E8A4890
2 changed files with 135 additions and 182 deletions

View File

@ -67,17 +67,13 @@ function parseJoinCommand(pieces, uri) {
// prettier-ignore // prettier-ignore
throw new Error(`Invalid URI "${uri}" for invite.parse, missing join hub pubkey`) throw new Error(`Invalid URI "${uri}" for invite.parse, missing join hub pubkey`)
} }
// TODO: base58 validation for the token // TODO: base58 validation for the token, if present at all
if (!token) {
// prettier-ignore
throw new Error(`Invalid URI "${uri}" for invite.parse, missing join hub token`)
}
pieces.shift() pieces.shift()
pieces.shift() pieces.shift()
pieces.shift() pieces.shift()
pieces.shift() pieces.shift()
pieces.shift() pieces.shift()
const shse = `shse:${pubkey}:${token}` const shse = token ? `shse:${pubkey}:${token}` : `shse:${pubkey}`
const address = `net:${host}:${port}~${shse}` // TODO: add ws address here const address = `net:${host}:${port}~${shse}` // TODO: add ws address here
return { type: 'join', address } return { type: 'join', address }
} }
@ -221,194 +217,151 @@ function parse(uri) {
} }
/** /**
* @param {{ shse: SHSE | null }} peer * @param {{
* @returns {asserts peer is { shse: SHSE }} * shse: SHSE;
* promise: PPPPPPromise;
* conn: ConnPlugin;
* }} peer
* @param {unknown} config
*/ */
function assertSHSEExists(peer) { function initInvite(peer, config) {
if (!peer.shse) throw new Error('"invite" plugin requires "shse" plugin') /**
} * @param {{
* hubs?: number,
* id: string,
* _hubMsAddr?: string,
* }} opts
*
* @param {CB<{uri: string, url: string}>} cb
*/
async function createForFriend(opts, cb) {
if (typeof opts !== 'object') {
return cb(new Error('invite.createForFriend is missing opts argument'))
}
if (!opts.id) {
// prettier-ignore
return cb(new Error(`invite.createForFriend opts.id is required for type "follow"`))
}
const hubs = opts.hubs ?? 1
if (typeof hubs !== 'number') {
// prettier-ignore
return cb(new Error(`invite.createForFriend opts.hubs should be a number but was ${hubs}`))
}
/** if (!opts._hubMsAddr) {
* @param {{ promise: PPPPPPromise | null }} peer // prettier-ignore
* @returns {asserts peer is { promise: PPPPPPromise }} return cb(new Error(`invite.createForFriend expected opts._hubMsAddr because loading from connDB not yet supported`))
*/ }
function assertPromisePlugin(peer) {
// prettier-ignore
if (!peer.promise) throw new Error('"invite" plugin requires "promise" plugin')
}
/** // Connect to hub and create token
* @param {{ conn: ConnPlugin | null }} peer const [err, rpc] = await p(peer.conn.connect)(opts._hubMsAddr)
* @returns {asserts peer is { conn: ConnPlugin }} if (err) return cb(err)
*/ const [err2, hubToken] = await p(rpc.hub.createToken)()
function assertConnPlugin(peer) { if (err2) return cb(err2)
if (!peer.conn) throw new Error('"invite" plugin requires "conn" plugin')
}
module.exports = { // Parse multiserver address
name: 'invite', // prettier-ignore
manifest: { const ERROR_MSG = `Invalid multiserver address ${opts._hubMsAddr} for invite.createForFriend`
createForFriend: 'async', const msAddr = MultiserverAddress.decode(opts._hubMsAddr)
createForMyself: 'async', const [netShsAddr, wsShsAddr] = msAddr
parse: 'sync', if (!netShsAddr) return cb(new Error(ERROR_MSG))
}, const [net, shse] = netShsAddr
if (!net) return cb(new Error(ERROR_MSG))
if (net.name !== 'net') return cb(new Error(ERROR_MSG))
const [host, port] = net.data
if (!shse) return cb(new Error(ERROR_MSG))
if (shse.name !== 'shse') return cb(new Error(ERROR_MSG))
const [pubkey] = shse.data
parse, // Create follow promise
const [err3, token] = await p(peer.promise.create)({ type: 'follow' })
if (err3) return cb(err3)
/** @type {JoinCommandStr} */
const joinCommand = `join/${host}/${port}/${pubkey}/${hubToken}`
/** @type {FollowCommandStr} */
const followCommand = `follow/${opts.id}`
/** @type {PromiseFollowCommandStr} */
const promiseCommand = `promise.follow/account.${opts.id}/${token}`
const uri = `ppppp://invite/${joinCommand}/${followCommand}/${promiseCommand}`
const url = `http://${host}/invite#${encodeURIComponent(uri)}`
cb(null, { uri, url })
}
/** /**
* @param {{ * @param {{
* shse: SHSE | null; * hubs?: number,
* promise: PPPPPPromise | null; * id: string,
* conn: ConnPlugin | null; * _hubMsAddr?: string,
* }} peer * }} opts
* @param {unknown} config *
* @param {CB<{uri: string, url: string}>} cb
*/ */
init(peer, config) { async function createForMyself(opts, cb) {
assertSHSEExists(peer) if (typeof opts !== 'object') {
assertPromisePlugin(peer) return cb(new Error('invite.createForMyself is missing opts argument'))
assertConnPlugin(peer) }
if (!opts.id) {
/**
* @param {{
* hubs?: number,
* id: string,
* _hubMsAddr?: string,
* }} opts
*
* @param {CB<{uri: string, url: string}>} cb
*/
async function createForFriend(opts, cb) {
try {
assertConnPlugin(peer)
assertPromisePlugin(peer)
} catch (err) {
return cb(/**@type {Error}*/ (err))
}
if (typeof opts !== 'object') {
return cb(new Error('invite.createForFriend is missing opts argument'))
}
if (!opts.id) {
// prettier-ignore
return cb(new Error(`invite.createForFriend opts.id is required for type "follow"`))
}
const hubs = opts.hubs ?? 1
if (typeof hubs !== 'number') {
// prettier-ignore
return cb(new Error(`invite.createForFriend opts.hubs should be a number but was ${hubs}`))
}
if (!opts._hubMsAddr) {
// prettier-ignore
return cb(new Error(`invite.createForFriend expected opts._hubMsAddr because loading from connDB not yet supported`))
}
// Connect to hub and create token
const [err, rpc] = await p(peer.conn.connect)(opts._hubMsAddr)
if (err) return cb(err)
const [err2, hubToken] = await p(rpc.hub.createToken)()
if (err2) return cb(err2)
// Parse multiserver address
// prettier-ignore // prettier-ignore
const ERROR_MSG = `Invalid multiserver address ${opts._hubMsAddr} for invite.createForFriend` return cb(new Error(`invite.createForMyself opts.id is required for type "follow"`))
const msAddr = MultiserverAddress.decode(opts._hubMsAddr) }
const [netShsAddr, wsShsAddr] = msAddr const hubs = opts.hubs ?? 1
if (!netShsAddr) return cb(new Error(ERROR_MSG)) if (typeof hubs !== 'number') {
const [net, shse] = netShsAddr // prettier-ignore
if (!net) return cb(new Error(ERROR_MSG)) return cb(new Error(`invite.createForMyself opts.hubs should be a number but was ${hubs}`))
if (net.name !== 'net') return cb(new Error(ERROR_MSG))
const [host, port] = net.data
if (!shse) return cb(new Error(ERROR_MSG))
if (shse.name !== 'shse') return cb(new Error(ERROR_MSG))
const [pubkey] = shse.data
// Create follow promise
const [err3, token] = await p(peer.promise.create)({ type: 'follow' })
if (err3) return cb(err3)
/** @type {JoinCommandStr} */
const joinCommand = `join/${host}/${port}/${pubkey}/${hubToken}`
/** @type {FollowCommandStr} */
const followCommand = `follow/${opts.id}`
/** @type {PromiseFollowCommandStr} */
const promiseCommand = `promise.follow/account.${opts.id}/${token}`
const uri = `ppppp://invite/${joinCommand}/${followCommand}/${promiseCommand}`
const url = `http://${host}/invite#${encodeURIComponent(uri)}`
cb(null, { uri, url })
} }
/** if (!opts._hubMsAddr) {
* @param {{
* hubs?: number,
* id: string,
* _hubMsAddr?: string,
* }} opts
*
* @param {CB<{uri: string, url: string}>} cb
*/
async function createForMyself(opts, cb) {
try {
assertSHSEExists(peer)
assertConnPlugin(peer)
assertPromisePlugin(peer)
} catch (err) {
return cb(/**@type {Error}*/ (err))
}
if (typeof opts !== 'object') {
return cb(new Error('invite.createForMyself is missing opts argument'))
}
if (!opts.id) {
// prettier-ignore
return cb(new Error(`invite.createForMyself opts.id is required for type "follow"`))
}
const hubs = opts.hubs ?? 1
if (typeof hubs !== 'number') {
// prettier-ignore
return cb(new Error(`invite.createForMyself opts.hubs should be a number but was ${hubs}`))
}
if (!opts._hubMsAddr) {
// prettier-ignore
return cb(new Error(`invite.createForMyself expected opts._hubMsAddr because loading from connDB not yet supported`))
}
// Connect to hub and create token
const [err, rpc] = await p(peer.conn.connect)(opts._hubMsAddr)
if (err) return cb(err)
const [err2, hubToken] = await p(rpc.hub.createToken)()
if (err2) return cb(err2)
// Parse multiserver address
// prettier-ignore // prettier-ignore
const ERROR_MSG = `Invalid multiserver address ${opts._hubMsAddr} for invite.createForMyself` return cb(new Error(`invite.createForMyself expected opts._hubMsAddr because loading from connDB not yet supported`))
const msAddr = MultiserverAddress.decode(opts._hubMsAddr)
const [netShsAddr, wsShsAddr] = msAddr
if (!netShsAddr) return cb(new Error(ERROR_MSG))
const [net, shse] = netShsAddr
if (!net) return cb(new Error(ERROR_MSG))
if (net.name !== 'net') return cb(new Error(ERROR_MSG))
const [host, port] = net.data
if (!shse) return cb(new Error(ERROR_MSG))
if (shse.name !== 'shse') return cb(new Error(ERROR_MSG))
const [pubkey] = shse.data
// Create account-add promise
const promise = { type: 'account-add', account: opts.id }
const [err3, token] = await p(peer.promise.create)(promise)
if (err3) return cb(err3)
/** @type {JoinCommandStr} */
const joinCommand = `join/${host}/${port}/${pubkey}/${hubToken}`
/** @type {TunnelConnectCommandStr} */
const tunnelCommand = `tunnel-connect/${pubkey}/${peer.shse.pubkey}`
/** @type {PromiseAccountAddCommandStr} */
const promiseCommand = `promise.account-add/account.${opts.id}/${token}`
const uri = `ppppp://invite/${joinCommand}/${tunnelCommand}/${promiseCommand}`
const url = `http://${host}/invite#${encodeURIComponent(uri)}`
cb(null, { uri, url })
} }
return { createForFriend, createForMyself, parse } // Connect to hub and create token
}, const [err, rpc] = await p(peer.conn.connect)(opts._hubMsAddr)
if (err) return cb(err)
const [err2, hubToken] = await p(rpc.hub.createToken)()
if (err2) return cb(err2)
// Parse multiserver address
// prettier-ignore
const ERROR_MSG = `Invalid multiserver address ${opts._hubMsAddr} for invite.createForMyself`
const msAddr = MultiserverAddress.decode(opts._hubMsAddr)
const [netShsAddr, wsShsAddr] = msAddr
if (!netShsAddr) return cb(new Error(ERROR_MSG))
const [net, shse] = netShsAddr
if (!net) return cb(new Error(ERROR_MSG))
if (net.name !== 'net') return cb(new Error(ERROR_MSG))
const [host, port] = net.data
if (!shse) return cb(new Error(ERROR_MSG))
if (shse.name !== 'shse') return cb(new Error(ERROR_MSG))
const [pubkey] = shse.data
// Create account-add promise
const promise = { type: 'account-add', account: opts.id }
const [err3, token] = await p(peer.promise.create)(promise)
if (err3) return cb(err3)
/** @type {JoinCommandStr} */
const joinCommand = `join/${host}/${port}/${pubkey}/${hubToken}`
/** @type {TunnelConnectCommandStr} */
const tunnelCommand = `tunnel-connect/${pubkey}/${peer.shse.pubkey}`
/** @type {PromiseAccountAddCommandStr} */
const promiseCommand = `promise.account-add/account.${opts.id}/${token}`
const uri = `ppppp://invite/${joinCommand}/${tunnelCommand}/${promiseCommand}`
const url = `http://${host}/invite#${encodeURIComponent(uri)}`
cb(null, { uri, url })
}
return { createForFriend, createForMyself, parse }
} }
exports.name = 'invite'
exports.needs = ['shse', 'promise', 'conn']
exports.manifest = {
createForFriend: 'async',
createForMyself: 'async',
parse: 'sync',
}
exports.init = initInvite
exports.parse = parse

View File

@ -37,7 +37,7 @@
"pretty-quick": "^3.1.3", "pretty-quick": "^3.1.3",
"rimraf": "^5.0.1", "rimraf": "^5.0.1",
"secret-handshake-ext": "0.0.11", "secret-handshake-ext": "0.0.11",
"secret-stack": "~8.0.0", "secret-stack": "~8.1.0",
"typescript": "^5.1.3" "typescript": "^5.1.3"
}, },
"scripts": { "scripts": {