|
| 1 | +import { Mode, Account } from "porto"; |
| 2 | +import { connectRp } from "../client/connectRp"; |
| 3 | +import { createTimeout } from "../createTimeout"; |
| 4 | +import { syncPort } from "../sync/syncPort"; |
| 5 | +import { sharedState } from "../sync/sharedState"; |
| 6 | + |
| 7 | +const rp = connectRp(); |
| 8 | +rp.port.then((port) => syncPort("rp", port)); |
| 9 | + |
| 10 | +async function createAccount() { |
| 11 | + const port = await rp.port; |
| 12 | + return await new Promise<Account.Account>((resolve, reject) => { |
| 13 | + function onPortMessage(event: MessageEvent) { |
| 14 | + if (event.data === "createResult") { |
| 15 | + console.log("got create result"); |
| 16 | + resolve({} as never); |
| 17 | + } |
| 18 | + } |
| 19 | + port.addEventListener("message", onPortMessage); |
| 20 | + |
| 21 | + const timeout = createTimeout(60_000); |
| 22 | + timeout.promise.finally(() => port.removeEventListener("message", onPortMessage)); |
| 23 | + timeout.promise.catch(reject); |
| 24 | + |
| 25 | + console.log("asking port to create account"); |
| 26 | + port.postMessage("create"); |
| 27 | + }); |
| 28 | +} |
| 29 | + |
| 30 | +export const mode = Mode.from({ |
| 31 | + name: "rp", |
| 32 | + actions: { |
| 33 | + async createAccount() { |
| 34 | + console.log("mode.actions.createAccount()"); |
| 35 | + const account = await createAccount(); |
| 36 | + return { account }; |
| 37 | + }, |
| 38 | + |
| 39 | + async loadAccounts() { |
| 40 | + console.log("mode.actions.loadAccounts()"); |
| 41 | + |
| 42 | + const accounts = sharedState.getState().accounts; |
| 43 | + |
| 44 | + if (!accounts.length) { |
| 45 | + console.log("create account"); |
| 46 | + await createAccount(); |
| 47 | + throw new Error("failed"); |
| 48 | + } else { |
| 49 | + console.log("TODO sign in"); |
| 50 | + // sign in? |
| 51 | + } |
| 52 | + |
| 53 | + return { accounts: [] }; |
| 54 | + }, |
| 55 | + |
| 56 | + addFunds() { |
| 57 | + console.log("mode.actions.addFunds()"); |
| 58 | + throw new Error("Not implemented."); |
| 59 | + }, |
| 60 | + disconnect() { |
| 61 | + console.log("mode.actions.disconnect()"); |
| 62 | + throw new Error("Not implemented."); |
| 63 | + }, |
| 64 | + getAccountVersion() { |
| 65 | + console.log("mode.actions.getAccountVersion()"); |
| 66 | + throw new Error("Not implemented."); |
| 67 | + }, |
| 68 | + getCallsStatus() { |
| 69 | + console.log("mode.actions.getCallsStatus()"); |
| 70 | + throw new Error("Not implemented."); |
| 71 | + }, |
| 72 | + getCapabilities() { |
| 73 | + console.log("mode.actions.getCapabilities()"); |
| 74 | + throw new Error("Not implemented."); |
| 75 | + }, |
| 76 | + getKeys() { |
| 77 | + console.log("mode.actions.getKeys()"); |
| 78 | + throw new Error("Not implemented."); |
| 79 | + }, |
| 80 | + grantAdmin() { |
| 81 | + console.log("mode.actions.grantAdmin()"); |
| 82 | + throw new Error("Not implemented."); |
| 83 | + }, |
| 84 | + grantPermissions() { |
| 85 | + console.log("mode.actions.grantPermissions()"); |
| 86 | + throw new Error("Not implemented."); |
| 87 | + }, |
| 88 | + prepareCalls() { |
| 89 | + console.log("mode.actions.prepareCalls()"); |
| 90 | + throw new Error("Not implemented."); |
| 91 | + }, |
| 92 | + prepareUpgradeAccount() { |
| 93 | + console.log("mode.actions.prepareUpgradeAccount()"); |
| 94 | + throw new Error("Not implemented."); |
| 95 | + }, |
| 96 | + revokeAdmin() { |
| 97 | + console.log("mode.actions.revokeAdmin()"); |
| 98 | + throw new Error("Not implemented."); |
| 99 | + }, |
| 100 | + revokePermissions() { |
| 101 | + console.log("mode.actions.revokePermissions()"); |
| 102 | + throw new Error("Not implemented."); |
| 103 | + }, |
| 104 | + sendCalls() { |
| 105 | + console.log("mode.actions.sendCalls()"); |
| 106 | + throw new Error("Not implemented."); |
| 107 | + }, |
| 108 | + sendPreparedCalls() { |
| 109 | + console.log("mode.actions.sendPreparedCalls()"); |
| 110 | + throw new Error("Not implemented."); |
| 111 | + }, |
| 112 | + signPersonalMessage() { |
| 113 | + console.log("mode.actions.signPersonalMessage()"); |
| 114 | + throw new Error("Not implemented."); |
| 115 | + }, |
| 116 | + signTypedData() { |
| 117 | + console.log("mode.actions.signTypedData()"); |
| 118 | + throw new Error("Not implemented."); |
| 119 | + }, |
| 120 | + updateAccount() { |
| 121 | + console.log("mode.actions.updateAccount()"); |
| 122 | + throw new Error("Not implemented."); |
| 123 | + }, |
| 124 | + upgradeAccount() { |
| 125 | + console.log("mode.actions.upgradeAccount()"); |
| 126 | + throw new Error("Not implemented."); |
| 127 | + }, |
| 128 | + verifyEmail() { |
| 129 | + console.log("mode.actions.verifyEmail()"); |
| 130 | + throw new Error("Not implemented."); |
| 131 | + }, |
| 132 | + }, |
| 133 | +}); |
0 commit comments