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

Commit

Permalink
🐛 Fix hex buffer behavior to match node 8
Browse files Browse the repository at this point in the history
  • Loading branch information
shuse2 committed Feb 15, 2018
1 parent 7a0631e commit a1d8ed2
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 14 deletions.
8 changes: 4 additions & 4 deletions src/crypto/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,17 @@ export const bufferToBigNumberString = bigNumberBuffer =>

export const bufferToHex = buffer => naclInstance.to_hex(buffer);

const hexRegex = /([0-9]|[a-f])/gim;
const hexRegex = /^([0-9]|[a-f])+/gi;
export const hexToBuffer = hex => {
if (typeof hex !== 'string') {
return Buffer.alloc(0);
throw new TypeError('argument must be string');
}
const matchedHex = hex.match(hexRegex) || [];
if (matchedHex.length === 0) {
return Buffer.alloc(0);
}
const evenLength = Math.floor(matchedHex.length / 2) * 2;
return Buffer.from(matchedHex.slice(0, evenLength).join(''), 'hex');
const evenLength = Math.floor(matchedHex[0].length / 2) * 2;
return Buffer.from(matchedHex[0].slice(0, evenLength), 'hex');
};

export const getFirstEightBytesReversed = publicKeyBytes =>
Expand Down
15 changes: 8 additions & 7 deletions src/transactions/utils/getTransactionBytes.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,10 @@ const getTransactionBytes = transaction => {
const transactionSenderPublicKey = cryptoModule.hexToBuffer(
transaction.senderPublicKey,
);
const transactionRequesterPublicKey = cryptoModule.hexToBuffer(
transaction.requesterPublicKey,
);

const transactionRequesterPublicKey = transaction.requesterPublicKey
? cryptoModule.hexToBuffer(transaction.requesterPublicKey)
: Buffer.alloc(0);

const transactionRecipientID = transaction.recipientId
? cryptoModule.bigNumberToBuffer(
Expand All @@ -181,12 +182,12 @@ const getTransactionBytes = transaction => {
const transactionAssetData = getAssetBytes(transaction);

const transactionSignature = transaction.signature
? Buffer.from(transaction.signature, 'hex')
? cryptoModule.hexToBuffer(transaction.signature)
: Buffer.alloc(0);

const transactionSecondSignature = cryptoModule.hexToBuffer(
transaction.signSignature,
);
const transactionSecondSignature = transaction.signSignature
? cryptoModule.hexToBuffer(transaction.signSignature)
: Buffer.alloc(0);

return Buffer.concat([
transactionType,
Expand Down
16 changes: 13 additions & 3 deletions test/crypto/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,15 @@ describe('convert', () => {
const buffer = hexToBuffer(defaultHex);
return buffer.should.be.eql(defaultBuffer);
});
it('should create empty Buffer from non-string', () => {
const buffer = hexToBuffer(123);
return buffer.should.be.eql(Buffer.alloc(0));
it('should throw TypeError with number', () => {
return (() => hexToBuffer(123)).should.throw(
new TypeError('argument must be string'),
);
});
it('should throw TypeError with object', () => {
return (() => hexToBuffer({})).should.throw(
new TypeError('argument must be string'),
);
});
it('should create empty Buffer from non hex string', () => {
const buffer = hexToBuffer('yKJh');
Expand All @@ -77,6 +83,10 @@ describe('convert', () => {
const buffer = hexToBuffer('Abxzzzz');
return buffer.should.be.eql(Buffer.from('Ab', 'hex'));
});
it('should create partial Buffer with only first valid hex string', () => {
const buffer = hexToBuffer('Abxzzab');
return buffer.should.be.eql(Buffer.from('Ab', 'hex'));
});
it('should create even numbered Buffer from odd number hex string', () => {
const buffer = hexToBuffer('c3a5c3a4c3b6a');
return buffer.should.be.eql(Buffer.from('c3a5c3a4c3b6', 'hex'));
Expand Down

0 comments on commit a1d8ed2

Please sign in to comment.