Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Grant Expiration Check #228

Merged
merged 3 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/sharp-carpets-confess.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@burnt-labs/abstraxion-core": minor
---

Added a grant validity check to verify expiration
59 changes: 46 additions & 13 deletions packages/abstraxion-core/src/AbstraxionAuth.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { GasPrice } from "@cosmjs/stargate";
import { fetchConfig } from "@burnt-labs/constants";
import type { ContractGrantDescription, SpendLimit } from "@/types";
import type {
ContractGrantDescription,
GrantsResponse,
SpendLimit,
} from "@/types";
import { GranteeSignerClient } from "./GranteeSignerClient";
import { SignArbSecp256k1HdWallet } from "./SignArbSecp256k1HdWallet";

Expand Down Expand Up @@ -312,12 +316,20 @@ export class AbstraxionAuth {
const res = await fetch(url, {
cache: "no-store",
});
const data = await res.json();
if (data.grants.length > 0) {
return true;
const data: GrantsResponse = await res.json();
if (data.grants.length === 0) {
console.warn("No grants found.");
return false;
}
// Retry if no grants found
throw new Error("No grants found.");

// Check expiration for each grant
const currentTime = new Date().toISOString();
const validGrant = data.grants.some((grant) => {
const { expiration } = grant;
return !expiration || expiration > currentTime;
});

return validGrant;
} catch (error) {
console.warn("Error fetching grants: ", error);
const delay = Math.pow(2, retries) * 1000;
Expand All @@ -341,16 +353,37 @@ export class AbstraxionAuth {

/**
* Authenticates the user based on the presence of a local keypair and a granter address.
* If a local keypair and granter address are found, sets the abstract account and triggers authentication state change.
* This method is typically called to authenticate the user and persist state between renders.
* Also checks if the grant is still valid by verifying the expiration.
* If valid, sets the abstract account and triggers authentication state change.
* If expired, clears local state and prompts reauthorization.
*
* @returns {Promise<void>} - Resolves if authentication is successful or logs out the user otherwise.
*/
async authenticate(): Promise<void> {
const keypair = await this.getLocalKeypair();
const granter = this.getGranter();
try {
const keypair = await this.getLocalKeypair();
const granter = this.getGranter();

if (keypair && granter) {
this.abstractAccount = keypair;
this.triggerAuthStateChange(true);
if (!keypair || !granter) {
console.warn("Missing keypair or granter, cannot authenticate.");
return;
}

const accounts = await keypair.getAccounts();
const keypairAddress = accounts[0].address;

// Check for existing grants with an expiration check
const isGrantValid = await this.pollForGrants(keypairAddress, granter);
justinbarry marked this conversation as resolved.
Show resolved Hide resolved

if (isGrantValid) {
this.abstractAccount = keypair;
this.triggerAuthStateChange(true);
} else {
throw new Error("Grant expired or not found. Logging out.");
}
} catch (error) {
console.error("Error during authentication:", error);
this.logout();
}
}

Expand Down
Loading