RFC: Private Key & HD Accounts #211
-
SummaryWe have the concept of an Account in viem, that have the ability to sign messages, transactions, typed data, etc. An account can come in two formats:
Right now, viem does not have first-class support for "Local Accounts", in order to use a Local Account in viem, you need to either: provide an interface or use the Ethers.js Wallet adapter. Ideally, we should have our own implementation built-in so folks don't have to go to the extra work to build their own signer interface, or import an Ethers.js Wallet. ExamplesLocal AccountsYou will be able to define either a Private Key or HD Account via the The
import { createWalletClient, http } from 'viem'
import * as Account from 'viem/account'
const client = createWalletClient({
transport: http()
})
// Private Key
const privateKey = Account.generatePrivateKey()
const account = Account.fromPrivateKey(privateKey)
// HD (Mnemonic)
const mnemonic = Account.generateMnemonic()
const account = Account.fromMnemonic(mnemonic, { index, path })
// HD (Others)
const account = Account.fromMasterSeed(seed, { index, path })
const account = Account.fromExtendedKey(extendedKey, { index, path })
const account = Account.fromJson(xpriv, { index, path })
const hash = await client.sendTransaction({
account,
to: '0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC',
value: parseEther('0.001')
}) If your Account is static, it will also be possible to define an import { createWalletClient, http } from 'viem'
import * as Account from 'viem/account'
// Private Key
const privateKey = Account.generatePrivateKey()
const account = Account.fromPrivateKey(privateKey)
// HD (Mnemonic)
const mnemonic = Account.generateMnemonic()
const account = Account.fromMnemonic(mnemonic, { index, path })
// HD (Others)
const account = Account.fromMasterSeed(seed, { index, path })
const account = Account.fromExtendedKey(extendedKey, { index, path })
const account = Account.fromJson(xpriv, { index, path })
const client = createWalletClient({
+ account,
transport: http()
})
const hash = await client.sendTransaction({
// account becomes optional here, but you can still override it.
- account,
to: '0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC',
value: parseEther('0.001')
}) Detailed DesignRemoval of the
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Really solid! Curious about tweaking the examples and/or names for the utilities. If the examples/docs, use a namespace import (e.g. Instead, we could use a similar naming pattern to shortcut functions that already exist in viem: const account = privateKeyToAccount(privateKey)
const account = mnemonicToAccount(mnemonic, { index, path })
const account = masterSeedToAccount(seed, { index, path })
const account = extendedKeyToAccount(extendedKey, { index, path })
const account = jsonToAccount(xpriv, { index, path }) |
Beta Was this translation helpful? Give feedback.
-
Local accounts are now out as part of viem@0.2! |
Beta Was this translation helpful? Give feedback.
Local accounts are now out as part of viem@0.2!