Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

export signTransaction, update docs #984

Merged
merged 3 commits into from
Aug 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/ninety-goats-grab.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"viem": minor
---

Added `signTransaction` & `privateKeyToAddress` exports to `viem/accounts` entrypoint.
11 changes: 8 additions & 3 deletions site/docs/accounts/custom.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,14 @@ import { toAccount } from 'viem/accounts'

```ts
import { createWalletClient, http } from 'viem'
import { toAccount } from 'viem/accounts' // [!code focus]
import { // [!code focus:7]
signMessage,
signTransaction,
signTypedData,
privateKeyToAddress,
toAccount
} from 'viem/accounts'
import { mainnet } from 'viem/chains'
import { getAddress, signMessage, signTransaction } from './sign-utils' // [!code focus]

const privateKey = '0x...' // [!code focus:13]
const account = toAccount({
Expand Down Expand Up @@ -130,4 +135,4 @@ const account = toAccount({
return signTypedData({ ...typedData, privateKey })
},
})
```
```
2 changes: 1 addition & 1 deletion site/docs/accounts/signMessage.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ With the calculated signature, you can:
## Usage

```ts
import { privateKeyToAccount } from 'viem'
import { privateKeyToAccount } from 'viem/accounts'

const account = privateKeyToAccount('0x...')

Expand Down
6 changes: 4 additions & 2 deletions site/docs/accounts/signTransaction.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ Signs a transaction with the Account's private key.
## Usage

```ts
import { privateKeyToAccount, parseGwei } from 'viem'
import { parseGwei } from 'viem'
import { privateKeyToAccount } from 'viem/accounts'

const account = privateKeyToAccount('0x...')

Expand All @@ -38,7 +39,8 @@ const signature = await account.signTransaction({
viem has a built-in serializer for **Legacy**, **EIP-2930** (`0x01`) and **EIP-1559** (`0x02`) transaction types. If you would like to serialize on another transaction type that viem does not support internally, you can pass a custom serializer.

```ts
import { privateKeyToAccount, parseGwei } from 'viem'
import { parseGwei } from 'viem'
import { privateKeyToAccount } from 'viem/accounts'

const account = privateKeyToAccount('0x...')

Expand Down
2 changes: 1 addition & 1 deletion site/docs/accounts/signTypedData.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Signs typed data and calculates an Ethereum-specific signature in [https://eips.
::: code-group

```ts [example.ts]
import { privateKeyToAccount } from 'viem'
import { privateKeyToAccount } from 'viem/accounts'
import { domain, types } from './data'

const account = privateKeyToAccount('0x...')
Expand Down
6 changes: 6 additions & 0 deletions src/accounts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,19 @@ export {
type SignMessageReturnType,
signMessage,
} from './utils/signMessage.js'
export {
type SignTransactionParameters,
type SignTransactionReturnType,
signTransaction,
} from './utils/signTransaction.js'
export {
type SignTypedDataParameters,
type SignTypedDataReturnType,
signTypedData,
} from './utils/signTypedData.js'
export { parseAccount } from './utils/parseAccount.js'
export { publicKeyToAddress } from './utils/publicKeyToAddress.js'
export { privateKeyToAddress } from './utils/privateKeyToAddress.js'
export { czech } from './wordlists/czech.js'
export { english } from './wordlists/english.js'
export { french } from './wordlists/french.js'
Expand Down
10 changes: 10 additions & 0 deletions src/accounts/utils/privateKeyToAddress.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { expect, test } from 'vitest'

import { accounts } from '../../_test/constants.js'
import { privateKeyToAddress } from './privateKeyToAddress.js'

test('default', () => {
expect(privateKeyToAddress(accounts[0].privateKey).toLowerCase()).toEqual(
accounts[0].address,
)
})
20 changes: 20 additions & 0 deletions src/accounts/utils/privateKeyToAddress.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { secp256k1 } from '@noble/curves/secp256k1'
import type { Address } from 'abitype'

import type { Hex } from '../../types/misc.js'
import { bytesToHex } from '../../utils/encoding/toHex.js'
import { publicKeyToAddress } from './publicKeyToAddress.js'

/**
* @description Converts an ECDSA private key to an address.
*
* @param privateKey The private key to convert.
*
* @returns The address.
*/
export function privateKeyToAddress(privateKey: Hex): Address {
const publicKey = bytesToHex(
secp256k1.getPublicKey(privateKey.slice(2), false),
)
return publicKeyToAddress(publicKey)
}
4 changes: 2 additions & 2 deletions src/accounts/utils/signTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {

import { sign } from './sign.js'

export type SignTransactionArgs<
export type SignTransactionParameters<
TTransactionSerializable extends TransactionSerializable = TransactionSerializable,
> = {
privateKey: Hex
Expand All @@ -31,7 +31,7 @@ export async function signTransaction<
privateKey,
transaction,
serializer = serializeTransaction,
}: SignTransactionArgs<TTransactionSerializable>): Promise<
}: SignTransactionParameters<TTransactionSerializable>): Promise<
SignTransactionReturnType<TTransactionSerializable>
> {
const signature = await sign({
Expand Down
Loading