11import React , { Component } from "react" ;
22import PropTypes from "prop-types" ;
33
4+ import Button from "@material-ui/core/Button" ;
45import Card from "@material-ui/core/Card" ;
56import FormControl from "@material-ui/core/FormControl" ;
6- import Button from "@material-ui/core/Button" ;
77import TextField from "@material-ui/core/TextField" ;
88
9+
910import ethers from "ethers" ;
1011import ValidTypes from "../config/types" ;
1112import { suffixed } from "../config/types" ;
@@ -15,19 +16,11 @@ class Encoder extends Component{
1516 constructor ( props ) {
1617 super ( props ) ;
1718
18- //Hanle binds
19- this . handleChange = this . handleChange . bind ( this ) ;
20- this . handleClick = this . handleClick . bind ( this ) ;
21- this . handleRequestClose = this . handleRequestClose . bind ( this ) ;
19+ //Handle binds
2220 this . encodeData = this . encodeData . bind ( this ) ;
2321
24- this . testRegExp = this . testRegExp . bind ( this ) ;
25- this . validateType = this . validateType . bind ( this ) ;
26- this . typesSet = this . typesSet . bind ( this ) ;
2722 this . typeUpdated = this . typeUpdated . bind ( this ) ;
2823 this . valueUpdated = this . valueUpdated . bind ( this ) ;
29- this . formFilled = this . formFilled . bind ( this ) ;
30- this . errorExists = this . errorExists . bind ( this ) ;
3124
3225 this . state = {
3326 types : "" ,
@@ -40,14 +33,18 @@ class Encoder extends Component{
4033 this . abiCoder = new ethers . utils . AbiCoder ( ) ;
4134 }
4235
43-
44- testRegExp ( search , array ) {
45- let found = 0 ;
46- array . forEach ( function ( a ) {
47- if ( new RegExp ( a ) . test ( search ) && search . trim ( ) . match ( new RegExp ( a ) ) . index === 0 )
48- found ++ ;
36+ parseForEncode ( values ) {
37+ const matched = this . matchRegExpValues ( values ) ;
38+
39+ return matched . map ( ( val ) => {
40+ if ( this . testArrayRegExpValues ( val ) ) {
41+ val = this . stripArray ( val ) ;
42+ }
43+ if ( this . testRegExpValues ( val ) ) {
44+ val = this . parseForEncode ( val ) ;
45+ }
46+ return val ;
4947 } ) ;
50- return found ;
5148 }
5249
5350 validateType ( self ) {
@@ -61,13 +58,16 @@ class Encoder extends Component{
6158 } ) ;
6259
6360 vals . forEach ( function ( v , id ) {
64- if ( ! ( id === vals . length - 1 && v === "" ) )
61+ if ( id === vals . length - 1 && v === "" ) {
62+ return ;
63+ } else {
6564 if ( that . testRegExp ( v , array ) < 1 ) {
6665 clean = false ;
6766 let error = { } ; error [ "types" ] = true ;
6867 let state = { error :Object . assign ( that . state . error , error ) } ;
6968 return that . setState ( state ) ;
7069 }
70+ }
7171 } ) ;
7272 if ( clean ) {
7373 let error = { } ; error [ "types" ] = false ;
@@ -79,8 +79,10 @@ class Encoder extends Component{
7979 validateValue ( self ) {
8080 if ( ! self && this . state . values . length === 0 && ! this . state . submitted )
8181 return ;
82- let vals = this . state . values . split ( "," ) , error = { } ;
83- if ( vals . length !== this . state . types . split ( "," ) . length )
82+
83+ let error = { } ;
84+ const matchedValues = this . matchRegExpValues ( this . state . values ) || [ ] ;
85+ if ( this . state . types . split ( "," ) . length !== matchedValues . length )
8486 error [ "values" ] = true ;
8587 else
8688 error [ "values" ] = false ;
@@ -89,6 +91,35 @@ class Encoder extends Component{
8991 return this . setState ( state ) ;
9092 }
9193
94+ stripArray ( value ) {
95+ const regEx = new RegExp ( / ^ \[ | \] $ / gi) ;
96+ return value . replace ( regEx , "" ) ;
97+ }
98+
99+ matchRegExpValues ( values ) {
100+ const regEx = new RegExp ( / ( \[ [ 0 - 9 a - z A - Z , ] + \] | [ 0 - 9 a - z A - Z ] + ) / gi) ;
101+ return values . match ( regEx ) ;
102+ }
103+
104+ testRegExp ( search , array ) {
105+ let found = 0 ;
106+ array . forEach ( function ( a ) {
107+ if ( new RegExp ( a ) . test ( search ) && search . trim ( ) . match ( new RegExp ( a ) ) . index === 0 )
108+ found ++ ;
109+ } ) ;
110+ return found ;
111+ }
112+
113+ testRegExpValues ( values ) {
114+ const regEx = new RegExp ( / ( , + ) / gi) ;
115+ return regEx . test ( values ) ;
116+ }
117+
118+ testArrayRegExpValues ( values ) {
119+ const regEx = new RegExp ( / \[ .* \] / gi) ;
120+ return regEx . test ( values ) ;
121+ }
122+
92123 typesSet ( ) {
93124 let types = this . state . types ;
94125 if ( ! types || types . length < 1 )
@@ -126,13 +157,15 @@ class Encoder extends Component{
126157 return ;
127158 try {
128159 let types = this . state . types . split ( "," ) ;
129- let values = this . state . values . split ( "," ) ;
160+ let values = this . parseForEncode ( this . state . values ) ;
130161
131162 Log ( types , values ) ;
132163
133164 if ( types . length !== values . length )
134165 throw new Error ( "Types/values mismatch" ) ;
135166 let encoded = this . abiCoder . encode ( types , values ) ;
167+ Log ( encoded ) ;
168+
136169 this . setState ( { encoded : encoded . substring ( 2 ) } ) ;
137170 }
138171 catch ( e ) {
@@ -152,22 +185,14 @@ class Encoder extends Component{
152185 return false ;
153186 }
154187
155- handleRequestClose ( ) {
156- this . setState ( {
157- open : false ,
158- } ) ;
159- }
160-
161- handleClick ( ) {
162- this . setState ( {
163- open : true ,
164- } ) ;
165- }
166-
167188 handleChange ( name , event ) {
168189 this . setState ( { [ name ] : event . target . value , submitted : false } ) ;
169190 }
170191
192+ selectTarget ( clickEvent ) {
193+ clickEvent . target . select ( ) ;
194+ }
195+
171196 render ( ) {
172197 const { classes } = this . props ;
173198
@@ -201,7 +226,7 @@ class Encoder extends Component{
201226 error = { this . state . error . values }
202227 onChange = { this . valueUpdated }
203228 onKeyUp = { this . valueUpdated }
204- helperText = "Add the values to match the number of types indicated above, each seperated by a comma (No spaces)"
229+ helperText = "Add the values to match the number of types indicated above, each seperated by a comma (No spaces), use [ ] to wrap array "
205230 fullWidth
206231 margin = "normal"
207232 />
@@ -224,10 +249,13 @@ class Encoder extends Component{
224249 shrink : true ,
225250 } }
226251 value = { this . state . encoded }
227- disabled
228252 helperText = "Abi encoded arguments"
229253 fullWidth
230254 margin = "normal"
255+ variant = "filled"
256+ readOnly = { true }
257+ onClick = { this . selectTarget }
258+ onFocus = { this . selectTarget }
231259 />
232260 </ FormControl >
233261 </ Card > }
0 commit comments