Skip to content

Commit

Permalink
change localStorage format of pubkeyCoordinates from string to bigint
Browse files Browse the repository at this point in the history
  • Loading branch information
Sednaoui committed Mar 29, 2024
1 parent 5dc44ee commit de9d7c4
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 37 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useMemo } from 'react'
import { SafeAccountWebAuth as SafeAccount, WebauthPublicKey } from 'abstractionkit'
import { SafeAccountWebAuth as SafeAccount } from 'abstractionkit'

import { PasskeyLocalStorageFormat } from '../logic/passkeys'
import { setItem } from '../logic/storage'
Expand All @@ -8,15 +8,10 @@ function PasskeyCard({ passkey, handleCreatePasskeyClick }: { passkey?: PasskeyL
const getAccountAddress = useMemo(() => {
if (!passkey) return undefined

const webauthPublicKey: WebauthPublicKey = {
x: BigInt(passkey.pubkeyCoordinates.x),
y: BigInt(passkey.pubkeyCoordinates.y),
}
const accountAddress = SafeAccount.createAccountAddress([passkey.pubkeyCoordinates]);
setItem('accountAddress', accountAddress);

const smartAccount = SafeAccount.initializeNewAccount([webauthPublicKey])

setItem('accountAddress', smartAccount.accountAddress)
return smartAccount.accountAddress
return accountAddress;
}, [passkey])

return passkey ? (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
MetaTransaction,
DummySignature,
CandidePaymaster,
WebauthPublicKey,
} from "abstractionkit";

import { PasskeyLocalStorageFormat } from "../logic/passkeys";
Expand Down Expand Up @@ -53,12 +52,7 @@ function SafeCard({ passkey }: { passkey: PasskeyLocalStorageFormat }) {
data: mintTransactionCallData,
};

const webauthPublicKey: WebauthPublicKey = {
x: BigInt(passkey.pubkeyCoordinates.x),
y: BigInt(passkey.pubkeyCoordinates.y),
};

const safeAccount = SafeAccount.initializeNewAccount([webauthPublicKey]);
const safeAccount = SafeAccount.initializeNewAccount([passkey.pubkeyCoordinates]);

let userOperation = await safeAccount.createUserOperation(
[mintTransaction],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ type PasskeyCredential = {

type PasskeyCredentialWithPubkeyCoordinates = PasskeyCredential & {
pubkeyCoordinates: {
x: string
y: string
x: bigint
y: bigint
}
}

Expand Down Expand Up @@ -75,8 +75,8 @@ async function createPasskey(): Promise<PasskeyCredentialWithPubkeyCoordinates>
// Create a PasskeyCredentialWithPubkeyCoordinates object
const passkeyWithCoordinates: PasskeyCredentialWithPubkeyCoordinates = Object.assign(passkeyCredential, {
pubkeyCoordinates: {
x: '0x' + Buffer.from(exportedKeyWithXYCoordinates.x, 'base64').toString('hex'),
y: '0x' + Buffer.from(exportedKeyWithXYCoordinates.y, 'base64').toString('hex'),
x: BigInt('0x' + Buffer.from(exportedKeyWithXYCoordinates.x, 'base64').toString('hex')),
y: BigInt('0x' + Buffer.from(exportedKeyWithXYCoordinates.y, 'base64').toString('hex')),
},
})

Expand All @@ -86,8 +86,8 @@ async function createPasskey(): Promise<PasskeyCredentialWithPubkeyCoordinates>
export type PasskeyLocalStorageFormat = {
rawId: string
pubkeyCoordinates: {
x: string
y: string
x: bigint
y: bigint
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@
* @template T - The type of the value being stored.
*/
function setItem<T>(key: string, value: T) {
// to prevent silly mistakes with double stringifying
if (typeof value === 'string') {
localStorage.setItem(key, value)
} else {
localStorage.setItem(key, JSON.stringify(value))
}
// to prevent silly mistakes with double stringifying
if (typeof value === "string") {
localStorage.setItem(key, value);
} else {
localStorage.setItem(
key,
JSON.stringify(value, (_key, value) =>
typeof value === "bigint" ? "0x" + value.toString(16) : value,
),
);
}
}

/**
Expand All @@ -20,7 +25,7 @@ function setItem<T>(key: string, value: T) {
* @returns The value associated with the key, or null if the key does not exist.
*/
function getItem(key: string): string | null {
return localStorage.getItem(key)
return localStorage.getItem(key);
}

export { setItem, getItem }
export { setItem, getItem };
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
WebauthSignatureData,
SendUseroperationResponse,
UserOperation,
WebauthPublicKey,
} from 'abstractionkit'

import { PasskeyLocalStorageFormat } from './passkeys'
Expand Down Expand Up @@ -111,13 +110,8 @@ async function signAndSendUserOp(

const webauthSignature: string = SafeAccount.createWebAuthnSignature(webauthSignatureData)

const webauthPublicKey: WebauthPublicKey = {
x: BigInt(passkey.pubkeyCoordinates.x),
y: BigInt(passkey.pubkeyCoordinates.y),
}

const SignerSignaturePair: SignerSignaturePair = {
signer: webauthPublicKey,
signer: passkey.pubkeyCoordinates,
signature: webauthSignature,
}

Expand Down

0 comments on commit de9d7c4

Please sign in to comment.