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

Commit

Permalink
Merge pull request #3810 from LiskHQ/3808-replace-crypto-lib
Browse files Browse the repository at this point in the history
Replace crypto with @liskhq/lisk-cryptography - Closes #3808
  • Loading branch information
shuse2 authored Jun 13, 2019
2 parents cf92717 + 825de3a commit f8b5d48
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
16 changes: 9 additions & 7 deletions framework/src/modules/chain/blocks/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ const {
verifyData,
} = require('@liskhq/lisk-cryptography');
const _ = require('lodash');
const crypto = require('crypto');
const ByteBuffer = require('bytebuffer');
const BigNum = require('@liskhq/bignum');
const validator = require('../../../controller/validator');
Expand Down Expand Up @@ -210,31 +209,34 @@ const create = ({
let size = 0;

const blockTransactions = [];
const payloadHash = crypto.createHash('sha256');
const transactionsBytesArray = [];

for (let i = 0; i < sortedTransactions.length; i++) {
const transaction = sortedTransactions[i];
const bytes = transaction.getBytes(transaction);
const transactionBytes = transaction.getBytes(transaction);

if (size + bytes.length > maxPayloadLength) {
if (size + transactionBytes.length > maxPayloadLength) {
break;
}

size += bytes.length;
size += transactionBytes.length;

totalFee = totalFee.plus(transaction.fee);
totalAmount = totalAmount.plus(transaction.amount);

blockTransactions.push(transaction);
payloadHash.update(bytes);
transactionsBytesArray.push(transactionBytes);
}

const transactionsBuffer = Buffer.concat(transactionsBytesArray);
const payloadHash = hash(transactionsBuffer).toString('hex');

const block = {
version: blockVersion.currentBlockVersion,
totalAmount,
totalFee,
reward,
payloadHash: payloadHash.digest().toString('hex'),
payloadHash,
timestamp,
numberOfTransactions: blockTransactions.length,
payloadLength: size,
Expand Down
17 changes: 10 additions & 7 deletions framework/src/modules/chain/blocks/verify.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@

'use strict';

const crypto = require('crypto');
const BigNum = require('@liskhq/bignum');
const { Status: TransactionStatus } = require('@liskhq/lisk-transactions');
const { hash } = require('@liskhq/lisk-cryptography');
const blockVersion = require('./block_version');
const blocksLogic = require('./block');
const blocksUtils = require('./utils');
Expand Down Expand Up @@ -454,14 +454,14 @@ const verifyPayload = (

let totalAmount = new BigNum(0);
let totalFee = new BigNum(0);
const payloadHash = crypto.createHash('sha256');
const transactionsBytesArray = [];
const appliedTransactions = {};

block.transactions.forEach(transaction => {
let bytes;
let transactionBytes;

try {
bytes = transaction.getBytes();
transactionBytes = transaction.getBytes();
} catch (e) {
result.errors.push(e.toString());
}
Expand All @@ -473,14 +473,17 @@ const verifyPayload = (
}

appliedTransactions[transaction.id] = transaction;
if (bytes) {
payloadHash.update(bytes);
if (transactionBytes) {
transactionsBytesArray.push(transactionBytes);
}
totalAmount = totalAmount.plus(transaction.amount);
totalFee = totalFee.plus(transaction.fee);
});

if (payloadHash.digest().toString('hex') !== block.payloadHash) {
const transactionsBuffer = Buffer.concat(transactionsBytesArray);
const payloadHash = hash(transactionsBuffer).toString('hex');

if (payloadHash !== block.payloadHash) {
result.errors.push(new Error('Invalid payload hash'));
}

Expand Down

0 comments on commit f8b5d48

Please sign in to comment.