-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
218 lines (169 loc) · 5.9 KB
/
Copy pathindex.js
File metadata and controls
218 lines (169 loc) · 5.9 KB
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// import {serverStatus} from "wallet"
const wallet = require('./wallet')
const identity = require('./identity')
const express = require('express')
const app = express()
const port = 5000
const bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
const cron = require('node-cron');
const { VerifiableCredential, Document, checkCredential } = require('@iota/identity-wasm/node')
const { CLIENT_CONFIG } = require('./iota_config')
const address = "19ew6Q7Bcb6VXJK2Bdi1iwa48Dn7jqDKW1oK5Pxiukoj3"
var serveIndex = require('serve-index')
app.use('/nfts', express.static('nfts'), serveIndex('nfts', { 'icons': true }))
app.use('/certs', express.static('certs'), serveIndex('certs', { 'icons': true }))
app.get('/status', (req, res) => {
console.log("wallet", wallet)
wallet.serverStatus().then((status) => {
res.send(status)
})
})
app.get('/create', (req, res) => {
console.log("create get")
res.sendfile(__dirname + '/create.html');
});
app.get('/create_cert', (req, res) => {
console.log("create_cert get")
res.sendfile(__dirname + '/create_cert.html');
});
app.get('/info', (req, res) => {
let object = {
absorptionRatePerDay: 0.5,
title: "Fichtenwald am Fischweiher",
sizeInSqm: 2300,
owner: "did:iota:xyz",
area: [
[49.31323852284926, 7.371573914351571],
[49.31327328404692, 7.372395622526794],
[49.31289059169224, 7.372374392054574],
[49.312997633327626, 7.371549470592754],
],
did: agent.doc
}
res.send(object)
})
let agent;
app.get('/', (req, res) => {
let html = `
<h1>IdentiTree Land</h1>
<a href="/nfts">NFT List</a> |
<a href="/create">Create NFT</a>
<br />
<a href="/certs">Certificate List</a> |
<a href="/create_cert">Create Certificate</a>
<p>To buy a C02 Certificate, send 1000 IOTA to this address:</p>
<pre>${address}</pre>
`
res.send(html)
})
app.post('/create_certificate', async (req, res) => {
console.log('Got body:', req.body);
let tree_did = req.body.did
let revieve_address = req.body.address
let response;
if (tree_did) {
// Prepare a credential subject indicating the degree earned by Alice
let credentialSubject = {
id: tree_did,
name: "Trusted Tree CO2 Certificate Crendential",
}
try {
console.log("1")
// Create an unsigned `UniversityDegree` credential for Alice
const unsignedVc = VerifiableCredential.extend({
id: "http://example.edu/credentials/3732",
type: "CO2Credential",
issuer: agent.doc.id,
credentialSubject,
co2_credits: {
amount: 1000
}
});
console.log("unsignedVc: ", unsignedVc)
let doc = Document.fromJSON(agent.doc)
console.log("doc: ", doc)
// Sign the credential with the Issuer's newKey
const signedVc = doc.signCredential(unsignedVc, {
method: doc.id.toString() + "#key",
public: agent.key.public,
secret: agent.key.secret,
});
console.log("signedVc: ", signedVc)
// Check if the credential is verifiable.
let cv_res = await checkCredential(signedVc.toString(), CLIENT_CONFIG);
let nft_res = await wallet.mintNftCert(signedVc)
console.log("nft_res: ", nft_res)
var searchstring = "Created NFT with ID: "
var start_string = nft_res.indexOf(searchstring) + searchstring.length;
var end_string = start_string + 44
var nft_id = nft_res.substring(start_string, end_string);
console.log("start_string: ", start_string)
console.log("nft_id: ", nft_id)
console.log("nft_id: ", nft_id.length)
response = {
cv_res,
nft_id
}
setTimeout(() => {
wallet.sendNft(nft_id, revieve_address).then((data) => {
console.log("send nft!", data)
});
}, 10000)
console.log("Verifiable Credential: ", response)
} catch (error) {
console.log("error: ", error)
}
} else {
response = {
error: "No DID given in the body"
}
}
res.send(response)
})
app.post('/create_nft', async (req, res) => {
console.log('Got body:', req.body);
let image_link = req.body.image_link
let response;
if (image_link) {
// Prepare a credential subject indicating the degree earned by Alice
response = await wallet.mintNft(image_link)
console.log("NFT created: ", response)
} else {
response = {
error: "No image_link given in the body"
}
}
res.send(response)
})
const main = async function () {
const low = require('lowdb')
const FileSync = require('lowdb/adapters/FileSync')
const adapter = new FileSync('db.json')
const db = low(adapter)
// Set some defaults (required if your JSON file is empty)
db.defaults({ agent: {} })
.write()
agent = db.get('agent')
.value()
// check agent
if (Object.keys(agent).length === 0 && agent.constructor === Object) {
console.log("agent not found...")
console.log("creating new agent")
identity.createIdentity().then((id) => {
agent = id
db.set('agent', agent)
.write()
console.log("agent created!")
});
} else {
console.log("agent found")
// create agent
console.log("agent: ", agent)
}
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
})
}
main()