-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.js
128 lines (115 loc) · 2.91 KB
/
util.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
const { dirname, resolve, join } = require('path')
const isBuffer = require('is-buffer')
const mkdirp = require('mkdirp')
const debug = require('debug')('ara:identity:util')
const pify = require('pify')
const dns = require('ara-identity-dns')
const fs = require('fs')
const os = require('os')
const { createIdentityKeyPath } = require('./key-path')
/* eslint-disable no-await-in-loop */
/**
* Converts a buffer to a hex string.
* @public
* @param {Buffer} value
* @return {String}
*/
function toHex(value) {
if (Buffer.isBuffer(value)) {
return value.toString('hex')
} if (value) {
return toHex(Buffer.from(value))
}
return toHex(Buffer.from(0))
}
/**
* Converts an ethereum style hex string into
* a buffer. This function will return `null'
* on input that is not a 'string' type.
* @public
* @param {String} hex
* @return {Buffer|null}
*/
function ethHexToBuffer(hex) {
if (hex && 'string' === typeof hex) {
if ('0x' === hex.slice(0, 2)) {
return Buffer.from(hex.slice(2), 'hex')
}
return Buffer.from(hex, 'hex')
}
return null
}
/**
* Convert a value, however possible, to a buffer
* @public
* @param {Mixed} value
* @return {Buffer}
*/
function toBuffer(value) {
if (isBuffer(value)) {
return value
} if (value && 'number' === typeof value) {
return Buffer.alloc(value)
} if ('string' === typeof value) {
return ethHexToBuffer(value)
} if (value) {
return Buffer.from(value)
}
return Buffer.alloc(0)
}
/**
* Write ARA Identity files to the root folder
* @public
* @param {Object} Identity
*/
async function writeIdentity(identity) {
if (null == identity || 'object' !== typeof identity) {
throw new TypeError('util.writeIdentity: Expecting object.')
}
if (null == identity.files || 'object' !== typeof identity.files) {
throw new TypeError('util.writeIdentity: Expecting files object.')
}
const output = createIdentityKeyPath(identity)
await mkdirp(output)
for (let i = 0; i < identity.files.length; ++i) {
if (
identity.files[i]
&& identity.files[i].path
&& identity.files[i].buffer
) {
const dir = dirname(resolve(output, identity.files[i].path))
await mkdirp(dir)
await pify(fs.writeFile)(
resolve(output, identity.files[i].path),
identity.files[i].buffer
)
}
}
}
async function resolveDNS(uri) {
const resolution = await dns.resolve(uri)
if (resolution && resolution.length) {
if (resolution[0] && resolution[0].identifier) {
return resolution[0].identifier
}
}
return null
}
async function writeCache(identifier, filename, buffer) {
try {
const cachePath = join(os.tmpdir(), 'aid', identifier, filename)
await mkdirp(dirname(cachePath))
await pify(fs.writeFile)(cachePath, buffer)
} catch (err) {
debug(err)
}
return true
}
module.exports = {
ethHexToBuffer,
writeIdentity,
writeCache,
resolveDNS,
toBuffer,
toHex,
}