-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsignMessage.ts
61 lines (55 loc) · 1.57 KB
/
signMessage.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
import * as utils from "ethereumjs-util";
import { ethers } from "hardhat";
import { ERC20 } from "./typechain-types";
import { BNLike } from "ethereumjs-util";
import { BigNumber } from "ethers";
const prefixedHash = (
contractAddr: string,
destination: string,
value: string,
data: string,
nonce: BigNumber
): string => {
const hash = ethers.utils.solidityKeccak256(
["address", "address", "uint256", "bytes", "uint256"],
[contractAddr, destination, value, data, nonce]
);
return ethers.utils.solidityKeccak256(
["string", "bytes"],
["\x19Ethereum Signed Message:\n32", hash]
);
};
// Recover address
const recover = (hashBuffer: Buffer, r: Buffer, s: Buffer, v: BNLike) => {
const pub = utils.ecrecover(hashBuffer, v, r, s);
return "0x" + utils.pubToAddress(pub).toString("hex");
};
const hashIt = (
contractAddr: string,
destination: string,
value: string,
data: string,
nonce: BigNumber
) => {
// 66 byte string, which represents 32 bytes of data
let hash = prefixedHash(contractAddr, destination, value, data, nonce);
hash = hash.slice(2, hash.length);
return Buffer.from(hash, "hex");
};
const sign = async (hash: Buffer, prKey: Buffer) => {
const { r, s, v } = utils.ecsign(hash, prKey);
return { r, s, v };
};
const getEncodedTransferFrom = (
token: ERC20,
sender: string,
recipient: string,
amount: BigNumber
) => {
return token.interface.encodeFunctionData("transferFrom", [
sender,
recipient,
amount.toString(),
]); // todo: make sure is correct
};
export { hashIt, sign, recover, getEncodedTransferFrom };