Skip to content

Commit f4d6b2f

Browse files
committed
update
1 parent a37a87f commit f4d6b2f

6 files changed

Lines changed: 115 additions & 1221 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,5 @@ release
2424
*.njsproj
2525
*.sln
2626
*.sw?
27+
28+
MINIFIREFLY-DB

main.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@ function createWindow() {
88
webPreferences: {
99
preload: path.join(__dirname, 'preload.js'),
1010
devTools: true,
11+
sandbox: false
1112
},
1213
})
1314

1415
win.loadFile(path.join(__dirname, 'index.html'))
16+
17+
win.openDevTools();
1518
}
1619

1720
app.on('window-all-closed', () => {

package.json

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,9 @@
66
"dev": "electron ./main.js"
77
},
88
"devDependencies": {
9-
"electron": "^26.1.0",
10-
"electron-builder": "^24.6.4"
9+
"electron": "22.3.27"
1110
},
1211
"dependencies": {
13-
"@iota/sdk": "^1.1.4",
14-
"reflect-metadata": "^0.1.13"
12+
"@iota/sdk": "^1.1.4"
1513
}
1614
}

preload.js

Lines changed: 39 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,31 @@
11
require('reflect-metadata');
22
const { contextBridge } = require('electron')
3-
const SDK = require('@iota/sdk');
3+
const { SecretManager, Wallet, Utils } = require('@iota/sdk');
4+
const fs = require('node:fs');
45

