-
Notifications
You must be signed in to change notification settings - Fork 0
/
signature.ts
85 lines (81 loc) · 2.1 KB
/
signature.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
import { BigNumber } from '@ethersproject/bignumber';
import { signTypedMessage, TypedMessage } from 'eth-sig-util';
import { ECDSASignature, fromRpcSig } from 'ethereumjs-util';
export const buildDelegationData = (
chainId: number,
verifyingContract: string,
delegatee: string,
nonce: string,
expiry: string
) => {
const typedData: TypedMessage<any> = {
primaryType: 'Delegation',
types: {
EIP712Domain: [
{ name: 'name', type: 'string' },
{ name: 'version', type: 'string' },
{ name: 'chainId', type: 'uint256' },
{ name: 'verifyingContract', type: 'address' },
],
Delegation: [
{ name: 'delegatee', type: 'address' },
{ name: 'nonce', type: 'uint256' },
{ name: 'expiry', type: 'uint256' },
],
},
domain: {
name: 'StakedElyfiToken',
version: '1',
chainId: chainId,
verifyingContract: verifyingContract,
},
message: {
delegatee: delegatee,
nonce: nonce,
expiry: expiry,
},
};
return typedData;
};
export const buildBallotData = (
chainId: number,
verifyingContract: string,
proposalId: BigNumber,
support: number
) => {
const typedData: TypedMessage<any> = {
primaryType: 'Ballot',
types: {
EIP712Domain: [
{ name: 'name', type: 'string' },
{ name: 'version', type: 'string' },
{ name: 'chainId', type: 'uint256' },
{ name: 'verifyingContract', type: 'address' },
],
Ballot: [
{ name: 'proposalId', type: 'uint256' },
{ name: 'support', type: 'uint8' },
],
},
domain: {
name: 'ElyfiGovernanceCore',
version: '1',
chainId: chainId,
verifyingContract: verifyingContract,
},
message: {
proposalId: proposalId.toString(),
support: support,
},
};
return typedData;
};
export const getSignatureFromTypedData = (
privateKey: string,
typedData: TypedMessage<any>
): ECDSASignature => {
const signature = signTypedMessage(Buffer.from(privateKey.substring(2, 66), 'hex'), {
data: typedData,
});
return fromRpcSig(signature);
};