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

Feat/passkeys #8

Merged
merged 5 commits into from
Oct 4, 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
2 changes: 1 addition & 1 deletion .project-cid
Original file line number Diff line number Diff line change
@@ -1 +1 @@
QmVU2fQeK1qwJhxf5Aj9CXkc4vCNgkV2rTVQkSh1W8zP8m
QmdKg5RktekMJbH1RjoTSfc8Uji9y4HVHpVVHBQW2z29qR
4 changes: 2 additions & 2 deletions project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import {
// These defaults are the testnet values
const HUB_CONTRACT_CODE_ID = process.env.HUB_CONTRACT_CODE_ID || "7";
const SMART_ACCOUNT_CONTRACT_CODE_ID =
process.env.SMART_ACCOUNT_CONTRACT_CODE_ID || "21";
process.env.SMART_ACCOUNT_CONTRACT_CODE_ID || "793";

const CHAIN_ID = process.env.CHAIN_ID || "xion-testnet-1";
const ENDPOINT_URL =
process.env.ENDPOINT_URL || "https://rpc.xion-testnet-1.burnt.com:443";
const START_BLOCK = Number(process.env.START_BLOCK || "3371922");
const START_BLOCK = Number(process.env.START_BLOCK || "9238800");

const project: CosmosProject = {
specVersion: "1.0.0",
Expand Down
2 changes: 2 additions & 0 deletions src/interfaces/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,13 @@ export type IAuthenticator = {
Ed25519?: { pubkey: string };
EthWallet?: { address: string };
Jwt?: { aud: string; sub: string };
Passkey? : { credential: string };
};

export type IAddAuthenticator = {
Secp256K1?: { id: number; pubkey: string; signature: string };
Ed25519?: { id: number; pubkey: string; signature: string };
EthWallet?: { id: number; address: string; signature: string };
Jwt?: { id: number; aud: string; sub: string; token: string };
Passkey? : { id: number, url: string, credential: string };
};
56 changes: 39 additions & 17 deletions src/smartContract/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ import { CosmosEvent } from "@subql/types-cosmos";
import { SmartAccount, SmartAccountAuthenticator } from "../types";
import { IAddAuthenticator, IAuthenticator } from "../interfaces";
export async function handleSmartAccountContractInstantiateMetadataHelper(
event: CosmosEvent,
event: CosmosEvent
): Promise<void> {
logger.info(
`Smart Account Data Instantiate event detected - ${event.event.type}`,
`Smart Account Data Instantiate event detected - ${event.event.type}`
);
let contractAddress = event.event.attributes.find(
(attr) => attr.key === "_contract_address",
(attr) => attr.key === "_contract_address"
)?.value;

let authenticatorIndex = Number(
event.event.attributes.find((attr) => attr.key === "authenticator_id")
?.value || "0",
?.value || "0"
);

if (contractAddress) {
Expand All @@ -24,7 +24,7 @@ export async function handleSmartAccountContractInstantiateMetadataHelper(
await smartAccount.save();

let authenticatorData = event.event.attributes.find(
(attr) => attr.key === "authenticator",
(attr) => attr.key === "authenticator"
)?.value;
if (authenticatorData) {
let authData: IAuthenticator = JSON.parse(authenticatorData);
Expand All @@ -43,6 +43,18 @@ export async function handleSmartAccountContractInstantiateMetadataHelper(
case "Jwt":
authenticator = `${authData[authType]?.aud}.${authData[authType]?.sub}`;
break;
case "Passkey": {
const cred = authData[authType]?.credential;
if (!cred) {
logger.info("No credential found for the passkey authenticator");
return;
}
// credential is base64 encoded, decode it
const credString = Buffer.from(cred, "base64").toString("utf-8");
const parsedCred = JSON.parse(credString);
authenticator = parsedCred.ID;
break;
}
default:
logger.info(`Unknown authenticator type - ${authType}`);
return;
Expand All @@ -69,11 +81,11 @@ export async function handleSmartAccountContractInstantiateMetadataHelper(
}

export async function handleSmartAccountContractAddAuthenticatorHelper(
event: CosmosEvent,
event: CosmosEvent
): Promise<void> {
logger.info("Smart Account Add Auth event detected");
let contractAddress = event.event.attributes.find(
(attr) => attr.key === "_contract_address",
(attr) => attr.key === "_contract_address"
)?.value;
if (contractAddress) {
let smartAccount = await SmartAccount.get(contractAddress);
Expand All @@ -82,30 +94,40 @@ export async function handleSmartAccountContractAddAuthenticatorHelper(
return;
} else {
let authenticatorData = event.event.attributes.find(
(attr) => attr.key === "authenticator",
(attr) => attr.key === "authenticator"
)?.value;
if (authenticatorData) {
let authData: IAddAuthenticator = JSON.parse(authenticatorData);
for (const authType of Object.keys(authData)) {
let authenticator: string | undefined;
let authenticatorIndex: number | undefined;
const authenticatorIndex = authData[authType as keyof IAddAuthenticator]?.id;
switch (authType) {
case "Secp256K1":
authenticatorIndex = authData[authType]?.id;
authenticator = authData[authType]?.pubkey;
break;
case "Ed25519":
authenticatorIndex = authData[authType]?.id;
authenticator = authData[authType]?.pubkey;
break;
case "EthWallet":
authenticatorIndex = authData[authType]?.id;
authenticator = authData[authType]?.address;
break;
case "Jwt":
authenticatorIndex = authData[authType]?.id;
authenticator = `${authData[authType]?.aud}.${authData[authType]?.sub}`;
break;
case "Passkey": {
const cred = authData[authType]?.credential;
if (!cred) {
logger.info(
"No credential found for the passkey authenticator"
);
return;
}
// credential is base64 encoded, decode it
const credString = Buffer.from(cred, "base64").toString("utf-8");
const parsedCred = JSON.parse(credString);
authenticator = parsedCred.ID;
break;
}
default:
logger.info(`Unknown authenticator type - ${authType}`);
return;
Expand All @@ -118,7 +140,7 @@ export async function handleSmartAccountContractAddAuthenticatorHelper(

if (!authenticatorIndex) {
logger.info(
`No authenticator index found for the type - ${authType}`,
`No authenticator index found for the type - ${authType}`
);
return;
}
Expand Down Expand Up @@ -147,12 +169,12 @@ export async function handleSmartAccountContractAddAuthenticatorHelper(
}

export async function handleSmartAccountContractRemoveAuthenticatorHelper(
event: CosmosEvent,
event: CosmosEvent
): Promise<void> {
if (event.event.type === "wasm-remove_auth_method") {
logger.info("Smart Account Remove Auth event detected");
const contractAddress = event.event.attributes.find(
(attr) => attr.key === "_contract_address",
(attr) => attr.key === "_contract_address"
)?.value;
if (contractAddress) {
const smartAccount = await SmartAccount.get(contractAddress);
Expand All @@ -161,7 +183,7 @@ export async function handleSmartAccountContractRemoveAuthenticatorHelper(
return;
} else {
const authenticatorId = event.event.attributes.find(
(attr) => attr.key === "authenticator_id",
(attr) => attr.key === "authenticator_id"
)?.value;
if (authenticatorId && Number(authenticatorId)) {
const authId = `${contractAddress}-${authenticatorId}`;
Expand Down
Loading