Skip to content

Commit

Permalink
Fixed Hex Hash Functions (#69)
Browse files Browse the repository at this point in the history
* fixed zero padding

* readme

* Update helpers/hash-functions.helpers.ts

Co-authored-by: Arthur Suslov <[email protected]>

---------

Co-authored-by: Arthur Suslov <[email protected]>
  • Loading branch information
Arvolear and ArtSuslov authored Mar 21, 2024
1 parent 2c03d7e commit 1223062
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 23 deletions.
10 changes: 4 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)

# Solarity Front End

**Everything about Solarity ecosystem.**

## Setup

Make sure to install the dependencies:

```bash
# yarn
yarn install

# npm
npm install

# pnpm
pnpm install --shamefully-hoist
```

## Development Server
Expand Down
2 changes: 1 addition & 1 deletion composables/use-notifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export const useNotifications = () => {
error: TYPE.ERROR,
warning: TYPE.WARNING,
}[messageType],
timeout: 30000,
timeout: 10000,
closeOnClick: false,
closeButton: false,
},
Expand Down
4 changes: 2 additions & 2 deletions forms/HashFunctionForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ import { RadioButtonField, TextareaField } from '@/fields'
import {
copyToClipboard,
ErrorHandler,
hexadecimal,
hexadecimalOrEmpty,
required,
sleep,
} from '@/helpers'
Expand Down Expand Up @@ -88,7 +88,7 @@ const form = reactive({
const rules = computed(() => ({
type: { required },
text: {
...(form.type === 'hex' && { required, hexadecimal }),
...(form.type === 'hex' && { required, hexadecimalOrEmpty }),
},
}))
Expand Down
25 changes: 15 additions & 10 deletions helpers/hash-functions.helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,36 @@ import {
keccak256 as _keccak256,
sha256 as _sha256,
ripemd160 as _ripemd160,
toBeHex,
toUtf8Bytes,
} from 'ethers'
import { type HashFunction } from '@/types'

export const keccak256: HashFunction = (str, type) => {
const dataHexString = _keccak256(
type === 'text' ? toUtf8Bytes(str) : toBeHex(str),
)
const dataHexString = _keccak256(getHashArg(str, type))

return hexlify(dataHexString)
}

export const sha256: HashFunction = (str, type) => {
const dataHexString = _sha256(
type === 'text' ? toUtf8Bytes(str) : toBeHex(str),
)
const dataHexString = _sha256(getHashArg(str, type))

return hexlify(dataHexString)
}

export const ripemd160: HashFunction = (str, type) => {
const dataHexString = _ripemd160(
type === 'text' ? toUtf8Bytes(str) : toBeHex(str),
)
const dataHexString = _ripemd160(getHashArg(str, type))

return hexlify(dataHexString)
}

const getHashArg = (str: string, type: 'text' | 'hex') => {
if (type === 'text') {
return toUtf8Bytes(str)
}

if (str.length % 2) {
str = '0x0' + str.slice(2)
}

return hexlify(str)
}
5 changes: 5 additions & 0 deletions helpers/validators.helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { isAddress, isBytesLike } from 'ethers'
const HASH_REGEX = /^0x[a-fA-F0-9]{64}$/
const HEX_REGEX = /^0x[a-fA-F0-9]*$/
const HEXADECIMAL_REGEX = /^0x[a-fA-F0-9]+$/
const HEXADECIMAL_OR_EMPTY_REGEX = /^0x[a-fA-F0-9]*$/
const BINARY_REGEX = /^0b[01]+$/
const OCTAL_REGEX = /^0o?[0-7]+$/
const CONTRACT_FUNC_NAME_REGEXP = /^[a-zA-Z_][a-zA-Z0-9_]*$/
Expand Down Expand Up @@ -50,6 +51,10 @@ export const hexadecimal = <ValidationRule>(
withI18nMessage(helpers.regex(HEXADECIMAL_REGEX))
)

export const hexadecimalOrEmpty = <ValidationRule>(
withI18nMessage(helpers.regex(HEXADECIMAL_OR_EMPTY_REGEX))
)

export const binary = <ValidationRule>(
withI18nMessage(helpers.regex(BINARY_REGEX))
)
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"name": "solidity-landing",
"version": "1.0.0-rc.0",
"private": true,
"name": "@solarity/frontend",
"version": "1.0.0",
"private": false,
"gitHooks": {
"pre-commit": "yarn lint"
},
"scripts": {
"dev": "nuxt dev -o",
"nuxt-build": "nuxt build",
"start": "cross-env VITE_APP_ENVIRONMENT=development yarn nuxt-dev-server",
"start": "cross-env VITE_APP_ENVIRONMENT=development yarn dev",
"build": "cross-env VITE_APP_ENVIRONMENT=production yarn nuxt-build",
"analyze": "cross-env VITE_APP_ENVIRONMENT=analyze yarn nuxt-build",
"test": "",
Expand Down
1 change: 1 addition & 0 deletions plugins/localization/resources/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"field-error_maxLength": "Field must be less than {max} symbols",
"field-error_binary": "Field must be a binary number with prefix \"0b\"",
"field-error_hexadecimal": "Field must be a hexadecimal with prefix \"0x\"",
"field-error_hexadecimalOrEmpty": "Field must be a hexadecimal with prefix \"0x\"",
"field-error_octal": "Field must be a octal number with prefix \"0o\"",
"field-error_contractFuncName": "Field must be a valid function name",
"field-error_functionSignature": "Field must be a valid function signature",
Expand Down

0 comments on commit 1223062

Please sign in to comment.