Skip to content

Commit

Permalink
Switch to JoyID
Browse files Browse the repository at this point in the history
  • Loading branch information
phroi committed Nov 1, 2024
1 parent 504d2c5 commit 1da6cca
Show file tree
Hide file tree
Showing 13 changed files with 2,088 additions and 1,735 deletions.
14 changes: 7 additions & 7 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head>
<meta charset="utf-8" />
<title>iCKB DApp</title>
<link rel="icon" href="src/assets/ickb_logo.png" />
<link rel="icon" href="favicon.png" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<meta property="og:title" content="iCKB DApp" />
Expand Down Expand Up @@ -36,17 +36,17 @@
>
Would you like to try out the DApp?
</h2>
<!-- <button
id="mainnet"
<button
id="JoyID_mainnet"
class="text-s h-12 w-full rounded border-2 border-amber-400 font-bold uppercase leading-relaxed tracking-wider text-amber-400 disabled:opacity-50"
>
Yes, connect Metamask to Mainnet 🚧
</button> -->
Yes, connect JoyID to Mainnet 🚧 (experimental)
</button>
<button
id="testnet"
id="JoyID_testnet"
class="text-s h-12 w-full rounded border-2 border-amber-400 font-bold uppercase leading-relaxed tracking-wider text-amber-400 disabled:opacity-50"
>
Yes, connect Metamask to Testnet ☕️
Yes, connect JoyID to Testnet ☕️
</button>
<a
href="https://t.me/NervosNation/307406"
Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
{
"name": "v1-interface",
"version": "1.4.1",
"version": "1.4.2",
"type": "module",
"scripts": {
"dev": "vite",
"build": "tsc && vite build && echo ickb.org > dist/CNAME",
"build": "tsc && vite build",
"lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
"preview": "vite preview"
},
"dependencies": {
"@ckb-ccc/ccc": "latest",
"@ckb-lumos/base": "^0.23.0",
"@ckb-lumos/codec": "^0.23.0",
"@ckb-lumos/common-scripts": "^0.23.0",
Expand Down Expand Up @@ -46,5 +47,5 @@
"typescript": "next",
"vite": "^5.4.9"
},
"packageManager": "pnpm@9.12.2+sha256.2ef6e547b0b07d841d605240dce4d635677831148cd30f6d564b8f4f928f73d2"
"packageManager": "pnpm@9.12.3+sha256.24235772cc4ac82a62627cd47f834c72667a2ce87799a846ec4e8e555e2d4b8b"
}
3,389 changes: 1,899 additions & 1,490 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions public/CNAME
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ickb.org
File renamed without changes
4 changes: 2 additions & 2 deletions src/Action.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,10 @@ async function transact(
formReset: () => void,
walletConfig: WalletConfig,
) {
const { rpc, signer } = walletConfig;
const { rpc, sendSigned } = walletConfig;
try {
freezeTxInfo(txInfo);
const txHash = await rpc.sendTransaction(await signer(txInfo.tx));
const txHash = await sendSigned(txInfo.tx);
let status = "pending";
while (status === "pending" || status === "proposed") {
await new Promise((r) => setTimeout(r, 10000));
Expand Down
99 changes: 99 additions & 0 deletions src/Connector.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import { useQuery } from "@tanstack/react-query";
import { I8Script, i8ScriptPadding, scriptEq } from "@ickb/lumos-utils";
import type { Cell } from "@ckb-lumos/base";
import type { RootConfig } from "./utils.ts";
import { EmptyDashboard } from "./Dashboard.tsx";
import App from "./App.tsx";
import Progress from "./Progress.tsx";
import { Transaction, type Signer } from "@ckb-ccc/ccc";
import type { TransactionSkeletonType } from "@ckb-lumos/helpers";

export default function Connector({
rootConfig,
signer,
walletName,
}: {
rootConfig: RootConfig;
signer: Signer;
walletName: string;
}) {
const {
isPending,
error,
data: walletConfig,
} = useQuery({
queryKey: ["walletConfig"],
queryFn: async () => {
if (!(await signer.isConnected())) {
await signer.connect();
}

const [address, recommendedAddressObj, addressObjs] = await Promise.all([
signer.getRecommendedAddress(),
signer.getRecommendedAddressObj(),
signer.getAddressObjs(),
]);

let accountLocks = [recommendedAddressObj, ...addressObjs].map((s) =>
I8Script.from({
...i8ScriptPadding,
...s.script,
}),
);
// Keep unique account locks, preferred one is the first one
accountLocks = [
...new Map(
accountLocks.map((s) => [`${s.args}-${s.hashType}-${s.codeHash}`, s]),
).values(),
];

const expander = (c: Cell) => {
const lock = c.cellOutput.lock;
for (const s of accountLocks) {
if (scriptEq(lock, s)) {
return s;
}
}
};

const getTxSizeOverhead = async (tx: TransactionSkeletonType) => {
const t0 = Transaction.fromLumosSkeleton(tx);
const size0 = t0.toBytes().length; // +4
const t1 = await signer.prepareTransaction(t0);
const size1 = t1.toBytes().length; // +4
return size1 - size0;
};

const sendSigned = async (tx: TransactionSkeletonType) =>
signer.sendTransaction(Transaction.fromLumosSkeleton(tx));

return {
...rootConfig,
address,
accountLocks,
expander,
getTxSizeOverhead,
sendSigned,
};
},
});

if (isPending)
return (
<>
<EmptyDashboard />
<p>Waiting for {walletName} authorization...</p>
<Progress />
</>
);

if (error) {
setTimeout(function () {
console.log(error);
location.reload();
}, 10000);
return <p>Unable to connect to {walletName} ⚠️</p>;
}

return <App {...{ walletConfig }} />;
}
4 changes: 1 addition & 3 deletions src/Dashboard.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { encodeToAddress } from "@ckb-lumos/helpers";
import type { WalletConfig } from "./utils.ts";
import { Button } from "react-aria-components";
import { CKB } from "@ickb/lumos-utils";
Expand Down Expand Up @@ -28,8 +27,7 @@ export function Dashboard({
formSetMax: (direction: boolean) => void;
walletConfig: WalletConfig;
}) {
const { chain, config, accountLock } = walletConfig;
const address = encodeToAddress(accountLock, { config });
const { chain, address } = walletConfig;
const hl = Math.floor(address.length / 2);
const href = `https://${chain !== "mainnet" ? "pudge." : ""}explorer.nervos.org/address/${address}`;
return (
Expand Down
168 changes: 0 additions & 168 deletions src/Metamask.tsx

This file was deleted.

Loading

0 comments on commit 1da6cca

Please sign in to comment.