Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
F
frontend
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
vicotor
frontend
Commits
5ffd2a62
Commit
5ffd2a62
authored
Dec 08, 2023
by
tom
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
bytes validation
parent
4f799da1
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
30 additions
and
2 deletions
+30
-2
stringToBytes.tsx
lib/stringToBytes.tsx
+4
-0
ContractMethodField.tsx
ui/address/contract/ContractMethodField.tsx
+24
-2
utils.ts
ui/address/contract/utils.ts
+2
-0
No files found.
lib/stringToBytes.tsx
0 → 100644
View file @
5ffd2a62
export
default
function
stringToBytes
(
str
:
string
)
{
const
utf8Encode
=
new
TextEncoder
();
return
utf8Encode
.
encode
(
str
);
}
ui/address/contract/ContractMethodField.tsx
View file @
5ffd2a62
...
@@ -14,10 +14,11 @@ import { isAddress } from 'viem';
...
@@ -14,10 +14,11 @@ import { isAddress } from 'viem';
import
type
{
MethodFormFields
}
from
'
./types
'
;
import
type
{
MethodFormFields
}
from
'
./types
'
;
import
type
{
SmartContractMethodArgType
}
from
'
types/api/contract
'
;
import
type
{
SmartContractMethodArgType
}
from
'
types/api/contract
'
;
import
stringToBytes
from
'
lib/stringToBytes
'
;
import
ClearButton
from
'
ui/shared/ClearButton
'
;
import
ClearButton
from
'
ui/shared/ClearButton
'
;
import
ContractMethodFieldZeroes
from
'
./ContractMethodFieldZeroes
'
;
import
ContractMethodFieldZeroes
from
'
./ContractMethodFieldZeroes
'
;
import
{
INT_REGEXP
,
getIntBoundaries
,
formatBooleanValue
}
from
'
./utils
'
;
import
{
INT_REGEXP
,
BYTES_REGEXP
,
getIntBoundaries
,
formatBooleanValue
}
from
'
./utils
'
;
interface
Props
{
interface
Props
{
control
:
Control
<
MethodFormFields
>
;
control
:
Control
<
MethodFormFields
>
;
...
@@ -59,6 +60,10 @@ const ContractMethodField = ({ control, name, valueType, placeholder, setValue,
...
@@ -59,6 +60,10 @@ const ContractMethodField = ({ control, name, valueType, placeholder, setValue,
return
{
isUnsigned
,
power
,
min
,
max
};
return
{
isUnsigned
,
power
,
min
,
max
};
},
[
valueType
]);
},
[
valueType
]);
const
bytesMatch
=
React
.
useMemo
(()
=>
{
return
valueType
.
match
(
BYTES_REGEXP
);
},
[
valueType
]);
const
renderInput
=
React
.
useCallback
((
const
renderInput
=
React
.
useCallback
((
{
field
,
formState
}:
{
field
:
ControllerRenderProps
<
MethodFormFields
>
;
formState
:
UseFormStateReturn
<
MethodFormFields
>
},
{
field
,
formState
}:
{
field
:
ControllerRenderProps
<
MethodFormFields
>
;
formState
:
UseFormStateReturn
<
MethodFormFields
>
},
)
=>
{
)
=>
{
...
@@ -129,8 +134,25 @@ const ContractMethodField = ({ control, name, valueType, placeholder, setValue,
...
@@ -129,8 +134,25 @@ const ContractMethodField = ({ control, name, valueType, placeholder, setValue,
}
}
}
}
if
(
bytesMatch
)
{
const
[
,
length
]
=
bytesMatch
;
if
(
value
.
startsWith
(
'
0x
'
))
{
if
(
value
.
replace
(
'
0x
'
,
''
).
length
%
2
!==
0
)
{
return
'
Invalid bytes format
'
;
}
}
if
(
length
)
{
const
valueLengthInBytes
=
value
.
startsWith
(
'
0x
'
)
?
value
.
replace
(
'
0x
'
,
''
).
length
/
2
:
stringToBytes
(
value
).
length
;
return
valueLengthInBytes
>
Number
(
length
)
?
`Value should be a maximum of
${
length
}
bytes`
:
true
;
}
return
true
;
}
return
true
;
return
true
;
},
[
intMatch
,
valueType
]);
},
[
bytesMatch
,
intMatch
,
valueType
]);
return
(
return
(
<>
<>
...
...
ui/address/contract/utils.ts
View file @
5ffd2a62
...
@@ -4,6 +4,8 @@ import type { SmartContractWriteMethod } from 'types/api/contract';
...
@@ -4,6 +4,8 @@ import type { SmartContractWriteMethod } from 'types/api/contract';
export
const
INT_REGEXP
=
/^
(
u
)?
int
(\d
+
)?
$/i
;
export
const
INT_REGEXP
=
/^
(
u
)?
int
(\d
+
)?
$/i
;
export
const
BYTES_REGEXP
=
/^bytes
(\d
+
)?
$/i
;
export
const
getIntBoundaries
=
(
power
:
number
,
isUnsigned
:
boolean
)
=>
{
export
const
getIntBoundaries
=
(
power
:
number
,
isUnsigned
:
boolean
)
=>
{
const
maxUnsigned
=
2
**
power
;
const
maxUnsigned
=
2
**
power
;
const
max
=
isUnsigned
?
maxUnsigned
-
1
:
maxUnsigned
/
2
-
1
;
const
max
=
isUnsigned
?
maxUnsigned
-
1
:
maxUnsigned
/
2
-
1
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment