Skip to content

Commit 15732f9

Browse files
e-asphyxac10ndsawali
committed
hex2buf now returns an error in case of an odd number of characters (#2580)
* hex2buf now returns an error in case of an odd number of characters * hex2buf throw tests * Update packages/taquito-utils/src/taquito-utils.ts Co-authored-by: AlirezaHaghshenas <alirezahaghshenas@gmail.com> * parse hex string digit by digit * fix: better handling of invalid hex strings * Revert "parse hex string digit by digit" This reverts commit 674f2b9. * feat: allow 0x prefix in hex * fix: fix a mistake in rebase * adjusted unit tests --------- Co-authored-by: AlirezaHaghshenas <alirezahaghshenas@gmail.com> Co-authored-by: Davis Sawali <davis.sawali@ecadlabs.com>
1 parent 8ac541f commit 15732f9

File tree

3 files changed

+66
-10
lines changed

3 files changed

+66
-10
lines changed

packages/taquito-signer/test/taquito-signer.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ describe('inmemory-signer', () => {
9898
'edskS3DtVSbWbPD1yviMGebjYwWJtruMjDcfAZsH9uba22EzKeYhmQkkraFosFETmEMfFNVcDYQ5QbFerj9ozDKroXZ6mb5oxV'
9999
);
100100

101-
expect((await signer.sign('123', new Uint8Array([3]))).sig).toEqual(
102-
'signvMhyzCmfN6JCYnqbtLCHdReCqwQM9viGJm1QPsiTrLGhrMi1eEmAsoXVjfNB1cJwnP9rj6i3cVCZeucqkPcsDuKmT9me'
101+
expect((await signer.sign('1234', new Uint8Array([3]))).sig).toEqual(
102+
'sigeeho3jK4MZKsyqcTc9mjhtz9w6enG3AFniufgUXCuXFW7VjShxLoNmxqkQSRYUwP1LHRMere5LrvxcqLgU9KmDGN356Yz'
103103
);
104104
done();
105105
});

packages/taquito-utils/src/taquito-utils.ts

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -197,12 +197,26 @@ export function encodeKeyHash(value: string) {
197197
* @throws {@link ValueConversionError}
198198
*/
199199
export const hex2buf = (hex: string): Uint8Array => {
200-
const match = hex.match(/[\da-f]{2}/gi);
201-
if (match) {
202-
return new Uint8Array(match.map((h) => parseInt(h, 16)));
203-
} else {
204-
throw new ValueConversionError(hex, 'Uint8Array');
200+
if (hex.length % 2 !== 0) {
201+
throw new InvalidHexStringError(hex, `: Expecting even number of characters`);
202+
}
203+
const hexDigits = stripHexPrefix(hex);
204+
if (!hexDigits.match(/^([\da-f]{2})*$/gi)) {
205+
throw new InvalidHexStringError(
206+
hex,
207+
`: Only characters 0-9, a-f and A-F are expected. Optionally, it can be prefixed with '0x'`
208+
);
205209
}
210+
const out = new Uint8Array(hexDigits.length / 2);
211+
let j = 0;
212+
for (let i = 0; i < hexDigits.length; i += 2) {
213+
const v = parseInt(hexDigits.slice(i, i + 2), 16);
214+
if (Number.isNaN(v)) {
215+
throw new ValueConversionError(hex, 'Uint8Array');
216+
}
217+
out[j++] = v;
218+
}
219+
return out;
206220
};
207221

208222
/**
@@ -351,10 +365,14 @@ export function bytes2Char(hex: string): string {
351365
* @param hex String value to convert to bytes
352366
*/
353367
export function hex2Bytes(hex: string): Buffer {
354-
if (!hex.match(/[\da-f]{2}/gi)) {
355-
throw new InvalidHexStringError(hex, `: Expecting even number of characters`);
368+
const hexDigits = stripHexPrefix(hex);
369+
if (!hexDigits.match(/^(0x)?([\da-f]{2})*$/gi)) {
370+
throw new InvalidHexStringError(
371+
hex,
372+
`: Expecting even number of characters: 0-9, a-z, A-Z, optionally prefixed with 0x`
373+
);
356374
}
357-
return Buffer.from(hex, 'hex');
375+
return Buffer.from(hexDigits, 'hex');
358376
}
359377

360378
/**

packages/taquito-utils/test/taquito-utils.spec.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
hex2Bytes,
1616
b58decodeL2Address,
1717
encodeL2Address,
18+
hex2buf,
1819
} from '../src/taquito-utils';
1920
import BigNumber from 'bignumber.js';
2021

@@ -251,6 +252,43 @@ describe('Hex conversions', () => {
251252
expect(result).toEqual(Buffer.from('abcd', 'hex'));
252253
});
253254

255+
it('Should be able to convert hex with 0x prefix to bytes', () => {
256+
const result: Buffer = hex2Bytes('0xabcd');
257+
258+
expect(result).toBeDefined();
259+
expect(result).toEqual(Buffer.from('abcd', 'hex'));
260+
});
261+
262+
it('Should throw an exception because of an odd number of characters', () => {
263+
expect(() => hex2Bytes('abcda')).toThrow();
264+
});
265+
266+
it('Should throw an exception because of invalid character', () => {
267+
expect(() => hex2Bytes('abcq')).toThrow();
268+
});
269+
270+
it('Should be able to convert hex to buffer', () => {
271+
const result: Uint8Array = hex2buf('412D74657374');
272+
273+
expect(result).toBeDefined();
274+
expect(result).toEqual(Uint8Array.from([65, 45, 116, 101, 115, 116]));
275+
});
276+
277+
it('Should be able to convert hex with 0x prefix to buffer', () => {
278+
const result: Uint8Array = hex2buf('0x412D74657374');
279+
280+
expect(result).toBeDefined();
281+
expect(result).toEqual(Uint8Array.from([65, 45, 116, 101, 115, 116]));
282+
});
283+
284+
it('Should throw an exception because of an odd number of characters (hex2buf)', () => {
285+
expect(() => hex2buf('abcda')).toThrow();
286+
});
287+
288+
it('Should throw an exception because of invalid character (hex2buf)', () => {
289+
expect(() => hex2buf('abcq')).toThrow();
290+
});
291+
254292
it('should be able to get phk from tz4 Public key', () => {
255293
const publicKey =
256294
'BLpk1w1wkESXN91Ry39ZMRAhaaHJsDaMZ8wBax5QsKPEKPWTjDBk6dgKMDkoejxxPWJf52cm2osh';

0 commit comments

Comments
 (0)