1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.MethodDataType = void 0;
|
4 | const ethUtil = require("ethereumjs-util");
|
5 | const _ = require("lodash");
|
6 | const set_1 = require("../abstract_data_types/types/set");
|
7 | const constants_1 = require("../utils/constants");
|
8 | const tuple_1 = require("./tuple");
|
9 | class MethodDataType extends set_1.AbstractSetDataType {
|
10 | constructor(abi, dataTypeFactory) {
|
11 | const methodDataItem = { type: 'method', name: abi.name, components: abi.inputs };
|
12 | super(methodDataItem, dataTypeFactory);
|
13 | this._methodSignature = this._computeSignature();
|
14 | this._methodSelector = this._computeSelector();
|
15 | const returnDataItem = { type: 'tuple', name: abi.name, components: abi.outputs };
|
16 | this._returnDataType = new tuple_1.TupleDataType(returnDataItem, this.getFactory());
|
17 | }
|
18 | encode(value, rules) {
|
19 | const calldata = super.encode(value, rules, this._methodSelector);
|
20 | return calldata;
|
21 | }
|
22 | decode(calldata, rules) {
|
23 | const value = super.decode(calldata, rules, this._methodSelector);
|
24 | return value;
|
25 | }
|
26 | strictDecode(calldata, rules) {
|
27 | const value = super.decode(calldata, Object.assign(Object.assign({}, rules), { isStrictMode: true }), this._methodSelector);
|
28 | const valueAsArray = _.isObject(value) ? _.values(value) : [value];
|
29 | switch (valueAsArray.length) {
|
30 | case 0:
|
31 | return undefined;
|
32 | case 1:
|
33 | return valueAsArray[0];
|
34 | default:
|
35 | return valueAsArray;
|
36 | }
|
37 | }
|
38 | encodeReturnValues(value, rules) {
|
39 | const returnData = this._returnDataType.encode(value, rules);
|
40 | return returnData;
|
41 | }
|
42 | decodeReturnValues(returndata, rules) {
|
43 | const returnValues = this._returnDataType.decode(returndata, rules);
|
44 | return returnValues;
|
45 | }
|
46 | strictDecodeReturnValue(returndata, rules) {
|
47 | const returnValues = this._returnDataType.decode(returndata, Object.assign(Object.assign({}, rules), { isStrictMode: true }));
|
48 | const returnValuesAsArray = _.isObject(returnValues) ? _.values(returnValues) : [returnValues];
|
49 | switch (returnValuesAsArray.length) {
|
50 | case 0:
|
51 | return undefined;
|
52 | case 1:
|
53 | return returnValuesAsArray[0];
|
54 | default:
|
55 | return returnValuesAsArray;
|
56 | }
|
57 | }
|
58 | getSignatureType() {
|
59 | return this._methodSignature;
|
60 | }
|
61 | getSelector() {
|
62 | return this._methodSelector;
|
63 | }
|
64 | getReturnValueDataItem() {
|
65 | const returnValueDataItem = this._returnDataType.getDataItem();
|
66 | return returnValueDataItem;
|
67 | }
|
68 | _computeSignature() {
|
69 | const memberSignature = this._computeSignatureOfMembers();
|
70 | const methodSignature = `${this.getDataItem().name}${memberSignature}`;
|
71 | return methodSignature;
|
72 | }
|
73 | _computeSelector() {
|
74 | const signature = this._computeSignature();
|
75 | const selector = ethUtil.bufferToHex(ethUtil.toBuffer(ethUtil
|
76 | .keccak256(Buffer.from(signature))
|
77 | .slice(constants_1.constants.HEX_SELECTOR_BYTE_OFFSET_IN_CALLDATA, constants_1.constants.HEX_SELECTOR_LENGTH_IN_BYTES)));
|
78 | return selector;
|
79 | }
|
80 | }
|
81 | exports.MethodDataType = MethodDataType;
|
82 |
|
\ | No newline at end of file |