Skip to content
This repository was archived by the owner on Jun 17, 2021. It is now read-only.

Commit 686f8da

Browse files
committed
Enforce buffer inputs for setLengthLeft/Right
1 parent 2cdd255 commit 686f8da

File tree

3 files changed

+34
-33
lines changed

3 files changed

+34
-33
lines changed

docs/modules/_bytes_.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -135,46 +135,46 @@ ___
135135

136136
### `Const` setLengthLeft
137137

138-
**setLengthLeft**(`msg`: any, `length`: number, `right`: boolean): *any*
138+
**setLengthLeft**(`msg`: Buffer, `length`: number, `right`: boolean): *any*
139139

140140
*Defined in [bytes.ts:20](https://github.com/ethereumjs/ethereumjs-util/blob/master/src/bytes.ts#L20)*
141141

142-
Left Pads an `Array` or `Buffer` with leading zeros till it has `length` bytes.
142+
Left Pads a `Buffer` with leading zeros till it has `length` bytes.
143143
Or it truncates the beginning if it exceeds.
144144

145145
**Parameters:**
146146

147147
Name | Type | Default | Description |
148148
------ | ------ | ------ | ------ |
149-
`msg` | any | - | the value to pad (Buffer|Array) |
149+
`msg` | Buffer | - | the value to pad (Buffer) |
150150
`length` | number | - | the number of bytes the output should be |
151151
`right` | boolean | false | whether to start padding form the left or right |
152152

153153
**Returns:** *any*
154154

155-
(Buffer|Array)
155+
(Buffer)
156156

157157
___
158158

159159
### `Const` setLengthRight
160160

161-
**setLengthRight**(`msg`: any, `length`: number): *any*
161+
**setLengthRight**(`msg`: Buffer, `length`: number): *any*
162162

163163
*Defined in [bytes.ts:46](https://github.com/ethereumjs/ethereumjs-util/blob/master/src/bytes.ts#L46)*
164164

165-
Right Pads an `Array` or `Buffer` with leading zeros till it has `length` bytes.
165+
Right Pads a `Buffer` with leading zeros till it has `length` bytes.
166166
Or it truncates the beginning if it exceeds.
167167

168168
**Parameters:**
169169

170170
Name | Type | Description |
171171
------ | ------ | ------ |
172-
`msg` | any | the value to pad (Buffer|Array) |
172+
`msg` | Buffer | the value to pad (Buffer) |
173173
`length` | number | the number of bytes the output should be |
174174

175175
**Returns:** *any*
176176

177-
(Buffer|Array)
177+
(Buffer)
178178

179179
___
180180

src/bytes.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const ethjsUtil = require('ethjs-util')
22
import * as BN from 'bn.js'
3+
import { assertIsBuffer } from './helpers'
34

45
/**
56
* Returns a buffer filled with 0s.
@@ -10,16 +11,16 @@ export const zeros = function(bytes: number): Buffer {
1011
}
1112

1213
/**
13-
* Left Pads an `Array` or `Buffer` with leading zeros till it has `length` bytes.
14+
* Left Pads a `Buffer` with leading zeros till it has `length` bytes.
1415
* Or it truncates the beginning if it exceeds.
15-
* @param msg the value to pad (Buffer|Array)
16+
* @param msg the value to pad (Buffer)
1617
* @param length the number of bytes the output should be
1718
* @param right whether to start padding form the left or right
18-
* @return (Buffer|Array)
19+
* @return (Buffer)
1920
*/
20-
export const setLengthLeft = function(msg: any, length: number, right: boolean = false) {
21+
export const setLengthLeft = function(msg: Buffer, length: number, right: boolean = false) {
22+
assertIsBuffer(msg)
2123
const buf = zeros(length)
22-
msg = toBuffer(msg)
2324
if (right) {
2425
if (msg.length < length) {
2526
msg.copy(buf)
@@ -37,14 +38,14 @@ export const setLengthLeft = function(msg: any, length: number, right: boolean =
3738
export const setLength = setLengthLeft
3839

3940
/**
40-
* Right Pads an `Array` or `Buffer` with leading zeros till it has `length` bytes.
41+
* Right Pads a `Buffer` with leading zeros till it has `length` bytes.
4142
* Or it truncates the beginning if it exceeds.
42-
* @param msg the value to pad (Buffer|Array)
43+
* @param msg the value to pad (Buffer)
4344
* @param length the number of bytes the output should be
44-
* @return (Buffer|Array)
45+
* @return (Buffer)
4546
*/
46-
export const setLengthRight = function(msg: any, length: number) {
47-
return setLength(msg, length, true)
47+
export const setLengthRight = function(msg: Buffer, length: number) {
48+
return setLengthLeft(msg, length, true)
4849
}
4950

5051
/**
@@ -61,6 +62,7 @@ export const unpad = function(a: any) {
6162
}
6263
return a
6364
}
65+
6466
export const stripZeros = unpad
6567

6668
/**

test/bytes.spec.ts

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import {
55
zeroAddress,
66
isZeroAddress,
77
unpad,
8-
setLength,
98
setLengthLeft,
109
setLengthRight,
1110
bufferToHex,
@@ -64,39 +63,39 @@ describe('unpad a hex string', function() {
6463
})
6564
})
6665

67-
describe('pad', function() {
66+
describe('setLengthLeft', function() {
6867
it('should left pad a Buffer', function() {
6968
const buf = Buffer.from([9, 9])
70-
const padded = setLength(buf, 3)
69+
const padded = setLengthLeft(buf, 3)
7170
assert.equal(padded.toString('hex'), '000909')
7271
})
7372
it('should left truncate a Buffer', function() {
7473
const buf = Buffer.from([9, 0, 9])
75-
const padded = setLength(buf, 2)
74+
const padded = setLengthLeft(buf, 2)
7675
assert.equal(padded.toString('hex'), '0009')
7776
})
78-
it('should left pad a Buffer - alias', function() {
79-
const buf = Buffer.from([9, 9])
80-
const padded = setLengthLeft(buf, 3)
81-
assert.equal(padded.toString('hex'), '000909')
77+
it('should throw if input is not a Buffer', function() {
78+
assert.throws(function() {
79+
setLengthLeft((<unknown>[9, 9]) as Buffer, 3)
80+
})
8281
})
8382
})
8483

85-
describe('rpad', function() {
84+
describe('setLengthRight', function() {
8685
it('should right pad a Buffer', function() {
8786
const buf = Buffer.from([9, 9])
88-
const padded = setLength(buf, 3, true)
87+
const padded = setLengthRight(buf, 3)
8988
assert.equal(padded.toString('hex'), '090900')
9089
})
9190
it('should right truncate a Buffer', function() {
9291
const buf = Buffer.from([9, 0, 9])
93-
const padded = setLength(buf, 2, true)
92+
const padded = setLengthRight(buf, 2)
9493
assert.equal(padded.toString('hex'), '0900')
9594
})
96-
it('should right pad a Buffer - alias', function() {
97-
const buf = Buffer.from([9, 9])
98-
const padded = setLengthRight(buf, 3)
99-
assert.equal(padded.toString('hex'), '090900')
95+
it('should throw if input is not a Buffer', function() {
96+
assert.throws(function() {
97+
setLengthRight((<unknown>[9, 9]) as Buffer, 3)
98+
})
10099
})
101100
})
102101

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy