Skip to content

Commit

Permalink
switch for telemetry
Browse files Browse the repository at this point in the history
  • Loading branch information
vol4tim committed Mar 9, 2024
1 parent 516b1c1 commit 8cd150a
Show file tree
Hide file tree
Showing 15 changed files with 1,800 additions and 33 deletions.
7 changes: 3 additions & 4 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
module.exports = {
presets: [
'@vue/cli-plugin-babel/preset'
]
}
presets: ["@vue/cli-plugin-babel/preset"],
plugins: ["@babel/plugin-transform-private-methods"]
};
11 changes: 10 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,31 @@
"lint": "vue-cli-service lint"
},
"dependencies": {
"@chainsafe/libp2p-noise": "^15.0.0",
"@libp2p/circuit-relay-v2": "^1.0.16",
"@libp2p/identify": "^1.0.15",
"@libp2p/mplex": "^10.0.16",
"@libp2p/webrtc": "^4.0.20",
"@libp2p/websockets": "^8.0.16",
"@polkadot/api": "^9.14.1",
"@polkadot/ui-keyring": "^2.11.1",
"axios": "^1.4.0",
"core-js": "^3.32.0",
"dayjs": "^1.11.9",
"decimal.js-light": "^2.5.1",
"ipfs-http-client": "^60.0.0",
"robonomics-interface": "^0.3.1",
"libp2p": "^1.2.4",
"robonomics-interface": "^0.3.2",
"robonomics-ui-vue": "^0.2.7",
"uuid": "^9.0.1",
"vue": "^3.4.15",
"vue-router": "^4.2.5",
"vuex": "^4.1.0"
},
"devDependencies": {
"@babel/core": "^7.23.7",
"@babel/eslint-parser": "^7.23.3",
"@babel/plugin-transform-private-methods": "^7.23.3",
"@vue/cli-plugin-babel": "^5.0.8",
"@vue/cli-plugin-eslint": "^5.0.8",
"@vue/cli-service": "^5.0.8",
Expand Down
43 changes: 43 additions & 0 deletions src/plugins/robonomicsAccountManager.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { encryptor } from "@/utils/encryptor";
import { decodePair } from "@polkadot/keyring/pair/decode";
import { u8aToHex, u8aWrapBytes } from "@polkadot/util";
import { base64Decode } from "@polkadot/util-crypto";
import { AccountManager } from "robonomics-interface";

export default class AccountManagerDapp extends AccountManager {
Expand All @@ -10,6 +13,10 @@ export default class AccountManagerDapp extends AccountManager {
}

async beforeSetSender(address, { type, extension }) {
// if (this.account && !this.account.meta.isInjected) {
// // this.keyring.forgetAccount(this.account.address);
// this.account.lock();
// }
this.extension = extension;
try {
this.keyring.getPair(address);
Expand All @@ -20,6 +27,7 @@ export default class AccountManagerDapp extends AccountManager {
}

async afterSetSender() {
await super.afterSetSender();
if (this.account.meta.isInjected && this.extension) {
this.api.setSigner(this.extension.signer);
this.account.signMsg = async (data) => {
Expand All @@ -33,4 +41,39 @@ export default class AccountManagerDapp extends AccountManager {
};
}
}

async addUri(suri, meta = {}, type = "ed25519") {
const pair = this.keyring.keyring.addFromUri(suri, meta, type);
return await this.setSender(pair.address, {
type: type
});
}

async addUriPassword(
suri,
password = undefined,
meta = {},
type = "ed25519"
) {
const { pair } = this.keyring.addUri(suri, password, meta, type);
return await this.setSender(pair.address, {
type: type
});
}

async addJsonPassword(json, password) {
const pair = this.keyring.createFromJson(json);
this.keyring.addPair(pair, password);
return await this.setSender(pair.address, {
type: pair.type
});
}

encryptor(password) {
if (!this.account.meta.isInjected && this.account.type === "ed25519") {
const json = this.account.toJson(password);
const decoded = decodePair(password, base64Decode(json.encoded));
return encryptor(decoded);
}
}
}
2 changes: 1 addition & 1 deletion src/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import RwsActivate from "./views/RwsActivate.vue";
import RwsSetup from "./views/RwsSetup.vue";
import RwsSetupNew from "./views/RwsSetupNew.vue";
import Services from "./views/Services.vue";
import Telemetry from "./views/Telemetry.vue";
import Telemetry from "./views/telemetry/Telemetry.vue";

const routes = [
{
Expand Down
25 changes: 22 additions & 3 deletions src/utils/encryptor.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,12 @@ export function encryptMessage(
);
return u8aConcat(nonce, sealed);
}
export function getPair(mnemonic) {
export function createPair(mnemonic) {
const seed = mnemonicToMiniSecret(mnemonic);
const pair = ed25519PairFromSeed(seed);
return pair;
}
export function encryptor(mnemonic) {
const pair = getPair(mnemonic);
export function encryptor(pair) {
return {
pair,
address: encodeAddress(pair.publicKey, 32),
Expand All @@ -82,3 +81,23 @@ export function encryptor(mnemonic) {
}
};
}
// export function encryptor(mnemonic) {
// const pair = createPair(mnemonic);
// return {
// pair,
// address: encodeAddress(pair.publicKey, 32),
// encodeAddress: (ss58Format = 32) => {
// return encodeAddress(pair.publicKey, ss58Format);
// },
// decryptMessage: (encryptedMessageWithNonce, senderPublicKey) => {
// return decryptMessage(
// encryptedMessageWithNonce,
// senderPublicKey,
// pair.secretKey
// );
// },
// encryptMessage: (message, recipientPublicKey, nonce) => {
// return encryptMessage(message, recipientPublicKey, pair.secretKey, nonce);
// }
// };
// }
77 changes: 77 additions & 0 deletions src/utils/libp2p/ha.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { pipe } from "it-pipe";
import { fromString as uint8ArrayFromString } from "uint8arrays/from-string";
import { toString as uint8ArrayToString } from "uint8arrays/to-string";

export function createHa() {
return (components) => {
async function getRequest(stream) {
return pipe(stream, async function (source) {
let result = "";
for await (const data of source) {
result += uint8ArrayToString(data.subarray());
}
return JSON.parse(result);
});
}

async function sendResponse(stream, msg) {
return pipe(
[uint8ArrayFromString(JSON.stringify(msg))],
stream.sink
).finally(() => {
stream.close();
});
}

return {
async handle(
protocol,
handler,
options = {
runOnTransientConnection: true
}
) {
await components.registrar.handle(
protocol,
async ({ stream }) => {
handler(await getRequest(stream), stream);
},
options
);
},
async request(
connection,
protocol,
data,
options = {
runOnTransientConnection: true
}
) {
if (connection.status !== "open") {
return;
}
const stream = await connection.newStream([protocol], options);
return pipe(
[uint8ArrayFromString(JSON.stringify(data))],
stream,
async function (source) {
let result = "";
for await (const data of source) {
result += uint8ArrayToString(data.subarray());
}
try {
// stream.close();
return JSON.parse(result);
} catch (error) {
return result;
}
}
);
},
utils: {
getRequest,
sendResponse
}
};
};
}
103 changes: 103 additions & 0 deletions src/utils/libp2p/libp2p.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import { noise } from "@chainsafe/libp2p-noise";
import { mplex } from "@libp2p/mplex";
import { webRTC } from "@libp2p/webrtc";
import { webSockets } from "@libp2p/websockets";
import * as filters from "@libp2p/websockets/filters";
import { multiaddr } from "@multiformats/multiaddr";
import { createLibp2p } from "libp2p";
import { circuitRelayTransport } from "@libp2p/circuit-relay-v2";
import { identify } from "@libp2p/identify";
import { createHa } from "./ha";

export async function createNode() {
const node = await createLibp2p({
addresses: {
listen: ["/webrtc"]
},
transports: [
webSockets({
filter: filters.all
}),
webRTC(),
circuitRelayTransport()
],
streamMuxers: [mplex()],
connectionEncryption: [noise()],
services: {
identify: identify(),
ha: createHa()
},
connectionGater: {
denyDialMultiaddr: () => {
return false;
}
},
connectionManager: {
minConnections: 0
}
});

return node;
}

let node = null;
let connections = [];
let connection = null;

export async function start() {
if (node) {
return;
}
node = await createNode();
await node.start();
console.log(`Node started with id ${node.peerId.toString()}`);

function updateConnectionsList() {
connections = node.getConnections().map((item) => {
return item.remoteAddr.toString();
});
console.log("Update Connections List", connections);
}

node.addEventListener("connection:open", (event) => {
console.log("connected", event.detail.remoteAddr.toString());
updateConnectionsList();
});

node.addEventListener("connection:close", (event) => {
console.log("disconected", event.detail.remoteAddr.toString());
updateConnectionsList();
if (
event.detail.remoteAddr.toString() === connection.remoteAddr.toString()
) {
reconnect(connection.remoteAddr.toString());
}
});

return node;
}

export async function reconnect(addr) {
try {
await connect(addr);
} catch (error) {
console.log(error);
setTimeout(async () => {
if (addr && !connections.includes(addr)) {
await reconnect(addr);
}
}, 3000);
}
}

export async function connect(addr) {
const listenerMultiaddr = multiaddr(addr);
connection = await node.dial(listenerMultiaddr);
}

export function request(data) {
if (node && connection) {
return node.services.ha.request(connection, "/call", data);
}
throw new Error("error");
}
4 changes: 2 additions & 2 deletions src/views/RwsSetup.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { useAccount } from "@/hooks/useAccount";
import { useDevices } from "@/hooks/useDevices";
import { useRobonomics } from "@/hooks/useRobonomics";
import { useSend } from "@/hooks/useSend";
import { encryptor } from "@/utils/encryptor";
import { createPair, encryptor } from "@/utils/encryptor";
import { Keyring } from "@polkadot/api";
import { u8aToHex } from "@polkadot/util";
import { decodeAddress, encodeAddress } from "@polkadot/util-crypto";
Expand Down Expand Up @@ -162,7 +162,7 @@ export default {
(item) => item.owner === setupOwner.value
);
const user = encryptor(userSeed);
const user = encryptor(createPair(userSeed));
const passwordForAdmin = user.encryptMessage(
passToSave,
Expand Down
38 changes: 38 additions & 0 deletions src/views/telemetry/Launch.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<template>

Check warning on line 1 in src/views/telemetry/Launch.vue

View workflow job for this annotation

GitHub Actions / deploy

Component name "Launch" should always be multi-word
<robo-template-devices-layout
:config="config"
:datalog="data"
:updateTime="updateTime"
/>
</template>

<script>
import { watch } from "vue";
import { useStore } from "vuex";
import { useData } from "./launch";
export default {
props: {
config: Object
},
setup() {
const { data, updateTime, run, launch } = useData();
const store = useStore();
run();
watch(
() => store.state.robonomicsUIvue.rws.launch,
(value) => {
try {
launch(JSON.parse(value));
} catch (error) {
console.log(error);
}
}
);
return { data, updateTime };
}
};
</script>
Loading

0 comments on commit 8cd150a

Please sign in to comment.