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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
|
import { Calldata, CalldataBlock, PayloadCalldataBlock, DependentCalldataBlock } from "./calldata";
import { MethodAbi, DataItem } from 'ethereum-types';
import { BigNumber } from '@0x/utils';
import ethUtil = require('ethereumjs-util');
var _ = require('lodash');
export abstract class DataType {
private dataItem: DataItem;
constructor(dataItem: DataItem) {
this.dataItem = dataItem;
}
public getDataItem(): DataItem {
return this.dataItem;
}
protected abstract createCalldataBlock(): CalldataBlock;
public abstract encode(value: any, calldata: Calldata): void;
public abstract getSignature(): string;
public abstract isStatic(): boolean;
}
export abstract class PayloadDataType extends DataType {
protected hasConstantSize: boolean;
public constructor(dataItem: DataItem, hasConstantSize: boolean) {
super(dataItem);
this.hasConstantSize = hasConstantSize;
}
protected generateCalldataBlock(payload: Buffer, calldata: Calldata): void {
const name = this.getDataItem().name;
const signature = this.getSignature();
const offsetInBytes = calldata.getSizeInBytes();
const relocatable = false;
const block = new PayloadCalldataBlock(name, signature, offsetInBytes, relocatable, payload);
calldata.pushBlock(block);
}
public isStatic(): boolean {
// If a payload has a constant size then it's static
return this.hasConstantSize;
}
}
export abstract class DependentDataType extends DataType {
protected dependency: DataType;
protected parent: DataType;
public constructor(dataItem: DataItem, dependency: DataType, parent: DataType) {
super(dataItem);
this.dependency = dependency;
this.parent = parent;
}
protected generateCalldataBlock(offsetInBytes: number, calldata: Calldata): CalldataBlock {
const name = this.getDataItem().name;
const signature = this.getSignature();
const relocatable = false;
const parentBlock = calldata.lookupBlockByName(this.parent.getDataItem().name);
const dependencyBlock = calldata.lookupBlockByName(this.parent.getDataItem().name);
const block = new DependentCalldataBlock(name, signature, offsetInBytes, relocatable, dependencyBlock, parentBlock);
calldata.pushBlock(block);
}
public encode(value: any, calldata: Calldata = new Calldata()): void {
const offsetInBytes = calldata.reserveSpace(DependentCalldataBlock.DEPENDENT_PAYLOAD_SIZE_IN_BYTES);
this.dependency.encode(value, calldata);
this.generateCalldataBlock(offsetInBytes, calldata);
}
public isStatic(): boolean {
return true;
}
}
export interface MemberMap {
[key: string]: number;
}
export abstract class MemberDataType extends DataType {
private memberMap: MemberMap;
private members: DataType[];
private isArray: boolean;
protected arrayLength: number | undefined;
public constructor(dataItem: DataItem, isArray: boolean = false, arrayLength?: number) {
super(dataItem);
this.memberMap = {};
this.members = [];
this.isArray = isArray;
this.arrayLength = arrayLength;
if (isArray && arrayLength !== undefined) {
[this.members, this.memberMap] = MemberDataType.createMembersWithLength(arrayLength);
} else if (!isArray) {
[this.members, this.memberMap] = MemberDataType.createMembersWithKeys(dataItem);
}
}
private static createMembersWithKeys(dataItem: DataItem): [DataType[], MemberMap] {
// Sanity check
if (dataItem.components === undefined) {
throw new Error(`Expected components`);
}
let members: DataType[] = [];
let memberMap: MemberMap = {};
_.each(dataItem.components, (dataItem: DataItem) => {
const childDataItem = {
type: dataItem.type,
name: `${dataItem.name}.${dataItem.name}`,
} as DataItem;
const child = DataTypeFactory.create(childDataItem, this);
members.push(child);
memberMap[dataItem.name] = members.length;
});
return [members, memberMap];
}
private static createMembersWithLength(dataItem: DataItem, length: number): [DataType[], MemberMap] {
let members: DataType[] = [];
let memberMap: MemberMap = {};
const range = _.range(length);
_.each(range, (idx: number) => {
const childDataItem = {
type: this.type,
name: `${dataItem.name}[${idx.toString(10)}]`,
} as DataItem;
const components = dataItem.components;
if (components !== undefined) {
childDataItem.components = components;
}
const child = DataTypeFactory.create(childDataItem, this);
members.push(child);
memberMap[idx.toString(10)] = members.length;
});
return [members, memberMap];
}
protected encodeFromArray(value: any[], calldata: Calldata) {
// Sanity check length
const valueLength = new BigNumber(value.length);
if (this.length !== SolArray.UNDEFINED_LENGTH && valueLength.equals(this.length) === false) {
throw new Error(
`Expected array of ${JSON.stringify(
this.length,
)} elements, but got array of length ${JSON.stringify(valueLength)}`,
);
}
// Assign values to children
for (let idx = new BigNumber(0); idx.lessThan(this.length); idx = idx.plus(1)) {
const idxNumber = idx.toNumber();
this.members[idxNumber].assignValue(value[idxNumber]);
}
}
protected encodeFromObject(obj: object, calldata: Calldata) {
let childMap = _.cloneDeep(this.memberMap);
_.forOwn(obj, (value: any, key: string) => {
if (key in childMap === false) {
throw new Error(`Could not assign tuple to object: unrecognized key '${key}'`);
}
this.members[this.childMap[key]].assignValue(value);
delete childMap[key];
});
if (Object.keys(childMap).length !== 0) {
throw new Error(`Could not assign tuple to object: missing keys ${Object.keys(childMap)}`);
}
}
public encode(value: any[] | object, calldata = new Calldata()) {
if (value instanceof Array) {
this.encodeFromArray(value, calldata);
} else if (typeof value === 'object') {
this.encodeFromObject(value, encodeFromObject);
} else {
throw new Error(`Unexpected type for ${value}`);
}
}
protected generateCalldataBlock(offsetInBytes: number, calldata: Calldata): CalldataBlock {
const name = this.getDataItem().name;
const signature = this.getSignature();
const relocatable = false;
const parentBlock = calldata.lookupBlockByName(this.parent.getDataItem().name);
const dependencyBlock = calldata.lookupBlockByName(this.parent.getDataItem().name);
const block = new DependentCalldataBlock(name, signature, offsetInBytes, relocatable, dependencyBlock, parentBlock);
calldata.pushBlock(block);
}
protected computeSignatureOfMembers(): string {
// Compute signature of members
let signature = `(`;
_.each(this.members, (member: DataType, i: number) => {
signature += member.getSignature();
if (i < this.members.length - 1) {
signature += ',';
}
});
signature += ')';
return signature;
}
public isStatic(): boolean {
/* For Tuple:
const isStaticTuple = this.children.length === 0;
return isStaticTuple; // @TODO: True in every case or only when dynamic data?
For Array:
if isLengthDefined = false then this is false
Otherwise if the first element is a Pointer then false
*/
if (this.isArray && this.arrayLength === undefined) {
return true;
}
// Search for dependent members
const dependentMember = _.find(this.members, (member: DataType) => {
return (member instanceof DependentDataType);
});
const isStatic = (dependentMember === undefined); // static if we couldn't find a dependent member
return isStatic;
}
}
export interface DataTypeFactoryImpl {
create: (dataItem: DataItem, parentDataType: DataType) => DataType;
mapDataItemToDataType: (dataItem: DataItem) => DataType;
}
export class DataTypeFactory {
private static instance: DataTypeFactory;
private provider: DataTypeFactoryImpl | undefined;
private constructor() { }
private static getInstance(): DataTypeFactory {
if (!DataTypeFactory.instance) {
DataTypeFactory.instance = new DataTypeFactory();
}
return DataTypeFactory.instance;
}
public static setImpl(provider: DataTypeFactoryImpl) {
const instance = DataTypeFactory.getInstance();
if (instance.provider !== undefined) {
throw new Error(`Tried to set implementation more than once`);
}
DataTypeFactory.getInstance().provider = provider;
}
public static create(dataItem: DataItem, parentDataType: DataType): DataType {
const instance = DataTypeFactory.getInstance();
if (instance.provider === undefined) {
throw new Error(`Tried to create before implementation is set`);
}
return instance.provider.create(dataItem, parentDataType);
}
public static mapDataItemToDataType(dataItem: DataItem): DataType {
const instance = DataTypeFactory.getInstance();
if (instance.provider === undefined) {
throw new Error(`Tried to create before implementation is set`);
}
return instance.provider.mapDataItemToDataType(dataItem);
}
}
|