|
| 1 | +import { |
| 2 | + mnemonicToSeedBip39, |
| 3 | + deriveHiveKeys, |
| 4 | + signTx, |
| 5 | + signExternalTx, |
| 6 | + getWallet, |
| 7 | + getKeysFromSeed, |
| 8 | +} from "@ecency/wallets"; |
| 9 | + |
| 10 | +export type RpcRequest = { method: string; params?: any }; |
| 11 | +export interface RpcArgs { origin: string; request: RpcRequest } |
| 12 | +export type OnRpcRequestHandler = (args: RpcArgs) => Promise<any>; |
| 13 | + |
| 14 | +interface SnapState { mnemonic?: string } |
| 15 | + |
| 16 | +async function getState(): Promise<SnapState> { |
| 17 | + return ( |
| 18 | + ((await (globalThis as any).snap.request({ |
| 19 | + method: "snap_manageState", |
| 20 | + params: { operation: "get" }, |
| 21 | + })) as SnapState) || {} |
| 22 | + ); |
| 23 | +} |
| 24 | + |
| 25 | +async function updateState(state: SnapState): Promise<void> { |
| 26 | + await (globalThis as any).snap.request({ |
| 27 | + method: "snap_manageState", |
| 28 | + params: { operation: "update", newState: state }, |
| 29 | + }); |
| 30 | +} |
| 31 | + |
| 32 | +async function getAddressFromMnemonic(mnemonic: string, chain: string) { |
| 33 | + if (chain === "HIVE") { |
| 34 | + const keys = deriveHiveKeys(mnemonic); |
| 35 | + return keys.activePubkey; |
| 36 | + } |
| 37 | + const wallet = getWallet(chain as any); |
| 38 | + if (!wallet) throw new Error("Unsupported chain"); |
| 39 | + const [, address] = await getKeysFromSeed(mnemonic, wallet, chain as any); |
| 40 | + return address; |
| 41 | +} |
| 42 | + |
| 43 | +async function signHiveTransaction(mnemonic: string, tx: any) { |
| 44 | + const keys = deriveHiveKeys(mnemonic); |
| 45 | + return signTx(tx, keys.active); |
| 46 | +} |
| 47 | + |
| 48 | +export const onRpcRequest: OnRpcRequestHandler = async ({ request }) => { |
| 49 | + const state = await getState(); |
| 50 | + |
| 51 | + switch (request.method) { |
| 52 | + case "initialize": { |
| 53 | + const mnemonic = request.params?.mnemonic; |
| 54 | + if (typeof mnemonic !== "string") throw new Error("mnemonic required"); |
| 55 | + mnemonicToSeedBip39(mnemonic); // validate |
| 56 | + await updateState({ mnemonic }); |
| 57 | + return true; |
| 58 | + } |
| 59 | + case "unlock": { |
| 60 | + const mnemonic = request.params?.mnemonic; |
| 61 | + if (typeof mnemonic !== "string") throw new Error("mnemonic required"); |
| 62 | + mnemonicToSeedBip39(mnemonic); // validate |
| 63 | + if (state.mnemonic && mnemonic !== state.mnemonic) { |
| 64 | + throw new Error("invalid mnemonic"); |
| 65 | + } |
| 66 | + await updateState({ mnemonic }); |
| 67 | + return true; |
| 68 | + } |
| 69 | + case "getAddress": { |
| 70 | + if (!state.mnemonic) throw new Error("locked"); |
| 71 | + const chain = request.params?.chain; |
| 72 | + const address = await getAddressFromMnemonic(state.mnemonic, chain); |
| 73 | + return { address }; |
| 74 | + } |
| 75 | + case "signHiveTx": { |
| 76 | + if (!state.mnemonic) throw new Error("locked"); |
| 77 | + const tx = request.params?.tx; |
| 78 | + return signHiveTransaction(state.mnemonic, tx); |
| 79 | + } |
| 80 | + case "signExternalTx": { |
| 81 | + if (!state.mnemonic) throw new Error("locked"); |
| 82 | + const { currency, params } = request.params ?? {}; |
| 83 | + return signExternalTx(currency, params); |
| 84 | + } |
| 85 | + case "getBalance": { |
| 86 | + // Placeholder implementation. In a full snap environment the |
| 87 | + // useGetExternalWalletBalanceQuery hook from @ecency/wallets |
| 88 | + // should be used via a QueryClient. |
| 89 | + return 0; |
| 90 | + } |
| 91 | + default: |
| 92 | + throw new Error("Method not found."); |
| 93 | + } |
| 94 | +}; |
0 commit comments