Skip to content

Commit

Permalink
hex2buf now returns an error in case of an odd number of characters (#…
Browse files Browse the repository at this point in the history
…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 <[email protected]>

* 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 <[email protected]>
Co-authored-by: Davis Sawali <[email protected]>
  • Loading branch information
3 people committed Jul 26, 2023
1 parent 8ac541f commit 15732f9
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 10 deletions.
4 changes: 2 additions & 2 deletions packages/taquito-signer/test/taquito-signer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ describe('inmemory-signer', () => {
'edskS3DtVSbWbPD1yviMGebjYwWJtruMjDcfAZsH9uba22EzKeYhmQkkraFosFETmEMfFNVcDYQ5QbFerj9ozDKroXZ6mb5oxV'
);

expect((await signer.sign('123', new Uint8Array([3]))).sig).toEqual(
'signvMhyzCmfN6JCYnqbtLCHdReCqwQM9viGJm1QPsiTrLGhrMi1eEmAsoXVjfNB1cJwnP9rj6i3cVCZeucqkPcsDuKmT9me'
expect((await signer.sign('1234', new Uint8Array([3]))).sig).toEqual(
'sigeeho3jK4MZKsyqcTc9mjhtz9w6enG3AFniufgUXCuXFW7VjShxLoNmxqkQSRYUwP1LHRMere5LrvxcqLgU9KmDGN356Yz'
);
done();
});
Expand Down
34 changes: 26 additions & 8 deletions packages/taquito-utils/src/taquito-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,12 +197,26 @@ export function encodeKeyHash(value: string) {
* @throws {@link ValueConversionError}
*/
export const hex2buf = (hex: string): Uint8Array => {
const match = hex.match(/[\da-f]{2}/gi);
if (match) {
return new Uint8Array(match.map((h) => parseInt(h, 16)));
} else {
throw new ValueConversionError(hex, 'Uint8Array');
if (hex.length % 2 !== 0) {
throw new InvalidHexStringError(hex, `: Expecting even number of characters`);
}
const hexDigits = stripHexPrefix(hex);
if (!hexDigits.match(/^([\da-f]{2})*$/gi)) {
throw new InvalidHexStringError(
hex,
`: Only characters 0-9, a-f and A-F are expected. Optionally, it can be prefixed with '0x'`
);
}
const out = new Uint8Array(hexDigits.length / 2);
let j = 0;
for (let i = 0; i < hexDigits.length; i += 2) {
const v = parseInt(hexDigits.slice(i, i + 2), 16);
if (Number.isNaN(v)) {
throw new ValueConversionError(hex, 'Uint8Array');
}
out[j++] = v;
}
return out;
};

/**
Expand Down Expand Up @@ -351,10 +365,14 @@ export function bytes2Char(hex: string): string {
* @param hex String value to convert to bytes
*/
export function hex2Bytes(hex: string): Buffer {
if (!hex.match(/[\da-f]{2}/gi)) {
throw new InvalidHexStringError(hex, `: Expecting even number of characters`);
const hexDigits = stripHexPrefix(hex);
if (!hexDigits.match(/^(0x)?([\da-f]{2})*$/gi)) {
throw new InvalidHexStringError(
hex,
`: Expecting even number of characters: 0-9, a-z, A-Z, optionally prefixed with 0x`
);
}
return Buffer.from(hex, 'hex');
return Buffer.from(hexDigits, 'hex');
}

/**
Expand Down
38 changes: 38 additions & 0 deletions packages/taquito-utils/test/taquito-utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
hex2Bytes,
b58decodeL2Address,
encodeL2Address,
hex2buf,
} from '../src/taquito-utils';
import BigNumber from 'bignumber.js';

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

it('Should be able to convert hex with 0x prefix to bytes', () => {
const result: Buffer = hex2Bytes('0xabcd');

expect(result).toBeDefined();
expect(result).toEqual(Buffer.from('abcd', 'hex'));
});

it('Should throw an exception because of an odd number of characters', () => {
expect(() => hex2Bytes('abcda')).toThrow();
});

it('Should throw an exception because of invalid character', () => {
expect(() => hex2Bytes('abcq')).toThrow();
});

it('Should be able to convert hex to buffer', () => {
const result: Uint8Array = hex2buf('412D74657374');

expect(result).toBeDefined();
expect(result).toEqual(Uint8Array.from([65, 45, 116, 101, 115, 116]));
});

it('Should be able to convert hex with 0x prefix to buffer', () => {
const result: Uint8Array = hex2buf('0x412D74657374');

expect(result).toBeDefined();
expect(result).toEqual(Uint8Array.from([65, 45, 116, 101, 115, 116]));
});

it('Should throw an exception because of an odd number of characters (hex2buf)', () => {
expect(() => hex2buf('abcda')).toThrow();
});

it('Should throw an exception because of invalid character (hex2buf)', () => {
expect(() => hex2buf('abcq')).toThrow();
});

it('should be able to get phk from tz4 Public key', () => {
const publicKey =
'BLpk1w1wkESXN91Ry39ZMRAhaaHJsDaMZ8wBax5QsKPEKPWTjDBk6dgKMDkoejxxPWJf52cm2osh';
Expand Down

0 comments on commit 15732f9

Please sign in to comment.