diff options
author | Greg Hysen <greg.hysen@gmail.com> | 2018-11-21 04:58:49 +0800 |
---|---|---|
committer | Greg Hysen <greg.hysen@gmail.com> | 2018-11-29 08:38:11 +0800 |
commit | e6ab6f38bacdec90c960ff1db4781d161b1f4103 (patch) | |
tree | 7edec03e3f249edf49b9deafcc44416bf25f7dc0 /packages/utils/src/abi_encoder/evm_data_types/string.ts | |
parent | 5934e5a57b5897594e22efc9e6ff28841c6951bf (diff) | |
download | dexon-0x-contracts-e6ab6f38bacdec90c960ff1db4781d161b1f4103.tar dexon-0x-contracts-e6ab6f38bacdec90c960ff1db4781d161b1f4103.tar.gz dexon-0x-contracts-e6ab6f38bacdec90c960ff1db4781d161b1f4103.tar.bz2 dexon-0x-contracts-e6ab6f38bacdec90c960ff1db4781d161b1f4103.tar.lz dexon-0x-contracts-e6ab6f38bacdec90c960ff1db4781d161b1f4103.tar.xz dexon-0x-contracts-e6ab6f38bacdec90c960ff1db4781d161b1f4103.tar.zst dexon-0x-contracts-e6ab6f38bacdec90c960ff1db4781d161b1f4103.zip |
Split EVM data types and factory into separate files
Diffstat (limited to 'packages/utils/src/abi_encoder/evm_data_types/string.ts')
-rw-r--r-- | packages/utils/src/abi_encoder/evm_data_types/string.ts | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/packages/utils/src/abi_encoder/evm_data_types/string.ts b/packages/utils/src/abi_encoder/evm_data_types/string.ts new file mode 100644 index 000000000..96b36e735 --- /dev/null +++ b/packages/utils/src/abi_encoder/evm_data_types/string.ts @@ -0,0 +1,47 @@ +/* tslint:disable prefer-function-over-method */ +import { DataItem } from 'ethereum-types'; +import * as ethUtil from 'ethereumjs-util'; +import * as _ from 'lodash'; + +import { RawCalldata } from '../calldata'; +import * as Constants from '../constants'; +import { DataTypeFactory, PayloadDataType } from '../data_type'; + +export class String extends PayloadDataType { + private static readonly _SIZE_KNOWN_AT_COMPILE_TIME: boolean = false; + + public static matchType(type: string): boolean { + return type === 'string'; + } + + public constructor(dataItem: DataItem, dataTypeFactory: DataTypeFactory) { + super(dataItem, dataTypeFactory, String._SIZE_KNOWN_AT_COMPILE_TIME); + if (!String.matchType(dataItem.type)) { + throw new Error(`Tried to instantiate String with bad input: ${dataItem}`); + } + } + + public encodeValue(value: string): Buffer { + const wordsForValue = Math.ceil(value.length / Constants.EVM_WORD_WIDTH_IN_BYTES); + const paddedDynamicBytesForValue = wordsForValue * Constants.EVM_WORD_WIDTH_IN_BYTES; + const valueBuf = ethUtil.setLengthRight(new Buffer(value), paddedDynamicBytesForValue); + const lengthBuf = ethUtil.setLengthLeft(ethUtil.toBuffer(value.length), Constants.EVM_WORD_WIDTH_IN_BYTES); + const encodedValueBuf = Buffer.concat([lengthBuf, valueBuf]); + return encodedValueBuf; + } + + public decodeValue(calldata: RawCalldata): string { + const lengthBuf = calldata.popWord(); + const lengthHex = ethUtil.bufferToHex(lengthBuf); + const length = parseInt(lengthHex, Constants.HEX_BASE); + const wordsForValue = Math.ceil(length / Constants.EVM_WORD_WIDTH_IN_BYTES); + const paddedValueBuf = calldata.popWords(wordsForValue); + const valueBuf = paddedValueBuf.slice(0, length); + const value = valueBuf.toString('ascii'); + return value; + } + + public getSignature(): string { + return 'string'; + } +} |