Skip to content

Commit

Permalink
Improve utils with hexToBuffer
Browse files Browse the repository at this point in the history
  • Loading branch information
yorhodes committed Nov 22, 2019
1 parent eed93e8 commit 138308a
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 40 deletions.
4 changes: 2 additions & 2 deletions packages/contractkit/src/wrappers/BaseWrapper.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ensureLeading0x, trimLeading0x } from '@celo/utils/lib/address'
import { ensureLeading0x, hexToBuffer } from '@celo/utils/lib/address'
import { zip } from '@celo/utils/lib/collections'
import BigNumber from 'bignumber.js'
import Contract from 'web3/eth/contract'
Expand Down Expand Up @@ -33,7 +33,7 @@ export const valueToInt = (input: BigNumber.Value) =>
export const valueToFrac = (numerator: BigNumber.Value, denominator: BigNumber.Value) =>
valueToBigNumber(numerator).div(valueToBigNumber(denominator))

export const stringToBuffer = (input: string) => Buffer.from(trimLeading0x(input), 'hex')
export const stringToBuffer = hexToBuffer

export const bufferToString = (buf: Buffer) => ensureLeading0x(buf.toString('hex'))

Expand Down
8 changes: 2 additions & 6 deletions packages/mobile/src/identity/commentKey.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { trimLeading0x } from '@celo/utils/src/address'
import { hexToBuffer } from '@celo/utils/src/address'
import { encryptComment as encryptCommentRaw } from '@celo/utils/src/commentEncryption'
import { getAttestationsContract, getDataEncryptionKey } from '@celo/walletkit'
import { web3 } from 'src/web3/contracts'
Expand All @@ -7,11 +7,7 @@ export async function getCommentKey(address: string): Promise<Buffer | null> {
const attestations = await getAttestationsContract(web3)
const hexString = await getDataEncryptionKey(attestations, address)
// No comment key -> empty string returned from getDEK. This is expected for old addresses created before comment encryption change
if (!hexString) {
return null
}
// Buffer.from will create an empty buffer if the input string has '0x' prepended
return Buffer.from(trimLeading0x(hexString), 'hex')
return !hexString ? null : hexToBuffer(hexString)
}

export async function encryptComment(
Expand Down
4 changes: 2 additions & 2 deletions packages/mobile/src/verify/VerificationCodeRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import withTextInputPasteAware from '@celo/react-components/components/WithTextI
import Checkmark from '@celo/react-components/icons/Checkmark'
import colors from '@celo/react-components/styles/colors'
import fontStyles from '@celo/react-components/styles/fonts'
import { trimLeading0x } from '@celo/utils/src/address'
import { hexToBuffer } from '@celo/utils/src/address'
import { extractAttestationCodeFromMessage } from '@celo/walletkit'
import * as React from 'react'
import { withNamespaces, WithNamespaces } from 'react-i18next'
Expand Down Expand Up @@ -92,7 +92,7 @@ function getRecodedAttestationValue(attestationCode: AttestationCode, t: i18n.Tr
if (!attestationCode.code || attestationCode.code === ATTESTATION_CODE_PLACEHOLDER) {
return t('input.codeAccepted')
}
return Buffer.from(trimLeading0x(attestationCode.code), 'hex').toString('base64')
return hexToBuffer(attestationCode.code).toString('base64')
} catch (error) {
Logger.warn(TAG, 'Could not recode verification code to base64')
return t('input.codeAccepted')
Expand Down
48 changes: 18 additions & 30 deletions packages/utils/src/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,37 +8,25 @@ import {

export type Address = string

export function eqAddress(a: Address, b: Address) {
return trimLeading0x(a).toLowerCase() === trimLeading0x(b).toLowerCase()
}

export function trimLeading0x(input: string) {
return input.startsWith('0x') ? input.slice(2) : input
}

export function ensureLeading0x(input: string) {
return input.startsWith('0x') ? input : `0x${input}`
}

export const privateKeyToAddress = (privateKey: string) => {
return toChecksumAddress(
ensureLeading0x(privateToAddress(Buffer.from(trimLeading0x(privateKey), 'hex')).toString('hex'))
)
}

export const privateKeyToPublicKey = (privateKey: string) => {
return toChecksumAddress(
ensureLeading0x(privateToPublic(Buffer.from(trimLeading0x(privateKey), 'hex')).toString('hex'))
)
}

export const publicKeyToAddress = (publicKey: string) => {
return toChecksumAddress(
ensureLeading0x(pubToAddress(Buffer.from(trimLeading0x(publicKey), 'hex')).toString('hex'))
)
}
export const eqAddress = (a: Address, b: Address) =>
trimLeading0x(a).toLowerCase() === trimLeading0x(b).toLowerCase()

export const trimLeading0x = (input: string) => (input.startsWith('0x') ? input.slice(2) : input)

export const ensureLeading0x = (input: string) => (input.startsWith('0x') ? input : `0x${input}`)

export const hexToBuffer = (input: string) => Buffer.from(trimLeading0x(input), 'hex')

export const privateKeyToAddress = (privateKey: string) =>
toChecksumAddress(ensureLeading0x(privateToAddress(hexToBuffer(privateKey)).toString('hex')))

export const privateKeyToPublicKey = (privateKey: string) =>
toChecksumAddress(ensureLeading0x(privateToPublic(hexToBuffer(privateKey)).toString('hex')))

export const publicKeyToAddress = (publicKey: string) =>
toChecksumAddress(ensureLeading0x(pubToAddress(hexToBuffer(publicKey)).toString('hex')))

export const isValidPrivateKey = (privateKey: string) =>
privateKey.startsWith('0x') && isValidPrivate(Buffer.from(privateKey.slice(2), 'hex'))
privateKey.startsWith('0x') && isValidPrivate(hexToBuffer(privateKey))

export { isValidAddress, isValidChecksumAddress, toChecksumAddress } from 'ethereumjs-util'

0 comments on commit 138308a

Please sign in to comment.