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

Commit

Permalink
Split unpad into unpadBuffer, unpadHexString, and unpadArray
Browse files Browse the repository at this point in the history
  • Loading branch information
cgewecke committed Apr 27, 2020
1 parent 34b4b7c commit d1fc502
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 16 deletions.
39 changes: 34 additions & 5 deletions src/bytes.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const ethjsUtil = require('ethjs-util')
import * as BN from 'bn.js'
import { assertIsBuffer } from './helpers'
import { assertIsBuffer, assertIsArray, assertIsHexString } from './helpers'

/**
* Returns a buffer filled with 0s.
Expand Down Expand Up @@ -48,20 +48,49 @@ export const setLengthRight = function(msg: Buffer, length: number) {
}

/**
* Trims leading zeros from a `Buffer` or an `Array`.
* Trims leading zeros from a `Buffer`.
* @param a (Buffer)
* @return (Buffer)
*/
export const unpadBuffer = function(a: any): Buffer {
assertIsBuffer(a)
return stripZeros(a) as Buffer
}

/**
* Trims leading zeros from a `Array` (of numbers).
* @param a (number[])
* @return (number[])
*/
export const unpadArray = function(a: number[]): number[] {
assertIsArray(a)
return stripZeros(a) as number[]
}

/**
* Trims leading zeros from a hex-prefixed `String`.
* @param a (String)
* @return (String)
*/
export const unpadHexString = function(a: string): string {
assertIsHexString(a)
a = ethjsUtil.stripHexPrefix(a)
return stripZeros(a) as string
}

/**
* Trims leading zeros from a `Buffer`, `String` or `Number[]`.
* @param a (Buffer|Array|String)
* @return (Buffer|Array|String)
*/
export const unpad = function(a: any) {
a = ethjsUtil.stripHexPrefix(a)
const stripZeros = function(a: any): Buffer | number[] | string {
let first = a[0]
while (a.length > 0 && first.toString() === '0') {
a = a.slice(1)
first = a[0]
}
return a
}
export const stripZeros = unpad

/**
* Attempts to turn a value into a `Buffer`. As input it supports `Buffer`, `String`, `Number`, null/undefined, `BN` and other objects with a `toArray()` method.
Expand Down
11 changes: 11 additions & 0 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,14 @@ export const assertIsBuffer = function(input: Buffer): void {
throw new Error(msg)
}
}

/**
* Throws if input is not an array
* @param {number[]} input value to check
*/
export const assertIsArray = function(input: number[]): void {
const msg = `This method only supports number arrays but input was: ${input}`
if (!Array.isArray(input)) {
throw new Error(msg)
}
}
4 changes: 2 additions & 2 deletions src/object.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const ethjsUtil = require('ethjs-util')
import * as assert from 'assert'
import * as rlp from 'rlp'
import { toBuffer, baToJSON, stripZeros } from './bytes'
import { toBuffer, baToJSON, unpadBuffer } from './bytes'

/**
* Defines properties on a `Object`. It make the assumption that underlying data is binary.
Expand Down Expand Up @@ -48,7 +48,7 @@ export const defineProperties = function(self: any, fields: any, data?: any) {
}

if (field.allowLess && field.length) {
v = stripZeros(v)
v = unpadBuffer(v)
assert(
field.length >= v.length,
`The field ${field.name} must not have more ${field.length} bytes`,
Expand Down
43 changes: 34 additions & 9 deletions test/bytes.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import {
zeros,
zeroAddress,
isZeroAddress,
unpad,
unpadBuffer,
unpadArray,
unpadHexString,
setLengthLeft,
setLengthRight,
bufferToHex,
Expand Down Expand Up @@ -47,20 +49,43 @@ describe('is zero address', function() {
})
})

describe('unpad', function() {
it('should unpad a string', function() {
const str = '0000000006600'
const r = unpad(str)
assert.equal(r, '6600')
describe('unpadBuffer', function() {
it('should unpad a Buffer', function() {
const buf = toBuffer('0x0000000006600')
const r = unpadBuffer(buf)
assert.deepEqual(r, toBuffer('0x6600'))
})
it('should throw if input is not a Buffer', function() {
assert.throws(function() {
unpadBuffer('0000000006600')
})
})
})

describe('unpadArray', function() {
it('should unpad an Array', function() {
const arr = [0, 0, 0, 1]
const r = unpadArray(arr)
assert.deepEqual(r, [1])
})
it('should throw if input is not an Array', function() {
assert.throws(function() {
unpadArray((<unknown>toBuffer([0, 0, 0, 1])) as number[])
})
})
})

describe('unpad a hex string', function() {
it('should unpad a string', function() {
describe('unpadHexString', function() {
it('should unpad a hex prefixed string', function() {
const str = '0x0000000006600'
const r = unpad(str)
const r = unpadHexString(str)
assert.equal(r, '6600')
})
it('should throw if input is not hex-prefixed', function() {
assert.throws(function() {
unpadHexString('0000000006600')
})
})
})

describe('setLengthLeft', function() {
Expand Down

0 comments on commit d1fc502

Please sign in to comment.