Skip to content

Commit

Permalink
feat(util): implement assertIsUint8Array
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Faust committed Sep 7, 2022
1 parent 1e0de28 commit 17e1952
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 16 deletions.
20 changes: 10 additions & 10 deletions packages/util/src/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
zeros,
} from './bytes'
import { KECCAK256_NULL, KECCAK256_RLP } from './constants'
import { assertIsBuffer, assertIsHexString, assertIsString } from './helpers'
import { assertIsHexString, assertIsString, assertIsUint8Array } from './helpers'
import { stripHexPrefix } from './internal'

import type { BigIntLike, BufferLike } from './types'
Expand Down Expand Up @@ -195,8 +195,8 @@ export const isValidChecksumAddress = function (
* @param nonce The nonce of the from account
*/
export const generateAddress = function (from: Buffer, nonce: Buffer): Buffer {
assertIsBuffer(from)
assertIsBuffer(nonce)
assertIsUint8Array(from)
assertIsUint8Array(nonce)

if (bufferToBigInt(nonce) === BigInt(0)) {
// in RLP we want to encode null in the case of zero nonce
Expand All @@ -215,9 +215,9 @@ export const generateAddress = function (from: Buffer, nonce: Buffer): Buffer {
* @param initCode The init code of the contract being created
*/
export const generateAddress2 = function (from: Buffer, salt: Buffer, initCode: Buffer): Buffer {
assertIsBuffer(from)
assertIsBuffer(salt)
assertIsBuffer(initCode)
assertIsUint8Array(from)
assertIsUint8Array(salt)
assertIsUint8Array(initCode)

if (from.length !== 20) {
throw new Error('Expected from to be of length 20')
Expand Down Expand Up @@ -247,7 +247,7 @@ export const isValidPrivate = function (privateKey: Buffer): boolean {
* @param sanitize Accept public keys in other formats
*/
export const isValidPublic = function (publicKey: Buffer, sanitize: boolean = false): boolean {
assertIsBuffer(publicKey)
assertIsUint8Array(publicKey)
if (publicKey.length === 64) {
// Convert to SEC1 for secp256k1
// Automatically checks whether point is on curve
Expand Down Expand Up @@ -278,7 +278,7 @@ export const isValidPublic = function (publicKey: Buffer, sanitize: boolean = fa
* @param sanitize Accept public keys in other formats
*/
export const pubToAddress = function (pubKey: Buffer, sanitize: boolean = false): Buffer {
assertIsBuffer(pubKey)
assertIsUint8Array(pubKey)
if (sanitize && pubKey.length !== 64) {
pubKey = Buffer.from(Point.fromHex(pubKey).toRawBytes(false).slice(1))
}
Expand All @@ -295,7 +295,7 @@ export const publicToAddress = pubToAddress
* @param privateKey A private key must be 256 bits wide
*/
export const privateToPublic = function (privateKey: Buffer): Buffer {
assertIsBuffer(privateKey)
assertIsUint8Array(privateKey)
// skip the type flag and use the X, Y points
return Buffer.from(Point.fromPrivateKey(privateKey).toRawBytes(false).slice(1))
}
Expand All @@ -312,7 +312,7 @@ export const privateToAddress = function (privateKey: Buffer): Buffer {
* Converts a public key to the Ethereum format.
*/
export const importPublic = function (publicKey: Buffer): Buffer {
assertIsBuffer(publicKey)
assertIsUint8Array(publicKey)
if (publicKey.length !== 64) {
publicKey = Buffer.from(Point.fromHex(publicKey).toRawBytes(false).slice(1))
}
Expand Down
8 changes: 4 additions & 4 deletions packages/util/src/bytes.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { assertIsArray, assertIsBuffer, assertIsHexString } from './helpers'
import { assertIsArray, assertIsHexString, assertIsUint8Array } from './helpers'
import { isHexPrefixed, isHexString, padToEven, stripHexPrefix } from './internal'

import type {
Expand Down Expand Up @@ -72,7 +72,7 @@ const setLength = function (msg: Buffer, length: number, right: boolean) {
* @return (Buffer)
*/
export const setLengthLeft = function (msg: Buffer, length: number) {
assertIsBuffer(msg)
assertIsUint8Array(msg)
return setLength(msg, length, false)
}

Expand All @@ -84,7 +84,7 @@ export const setLengthLeft = function (msg: Buffer, length: number) {
* @return (Buffer)
*/
export const setLengthRight = function (msg: Buffer, length: number) {
assertIsBuffer(msg)
assertIsUint8Array(msg)
return setLength(msg, length, true)
}

Expand All @@ -108,7 +108,7 @@ const stripZeros = function (a: any): Buffer | number[] | string {
* @return (Buffer)
*/
export const unpadBuffer = function (a: Buffer): Buffer {
assertIsBuffer(a)
assertIsUint8Array(a)
return stripZeros(a) as Buffer
}

Expand Down
11 changes: 11 additions & 0 deletions packages/util/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@ export const assertIsBuffer = function (input: Buffer): void {
}
}

/**
* Throws if input is not a `Uint8Array`
* @param {Buffer} input value to check
*/
export const assertIsUint8Array = function (input: Uint8Array): void {
if (!(input instanceof Uint8Array)) {
const msg = `This method only supports Buffer but input was: ${input}`
throw new Error(msg)
}
}

/**
* Throws if input is not an array
* @param {number[]} input value to check
Expand Down
4 changes: 2 additions & 2 deletions packages/util/src/signature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { recoverPublicKey, signSync } from 'ethereum-cryptography/secp256k1'

import { bufferToBigInt, bufferToHex, bufferToInt, setLengthLeft, toBuffer } from './bytes'
import { SECP256K1_ORDER, SECP256K1_ORDER_DIV_2 } from './constants'
import { assertIsBuffer } from './helpers'
import { assertIsUint8Array } from './helpers'

export interface ECDSASignature {
v: bigint
Expand Down Expand Up @@ -187,7 +187,7 @@ export const isValidSignature = function (
* used to produce the signature.
*/
export const hashPersonalMessage = function (message: Buffer): Buffer {
assertIsBuffer(message)
assertIsUint8Array(message)
const prefix = Buffer.from(`\u0019Ethereum Signed Message:\n${message.length}`, 'utf-8')
return Buffer.from(keccak256(Buffer.concat([prefix, message])))
}

0 comments on commit 17e1952

Please sign in to comment.