Skip to content

Commit f33659d

Browse files
author
Guru
committed
btc transaction
1 parent 04aa56b commit f33659d

File tree

2 files changed

+122
-29
lines changed

2 files changed

+122
-29
lines changed

demo/redirect-flow-example/src/components/UserCard.tsx

Lines changed: 79 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import { HiOutlineDuplicate } from "react-icons/hi";
88
import { HiOutlineCheckCircle } from "react-icons/hi";
99
import { Link } from "./Link";
1010
import useUnifiedRPC from "../composibles/useRpc";
11+
import { Dropdown } from "./DropDown";
12+
import { TextField } from "./TextField";
1113

1214
const UserCard: React.FC = () => {
1315
const { drawerHeading, setDrawerHeading, drawerInfo, setDrawerInfo, userInfo, coreKitInstance } = useCoreKit();
@@ -18,7 +20,37 @@ const UserCard: React.FC = () => {
1820
const [imageError, setImageError] = React.useState(false);
1921
const [currentDrawerHeading, setCurrentDrawerHeading] = React.useState("");
2022
const [currentDrawerInfo, setCurrentDrawerInfo] = React.useState<any>(null);
23+
const [walletAddresses, setWalletAddresses] = React.useState<{ index: number; address: string }[]>([]);
24+
const [selectedWallet, setSelectedWallet] = React.useState<string>("");
25+
const [newWalletName, setNewWalletName] = React.useState<string>("");
2126

27+
const fetchWalletAddresses = async () => {
28+
const indices = await coreKitInstance.getTssWalletIndices();
29+
indices.push({ address: account, index: 0 })
30+
indices.sort((a, b) => a.index - b.index);
31+
setWalletAddresses(indices);
32+
if (indices.length > 0) {
33+
setSelectedWallet(indices[1].address);
34+
}
35+
};
36+
37+
React.useEffect(() => {
38+
if (account)
39+
fetchWalletAddresses();
40+
}, [account]);
41+
42+
const createNewWallet = async () => {
43+
const indices = coreKitInstance.getTssWalletIndices();
44+
const newIndex = indices[indices.length - 1].index + 1;
45+
await coreKitInstance.setTssWalletIndex(newIndex);
46+
await fetchWalletAddresses();
47+
};
48+
49+
const switchTssWalletIndex = async () => {
50+
console.log("1", coreKitInstance.getTssWalletIndices());
51+
await coreKitInstance.setTssWalletIndex(1);
52+
console.log("2", coreKitInstance.getTssWalletIndices());
53+
};
2254
React.useEffect(() => {
2355
if (drawerHeading) {
2456
setCurrentDrawerHeading(drawerHeading);
@@ -35,7 +67,6 @@ const UserCard: React.FC = () => {
3567
}
3668
}, [drawerInfo]);
3769

38-
3970
React.useEffect(() => {
4071
const getAccountRPC = async () => {
4172
if (coreKitInstance.state.userInfo) {
@@ -55,6 +86,7 @@ const UserCard: React.FC = () => {
5586
setIsCopied(true);
5687
navigator.clipboard.writeText(account);
5788
setTimeout(() => {
89+
switchTssWalletIndex();
5890
setIsCopied(false);
5991
}, 1000);
6092
};
@@ -101,27 +133,53 @@ const UserCard: React.FC = () => {
101133
</div>
102134
<div className="my-4 border-t border-app-gray-200 dark:border-app-gray-600"></div>
103135
<div className="space-y-2">
104-
<Button
105-
size="sm"
106-
className="gap-2 w-full !border-app-gray-300 !text-app-gray-800 dark:!text-app-white"
107-
variant="secondary"
108-
onClick={handleCopyAddress}
109-
>
110-
{getTruncateString(account)}
111-
<div className="relative">
112-
{isCopied && (
113-
<div className="absolute bottom-[150%] left-1/2 -translate-x-1/2 bg-app-white dark:bg-app-gray-600 py-2 px-4 rounded-lg text-black text-sm text-center w-max shadow-md">
114-
Copied
115-
<div className="absolute border-8 border-b-0 border-r-transparent border-t-app-white dark:border-t-app-gray-600 border-l-transparent top-[100%] left-[calc(50%_-_8px)]"></div>
116-
</div>
117-
)}
118-
{isCopied ? (
119-
<HiOutlineCheckCircle className={`cursor-pointer ${isCopied ? "text-app-success" : "text-app-gray-800 dark:text-app-white"}`} />
120-
) : (
121-
<HiOutlineDuplicate className={`cursor-pointer ${isCopied ? "text-app-success" : "text-app-gray-800 dark:text-app-white"}`} />
122-
)}
123-
</div>
136+
{
137+
selectedWallet && (
138+
<Dropdown
139+
options={walletAddresses.map((wallet) => ({ name: getTruncateString(wallet.address), value: wallet.address }))}
140+
defaultValue={selectedWallet}
141+
onChange={(val) => setSelectedWallet(val as string)}
142+
classes={{ container: "w-full" }}
143+
/>
144+
)
145+
}
146+
<TextField
147+
value={newWalletName}
148+
onChange={(e) => setNewWalletName(e.target.value)}
149+
label="New Wallet Name"
150+
pill={true}
151+
type="text"
152+
className="w-full rounded-md"
153+
placeholder="Enter wallet name"
154+
/>
155+
<Button onClick={createNewWallet} className="my-4" variant="primary" block>
156+
Create New Wallet {selectedWallet}
124157
</Button>
158+
{
159+
account && (
160+
<Button
161+
size="sm"
162+
className="gap-2 w-full !border-app-gray-300 !text-app-gray-800 dark:!text-app-white"
163+
variant="secondary"
164+
onClick={handleCopyAddress}
165+
>
166+
{getTruncateString(account)}
167+
<div className="relative">
168+
{isCopied && (
169+
<div className="absolute bottom-[150%] left-1/2 -translate-x-1/2 bg-app-white dark:bg-app-gray-600 py-2 px-4 rounded-lg text-black text-sm text-center w-max shadow-md">
170+
Copied
171+
<div className="absolute border-8 border-b-0 border-r-transparent border-t-app-white dark:border-t-app-gray-600 border-l-transparent top-[100%] left-[calc(50%_-_8px)]"></div>
172+
</div>
173+
)}
174+
{isCopied ? (
175+
<HiOutlineCheckCircle className={`cursor-pointer ${isCopied ? "text-app-success" : "text-app-gray-800 dark:text-app-white"}`} />
176+
) : (
177+
<HiOutlineDuplicate className={`cursor-pointer ${isCopied ? "text-app-success" : "text-app-gray-800 dark:text-app-white"}`} />
178+
)}
179+
</div>
180+
</Button>
181+
)
182+
}
125183
</div>
126184
<Drawer
127185
open={openConsole}

demo/redirect-flow-example/src/components/transactions/TransactionCard.tsx

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,43 @@
11
import * as React from "react";
22
import { useCoreKit } from "../../composibles/useCoreKit";
33
import { Button } from "../Button";
4-
import { Card } from "../Card";
4+
import { Card, cn } from "../Card";
55
import { TextField } from "../TextField";
66
import useUnifiedRPC from "../../composibles/useRpc";
77
import { KeyType } from "@tkey/common-types";
88

99
const TransactionCard: React.FC = () => {
1010
const [amount, setAmount] = React.useState("0.0001");
11-
const { setDrawerHeading, setDrawerInfo, coreKitInstance } = useCoreKit();
11+
const [toAddress, setToAddress] = React.useState("");
12+
const { setDrawerHeading, setDrawerInfo, networkName } = useCoreKit();
1213
const [isLoading, setIsLoading] = React.useState(false);
1314
const { sendTransaction, account } = useUnifiedRPC();
15+
const [faucetLink, setFaucetLink] = React.useState("");
16+
17+
React.useEffect(() => {
18+
setToAddress(account);
19+
}, [account]);
20+
21+
React.useEffect(() => {
22+
if (networkName === "ETH") {
23+
setFaucetLink("https://cloud.google.com/application/web3/faucet/ethereum/sepolia");
24+
} else if (networkName === "SOL") {
25+
setFaucetLink("https://faucet.solana.com/");
26+
} else if (networkName === "BTC") {
27+
setFaucetLink("https://coinfaucet.eu/en/btc-testnet/");
28+
}
29+
}, [networkName]);
1430

1531
const sendWeb3AuthTx = async () => {
1632
setIsLoading(true);
1733

1834
try {
19-
const toAddress = account;
20-
if (!toAddress) {
35+
const sendAddress = toAddress || account;
36+
if (!sendAddress) {
2137
console.error("No account found");
2238
return;
2339
}
24-
const receipt = await sendTransaction(toAddress, amount);
40+
const receipt = await sendTransaction(sendAddress, amount);
2541
setDrawerHeading("Send Transaction Result");
2642
setDrawerInfo(`${receipt}`);
2743
} catch (error) {
@@ -33,15 +49,34 @@ const TransactionCard: React.FC = () => {
3349
}
3450
};
3551

52+
const openFaucet = () => {
53+
window.open(faucetLink, "blank");
54+
}
55+
3656
return (
3757
<Card className="px-8 py-6 w-full !rounded-2xl !shadow-modal !border-0 dark:!border-app-gray-800 dark:!shadow-dark">
3858
<div className="text-center">
39-
<h3 className="font-semibold text-app-gray-900 dark:text-app-white mb-4">Send Transaction</h3>
59+
<h3 className="font-semibold text-app-gray-900 dark:text-app-white mb-2">Send Transaction</h3>
60+
<Button variant={"text"}
61+
className={cn("text-center w-full text-sm font-medium mb-2")}
62+
onClick={openFaucet}>
63+
Visit {networkName} Faucet
64+
</Button>
65+
<TextField
66+
value={toAddress}
67+
onChange={(e) => setToAddress(e.target.value)}
68+
label={`To Address`}
69+
placeholder={`Enter To Address`}
70+
className="mb-4 rounded-md"
71+
classes={{
72+
container: "flex flex-col justify-center items-center",
73+
}}
74+
/>
4075
<TextField
4176
value={amount}
4277
onChange={(e) => setAmount(e.target.value)}
43-
label={`Amount ${coreKitInstance?.keyType === KeyType.ed25519 ? "(SOL)" : "(ETH)"}`}
44-
placeholder={`Enter amount in ${coreKitInstance?.keyType === KeyType.ed25519 ? "SOL" : "ETH"}`}
78+
label={`Amount ${networkName}`}
79+
placeholder={`Enter amount in ${networkName}`}
4580
className="mb-4 rounded-md"
4681
classes={{
4782
container: "flex flex-col justify-center items-center",

0 commit comments

Comments
 (0)