-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.cjs
204 lines (196 loc) · 10.8 KB
/
index.cjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/index.ts
var src_exports = {};
__export(src_exports, {
LoginWithFB: () => LoginWithFB_default,
LoginWithGoogle: () => LoginWithGoogle_default2,
WalletProvider: () => ConnectWalletProvider_default,
useWallet: () => useWallet
});
module.exports = __toCommonJS(src_exports);
// src/context/connectWallet.ts
var import_react = __toESM(require("react"), 1);
var ConnectWalletContext = (0, import_react.createContext)({
address: "",
client: void 0,
wallet: void 0,
login: (type) => {
return type;
},
logout: () => {
return false;
}
});
function useWallet() {
return import_react.default.useContext(ConnectWalletContext);
}
var connectWallet_default = ConnectWalletContext;
// src/hooks/useConnectWallet.tsx
var import_js_cookie = __toESM(require("js-cookie"), 1);
var import_react2 = require("react");
var import_socket = __toESM(require("socket.io-client"), 1);
var import_encoding = require("@cosmjs/encoding");
var import_proto_signing = require("@cosmjs/proto-signing");
var import_cosmwasm_stargate = require("@cosmjs/cosmwasm-stargate");
var getClient = async (rpc, key = "") => {
if (key.length > 0) {
try {
const privateKey = (0, import_encoding.toUtf8)(key);
const wallet = await import_proto_signing.DirectSecp256k1Wallet.fromKey(privateKey, "juno");
const client = await import_cosmwasm_stargate.SigningCosmWasmClient.connectWithSigner(
rpc,
wallet
);
return {
wallet,
client
};
} catch (e) {
console.log("wallet connection failed", e);
return void 0;
}
}
return void 0;
};
var useConnectWallet = (config) => {
const [address, setAddress] = (0, import_react2.useState)("");
const [client, setClient] = (0, import_react2.useState)("");
const [wallet, setWallet] = (0, import_react2.useState)("");
const count = (0, import_react2.useRef)(0);
const socket = (0, import_socket.default)(config.backend_url, {
transports: ["websocket"],
withCredentials: true
});
(0, import_react2.useEffect)(() => {
socket.on("getTokenRes", (msg) => {
if (msg.success && socket.id === msg.id) {
getClient(config.rpc, msg.token).then((cli) => {
setClient(cli.client);
setWallet(cli.wallet);
});
}
});
socket.on("error", () => {
console.log("socket disconnected");
});
return () => {
socket.off("connect");
socket.off("disconnect");
};
}, []);
const getToken = () => {
const cookie = import_js_cookie.default.get("cookie");
cookie && socket.id && socket.emit("getToken", { cookie, id: socket.id });
};
const initialize = () => {
getToken();
const address2 = import_js_cookie.default.get("address");
address2 && setAddress(address2);
};
(0, import_react2.useEffect)(() => {
const interval = setInterval(() => {
if (!client && count.current <= 7) {
initialize();
count.current += 1;
}
}, 1e3);
return () => clearInterval(interval);
}, []);
(0, import_react2.useEffect)(() => {
const handler = (event) => {
if (event.data.address) {
setAddress(event.data.address);
import_js_cookie.default.set("cookie", event.data.cookie);
import_js_cookie.default.set("address", event.data.address);
getToken();
}
};
window.addEventListener("message", handler);
return () => window.removeEventListener("message", handler);
}, []);
const login = (type) => {
const namespace = {
"sms": "/sms",
"google": "/login/federated/google",
"facebook": "/login/federated/facebook"
}[type];
console.log("REACT_APP_SOCKET_URL", namespace);
const url = config.backend_url + namespace;
const newWindow = window.open(url, "name", "height=600,width=600");
newWindow?.postMessage(JSON.stringify({ obj: window }), "*");
if (window.focus()) {
newWindow?.focus();
}
return false;
};
const logout = () => {
import_js_cookie.default.set("cookie", "");
import_js_cookie.default.set("address", "");
setAddress(void 0);
};
return { logout, address, client, login, wallet };
};
var useConnectWallet_default = useConnectWallet;
// src/providers/ConnectWalletProvider.tsx
function WalletProvider({ children, config }) {
const { address, client, login, logout, wallet } = useConnectWallet_default(config);
return /* @__PURE__ */ React.createElement(connectWallet_default.Provider, { value: { address, client, login, logout, wallet } }, children);
}
var ConnectWalletProvider_default = WalletProvider;
// src/components/LoginWithGoogle.tsx
var import_react3 = __toESM(require("react"), 1);
var import_classnames = __toESM(require("classnames"), 1);
// src/components/LoginWithGoogle.css
var LoginWithGoogle_default = {};
// src/components/LoginWithGoogle.tsx
var LoginWithGoogle = (props) => {
return /* @__PURE__ */ import_react3.default.createElement("div", { ...props, className: (0, import_classnames.default)(LoginWithGoogle_default.googleBtn, props?.className) }, /* @__PURE__ */ import_react3.default.createElement("div", { className: (0, import_classnames.default)(LoginWithGoogle_default.googleIconWrapper, "wrapper") }, /* @__PURE__ */ import_react3.default.createElement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A5MzQgMTQuNjA2NCAyMyAxMS45OTk2IDIzQzcuODEwNTUgMjMgNC4xNjg0NyAyMC42NTg2IDIuMzEwNTUgMTcuMjEyOUw1Ljg3NTI4IDE0LjI5NDlDNi44MDQyMiAxNi43NzQxIDkuMTk1ODIgMTguNTM5IDExLjk5OTYgMTguNTM5QzEzLjIwNDggMTguNTM5IDE0LjMzMzggMTguMjEzMiAxNS4zMDI2IDE3LjY0NDVMMTguODg1NSAyMC41Nzc2WiIgZmlsbD0iIzI4QjQ0NiIvPgo8cGF0aCBkPSJNMTkuMDIwOCAzLjUzMjQxTDE1LjQ1NzMgNi40NDk4MUMxNC40NTQ2IDUuODIzMDcgMTMuMjY5NCA1LjQ2MTAyIDExLjk5OTYgNS40NjEwMkM5LjEzMjMgNS40NjEwMiA2LjY5NTk3IDcuMzA2ODMgNS44MTM1NiA5Ljg3NDk0TDIuMjMwMDkgNi45NDEyMUgyLjIyOTQ5QzQuMDYwMjIgMy40MTE1NCA3Ljc0ODIzIDEgMTEuOTk5NiAxQzE0LjY2ODYgMSAxNy4xMTU4IDEuOTUwNzMgMTkuMDIwOCAzLjUzMjQxWiIgZmlsbD0iI0YxNDMzNiIvPgo8L3N2Zz4K"
}
)), /* @__PURE__ */ import_react3.default.createElement("div", { className: LoginWithGoogle_default.text }, /* @__PURE__ */ import_react3.default.createElement("p", { className: LoginWithGoogle_default.btnText }, /* @__PURE__ */ import_react3.default.createElement("b", null, "Continue with Google"))));
};
var LoginWithGoogle_default2 = LoginWithGoogle;
// src/components/LoginWithFB.tsx
var import_classnames2 = __toESM(require("classnames"), 1);
var import_react4 = __toESM(require("react"), 1);
var LoginWithFB = (props) => {
return /* @__PURE__ */ import_react4.default.createElement("div", { ...props, className: (0, import_classnames2.default)(LoginWithGoogle_default.googleBtn, props?.className) }, /* @__PURE__ */ import_react4.default.createElement("div", { className: LoginWithGoogle_default.googleIconWrapper }, /* @__PURE__ */ import_react4.default.createElement("img", { className: LoginWithGoogle_default.googleIcon, src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UgMjMuODU0MlYxNS40Njg4SDE2LjY3MTFaIiBmaWxsPSJ3aGl0ZSIvPgo8L3N2Zz4K" })), /* @__PURE__ */ import_react4.default.createElement("div", { className: LoginWithGoogle_default.text }, /* @__PURE__ */ import_react4.default.createElement("p", { className: LoginWithGoogle_default.btnText }, /* @__PURE__ */ import_react4.default.createElement("b", null, "Continue with Facebook"))));
};
var LoginWithFB_default = LoginWithFB;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
LoginWithFB,
LoginWithGoogle,
WalletProvider,
useWallet
});