2019-02-04 14:59:45 +01:00
|
|
|
import { assert, expect, use, spy } from "chai"
|
|
|
|
import * as spies from "chai-spies"
|
|
|
|
|
|
|
|
import { signText, verifyText } from "../../src/utils/SignatureHelpers"
|
|
|
|
import Web3Provider from '../../src/keeper/Web3Provider'
|
|
|
|
import ConfigProvider from "../../src/ConfigProvider"
|
|
|
|
import config from "../config"
|
|
|
|
|
|
|
|
use(spies);
|
|
|
|
|
|
|
|
describe("SignatureHelpers", () => {
|
|
|
|
|
2019-02-04 16:26:56 +01:00
|
|
|
const publicKey = `0x${'a'.repeat(40)}`
|
2019-02-04 14:59:45 +01:00
|
|
|
const text = "0123456789abcde"
|
|
|
|
const signature = `0x${'a'.repeat(130)}`
|
|
|
|
|
|
|
|
before(async () => {
|
|
|
|
ConfigProvider.setConfig(config)
|
|
|
|
})
|
2019-02-04 16:26:56 +01:00
|
|
|
afterEach(() => {
|
|
|
|
spy.restore()
|
|
|
|
})
|
2019-02-04 14:59:45 +01:00
|
|
|
|
|
|
|
describe("#signText", () => {
|
|
|
|
let personalSignSpy
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
const web3 = Web3Provider.getWeb3()
|
|
|
|
personalSignSpy = spy.on(web3.eth.personal, 'sign', () => signature)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should sign a text as expected", async () => {
|
|
|
|
const signed = await signText(text, publicKey)
|
|
|
|
|
|
|
|
assert.equal(signed, signature)
|
|
|
|
expect(personalSignSpy).to.have.been.called.with(text, publicKey)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should sign a text as expected using password", async () => {
|
|
|
|
const signed = await signText(text, publicKey, 'test')
|
|
|
|
|
|
|
|
assert.equal(signed, signature)
|
|
|
|
expect(personalSignSpy).to.have.been.called.with(text, publicKey, 'test')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe("#verifyText", () => {
|
|
|
|
it("should recover the privateKey of a signed message", async () => {
|
|
|
|
const web3 = Web3Provider.getWeb3()
|
|
|
|
const personalRecoverSpy = spy.on(web3.eth.personal, 'ecRecover', () => publicKey)
|
|
|
|
|
|
|
|
const verifiedPublicKey = await verifyText(text, signature)
|
|
|
|
|
|
|
|
assert.equal(publicKey, verifiedPublicKey)
|
|
|
|
expect(personalRecoverSpy).to.have.been.called.with(text, signature)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|