En esta página
eth_signTypedData_v4
Firma datos estructurados tipados conforme a EIP-712. El dominio y el esquema hacen explícito el contexto de firma; el verificador debe comprobarlos de todos modos y aplicar protección contra ataques de repetición.
Parámetros
| Índice | Tipo | Descripción |
|---|---|---|
| 0 | string | Dirección con la que firmar |
| 1 | string | Cadena JSON de datos tipados |
Devuelve
string - La firma (cadena hexadecimal de 65 bytes)
Estructura de datos tipados
interface TypedData {
types: {
EIP712Domain: Array<{ name: string; type: string }>
[typeName: string]: Array<{ name: string; type: string }>
}
primaryType: string
domain: {
name?: string
version?: string
chainId?: number
verifyingContract?: string
salt?: string
}
message: Record<string, any>
}Ejemplo
const typedData = {
types: {
EIP712Domain: [
{ name: 'name', type: 'string' },
{ name: 'version', type: 'string' },
{ name: 'chainId', type: 'uint256' },
{ name: 'verifyingContract', type: 'address' }
],
Person: [
{ name: 'name', type: 'string' },
{ name: 'wallet', type: 'address' }
],
Mail: [
{ name: 'from', type: 'Person' },
{ name: 'to', type: 'Person' },
{ name: 'contents', type: 'string' }
]
},
primaryType: 'Mail',
domain: {
name: 'My dApp',
version: '1',
chainId: 1,
verifyingContract: '0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC'
},
message: {
from: {
name: 'Alice',
wallet: '0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826'
},
to: {
name: 'Bob',
wallet: '0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB'
},
contents: 'Hello, Bob!'
}
}
const signature = await window.$onekey.ethereum.request({
method: 'eth_signTypedData_v4',
params: [accounts[0], JSON.stringify(typedData)]
})
console.log('Signature:', signature)Firma de permiso (ERC-2612)
const permitData = {
types: {
EIP712Domain: [
{ name: 'name', type: 'string' },
{ name: 'version', type: 'string' },
{ name: 'chainId', type: 'uint256' },
{ name: 'verifyingContract', type: 'address' }
],
Permit: [
{ name: 'owner', type: 'address' },
{ name: 'spender', type: 'address' },
{ name: 'value', type: 'uint256' },
{ name: 'nonce', type: 'uint256' },
{ name: 'deadline', type: 'uint256' }
]
},
primaryType: 'Permit',
domain: {
name: 'USD Coin',
version: '2',
chainId: 1,
verifyingContract: '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48'
},
message: {
owner: accounts[0],
spender: '0xSpenderAddress...',
value: '1000000000', // 1000 USDC
nonce: tokenNonce, // Read the current permit nonce for this owner.
deadline: Math.floor(Date.now() / 1000) + 3600
}
}
const signature = await window.$onekey.ethereum.request({
method: 'eth_signTypedData_v4',
params: [accounts[0], JSON.stringify(permitData)]
})Errores
| Código | Mensaje | Descripción |
|---|---|---|
| 4001 | El usuario rechazó la solicitud | El usuario denegó la solicitud de firma |
| -32602 | Parámetros no válidos | Estructura de datos tipados no válida |
Notas
- Los datos tipados deben ser una cadena JSON válida
chainIddel dominio debe coincidir con la cadena conectada- El usuario verá una representación legible de los datos
- Definido por EIP-712
El ejemplo de permiso requiere el dominio EIP-712 real del contrato del token, el nonce actual, el destinatario de la autorización, el importe y el plazo. Sustituye los valores de ejemplo y consulta tokenNonce antes de solicitar la firma.
Última actualización el