-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathApp.js
189 lines (152 loc) · 5.53 KB
/
App.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import React, { Component } from 'react';
import { Button, Jumbotron, Container, InputGroup, FormControl } from 'react-bootstrap';
import { ToastContainer, toast } from 'react-toastify';
import 'react-toastify/dist/ReactToastify.css';
import "./App.css";
import logo from "./logo.png";
import Loader from 'react-loader-spinner';
import { publishIDXConfig, schemas, createDefinition } from '@ceramicstudio/idx-tools';
import { IDX } from '@ceramicstudio/idx';
import CeramicClient from '@ceramicnetwork/ceramic-http-client'
import IdentityWallet from 'identity-wallet'
const API_URL = "http://localhost:7007";
var ceramic = "";
var profileID = "";
window.idx = "";
class App extends Component {
state = {
idw : "",
ceramic_url : "",
did : "",
seed : "", // need to change from default value
name : "",
image :"",
eth : "",
showGreeting : false,
showProfileImg : false,
showMainLoader : true,
showSubLoader : false,
showDidTab : false
}
publishIDXConfig = async() => {
let definitions = await publishIDXConfig(ceramic);
return definitions;
}
connectToCeramic = (ceramic_url = API_URL) => {
ceramic = new CeramicClient(ceramic_url);
this.setState({ ceramic_url });
}
genRanHex = size => [...Array(size)].map(() => Math.floor(Math.random() * 16).toString(16)).join('');
handleGenerateIdentityWallet = async() => {
this.setState({showSubLoader : true});
await this.generateIdentityWallet();
this.setState({showSubLoader : false});
}
generateIdentityWallet = async (seed) => {
// create new did from random seed
let randomSeed = seed ? seed : this.genRanHex(64);
let seedBuffer = Buffer.from(randomSeed, 'hex');
let idw = await IdentityWallet.create({
getPermission: async () => [],
seed : seedBuffer
});
console.log(idw);
// set the provider to ceramic
await ceramic.setDIDProvider(idw.getDidProvider());
console.log(profileID);
window.idx = new IDX({ ceramic, definitions: { profile: profileID } });
console.log(window.idx);
console.log(window.idx.did._id);
let data = await window.idx.get('profile');
console.log(data);
this.setState({did : window.idx.did._id})
if (data){
this.setState({name : data['name'], showGreeting : true});
if (data['image']){
this.setState({image : data['image'], showProfileImg : true});
}
if (data['eth']){
this.setState({eth : data['eth']});
}
}
// remember user
window.localStorage.setItem("idx-seed", randomSeed);
this.setState({seed : randomSeed});
console.log("Generated Wallet!");
}
updateName = async(value, imgurl, eth_addr) => {
this.setState({showSubLoader : true});
console.log(`Setting name to ${value}`);
await window.idx.set('profile', { name: value, image: imgurl, eth: eth_addr });
this.setState({name : value})
console.log("Update Complete!")
toast.success("Profile Updated!");
this.setState({showSubLoader : false});
}
// handleFile = async (path) => {
// console.log(path);
// let result = await
// console.log(result);
// return;
// }
async componentDidMount(){
this.connectToCeramic();
let def = await this.publishIDXConfig();
console.log(def);
profileID = def.definitions.basicProfile;
let seed = window.localStorage.getItem("idx-seed");
if (seed) { await this.generateIdentityWallet(seed); }
this.setState({showMainLoader : false, showDidTab : true});
console.log("Initial Setup Complete")
}
render() {
return (
<div className="App">
<ToastContainer />
<Jumbotron>
<Container>
<p>Ceramic Network : {this.state.ceramic_url}</p>
<h1><img src={this.state.showProfileImg ? this.state.image : logo} width="100" height="100"/></h1>
{this.state && this.state.showMainLoader && (
<div>
<Loader type="ThreeDots" color="#3f51b5" height="20" width="30"/>
<small>Loading network configs..</small>
</div>
)}
</Container>
</Jumbotron>
{this.state && this.state.showGreeting && (
<p>Hi, {this.state.name}</p>
)}
{this.state && this.state.showDidTab && (
<div>
<small>Your DID : {this.state.did ? this.state.did.substring(0,15) + "..." + this.state.did.slice(this.state.did.length - 5) : "No DID found"}</small>
<hr/>
</div>
)}
{this.state && this.state.showDidTab && !this.state.did && (
<Button type="submit" onClick={() => this.handleGenerateIdentityWallet()}>Create a new DID wallet</Button>
)}
{this.state && this.state.did && (
<div>
<h4>Your Profile</h4>
{this.state && this.state.showSubLoader && (
<div>
<Loader type="ThreeDots" color="#3f51b5" height="20" width="30"/>
<small>Processing..</small>
</div>
)}
<hr />
<p>Name : <input value={this.state.name} onChange={e => this.setState({name : e.target.value})}/></p>
<p>Image : <input value={this.state.image} onChange={e => this.setState({image : e.target.value})}/></p>
<p>Eth Addr : <input value={this.state.eth} onChange={e => this.setState({eth : e.target.value})}/></p>
<br />
<Button className="primary" onClick={() => this.updateName(this.state.name, this.state.image, this.state.eth)}>Update Profile</Button>
</div>
)}
<br/><br/>
</div>
);
}
}
export default App;