-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
289 lines (237 loc) · 9 KB
/
index.js
File metadata and controls
289 lines (237 loc) · 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
const ethers = require('ethers')
const axios = require('axios')
const CryptoJS = require('crypto-js')
const httpRequestTimeout = 5000 // ms
const keySize = 256
const iterations = 100
class Blocace {
constructor(wallet, protocol, hostname, port) {
this.wallet = wallet
this.hostname = hostname || 'localhost'
this.port = port || 6899
this.protocol = protocol || 'http'
}
static create(protocol, hostname, port) {
return new this(new ethers.Wallet.createRandom(), protocol, hostname, port)
}
static createFromPrivateKey(privKey, protocol, hostname, port) {
return new this(new ethers.Wallet('0x' + privKey), protocol, hostname, port)
}
encryptPrivateKey(pass) {
try {
var salt = CryptoJS.lib.WordArray.random(128 / 8)
var key = CryptoJS.PBKDF2(pass, salt, {
keySize: keySize / 32,
iterations: iterations
})
var iv = CryptoJS.lib.WordArray.random(128 / 8)
var encrypted = CryptoJS.AES.encrypt(this.wallet.privateKey, key, {
iv: iv,
padding: CryptoJS.pad.Pkcs7,
mode: CryptoJS.mode.CBC
})
// salt, iv will be hex 32 in length
// append them to the ciphertext for use in decryption
var transitMessage = salt.toString() + iv.toString() + encrypted.toString()
return transitMessage
} catch (exception) {
throw new Error(exception.message)
}
}
static decryptPrivateKey(encrypted, pass) {
try {
var salt = CryptoJS.enc.Hex.parse(encrypted.substr(0, 32))
var iv = CryptoJS.enc.Hex.parse(encrypted.substr(32, 32))
var encrypted = encrypted.substring(64)
var key = CryptoJS.PBKDF2(pass, salt, {
keySize: keySize / 32,
iterations: iterations
})
var decrypted = CryptoJS.AES.decrypt(encrypted, key, {
iv: iv,
padding: CryptoJS.pad.Pkcs7,
mode: CryptoJS.mode.CBC
})
return decrypted.toString(CryptoJS.enc.Utf8)
} catch (exception) {
throw new Error(exception.message)
}
}
static verifySignature(rawDocument, signature, address) {
const docDigest = ethers.utils.keccak256(Buffer.from(rawDocument))
// for recoveryParam == 0 or 1
return ethers.utils.recoverAddress(docDigest, '0x' + signature + '00') == address ||
ethers.utils.recoverAddress(docDigest, '0x' + signature + '01') == address
}
getPublicKey() {
return ethers.utils.computePublicKey(this.wallet.privateKey).substring(4)
}
static async createAccount(accountPayload, protocol, hostname, port) {
// accountPayload.publicKey = this.wallet.getPublicKey().toString('hex')
const accountRes = await axios.request({
url: protocol + '://' + hostname + ':' + port + '/account',
method: 'post',
timeout: httpRequestTimeout,
data: accountPayload
})
return accountRes
}
async updateAccount(accountPayload, address) {
const accountRes = await axios.request({
url: this.protocol + '://' + this.hostname + ':' + this.port + '/account/' + address,
method: 'post',
timeout: httpRequestTimeout,
headers: { 'Authorization': 'Bearer ' + this.token },
data: accountPayload
})
return accountRes
}
async setAccountReadWrite(permissionPayload, address) {
const accountRes = await axios.request({
url: this.protocol + '://' + this.hostname + ':' + this.port + '/setaccountpermission/' + address,
method: 'post',
timeout: httpRequestTimeout,
headers: { 'Authorization': 'Bearer ' + this.token },
data: permissionPayload
})
return accountRes
}
async getChallenge() {
return axios.get(this.protocol + '://' + this.hostname + ':' + this.port + '/jwt/challenge/' + this.wallet.address)
}
async getJWT() {
const challengeResponse = await this.getChallenge()
const challengeHash = ethers.utils.keccak256(Buffer.from(challengeResponse.data.challenge))
const sig = this.wallet.signingKey.signDigest(challengeHash)
const jwt = await axios.post(this.protocol + '://' + this.hostname + ':' + this.port + '/jwt', {
'address': this.wallet.address,
'challengeWord': challengeResponse.data.challenge,
'signature': sig.r.substring(2) + sig.s.substring(2)
})
this.token = jwt.data.token
return jwt.data.token
}
async getAccount(address) {
const accountRes = await axios.request({
url: this.protocol + '://' + this.hostname + ':' + this.port + '/account/' + address,
method: 'get',
timeout: httpRequestTimeout,
headers: { 'Authorization': 'Bearer ' + this.token }
})
return accountRes.data
}
async createCollection(collectionPayload) {
const collectionCreationRes = await axios.request({
url: this.protocol + '://' + this.hostname + ':' + this.port + '/collection',
method: 'post',
timeout: httpRequestTimeout,
headers: { 'Authorization': 'Bearer ' + this.token },
data: collectionPayload
})
return collectionCreationRes.data
}
async signAndPutDocument(document, collection) {
const docHash = ethers.utils.keccak256(Buffer.from(JSON.stringify(document)))
const sig = this.wallet.signingKey.signDigest(docHash)
const putDocRes = await axios.request({
url: this.protocol + '://' + this.hostname + ':' + this.port + '/document/' + collection,
method: 'post',
timeout: httpRequestTimeout,
headers: { 'Authorization': 'Bearer ' + this.token },
data: {
'rawDocument': JSON.stringify(document),
'signature': sig.r.substring(2) + sig.s.substring(2)
}
})
return putDocRes.data
}
// WARNING: this makes the document unverifiable
async putDocumentBulk(documents, collection) {
const putDocRes = await axios.request({
url: this.protocol + '://' + this.hostname + ':' + this.port + '/bulk/' + collection,
method: 'post',
timeout: httpRequestTimeout,
headers: { 'Authorization': 'Bearer ' + this.token },
data: documents
})
return putDocRes.data
}
async query(queryPayload, collection) {
const queryRes = await axios.request({
url: this.protocol + '://' + this.hostname + ':' + this.port + '/search/' + collection,
method: 'post',
timeout: httpRequestTimeout,
headers: { 'Authorization': 'Bearer ' + this.token },
data: queryPayload
})
return queryRes.data
}
async verifyTransaction(blockchainId, blockId, transactionId) {
const verificationPathRes = await axios.request({
url: this.protocol + '://' + this.hostname + ':' + this.port + '/verification/' + blockchainId + '/' + blockId + '/' + transactionId,
method: 'get',
timeout: httpRequestTimeout,
headers: { 'Authorization': 'Bearer ' + this.token }
})
const verificationPath = verificationPathRes.data.verificationPath
var keys = Object.keys(verificationPath)
var hashData = Buffer.from(transactionId, 'hex')
for (let i = keys.length - 1; i > 0; i--) {
var secondHash = Buffer.from(verificationPath[keys[i]], 'hex')
if (keys[i] % 2 === 0) { // right child
var prevHashes = Buffer.concat([hashData, secondHash])
hashData = Buffer.from(ethers.utils.keccak256(prevHashes).substring(2), 'hex')
} else {
prevHashes = Buffer.concat([secondHash, hashData])
hashData = Buffer.from(ethers.utils.keccak256(prevHashes).substring(2), 'hex')
}
}
return hashData.equals(Buffer.from(verificationPath['0'], 'hex'))
}
async getBlockInfo(blockchainId, blockId) {
const blockInfoRes = await axios.request({
url: this.protocol + '://' + this.hostname + ':' + this.port + '/block/' + blockchainId + '/' + blockId,
method: 'get',
timeout: httpRequestTimeout,
headers: { 'Authorization': 'Bearer ' + this.token }
})
return blockInfoRes.data
}
async getBlockchainInfo() {
const blockchainRes = await axios.request({
url: this.protocol + '://' + this.hostname + ':' + this.port + '/info',
method: 'get',
timeout: httpRequestTimeout,
headers: { 'Authorization': 'Bearer ' + this.token }
})
return blockchainRes.data
}
async getPeers() {
const peersRes = await axios.request({
url: this.protocol + '://' + this.hostname + ':' + this.port + '/peers',
method: 'get',
timeout: httpRequestTimeout,
headers: { 'Authorization': 'Bearer ' + this.token }
})
return peersRes.data
}
async getCollections() {
const collectionsRes = await axios.request({
url: this.protocol + '://' + this.hostname + ':' + this.port + '/collections',
method: 'get',
timeout: httpRequestTimeout,
headers: { 'Authorization': 'Bearer ' + this.token }
})
return collectionsRes.data
}
async getCollection(collectionName) {
const collectionRes = await axios.request({
url: this.protocol + '://' + this.hostname + ':' + this.port + '/collection/' + collectionName,
method: 'get',
timeout: httpRequestTimeout,
headers: { 'Authorization': 'Bearer ' + this.token }
})
return collectionRes.data
}
}
module.exports = Blocace