115 lines
2.7 KiB
Plaintext
115 lines
2.7 KiB
Plaintext
# `Account.fromP256`
|
|
|
|
Instantiates an Account from a P256 private key.
|
|
|
|
## Usage
|
|
|
|
```ts twoslash
|
|
import { Account } from 'viem/tempo'
|
|
|
|
const account = Account.fromP256(
|
|
'0x...'
|
|
)
|
|
|
|
console.log('Address:', account.address)
|
|
// @log: Address: 0x...
|
|
```
|
|
|
|
### Access Keys
|
|
|
|
Create an account that acts as an access key for a parent account:
|
|
|
|
:::code-group
|
|
|
|
```ts twoslash [example.ts]
|
|
import { Account, P256 } from 'viem/tempo'
|
|
import { client } from './viem.config'
|
|
|
|
// Create root account
|
|
const account = Account.fromSecp256k1(
|
|
'0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80'
|
|
)
|
|
|
|
// Create access key
|
|
const accessKey = Account.fromP256(
|
|
P256.randomPrivateKey(),
|
|
{ access: account }
|
|
)
|
|
|
|
// Sign a key authorization
|
|
const keyAuthorization = await account.signKeyAuthorization(accessKey, {
|
|
expiry: Math.floor(Date.now() / 1000) + 86400, // 24 hour expiry
|
|
})
|
|
|
|
// Attach to next transaction
|
|
const receipt = await client.sendTransactionSync({
|
|
account: accessKey,
|
|
keyAuthorization,
|
|
to: '0xdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef',
|
|
})
|
|
|
|
// Now any subsequent transaction can be used with `accessKey`
|
|
// WITHOUT the `keyAuthorization` parameter!
|
|
```
|
|
|
|
```ts twoslash [viem.config.ts] filename="viem.config.ts"
|
|
// [!include ~/snippets/tempo/viem.config.ts:setup]
|
|
```
|
|
|
|
:::
|
|
|
|
## Return Type
|
|
|
|
The return type of `Account.fromP256` is backwards compatible with Viem's `Account` type.
|
|
|
|
```ts
|
|
type ReturnType = Account
|
|
|
|
type Account = {
|
|
/** Account address */
|
|
address: Address
|
|
/** Key type */
|
|
keyType: string
|
|
/** Public key (hex) */
|
|
publicKey: Hex
|
|
/** Account source */
|
|
source: string
|
|
/** Account type */
|
|
type: 'local'
|
|
|
|
/** Assigns a key authorization to the next transaction */
|
|
assignKeyAuthorization: (keyAuthorization: KeyAuthorization) => Promise<void>
|
|
/** Signs a raw digest */
|
|
sign: (parameters: { hash: Hex }) => Promise<Hex>
|
|
/** Signs an EIP-7702 authorization */
|
|
signAuthorization: (parameters: SignAuthorizationParameters) => Promise<Authorization>
|
|
/** Signs a key authorization */
|
|
signKeyAuthorization: (
|
|
key: { accessKeyAddress: Address; keyType: string },
|
|
parameters?: { expiry?: bigint; limits?: Limits }
|
|
) => Promise<KeyAuthorization>
|
|
/** Signs a EIP-191 `personal_sign` message */
|
|
signMessage: (parameters: { message: string | { raw: Hex } }) => Promise<Hex>
|
|
/** Signs a transaction */
|
|
signTransaction: (transaction: TransactionRequest) => Promise<Hex>
|
|
/** Signs a EIP-712 typed data */
|
|
signTypedData: (typedData: TypedData) => Promise<Hex>
|
|
}
|
|
```
|
|
|
|
## Parameters
|
|
|
|
### privateKey
|
|
|
|
- **Type:** `Hex`
|
|
|
|
The P256 private key as a hex string.
|
|
|
|
### options (optional)
|
|
|
|
#### options.access
|
|
|
|
- **Type:** `Address | Account`
|
|
|
|
Parent account to access.
|