@oko-wallet/oko-sdk-cosmos
Version:
72 lines • 2.4 kB
JavaScript
export async function lazyInit(okoCosmosWallet) {
const walletStateRes = await okoCosmosWallet.okoWallet.waitUntilInitialized;
if (!walletStateRes.success) {
return {
success: false,
err: { type: "oko_cosmos_wallet_lazy_init_fail" },
};
}
setUpEventHandlers.call(okoCosmosWallet);
const walletState = walletStateRes.data;
if (walletState.publicKey) {
const pk = Buffer.from(walletState.publicKey, "hex");
okoCosmosWallet.state = {
publicKey: pk,
publicKeyRaw: walletState.publicKey,
};
okoCosmosWallet.eventEmitter.emit({
type: "accountsChanged",
email: walletState.email,
publicKey: pk,
});
}
else {
okoCosmosWallet.state = {
publicKey: null,
publicKeyRaw: null,
};
okoCosmosWallet.eventEmitter.emit({
type: "accountsChanged",
email: walletState.email,
publicKey: null,
});
}
return { success: true, data: okoCosmosWallet.state };
}
export function setUpEventHandlers() {
console.log("[oko-cosmos] set up event handlers");
this.okoWallet.on({
type: "CORE__accountsChanged",
handler: (payload) => {
console.log("[oko-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.okoWallet.on({
type: "CORE__chainChanged",
handler: (_payload) => {
this.eventEmitter.emit({ type: "chainChanged" });
},
});
}
//# sourceMappingURL=lazy_init.js.map