aboutsummaryrefslogtreecommitdiffstats
path: root/packages
diff options
context:
space:
mode:
authorGreg Hysen <greg.hysen@gmail.com>2018-11-08 04:54:29 +0800
committerGreg Hysen <greg.hysen@gmail.com>2018-11-29 08:38:10 +0800
commit91a08b9fdd6ec406c6a0e853ff314501347fac94 (patch)
treed9f05bed487bac8c68ac6e7e9fd115379bf145bc /packages
parentcef254fa8cacdcba37ec41dec574566b9249b84c (diff)
downloaddexon-0x-contracts-91a08b9fdd6ec406c6a0e853ff314501347fac94.tar
dexon-0x-contracts-91a08b9fdd6ec406c6a0e853ff314501347fac94.tar.gz
dexon-0x-contracts-91a08b9fdd6ec406c6a0e853ff314501347fac94.tar.bz2
dexon-0x-contracts-91a08b9fdd6ec406c6a0e853ff314501347fac94.tar.lz
dexon-0x-contracts-91a08b9fdd6ec406c6a0e853ff314501347fac94.tar.xz
dexon-0x-contracts-91a08b9fdd6ec406c6a0e853ff314501347fac94.tar.zst
dexon-0x-contracts-91a08b9fdd6ec406c6a0e853ff314501347fac94.zip
Static bytes tests
Diffstat (limited to 'packages')
-rw-r--r--packages/order-utils/test/abi_encoder_test.ts92
1 files changed, 80 insertions, 12 deletions
diff --git a/packages/order-utils/test/abi_encoder_test.ts b/packages/order-utils/test/abi_encoder_test.ts
index fb149faa1..6f723036b 100644
--- a/packages/order-utils/test/abi_encoder_test.ts
+++ b/packages/order-utils/test/abi_encoder_test.ts
@@ -484,29 +484,43 @@ namespace AbiEncoder {
'^(byte|bytes(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))$',
);
- static DEFAULT_WIDTH = new BigNumber(1);
- width: BigNumber = Byte.DEFAULT_WIDTH;
+ static DEFAULT_WIDTH = 1;
+ width: number = Byte.DEFAULT_WIDTH;
constructor(dataItem: DataItem) {
super(dataItem);
const matches = Byte.matcher.exec(dataItem.type);
expect(matches).to.be.not.null();
- if (matches !== null && matches.length === 2) {
- this.width = new BigNumber(matches[1], 10);
+ if (matches !== null && matches.length === 3 && matches[2] !== undefined) {
+ this.width = parseInt(matches[2]);
+ } else {
+ this.width = Byte.DEFAULT_WIDTH;
}
}
- public assignValue(value: string) {
- //const hexValue = ethUtil.bufferToHex(new Buffer(value));
- //this.assignHexValue(hexValue);
- }
+ public assignValue(value: string | Buffer) {
+ // Convert value into a buffer and do bounds checking
+ const valueBuf = ethUtil.toBuffer(value);
+ if (valueBuf.byteLength > this.width) {
+ throw new Error(
+ `Tried to assign ${value} (${
+ valueBuf.byteLength
+ } bytes), which exceeds max bytes that can be stored in a ${this.getSignature()}`,
+ );
+ } else if (value.length % 2 !== 0) {
+ throw new Error(`Tried to assign ${value}, which is contains a half-byte. Use full bytes only.`);
+ }
- public getSignature(): string {
- throw 1;
+ // Store value as hex
+ const evmWordWidth = 32;
+ const paddedValue = ethUtil.setLengthRight(valueBuf, evmWordWidth);
+ const hexValue = ethUtil.bufferToHex(paddedValue);
+ this.assignHexValue(hexValue);
}
- public encodeToCalldata(calldata: Calldata): void {
- throw 2;
+ public getSignature(): string {
+ // Note that `byte` reduces to `bytes1`
+ return `bytes${this.width}`;
}
public static matchGrammar(type: string): boolean {
@@ -892,6 +906,60 @@ describe.only('ABI Encoder', () => {
// TODO: Add bounds tests + tests for different widths
});
+ describe.only('Static Bytes', () => {
+ it('Byte (padded)', async () => {
+ const testByteDataItem = { name: 'testStaticBytes', type: 'byte' };
+ const byteDataType = new AbiEncoder.Byte(testByteDataItem);
+ byteDataType.assignValue('0x05');
+ const expectedAbiEncodedByte = '0x0500000000000000000000000000000000000000000000000000000000000000';
+ expect(byteDataType.getHexValue()).to.be.equal(expectedAbiEncodedByte);
+ });
+
+ it.skip('Byte (no padding)', async () => {
+ const testByteDataItem = { name: 'testStaticBytes', type: 'byte' };
+ const byteDataType = new AbiEncoder.Byte(testByteDataItem);
+
+ // @TODO: This does not catch the Error
+ expect(byteDataType.assignValue('0x5')).to.throw();
+ });
+
+ it('Bytes1', async () => {
+ const testByteDataItem = { name: 'testStaticBytes', type: 'bytes1' };
+ const byteDataType = new AbiEncoder.Byte(testByteDataItem);
+ byteDataType.assignValue('0x05');
+ const expectedAbiEncodedByte = '0x0500000000000000000000000000000000000000000000000000000000000000';
+ expect(byteDataType.getHexValue()).to.be.equal(expectedAbiEncodedByte);
+ });
+
+ it('Bytes32 (padded)', async () => {
+ const testByteDataItem = { name: 'testStaticBytes', type: 'bytes32' };
+ const byteDataType = new AbiEncoder.Byte(testByteDataItem);
+ byteDataType.assignValue('0x0001020304050607080911121314151617181920212223242526272829303132');
+ const expectedAbiEncodedByte = '0x0001020304050607080911121314151617181920212223242526272829303132';
+ expect(byteDataType.getHexValue()).to.be.equal(expectedAbiEncodedByte);
+ });
+
+ it('Bytes32 (unpadded)', async () => {
+ const testByteDataItem = { name: 'testStaticBytes', type: 'bytes32' };
+ const byteDataType = new AbiEncoder.Byte(testByteDataItem);
+ byteDataType.assignValue('0x1a18bf61');
+ const expectedAbiEncodedByte = '0x1a18bf6100000000000000000000000000000000000000000000000000000000';
+ expect(byteDataType.getHexValue()).to.be.equal(expectedAbiEncodedByte);
+ });
+
+ it.skip('Bytes32 - Too long', async () => {
+ const testByteDataItem = { name: 'testStaticBytes', type: 'bytes32' };
+ const byteDataType = new AbiEncoder.Byte(testByteDataItem);
+
+ // @TODO: This does not catch the Error
+ expect(
+ byteDataType.assignValue('0x000102030405060708091112131415161718192021222324252627282930313233'),
+ ).to.throw(
+ `Tried to assign 0x000102030405060708091112131415161718192021222324252627282930313233 (33 bytes), which exceeds max bytes that can be stored in a bytes32`,
+ );
+ });
+ });
+
describe('String', () => {
const testStringDataItem = { name: 'testString', type: 'string' };
it('Less than 32 bytes', async () => {