Skip to content

Commit

Permalink
finally got xrpl address
Browse files Browse the repository at this point in the history
  • Loading branch information
yuki-js committed Dec 9, 2023
1 parent 7090ff6 commit ed14df1
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 22 deletions.
33 changes: 33 additions & 0 deletions frontend/src/blockchain/AddrFetcher.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { useEffect } from "react";
import { web3auth } from ".";
import { ADAPTER_EVENTS } from "@web3auth/base";

export function AddrFetcher() {
useEffect(() => {
window.currentAddr = null;
const conn = async () => {
if (!web3auth.connected) {
window.currentAddr = null;
console.log("DISCONNECTED");
return;
}
const result = (await web3auth.provider!.request({
method: "xrpl_getAccounts",
})) as string[];
window.currentAddr = result[0];
console.log("CONNECTED", window.currentAddr);
};
conn();

web3auth.on(ADAPTER_EVENTS.CONNECTED, conn);
web3auth.on(ADAPTER_EVENTS.DISCONNECTED, conn);
console.log("attached event handlers");

return () => {
web3auth.off("CONNECTED", conn);
web3auth.off("DISCONNECTED", conn);
console.log("detached event handlers");
};
}, []);
return <></>;
}
47 changes: 27 additions & 20 deletions frontend/src/blockchain/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@ import {
XrplPrivateKeyProvider,
getXRPLChainConfig,
} from "@web3auth/xrpl-provider";
import { convertStringToHex, Payment, xrpToDrops } from "xrpl";

const chainConfig = getXRPLChainConfig("devnet");

const web3auth = new Web3AuthNoModal({
chainConfig: {
chainNamespace: CHAIN_NAMESPACES.OTHER,
chainId: "0x3",
rpcTarget: "https://s.altnet.rippletest.net:51234",
},
const chainConfig = {
...getXRPLChainConfig("testnet"),
wsTarget: "wss://testnet.xrpl-labs.com/",
rpcTarget: "https://testnet.xrpl-labs.com/",
};
console.log("chainConfig", chainConfig);
export const web3auth = new Web3AuthNoModal({
chainConfig,
clientId:
"BOyXHg9WYpxLJEzFcvTctGwM_Bof6WItmq31Mf8A4WFutBFv-urrckEGXx2lEZ0DN0511F13_QhKYgTgwY2mBQI", // get from https://dashboard.web3auth.io
web3AuthNetwork: "sapphire_devnet",
Expand All @@ -28,31 +27,39 @@ const xrplProvider = new XrplPrivateKeyProvider({
const adapter = new OpenloginAdapter({
privateKeyProvider: xrplProvider,
adapterSettings: {
uxMode: "redirect",
uxMode: "popup",
loginConfig: {
jwt: {
verifier: "b2clogin",
verifier: "xrpense-auth0",
typeOfLogin: "jwt",
name: "企業アカウントでシングルサインオン",
description: "Azure AD B2C経由で企業アカウントでログイン",
clientId: "fd3b26bf-6c50-411f-bc92-052b5693e413",
description: "Auth0経由で企業アカウントでログイン",
clientId: "a3ljjOhdkgUdG4xSMQ4rlFJsqAfVy7jg",
},
},
},
});
web3auth.configureAdapter(adapter);

web3auth.init();
export async function signIn() {
await web3auth.init();
if (web3auth.connected) {
return web3auth.provider;
}
const provider = await web3auth.connectTo(WALLET_ADAPTERS.OPENLOGIN, {
loginProvider: "jwt",
extraLoginOptions: {
domain:
"https://xrpensehackathon.b2clogin.com/xrpensehackathon.onmicrosoft.com/oauth2/v2.0/?p=b2c_1_xrpenselogin",
response_type: "token",
scope:
"openid offline_access https://xrpensehackathon.onmicrosoft.com/xrpense/email.read",
response_mode: "fragment",
domain: "https://dev-lvysvbffb7t6gp8w.us.auth0.com",
},
});
if (!provider) {
alert("provider is undefined");
}
return provider;
}

declare global {
interface Window {
currentAddr: string | null;
}
}
7 changes: 6 additions & 1 deletion frontend/src/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,16 @@ import * as ph from "@plasmicapp/react-web/lib/host";
import { UserTypeContext } from "../components/plasmic/xrpense/PlasmicGlobalVariant__UserType";
import GlobalContextsProvider from "../components/plasmic/xrpense/PlasmicGlobalContextsProvider";
import "@/styles/globals.css";
import { web3auth } from "@/blockchain";
import { AddrFetcher } from "@/blockchain/AddrFetcher";

export default function MyApp({ Component, pageProps }: AppProps) {
return (
<PlasmicRootProvider Head={Head}>
<UserTypeContext.Provider value={undefined}>
<AddrFetcher />
<UserTypeContext.Provider
value={web3auth.connected ? "user" : "anonymous"}
>
<GlobalContextsProvider>
<ph.PageParamsProvider
route={useRouter()?.pathname}
Expand Down
10 changes: 9 additions & 1 deletion frontend/src/pages/signin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,15 @@ import { useRouter } from "next/router";
import { signIn } from "@/blockchain";

function SignIn() {
return <PlasmicSignIn onLogin={signIn} />;
const router = useRouter();
return (
<PlasmicSignIn
onLogin={async () => {
await signIn();
router.push("/");
}}
/>
);
}

export default SignIn;

0 comments on commit ed14df1

Please sign in to comment.