@keplr-ewallet/ewallet-sdk-cosmos
Version:
69 lines • 2.36 kB
JavaScript
export async function lazyInit(cosmosEWallet) {
const eWalletStateRes = await cosmosEWallet.eWallet.waitUntilInitialized;
if (!eWalletStateRes.success) {
return { success: false, err: { type: "eWallet failed to initailize" } };
}
setUpEventHandlers.call(cosmosEWallet);
const eWalletState = eWalletStateRes.data;
if (eWalletState.publicKey) {
const pk = Buffer.from(eWalletState.publicKey, "hex");
cosmosEWallet.state = {
publicKey: pk,
publicKeyRaw: eWalletState.publicKey,
};
cosmosEWallet.eventEmitter.emit({
type: "accountsChanged",
email: eWalletState.email,
publicKey: pk,
});
}
else {
cosmosEWallet.state = {
publicKey: null,
publicKeyRaw: null,
};
cosmosEWallet.eventEmitter.emit({
type: "accountsChanged",
email: eWalletState.email,
publicKey: null,
});
}
return { success: true, data: cosmosEWallet.state };
}
export function setUpEventHandlers() {
console.log("[keplr-cosmos] set up event handlers");
this.eWallet.on({
type: "CORE__accountsChanged",
handler: (payload) => {
console.log("[keplr-cosmos] CORE__accountsChanged callback, payload: %s", JSON.stringify(payload));
const { publicKey, email } = payload;
if (this.state.publicKeyRaw !== publicKey) {
if (publicKey !== null) {
const pk = Buffer.from(publicKey, "hex");
this.state = {
publicKey: pk,
publicKeyRaw: publicKey,
};
}
else {
this.state = {
publicKey: null,
publicKeyRaw: null,
};
}
this.eventEmitter.emit({
type: "accountsChanged",
email: email,
publicKey: this.state.publicKey,
});
}
},
});
this.eWallet.on({
type: "CORE__chainChanged",
handler: (_payload) => {
this.eventEmitter.emit({ type: "chainChanged" });
},
});
}
//# sourceMappingURL=lazy_init.js.map