Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add constant padding to random hexes, hashes and addresses #494

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/common-universal/src/types/CheckedAddress.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe(CheckedAddress.name, () => {

it('generates a random address with prefix', () => {
const address = CheckedAddress.random('alice')
expect(address.toLowerCase()).toMatchRegex(/^0xa11ce[0-9a-f]{35}$/)
expect(address.toLowerCase()).toMatchRegex(/^0x0000a11ce[0-9a-f]{31}$/)
})
})
})
5 changes: 3 additions & 2 deletions packages/common-universal/src/types/CheckedAddress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ export function CheckedAddress(value: string): CheckedAddress {
* Generates a random address. Tries to represent desired ascii prefix as hex value. Helps to identify transactions in the logs.
*/
CheckedAddress.random = (asciiPrefix = ''): CheckedAddress => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it will be infix now, since it's not first part of the address anymore.

I'm curious, why this change was needed in the first place?

Copy link
Contributor Author

@barrutko barrutko Dec 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if we need to be that precise to rename that to infix - both of these parts are still prerandomPart 😛

const constantAddressPrefix = '0000'
Copy link
Contributor

@krzkaczor krzkaczor Dec 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i thought about this more: i think a good prefix is deadbeef as it's obviously not random. Or simply make the prefix longer - for example: 8 chars instead of 4.

Copy link
Contributor Author

@barrutko barrutko Dec 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed to deadbeef

const hexPrefix = asciiToHex(asciiPrefix)
const postfixLength = 40 - hexPrefix.length
const postfixLength = 40 - hexPrefix.length - constantAddressPrefix.length
assert(postfixLength >= 0, `Prefix too long: ${asciiPrefix}`)
const address = hexPrefix + randomPartialHex(postfixLength)
const address = `${constantAddressPrefix}${hexPrefix}${randomPartialHex(postfixLength)}`

return CheckedAddress(`0x${address}`)
}
8 changes: 6 additions & 2 deletions packages/common-universal/src/types/Hex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ export function Hex(hex: string, { allowEmpty = false } = {}): Hex {
}

Hex.random = (ascii = '', length = 64): Hex => {
assert(ascii.length <= length, `Ascii prefix too long: ${ascii}`)
return Hex(`0x${asciiToHex(ascii)}${randomPartialHex(length - ascii.length)}`)
const constantHexPrefix = '0000'
assert(ascii.length <= length - constantHexPrefix.length, `Ascii prefix too long: ${ascii}`)
assert(length >= constantHexPrefix.length, `Total length too short: ${length}`)
return Hex(
`0x${constantHexPrefix}${asciiToHex(ascii)}${randomPartialHex(length - ascii.length - constantHexPrefix.length)}`,
)
}
Loading