Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Develop #1

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions contracts/Invitation.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ contract Invitation {
User public inviter;
User public invitee;

event InvitationAccepted();
event InvitationRejected();

modifier inviteeOnly() {
require(msg.sender == address(invitee));
_ ;
Expand All @@ -19,9 +22,11 @@ contract Invitation {

function accept() public inviteeOnly {
inviter.invitationAccepted();
InvitationAccepted();
}

function reject() public inviteeOnly {
inviter.invitationRejected();
InvitationRejected();
}
}
4 changes: 2 additions & 2 deletions contracts/Migrations.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ contract Migrations {
last_completed_migration = completed;
}

function upgrade(address new_address) public restricted {
Migrations upgraded = Migrations(new_address);
function upgrade(address newAddress) public restricted {
Migrations upgraded = Migrations(newAddress);
upgraded.setCompleted(last_completed_migration);
}
}
34 changes: 22 additions & 12 deletions contracts/User.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,21 @@ import "./Invitation.sol";
contract User {
struct Profile {
string name;
string avatar;
bytes32 avatar; //Swarm hash
}

struct WhisperInfo {
string pubKey;
string key;
bytes pubKey;
bytes key;
}

event UserProfileUpdated();
event WhisperInfoUpdated();
event InvitationSent(Invitation invitation, User to);
event InvitationReceived(Invitation invitation, User from);
event ContactAdded(User contact);
event ContactRemoved(User contact);
event OwnerChanged(address from, address to);

Profile private _profile;
address private _owner;
Expand All @@ -32,12 +37,12 @@ contract User {
_ ;
}

modifier iscontact() {
modifier isContactOrOwner() {
var found = false;
for (uint i = 0; i < _contacts.length && !found; i++) {
found = _contacts[i] == msg.sender;
}
require(found);
require(found || msg.sender == _owner);
_ ;
}

Expand All @@ -47,11 +52,11 @@ contract User {
registry = UserRegistry(msg.sender);
}

function getProfileInfo() public view returns (string name, string avatar) {
function getProfileInfo() public view returns (string name, bytes32 avatar) {
return (_profile.name, _profile.avatar);
}

function setProfileInfo(string name, string avatar) public onlyowner {
function setProfileInfo(string name, bytes32 avatar) public onlyowner {
_profile.name = name;
_profile.avatar = avatar;
UserProfileUpdated();
Expand All @@ -66,17 +71,18 @@ contract User {
var oldOwner = _owner;
_owner = newOwner;
registry.ownerUpdated(oldOwner, newOwner);
OwnerChanged(oldOwner, newOwner);
}

function getWhisperPubKey() public view iscontact returns (string) {
function getWhisperPubKey() public view isContactOrOwner returns (bytes) {
return _whisperInfo.pubKey;
}

function getWhisperInfo() public view onlyowner returns (string pubKey, string key) {
function getWhisperInfo() public view onlyowner returns (bytes pubKey, bytes key) {
return (_whisperInfo.pubKey, _whisperInfo.key);
}

function setWhisperInfo(string pubKey, string key) public onlyowner {
function setWhisperInfo(bytes pubKey, bytes key) public onlyowner {
_whisperInfo.key = key;
_whisperInfo.pubKey = pubKey;
WhisperInfoUpdated();
Expand All @@ -102,6 +108,7 @@ contract User {
}

_contacts.push(contact);
ContactAdded(contact);
}

function removeContactPrivate(User contact) private {
Expand All @@ -122,16 +129,18 @@ contract User {
function removeContact(User contact) external onlyowner {
removeContactPrivate(contact);
contact.removeMe();
ContactRemoved(contact);
}

function removeMe() public {
require(msg.sender != 0x0);
User contact = User(msg.sender);

removeContactPrivate(contact);
ContactRemoved(contact);
}

function sendInvitation(User invitee) external onlyowner returns(Invitation) {
function sendInvitation(User invitee) external onlyowner {
require(address(invitee) != 0x0);
require(this != invitee);

Expand All @@ -145,7 +154,7 @@ contract User {

invitee.receiveInvitation(invitation);

return invitation;
InvitationSent(invitation, invitee);
}

function receiveInvitation(Invitation invitation) public {
Expand All @@ -159,6 +168,7 @@ contract User {
}

_inbox.push(invitation);
InvitationReceived(invitation, invitation.inviter());
}

//TODO: withdrawInvitation
Expand Down
9 changes: 7 additions & 2 deletions contracts/UserRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,19 @@ import "./User.sol";
contract UserRegistry {
mapping(address => User) private users;

event UserRegistered(User user);
event UserOwnerUpdated(User user, address newOwner);

function me() view external returns (User) {
return users[msg.sender];
}

function register() external returns (User) {
function register() external {
require(msg.sender != 0x0);
require(address(users[msg.sender]) == 0x0);
var user = new User(msg.sender);
users[msg.sender] = user;
return user;
UserRegistered(user);
}

function getUser(address walletId) public view returns (User) {
Expand All @@ -28,5 +32,6 @@ contract UserRegistry {
require(user == msg.sender);
users[oldWalletId] = User(0x0);
users[newWalletId] = user;
UserOwnerUpdated(user, newWalletId);
}
}
2 changes: 1 addition & 1 deletion migrations/1_initial_migration.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var Migrations = artifacts.require("./Migrations.sol");
var Migrations = artifacts.require("Migrations");

module.exports = function(deployer) {
deployer.deploy(Migrations);
Expand Down
19 changes: 9 additions & 10 deletions migrations/2_deploy_contracts.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
var User = artifacts.require("./User.sol");
var UserRegistry = artifacts.require("./UserRegistry.sol");
var Invitation = artifacts.require("./Invitation.sol");
var User = artifacts.require("User");
var UserRegistry = artifacts.require("UserRegistry");
var Invitation = artifacts.require("Invitation");


module.exports = function(deployer, network, accounts) {
return deployer.deploy(UserRegistry)
.then(function() {
return deployer.deploy(User, accounts[0]);
})
.then(function() {
return deployer.deploy(Invitation, User.address, User.address);
});
return deployer.deploy([
[User, accounts[0]],
UserRegistry
]).then(function() {
return deployer.deploy(Invitation, User.address, User.address)
});
};
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,12 @@
"recursive-readdir": "2.1.0",
"strip-ansi": "3.0.1",
"style-loader": "0.13.1",
"truffle-contract": "^1.1.8",
"swarm-js": "^0.1.37",
"truffle": "^4.0.6",
"truffle-contract": "^3.0.3",
"truffle-solidity-loader": "0.0.8",
"url-loader": "0.5.7",
"web3": "^0.20.4",
"webpack": "1.14.0",
"webpack-dev-server": "1.16.2",
"webpack-manifest-plugin": "1.1.0",
Expand All @@ -58,7 +61,9 @@
"start": "node scripts/start.js",
"build": "node scripts/build.js",
"test": "node scripts/test.js --env=jsdom",
"truffle": "truffle"
"truffle": "truffle",
"swarm": "~/Library/Application\\ Support/Mist/swarmjs/bin/swarm -datadir ~/Library/Ethereum/rinkeby --ens-api ~/Library/Ethereum/rinkeby/geth.ipc --corsdomain '*' --bzzaccount $ACCOUNT",
"geth": "~/Library/Application\\ Support/Mist/binaries/Geth/unpacked/geth --rpc --shh --rpcport 9545 --rpccorsdomain '*' --rinkeby --rpcapi eth,web3,shh,net --unlock $ACCOUNT"
},
"jest": {
"collectCoverageFrom": [
Expand Down
94 changes: 53 additions & 41 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { Component } from 'react'
import SimpleStorageContract from '../build/contracts/SimpleStorage.json'
import API from './api'
import getWeb3 from './utils/getWeb3'

import './css/oswald.css'
Expand All @@ -12,58 +12,70 @@ class App extends Component {
super(props)

this.state = {
storageValue: 0,
web3: null
web3: null,
userRegistry: null,
user: null,
chatSocket: null,
userId: ""
}
}

componentWillMount() {
// Get network provider and web3 instance.
// See utils/getWeb3 for more info.
getWeb3.then((web3) => {

getWeb3
.then(results => {
this.setState({
web3: results.web3
let api = API(web3)
this.setState(() => {
return {
web3: web3,
userRegistry: api.UserRegistry.instance()
}
})

// Instantiate contract once web3 provided.
this.instantiateContract()
})
.catch(() => {
console.log('Error finding web3.')
})
}
api.Accounts.instance().getAccounts()
.then((accounts) => { // Search for account with registered user. If not found - register with 0 account
let found = accounts.find((value) => value.user != null)
if (found) {
api.Accounts.instance().currentAccount = found.id
return found
} else {
let account = accounts[0]
return api.UserRegistry.instance().register().then((user) => {
account.user = user
return account
})
}
})
.then((account) => {
console.log("Account", account)
let socket = new api.Whisper(account.user)
this.setState(() => {
return {
user: account.user,
chatSocket: socket,
userId: account.user.id
}
})

instantiateContract() {
/*
* SMART CONTRACT EXAMPLE
*
* Normally these functions would be called in the context of a
* state management library, but for convenience I've placed them here.
*/
socket.on('error', (err) => {
console.error('Socket error', err)
})

const contract = require('truffle-contract')
const simpleStorage = contract(SimpleStorageContract)
simpleStorage.setProvider(this.state.web3.currentProvider)
socket.on('message', (message) => {
console.log('New message', message)
})

// Declaring this for later so we can chain functions on SimpleStorage.
var simpleStorageInstance
socket.on('started', () => {
console.log('Socket started')
socket.sendMessage(account.user, "TEST MESSAGE")
})

// Get accounts.
this.state.web3.eth.getAccounts((error, accounts) => {
simpleStorage.deployed().then((instance) => {
simpleStorageInstance = instance

// Stores a given value, 5 by default.
return simpleStorageInstance.set(5, {from: accounts[0]})
}).then((result) => {
// Get the value from the contract to prove it worked.
return simpleStorageInstance.get.call(accounts[0])
}).then((result) => {
// Update state with the result.
return this.setState({ storageValue: result.c[0] })
})
socket.start()
})
.catch((err) => {
console.error("Error", err)
})
})
}

Expand All @@ -82,7 +94,7 @@ class App extends Component {
<h2>Smart Contract Example</h2>
<p>If your contracts compiled and migrated successfully, below will show a stored value of 5 (by default).</p>
<p>Try changing the value stored on <strong>line 59</strong> of App.js.</p>
<p>The stored value is: {this.state.storageValue}</p>
<p>The stored value is: {this.state.userId}</p>
</div>
</div>
</main>
Expand Down
Loading