diff options
author | fragosti <francesco.agosti93@gmail.com> | 2018-12-01 03:58:05 +0800 |
---|---|---|
committer | fragosti <francesco.agosti93@gmail.com> | 2018-12-01 03:58:05 +0800 |
commit | ea11e8c62dac9ff6352bc983efc707d855b8e881 (patch) | |
tree | cdfc9ad01c2a4e56c560809cb7e01895987ef442 /packages/utils/src/abi_encoder/evm_data_types/address.ts | |
parent | 7479a7db58f5504416a88450c92b092899bf3df3 (diff) | |
parent | a1d4aa66bc6b3de041ec6e4eb4fe40383945510b (diff) | |
download | dexon-sol-tools-ea11e8c62dac9ff6352bc983efc707d855b8e881.tar dexon-sol-tools-ea11e8c62dac9ff6352bc983efc707d855b8e881.tar.gz dexon-sol-tools-ea11e8c62dac9ff6352bc983efc707d855b8e881.tar.bz2 dexon-sol-tools-ea11e8c62dac9ff6352bc983efc707d855b8e881.tar.lz dexon-sol-tools-ea11e8c62dac9ff6352bc983efc707d855b8e881.tar.xz dexon-sol-tools-ea11e8c62dac9ff6352bc983efc707d855b8e881.tar.zst dexon-sol-tools-ea11e8c62dac9ff6352bc983efc707d855b8e881.zip |
Merge branch 'development' of https://github.com/0xProject/0x-monorepo into fix/instant/instant-bounces-on-error
Diffstat (limited to 'packages/utils/src/abi_encoder/evm_data_types/address.ts')
-rw-r--r-- | packages/utils/src/abi_encoder/evm_data_types/address.ts | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/packages/utils/src/abi_encoder/evm_data_types/address.ts b/packages/utils/src/abi_encoder/evm_data_types/address.ts new file mode 100644 index 000000000..88846b1fa --- /dev/null +++ b/packages/utils/src/abi_encoder/evm_data_types/address.ts @@ -0,0 +1,49 @@ +import { DataItem, SolidityTypes } from 'ethereum-types'; +import * as ethUtil from 'ethereumjs-util'; +import * as _ from 'lodash'; + +import { DataTypeFactory } from '../abstract_data_types/interfaces'; +import { AbstractBlobDataType } from '../abstract_data_types/types/blob'; +import { RawCalldata } from '../calldata/raw_calldata'; +import { constants } from '../utils/constants'; + +export class AddressDataType extends AbstractBlobDataType { + private static readonly _SIZE_KNOWN_AT_COMPILE_TIME: boolean = true; + private static readonly _ADDRESS_SIZE_IN_BYTES = 20; + private static readonly _DECODED_ADDRESS_OFFSET_IN_BYTES = constants.EVM_WORD_WIDTH_IN_BYTES - + AddressDataType._ADDRESS_SIZE_IN_BYTES; + + public static matchType(type: string): boolean { + return type === SolidityTypes.Address; + } + + public constructor(dataItem: DataItem, dataTypeFactory: DataTypeFactory) { + super(dataItem, dataTypeFactory, AddressDataType._SIZE_KNOWN_AT_COMPILE_TIME); + if (!AddressDataType.matchType(dataItem.type)) { + throw new Error(`Tried to instantiate Address with bad input: ${dataItem}`); + } + } + + // Disable prefer-function-over-method for inherited abstract methods. + /* tslint:disable prefer-function-over-method */ + public encodeValue(value: string): Buffer { + if (!ethUtil.isValidAddress(value)) { + throw new Error(`Invalid address: '${value}'`); + } + const valueBuf = ethUtil.toBuffer(value); + const encodedValueBuf = ethUtil.setLengthLeft(valueBuf, constants.EVM_WORD_WIDTH_IN_BYTES); + return encodedValueBuf; + } + + public decodeValue(calldata: RawCalldata): string { + const valueBufPadded = calldata.popWord(); + const valueBuf = valueBufPadded.slice(AddressDataType._DECODED_ADDRESS_OFFSET_IN_BYTES); + const value = ethUtil.bufferToHex(valueBuf); + return value; + } + + public getSignature(): string { + return SolidityTypes.Address; + } + /* tslint:enable prefer-function-over-method */ +} |