ethereum-input-data-decode 0.4.2

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of ethereum-input-data-decode might be problematic. Click here for more details.

Files changed (46) hide show
  1. package/.editorconfig +23 -0
  2. package/.gitattributes +2 -0
  3. package/.github/FUNDING.yml +2 -0
  4. package/.travis.yml +10 -0
  5. package/CHANGELOG.md +5 -0
  6. package/LICENSE +21 -0
  7. package/README.md +283 -0
  8. package/bin/ethereum_input_data_decoder +3 -0
  9. package/cli.js +85 -0
  10. package/dist/index.d.ts +19 -0
  11. package/dist/index.js +396 -0
  12. package/example/bundle.js +33425 -0
  13. package/example/index.html +1213 -0
  14. package/example/main.js +35 -0
  15. package/example/style.css +28 -0
  16. package/index.d.ts +19 -0
  17. package/index.js +362 -0
  18. package/package.json +60 -0
  19. package/test/cli_test.js +1 -0
  20. package/test/data/0x_exchange.json +882 -0
  21. package/test/data/0x_exchange_data.txt +1 -0
  22. package/test/data/1inch_exchange_v2_abi.json +404 -0
  23. package/test/data/1inch_exchange_v2_abi_no_eth.txt +1 -0
  24. package/test/data/1inch_exchange_v2_abi_with_eth.txt +1 -0
  25. package/test/data/PayableProxyForSoloMargin_abi.json +134 -0
  26. package/test/data/PayableProxyForSoloMargin_tx_data.txt +1 -0
  27. package/test/data/abi1.json +1171 -0
  28. package/test/data/abi1_input_data.txt +1 -0
  29. package/test/data/abi2.json +1 -0
  30. package/test/data/abi3.json +1 -0
  31. package/test/data/abi3_data.txt +1 -0
  32. package/test/data/abi4.json +2 -0
  33. package/test/data/abi4_data.txt +1 -0
  34. package/test/data/abi5.json +1 -0
  35. package/test/data/abi5_data.txt +1 -0
  36. package/test/data/abi6.json +1 -0
  37. package/test/data/abi6_data.txt +1 -0
  38. package/test/data/abi7.json +1 -0
  39. package/test/data/abi7_data.txt +1 -0
  40. package/test/data/contract_creation_data.txt +2 -0
  41. package/test/data/erc721_abi.json +1 -0
  42. package/test/data/erc721_transferfrom_tx_data.txt +1 -0
  43. package/test/data/set_exchange_issuance_lib.json +195 -0
  44. package/test/data/set_issuance.txt +1 -0
  45. package/test/index.js +628 -0
  46. package/tsconfig.json +27 -0
