Last active
October 10, 2019 10:59
-
-
Save davidhq/711ebf2b9ee79188870aa77fd2ac024d to your computer and use it in GitHub Desktop.
batman hooks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Batman shooting dmt-fibers with a grapple gun | |
// | |
// _____ _____ | |
// ,-'``_.-'` \ / `'-._``'-. | |
// ,` .' |`-'| `. `. | |
// ,` ( /\ | | /\ ) `. | |
// / `--' `-' `-' `--' \ | |
// | | | |
// \ .--. ,--. ,--. ,--. / | |
// `. ( \/ lt.\ / \/ ) ,' | |
// `._ `--.___ V ___.--' _,' | |
// `'----'` `'----'` | |
// | |
// | |
// ------------------------------------------------------------------------------------------- | |
// | |
// | |
// +-----------------------------------+ | |
// | | | |
// | dmt-node | | |
// | | | |
// | | | |
// | | | |
// | | +--------------------------------+ | |
// | | | | | |
// +-------------- +-------+ | dmt-node | | |
// | | | x x | | | | |
// | FiberServer | | FCs | | | | |
// | | | x x | | | | |
// +-------------+-------------+-----+-+ +-------------+ +------+ | |
// | | | |x | | |
// | | FiberServer | | FCs | | |
// +-------------------->+ | |x | | |
// FCs = (one or many) FiberClients ^ +-------------+------------------+ | |
// DMT FIBER | | |
// | | |
// grappling hook | |
// | |
// | |
const colors = require('colors'); | |
const dmt = require('dmt-bridge'); | |
const { def, log } = dmt; | |
const { FasterixApi } = require('dmt-fasterix'); | |
function connectKnownDevice({ address, fiberClient }) { | |
if (address.startsWith('@')) { | |
const deviceName = address.replace('@', ''); | |
// todo: test if we can connect to a device by its local ip and do it | |
const globalIp = dmt.getGlobalIp({ deviceName }); | |
if (globalIp && !globalIp.error) { | |
fiberClient.connect(globalIp, { mnemonic: { deviceName } }); | |
return true; | |
} | |
log.red(`${colors.cyan('dmt-fiber')} - couldn't find global ip of device: ${colors.magenta(address)}`); | |
} | |
} | |
function connectViaFasterixHandle({ address, fiberClient, fasterixApi }) { | |
if (address.startsWith('f/')) { | |
if (!fasterixApi) { | |
log.red( | |
`Cannot translate ${colors.cyan(address)} into address and pubkey because ${colors.green( | |
'commonTruthSource' | |
)} def attribute is missing, recommendation: set it as ${colors.green('https://fasterix.com')}.` | |
); | |
return true; // "has been dealt with", although not successfully - doesn't matter, continue with next | |
} | |
const handle = address.replace('f/', ''); | |
fasterixApi | |
.mapHandleToAddressAndPubkey(handle) | |
.then(({ address, pubkey }) => { | |
fiberClient.connect(address, { mnemonic: { handle } }); | |
}) | |
.catch(() => { | |
log.red( | |
`${colors.cyan('dmt-fiber')} - no keysnippet for ${colors.magenta(address)}, ${colors.gray( | |
`handle ${colors.cyan(handle)} is probably not registered yet and could be available!` | |
)}` | |
); | |
}); | |
return true; | |
} | |
} | |
function connectFibers({ program, fiberClient }) { | |
const { device } = program; | |
let fasterixApi; | |
if (dmt.commonTruthSource()) { | |
fasterixApi = new FasterixApi(dmt.commonTruthSource()); | |
} | |
def.listify(device.try('fiber.connect')).forEach(connectInfo => { | |
const address = connectInfo.id; | |
if (!connectKnownDevice({ address, fiberClient })) { | |
if (!connectViaFasterixHandle({ address, fiberClient, fasterixApi })) { | |
fiberClient.connect(address, { mnemonic: { userProvided: connectInfo.mnemonic } }); | |
} | |
} | |
}); | |
} | |
module.exports = connectFibers; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment