-
Notifications
You must be signed in to change notification settings - Fork 0
/
verifysignature.js
38 lines (30 loc) · 1.23 KB
/
verifysignature.js
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
const { expect } = require("chai")
const { ethers } = require("hardhat")
describe("VerifySignature", function () {
it("Check signature", async function () {
const accounts = await ethers.getSigners(2)
const VerifySignature = await ethers.getContractFactory("VerifySignature")
const contract = await VerifySignature.deploy()
await contract.deployed()
// const PRIV_KEY = "0x..."
// const signer = new ethers.Wallet(PRIV_KEY)
const signer = accounts[0]
const to = accounts[1].address
const amount = 999
const message = "Hello"
const nonce = 123
const hash = await contract.getMessageHash(to, amount, message, nonce)
const sig = await signer.signMessage(ethers.utils.arrayify(hash))
const ethHash = await contract.getEthSignedMessageHash(hash)
console.log("signer ", signer.address)
console.log("recovered signer", await contract.recoverSigner(ethHash, sig))
// Correct signature and message returns true
expect(
await contract.verify(signer.address, to, amount, message, nonce, sig)
).to.equal(true)
// Incorrect message returns false
expect(
await contract.verify(signer.address, to, amount + 1, message, nonce, sig)
).to.equal(false)
})
})