Skip to content

Instantly share code, notes, and snippets.

@davidhq
Last active October 10, 2019 10:59
Show Gist options
  • Save davidhq/711ebf2b9ee79188870aa77fd2ac024d to your computer and use it in GitHub Desktop.
Save davidhq/711ebf2b9ee79188870aa77fd2ac024d to your computer and use it in GitHub Desktop.
batman hooks
// 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