Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Implement IPFS (AIP11) #2537

Merged
merged 15 commits into from
May 7, 2019
Prev Previous commit
Next Next commit
test: ipfs transactions
  • Loading branch information
air1one committed May 7, 2019
commit d126a4a4dd11fde26d72976716430c5db588001b
119 changes: 119 additions & 0 deletions __tests__/functional/transaction-forging/ipfs.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
import { Identities } from "@arkecosystem/crypto";
import { TransactionFactory } from "../../helpers/transaction-factory";
import { secrets } from "../../utils/config/testnet/delegates.json";
import * as support from "./__support__";

const { passphrase, secondPassphrase } = support.passphrases;
const ipfsIds = [
"QmR45FmbVVrixReBwJkhEKde2qwHYaQzGxu4ZoDeswuF9w",
"QmYSK2JyM3RyDyB52caZCTKFR3HKniEcMnNJYdk8DQ6KKB",
"QmQeUqdjFmaxuJewStqCLUoKrR9khqb4Edw9TfRQQdfWz3",
"Qma98bk1hjiRZDTmYmfiUXDj8hXXt7uGA5roU5mfUb3sVG",
];

beforeAll(support.setUp);
afterAll(support.tearDown);

describe("Transaction Forging - IPFS", () => {
it("should broadcast, accept and forge it [Signed with 1 Passphase]", async () => {
// Initial Funds
const initialFunds = TransactionFactory.transfer(Identities.Address.fromPassphrase(passphrase), 100 * 1e8)
.withPassphrase(secrets[0])
.create();

await support.expectAcceptAndBroadcast(initialFunds, initialFunds[0].id);
await support.snoozeForBlock(1);
await support.expectTransactionForged(initialFunds[0].id);

// Submit ipfs transaction
const transactions = TransactionFactory.ipfs(ipfsIds[0])
.withPassphrase(passphrase)
.create();

await support.expectAcceptAndBroadcast(transactions, transactions[0].id);
await support.snoozeForBlock(1);
await support.expectTransactionForged(transactions[0].id);
});

it("should broadcast, accept and forge it [Signed with 2 Passphrases]", async () => {
// Make a fresh wallet for the second signature tests
const passphrase = secondPassphrase;

// Initial Funds
const initialFunds = TransactionFactory.transfer(Identities.Address.fromPassphrase(passphrase), 100 * 1e8)
.withPassphrase(secrets[0])
.create();

await support.expectAcceptAndBroadcast(initialFunds, initialFunds[0].id);
await support.snoozeForBlock(1);
await support.expectTransactionForged(initialFunds[0].id);

// Register a second passphrase
const secondSignature = TransactionFactory.secondSignature(secondPassphrase)
.withPassphrase(passphrase)
.create();

await support.expectAcceptAndBroadcast(secondSignature, secondSignature[0].id);
await support.snoozeForBlock(1);
await support.expectTransactionForged(secondSignature[0].id);

// Submit ipfs transaction
const transactions = TransactionFactory.ipfs(ipfsIds[1])
.withPassphrasePair({ passphrase, secondPassphrase })
.create();

await support.expectAcceptAndBroadcast(transactions, transactions[0].id);
await support.snoozeForBlock(1);
await support.expectTransactionForged(transactions[0].id);
});

it("should broadcast, accept and forge it [3-of-3 multisig]", async () => {
// Funds to register a multi signature wallet
const initialFunds = TransactionFactory.transfer(Identities.Address.fromPassphrase(secrets[3]), 50 * 1e8)
.withPassphrase(secrets[0])
.create();

await support.expectAcceptAndBroadcast(initialFunds, initialFunds[0].id);
await support.snoozeForBlock(1);
await support.expectTransactionForged(initialFunds[0].id);

// Register a multi signature wallet with defaults
const passphrases = [secrets[3], secrets[4], secrets[5]];
const participants = [
Identities.PublicKey.fromPassphrase(passphrases[0]),
Identities.PublicKey.fromPassphrase(passphrases[1]),
Identities.PublicKey.fromPassphrase(passphrases[2]),
];

const multiSignature = TransactionFactory.multiSignature(participants, 3)
.withPassphrase(secrets[3])
.withPassphraseList(passphrases)
.create();

await support.expectAcceptAndBroadcast(multiSignature, multiSignature[0].id);
await support.snoozeForBlock(1);
await support.expectTransactionForged(multiSignature[0].id);

// Send funds to multi signature wallet
const multiSigAddress = Identities.Address.fromMultiSignatureAsset(multiSignature[0].asset.multiSignature);
const multiSigPublicKey = Identities.PublicKey.fromMultiSignatureAsset(multiSignature[0].asset.multiSignature);

const multiSignatureFunds = TransactionFactory.transfer(multiSigAddress, 20 * 1e8)
.withPassphrase(secrets[0])
.create();

await support.expectAcceptAndBroadcast(multiSignatureFunds, multiSignatureFunds[0].id);
await support.snoozeForBlock(1);
await support.expectTransactionForged(multiSignatureFunds[0].id);

// Submit ipfs transaction
const transactions = TransactionFactory.ipfs(ipfsIds[0])
.withSenderPublicKey(multiSigPublicKey)
.withPassphraseList(passphrases)
.create();

await support.expectAcceptAndBroadcast(transactions, transactions[0].id);
await support.snoozeForBlock(1);
await support.expectTransactionForged(transactions[0].id);
});
});
4 changes: 4 additions & 0 deletions __tests__/helpers/transaction-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ export class TransactionFactory {
return factory;
}

