Skip to content

Commit

Permalink
chore: add guardian packages
Browse files Browse the repository at this point in the history
  • Loading branch information
amacar committed Oct 19, 2020
1 parent e04b966 commit b65147c
Show file tree
Hide file tree
Showing 100 changed files with 5,351 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
![Img](core-starter-kit.png)
![Img](guardian.png)
[![License: CC BY-NC-SA 4.0](https://img.shields.io/badge/License-CC%20BY--NC--SA%204.0-lightgrey.svg)](https://creativecommons.org/licenses/by-nc-sa/4.0/)

# ARK Core-v3 Starter Kit
Expand Down
Binary file removed core-starter-kit.png
Binary file not shown.
Binary file added guardian.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file removed packages/.gitkeep
Empty file.
22 changes: 22 additions & 0 deletions packages/guardian-api/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# License
===============

This work is licensed under [Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License](https://creativecommons.org/licenses/by-nc-sa/4.0/), under the following terms:

Attribution
---------------
You must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use.

NonCommercial
---------------
You may not use the material for commercial purposes. For commercial purposes please reach out to [email protected].

ShareAlike
---------------
If you remix, transform, or build upon the material, you must distribute your contributions under the same license as the original.

Legal code
---------------
Read the rest of the obligatory [license legal code](https://creativecommons.org/licenses/by-nc-sa/4.0/legalcode).

Copyright (c) Protokol.com 2020
30 changes: 30 additions & 0 deletions packages/guardian-api/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
![Img](guardian-api.png)
[![License: CC BY-NC-SA 4.0](https://img.shields.io/badge/License-CC%20BY--NC--SA%204.0-lightgrey.svg)](https://creativecommons.org/licenses/by-nc-sa/4.0/)

# Guardian API

A Protokol module providing Guardian API support for the ARK Core Blockchain Framework.

# License

[![License: CC BY-NC-SA 4.0](https://img.shields.io/badge/License-CC%20BY--NC--SA%204.0-lightgrey.svg)](https://creativecommons.org/licenses/by-nc-sa/4.0/)

This work is licensed under [Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License](https://creativecommons.org/licenses/by-nc-sa/4.0/), under the following terms:

#### Attribution

You must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use.

#### NonCommercial

You may not use the material for commercial purposes. For commercial purposes please reach out to [email protected].

#### ShareAlike

If you remix, transform, or build upon the material, you must distribute your contributions under the same license as the original.

#### Legal code

Read the rest of the obligatory [license legal code](https://creativecommons.org/licenses/by-nc-sa/4.0/legalcode).

Copyright (c) Protokol.com 2020
60 changes: 60 additions & 0 deletions packages/guardian-api/__tests__/integration/__support__/app.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
{
"core": {
"plugins": [
{
"package": "@arkecosystem/core-logger-pino"
},
{
"package": "@arkecosystem/core-manager"
},
{
"package": "@arkecosystem/core-state"
},
{
"package": "@arkecosystem/core-database"
},
{
"package": "@arkecosystem/core-transactions"
},
{
"package": "@arkecosystem/core-magistrate-transactions"
},
{
"package": "@arkecosystem/core-transaction-pool"
},
{
"package": "@protokol/guardian-transactions"
},
{
"package": "@arkecosystem/core-p2p"
},
{
"package": "@arkecosystem/core-blockchain"
},
{
"package": "@arkecosystem/core-api"
},
{
"package": "@protokol/guardian-api"
},
{
"package": "@arkecosystem/core-magistrate-api"
},
{
"package": "@arkecosystem/core-webhooks"
},
{
"package": "@arkecosystem/core-forger"
},
{
"package": "@arkecosystem/core-snapshots"
}
]
},
"relay": {
"plugins": []
},
"forger": {
"plugins": []
}
}
48 changes: 48 additions & 0 deletions packages/guardian-api/__tests__/integration/__support__/setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { Utils as AppUtils } from "@arkecosystem/core-kernel";
import { Sandbox } from "@arkecosystem/core-test-framework";
import { Managers } from "@arkecosystem/crypto";

const sandbox: Sandbox = new Sandbox();

export const setUp = async () => {
jest.setTimeout(60000);

process.env.DISABLE_P2P_SERVER = "true"; // no need for p2p socket server to run
process.env.CORE_RESET_DATABASE = "1";

sandbox.withCoreOptions({
flags: {
token: "ark",
network: "unitnet",
env: "test",
},
peers: {
list: [{ ip: "127.0.0.1", port: 4000 }], // need some peers defined for the app to run
},
app: require("./app.json"),
});
await sandbox.boot(async ({ app }) => {
await app.bootstrap({
flags: {
token: "ark",
network: "unitnet",
env: "test",
processType: "core",
},
});

Managers.configManager.getMilestone().aip11 = false;
Managers.configManager.getMilestone().htlcEnabled = false;

await app.boot();

Managers.configManager.getMilestone().aip11 = true;
Managers.configManager.getMilestone().htlcEnabled = true;

await AppUtils.sleep(1000); // give some more time for api server to be up
});

return sandbox.app;
};

export const tearDown = async () => sandbox.dispose();
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import "@arkecosystem/core-test-framework/dist/matchers";

import { Contracts } from "@arkecosystem/core-kernel";
import { ApiHelpers } from "@arkecosystem/core-test-framework";
import latestVersion from "latest-version";

import { setUp, tearDown } from "../__support__/setup";

let app: Contracts.Kernel.Application;
let api: ApiHelpers;

beforeAll(async () => {
app = await setUp();
api = new ApiHelpers(app);
});

afterAll(async () => await tearDown());

describe("API - Configurations", () => {
describe("GET /guardian/configurations", () => {
it("should GET guardian-api configurations data", async () => {
const response = await api.request("GET", "guardian/configurations");
expect(response).toBeSuccessfulResponse();

expect(response.data.data.package.name).toStrictEqual(require("../../../package.json").name);
expect(response.data.data.package.currentVersion).toStrictEqual(require("../../../package.json").version);
expect(response.data.data.package.latestVersion).toStrictEqual(
await latestVersion(require("../../../package.json").name),
);
expect(response.data.data.crypto).toBeObject();
expect(response.data.data.transactions).toBeObject();
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import "@arkecosystem/core-test-framework/dist/matchers";

import { Contracts } from "@arkecosystem/core-kernel";
import { ApiHelpers, passphrases } from "@arkecosystem/core-test-framework";
import { Builders } from "@protokol/guardian-crypto";
import { Interfaces } from "@protokol/guardian-transactions";

import { setUp, tearDown } from "../__support__/setup";

let app: Contracts.Kernel.Application;
let api: ApiHelpers;

beforeAll(async () => {
app = await setUp();
api = new ApiHelpers(app);
});

afterAll(async () => await tearDown());

describe("API - Post transaction", () => {
it("should return Forbidden if wallet doesn't have permissions to POST transactions", async () => {
// change permission resolver to reject all transactions
app.get<any>(Interfaces.Identifiers.PermissionsResolver).resolve = () => Promise.resolve(false);

const actual = new Builders.GuardianUserPermissionsBuilder()
.GuardianUserPermissions({
publicKey: "02def27da9336e7fbf63131b8d7e5c9f45b296235db035f1f4242c507398f0f21d",
})
.nonce("3")
.sign(passphrases[0])
.build();

const response = await api.request("POST", "transactions", {
transactions: [actual.data],
});

// response should be 403 (without custom error handler is 500)
expect(response.data.statusCode).toBe(403);
});
});
108 changes: 108 additions & 0 deletions packages/guardian-api/__tests__/integration/handlers/groups.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import "@arkecosystem/core-test-framework/dist/matchers";

import { Container, Contracts } from "@arkecosystem/core-kernel";
import { ApiHelpers } from "@arkecosystem/core-test-framework";
import { Enums } from "@protokol/guardian-crypto";
import { Indexers, Interfaces } from "@protokol/guardian-transactions";

import { setUp, tearDown } from "../__support__/setup";

let app: Contracts.Kernel.Application;
let api: ApiHelpers;

const groups = [
{
name: "group name1",
priority: 1,
default: false,
active: true,
allow: [
{
transactionType: Enums.GuardianTransactionTypes.GuardianSetGroupPermissions,
transactionTypeGroup: Enums.GuardianTransactionGroup,
},
],
deny: [],
},
{
name: "group name2",
priority: 2,
default: false,
active: true,
deny: [{ transactionType: 9000, transactionTypeGroup: 0 }],
allow: [],
},
];

beforeAll(async () => {
app = await setUp();
api = new ApiHelpers(app);

const groupsPermissionsCache = app.getTagged<
Contracts.Kernel.CacheStore<Interfaces.IGroupPermissions["name"], Interfaces.IGroupPermissions>
>(Container.Identifiers.CacheService, "cache", "@protokol/guardian-transactions");

// set mock groups
for (const group of groups) {
await groupsPermissionsCache.put(group.name, group, -1);
}
});

afterAll(async () => await tearDown());

describe("API - Groups", () => {
describe("GET /guardian/groups", () => {
it("should GET get all groups", async () => {
const response = await api.request("GET", "guardian/groups");

expect(response).toBeSuccessfulResponse();
api.expectPaginator(response);
expect(response.data.data).toBeArray();
expect(response.data.data.length).toBe(2);
expect(response.data.data[0]).toStrictEqual(groups[0]);
});
});

describe("GET /guardian/groups/{id}", () => {
it("should GET user by id", async () => {
const response = await api.request("GET", `guardian/groups/${groups[0].name}`);

expect(response).toBeSuccessfulResponse();
expect(response.data.data).toStrictEqual(groups[0]);
});

it("should fail to GET a group by id if it doesn't exist", async () => {
api.expectError(await api.request("GET", "guardian/groups/non-existing"));
});
});

describe("GET /guardian/groups/{id}/users", () => {
it("should GET group's users", async () => {
const walletRepository = app.getTagged<Contracts.State.WalletRepository>(
Container.Identifiers.WalletRepository,
"state",
"blockchain",
);
const publicKey = "02def27da9336e7fbf63131b8d7e5c9f45b296235db035f1f4242c507398f0f21d";
const user = {
groups: ["group name1"],
allow: [],
deny: [],
};
const wallet = walletRepository.findByPublicKey(publicKey);
wallet.setAttribute("guardian.userPermissions", user);
walletRepository.getIndex(Indexers.GuardianIndexers.UserPermissionsIndexer).index(wallet);

const response = await api.request("GET", `guardian/groups/${groups[0].name}/users`);

expect(response).toBeSuccessfulResponse();
expect(response.data.data).toBeArray();
expect(response.data.data.length).toBe(1);
expect(response.data.data[0]).toStrictEqual({ ...user, publicKey });
});

it("should fail to GET a group's users if group doesn't exist", async () => {
api.expectError(await api.request("GET", "guardian/groups/non-existing/users"));
});
});
});
Loading

0 comments on commit b65147c

Please sign in to comment.