diff options
author | Greg Hysen <greg.hysen@gmail.com> | 2018-11-20 03:18:34 +0800 |
---|---|---|
committer | Greg Hysen <greg.hysen@gmail.com> | 2018-11-29 08:38:10 +0800 |
commit | a2ad15be0dc500fa196021a5b32e80af8500043c (patch) | |
tree | 3f7dcd493c3a92e39b8583f67c6b79a02f579ce2 /packages/utils/src/abi_encoder | |
parent | a630312074758b31951c194533dcae596424592d (diff) | |
download | dexon-0x-contracts-a2ad15be0dc500fa196021a5b32e80af8500043c.tar dexon-0x-contracts-a2ad15be0dc500fa196021a5b32e80af8500043c.tar.gz dexon-0x-contracts-a2ad15be0dc500fa196021a5b32e80af8500043c.tar.bz2 dexon-0x-contracts-a2ad15be0dc500fa196021a5b32e80af8500043c.tar.lz dexon-0x-contracts-a2ad15be0dc500fa196021a5b32e80af8500043c.tar.xz dexon-0x-contracts-a2ad15be0dc500fa196021a5b32e80af8500043c.tar.zst dexon-0x-contracts-a2ad15be0dc500fa196021a5b32e80af8500043c.zip |
Tests for Address
Diffstat (limited to 'packages/utils/src/abi_encoder')
-rw-r--r-- | packages/utils/src/abi_encoder/calldata.ts | 15 | ||||
-rw-r--r-- | packages/utils/src/abi_encoder/data_type.ts | 2 | ||||
-rw-r--r-- | packages/utils/src/abi_encoder/evm_data_types.ts | 13 |
3 files changed, 24 insertions, 6 deletions
diff --git a/packages/utils/src/abi_encoder/calldata.ts b/packages/utils/src/abi_encoder/calldata.ts index 078767c22..11288064e 100644 --- a/packages/utils/src/abi_encoder/calldata.ts +++ b/packages/utils/src/abi_encoder/calldata.ts @@ -248,7 +248,7 @@ export class Calldata { private selector: string; private rules: EncodingRules; private sizeInBytes: number; - private root: MemberCalldataBlock | undefined; + private root: CalldataBlock | undefined; constructor(rules: EncodingRules) { this.selector = ''; @@ -257,8 +257,17 @@ export class Calldata { this.root = undefined; } - private createQueue(memberBlock: MemberCalldataBlock): Queue<CalldataBlock> { + private createQueue(block: CalldataBlock): Queue<CalldataBlock> { const blockQueue = new Queue<CalldataBlock>(); + + // Base Case + if (block instanceof MemberCalldataBlock === false) { + blockQueue.push(block); + return blockQueue; + } + + // This is a Member Block + const memberBlock = block as MemberCalldataBlock; _.eachRight(memberBlock.getMembers(), (member: CalldataBlock) => { if (member instanceof MemberCalldataBlock) { blockQueue.mergeFront(this.createQueue(member)); @@ -429,7 +438,7 @@ export class Calldata { return ""; } - public setRoot(block: MemberCalldataBlock) { + public setRoot(block: CalldataBlock) { this.root = block; this.sizeInBytes += block.getSizeInBytes(); } diff --git a/packages/utils/src/abi_encoder/data_type.ts b/packages/utils/src/abi_encoder/data_type.ts index 022f5621f..0f3cecac5 100644 --- a/packages/utils/src/abi_encoder/data_type.ts +++ b/packages/utils/src/abi_encoder/data_type.ts @@ -35,7 +35,7 @@ export abstract class DataType { const calldata = new Calldata(rules_); if (selector) calldata.setSelector(selector); const block = this.generateCalldataBlock(value); - calldata.setRoot(block as MemberCalldataBlock); // @TODO CHANGE + calldata.setRoot(block); // @TODO CHANGE const calldataHex = calldata.toHexString(); return calldataHex; } diff --git a/packages/utils/src/abi_encoder/evm_data_types.ts b/packages/utils/src/abi_encoder/evm_data_types.ts index bfb2808da..1ee95863b 100644 --- a/packages/utils/src/abi_encoder/evm_data_types.ts +++ b/packages/utils/src/abi_encoder/evm_data_types.ts @@ -20,6 +20,8 @@ export interface DataTypeStaticInterface { export class Address extends PayloadDataType { private static SIZE_KNOWN_AT_COMPILE_TIME: boolean = true; + public static ERROR_MESSAGE_ADDRESS_MUST_START_WITH_0X = "Address must start with '0x'"; + public static ERROR_MESSAGE_ADDRESS_MUST_BE_20_BYTES = "Address must be 20 bytes"; constructor(dataItem: DataItem) { super(dataItem, EvmDataTypeFactory.getInstance(), Address.SIZE_KNOWN_AT_COMPILE_TIME); @@ -36,9 +38,16 @@ export class Address extends PayloadDataType { return type === 'address'; } - public encodeValue(value: boolean): Buffer { + public encodeValue(value: string): Buffer { + if (value.startsWith('0x') === false) { + throw new Error(Address.ERROR_MESSAGE_ADDRESS_MUST_START_WITH_0X); + } + const valueAsBuffer = ethUtil.toBuffer(value); + if (valueAsBuffer.byteLength !== 20) { + throw new Error(Address.ERROR_MESSAGE_ADDRESS_MUST_BE_20_BYTES); + } const evmWordWidth = 32; - const encodedValueBuf = ethUtil.setLengthLeft(ethUtil.toBuffer(value), evmWordWidth); + const encodedValueBuf = ethUtil.setLengthLeft(valueAsBuffer, evmWordWidth); return encodedValueBuf; } |