-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmnemonicLib.js
49 lines (40 loc) · 1.53 KB
/
mnemonicLib.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
const ffi = require('ffi');
const path = require('path');
const child = require('child_process');
const { platform } = process;
let mnemoniclibLoc = null;
// Hack for electron asar package
const basePath = __dirname.replace('app.asar', 'app.asar.unpacked');
const isMusl = () => {
const output = child.spawnSync('ldd', ['--version']).stderr.toString('utf8');
if (output.indexOf('musl') > -1) {
return true;
}
return false;
};
if (platform === 'darwin') {
mnemoniclibLoc = path.join(basePath, '/lib/libmnemoniclib.dylib');
} else if (platform === 'win32') {
mnemoniclibLoc = path.join(basePath, '/lib/mnemoniclib.dll');
// add ./lib to dll directory (for windows we need linked libraries)
process.env.PATH = process.env.PATH === '' ? path.join(basePath, '/lib') : `${process.env.PATH};${path.join(basePath, '/lib')}`;
} else if (platform === 'linux') {
if (isMusl()) {
mnemoniclibLoc = path.join(basePath, '/lib/musl/libmnemoniclib.so');
} else {
mnemoniclibLoc = path.join(basePath, '/lib/libmnemoniclib.so');
}
} else {
throw new Error('unsupported platform for libmnemonic');
}
const init = () => {
const callback = ffi.Function('void', ['CString', 'CString']);
const boolCallback = ffi.Function('void', ['bool', 'CString']);
return ffi.Library(mnemoniclibLoc, {
create: ['void', ['CString', callback]],
getPrivateKey: ['void', ['CString', 'CString', callback]],
isBasicSeed: ['void', ['CString', boolCallback]],
isPasswordSeed: ['void', ['CString', boolCallback]],
});
};
module.exports = init;