Skip to content

Account.getKey

Extracts a signing key from a Porto Account based on the provided parameters.

Usage

Providing no parameters will return the first "admin" key.

import { Account, Key } from 'porto/viem'
 
const passkey = await Key.createWebAuthnP256({
  label: 'My Passkey',
})
 
const account = Account.fromPrivateKey('0x...', {
  keys: [passkey],
})
 
const key = Account.getKey(account) 

By Index

import { Account, Key } from 'porto/viem'
 
const passkey1 = await Key.createWebAuthnP256({
  label: 'Passkey 1',
})
 
const passkey2 = await Key.createWebAuthnP256({
  label: 'Passkey 2',
})
 
const account = Account.fromPrivateKey('0x...', {
  keys: [passkey1, passkey2],
})
 
const firstKey = Account.getKey(account, { key: 0 }) 
const secondKey = Account.getKey(account, { key: 1 }) 

By Role

Returns the first key of the specified role.

import { Account, Key } from 'porto/viem'
 
const adminKey = await Key.createWebAuthnP256({
  label: 'My passkey',
  role: 'admin',
})
 
const sessionKey = await Key.createP256({
  role: 'session',
})
 
const account = Account.fromPrivateKey('0x...', {
  keys: [adminKey, sessionKey],
})
 
const key1 = Account.getKey(account, { role: 'admin' }) 
const key2 = Account.getKey(account, { role: 'session' }) 

Parameters

account

  • Type: Account

The Porto Account to extract the key from.

options

options.key

  • Type: number | undefined

The index of the key to extract.

options.role

  • Type: "admin" | "session" | undefined

The role of the key to extract.

Return Value

type ReturnValue = Key.Key | undefined