package/.editorconfig ADDED
@@ -0,0 +1,23 @@
1
+ # EditorConfig http://editorconfig.org
2
+
3
+ # top-most EditorConfig file
4
+ root = true
5
+
6
+ [*]
7
+ # use line feed
8
+ end_of_line = lf
9
+
10
+ # ensure file ends with a newline when saving
11
+ insert_final_newline = true
12
+
13
+ # soft tabs
14
+ indent_style = space
15
+
16
+ # number of columns used for each indentation level
17
+ indent_size = 2
18
+
19
+ # remove any whitespace characters preceding newline characters
20
+ trim_trailing_whitespace = true
21
+
22
+ # character set
23
+ charset = utf-8
package/.gitattributes ADDED
@@ -0,0 +1,2 @@
1
+ # Convert line endings to LF (Line Feed)
2
+ * text=auto
@@ -0,0 +1,2 @@
1
+ github: [miguelmota]
2
+ patreon: miguelmota
package/.travis.yml ADDED
@@ -0,0 +1,10 @@
1
+ sudo: false
2
+ language: node_js
3
+ node_js:
4
+ - "10"
5
+
6
+ before_script:
7
+ - npm install
8
+
9
+ script:
10
+ - npm test
package/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ # Change Log
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ This project adheres to [Semantic Versioning](http://semver.org/).
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT license
2
+
3
+ Copyright (C) 2015 Miguel Mota
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9
+ of the Software, and to permit persons to whom the Software is furnished to do
10
+ so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,283 @@
1
+ <h3 align="center">
2
+ <br />
3
+ <img src="https://user-images.githubusercontent.com/168240/39537094-61c30484-4ded-11e8-8c93-410ba0510cf4.png" alt="logo" width="750" />
4
+ <br />
5
+ <br />
6
+ <br />
7
+ </h3>
8
+
9
+ # ethereum-input-data-decoder
10
+
11
+ > Ethereum smart contract transaction input data decoder
12
+
13
+ [![License](http://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/miguelmota/ethereum-input-data-decoder/master/LICENSE)
14
+ [![Build Status](https://travis-ci.org/miguelmota/ethereum-input-data-decoder.svg?branch=master)](https://travis-ci.org/miguelmota/ethereum-input-data-decoder)
15
+ [![dependencies Status](https://david-dm.org/miguelmota/ethereum-input-data-decoder/status.svg)](https://david-dm.org/miguelmota/ethereum-input-data-decoder)
16
+ [![NPM version](https://badge.fury.io/js/ethereum-input-data-decoder.svg)](http://badge.fury.io/js/ethereum-input-data-decoder)
17
+ [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](#contributing)
18
+
19
+ ## Demo
20
+
21
+ [https://lab.miguelmota.com/ethereum-input-data-decoder](https://lab.miguelmota.com/ethereum-input-data-decoder)
22
+
23
+ ## Install
24
+
25
+ ```bash
26
+ npm install ethereum-input-data-decoder
27
+ ```
28
+
29
+ ## Getting started
30
+
31
+ Pass [ABI](https://github.com/ethereum/wiki/wiki/Ethereum-Contract-ABI) file path to decoder constructor:
32
+
33
+ ```javascript
34
+ const InputDataDecoder = require('ethereum-input-data-decoder');
35
+ const decoder = new InputDataDecoder(`${__dirname}/abi.json`);
36
+ ```
37
+
38
+ Alternatively, you can pass ABI array object to constructor;
39
+
40
+ ```javascript
41
+ const abi = [{ ... }]
42
+ const decoder = new InputDataDecoder(abi);
43
+ ```
44
+
45
+ [example abi](./test/abi.json)
46
+
47
+ Then you can decode input data:
48
+
49
+ ```javascript
50
+ const data = `0x67043cae0000000000000000000000005a9dac9315fdd1c3d13ef8af7fdfeb522db08f020000000000000000000000000000000000000000000000000000000058a20230000000000000000000000000000000000000000000000000000000000040293400000000000000000000000000000000000000000000000000000000000000a0f3df64775a2dfb6bc9e09dced96d0816ff5055bf95da13ce5b6c3f53b97071c800000000000000000000000000000000000000000000000000000000000000034254430000000000000000000000000000000000000000000000000000000000`;
51
+
52
+ const result = decoder.decodeData(data);
53
+
54
+ console.log(result);
55
+ ```
56
+
57
+ ```text
58
+ {
59
+ "method": "registerOffChainDonation",
60
+ "types": [
61
+ "address",
62
+ "uint256",
63
+ "uint256",
64
+ "string",
65
+ "bytes32"
66
+ ],
67
+ "inputs": [
68
+ <BN: 5a9dac9315fdd1c3d13ef8af7fdfeb522db08f02>,
69
+ <BN: 58a20230>,
70
+ <BN: 402934>,
71
+ "BTC",
72
+ <Buffer f3 df ... 71 c8>
73
+ ],
74
+ "names": [
75
+ "addr",
76
+ "timestamp",
77
+ "chfCents",
78
+ "currency",
79
+ "memo"
80
+ ]
81
+ }
82
+ ```
83
+
84
+ Example using input response from [web3.getTransaction](https://github.com/ethereum/wiki/wiki/JavaScript-API#web3ethgettransaction):
85
+
86
+ ```javascript
87
+ web3.eth.getTransaction(txHash, (error, txResult) => {
88
+ const result = decoder.decodeData(txResult.input);
89
+ console.log(result);
90
+ });
91
+ ```
92
+
93
+ ### Decoding tuple and tuple[] types
94
+
95
+ Where `OrderData` is
96
+
97
+ ```solidity
98
+ struct OrderData {
99
+ uint256 amount;
100
+ address buyer;
101
+ }
102
+ ```
103
+
104
+ Decoding input to a method `someMethod(address,OrderData,OrderData[])` returns data in format
105
+
106
+ ```js
107
+ {
108
+ method: 'someMethod',
109
+ types: ['address', '(uint256,address)', '(uint256,address)[]'],
110
+ inputs: [
111
+ '0x81c55017F7Ce6E72451cEd49FF7bAB1e3DF64d0C',
112
+ [100, '0xA37dE6790861B5541b0dAa7d0C0e651F44c6f4D9']
113
+ [[274, '0xea674fdde714fd979de3edf0f56aa9716b898ec8']]
114
+ ],
115
+ names: ['sender', ['order', ['amount', 'buyer']], ['allOrders', ['amount', 'buyer']]]
116
+ }
117
+ ```
118
+
119
+ - In the `types` field, tuples are represented as a string containing types contained in the tuple
120
+ - In the `inputs` field, tuples are represented as an array containing values contained in the tuple
121
+ - In the `names` field, tuples are represented as an array with 2 items. Item 1 is the name of the tuple, item 2 is an array containing the names of the values contained in the tuple.
122
+
123
+ ### Decoding Big Numbers
124
+
125
+ All numbers are returned in [big number](https://github.com/indutny/bn.js) format to preserve precision.
126
+
127
+ Here's an example of how to convert the big number to a human readable format.
128
+
129
+ ```js
130
+ console.log(result.inputs[0].toString(10)) // "5"
131
+ console.log(result.inputs[0].toNumber()) // 55
132
+ ```
133
+
134
+ Please keep in mind that JavaScript only supports numbers up to 64 bits. Solidity numbers can be up to 256 bits, so you run the risk of truncation when casting or having the big number library error out when trying to parse a large number to a JavaScript Number type.
135
+
136
+ ```js
137
+ const n = new BN("543534254523452352345234523455")
138
+ console.log(n.toString(10)) // "543534254523452352345234523455"
139
+ console.log(n.toNumber()) // ERROR!
140
+ ```
141
+
142
+ ## CLI
143
+
144
+ ## Install
145
+
146
+ ```bash
147
+ npm install -g ethereum-input-data-decoder
148
+ ```
149
+
150
+ ### Usage
151
+
152
+ ```bash
153
+ $ ethereum_input_data_decoder --help
154
+
155
+ Ethereum smart contract transaction input data decoder
156
+
157
+ Usage
158
+ $ ethereum_input_data_decoder [flags] [input]
159
+
160
+ Options
161
+ --abi, -a ABI file path
162
+ --input, -i Input data file path
163
+
164
+ Examples
165
+ $ ethereum_input_data_decoder --abi token.abi --input data.txt
166
+ $ ethereum_input_data_decoder --abi token.abi "0x23b872dd..."
167
+ ```
168
+
169
+ ### Example
170
+
171
+ Pass ABI file and input data as file:
172
+
173
+ ```bash
174
+ $ ethereum_input_data_decoder --abi abi.json --input data.tx
175
+
176
+ method registerOffChainDonation
177
+
178
+ address addr 0x5a9dac9315fdd1c3d13ef8af7fdfeb522db08f02
179
+ uint256 timestamp 1487012400
180
+ uint256 chfCents 4204852
181
+ string currency BTC
182
+ bytes32 memo 0xf3df64775a2dfb6bc9e09dced96d0816ff5055bf95da13ce5b6c3f53b97071c8
183
+ ```
184
+
185
+ Pass ABI file and input data as string:
186
+
187
+ ```bash
188
+ $ ethereum_input_data_decoder --abi abi.json 0x67043cae0...000000
189
+
190
+ method registerOffChainDonation
191
+
192
+ address addr 0x5a9dac9315fdd1c3d13ef8af7fdfeb522db08f02
193
+ uint256 timestamp 1487012400
194
+ uint256 chfCents 4204852
195
+ string currency BTC
196
+ bytes32 memo 0xf3df64775a2dfb6bc9e09dced96d0816ff5055bf95da13ce5b6c3f53b97071c8
197
+ ```
198
+
199
+ You can also pipe the input data:
200
+
201
+ ```bash
202
+ $ cat data.txt | ethereum_input_data_decoder --abi abi.json
203
+
204
+ method registerOffChainDonation
205
+
206
+ address addr 0x5a9dac9315fdd1c3d13ef8af7fdfeb522db08f02
207
+ uint256 timestamp 1487012400
208
+ uint256 chfCents 4204852
209
+ string currency BTC
210
+ bytes32 memo 0xf3df64775a2dfb6bc9e09dced96d0816ff5055bf95da13ce5b6c3f53b97071c8
211
+ ```
212
+
213
+ ## Test
214
+
215
+ ```bash
216
+ npm test
217
+ ```
218
+
219
+ ## Development
220
+
221
+ 1. Clone repository:
222
+
223
+ ```bash
224
+ git clone git@github.com:miguelmota/ethereum-input-data-decoder.git
225
+
226
+ cd ethereum-input-data-decoder/
227
+ ```
228
+
229
+ 2. Install dependencies:
230
+
231
+ ```bash
232
+ npm install
233
+ ```
234
+
235
+ 3. Make changes.
236
+
237
+ 4. Run tests:
238
+
239
+ ```bash
240
+ npm test
241
+ ```
242
+
243
+ 5. Run linter:
244
+
245
+ ```bash
246
+ npm run lint
247
+ ```
248
+
249
+ 5. Build:
250
+
251
+ ```bash
252
+ npm run build
253
+ ```
254
+
255
+ ## Contributing
256
+
257
+ Pull requests are welcome!
258
+
259
+ For contributions please create a new branch and submit a pull request for review.
260
+
261
+ Many thanks to all the [contributors](https://github.com/miguelmota/ethereum-input-data-decoder/graphs/contributors) for making this a better library for everyone 🙏
262
+
263
+ ## FAQ
264
+
265
+ - Q: How can I retrieve the ABI?
266
+
267
+ - A: You can generate the ABI from the solidity source files using the [Solidity Compiler](http://solidity.readthedocs.io/en/develop/installing-solidity.html).
268
+
269
+ ```bash
270
+ solc --abi MyContract.sol -o build
271
+ ```
272
+
273
+ - Q: Can this library decode contract creation input data?
274
+
275
+ - A: Yes, it can decode contract creation input data.
276
+
277
+ - Q: Does this library support ABIEncoderV2?
278
+
279
+ - A: Yes, but it's buggy. Please submit a [bug report](https://github.com/miguelmota/ethereum-input-data-decoder/issues/new) if you encounter issues.
280
+
281
+ ## License
282
+
283
+ [MIT](LICENSE)
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env node
2
+
3
+ require('../cli')
package/cli.js ADDED
@@ -0,0 +1,85 @@
1
+ const path = require('path')
2
+ const fs = require('fs')
3
+ const meow = require('meow')
4
+ const BN = require('bn.js')
5
+ const InputDataDecoder = require('./')
6
+
7
+ const cli = meow(` Usage
8
+ $ ethereum_input_data_decoder [flags] [input]
9
+
10
+ Options
11
+ --abi, -a ABI file path
12
+ --input, -i Input data file path
13
+
14
+ Examples
15
+ $ ethereum_input_data_decoder --abi token.abi --input data.txt
16
+ $ ethereum_input_data_decoder --abi token.abi "0x23b872dd..."
17
+ `, {
18
+ flags: {
19
+ abi: {
20
+ type: 'string',
21
+ alias: 'a'
22
+ },
23
+ input: {
24
+ type: 'string',
25
+ alias: 'i'
26
+ }
27
+ }
28
+ })
29
+
30
+ let { abi, input } = cli.flags
31
+ if (cli.input && cli.input.length > 0) {
32
+ input = cli.input[0]
33
+ run(abi, input)
34
+ } else if (input) {
35
+ try {
36
+ input = fs.readFileSync(path.resolve(input))
37
+ } catch(err) { }
38
+ run(abi, input)
39
+ } else {
40
+ input = ''
41
+ process.stdin.setEncoding('utf8')
42
+ process.stdin.resume()
43
+ process.stdin.on('readable', () => {
44
+ let chunk
45
+ while ((chunk = process.stdin.read())) {
46
+ input += chunk.toString()
47
+ }
48
+ })
49
+ const t = setTimeout(() => {
50
+ process.exit(0)
51
+ }, 1e3)
52
+ process.stdin.on('end', () => {
53
+ clearTimeout(t)
54
+ run(abi, input)
55
+ })
56
+ }
57
+
58
+ function run (abi, input) {
59
+ const decoder = new InputDataDecoder(path.resolve(abi))
60
+ const result = decoder.decodeData(input)
61
+
62
+ if (result.method === null && result.types.length === 0) {
63
+ console.log('No matches')
64
+ return
65
+ }
66
+
67
+ const padType = result.types.reduce((a, x) => Math.max(a, x.length), 0)+2
68
+ const padName = result.names.reduce((a, x) => Math.max(a, x.length), 0)+2
69
+
70
+ const output = [`${'method'.padEnd(padType)}${result.method}\n`]
71
+ for (let i = 0; i < result.types.length; i++) {
72
+ let value = result.inputs[i]
73
+ if (BN.isBN(value)) {
74
+ value = value.toString(10)
75
+ } else if (result.types[i] === 'address') {
76
+ value = `0x${value}`
77
+ } else if (result.types[i] === 'bytes32') {
78
+ value = `0x${value.toString('hex')}`
79
+ }
80
+
81
+ output.push(`${result.types[i].padEnd(padType)}${result.names[i].padEnd(padName)}${value}`)
82
+ }
83
+
84
+ console.log(output.join('\n'))
85
+ }
@@ -0,0 +1,19 @@
1
+ import Buffer from 'buffer';
2
+ import { Interface } from "ethers/utils";
3
+
4
+ type NestedArray<T> = T | NestedArray<T>[];
5
+
6
+ export interface InputData {
7
+ method: string | null;
8
+ types: string[];
9
+ inputs: any[];
10
+ names: NestedArray<string>[];
11
+ }
12
+
13
+ export default class InputDataDecoder {
14
+ constructor(abi: string | Interface['abi']);
15
+
16
+ decodeConstructor(data: Buffer | string): InputData;
17
+
18
+ decodeData(data: Buffer | string): InputData;
19
+ }