public static ipfs(ipfsId: string): TransactionFactory {
return new TransactionFactory(Transactions.BuilderFactory.ipfs().ipfsAsset(ipfsId));
}

private builder: any;
private network: Types.NetworkName = "testnet";
private fee: Utils.BigNumber;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ describe("Registry", () => {
Registry.get(TransactionTypes.DelegateRegistration);
Registry.get(TransactionTypes.Vote);
Registry.get(TransactionTypes.MultiSignature);
Registry.get(TransactionTypes.Ipfs);
}).not.toThrow(Errors.InvalidTransactionTypeError);
});

Expand Down
70 changes: 58 additions & 12 deletions __tests__/unit/core-transactions/handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -630,32 +630,78 @@ describe("MultiSignatureRegistrationTransaction", () => {
});
});

describe.skip("IpfsTransaction", () => {
describe("Ipfs", () => {
beforeAll(() => {
Managers.configManager.setFromPreset("testnet");
});

beforeEach(() => {
transaction = transactionFixture;
senderWallet = walletFixture;
senderWallet.balance = transaction.amount.plus(transaction.fee);
senderWallet = new Wallets.Wallet("AXYxHAFdRC41VdFPS2jvmsvtSQPtCfUgon");
senderWallet.balance = Utils.BigNumber.make("6453530000000");
senderWallet.publicKey = "02a47a2f594635737d2ce9898680812ff7fa6aaa64ddea1360474c110e9985a087";
senderWallet.secondPublicKey = undefined;

walletManager.reindex(senderWallet);

transaction = {
asset: {
ipfs: "QmR45FmbVVrixReBwJkhEKde2qwHYaQzGxu4ZoDeswuF9w",
},
fee: Utils.BigNumber.make(500000000),
amount: Utils.BigNumber.ZERO,
id: "210a785d6e4a8d04f918a5126e3503a8b247d20ff7792e3c05c8a7f10be927b3",
network: 23,
senderPublicKey: "02a47a2f594635737d2ce9898680812ff7fa6aaa64ddea1360474c110e9985a087",
signature:
"9f325478e3a51d15e0979d08ef8f0b6a9b45febec7cc52f648550a2164d212a07a56d126c4662d47add5791ffd38156c6ab744fbd7d10a0ad481da2164c447be",
timestamp: 67108989,
type: 5,
version: 2,
};

handler = Handlers.Registry.get(transaction.type);
instance = Transactions.TransactionFactory.fromData(transaction);
});

describe("canApply", () => {
describe("canBeApplied", () => {
it("should be true", () => {
expect(handler.canBeApplied(instance, senderWallet, walletManager)).toBeTrue();
});

it("should be false", () => {
instance.data.senderPublicKey = "a".repeat(66);
expect(() => handler.canBeApplied(instance, senderWallet, walletManager)).toThrow(
SenderWalletMismatchError,
);
});

it("should be false if wallet has insufficient funds", () => {
senderWallet.balance = Utils.BigNumber.ZERO;

expect(() => handler.canBeApplied(instance, senderWallet, walletManager)).toThrow(InsufficientBalanceError);
});
});

describe("apply", () => {
it("should apply ipfs transaction", () => {
expect(handler.canBeApplied(instance, senderWallet, walletManager)).toBeTrue();

const balanceBefore = senderWallet.balance;

handler.apply(instance, walletManager);

expect(senderWallet.balance).toEqual(balanceBefore.minus(transaction.fee));
});
});

describe("revert", () => {
it("should be ok", () => {
expect(handler.canBeApplied(instance, senderWallet, walletManager)).toBeTrue();

const balanceBefore = senderWallet.balance;

handler.apply(instance, walletManager);

expect(senderWallet.balance).toEqual(balanceBefore.minus(transaction.fee));

handler.revert(instance, walletManager);

expect(senderWallet.balance).toEqual(balanceBefore);
});
});
});

describe.skip("TimelockTransferTransaction", () => {
Expand Down
19 changes: 15 additions & 4 deletions __tests__/unit/crypto/transactions/deserializer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,13 +212,24 @@ describe("Transaction serializer / deserializer", () => {
});
});

describe.skip("ser/deserialize - ipfs", () => {
describe("ser/deserialize - ipfs", () => {
const ipfsIds = [
"QmR45FmbVVrixReBwJkhEKde2qwHYaQzGxu4ZoDeswuF9w",
"QmYSK2JyM3RyDyB52caZCTKFR3HKniEcMnNJYdk8DQ6KKB",
"QmQeUqdjFmaxuJewStqCLUoKrR9khqb4Edw9TfRQQdfWz3",
"Qma98bk1hjiRZDTmYmfiUXDj8hXXt7uGA5roU5mfUb3sVG",
];

beforeAll(() => {
configManager.setFromPreset("testnet");
});

it("should ser/deserialize giving back original fields", () => {
const ipfs = BuilderFactory.ipfs()
.fee("50000000")
.version(1)
.network(30)
.dag("da304502")
.version(2)
.network(23)
.ipfsAsset(ipfsIds[0])
.sign("dummy passphrase")
.getStruct();

Expand Down