Skip to content

Commit

Permalink
fix: getting output to be identical to current lighthouse API
Browse files Browse the repository at this point in the history
  • Loading branch information
dafuga committed Jun 28, 2024
1 parent 85eb62a commit 71d0546
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 39 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# lighthouse-node
# lighthouse-js

To install dependencies:

Expand All @@ -9,7 +9,13 @@ bun install
To run:

```bash
bun run index.ts
bun run dev
```

To build:

```bash
bun run build
```

This project was created using `bun init` in bun v1.1.16. [Bun](https://bun.sh) is a fast all-in-one JavaScript runtime.
Binary file removed lighthouse
Binary file not shown.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"type": "module",
"scripts": {
"start": "bun src/index.ts",
"build": "bun build src/index.ts --compile --outfile lighthouse",
"build": "bun build src/index.ts --compile --outfile dist/lighthouse",
"dev": "bun --hot src/index.ts"
},
"peerDependencies": {
Expand Down
53 changes: 53 additions & 0 deletions src/chains.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { ChainDefinition, Chains } from "@wharfkit/common";

export type Chain = ChainDefinition | {
id: string;
name: string;
url: string;
};

const AYETU_MAINNET = {
id: "9b06067cf9f0a293e854cbdbcf4bc0292bbf1137dd01d3d9300f403706444504",
name: "Ayetu",
url: "https://mainnet.ayetu.net",
};

const KOY_MAINNET = {
id: "adf3860dc671acafa2e4ce7ab4fd90920a487e8e82a36e8b4364aad5129552cd",
name: "KOY",
url: "https://api.mainnet.koynetwork.io",
};

const AYETU_TESTNET = {
id: "38b20c9055b39035eaee7fdf450ce9b2572024bcc6d4ee8cddd50662a0cdeff1",
name: "Ayetu (Testnet)",
url: "https://testnet.ayetu.net",
}

const KOY_TESTNET = {
id: "181e289803751d4e0fc257fd186edaa6df8169e28631f1bf63fc9287a80cfb5f",
name: "KOY (Testnet)",
url: "https://api.testnet.koynetwork.io",
}

export const MAINNET_CHAINS = [
Chains.EOS,
Chains.Telos,
Chains.WAX,
Chains.Proton,
Chains.Libre,
Chains.UX,
Chains.FIO,
AYETU_MAINNET,
KOY_MAINNET,
];

export const TESTNET_CHAINS = [
Chains.TelosTestnet,
Chains.WAXTestnet,
Chains.LibreTestnet,
Chains.FIOTestnet,
Chains.Jungle4,
AYETU_TESTNET,
KOY_TESTNET,
];
39 changes: 9 additions & 30 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,6 @@
import { serve } from "bun";
import { APIClient, PublicKey } from "@wharfkit/antelope";
import { ChainDefinition, Chains } from "@wharfkit/common";

const MAINNET_CHAINS = [
Chains.EOS,
Chains.Telos,
Chains.WAX,
Chains.Proton,
Chains.Libre,
Chains.UX,
Chains.FIO,
// Chains.Ayetu,
// Chains.KOY,
];

const TESTNET_CHAINS = [
Chains.TelosTestnet,
Chains.WAXTestnet,
Chains.ProtonTestnet,
Chains.LibreTestnet,
Chains.FIOTestnet,
// Chains.UXTestnet,
// Chains.AyetuTestnet,
// Chains.KOYTestnet,
];
import { MAINNET_CHAINS, TESTNET_CHAINS, type Chain } from "./chains";

export const accountLookup = async (req: Request) => {
const url = new URL(req.url);
Expand All @@ -38,8 +15,8 @@ export const accountLookup = async (req: Request) => {
return new Response(JSON.stringify({ error: "Invalid public key" }), { status: 400 });
}

const chains: ChainDefinition[] = includeTestnets ? [...MAINNET_CHAINS, ...TESTNET_CHAINS] : MAINNET_CHAINS;
const lookups = await Promise.all(chains.map(chain => lookupNetwork(publicKey, chain)));
const chains: Chain[] = includeTestnets ? [...MAINNET_CHAINS, ...TESTNET_CHAINS] : MAINNET_CHAINS;
const lookups = (await Promise.all(chains.map(chain => lookupNetwork(publicKey, chain)))).filter(({ accounts }) => accounts.length > 0)

const networkAccounts = lookups.map(({ chain, accounts }) => ({
network: chain.name,
Expand All @@ -53,7 +30,7 @@ export const accountLookup = async (req: Request) => {
return new Response(JSON.stringify(networkAccounts));
};

export const lookupNetwork = async (publicKey: PublicKey, chain: ChainDefinition, apiClient?: APIClient) => {
export const lookupNetwork = async (publicKey: PublicKey, chain: Chain, apiClient?: APIClient) => {
try {
const accounts = await networkRequest(publicKey, chain, apiClient);
return { chain, accounts };
Expand All @@ -63,12 +40,12 @@ export const lookupNetwork = async (publicKey: PublicKey, chain: ChainDefinition
}
};

const networkRequest = async (publicKey: PublicKey, chain: ChainDefinition, apiClient?: APIClient) => {
const networkRequest = async (publicKey: PublicKey, chain: Chain, apiClient?: APIClient) => {
const client = apiClient || new APIClient(chain);
const response = await client.v1.chain.get_accounts_by_authorizers({ keys: [publicKey] });
return response.accounts.map((account: any) => ({
accountName: account.account_name,
permissionName: account.permission_name,
actor: account.account_name,
permission: account.permission_name,
}));
};

Expand All @@ -83,3 +60,5 @@ serve({
},
port: 3000,
});

console.log('Server running at http://localhost:3000/ 🚀')
22 changes: 16 additions & 6 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ const mockClient = makeClient('https://jungle4.greymass.com');

describe("accountLookup", () => {
it("should handle case where bad public key is provided", async () => {
const req = new Request(`https://lighthouse.greymass.com/lookup/${BAD_PUBLIC_KEY}`);
const req = new Request(`https://eosio.greymass.com/lookup/${BAD_PUBLIC_KEY}`);
const result = await accountLookup(req);
assert.equal(result.status, 400);
})

it("should handle case where no public key is provided", async () => {
const req = new Request(`https://lighthouse.greymass.com/lookup/`);
const req = new Request(`https://eosio.greymass.com/lookup/`);
const result = await accountLookup(req);
assert.equal(result.status, 400);
})
Expand All @@ -27,22 +27,32 @@ describe("accountLookup", () => {
describe("lookupNetwork", () => {
it("should handle network lookup", async () => {
const publicKey = PublicKey.from(PUBLIC_KEY);
const chain = Chains.EOS;
const chain = Chains.Jungle4;

const result = await lookupNetwork(publicKey, chain, mockClient);
const result = await lookupNetwork(publicKey, Chains.Jungle4, mockClient);
assert.containsAllKeys(result, ['chain', 'accounts']);
assert.equal(result.chain.name, chain.name);
assert.equal(result.accounts.length, 2);
assert.deepEqual(String(result.accounts[0].accountName), "testerman123");
assert.deepEqual(String(result.accounts[0].actor), "testerman123");
});

it("should handle network lookup with non existent public key", async () => {
const publicKey = PublicKey.from(NON_EXISTENT_PUBLIC_KEY);
const chain = Chains.EOS;
const chain = Chains.Jungle4;

const result = await lookupNetwork(publicKey, chain, mockClient);
assert.containsAllKeys(result, ['chain', 'accounts']);
assert.equal(result.chain.name, chain.name);
assert.equal(result.accounts.length, 0);
});

it("should handle network lookup error", async () => {
const publicKey = PublicKey.from(PUBLIC_KEY);
const chain = Chains.Jungle4;

const result = await lookupNetwork(publicKey, chain, makeClient('https://jungle0.greymass.com'));
assert.containsAllKeys(result, ['chain', 'accounts']);
assert.equal(result.chain.name, chain.name);
assert.equal(result.accounts.length, 0);
});
});

0 comments on commit 71d0546

Please sign in to comment.