Skip to content

Commit

Permalink
refactor: always use crypto identities to work with keys and addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
faustbrian committed Apr 18, 2019
1 parent 14c68a9 commit 7a8df7d
Show file tree
Hide file tree
Showing 13 changed files with 52 additions and 47 deletions.
6 changes: 3 additions & 3 deletions __tests__/integration/core-blockchain/blockchain.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import "../../utils";
/* tslint:disable:max-line-length */
import { Wallet } from "@arkecosystem/core-database";
import { roundCalculator } from "@arkecosystem/core-utils";
import { Blocks, Crypto, Interfaces, Transactions, Utils } from "@arkecosystem/crypto";
import { Blocks, Crypto, Identities, Interfaces, Transactions, Utils } from "@arkecosystem/crypto";
import delay from "delay";
import { Blockchain } from "../../../packages/core-blockchain/src/blockchain";
import { genesisBlock as GB } from "../../utils/config/testnet/genesisBlock";
Expand Down Expand Up @@ -170,14 +170,14 @@ describe("Blockchain", () => {
transactions: sortedTransactions,
};

return Blocks.BlockFactory.make(data, Crypto.crypto.getKeys(generatorKeys.secret));
return Blocks.BlockFactory.make(data, Identities.Keys.fromPassphrase(generatorKeys.secret));
};

