-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
* feat: add wallet-popup * feat: add DecodedCallData component
- Loading branch information
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<!doctype html> | ||
<html> | ||
<head> | ||
<title>Substrate Connect</title> | ||
<meta charset="utf-8" /> | ||
<link | ||
href="https://fonts.googleapis.com/css2?family=Poppins:ital@0;1&display=swap" | ||
rel="stylesheet" | ||
/> | ||
<link | ||
href="http://fonts.googleapis.com/css?family=Roboto:400,100,100italic,300, 500,700,900" | ||
rel="stylesheet" | ||
/> | ||
<link | ||
href="http://fonts.googleapis.com/css?family=Inter:400,100,100italic,300, 500,700,900" | ||
rel="stylesheet" | ||
/> | ||
</head> | ||
<body> | ||
<div id="popup"></div> | ||
<script type="module" src="../src/wallet-popup.tsx"></script> | ||
</body> | ||
</html> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { createRpc } from "@substrate/light-client-extension-helpers/utils" | ||
import { useEffect, useState } from "react" | ||
import type { BackgroundRpcSpec, SignRequest } from "../background/types" | ||
import { DecodedCallData } from "./WalletPopup/components" | ||
|
||
// FIXME: use hook | ||
const port = chrome.runtime.connect({ | ||
name: "substrate-wallet-template/wallet-popup", | ||
}) | ||
const rpc = createRpc((msg) => | ||
port.postMessage(msg), | ||
).withClient<BackgroundRpcSpec>() | ||
port.onMessage.addListener(rpc.handle) | ||
|
||
export const WalletPopup = () => { | ||
const [signRequest, setSignRequest] = useState<SignRequest>() | ||
const [signRequestId, setSignRequestId] = useState<string>() | ||
useEffect(() => { | ||
const init = async () => { | ||
const signRequests = await rpc.client.getSignRequests() | ||
// TODO: handle many signRequests | ||
const keys = Object.keys(signRequests) | ||
if (keys.length === 0) return | ||
setSignRequestId(keys[0]) | ||
setSignRequest(signRequests[keys[0]]) | ||
} | ||
init() | ||
}, []) | ||
if (!signRequestId || !signRequest) return null | ||
return ( | ||
<main className="w-[32rem] mx-auto px-6 py-8"> | ||
<div> | ||
<h1 className="text-3xl font-bold">Sign Request #{signRequestId}</h1> | ||
<div className="my-4"> | ||
<div className="my-2"> | ||
<div className="text-sm font-semibold">Origin</div> | ||
<div>{signRequest.url}</div> | ||
</div> | ||
<div className="my-2 overflow-hidden"> | ||
<div className="text-sm font-semibold">Chain Id</div> | ||
<pre className="overflow-auto">{signRequest.chainId}</pre> | ||
</div> | ||
<div className="my-2"> | ||
<div className="text-sm font-semibold">From</div> | ||
<pre>{signRequest.address}</pre> | ||
</div> | ||
<div className="my-2 overflow-hidden"> | ||
<div className="text-sm font-semibold">Call data</div> | ||
<pre className="overflow-auto">{signRequest.callData}</pre> | ||
</div> | ||
<div className="my-2"> | ||
<div className="text-sm font-semibold">Decoded Call data</div> | ||
<DecodedCallData | ||
chainId={signRequest.chainId} | ||
callData={signRequest.callData} | ||
/> | ||
</div> | ||
<div className="my-2"> | ||
<div className="text-sm font-semibold">Signed extensions</div> | ||
<div>coming soon...</div> | ||
</div> | ||
</div> | ||
<div className="my-4 text-center"> | ||
<button | ||
onClick={() => rpc.client.approveSignRequest(signRequestId)} | ||
className="py-1.5 px-8 mr-4 text-sm rounded border border-[#24cc85] text-[#24cc85] hover:text-white | ||
hover:bg-[#24cc85]" | ||
> | ||
Approve | ||
</button> | ||
<button | ||
onClick={() => rpc.client.cancelSignRequest(signRequestId)} | ||
className="py-1.5 px-8 text-sm rounded border border-[#24cc85] text-[#24cc85] hover:text-white | ||
hover:bg-[#24cc85]" | ||
> | ||
Cancel | ||
</button> | ||
</div> | ||
</div> | ||
</main> | ||
) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { useDecodedCallData } from "../hooks" | ||
|
||
type Props = { | ||
chainId: string | ||
callData: string | ||
} | ||
|
||
const jsonStringify = (value: any) => | ||
JSON.stringify( | ||
value, | ||
(_, value) => (typeof value === "bigint" ? value.toString() : value), | ||
2, | ||
) | ||
|
||
export const DecodedCallData = ({ chainId, callData }: Props) => { | ||
const { decodedCallData } = useDecodedCallData(chainId, callData) | ||
if (!decodedCallData) return <div>decoding...</div> | ||
return ( | ||
<div> | ||
<div className="text-sm">Pallet: {decodedCallData.pallet.value.name}</div> | ||
<div className="text-sm">Call: {decodedCallData.call.value.name}</div> | ||
<div className="text-sm">Args</div> | ||
<pre>{jsonStringify(decodedCallData?.args.value)}</pre> | ||
</div> | ||
) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./DecodedCallData" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./useDecodedCallData" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { helper } from "@substrate/light-client-extension-helpers/extension-page" | ||
import { getObservableClient } from "@polkadot-api/client" | ||
import { createClient } from "@polkadot-api/substrate-client" | ||
import { DecodedCall, getViewBuilder } from "@polkadot-api/metadata-builders" | ||
import { useEffect, useState } from "react" | ||
import { filter, firstValueFrom } from "rxjs" | ||
import { useIsMounted } from "../../../hooks/useIsMounted" | ||
|
||
export const useDecodedCallData = (chainId: string, callData: string) => { | ||
const [decodedCallData, setDecodedCallData] = useState<DecodedCall>() | ||
const isMounted = useIsMounted() | ||
|
||
useEffect(() => { | ||
if (!chainId || !callData) return | ||
helper.getChains().then(async (chains) => { | ||
const chain = chains.find(({ genesisHash }) => genesisHash === chainId) | ||
if (!chain) return | ||
const client = getObservableClient(createClient(chain.provider)) | ||
const { metadata$, unfollow } = client.chainHead$() | ||
const metadata = await firstValueFrom(metadata$.pipe(filter(Boolean))) | ||
unfollow() | ||
client.destroy() | ||
const builder = getViewBuilder(metadata) | ||
if (!isMounted()) return | ||
setDecodedCallData(builder.callDecoder(callData)) | ||
}) | ||
}, [chainId, callData]) | ||
Check warning on line 27 in projects/wallet-template/src/containers/WalletPopup/hooks/useDecodedCallData.ts
|
||
|
||
return { decodedCallData } | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export { Options } from "./Options" | ||
export { default as Popup } from "./Popup" | ||
export { default as Logo } from "../components/Logo" | ||
export { WalletPopup } from "./WalletPopup" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { render } from "react-dom" | ||
import { WalletPopup } from "./containers" | ||
import "./style.css" | ||
|
||
render(<WalletPopup />, document.getElementById("popup")) |