-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathencode-decode-transaction.ts
152 lines (144 loc) · 4.91 KB
/
encode-decode-transaction.ts
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
import { Keccak } from 'sha3';
import { tinyBig, toChecksumAddress } from '../..';
import type {
ContractTypes,
JSONABIArgument,
} from '../../types/Contract.types';
import { hexToDecimal } from './hex-to-decimal';
export const hexFalse = '0'.repeat(64);
const hexTrue = '0'.repeat(63) + '1';
/**
* Expands an integer type to use a default of 256 bits. Used for consistency; not required in Solidity
*
* @see https://ethereum.stackexchange.com/questions/43241/why-write-uint256-instead-of-uint-if-theyre-the-same-thing
* @param type the type to explicitly define as 256 bits
* @returns the integer type expanded to explicitly be 256 bits when possible
* @example
* ```javascript
* expandType('uint[]');
* // 'uint256[]'
* ```
*/
function expandType(type: ContractTypes) {
// https://docs.soliditylang.org/en/v0.8.7/types.html#integers
if (type === 'uint[]') {
return 'uint256[]';
} else if (type === 'int[]') {
return 'int256[]';
}
return type;
}
/**
*
* @param jsonABIArgument
* @param args
* @example
*/
export function encodeData(jsonABIArgument: JSONABIArgument, args: any[]) {
const hash = new Keccak(256);
/* first 4 bytes will create the data parameter */
const functionString = `${jsonABIArgument.name}(${jsonABIArgument.inputs.map(
(input) => expandType(input.type),
)})`;
// encoding learnt from https://ethereum.stackexchange.com/questions/3514/how-to-call-a-contract-method-using-the-eth-call-json-rpc-api
const functionHash = hash.update(functionString).digest('hex');
// no arrays
const jsonABIInputsLength = jsonABIArgument.inputs.length;
let shouldValidateInputLength = true;
// inputs contains 1 or more arrays
if (jsonABIArgument.inputs.find((input) => input.type.includes('['))) {
shouldValidateInputLength = false;
}
if (shouldValidateInputLength && args.length !== jsonABIInputsLength) {
throw new Error(
`args inputs of "${args.length}" does not match expected length of "${jsonABIArgument.inputs.length}"`,
);
}
const argsWithTypes: [arg: any, type: ContractTypes][] = (
jsonABIArgument.inputs || []
).reduce((acc, input, i) => {
if (input.type.includes('[')) {
// strip array and length like "[2]" from type
const basicType = /([^[]*)\[.*$/g.exec(input.type)?.[1] as string;
args.forEach((arg: any) => {
acc = acc.concat([[arg, basicType]]);
});
return acc;
} else {
return acc.concat([[args[i], input.type]]);
}
}, [] as [arg: any, type: ContractTypes][]);
const encodedArgs = argsWithTypes.map(([arg, inputType]) => {
let rawArg = arg;
switch (inputType) {
case 'bool':
return arg ? hexTrue : hexFalse;
case 'address':
// remove leading "0x"
rawArg = arg.replace(/^0x/g, '').toLowerCase();
break;
default:
if (inputType.startsWith('bytes')) {
// encode each character to hex
const argEncoded = rawArg
.split('')
.map((character: string) => character.charCodeAt(0).toString(16))
.join('');
const paddedEncodedArg = argEncoded.padEnd(64, '0');
return paddedEncodedArg;
} else if (inputType === 'uint256') {
const argEncoded = BigInt(arg).toString(16);
const paddedEncodedArg = argEncoded.padStart(64, '0');
return paddedEncodedArg;
} else if (inputType.startsWith('uint')) {
break;
} else {
throw new Error(
`essential-eth does not yet support "${inputType}" inputs. Make a PR today!"`,
);
}
}
const argEncoded = rawArg.toString(16) as string;
const paddedEncodedArg = argEncoded.padStart(64, '0');
return paddedEncodedArg;
});
const functionEncoded = functionHash.slice(0, 8);
const data = `0x${functionEncoded}${encodedArgs.join('')}`;
return data;
}
/**
* @internal
* @param jsonABIArgument
* @param nodeResponse
* @example
*/
export function decodeRPCResponse(
jsonABIArgument: JSONABIArgument,
nodeResponse: string,
) {
const rawOutputs = jsonABIArgument.outputs;
// chunk response every 64 characters
const encodedOutputs = nodeResponse.slice(2).match(/.{1,64}/g);
const outputs = (encodedOutputs || []).map((output: string, i: number) => {
const outputType = (rawOutputs || [])[i].type;
switch (outputType) {
case 'bool':
return output === hexTrue;
case 'address':
/* address types have 26 leading zeroes to remove */
return toChecksumAddress(`0x${output.slice(24)}`);
case 'uint256':
case 'uint120':
return tinyBig(hexToDecimal(`0x${output}`));
case 'bytes32':
return `0x${output}`;
case 'uint8':
return Number(hexToDecimal(`0x${output}`));
default:
throw new Error(
`essential-eth does not yet support "${outputType}" outputs. Make a PR today!"`,
);
}
});
return outputs.length === 1 ? outputs[0] : outputs;
}