it("should restore vote balances after a rollback", async () => {
const mockCallback = jest.fn(() => true);

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

let nextForger = await getNextForger();
Expand Down
10 changes: 5 additions & 5 deletions __tests__/integration/core-transaction-pool/guard.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import "jest-extended";

import { Container } from "@arkecosystem/core-interfaces";
import { TransactionHandlerRegistry } from "@arkecosystem/core-transactions";
import { Blocks, Crypto, Interfaces, Utils } from "@arkecosystem/crypto";
import { Blocks, Crypto, 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";
Expand Down Expand Up @@ -108,7 +108,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 } = crypto.getKeys(generateMnemonic());
const { publicKey } = Identities.Keys.fromPassphrase(generateMnemonic());
const newAddress = crypto.getAddress(publicKey);

const delegateWallet = transactionPool.walletManager.findByPublicKey(delegate0.publicKey);
Expand All @@ -133,7 +133,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 } = crypto.getKeys(generateMnemonic());
const { publicKey } = Identities.Keys.fromPassphrase(generateMnemonic());
const newAddress = crypto.getAddress(publicKey);

const delegateWallet = transactionPool.walletManager.findByPublicKey(delegate1.publicKey);
Expand Down Expand Up @@ -163,7 +163,7 @@ describe("Transaction Guard", () => {
it("should update the balance of the sender & recipient with multiple transactions type", async () => {
const delegate2 = delegates[2];
const newWalletPassphrase = generateMnemonic();
const { publicKey } = crypto.getKeys(newWalletPassphrase);
const { publicKey } = Identities.Keys.fromPassphrase(newWalletPassphrase);
const newAddress = crypto.getAddress(publicKey);

const delegateWallet = transactionPool.walletManager.findByPublicKey(delegate2.publicKey);
Expand Down Expand Up @@ -226,7 +226,7 @@ describe("Transaction Guard", () => {
it("should not accept transaction in excess", async () => {
const delegate3 = delegates[3];
const newWalletPassphrase = generateMnemonic();
const { publicKey } = crypto.getKeys(newWalletPassphrase);
const { publicKey } = Identities.Keys.fromPassphrase(newWalletPassphrase);
const newAddress = crypto.getAddress(publicKey);

const delegateWallet = transactionPool.walletManager.findByPublicKey(delegate3.publicKey);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Blockchain, Container, Database } from "@arkecosystem/core-interfaces";
import { TransactionHandlerRegistry } from "@arkecosystem/core-transactions";
import { Blocks, Crypto, Utils } from "@arkecosystem/crypto";
import { Blocks, Crypto, Identities, Utils } from "@arkecosystem/crypto";
import { generateMnemonic } from "bip39";
import { TransactionFactory } from "../../helpers/transaction-factory";
import { delegates, genesisBlock, wallets } from "../../utils/fixtures/unitnet";
Expand Down Expand Up @@ -60,7 +60,7 @@ describe("applyPoolTransactionToSender", () => {
describe("update the balance", () => {
it("should only update the balance of the sender", async () => {
const delegate0 = delegates[0];
const { publicKey } = crypto.getKeys(generateMnemonic());
const { publicKey } = Identities.Keys.fromPassphrase(generateMnemonic());
const newAddress = crypto.getAddress(publicKey);

const delegateWallet = poolWalletManager.findByAddress(delegate0.address);
Expand All @@ -84,7 +84,7 @@ describe("applyPoolTransactionToSender", () => {

it("should only update the balance of the sender with dyn fees", async () => {
const delegate0 = delegates[1];
const { publicKey } = crypto.getKeys(generateMnemonic());
const { publicKey } = Identities.Keys.fromPassphrase(generateMnemonic());
const newAddress = crypto.getAddress(publicKey);

const delegateWallet = poolWalletManager.findByAddress(delegate0.address);
Expand Down
10 changes: 5 additions & 5 deletions __tests__/unit/core-database/wallet-manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import "./mocks/core-container";

import { Database } from "@arkecosystem/core-interfaces";
import { InsufficientBalanceError } from "@arkecosystem/core-transactions/src/errors";
import { Blocks, Constants, Crypto, Enums, Interfaces, Transactions, Utils } from "@arkecosystem/crypto";
import { Blocks, Constants, Crypto, Enums, Identities, Interfaces, Transactions, Utils } from "@arkecosystem/crypto";
import { Wallet } from "../../../packages/core-database/src";
import { TransactionFactory } from "../../helpers/transaction-factory";
import { fixtures } from "../../utils";
Expand Down Expand Up @@ -264,8 +264,8 @@ describe("Wallet Manager", () => {
});

it("should revert vote transaction and correctly update vote balances", async () => {
const delegateKeys = crypto.getKeys("delegate");
const voterKeys = crypto.getKeys("secret");
const delegateKeys = Identities.Keys.fromPassphrase("delegate");
const voterKeys = Identities.Keys.fromPassphrase("secret");

const delegate = walletManager.findByPublicKey(delegateKeys.publicKey);
delegate.username = "unittest";
Expand Down Expand Up @@ -299,8 +299,8 @@ describe("Wallet Manager", () => {
});

it("should revert unvote transaction and correctly update vote balances", async () => {
const delegateKeys = crypto.getKeys("delegate");
const voterKeys = crypto.getKeys("secret");
const delegateKeys = Identities.Keys.fromPassphrase("delegate");
const voterKeys = Identities.Keys.fromPassphrase("secret");

const delegate = walletManager.findByPublicKey(delegateKeys.publicKey);
delegate.username = "unittest";
Expand Down
4 changes: 2 additions & 2 deletions __tests__/unit/core-transactions/handler-registry.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import "jest-extended";

import { Database, TransactionPool } from "@arkecosystem/core-interfaces";
import { Crypto, Enums, Interfaces, Managers, Transactions, Utils } from "@arkecosystem/crypto";
import { Crypto, Enums, Identities, Interfaces, Managers, Transactions, Utils } from "@arkecosystem/crypto";
import bs58check from "bs58check";
import ByteBuffer from "bytebuffer";
import { errors, TransactionHandler, TransactionHandlerRegistry } from "../../../packages/core-transactions/src";
Expand Down Expand Up @@ -110,7 +110,7 @@ describe("TransactionHandlerRegistry", () => {
it("should be able to instantiate a custom transaction", () => {
TransactionHandlerRegistry.registerCustomTransactionHandler(TestTransactionHandler);

const keys = crypto.getKeys("secret");
const keys = Identities.Keys.fromPassphrase("secret");
const data: Interfaces.ITransactionData = {
type: TEST_TRANSACTION_TYPE,
timestamp: slots.getTime(),
Expand Down
21 changes: 9 additions & 12 deletions __tests__/unit/crypto/crypto/message.test.ts
Original file line number Diff line number Diff line change
@@ -1,47 +1,44 @@
import "jest-extended";

import { crypto } from "../../../../packages/crypto/src/crypto";
import { Message } from "../../../../packages/crypto/src/crypto/message";
import { devnet } from "../../../../packages/crypto/src/networks";
import { identity } from "../../../utils/identities";

const passphrase = "sample passphrase";
const wif = crypto.keysToWIF(crypto.getKeys(passphrase), devnet.network);
const signedMessageEntries: any = [
["publicKey", "03bb51bbf5bf84759452e33dd97cf72cc8904be07df07a946a0d84939400f17e87"],
["publicKey", identity.publicKey],
[
"signature",
"304402204550cd28d369a7f6eccd399b315e42e054a2f21f6771983af4ed3c5f7c7fa83102200699fef72cc64e79ccba85a31666e9508c052038c71c04260264e3d2d11c7e08",
"3045022100b5ad008d8a2935cd2261c56ef1605b2e35810f47940277d1d8a6a202a08c6de0022021fcbf9ec9db67f8c7019ff2ce07376f8a203ea77f26f2f7d564d5b8f4bde1a7",
],
["message", "test"],
];

describe("Message", () => {
describe("sign", () => {
it("should sign a message", () => {
expect(Message.sign("test", passphrase)).toContainAllEntries(signedMessageEntries);
expect(Message.sign("test", identity.bip39)).toContainAllEntries(signedMessageEntries);
});
});

describe("signWithWif", () => {
it("should sign a message", () => {
expect(Message.signWithWif("test", wif)).toContainAllEntries(signedMessageEntries);
expect(Message.signWithWif("test", identity.wif)).toContainAllEntries(signedMessageEntries);
});

it("should sign a message and match passphrase", () => {
const signedMessage = Message.sign("test", passphrase);
const signedWifMessage = Message.signWithWif("test", wif);
const signedMessage = Message.sign("test", identity.bip39);
const signedWifMessage = Message.signWithWif("test", identity.wif);
expect(signedMessage).toEqual(signedWifMessage);
});
});

describe("verify", () => {
it("should verify a signed message", () => {
const signedMessage = Message.sign("test", passphrase);
const signedMessage = Message.sign("test", identity.bip39);
expect(Message.verify(signedMessage)).toBe(true);
});

it("should verify a signed wif message", () => {
const signedMessage = Message.signWithWif("test", wif);
const signedMessage = Message.signWithWif("test", identity.wif);
expect(Message.verify(signedMessage)).toBe(true);
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,13 @@ export const transactionBuilder = <T extends TransactionBuilder<T>>(provider: ()
});

it("establishes the public key of the sender", () => {
const spySign = jest.spyOn(crypto, "sign").mockImplementationOnce(jest.fn());

const builder = provider();
builder.sign(identity.bip39);

expect(builder.data.senderPublicKey).toBe(identity.keys.publicKey);
expect(spySign).toHaveBeenCalledWith((builder as any).getSigningObject(), identity.keys);
});
});

Expand All @@ -149,10 +152,13 @@ export const transactionBuilder = <T extends TransactionBuilder<T>>(provider: ()
});

it("establishes the public key of the sender", () => {
const spySign = jest.spyOn(crypto, "sign").mockImplementationOnce(jest.fn());

const builder = provider();
builder.signWithWif(identity.wif);

expect(builder.data.senderPublicKey).toBe(identity.publicKey);
expect(spySign).toHaveBeenCalledWith((builder as any).getSigningObject(), identity.keys);
});
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import "jest-extended";

import { Utils } from "@arkecosystem/crypto";
import { crypto } from "../../../../../../packages/crypto/src/crypto";
import { TransactionTypes } from "../../../../../../packages/crypto/src/enums";
import { Keys, WIF } from "../../../../../../packages/crypto/src/identities";
import { feeManager } from "../../../../../../packages/crypto/src/managers/fee";
import { devnet } from "../../../../../../packages/crypto/src/networks";
import { BuilderFactory } from "../../../../../../packages/crypto/src/transactions";
Expand Down Expand Up @@ -45,8 +45,8 @@ describe("Transfer Transaction", () => {
it("should sign a transaction and match signed with a passphrase", () => {
const passphrase = "sample passphrase";
const network = 23;
const keys = crypto.getKeys(passphrase);
const wif = crypto.keysToWIF(keys, devnet.network);
const keys = Keys.fromPassphrase(passphrase);
const wif = WIF.fromKeys(keys, devnet.network);

const wifTransaction = builder
.amount("10")
Expand All @@ -68,8 +68,8 @@ describe("Transfer Transaction", () => {
const passphrase = "first passphrase";
const secondPassphrase = "second passphrase";
const network = 23;
const keys = crypto.getKeys(secondPassphrase);
const wif = crypto.keysToWIF(keys, devnet.network);
const keys = Keys.fromPassphrase(secondPassphrase);
const wif = WIF.fromKeys(keys, devnet.network);

const wifTransaction = builder
.amount("10")
Expand Down
4 changes: 2 additions & 2 deletions __tests__/unit/crypto/transactions/schemas.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Utils } from "@arkecosystem/crypto";
import { ARKTOSHI } from "../../../../packages/crypto/src/constants";
import { crypto } from "../../../../packages/crypto/src/crypto";
import { TransactionTypes } from "../../../../packages/crypto/src/enums";
import { PublicKey } from "../../../../packages/crypto/src/identities";
import { configManager } from "../../../../packages/crypto/src/managers";
import { BuilderFactory } from "../../../../packages/crypto/src/transactions";
import { TransactionRegistry } from "../../../../packages/crypto/src/transactions";
Expand Down Expand Up @@ -620,7 +620,7 @@ describe.skip("Multi Signature Transaction", () => {
for (let i = 0; i < 20; i++) {
const value = `passphrase ${i}`;
values.push(value);
multiSignatureAsset.keysgroup.push(crypto.getKeys(value).publicKey);
multiSignatureAsset.keysgroup.push(PublicKey.fromPassphrase(value));
}
transaction.multiSignatureAsset(multiSignatureAsset).sign("passphrase");
signTransaction(transaction, values);
Expand Down
3 changes: 2 additions & 1 deletion __tests__/unit/crypto/transactions/transaction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
TransactionVersionError,
UnkownTransactionError,
} from "../../../../packages/crypto/src/errors";
import { PublicKey } from "../../../../packages/crypto/src/identities";
import { ITransactionData } from "../../../../packages/crypto/src/interfaces";
import { configManager } from "../../../../packages/crypto/src/managers";
import { BuilderFactory, Transaction, TransactionFactory } from "../../../../packages/crypto/src/transactions";
Expand Down Expand Up @@ -63,7 +64,7 @@ const createRandomTx = type => {
case 4: {
// multisignature registration
const passphrases = [1, 2, 3].map(() => Math.random().toString(36));
const publicKeys = passphrases.map(passphrase => `+${crypto.getKeys(passphrase).publicKey}`);
const publicKeys = passphrases.map(passphrase => `+${PublicKey.fromPassphrase(passphrase)}`);
const min = Math.min(1, publicKeys.length);
const max = Math.max(1, publicKeys.length);
const minSignatures = Math.floor(Math.random() * (max - min)) + min;
Expand Down
6 changes: 3 additions & 3 deletions __tests__/utils/fixtures/testnet/delegates.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Crypto, Managers } from "@arkecosystem/crypto";
import { Identities, Managers } from "@arkecosystem/crypto";

/**
* Get the testnet genesis delegates information
Expand All @@ -11,8 +11,8 @@ import { secrets } from "../../config/testnet/delegates.json";
import { genesisBlock } from "../../config/testnet/genesisBlock";

export const delegates: any = secrets.map(secret => {
const publicKey = Crypto.crypto.getKeys(secret).publicKey;
const address = Crypto.crypto.getAddress(publicKey);
const publicKey: string = Identities.PublicKey.fromPassphrase(secret);
const address: string = Identities.Address.fromPassphrase(secret);
const balance = genesisBlock.transactions.find(
transaction => transaction.recipientId === address && transaction.type === 0,
).amount;
Expand Down
6 changes: 3 additions & 3 deletions __tests__/utils/fixtures/unitnet/delegates.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Crypto, Managers } from "@arkecosystem/crypto";
import { Identities, Managers } from "@arkecosystem/crypto";

/**
* Get the unitnet genesis delegates information
Expand All @@ -11,8 +11,8 @@ import { secrets } from "../../config/unitnet/delegates.json";
import { genesisBlock } from "../../config/unitnet/genesisBlock";

export const delegates: any = secrets.map(secret => {
const publicKey = Crypto.crypto.getKeys(secret).publicKey;
const address = Crypto.crypto.getAddress(publicKey);
const publicKey: string = Identities.PublicKey.fromPassphrase(secret);
const address: string = Identities.Address.fromPassphrase(secret);
const balance = genesisBlock.transactions.find(
transaction => transaction.recipientId === address && transaction.type === 0,
).amount;
Expand Down
7 changes: 4 additions & 3 deletions __tests__/utils/generators/wallets.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Crypto, Managers } from "@arkecosystem/crypto";
import { Identities } from "@arkecosystem/crypto/src";
import { generateMnemonic } from "bip39";

export const generateWallets = (network, quantity = 10) => {
Expand All @@ -11,9 +12,9 @@ export const generateWallets = (network, quantity = 10) => {

const wallets = [];
for (let i = 0; i < quantity; i++) {
const passphrase = generateMnemonic();
const publicKey = Crypto.crypto.getKeys(passphrase).publicKey;
const address = Crypto.crypto.getAddress(publicKey);
const passphrase: string = generateMnemonic();
const publicKey: string = Identities.PublicKey.fromPassphrase(passphrase);
const address: string = Identities.Address.fromPassphrase(passphrase);

wallets.push({ address, passphrase, publicKey });
}
Expand Down

0 comments on commit 7a8df7d

Please sign in to comment.