ic-auth
Version:
A simple to use, modular package for integrating Internet Computer authentication providers into your app.
56 lines (55 loc) • 2.46 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.StoicWallet = void 0;
const react_1 = require("react");
// @ts-ignore
const ic_stoic_identity_1 = require("ic-stoic-identity");
const agent_1 = require("@dfinity/agent");
const helloIDL = require("../interfaces/hello");
const stoic_png_1 = require("../assets/logos/stoic.png");
require("../assets/index.css");
function StoicWallet(props) {
const changeProvider = props.changeProvider;
const [stoicButtonText, setStoicButtonText] = (0, react_1.useState)("Stoic");
const [loggedIn, setLoggedIn] = (0, react_1.useState)(false);
const buttonState = (0, react_1.useRef)(null);
const stoicStatus = (0, react_1.useRef)(null);
const stoicLogin = async () => {
let identity;
// @ts-ignore
ic_stoic_identity_1.StoicIdentity.load().then(async (identity) => {
if (identity == false) {
identity = await ic_stoic_identity_1.StoicIdentity.connect();
}
const theUserPrincipal = identity.getPrincipal().toText();
changeProvider(theUserPrincipal);
setStoicButtonText("Connected!");
setLoggedIn(true);
buttonState.current.disabled = true;
stoicStatus.current.style.backgroundColor = "#42ff0f";
});
return identity;
};
const createStoicActor = async () => {
// @ts-ignore
let identity = await stoicLogin();
const host = "https://ic0.app";
const idlFactory = helloIDL.idlFactory;
const actor = agent_1.Actor.createActor(idlFactory, {
agent: new agent_1.HttpAgent({ identity, host }),
canisterId: "oyjva-2yaaa-aaaam-qbaya-cai"
});
return actor;
};
const testStoic = async () => {
const actor = await createStoicActor();
const result = await actor.hello_world();
console.log(result);
};
return (react_1.default.createElement("div", { className: "walletContainer" },
react_1.default.createElement("button", { ref: buttonState, onClick: !loggedIn ? testStoic : undefined },
react_1.default.createElement("img", { src: stoic_png_1.default }),
react_1.default.createElement("p", null, stoicButtonText),
react_1.default.createElement("div", { ref: stoicStatus, className: "statusBubble", id: "statusBubble" }))));
}
exports.StoicWallet = StoicWallet;