Skip to content

Commit

Permalink
refactor(crypto): split the Crypto class into Hash and Transaction (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
faustbrian authored Apr 18, 2019
1 parent 4d39748 commit 43c4129
Show file tree
Hide file tree
Showing 41 changed files with 422 additions and 654 deletions.
15 changes: 6 additions & 9 deletions __tests__/integration/core-api/repositories/transactions.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import "jest-extended";
import "../../../utils";

import { Crypto } from "@arkecosystem/crypto";
// noinspection TypeScriptPreferShortImport
import { TransactionsRepository } from "../../../../packages/core-api/src/repositories/transactions";
import { Address } from "../../../../packages/crypto/src/identities";
import { genesisBlock } from "../../../utils/config/testnet/genesisBlock";
import { setUp, tearDown } from "../__support__/setup";

const { crypto } = Crypto;

let repository;
let genesisTransaction;

Expand Down Expand Up @@ -84,7 +81,7 @@ describe("Transaction Repository", () => {
describe("`senderId`", () => {
it("should search transactions by the specified `senderId`", async () => {
const senderPublicKey = genesisTransaction.senderPublicKey;
const senderId = crypto.getAddress(senderPublicKey, 23);
const senderId = Address.fromPublicKey(senderPublicKey, 23);

const transactions = await repository.search({ senderId });

Expand Down Expand Up @@ -135,7 +132,7 @@ describe("Transaction Repository", () => {

describe("when searching by `senderId` and `recipientId`", () => {
it("should search transactions by sent by `senderId` to `recipientId`", async () => {
const senderId = crypto.getAddress(genesisTransaction.senderPublicKey, 23);
const senderId = Address.fromPublicKey(genesisTransaction.senderPublicKey, 23);
const recipientId = genesisBlock.transactions[2].recipientId;

let transactions = await repository.search({
Expand Down Expand Up @@ -176,7 +173,7 @@ describe("Transaction Repository", () => {

describe("when searching by `addresses` and `senderId`", () => {
it("should search transactions by the `addresses`, but only include those received from `senderId`", async () => {
const senderId = crypto.getAddress(genesisTransaction.senderPublicKey, 23);
const senderId = Address.fromPublicKey(genesisTransaction.senderPublicKey, 23);

let transactions = await repository.search({
senderId,
Expand Down Expand Up @@ -224,7 +221,7 @@ describe("Transaction Repository", () => {

describe("when searching by `addresses` and `recipientId`", () => {
it("should search transactions by the `addresses`, but only include those sent to `recipientId`", async () => {
const senderId = crypto.getAddress(genesisTransaction.senderPublicKey, 23);
const senderId = Address.fromPublicKey(genesisTransaction.senderPublicKey, 23);
const recipientId = genesisBlock.transactions[2].recipientId;

let transactions = await repository.search({
Expand All @@ -249,7 +246,7 @@ describe("Transaction Repository", () => {

describe("when searching by `addresses`, `senderId` and `recipientId`", () => {
it("should search transactions by `senderId` and `recipientId` only", async () => {
const senderId = crypto.getAddress(genesisTransaction.senderPublicKey, 23);
const senderId = Address.fromPublicKey(genesisTransaction.senderPublicKey, 23);
const params = {
senderId,
recipientId: genesisTransaction.recipientId,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import "../../../../utils";

import { Crypto } from "@arkecosystem/crypto";
import { Address } from "@arkecosystem/crypto/dist/identities";
import { genesisBlock } from "../../../../utils/config/testnet/genesisBlock";
import { setUp, tearDown } from "../../__support__/setup";
import { utils } from "../utils";

const { crypto } = Crypto;

const address1 = "APnhwwyTbMiykJwYbGhYjNgtHiVJDSEhSn";
const address2 = "AHXtmB84sTZ9Zd35h9Y1vfFvPE2Xzqj8ri";

Expand Down Expand Up @@ -202,7 +200,7 @@ describe("API 1.0 - Transactions", () => {
expect(response.data.transaction).toHaveProperty("recipientId", transactionInCheck.recipientId);
expect(response.data.transaction).toHaveProperty(
"senderId",
crypto.getAddress(transactionInCheck.senderPublicKey, 23),
Address.fromPublicKey(transactionInCheck.senderPublicKey, 23),
);
expect(response.data.transaction).toHaveProperty("type", transactionInCheck.type);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@ import "../../../../utils";
import { setUp, tearDown } from "../../__support__/setup";
import { utils } from "../utils";

import { Crypto } from "@arkecosystem/crypto";
import { Identities } from "@arkecosystem/crypto";
import { TransactionFactory } from "../../../../helpers/transaction-factory";
import { genesisBlock } from "../../../../utils/config/testnet/genesisBlock";
import { delegates } from "../../../../utils/fixtures/testnet/delegates";
import { generateWallets } from "../../../../utils/generators/wallets";

const { crypto } = Crypto;

const transferFee = 10000000;

let genesisTransaction;
Expand Down Expand Up @@ -45,7 +43,7 @@ beforeAll(async () => {
wrongType = 3;
version = 1;
senderPublicKey = genesisTransaction.senderPublicKey;
senderAddress = crypto.getAddress(genesisTransaction.senderPublicKey, 23);
senderAddress = Identities.Address.fromPublicKey(genesisTransaction.senderPublicKey, 23);
recipientAddress = genesisTransaction.recipientId;
timestamp = genesisTransaction.timestamp;
timestampFrom = timestamp;
Expand Down
4 changes: 2 additions & 2 deletions __tests__/integration/core-blockchain/blockchain.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ describe("Blockchain", () => {

// Create key pair for new voter
const keyPair = Identities.Keys.fromPassphrase("secret");
const recipient = Crypto.crypto.getAddress(keyPair.publicKey);
const recipient = Identities.Address.fromPublicKey(keyPair.publicKey);

let nextForger = await getNextForger();
const initialVoteBalance = nextForger.voteBalance;
Expand Down Expand Up @@ -307,7 +307,7 @@ async function __resetToHeight1() {
await blockchain.database.buildWallets();

// Index the genesis wallet or else revert block at height 1 fails
const generator = Crypto.crypto.getAddress(genesisBlock.data.generatorPublicKey);
const generator = Identities.Address.fromPublicKey(genesisBlock.data.generatorPublicKey);
const genesis = new Wallet(generator);
genesis.publicKey = genesisBlock.data.generatorPublicKey;
genesis.username = "genesis";
Expand Down
10 changes: 5 additions & 5 deletions __tests__/integration/core-json-rpc/transactions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import "jest-extended";

import { app } from "@arkecosystem/core-container";
import { Peer } from "@arkecosystem/core-p2p";
import { Crypto, Transactions } from "@arkecosystem/crypto";
import { Transactions } from "@arkecosystem/crypto";
import { randomBytes } from "crypto";
import nock from "nock";
import { sendRequest } from "./__support__/request";
Expand Down Expand Up @@ -35,7 +35,7 @@ afterEach(async () => {
});

function verifyTransaction(data): boolean {
return Crypto.crypto.verify(Transactions.TransactionFactory.fromData(data).data);
return Transactions.TransactionFactory.fromData(data).verify();
}

describe("Transactions", () => {
Expand Down Expand Up @@ -96,7 +96,7 @@ describe("Transactions", () => {
});

it("should return 422 if it fails to verify the transaction", async () => {
const spyVerify = jest.spyOn(Crypto.crypto, "verify").mockImplementation(() => false);
const spyVerify = jest.spyOn(Transactions.Transaction, "verifyData").mockImplementation(() => false);

const response = await sendRequest("transactions.create", {
amount: 100000000,
Expand Down Expand Up @@ -146,7 +146,7 @@ describe("Transactions", () => {

mockHost.post("/api/transactions").reply(200, {}, peerMock.headers);

const spyVerify = jest.spyOn(Crypto.crypto, "verify").mockImplementation(() => false);
const spyVerify = jest.spyOn(Transactions.Transaction, "verifyData").mockImplementation(() => false);

const response = await sendRequest("transactions.broadcast", {
id: transaction.body.result.id,
Expand Down Expand Up @@ -204,7 +204,7 @@ describe("Transactions", () => {
});

it("should return 422 if it fails to verify the transaction", async () => {
const spyVerify = jest.spyOn(Crypto.crypto, "verify").mockImplementation(() => false);
const spyVerify = jest.spyOn(Transactions.Transaction, "verifyData").mockImplementation(() => false);

const response = await sendRequest("transactions.bip38.create", {
bip38: "this is a top secret passphrase",
Expand Down
15 changes: 6 additions & 9 deletions __tests__/integration/core-transaction-pool/guard.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@ import "jest-extended";

import { Container } from "@arkecosystem/core-interfaces";
import { TransactionHandlerRegistry } from "@arkecosystem/core-transactions";
import { Blocks, Crypto, Identities, Interfaces, Utils } from "@arkecosystem/crypto";
import { Blocks, Identities, Interfaces, Utils } from "@arkecosystem/crypto";
import { generateMnemonic } from "bip39";
import { TransactionFactory } from "../../helpers/transaction-factory";
import { delegates, genesisBlock, wallets, wallets2ndSig } from "../../utils/fixtures/unitnet";
import { generateWallets } from "../../utils/generators/wallets";
import { setUpFull, tearDownFull } from "./__support__/setup";

const { BlockFactory } = Blocks;
const { crypto } = Crypto;

let TransactionGuard;

let container: Container.IContainer;
Expand Down Expand Up @@ -109,7 +106,7 @@ describe("Transaction Guard", () => {
it("should not apply the tx to the balance of the sender & recipient with dyn fee < min fee", async () => {
const delegate0 = delegates[14];
const { publicKey } = Identities.Keys.fromPassphrase(generateMnemonic());
const newAddress = crypto.getAddress(publicKey);
const newAddress = Identities.Address.fromPublicKey(publicKey);

const delegateWallet = transactionPool.walletManager.findByPublicKey(delegate0.publicKey);
const newWallet = transactionPool.walletManager.findByPublicKey(publicKey);
Expand All @@ -134,7 +131,7 @@ describe("Transaction Guard", () => {
it("should update the balance of the sender & recipient with dyn fee > min fee", async () => {
const delegate1 = delegates[1];
const { publicKey } = Identities.Keys.fromPassphrase(generateMnemonic());
const newAddress = crypto.getAddress(publicKey);
const newAddress = Identities.Address.fromPublicKey(publicKey);

const delegateWallet = transactionPool.walletManager.findByPublicKey(delegate1.publicKey);
const newWallet = transactionPool.walletManager.findByPublicKey(publicKey);
Expand Down Expand Up @@ -164,7 +161,7 @@ describe("Transaction Guard", () => {
const delegate2 = delegates[2];
const newWalletPassphrase = generateMnemonic();
const { publicKey } = Identities.Keys.fromPassphrase(newWalletPassphrase);
const newAddress = crypto.getAddress(publicKey);
const newAddress = Identities.Address.fromPublicKey(publicKey);

const delegateWallet = transactionPool.walletManager.findByPublicKey(delegate2.publicKey);
const newWallet = transactionPool.walletManager.findByPublicKey(publicKey);
Expand Down Expand Up @@ -227,7 +224,7 @@ describe("Transaction Guard", () => {
const delegate3 = delegates[3];
const newWalletPassphrase = generateMnemonic();
const { publicKey } = Identities.Keys.fromPassphrase(newWalletPassphrase);
const newAddress = crypto.getAddress(publicKey);
const newAddress = Identities.Address.fromPublicKey(publicKey);

const delegateWallet = transactionPool.walletManager.findByPublicKey(delegate3.publicKey);
const newWallet = transactionPool.walletManager.findByPublicKey(publicKey);
Expand Down Expand Up @@ -674,7 +671,7 @@ describe("Transaction Guard", () => {
"3045022100e7385c6ea42bd950f7f6ab8c8619cf2f66a41d8f8f185b0bc99af032cb25f30d02200b6210176a6cedfdcbe483167fd91c21d740e0e4011d24d679c601fdd46b0de9",
createdAt: "2019-07-11T16:48:50.550Z",
};
const blockVerified = BlockFactory.fromData(block);
const blockVerified = Blocks.BlockFactory.fromData(block);
blockVerified.verification.verified = true;

await blockchain.processBlock(blockVerified, () => null);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import { Blockchain, Container, Database } from "@arkecosystem/core-interfaces";
import { TransactionHandlerRegistry } from "@arkecosystem/core-transactions";
import { Blocks, Crypto, Identities, Utils } from "@arkecosystem/crypto";
import { Blocks, Identities, Utils } from "@arkecosystem/crypto";
import { generateMnemonic } from "bip39";
import { TransactionFactory } from "../../helpers/transaction-factory";
import { delegates, genesisBlock, wallets } from "../../utils/fixtures/unitnet";
import { generateWallets } from "../../utils/generators/wallets";
import { setUpFull, tearDownFull } from "./__support__/setup";

const { BlockFactory } = Blocks;
const { crypto } = Crypto;

const satoshi = 1e8;
let container: Container.IContainer;
let PoolWalletManager;
Expand Down Expand Up @@ -61,7 +58,7 @@ describe("applyPoolTransactionToSender", () => {
it("should only update the balance of the sender", async () => {
const delegate0 = delegates[0];
const { publicKey } = Identities.Keys.fromPassphrase(generateMnemonic());
const newAddress = crypto.getAddress(publicKey);
const newAddress = Identities.Address.fromPublicKey(publicKey);

const delegateWallet = poolWalletManager.findByAddress(delegate0.address);
const newWallet = poolWalletManager.findByAddress(newAddress);
Expand All @@ -85,7 +82,7 @@ describe("applyPoolTransactionToSender", () => {
it("should only update the balance of the sender with dyn fees", async () => {
const delegate0 = delegates[1];
const { publicKey } = Identities.Keys.fromPassphrase(generateMnemonic());
const newAddress = crypto.getAddress(publicKey);
const newAddress = Identities.Address.fromPublicKey(publicKey);

const delegateWallet = poolWalletManager.findByAddress(delegate0.address);
const newWallet = poolWalletManager.findByAddress(newAddress);
Expand Down Expand Up @@ -208,7 +205,7 @@ describe("Apply transactions and block rewards to wallets on new block", () => {
"3045022100e7385c6ea42bd950f7f6ab8c8619cf2f66a41d8f8f185b0bc99af032cb25f30d02200b6210176a6cedfdcbe483167fd91c21d740e0e4011d24d679c601fdd46b0de9",
createdAt: "2019-07-11T16:48:50.550Z",
};
const blockWithRewardVerified = BlockFactory.fromData(blockWithReward);
const blockWithRewardVerified = Blocks.BlockFactory.fromData(blockWithReward);
blockWithRewardVerified.verification.verified = true;

await blockchain.processBlock(blockWithRewardVerified, () => null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import "../mocks/core-container";

import { Database } from "@arkecosystem/core-interfaces";
import { delegateCalculator } from "@arkecosystem/core-utils";
import { Crypto, Utils } from "@arkecosystem/crypto";
import { Utils } from "@arkecosystem/crypto";
import { DelegatesBusinessRepository, Wallet, WalletsBusinessRepository } from "../../../../packages/core-database/src";
import { DatabaseService } from "../../../../packages/core-database/src/database-service";
import { Address } from "../../../../packages/crypto/src/identities";
import { genesisBlock } from "../../../utils/fixtures/testnet/block-model";

let repository;
Expand All @@ -26,7 +27,8 @@ beforeEach(async () => {

function generateWallets(): Wallet[] {
return genesisBlock.transactions.map((transaction, index) => {
const address = Crypto.crypto.getAddress(transaction.data.senderPublicKey);
// @TODO: switch to unitnet
const address: string = Address.fromPublicKey(transaction.data.senderPublicKey, 23);

return {
address,
Expand All @@ -52,17 +54,17 @@ describe("Delegate Repository", () => {

it("should return the local wallets of the connection that are delegates", () => {
// @ts-ignore
jest.spyOn(walletManager, "allByAddress").mockReturnValue(wallets);
jest.spyOn(walletManager, "allByUsername").mockReturnValue(wallets);

const actualDelegates = repository.getLocalDelegates();

expect(actualDelegates).toEqual(expect.arrayContaining(delegates));
expect(walletManager.allByAddress).toHaveBeenCalled();
expect(walletManager.allByUsername).toHaveBeenCalled();
});

it("should be ok with params (forgedTotal)", () => {
// @ts-ignore
jest.spyOn(walletManager, "allByAddress").mockReturnValue(wallets);
jest.spyOn(walletManager, "allByUsername").mockReturnValue(wallets);

const actualDelegates = repository.getLocalDelegates({ forgedTotal: null });

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import "../mocks/core-container";

import { Database } from "@arkecosystem/core-interfaces";
import { Crypto, Utils } from "@arkecosystem/crypto";
import { Utils } from "@arkecosystem/crypto";
import compact from "lodash.compact";
import uniq from "lodash.uniq";
import { genesisBlock } from "../../../utils/fixtures/testnet/block-model";

import { Wallet, WalletsBusinessRepository } from "../../../../packages/core-database/src";
import { DatabaseService } from "../../../../packages/core-database/src/database-service";
import { Address } from "../../../../packages/crypto/src/identities";

let genesisSenders;
let repository;
Expand All @@ -29,21 +30,21 @@ beforeEach(async () => {

function generateWallets() {
return genesisSenders.map((senderPublicKey, index) => ({
address: Crypto.crypto.getAddress(senderPublicKey),
address: Address.fromPublicKey(senderPublicKey),
balance: Utils.BigNumber.make(index),
}));
}

function generateVotes() {
return genesisSenders.map(senderPublicKey => ({
address: Crypto.crypto.getAddress(senderPublicKey),
address: Address.fromPublicKey(senderPublicKey),
vote: genesisBlock.transactions[0].data.senderPublicKey,
}));
}

function generateFullWallets() {
return genesisSenders.map(senderPublicKey => {
const address = Crypto.crypto.getAddress(senderPublicKey);
const address = Address.fromPublicKey(senderPublicKey);

return {
address,
Expand Down
Loading

0 comments on commit 43c4129

Please sign in to comment.