Skip to content

Commit

Permalink
chore: tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
jxom committed Dec 18, 2024
1 parent 12f8d49 commit edebe18
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 57 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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',
}[]
}]
}
Expand Down
52 changes: 7 additions & 45 deletions src/internal/accountDelegation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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',
Expand Down Expand Up @@ -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<chain extends Chain | undefined>(
client: Client<Transport, chain>,
Expand Down
18 changes: 8 additions & 10 deletions src/internal/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
2 changes: 1 addition & 1 deletion src/internal/rpcSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ export type GrantSessionParameters = {
expiry?: number | undefined
keys?:
| readonly {
algorithm: 'p256' | 'secp256k1'
publicKey: Hex.Hex
type: AccountDelegation.KeyType
}[]
| undefined
}
Expand Down

0 comments on commit edebe18

Please sign in to comment.