From edebe185eea13ebdda0f4215fc1fb53fa1a628de Mon Sep 17 00:00:00 2001 From: jxom <7336481+jxom@users.noreply.github.com> Date: Wed, 18 Dec 2024 07:29:43 +0200 Subject: [PATCH] chore: tweaks --- README.md | 3 +- src/internal/accountDelegation.ts | 52 +++++-------------------------- src/internal/provider.ts | 18 +++++------ src/internal/rpcSchema.ts | 2 +- 4 files changed, 18 insertions(+), 57 deletions(-) diff --git a/README.md b/README.md index 50052f5f..47335b76 100644 --- a/README.md +++ b/README.md @@ -257,9 +257,10 @@ Grants a session on the account. expiry?: number // The keys to grant on the session. + // Defaults to a Web Crypto P256 key. keys?: { - algorithm: 'p256' | 'secp256k1', publicKey: `0x${string}`, + type: 'p256' | 'secp256k1' | 'webauthn', }[] }] } diff --git a/src/internal/accountDelegation.ts b/src/internal/accountDelegation.ts index 6fa7abf4..e94772e0 100644 --- a/src/internal/accountDelegation.ts +++ b/src/internal/accountDelegation.ts @@ -49,7 +49,11 @@ export type Calls = readonly { data?: Hex.Hex | undefined }[] -export type Key = WebAuthnKey | WebCryptoKey | Secp256k1Key +export type Key = + | WebAuthnKey + | WebCryptoKey + | Secp256k1Key + | BaseKey<'unknown', unknown> export type SerializedKey = { expiry: bigint @@ -73,10 +77,12 @@ export type Secp256k1Key = BaseKey<'secp256k1', {}> //////////////////////////////////////////////////////////////// const keyType = { + unknown: -1, p256: 0, webauthn: 1, secp256k1: 2, } as const +export type KeyType = keyof typeof keyType const keyTypeSerialized = { 0: 'p256', @@ -248,50 +254,6 @@ export declare namespace createWebCryptoKey { } } -/** Gets a WebCrypto-P256 key in the required type **/ -export function getWebCryptoKey( - parameters: getWebCryptoKey.Parameters, -): WebCryptoKey { - const { expiry, publicKey } = parameters - return { - expiry, - publicKey, - status: 'locked', - type: 'p256', - } -} - -export declare namespace getWebCryptoKey { - type Parameters = { - /** Expiry for the key. */ - expiry: bigint - /** Public key for the key. */ - publicKey: PublicKey.PublicKey - } -} - -/** Gets a Secp256k1Key in the required type **/ -export function getSecp256k1Key( - parameters: getSecp256k1Key.Parameters, -): Secp256k1Key { - const { expiry, publicKey } = parameters - return { - expiry, - publicKey, - status: 'locked', - type: 'secp256k1', - } -} - -export declare namespace getSecp256k1Key { - type Parameters = { - /** Expiry for the key. */ - expiry: bigint - /** Public key for the key. */ - publicKey: PublicKey.PublicKey - } -} - /** Executes calls on an Account. */ export async function execute( client: Client, diff --git a/src/internal/provider.ts b/src/internal/provider.ts index 0ee94550..a65a2bfc 100644 --- a/src/internal/provider.ts +++ b/src/internal/provider.ts @@ -253,16 +253,14 @@ export function from< let keysToAuthorize: AccountDelegation.Key[] = [] if (keys) { - keysToAuthorize = keys.map((key) => - key.algorithm === 'secp256k1' - ? AccountDelegation.getSecp256k1Key({ - expiry: BigInt(expiry), - publicKey: PublicKey.fromHex(key.publicKey), - }) - : AccountDelegation.getWebCryptoKey({ - expiry: BigInt(expiry), - publicKey: PublicKey.fromHex(key.publicKey), - }), + keysToAuthorize = keys.map( + (key) => + ({ + expiry: BigInt(expiry), + publicKey: PublicKey.fromHex(key.publicKey), + status: 'unlocked', + type: key.type as 'unknown', + }) as const, ) } else { const key = await AccountDelegation.createWebCryptoKey({ diff --git a/src/internal/rpcSchema.ts b/src/internal/rpcSchema.ts index d3bdb6a6..c69ff262 100644 --- a/src/internal/rpcSchema.ts +++ b/src/internal/rpcSchema.ts @@ -99,8 +99,8 @@ export type GrantSessionParameters = { expiry?: number | undefined keys?: | readonly { - algorithm: 'p256' | 'secp256k1' publicKey: Hex.Hex + type: AccountDelegation.KeyType }[] | undefined }