-
Notifications
You must be signed in to change notification settings - Fork 751
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
feat(util): implement assertIsUint8Array
#2271
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,11 +10,11 @@ import { bigIntToBuffer, bufferToBigInt, toBuffer, zeros } from './bytes' | |
export class Address { | ||
public readonly buf: Buffer | ||
|
||
constructor(buf: Buffer) { | ||
constructor(buf: Uint8Array) { | ||
if (buf.length !== 20) { | ||
throw new Error('Invalid address length') | ||
} | ||
this.buf = buf | ||
this.buf = toBuffer(buf) | ||
} | ||
|
||
/** | ||
|
@@ -39,7 +39,7 @@ export class Address { | |
* Returns an address for a given public key. | ||
* @param pubKey The two points of an uncompressed key | ||
*/ | ||
static fromPublicKey(pubKey: Buffer): Address { | ||
static fromPublicKey(pubKey: Uint8Array): Address { | ||
if (!Buffer.isBuffer(pubKey)) { | ||
throw new Error('Public key should be Buffer') | ||
} | ||
|
@@ -51,7 +51,7 @@ export class Address { | |
* Returns an address for a given private key. | ||
* @param privateKey A private key must be 256 bits wide | ||
*/ | ||
static fromPrivateKey(privateKey: Buffer): Address { | ||
static fromPrivateKey(privateKey: Uint8Array): Address { | ||
if (!Buffer.isBuffer(privateKey)) { | ||
throw new Error('Private key should be Buffer') | ||
} | ||
|
@@ -77,7 +77,7 @@ export class Address { | |
* @param salt A salt | ||
* @param initCode The init code of the contract being created | ||
*/ | ||
static generate2(from: Address, salt: Buffer, initCode: Buffer): Address { | ||
static generate2(from: Address, salt: Buffer, initCode: Uint8Array): Address { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
if (!Buffer.isBuffer(salt)) { | ||
throw new Error('Expected salt to be a Buffer') | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,7 @@ | ||
import { assertIsArray, assertIsBuffer, assertIsHexString } from './helpers' | ||
import { assertIsArray, assertIsHexString, assertIsUint8Array } from './helpers' | ||
import { isHexPrefixed, isHexString, padToEven, stripHexPrefix } from './internal' | ||
|
||
import type { | ||
NestedBufferArray, | ||
NestedUint8Array, | ||
PrefixedHexString, | ||
TransformableToArray, | ||
|
@@ -72,7 +71,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) | ||
} | ||
|
||
|
@@ -84,7 +83,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) | ||
} | ||
|
||
|
@@ -107,8 +106,8 @@ const stripZeros = function (a: any): Buffer | number[] | string { | |
* @param a (Buffer) | ||
* @return (Buffer) | ||
*/ | ||
export const unpadBuffer = function (a: Buffer): Buffer { | ||
assertIsBuffer(a) | ||
export const unpadBuffer = function (a: Uint8Array): Buffer { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have again this naming problem here. Additionally I don't think it's a good thing for conversion functions to change the type in between, so here, to then have I wouldn't want to do too quick decisions on these kind of things, since implications on how we can deal with this later on can be significant. So I think if we do we should likely go the a bit harder way and also add here parallel There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There's no reason to support both because a
|
||
assertIsUint8Array(a) | ||
return stripZeros(a) as Buffer | ||
} | ||
|
||
|
@@ -202,15 +201,15 @@ export const toBuffer = function (v: ToBufferInputTypes): Buffer { | |
* Converts a `Buffer` into a `0x`-prefixed hex `String`. | ||
* @param buf `Buffer` object to convert | ||
*/ | ||
export const bufferToHex = function (buf: Buffer): string { | ||
export const bufferToHex = function (buf: Uint8Array): string { | ||
buf = toBuffer(buf) | ||
return '0x' + buf.toString('hex') | ||
return '0x' + Buffer.from(buf).toString('hex') | ||
} | ||
|
||
/** | ||
* Converts a {@link Buffer} to a {@link bigint} | ||
*/ | ||
export function bufferToBigInt(buf: Buffer) { | ||
export function bufferToBigInt(buf: Uint8Array) { | ||
const hex = bufferToHex(buf) | ||
if (hex === '0x') { | ||
return BigInt(0) | ||
|
@@ -230,7 +229,7 @@ export function bigIntToBuffer(num: bigint) { | |
* @param buf `Buffer` object to convert | ||
* @throws If the input number exceeds 53 bits. | ||
*/ | ||
export const bufferToInt = function (buf: Buffer): number { | ||
export const bufferToInt = function (buf: Uint8Array): number { | ||
const res = Number(bufferToBigInt(buf)) | ||
if (!Number.isSafeInteger(res)) throw new Error('Number exceeds 53 bits') | ||
return res | ||
|
@@ -240,7 +239,7 @@ export const bufferToInt = function (buf: Buffer): number { | |
* Interprets a `Buffer` as a signed integer and returns a `BigInt`. Assumes 256-bit numbers. | ||
* @param num Signed integer value | ||
*/ | ||
export const fromSigned = function (num: Buffer): bigint { | ||
export const fromSigned = function (num: Uint8Array): bigint { | ||
return BigInt.asIntN(256, bufferToBigInt(num)) | ||
} | ||
|
||
|
@@ -346,25 +345,25 @@ export const validateNoLeadingZeroes = function (values: { [key: string]: Buffer | |
} | ||
|
||
/** | ||
* Converts a {@link Uint8Array} or {@link NestedUint8Array} to {@link Buffer} or {@link NestedBufferArray} | ||
* Converts a {@link Uint8Array} or {@link NestedUint8Array} to {@link Buffer} or {@link NestedUint8Array} | ||
*/ | ||
export function arrToBufArr(arr: Uint8Array): Buffer | ||
export function arrToBufArr(arr: NestedUint8Array): NestedBufferArray | ||
export function arrToBufArr(arr: Uint8Array | NestedUint8Array): Buffer | NestedBufferArray | ||
export function arrToBufArr(arr: Uint8Array | NestedUint8Array): Buffer | NestedBufferArray { | ||
export function arrToBufArr(arr: NestedUint8Array): NestedUint8Array | ||
export function arrToBufArr(arr: Uint8Array | NestedUint8Array): Buffer | NestedUint8Array | ||
export function arrToBufArr(arr: Uint8Array | NestedUint8Array): Buffer | NestedUint8Array { | ||
if (!Array.isArray(arr)) { | ||
return Buffer.from(arr) | ||
} | ||
return arr.map((a) => arrToBufArr(a)) | ||
} | ||
|
||
/** | ||
* Converts a {@link Buffer} or {@link NestedBufferArray} to {@link Uint8Array} or {@link NestedUint8Array} | ||
* Converts a {@link Buffer} or {@link NestedUint8Array} to {@link Uint8Array} or {@link NestedUint8Array} | ||
*/ | ||
export function bufArrToArr(arr: Buffer): Uint8Array | ||
export function bufArrToArr(arr: NestedBufferArray): NestedUint8Array | ||
export function bufArrToArr(arr: Buffer | NestedBufferArray): Uint8Array | NestedUint8Array | ||
export function bufArrToArr(arr: Buffer | NestedBufferArray): Uint8Array | NestedUint8Array { | ||
export function bufArrToArr(arr: Uint8Array): Uint8Array | ||
export function bufArrToArr(arr: NestedUint8Array): NestedUint8Array | ||
export function bufArrToArr(arr: Uint8Array | NestedUint8Array): Uint8Array | NestedUint8Array | ||
export function bufArrToArr(arr: Uint8Array | NestedUint8Array): Uint8Array | NestedUint8Array { | ||
if (!Array.isArray(arr)) { | ||
return Uint8Array.from(arr ?? []) | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
from
forgotten