-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
146 lines (97 loc) · 3.16 KB
/
index.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
var request = require('request');
const express = require('express')
const path = require('path')
var crypto = require('crypto');
const PORT = process.env.PORT || 5000
var bodyParser = require("body-parser");
var cors = require('cors');
var Wallet = require('ethereumjs-wallet');
const Box = require('3box');
var app = express();
//var io = require('socket.io')(app);
const http = require('http')
var bip39 = require('bip39') // npm i -S bip39
var crypto = require('crypto')
var bitcoin = require('bitcoinjs-lib')
var server = http.createServer(app).listen(PORT, () => console.log(`Listening on ${ PORT }`))
app.use(express.static(path.join(__dirname, 'public')))
//const Web3 = require('web3');
//let web3 = new Web3('ws://localhost:8546');
var cors = require('cors');
app.use(cors({credentials: true, origin: '*'}));
// .set('views', path.join(__dirname, 'views'))
//.set('view engine', 'ejs')
//.get('/', (req, res) => res.render('pages/index'))
app.get("/", function(req, res) {
res.send("Hello Welcome to Secrets/3Box API Implementation");
})
//Create ETH address
app.get("/ethcreate", function(req, res) {
var addr = Wallet.generate();
var pri = addr.getPrivateKeyString()
var add = addr.getAddressString()
res.send([add, pri])
})
app.get("/upload", async function(req, res){
var contentNeedSplitting = req.query.content;
if(typeof req.query.content != "string"){
res.send({"status":"fail", "msg":"Please send content"});
return;
}
splitUp = split3Parts(contentNeedSplitting);
const cw3p = require('create-web3-provider');
let provider = cw3p();
//add your web3 provider/Infura info here
provider = cw3p({ws: true, network: 'ropsten'});
const box = await Box.create()
var addr = Wallet.generate();
const address = addr.getAddressString()
const spaces = ['secretsDapp']
await box.auth(spaces, { address, provider })
await box.private.set('content1', splitUp[0])
await box.private.set('content2', splitUp[1])
await box.private.set('content3', splitUp[2])
res.send({"status":"success", "msg":"It worked!"});
})
function makeETHAddress(respObj){
privateKey = web3.eth.accounts.create().privateKey.substr(2)
web3.eth.personal.importRawKey(privateKey, pin)
.then((result) => {
//store pub address.
publicAddr = web3.utils.toChecksumAddress(result)
respObj.send(publicAddr)
})
}
function split3Parts(theSubString){
var parts = theSubString.length;
var by3 = parseInt(parts/3);
regexA = new RegExp('.{1,'+by3+'}', "g");
theReturn = theSubString.match(regexA);
if(theReturn.length >3){
theReturn[2] = theReturn[2]+ theReturn[3];
}
theReturn.pop()
return theReturn;
}
function callURL(theURL){
var requestOptions = {
headers: headers,
url:theURL,
method: "GET",
// body: postBody
};
var headers = {
'content-type' : 'application/json'
};
request(requestOptions, function(error, response, body) {
if (error) { console.log(error); return;}
try{
var jsonResp = JSON.parse(body);
console.log(jsonResp)
}
catch(err){
console.log(err)
console.log("^^ json error")
}
});
}