Skip to content

Commit

Permalink
fix: add padding to bigints whose byte-length is expected
Browse files Browse the repository at this point in the history
fixes #283
  • Loading branch information
mirceanis committed Jun 26, 2023
1 parent 49aed9c commit 077dc9b
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/__tests__/VerifierAlgorithm.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,8 @@ const publicKeyBase58 = bytesToBase58(publicKeyBytes)
const publicKeyJwk = {
crv: 'secp256k1',
kty: 'EC',
x: bytesToBase64url(bigintToBytes(publicKeyPoint.x)),
y: bytesToBase64url(bigintToBytes(publicKeyPoint.y)),
x: bytesToBase64url(bigintToBytes(publicKeyPoint.x, 32)),
y: bytesToBase64url(bigintToBytes(publicKeyPoint.y, 32)),
}
const publicKeyMultibase = bytesToMultibase(publicKeyBytes, 'base58btc')
const eip155 = toEthereumAddress(publicKeyHex)
Expand Down
65 changes: 65 additions & 0 deletions src/__tests__/util.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { bigintToBytes, hexToBytes } from '../util'

describe('bigintToBytes', () => {
it('should convert a bigint to bytes', () => {
const bn = BigInt(65535)
const bytes = bigintToBytes(bn)
expect(bytes).toEqual(new Uint8Array([255, 255]))
})

it('should convert a bigint to bytes given a minimum length', () => {
const bn = BigInt(65535)
const bytes = bigintToBytes(bn, 32)
expect(bytes).toEqual(
new Uint8Array([
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255,
])
)
})

it('should convert a bigint to bytes given a minimum length less than the number', () => {
const bn = BigInt('0x112233445566778899')
const bytes = bigintToBytes(bn, 5)
expect(bytes).toEqual(new Uint8Array([17, 34, 51, 68, 85, 102, 119, 136, 153]))
})

it('should convert a bigint to bytes given an odd number of bytes', () => {
const bn = BigInt('0x101010101010101')
const bytes = bigintToBytes(bn)
expect(bytes).toEqual(new Uint8Array([1, 1, 1, 1, 1, 1, 1, 1]))
})
})

describe('hexToBytes', () => {
it('should convert a hex string to bytes', () => {
const bn = '0101'
const bytes = hexToBytes(bn)
expect(bytes).toEqual(new Uint8Array([1, 1]))
})

it('should convert a hex string with a prefix to bytes', () => {
const bn = '0x0101'
const bytes = hexToBytes(bn)
expect(bytes).toEqual(new Uint8Array([1, 1]))
})

it('should convert a hex string to bytes given a minimum length', () => {
const bn = '0101'
const bytes = hexToBytes(bn, 32)
expect(bytes).toEqual(
new Uint8Array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1])
)
})

it('should convert a hex string to bytes given a minimum length less than the number', () => {
const bn = '0x112233445566778899'
const bytes = hexToBytes(bn, 5)
expect(bytes).toEqual(new Uint8Array([17, 34, 51, 68, 85, 102, 119, 136, 153]))
})

it('should convert a hexString to bytes given an odd number of bytes', () => {
const bn = '0x101010101010101'
const bytes = hexToBytes(bn)
expect(bytes).toEqual(new Uint8Array([1, 1, 1, 1, 1, 1, 1, 1]))
})
})
18 changes: 14 additions & 4 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,18 @@ export function bytesToMultibase(b: Uint8Array, base: keyof typeof bases): strin
return bases[base].encode(b)
}

export function hexToBytes(s: string): Uint8Array {
const input = s.startsWith('0x') ? s.substring(2) : s
export function hexToBytes(s: string, minLength?: number): Uint8Array {
let input = s.startsWith('0x') ? s.substring(2) : s

if (input.length % 2 !== 0) {
input = `0${input}`
}

if (minLength) {
const paddedLength = Math.max(input.length, minLength * 2)
input = input.padStart(paddedLength, '00')
}

return u8a.fromString(input.toLowerCase(), 'base16')
}

Expand All @@ -77,8 +87,8 @@ export function bytesToBigInt(b: Uint8Array): bigint {
return BigInt(`0x` + u8a.toString(b, 'base16'))
}

export function bigintToBytes(n: bigint): Uint8Array {
return u8a.fromString(n.toString(16), 'base16')
export function bigintToBytes(n: bigint, minLength?: number): Uint8Array {
return hexToBytes(n.toString(16), minLength)
}

export function stringToBytes(s: string): Uint8Array {
Expand Down

0 comments on commit 077dc9b

Please sign in to comment.