-
Notifications
You must be signed in to change notification settings - Fork 559
Expand file tree
/
Copy pathgnosis.ts
More file actions
136 lines (124 loc) · 3.29 KB
/
gnosis.ts
File metadata and controls
136 lines (124 loc) · 3.29 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import {
recoverTypedSignature,
SignTypedDataVersion,
recoverPersonalSignature,
} from '@metamask/eth-sig-util';
import { SafeTransactionDataPartial } from '@safe-global/types-kit';
import semverSatisfies from 'semver/functions/satisfies';
import { isSameAddress } from './index';
const EIP712_DOMAIN_BEFORE_V130 = [
{
type: 'address',
name: 'verifyingContract',
},
];
const EIP712_DOMAIN = [
{
type: 'uint256',
name: 'chainId',
},
{
type: 'address',
name: 'verifyingContract',
},
];
const getEip712MessageTypes = (version) => {
const eip712WithChainId = semverSatisfies(version, '>=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 const validateETHSign = (
signature: string,
txHash: string,
owner: string
) => {
let v = parseInt(signature.slice(-2), 16);
if (v > 30) {
v -= 4;
}
const str = signature.slice(0, -2) + v.toString(16);
return isSameAddress(
recoverPersonalSignature({
data: txHash,
signature: str,
}),
owner
);
};
export const validateEOASign = (
signature: string,
owner: string,
tx: SafeTransactionDataPartial,
version: string,
safeAddress: string,
networkId: number
) => {
const eip712WithChainId = semverSatisfies(version, '>=1.3.0');
const typedData = {
types: getEip712MessageTypes(version),
domain: {
chainId: eip712WithChainId ? networkId : undefined,
verifyingContract: safeAddress,
},
primaryType: 'SafeTx',
message: {
to: tx.to,
value: tx.value,
data: tx.data,
operation: tx.operation,
safeTxGas: tx.safeTxGas,
baseGas: tx.baseGas,
gasPrice: tx.gasPrice,
gasToken: tx.gasToken,
refundReceiver: tx.refundReceiver,
nonce: Number(tx.nonce),
},
};
const address = recoverTypedSignature({
data: typedData as any,
signature,
version: SignTypedDataVersion.V4,
});
return isSameAddress(address, owner);
};
export const crossCompareOwners = (owners1: string[], owners2: string[]) => {
return owners1.filter(
(owner) => !!owners2.find((own) => isSameAddress(own, owner))
);
};
type AdjustVOverload = {
(signingMethod: 'eth_signTypedData', signature: string): string;
(
signingMethod: 'eth_sign',
signature: string,
safeTxHash: string,
sender: string
): string;
};
export const adjustV: AdjustVOverload = (
signingMethod: 'eth_sign' | 'eth_signTypedData',
signature: string
): string => {
const MIN_VALID_V_VALUE = 27;
let sigV = parseInt(signature.slice(-2), 16);
if (signingMethod === 'eth_signTypedData') {
// Metamask with ledger returns V=0/1 here too, we need to adjust it to be ethereum's valid value (27 or 28)
if (sigV < MIN_VALID_V_VALUE) {
sigV += MIN_VALID_V_VALUE;
}
}
return signature.slice(0, -2) + sigV.toString(16);
};