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
/* tslint:disable:no-empty */
import { expect } from '../../../setup'
/* External Imports */
import { ethers } from '@nomiclabs/buidler'
import { Contract } from 'ethers'
/* Internal Imports */
import { Lib_RLPWriter_TEST_JSON } from '../../../data'
const encode = async (Lib_RLPWriter: Contract, input: any): Promise<void> => {
if (Array.isArray(input)) {
const elements = await Promise.all(
input.map(async (el) => {
return encode(Lib_RLPWriter, el)
})
)
return Lib_RLPWriter.writeList(elements)
} else if (Number.isInteger(input)) {
return Lib_RLPWriter.writeUint(input)
} else if (input[0] === '#') {
return Lib_RLPWriter.writeInt(input.slice(1))
} else {
return Lib_RLPWriter.writeString(input)
}
}
describe('Lib_RLPWriter', () => {
let Lib_RLPWriter: Contract
before(async () => {
Lib_RLPWriter = await (
await ethers.getContractFactory('TestLib_RLPWriter')
).deploy()
})
describe('Official Ethereum RLP Tests', () => {
for (const [key, test] of Object.entries(Lib_RLPWriter_TEST_JSON)) {
it(`should properly encode: ${key}`, async () => {
expect(await encode(Lib_RLPWriter, test.in)).to.equal(test.out)
})
}
})
describe('Use of library with other memory-modifying operations', () => {
it('should allow creation of a contract beforehand and still work', async () => {
const randomAddress = '0x1234123412341234123412341234123412341234'
const rlpEncodedRandomAddress =
'0x941234123412341234123412341234123412341234'
const encoded = await Lib_RLPWriter.callStatic.writeAddressWithOtherMemory(
randomAddress
)
expect(encoded).to.eq(rlpEncodedRandomAddress)
})
})
})