Skip to content

Commit

Permalink
fix: storing kukai credentials to use in other sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
talha-trili committed Jan 6, 2025
1 parent a66f680 commit 3ec99b6
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 56 deletions.
2 changes: 1 addition & 1 deletion WebGLFrontend/output/StreamingAssets/webgl-frontend.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion WebGLFrontend/output/StreamingAssets/webgl-frontend.js.map

Large diffs are not rendered by default.

128 changes: 74 additions & 54 deletions WebGLFrontend/src/WalletProviders/Kukai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,39 @@ class KukaiWallet extends BaseWallet implements Wallet {
initInProgress: boolean;

constructor(appName: string, appUrl: string, iconUrl: string, unityObjectName: string) {
console.log("KukaiWallet constructor", appName, appUrl, iconUrl);
super(appName, appUrl, iconUrl, unityObjectName);
this.StoreCredentials();
}

private async StoreCredentials() {
const storedNetwork = localStorage.getItem("networkName");
if (storedNetwork) {
this.networkName = storedNetwork;
}

const storedPk = localStorage.getItem("kukaiUserPk");
const storedPkh = localStorage.getItem("kukaiUserPkh");
const storedAuth = localStorage.getItem("kukaiUserAuthResponse");
const storedUserData = localStorage.getItem("kukaiUserData");

if (storedPk && storedPkh) {
this.client = new KukaiEmbed({ net: this.networkName });
await this.client.init();
this.client.user.pk = storedPk;
this.client.user.pkh = storedPkh;

if (storedAuth) {
try {
this.client.user.authResponse = JSON.parse(storedAuth);
} catch {}
}

if (storedUserData) {
try {
this.client.user.userData = JSON.parse(storedUserData);
} catch {}
}
}
}

SetNetwork(networkName: string, rpcUrl: string) {
Expand All @@ -18,77 +49,64 @@ class KukaiWallet extends BaseWallet implements Wallet {
}

async ConnectAccount() {
console.log("KukaiWallet ConnectAccount");

if (!this.client?.initialized) {

console.log("!this.client?.initialized");
console.log("initInProgress", this.initInProgress);

if (!this.client) {
this.client = new KukaiEmbed({ net: this.networkName });
}

if (!this.client.initialized) {
if (this.initInProgress) return;

this.client = new KukaiEmbed({
net: this.networkName,
});

console.log("this.client", this.client)

this.initInProgress = true;

try {
console.log("this.client.init() 1");
await this.client.init();
console.log("this.client.init() 2");
}
catch (error) {
console.error(`Error during initializing Kukai, ${error.message}`);
}
finally {
} finally {
this.initInProgress = false;
}

}

console.log("this.client.user", this.client.user);

if (this.client.user) {
if (this.client.user && this.client.user.pk && this.client.user.pkh) {
this.CallUnityOnAccountConnected({
walletAddress: this.client.user.pkh,
publicKey: this.client.user.pk,
accountInfo: null
});
} else {
console.log("KukaiWallet ConnectAccount !this.client.user");
try {
console.log("KukaiWallet ConnectAccount try");
await this.client.login();
this.CallUnityOnAccountConnected({
walletAddress: this.client.user.pkh,
publicKey: this.client.user.pk,
accountInfo: null
});
} catch (error) {
console.error(`Error during connecting account, ${error.message}`);
this.CallUnityOnAccountFailedToConnect(error);
return;
}

try {
await this.client.login();
localStorage.setItem("kukaiUserPk", this.client.user.pk);
localStorage.setItem("kukaiUserPkh", this.client.user.pkh);

if (this.client.user.authResponse) {
localStorage.setItem("kukaiUserAuthResponse", JSON.stringify(this.client.user.authResponse));
}

if (this.client.user.userData) {
localStorage.setItem("kukaiUserData", JSON.stringify(this.client.user.userData));
}

this.CallUnityOnAccountConnected({
walletAddress: this.client.user.pkh,
publicKey: this.client.user.pk,
accountInfo: null
});
} catch (error) {
this.CallUnityOnAccountFailedToConnect(error);
}
}

GetActiveAccountAddress() {
return this.client?.user?.pkh ?? "";
}

async SendContract(
destination: string,
amount: string,
entryPoint: string,
parameter: string
) {
async SendContract(destination: string, amount: string, entryPoint: string, parameter: string) {
try {
if (!this.client) {
await this.ConnectAccount();
}
const transactionHash = await this.client.send(
this.GetOperationsList(destination, amount, entryPoint, parameter)
);

this.CallUnityOnContractCallInjected({ transactionHash });
} catch (error) {
this.CallUnityOnContractCallFailed(error);
Expand All @@ -101,7 +119,6 @@ class KukaiWallet extends BaseWallet implements Wallet {
// @ts-ignore
this.GetOriginationOperationsList(script, delegateAddress)
);

this.CallUnityOnContractCallInjected({ transactionHash });
} catch (error) {
this.CallUnityOnContractCallFailed(error);
Expand All @@ -113,12 +130,10 @@ class KukaiWallet extends BaseWallet implements Wallet {
this.NumToSigningType(signingType),
plainTextPayload
);
try{
try {
const signature = await this.client.signExpr(hexPayload);
this.CallUnityOnPayloadSigned({ signature });
}
catch (error) {
console.error(`Error during payload signing, ${error.message}`);
} catch (error) {
this.CallUnityOnPayloadSignFailed(error);
}
}
Expand All @@ -130,10 +145,15 @@ class KukaiWallet extends BaseWallet implements Wallet {
accountInfo: null
};

await this.client?.logout();
localStorage.removeItem("kukaiUserPk");
localStorage.removeItem("kukaiUserPkh");
localStorage.removeItem("kukaiUserAuthResponse");
localStorage.removeItem("kukaiUserData");
localStorage.removeItem("networkName");

await this.client?.logout();
this.CallUnityOnAccountDisconnected(accountInfo);
}
}

export default KukaiWallet;
export default KukaiWallet;

0 comments on commit 3ec99b6

Please sign in to comment.