-
Notifications
You must be signed in to change notification settings - Fork 0
/
preload.js
120 lines (109 loc) · 3.22 KB
/
preload.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
require('reflect-metadata');
const { ipcRenderer, contextBridge } = require('electron')
const { SecretManager, Wallet, Utils } = require('@iota/sdk');
const IotaSdk = require('@iota/sdk')
const fs = require('node:fs');
function bindMethodsAcrossContextBridge(prototype, object) {
const prototypeProperties = Object.getOwnPropertyNames(prototype)
prototypeProperties.forEach((key) => {
if (key !== 'constructor') {
object[key] = object[key].bind(object)
}
})
}
const node = process.env["MINIFIREFLY_NODE"]
// IOTA
const WALLET_DB_PATH = 'MINIFIREFLY-DB';
const NODE_URL = node
const STRONGHOLD_PASSWORD = 'firefly';
const STRONGHOLD_SNAPSHOT_PATH = 'MINIFIREFLY-DB/minifirefly.stronghold';
const strongholdSecretManager = {
stronghold: {
snapshotPath: STRONGHOLD_SNAPSHOT_PATH,
password: STRONGHOLD_PASSWORD,
},
};
try {
ipcRenderer.invoke('get-path', 'userData').then(async (baseDir) => {
const logDir = `${baseDir}/logs`
if (!fs.existsSync(logDir)) {
fs.mkdirSync(logDir)
}
const versionDetails = await ipcRenderer.invoke('get-version-details')
const today = new Date().toISOString().slice(0, 16).replace('T', '-').replace(':', '-')
const loggerOptions = {
colorEnabled: true,
name: `${logDir}/minifirefly-v${versionDetails.currentVersion}-d${today}.log`,
levelFilter: 'debug',
targetExclusions: ['h2', 'hyper', 'rustls', 'message_handler'],
}
IotaSdk.initLogger(loggerOptions)
})
} catch (err) {
console.error('[Preload Context] Error:', err)
}
contextBridge.exposeInMainWorld('__MINIFIREFLY__', {
async createWallet() {
const secretManager = SecretManager.create(strongholdSecretManager);
const mnemonic = Utils.generateMnemonic();
await secretManager.storeMnemonic(mnemonic);
const wallet_address = await secretManager.generateEd25519Addresses({
coinType: 1,
accountIndex: 0,
range: {
start: 0,
end: 1,
},
bech32Hrp: 'tst',
});
const walletOptions = {
address: wallet_address[0],
storagePath: WALLET_DB_PATH,
clientOptions: {
nodes: [NODE_URL],
},
bipPath: {
coinType: 1,
},
secretManager: strongholdSecretManager,
};
const wallet = await Wallet.create(walletOptions);
bindMethodsAcrossContextBridge(Wallet.prototype, wallet)
return wallet
},
async loadWallet() {
const walletOptions = {
storagePath: WALLET_DB_PATH,
clientOptions: {
nodes: [NODE_URL],
},
bipPath: {
coinType: 1,
},
secretManager: null,
};
const wallet = await Wallet.create(walletOptions);
bindMethodsAcrossContextBridge(Wallet.prototype, wallet)
await wallet.sync({
forceSyncing: true,
account: {
basicOutputs: true,
aliasOutputs: true,
},
wallet: {
basicOutputs: false,
accountOutputs: false,
},
syncIncomingTransactions: true,
syncNativeTokenFoundries: true,
syncImplicitAccounts: true,
})
return wallet
},
removeWallet() {
fs.rmSync(WALLET_DB_PATH, {
recursive: true,
force: true
})
}
})