Skip to content

Commit

Permalink
refactor(core-test-framework): create a real wallet via wallet factory
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Faust committed Nov 27, 2019
1 parent cded589 commit 838ca5d
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 46 deletions.
3 changes: 1 addition & 2 deletions __tests__/integration/core-api/handlers/peers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ beforeAll(async () => {
api = new ApiHelpers(app);

const peerMocks = peers.map(mock => {
const peerMock = new Peer(app, mock.ip);
peerMock.port = mock.port;
const peerMock = new Peer(mock.ip, mock.port);
peerMock.version = mock.version;
peerMock.latency = mock.latency;
peerMock.state.height = mock.state.height;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
import "jest-extended";

import { Wallets } from "@arkecosystem/core-state";
import { FactoryBuilder, Factories } from "@packages/core-test-framework/src/factories";

interface Wallet {
address: string;
publicKey: string;
privateKey: string;
wif: string;
passphrase: string;
secondPassphrase?: string;
}

let factory: FactoryBuilder;

beforeEach(() => {
Expand All @@ -20,36 +12,23 @@ beforeEach(() => {
});

describe("WalletFactory", () => {
it("should make a wallet with a single passphrase", () => {
const entity: Wallet = factory.get("Wallet").make<Wallet>() as Wallet;
it("should make a wallet", () => {
const entity: Wallets.Wallet = factory.get("Wallet").make<Wallets.Wallet>();

expect(entity).toContainAllKeys(["address", "publicKey", "privateKey", "wif", "passphrase"]);
expect(entity).toBeInstanceOf(Wallets.Wallet);
expect(entity.address).toBeString();
expect(entity.publicKey).toBeString();
expect(entity.privateKey).toBeString();
expect(entity.wif).toBeString();
expect(entity.passphrase).toBeString();
});

it("should make a wallet with a second passphrase", () => {
const entity: Wallet = factory
it("should make a wallet with a second public key", () => {
const entity: Wallets.Wallet = factory
.get("Wallet")
.withStates("secondPassphrase")
.make<Wallet>() as Wallet;

expect(entity).toContainAllKeys([
"address",
"publicKey",
"privateKey",
"wif",
"passphrase",
"secondPassphrase",
]);
.withStates("secondPublicKey")
.make<Wallets.Wallet>();

expect(entity).toBeInstanceOf(Wallets.Wallet);
expect(entity.address).toBeString();
expect(entity.publicKey).toBeString();
expect(entity.privateKey).toBeString();
expect(entity.wif).toBeString();
expect(entity.passphrase).toBeString();
expect(entity.secondPassphrase).toBeString();
expect(entity.getAttribute("secondPublicKey")).toBeString();
});
});
45 changes: 33 additions & 12 deletions packages/core-test-framework/src/factories/factories/wallet.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,46 @@
import { Services } from "@arkecosystem/core-kernel";
import { Wallets } from "@arkecosystem/core-state";
import { Identities } from "@arkecosystem/crypto";
import { generateMnemonic } from "bip39";

import { FactoryBuilder } from "../factory-builder";

export const registerWalletFactory = (factory: FactoryBuilder): void => {
factory.set("Wallet", () => {
const passphrase: string = generateMnemonic();
const knownAttributes: Services.Attributes.AttributeSet = new Services.Attributes.AttributeSet();
knownAttributes.set("delegate.approval");
knownAttributes.set("delegate.forgedFees");
knownAttributes.set("delegate.forgedRewards");
knownAttributes.set("delegate.forgedTotal");
knownAttributes.set("delegate.lastBlock");
knownAttributes.set("delegate.producedBlocks");
knownAttributes.set("delegate.rank");
knownAttributes.set("delegate.resigned");
knownAttributes.set("delegate.round");
knownAttributes.set("delegate.username");
knownAttributes.set("delegate.voteBalance");
knownAttributes.set("delegate");
knownAttributes.set("htlc.lockedBalance");
knownAttributes.set("htlc.locks");
knownAttributes.set("ipfs.hashes");
knownAttributes.set("ipfs");
knownAttributes.set("multiSignature");
knownAttributes.set("secondPublicKey");
knownAttributes.set("vote");

const { publicKey, privateKey } = Identities.Keys.fromPassphrase(passphrase);
factory.set("Wallet", ({ options }) => {
const passphrase: string = options.passphrase || generateMnemonic();

return {
publicKey,
privateKey,
address: Identities.Address.fromPassphrase(passphrase),
wif: Identities.WIF.fromPassphrase(passphrase),
passphrase,
};
const wallet: Wallets.Wallet = new Wallets.Wallet(
Identities.Address.fromPassphrase(passphrase),
new Services.Attributes.AttributeMap(knownAttributes),
);
wallet.publicKey = Identities.PublicKey.fromPassphrase(passphrase);

return wallet;
});

factory.get("Wallet").state("secondPassphrase", ({ entity }) => {
entity.secondPassphrase = generateMnemonic();
factory.get("Wallet").state("secondPublicKey", ({ entity }) => {
entity.setAttribute("secondPublicKey", Identities.PublicKey.fromPassphrase(generateMnemonic()));

return entity;
});
Expand Down

0 comments on commit 838ca5d

Please sign in to comment.