amark / gun

An open source cybersecurity protocol for syncing decentralized graph data.
https://gun.eco/docs
Other
18.03k stars 1.16k forks source link

Need to be more clear about Node and SEA, Node relay needs require (ES5 and not ES6) #1264

Closed bitdom8 closed 2 years ago

bitdom8 commented 2 years ago

Checked react tutorial and learned that you need to install these 2 packages for node to be working. Need to be more clear as it takes more time to even install default node server:

npm install --save buffer text-encoding

I had "Signature did not match." on gundb data base a lot of times before fixing it

bitdom8 commented 2 years ago

The problem persists after few login and logout.

"Signature did not match." in gundb. When using this relay server ('https://gun-manhattan.herokuapp.com/gun',) it went away. How can it work with this default relay server and not with ours?

Wanna use my own relay server. Taken the code from here examples but still errs.

bitdom8 commented 2 years ago

Maybe I changed the relay node to ES6 script not using require but using import as below:

import cluster from "cluster";
import fs from "fs"
import Gun from 'gun'
import 'gun/sea.js';
import 'gun/lib/radix.js'
import 'gun/lib/radisk.js'
import 'gun/lib/store.js'
import https from "https"
import http from "http"
import path from 'path'
import {fileURLToPath} from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// import crashed from 
// cluster = cluster();
// require('../lib/crashed')
var gun

;(function(){

    if(cluster.isPrimary){
      return cluster.fork() && cluster.on('exit', function(){ cluster.fork(); });
    }

    // var fs = require('fs');
    var config = {
        port: process.env.OPENSHIFT_NODEJS_PORT || process.env.VCAP_APP_PORT || process.env.PORT || process.argv[2] || 8765,
        peers: process.env.PEERS && process.env.PEERS.split(',') || []
    };
    // var Gun = require('../'); // require('gun')

    if(process.env.HTTPS_KEY){
        config.key = fs.readFileSync(process.env.HTTPS_KEY);
        config.cert = fs.readFileSync(process.env.HTTPS_CERT);
        config.server = https.createServer(config, Gun.serve(__dirname));

    } else {
        config.server = http.createServer(Gun.serve(__dirname));
    }

    gun = Gun({web: config.server.listen(config.port), peers: config.peers});

    console.log('Relay peer started on port ' + config.port + ' with /gun');

    // module.exports = gun;

}());

export {gun}

All works great until I refresh the page. Got an error "Signature did not match."

bitdom8 commented 2 years ago

Ok now understood you need to use ES5 with gun relay server and not ES6: please see here https://github.com/amark/gun/issues/636

Also a note with express. This is server.js file

console.log("If module not found, install express globally `npm i express -g`!");
var port    = process.env.OPENSHIFT_NODEJS_PORT || process.env.VCAP_APP_PORT || process.env.PORT || process.argv[2] || 8765;
var express = require('express');
var Gun     = require('gun');
require('gun/axe');

var app    = express();
app.use(Gun.serve);
app.use(express.static(__dirname));

var server = app.listen(port);
var gun = Gun({ file: 'data', web: server });

global.Gun = Gun; /// make global to `node --inspect` - debug only
global.gun = gun; /// make global to `node --inspect` - debug only

console.log('Server started on port ' + port + ' with /gun');

this is package.js file


{

    "type": "commonjs",

    "dependencies": {

    "express": "^4.18.1",

    "gun": "^0.2020.1237"

    }
}
bitdom8 commented 2 years ago

Ok after few try again receive "User is already being created or authenticated!" from front end and "Signature did not match." in gundb relay server. Back to the beginning.

draeder commented 2 years ago

@bitdom8 You didn't post your client code, so I'm making assumptions here...

You need to wait until the user is created and/or authenticated before you can work with that user's graph.

gun.user().create('someuser', 'somepass') // authenticates automatically after user creation IIRC
// or
let pair = await SEA.pair()
gun.auth(pair)

// Wait for the `'auth'` event
gun.on('auth', ack => {
  if(ack.ok) console.log('user is authenticated') // now you can do stuff with the user's graph
})
bitdom8 commented 2 years ago

I had encountered with converting "require" statements on node relay server to import. My problem was rooted when just running the server and client side app, using sveltekit.

Now all is well with "require" on server "relay" side.

Had nothing to do with user or SEA but have seen this is issue most of the times. thanks @draeder