2019-06-20 00:20:09 +02:00
|
|
|
import { assert, expect, spy, use } from 'chai'
|
2019-11-11 12:27:18 +01:00
|
|
|
import spies from 'chai-spies'
|
|
|
|
import Web3 from 'web3'
|
2019-06-20 00:20:09 +02:00
|
|
|
import config from '../../config'
|
2019-03-28 12:20:22 +01:00
|
|
|
|
2019-06-20 00:20:09 +02:00
|
|
|
import { Ocean } from '../../../src/ocean/Ocean'
|
2019-02-04 14:59:45 +01:00
|
|
|
|
2019-02-04 18:13:54 +01:00
|
|
|
use(spies)
|
2019-02-04 14:59:45 +01:00
|
|
|
|
2019-06-20 00:20:09 +02:00
|
|
|
describe('SignatureUtils', () => {
|
|
|
|
const publicKey = `0x${'a'.repeat(40)}`
|
|
|
|
const text = '0123456789abcde'
|
|
|
|
const signature = `0x${'a'.repeat(130)}`
|
2019-11-11 12:27:18 +01:00
|
|
|
let web3: Web3
|
2019-03-28 12:20:22 +01:00
|
|
|
let ocean: Ocean
|
|
|
|
|
|
|
|
before(async () => {
|
|
|
|
ocean = await Ocean.getInstance(config)
|
|
|
|
web3 = (ocean as any).web3
|
|
|
|
})
|
2019-02-04 14:59:45 +01:00
|
|
|
|
2019-02-04 16:26:56 +01:00
|
|
|
afterEach(() => {
|
|
|
|
spy.restore()
|
|
|
|
})
|
2019-02-04 14:59:45 +01:00
|
|
|
|
2019-06-20 00:20:09 +02:00
|
|
|
describe('#signText', () => {
|
2019-02-04 14:59:45 +01:00
|
|
|
let personalSignSpy
|
|
|
|
|
|
|
|
beforeEach(() => {
|
2019-06-20 00:20:09 +02:00
|
|
|
personalSignSpy = spy.on(web3.eth.personal, 'sign', () => signature)
|
2019-02-04 14:59:45 +01:00
|
|
|
})
|
|
|
|
|
2019-06-20 00:20:09 +02:00
|
|
|
it('should sign a text as expected', async () => {
|
2019-03-28 12:20:22 +01:00
|
|
|
const signed = await ocean.utils.signature.signText(text, publicKey)
|
2019-02-04 14:59:45 +01:00
|
|
|
|
|
|
|
assert.equal(signed, signature)
|
|
|
|
expect(personalSignSpy).to.have.been.called.with(text, publicKey)
|
|
|
|
})
|
|
|
|
|
2019-06-20 00:20:09 +02:00
|
|
|
it('should sign a text as expected using password', async () => {
|
2019-09-09 12:18:54 +02:00
|
|
|
const signed = await ocean.utils.signature.signText(text, publicKey, 'test')
|
2019-02-04 14:59:45 +01:00
|
|
|
|
|
|
|
assert.equal(signed, signature)
|
2019-09-09 12:18:54 +02:00
|
|
|
expect(personalSignSpy).to.have.been.called.with(text, publicKey, 'test')
|
2019-02-04 14:59:45 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-06-20 00:20:09 +02:00
|
|
|
describe('#verifyText', () => {
|
|
|
|
it('should recover the privateKey of a signed message', async () => {
|
2019-09-09 12:18:54 +02:00
|
|
|
const personalRecoverSpy = spy.on(web3.eth.personal, 'ecRecover', () => publicKey)
|
2019-06-20 00:20:09 +02:00
|
|
|
|
2019-09-09 12:18:54 +02:00
|
|
|
const verifiedPublicKey = await ocean.utils.signature.verifyText(text, signature)
|
2019-02-04 14:59:45 +01:00
|
|
|
|
|
|
|
assert.equal(publicKey, verifiedPublicKey)
|
|
|
|
expect(personalRecoverSpy).to.have.been.called.with(text, signature)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|