Browse Source

OrbitDB init

develop
Ezerous 7 years ago
parent
commit
291a2b729b
  1. 21
      contracts/Forum.sol
  2. 2
      package.json
  3. 96
      src/containers/LoadingContainer.js
  4. 12
      src/containers/UsernameFormContainer.js
  5. 4
      src/index.js
  6. 2
      src/reducer.js
  7. BIN
      src/resources/ipfs_logo.png
  8. 2
      src/store.js
  9. 2
      src/util/drizzleOptions.js
  10. 32
      src/util/orbit.js
  11. 21
      src/util/orbitReducer.js

21
contracts/Forum.sol

@ -2,10 +2,13 @@ pragma solidity ^0.4.23;
contract Forum { contract Forum {
//----------------------------------------AUTHENTICATION---------------------------------------- //----------------------------------------USER----------------------------------------
struct User { struct User {
string username; // TODO: set an upper bound instead of arbitrary string string username; // TODO: set an upper bound instead of arbitrary string
// TODO: orbitDBAddress; string orbitMainDB; // TODO: set an upper bound instead of arbitrary string
string orbitTopicsDB;
string orbitPostsDB;
uint[] topicIDs; // IDs of the topics the user created uint[] topicIDs; // IDs of the topics the user created
uint[] postIDs; // IDs of the posts the user created uint[] postIDs; // IDs of the posts the user created
bool signedUp; // Helper variable for hasUserSignedUp() bool signedUp; // Helper variable for hasUserSignedUp()
@ -17,10 +20,10 @@ contract Forum {
event UserSignedUp(string username, address userAddress); event UserSignedUp(string username, address userAddress);
event UsernameUpdated(string newName, string oldName,address userAddress); event UsernameUpdated(string newName, string oldName,address userAddress);
function signUp(string username) public returns (bool) { function signUp(string username, string orbitMainDB, string orbitTopicsDB, string orbitPostsDB) public returns (bool) {
require (!hasUserSignedUp(msg.sender), "User has already signed up."); require (!hasUserSignedUp(msg.sender), "User has already signed up.");
require(!isUserNameTaken(username), "Username is already taken."); require(!isUserNameTaken(username), "Username is already taken.");
users[msg.sender] = User(username, new uint[](0), new uint[](0), true); users[msg.sender] = User(username, orbitMainDB, orbitTopicsDB, orbitPostsDB, new uint[](0), new uint[](0), true);
userAddresses[username] = msg.sender; userAddresses[username] = msg.sender;
emit UserSignedUp(username, msg.sender); emit UserSignedUp(username, msg.sender);
return true; return true;
@ -55,7 +58,17 @@ contract Forum {
return false; return false;
} }
function getOrbitMainDB(address userAddress) public view returns (string) {
return users[userAddress].orbitMainDB;
}
function getOrbitTopicsDB(address userAddress) public view returns (string) {
return users[userAddress].orbitTopicsDB;
}
function getOrbitPostsDB(address userAddress) public view returns (string) {
return users[userAddress].orbitPostsDB;
}
//----------------------------------------POSTING---------------------------------------- //----------------------------------------POSTING----------------------------------------
struct Topic { struct Topic {
uint topicID; uint topicID;

2
package.json

@ -11,6 +11,8 @@
"drizzle-react": "^1.1.1", "drizzle-react": "^1.1.1",
"drizzle-react-components": "^1.1.0", "drizzle-react-components": "^1.1.0",
"eth-block-tracker-es5": "^2.3.2", "eth-block-tracker-es5": "^2.3.2",
"ipfs":"^0.28.2",
"orbit-db":"^0.19.7",
"prop-types": "^15.6.1", "prop-types": "^15.6.1",
"react": "^16.3.2", "react": "^16.3.2",
"react-dom": "^16.3.2", "react-dom": "^16.3.2",

96
src/containers/LoadingContainer.js

@ -0,0 +1,96 @@
import { drizzleConnect } from 'drizzle-react'
import React, { Children, Component } from 'react'
import PropTypes from 'prop-types'
import ipfs_logo from './../resources/ipfs_logo.png';
/*
* Create component.
*/
class LoadingContainer extends Component {
render() {
if (this.props.web3.status === 'failed')
{
if (this.props.errorComp) {
return this.props.errorComp
}
return(
<main className="container loading-screen">
<div className="pure-g">
<div className="pure-u-1-1">
<h1></h1>
<p>This browser has no connection to the Ethereum network. Please use the Chrome/FireFox extension MetaMask, or dedicated Ethereum browsers Mist or Parity.</p>
</div>
</div>
</main>
)
}
if (this.props.web3.status === 'initialized' && Object.keys(this.props.accounts).length === 0)
{
return(
<main className="container loading-screen">
<div className="pure-g">
<div className="pure-u-1-1">
<h1>🦊</h1>
<p><strong>We can't find any Ethereum accounts!</strong> Please check and make sure Metamask or you browser are pointed at the correct network and your account is unlocked.</p>
</div>
</div>
</main>
)
}
if (!this.props.orbitDB.initialized)
{
return(
<main className="container loading-screen">
<div className="pure-g">
<div className="pure-u-1-1">
<img src={ipfs_logo} alt="ipfs_logo" height="50"/>
<p><strong>Initializing IPFS...</strong></p>
</div>
</div>
</main>
)
}
if (this.props.drizzleStatus.initialized)
return Children.only(this.props.children);
if (this.props.loadingComp)
return this.props.loadingComp;
return(
<main className="container loading-screen">
<div className="pure-g">
<div className="pure-u-1-1">
<h1></h1>
<p>Loading dapp...</p>
</div>
</div>
</main>
)
}
}
LoadingContainer.contextTypes = {
drizzle: PropTypes.object
};
/*
* Export connected component.
*/
const mapStateToProps = state => {
return {
accounts: state.accounts,
drizzleStatus: state.drizzleStatus,
web3: state.web3,
orbitDB: state.orbitDB
}
};
export default drizzleConnect(LoadingContainer, mapStateToProps)

12
src/containers/UsernameFormContainer.js

@ -1,5 +1,8 @@
import { drizzleConnect } from 'drizzle-react' import { drizzleConnect } from 'drizzle-react'
import React, { Component } from 'react' import React, { Component } from 'react'
import { createDatabases } from './../util/orbit'
import PropTypes from 'prop-types' import PropTypes from 'prop-types'
const contract = "Forum"; const contract = "Forum";
@ -17,12 +20,15 @@ class UsernameFormContainer extends Component {
this.state = {usernameInput:''}; this.state = {usernameInput:''};
} }
handleSubmit() { async handleSubmit() {
this.setState({usernameInput:''});
if(this.props.user.hasSignedUp) if(this.props.user.hasSignedUp)
this.contracts[contract].methods[updateUsernameMethod].cacheSend(...[this.state.usernameInput]); this.contracts[contract].methods[updateUsernameMethod].cacheSend(...[this.state.usernameInput]);
else else
this.contracts[contract].methods[signUpMethod].cacheSend(...[this.state.usernameInput]); {
const orbitdb = await createDatabases();
this.contracts[contract].methods[signUpMethod].cacheSend(...[this.state.usernameInput, orbitdb.mainDB, orbitdb.topicsDB, orbitdb.postsDB]);
}
} }

4
src/index.js

@ -7,10 +7,10 @@ import { DrizzleProvider } from 'drizzle-react'
// Layouts // Layouts
import App from './App' import App from './App'
import HomeContainer from './layouts/home/HomeContainer' import HomeContainer from './layouts/home/HomeContainer'
import { LoadingContainer } from 'drizzle-react-components' import LoadingContainer from './containers/LoadingContainer'
import store from './store' import store from './store'
import drizzleOptions from './drizzleOptions' import drizzleOptions from './util/drizzleOptions'
import './css/index.css' import './css/index.css'

2
src/reducer.js

@ -2,10 +2,12 @@ import { combineReducers } from 'redux'
import { routerReducer } from 'react-router-redux' import { routerReducer } from 'react-router-redux'
import { drizzleReducers } from 'drizzle' import { drizzleReducers } from 'drizzle'
import userReducer from "./userReducer"; import userReducer from "./userReducer";
import orbitReducer from "./util/orbitReducer";
const reducer = combineReducers({ const reducer = combineReducers({
routing: routerReducer, routing: routerReducer,
user: userReducer, user: userReducer,
orbitDB: orbitReducer,
...drizzleReducers ...drizzleReducers
}); });

BIN
src/resources/ipfs_logo.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

2
src/store.js

@ -5,7 +5,7 @@ import reducer from './reducer'
import rootSaga from './rootSaga' import rootSaga from './rootSaga'
import createSagaMiddleware from 'redux-saga' import createSagaMiddleware from 'redux-saga'
import { generateContractsInitialState } from 'drizzle' import { generateContractsInitialState } from 'drizzle'
import drizzleOptions from './drizzleOptions' import drizzleOptions from './util/drizzleOptions'
// Redux DevTools // Redux DevTools
const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose; const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;

2
src/drizzleOptions.js → src/util/drizzleOptions.js

@ -1,4 +1,4 @@
import Forum from './build/contracts/Forum.json' import Forum from './../build/contracts/Forum.json'
const drizzleOptions = { const drizzleOptions = {
web3: { web3: {

32
src/util/orbit.js

@ -0,0 +1,32 @@
import IPFS from 'ipfs';
import OrbitDB from 'orbit-db';
import store from './../store';
// OrbitDB uses Pubsub which is an experimental feature
// and need to be turned on manually.
// Note that these options need to be passed to IPFS in
// all examples in this document even if not specified so.
const ipfsOptions = {
EXPERIMENTAL: {
pubsub: true
},
};
// Create IPFS instance
const ipfs = new IPFS(ipfsOptions);
ipfs.on('ready', async () => {
store.dispatch({type: "IPFS_READY"});
});
async function createDatabases() {
const orbitdb = new OrbitDB(ipfs);
const topicsDB = await orbitdb.keyvalue('topics');
const postsDB = await orbitdb.keyvalue('posts');
console.log("OrbitDBs created successfully!");
return {mainDB: orbitdb.id, topicsDB: topicsDB.address.toString(), postsDB: postsDB.address.toString()}; //TODO: regex in the latter two
}
export { createDatabases }

21
src/util/orbitReducer.js

@ -0,0 +1,21 @@
const initialState = {
initialized: false,
databasesReady: false
};
const orbitReducer = (state = initialState, action) => {
switch (action.type) {
case 'IPFS_READY':
return {
initialized: true
};
case 'DATABASES_CREATED':
return {
databasesReady: true
};
default:
return state
}
};
export default orbitReducer
Loading…
Cancel
Save