-
Notifications
You must be signed in to change notification settings - Fork 228
/
Copy pathindex.ts
122 lines (106 loc) · 3.24 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import { hashMessage as performMessageHash } from 'viem'
import {
EIP712MessageTypes,
EIP712TxTypes,
EIP712TypedData,
SafeEIP712Args,
SafeTransactionData,
EIP712TypedDataMessage,
EIP712TypedDataTx
} from '@safe-global/types-kit'
import semverSatisfies from 'semver/functions/satisfies'
import { hashTypedData as hashTypedStructuredData } from './encode'
const EQ_OR_GT_1_3_0 = '>=1.3.0'
export const EIP712_DOMAIN_BEFORE_V130 = [
{
type: 'address',
name: 'verifyingContract'
}
]
export const EIP712_DOMAIN = [
{
type: 'uint256',
name: 'chainId'
},
{
type: 'address',
name: 'verifyingContract'
}
]
// This function returns the types structure for signing off-chain messages according to EIP-712
export function getEip712TxTypes(safeVersion: string): EIP712TxTypes {
const eip712WithChainId = semverSatisfies(safeVersion, EQ_OR_GT_1_3_0)
return {
EIP712Domain: eip712WithChainId ? EIP712_DOMAIN : EIP712_DOMAIN_BEFORE_V130,
SafeTx: [
{ type: 'address', name: 'to' },
{ type: 'uint256', name: 'value' },
{ type: 'bytes', name: 'data' },
{ type: 'uint8', name: 'operation' },
{ type: 'uint256', name: 'safeTxGas' },
{ type: 'uint256', name: 'baseGas' },
{ type: 'uint256', name: 'gasPrice' },
{ type: 'address', name: 'gasToken' },
{ type: 'address', name: 'refundReceiver' },
{ type: 'uint256', name: 'nonce' }
]
}
}
export function getEip712MessageTypes(safeVersion: string): EIP712MessageTypes {
const eip712WithChainId = semverSatisfies(safeVersion, EQ_OR_GT_1_3_0)
return {
EIP712Domain: eip712WithChainId ? EIP712_DOMAIN : EIP712_DOMAIN_BEFORE_V130,
SafeMessage: [{ type: 'bytes', name: 'message' }]
}
}
export const hashTypedData = (typedData: EIP712TypedData): string => {
return hashTypedStructuredData(typedData)
}
const hashMessage = (message: string): string => {
return performMessageHash(message)
}
export const hashSafeMessage = (message: string | EIP712TypedData): string => {
return typeof message === 'string' ? hashMessage(message) : hashTypedData(message)
}
export function generateTypedData({
safeAddress,
safeVersion,
chainId,
data
}: SafeEIP712Args): EIP712TypedDataTx | EIP712TypedDataMessage {
const isSafeTransactionDataType = data.hasOwnProperty('to')
const eip712WithChainId = semverSatisfies(safeVersion, EQ_OR_GT_1_3_0)
let typedData: EIP712TypedDataTx | EIP712TypedDataMessage
if (isSafeTransactionDataType) {
const txData = data as SafeTransactionData
typedData = {
types: getEip712TxTypes(safeVersion),
domain: {
verifyingContract: safeAddress
},
primaryType: 'SafeTx',
message: {
...txData,
value: txData.value,
safeTxGas: txData.safeTxGas,
baseGas: txData.baseGas,
gasPrice: txData.gasPrice,
nonce: txData.nonce
}
}
} else {
const message = data as string | EIP712TypedData
typedData = {
types: getEip712MessageTypes(safeVersion),
domain: {
verifyingContract: safeAddress
},
primaryType: 'SafeMessage',
message: { message: hashSafeMessage(message) }
}
}
if (eip712WithChainId) {
typedData.domain.chainId = Number(chainId)
}
return typedData
}