5-
console.log(SDK)
6-
7-
// `exposeInMainWorld` can't detect attributes and methods of `prototype`, manually patching it.
8-
function withPrototype(obj) {
9-
const protos = Object.getPrototypeOf(obj)
10-
11-
for (const [key, value] of Object.entries(protos)) {
12-
if (Object.prototype.hasOwnProperty.call(obj, key)) continue
13-
14-
if (typeof value === 'function') {
15-
// Some native APIs, like `NodeJS.EventEmitter['on']`, don't work in the Renderer process. Wrapping them into a function.
16-
obj[key] = function (...args) {
17-
return value.call(obj, ...args)
6+
function bindMethodsAcrossContextBridge(prototype, object) {
7+
const prototypeProperties = Object.getOwnPropertyNames(prototype)
8+
prototypeProperties.forEach((key) => {
9+
if (key !== 'constructor') {
10+
object[key] = object[key].bind(object)
1811
}
19-
} else {
20-
obj[key] = value
21-
}
22-
}
23-
return obj
12+
})
2413
}
2514

2615
// IOTA
2716
const WALLET_DB_PATH = 'MINIFIREFLY-DB';
28-
const NODE_URL = console.error("ADD A NODE!")
17+
const NODE_URL = process.env["MINIFIREFLY_NODE"]
2918
const STRONGHOLD_PASSWORD = 'firefly';
30-
const STRONGHOLD_SNAPSHOT_PATH = 'minifirefly.stronghold';
19+
const STRONGHOLD_SNAPSHOT_PATH = 'MINIFIREFLY-DB/minifirefly.stronghold';
20+
const strongholdSecretManager = {
21+
stronghold: {
22+
snapshotPath: STRONGHOLD_SNAPSHOT_PATH,
23+
password: STRONGHOLD_PASSWORD,
24+
},
25+
};
3126

3227
contextBridge.exposeInMainWorld('__MINIFIREFLY__', {
3328
async createWallet() {
34-
const strongholdSecretManager = {
35-
stronghold: {
36-
snapshotPath: STRONGHOLD_SNAPSHOT_PATH,
37-
password: STRONGHOLD_PASSWORD,
38-
},
39-
};
40-
4129
const secretManager = SecretManager.create(strongholdSecretManager);
4230
const mnemonic = Utils.generateMnemonic();
4331
await secretManager.storeMnemonic(mnemonic);
@@ -64,9 +52,29 @@ contextBridge.exposeInMainWorld('__MINIFIREFLY__', {
6452
};
6553

6654
const wallet = await Wallet.create(walletOptions);
55+
bindMethodsAcrossContextBridge(Wallet.prototype, wallet)
56+
return wallet
57+
},
58+
async loadWallet() {
59+
const walletOptions = {
60+
storagePath: WALLET_DB_PATH,
61+
clientOptions: {
62+
nodes: [NODE_URL],
63+
},
64+
bipPath: {
65+
coinType: 1,
66+
},
67+
secretManager: strongholdSecretManager,
68+
};
6769

68-
console.log('Generated wallet with address: ' + (await wallet.address()));
69-
70-
return withPrototype(wallet)
70+
const wallet = await Wallet.create(walletOptions);
71+
bindMethodsAcrossContextBridge(Wallet.prototype, wallet)
72+
return wallet
73+
},
74+
removeWallet() {
75+
fs.rmSync(WALLET_DB_PATH, {
76+
recursive: true,
77+
force: true
78+
})
7179
}
7280
})

renderer.js

Lines changed: 51 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,89 @@
11
console.log("welcome to minifirefly!")
22

3-
const API = window["__MINIFIREFLY__"];
4-
console.log("api -> ", API)
3+
const preload = window["__MINIFIREFLY__"];
4+
5+
window["api"] = {}
6+
7+
const handle = window["api"]
8+
9+
// ---
10+
11+
const removeWallet = () => {
12+
preload.removeWallet()
13+
window["_wallet"] = null
14+
console.log("👋 REMOVED wallet")
15+
}
16+
handle["removeWallet"] = removeWallet
517

618
// ---
719

820
const createWallet = () => {
9-
API.createWallet().then((wallet) => {
21+
preload.createWallet().then((wallet) => {
1022
window["_wallet"] = wallet
11-
console.log("created wallet: ", wallet)
23+
console.log("🆕 CREATED wallet: ", wallet)
1224
})
1325
}
14-
window["createWallet"] = createWallet
15-
console.log("createWallet -> ", createWallet)
26+
handle["createWallet"] = createWallet
27+
28+
// ---
29+
30+
const loadWallet = () => {
31+
preload.loadWallet().then((wallet) => {
32+
window["_wallet"] = wallet
33+
console.log("🔄 LOADED wallet: ", wallet)
34+
})
35+
}
36+
handle["loadWallet"] = loadWallet
37+
1638

1739
// ---
1840

1941
const implicitAccountCreationAddress = () => {
2042
window["_wallet"].implicitAccountCreationAddress().then((address) => {
21-
console.log("send funds to ", address);
43+
console.log("💸 SEND funds to ", address);
2244
window["address"] = address
2345
})
2446
}
25-
window["implicitAccountCreationAddress"] = implicitAccountCreationAddress
26-
console.log("implicitAccountCreationAddress -> ", implicitAccountCreationAddress)
47+
handle["implicitAccountCreationAddress"] = implicitAccountCreationAddress
2748

2849
// ---
2950

3051
const sync = () => {
31-
window["_wallet"].sync({ syncImplicitAccounts: true }).then(() => {
32-
console.log("synched wallet")
52+
window["_wallet"].sync({ syncImplicitAccounts: true }).then(async () => {
53+
console.log("📡 SYNCHED wallet", (await window["_wallet"].getBalance()).baseCoin)
3354
})
3455
}
35-
window["sync"] = sync
36-
console.log("sync -> ", sync)
56+
handle["sync"] = sync
3757

3858
// ---
3959

4060
const implicitAccounts = () => {
4161
window["_wallet"].implicitAccounts().then((implicitAccounts) => {
4262
window["_implicitAccounts"] = implicitAccounts
43-
console.log("implicitAccounts", implicitAccounts)
63+
console.log("implicitAccounts", implicitAccounts)
4464
})
4565
}
46-
window["implicitAccounts"] = implicitAccounts
47-
console.log("implicitAccounts -> ", implicitAccounts)
66+
handle["implicitAccounts"] = implicitAccounts
4867

4968
// ---
5069

5170
const prepareImplicitAccountTransition = () => {
5271
window["_wallet"].prepareImplicitAccountTransition(window["_implicitAccounts"][0].outputId).then((res) => {
53-
console.log("res of prepareImplicitAccountTransition", res)
72+
console.log("✅✅ prepareImplicitAccountTransition", res)
5473
})
5574
}
56-
window["prepareImplicitAccountTransition"] = prepareImplicitAccountTransition
57-
console.log("prepareImplicitAccountTransition -> ", prepareImplicitAccountTransition)
75+
handle["prepareImplicitAccountTransition"] = prepareImplicitAccountTransition
76+
77+
setTimeout(async () => {
78+
console.clear()
79+
80+
console.log(`
81+
Use the 'api' object with these methods:
82+
83+
1. createWallet / loadWallet / removeWallet
84+
2. implicitAccountCreationAddress
85+
3. sync
86+
4. implicitAccounts
87+
5. prepareImplicitAccountTransition
88+
`)
89+
}, 100)

0 commit comments

Comments
 (0)