Skip to content

Commit

Permalink
chore: jsdoc
Browse files Browse the repository at this point in the history
  • Loading branch information
tmm committed Aug 20, 2024
1 parent e7b66a5 commit 6f1fe74
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/internal/typedData/domainSeparator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,22 @@ import type { Hex } from '../types/data.js'
import { extractEip712DomainTypes } from './extractEip712DomainTypes.js'
import { hashDomain } from './hashDomain.js'

/**
* Creates [EIP-712 Typed Data](https://eips.ethereum.org/EIPS/eip-712) domainSeparator for the provided domain.
*
* - Docs: https://oxlib.sh/api/typedData/domainSeparator
*
* @example
* import { TypedData } from 'ox'
*
* TypedData.domainSeparator({
* name: 'Ether!',
* version: '1',
* chainId: 1,
* verifyingContract: '0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC',
* })
* // '0x9911ee4f58a7059a8f5385248040e6984d80e2c849500fe6a4d11c4fa98c2af3'
*/
export function domainSeparator(
domain: TypedDataDomain,
): domainSeparator.ReturnType {
Expand Down
22 changes: 22 additions & 0 deletions src/internal/typedData/encodeType.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,28 @@
import type { TypedData } from 'abitype'
import type { GlobalErrorType } from '../errors/error.js'

// TODO: Add error for `primaryType` not in `types`

/**
* Encodes [EIP-712 Typed Data](https://eips.ethereum.org/EIPS/eip-712) schema for the provided primaryType.
*
* - Docs: https://oxlib.sh/api/typedData/encodeType
*
* @example
* import { TypedData } from 'ox'
*
* TypedData.encodeType({
* types: {
* Foo: [
* { name: 'address', type: 'address' },
* { name: 'name', type: 'string' },
* { name: 'foo', type: 'string' },
* ],
* },
* primaryType: 'Foo',
* })
* // 'Foo(address address,string name,string foo)'
*/
export function encodeType(value: encodeType.Value): encodeType.ReturnType {
const { primaryType, types } = value

Expand Down
25 changes: 25 additions & 0 deletions src/internal/typedData/hashDomain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,31 @@ import type { TypedData, TypedDataDomain } from 'abitype'
import type { GlobalErrorType } from '../errors/error.js'
import { hashStruct } from './hashStruct.js'

/**
* Hashes [EIP-712 Typed Data](https://eips.ethereum.org/EIPS/eip-712) domain.
*
* - Docs: https://oxlib.sh/api/typedData/hashDomain
*
* @example
* import { TypedData } from 'ox'
*
* TypedData.hashDomain({
* domain: {
* name: 'Ether Mail',
* version: '1',
* chainId: 1,
* verifyingContract: '0x0000000000000000000000000000000000000000',
* },
* types: {
* Foo: [
* { name: 'address', type: 'address' },
* { name: 'name', type: 'string' },
* { name: 'foo', type: 'string' },
* ],
* },
* })
* // '0x6192106f129ce05c9075d319c1fa6ea9b3ae37cbd0c1ef92e2be7137bb07baa1'
*/
export function hashDomain(value: hashDomain.Value) {
const { domain, types } = value
return hashStruct({
Expand Down
28 changes: 28 additions & 0 deletions src/internal/typedData/hashStruct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,34 @@ import { toHex } from '../hex/toHex.js'
import type { Hex } from '../types/data.js'
import { encodeType } from './encodeType.js'

// TODO: Add error for `primaryType` not in `types`
// TODO: Add type inference?

/**
* Hashes [EIP-712 Typed Data](https://eips.ethereum.org/EIPS/eip-712) struct.
*
* - Docs: https://oxlib.sh/api/typedData/hashStruct
*
* @example
* import { TypedData } from 'ox'
*
* TypedData.hashStruct({
* types: {
* Foo: [
* { name: 'address', type: 'address' },
* { name: 'name', type: 'string' },
* { name: 'foo', type: 'string' },
* ],
* },
* primaryType: 'Foo',
* data: {
* address: '0xb9CAB4F0E46F7F6b1024b5A7463734fa68E633f9',
* name: 'jxom',
* foo: '0xb9CAB4F0E46F7F6b1024b5A7463734fa68E633f9',
* },
* })
* // '0x996fb3b6d48c50312d69abdd4c1b6fb02057c85aa86bb8d04c6f023326a168ce'
*/
export function hashStruct(value: hashStruct.Value): hashStruct.ReturnType {
const { data, primaryType, types } = value
const encoded = encodeData({
Expand Down
25 changes: 25 additions & 0 deletions src/internal/typedData/hashTypedData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,31 @@ import { hashDomain } from './hashDomain.js'
import { hashStruct } from './hashStruct.js'
import { validateTypedData } from './validateTypedData.js'

/**
* Hashes [EIP-712 Typed Data](https://eips.ethereum.org/EIPS/eip-712).
*
* - Docs: https://oxlib.sh/api/typedData/hash
*
* @example
* import { TypedData } from 'ox'
*
* TypedData.hash({
* types: {
* Foo: [
* { name: 'address', type: 'address' },
* { name: 'name', type: 'string' },
* { name: 'foo', type: 'string' },
* ],
* },
* primaryType: 'Foo',
* message: {
* address: '0xb9CAB4F0E46F7F6b1024b5A7463734fa68E633f9',
* name: 'jxom',
* foo: '0xb9CAB4F0E46F7F6b1024b5A7463734fa68E633f9',
* },
* })
* // '0x7ef8d6931ed54977c7593289c0feb25c7d7424fb997f4fc20aa3fe51b5141188'
*/
export function hashTypedData<
const typedData extends TypedData | Record<string, unknown>,
primaryType extends keyof typedData | 'EIP712Domain',
Expand Down

0 comments on commit 6f1fe74

Please sign in to comment.