Skip to content

Commit

Permalink
added latest mesh node support fluidtrends#55
Browse files Browse the repository at this point in the history
  • Loading branch information
idancali committed Sep 28, 2021
1 parent 211f42a commit ac466dd
Show file tree
Hide file tree
Showing 17 changed files with 190 additions and 114 deletions.
29 changes: 15 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@
"@babel/plugin-proposal-private-methods": "^7.12.1",
"@babel/plugin-transform-runtime": "^7.12.10",
"@babel/preset-env": "^7.12.10",
"@carmel/mesh": "^1.1.7",
"@carmel/mesh": "^1.1.8",
"@hapi/hapi": "^20.0.3",
"@heroicons/react": "^1.0.4",
"@react-native-community/async-storage": "^1.11.0",
Expand Down Expand Up @@ -204,6 +204,7 @@
"decompress": "^4.2.1",
"decompress-targz": "^4.1.1",
"deep-extend": "^0.6.0",
"deepmerge": "^4.2.2",
"electron-dl": "^3.0.1",
"electron-serve": "^1.0.0",
"electron-squirrel-startup": "^1.0.0",
Expand Down
25 changes: 13 additions & 12 deletions src/main/events/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,22 @@ const _sign = async (message: string) => {


export const fetchIdentity = async (data: any) => {
const identity = await system.server.gateway.fetchIdentity(data.username)
// const identity = await system.server.gateway.fetchIdentity(data.username)

await send({
id: data.id,
type: 'fetchIdentity',
identity: identity ? identity.data : false
})
// await send({
// id: data.id,
// type: 'fetchIdentity',
// identity: identity ? identity.data : false
// })
}

export const saveAccount = async (props: any) => {
const data: any = {}
Object.keys(props.data).map((k: string) => data[k] = props.data[k].value)

const update = await system.server.identity.update(data, _sign)
console.log(update)
const result = await system.mesh.send("stats:utils", { id: "time", data: { format: "YYYY" } }, { id: props.id })
// const update = await system.server.identity.update(data, _sign)
// console.log(update)
// const { privateKey } = await system.getSecret('identity')

// const identity = await system.server.chain.getId()
Expand All @@ -92,7 +93,7 @@ export const saveAccount = async (props: any) => {
await send({
id: props.id,
type: 'saveAccount',
error: "Oops"
result
})
}

Expand All @@ -112,9 +113,9 @@ export const register = async (credentials: any) => {
// return
// }

await system.server.identity.create({
publicKey, username
}, _sign)
// await system.server.identity.create({
// publicKey, username
// }, _sign)

// if (credentials.plan.requiredTokens > 0) {
// // Make a payment
Expand Down
1 change: 0 additions & 1 deletion src/main/events/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,3 @@ export * from './commands'
export * from './security'
export * from './challenges'
export * from './auth'

5 changes: 2 additions & 3 deletions src/main/events/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,7 @@ export const setup = async (e: any) => {

const now = Date.now()

await system.init({
createdTimestamp: now,
system.update({
loadedTimestamp: now,
identity,
node: {
Expand All @@ -128,7 +127,7 @@ export const setup = async (e: any) => {
},
bundles: {
}
}, e.password)
})

await system.setSecret('identity', { privateKey, username })
await send({
Expand Down
3 changes: 3 additions & 0 deletions src/main/node/functions/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * as register from './register'
export * as update from './update'
export * as time from './time'
10 changes: 10 additions & 0 deletions src/main/node/functions/register.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export const request = async ({ session, event, log }: any) => {
const { data } = event
const { username, publicKey, did } = data

log(`registering ${username} ...`)

const result = await session.chain.system("caccount", { username, pub_key: publicKey, did })

return result
}
16 changes: 16 additions & 0 deletions src/main/node/functions/time.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import moment from 'moment'
import { mesh } from '../../system'

export const request = async ({ log, session, channel, id, from, type, data }: any) => {
log(`getting time ...`, data)

return { timestamp: `${Date.now()}`, formatted: `${moment().format(data.format)}` }
}

export const response = async ({ log, session, channel, data }: any) => {
log(`getting time response ...`, data)

mesh.onResponse(data)

return data
}
10 changes: 10 additions & 0 deletions src/main/node/functions/update.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export const request = async ({ session, event, log }: any) => {
const { data } = event
const { username, signature, did } = data

log(`updating ${username} ...`)

const result = await session.chain.system("uaccount", { username, sig: signature, did })

return result
}
8 changes: 0 additions & 8 deletions src/main/node/handlers/accept.ts

This file was deleted.

3 changes: 0 additions & 3 deletions src/main/node/handlers/index.ts

This file was deleted.

8 changes: 0 additions & 8 deletions src/main/node/handlers/ping.ts

This file was deleted.

9 changes: 0 additions & 9 deletions src/main/node/handlers/system.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/main/node/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from './server'
import * as handlers from './handlers'
import * as functions from './functions'
51 changes: 18 additions & 33 deletions src/main/node/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import cors from 'cors'
import { asset } from '../assets'
import cookieParser from 'cookie-parser'
import http from 'http'
import * as system from '../system'
import path from 'path'
import getPort from 'get-port'
import { Session } from '@carmel/mesh/src'
import debug from 'debug'
import { ipfsConfig } from './config'
import fs from 'fs-extra'
import * as handlers from './handlers'
import * as functions from './functions'
import merge from 'deepmerge'

const USER_HOME = process.env[(process.platform === 'win32') ? 'USERPROFILE' : 'HOME']
const CARMEL_HOME = path.resolve(USER_HOME, '.carmel')
Expand All @@ -29,19 +31,12 @@ export class Server {
private _session: Session
private _root: string

constructor(env: any) {
constructor() {
this._app = express()
this._env = env
this._root = DEFAULT_ROOT
this._env = system.env()

fs.existsSync(this.root) || fs.mkdirpSync(this.root)

this._session = new Session({
isOperator: false,
revision: DEFAULT_REVISION,
handlers,
root: this.root
})
}

get root () {
Expand Down Expand Up @@ -73,6 +68,17 @@ export class Server {
}

async init() {
const channels = await system.getSetting('channels')

this._session = new Session({
isOperator: false,
channels,
revision: DEFAULT_REVISION,
root: this.root
})

this.session.registerFunctions(functions)

this._port = await getPort()
this.app.set('port', this.port)
// this.app.set('views', this.dir.path!)
Expand Down Expand Up @@ -110,8 +116,8 @@ export class Server {
async startNode () {
LOG('starting node ...')

const relays = await this.session.server.resolveRelays()
const config = ipfsConfig(relays, `${this.root}ipfs`, [4902, 4903, 5902, 5903, 9990])
const relays = await this.session.chain.fetch.relays()
const config = ipfsConfig(relays, `${this.root}/ipfs`, this.session.config.isOperator ? [4402, 4403, 5402, 5403, 9490] : [4302, 4303, 5302, 5303, 9390])

try {
const { ipfsBin } = config
Expand Down Expand Up @@ -149,26 +155,5 @@ export class Server {
}))

await this.startNode()
await this.send.ping({ message: "Hello from Studio" })
}

get send () {
return this.session.server.send
}

get push () {
return this.session.server._push
}

get pull () {
return this.session.server._pull
}

get identity() {
return this.session.identity
}

get gateway() {
return this.session
}
}
Loading

0 comments on commit ac466dd

Please sign in to comment.