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

Commit

Permalink
Merge pull request #562 from LiskHQ/561-support_multiple_node_version
Browse files Browse the repository at this point in the history
Add multiple node.js version support - Closes #561
  • Loading branch information
Tobias Schwarz authored Mar 1, 2018
2 parents d16a771 + 01f257b commit 794a7d4
Show file tree
Hide file tree
Showing 11 changed files with 1,621 additions and 4,028 deletions.
5,555 changes: 1,548 additions & 4,007 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
"version": "0.4.5",
"description": "JavaScript library for sending Lisk transactions from the client or server",
"main": "./dist-node/index.js",
"engines": {
"node": ">=6.3 <=9.5",
"npm": ">=3 <=5"
},
"scripts": {
"prestart": "./bin/prestart.sh",
"start": "./bin/start.sh",
Expand Down
15 changes: 14 additions & 1 deletion src/cryptography/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,20 @@ export const bufferToBigNumberString = bigNumberBuffer =>

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

export const hexToBuffer = hex => Buffer.from(hex, 'hex');
const hexRegex = /^[0-9a-f]+/i;
export const hexToBuffer = hex => {
if (typeof hex !== 'string') {
throw new TypeError('Argument must be a string.');
}
const matchedHex = (hex.match(hexRegex) || [])[0];
if (!matchedHex || matchedHex.length !== hex.length) {
throw new TypeError('Argument must be a valid hex string.');
}
if (matchedHex.length % 2 !== 0) {
throw new TypeError('Argument must have a valid length of hex string.');
}
return Buffer.from(matchedHex, 'hex');
};

export const getFirstEightBytesReversed = publicKeyBytes =>
Buffer.from(publicKeyBytes)
Expand Down
3 changes: 0 additions & 3 deletions src/cryptography/encrypt.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,6 @@ const encryptAES256GCMWithPassword = (plainText, password) => {

const getTagBuffer = tag => {
const tagBuffer = hexToBuffer(tag);
if (bufferToHex(tagBuffer) !== tag) {
throw new Error('Tag must be a hex string.');
}
if (tagBuffer.length !== 16) {
throw new Error('Tag must be 16 bytes.');
}
Expand Down
12 changes: 6 additions & 6 deletions src/transactions/utils/getTransactionBytes.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export const getAssetDataForRegisterSecondSignatureTransaction = ({
}) => {
checkRequiredFields(['publicKey'], signature);
const { publicKey } = signature;
return Buffer.from(publicKey, 'hex');
return cryptography.hexToBuffer(publicKey);
};

export const getAssetDataForRegisterDelegateTransaction = ({ delegate }) => {
Expand Down Expand Up @@ -159,12 +159,12 @@ const getTransactionBytes = transaction => {
BYTESIZES.TIMESTAMP,
);

const transactionSenderPublicKey = Buffer.from(
const transactionSenderPublicKey = cryptography.hexToBuffer(
transaction.senderPublicKey,
'hex',
);

const transactionRequesterPublicKey = transaction.requesterPublicKey
? Buffer.from(transaction.requesterPublicKey, 'hex')
? cryptography.hexToBuffer(transaction.requesterPublicKey)
: Buffer.alloc(0);

const transactionRecipientID = transaction.recipientId
Expand All @@ -182,11 +182,11 @@ const getTransactionBytes = transaction => {
const transactionAssetData = getAssetBytes(transaction);

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

const transactionSecondSignature = transaction.signSignature
? Buffer.from(transaction.signSignature, 'hex')
? cryptography.hexToBuffer(transaction.signSignature)
: Buffer.alloc(0);

return Buffer.concat([
Expand Down
6 changes: 1 addition & 5 deletions src/transactions/utils/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,16 @@
*
*/
import bignum from 'browserify-bignum';
import { bufferToHex, hexToBuffer } from 'cryptography/convert';
import { hexToBuffer } from 'cryptography/convert';
import { MAX_ADDRESS_NUMBER } from 'constants';

export const validatePublicKey = publicKey => {
const publicKeyBuffer = hexToBuffer(publicKey);
if (bufferToHex(publicKeyBuffer) !== publicKey) {
throw new Error('Public key must be a valid hex string.');
}
if (publicKeyBuffer.length !== 32) {
throw new Error(
`Public key ${publicKey} length differs from the expected 32 bytes for a public key.`,
);
}

return true;
};

Expand Down
42 changes: 42 additions & 0 deletions test/cryptography/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,48 @@ describe('convert', () => {
const buffer = hexToBuffer(defaultHex);
return buffer.should.be.eql(defaultBuffer);
});

it('should throw TypeError with number', () => {
return hexToBuffer
.bind(null, 123)
.should.throw(TypeError, { message: 'Argument must be a string.' });
});

it('should throw TypeError with object', () => {
return hexToBuffer
.bind(null, {})
.should.throw(TypeError, { message: 'Argument must be a string.' });
});

it('should throw TypeError with non hex string', () => {
return hexToBuffer.bind(null, 'yKJj').should.throw(TypeError, {
message: 'Argument must be a valid hex string.',
});
});

it('should throw TypeError with partially correct hex string', () => {
return hexToBuffer.bind(null, 'Abxzzzz').should.throw(TypeError, {
message: 'Argument must be a valid hex string.',
});
});

it('should throw TypeError with odd number of string with partially correct hex string', () => {
return hexToBuffer.bind(null, 'Abxzzab').should.throw(TypeError, {
message: 'Argument must be a valid hex string.',
});
});

it('should throw TypeError with odd number hex string with invalid hex', () => {
return hexToBuffer.bind(null, '123xxxx').should.throw(TypeError, {
message: 'Argument must be a valid hex string.',
});
});

it('should throw TypeError with odd number of hex string', () => {
return hexToBuffer.bind(null, 'c3a5c3a4c3b6a').should.throw(TypeError, {
message: 'Argument must have a valid length of hex string.',
});
});
});

describe('#getFirstEightBytesReversed', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/cryptography/encrypt.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ describe('encrypt', () => {
)}gg`;
return decryptPassphraseWithPassword
.bind(null, cipherIvSaltTagAndVersion, defaultPassword)
.should.throw('Tag must be a hex string.');
.should.throw('Argument must be a valid hex string.');
});

it('should inform the user if the tag has been altered', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/transactions/3_castVotes.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ describe('#castVotes transaction', () => {
unvotes: unvotePublicKeys,
votes: [plusPrependedPublicKey],
})
.should.throw('Invalid hex string');
.should.throw('Argument must be a valid hex string.');
});
});

Expand Down
2 changes: 1 addition & 1 deletion test/transactions/4_registerMultisignatureAccount.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ describe('#registerMultisignatureAccount transaction', () => {
lifetime,
minimum,
})
.should.throw('Invalid hex string');
.should.throw('Argument must be a valid hex string.');
});
});

Expand Down
6 changes: 3 additions & 3 deletions test/transactions/utils/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe('public key validation', () => {
it('should throw an error', () => {
return validatePublicKey
.bind(null, invalidHexPublicKey)
.should.throw('Invalid hex string');
.should.throw('Argument must have a valid length of hex string.');
});
});

Expand All @@ -39,7 +39,7 @@ describe('public key validation', () => {
it('should throw an error', () => {
return validatePublicKey
.bind(null, invalidHexPublicKey)
.should.throw('Public key must be a valid hex string.');
.should.throw('Argument must be a valid hex string.');
});
});

Expand Down Expand Up @@ -95,7 +95,7 @@ describe('public key validation', () => {
it('should throw an error', () => {
return validatePublicKeys
.bind(null, publicKeys)
.should.throw('Invalid hex string');
.should.throw('Argument must have a valid length of hex string.');
});
});

Expand Down

0 comments on commit 794a7d4

Please sign in to comment.