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

Prepare for React Native support #205

Merged
merged 1 commit into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"packageManager": "[email protected]",
"scripts": {
"build": "yarn workspaces foreach -t run build",
"clean": "yarn workspaces foreach -t run clean",
"lint": "yarn workspaces foreach -t run lint",
"test": "yarn workspaces foreach -t run test"
}
Expand Down
5 changes: 5 additions & 0 deletions packages/client-browser/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"extends": [
"../../.eslintrc.json"
]
}
6 changes: 6 additions & 0 deletions packages/client-browser/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/test/**
/src/**
jasmine*.json
tsconfig*.json
.eslintrc.json
typedoc.json
11 changes: 11 additions & 0 deletions packages/client-browser/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Logion Client SDK (browser)

This project provides a JS/TypeScript SDK enabling an application running in a browser to interact with a logion network.

## Installation

Use your favorite package manager (e.g. yarn) and install package `@logion/client-browser` in your JavaScript/TypeScript project.

## Usage

See [core client](../client/README.md).
15 changes: 15 additions & 0 deletions packages/client-browser/jasmine.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"spec_dir": "test",
"spec_files": [
"**/*.spec.ts"
],
"helpers": [
"../typescript.js"
],
"stopSpecOnExpectationFailure": false,
"reporters": [
{
"name": "jasmine-spec-reporter#SpecReporter"
}
]
}
51 changes: 51 additions & 0 deletions packages/client-browser/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{
"name": "@logion/client-browser",
"version": "0.1.0-1",
"description": "logion SDK for client applications running in a browser",
"main": "dist/index.js",
"packageManager": "[email protected]",
"type": "module",
"scripts": {
"build": "yarn lint && tsc -p tsconfig.json",
"lint": "yarn eslint src/**",
"test": "NODE_OPTIONS=--loader=ts-node/esm jasmine --config=jasmine.json",
"clean": "rm -rf dist"
},
"repository": {
"type": "git",
"url": "git+https://github.com/logion-network/logion-api.git",
"directory": "packages/client-browser"
},
"keywords": [
"logion",
"api",
"client",
"browser"
],
"author": "Logion Team",
"license": "Apache-2.0",
"dependencies": {
"@logion/client": "workspace:^"
},
"bugs": {
"url": "https://github.com/logion-network/logion-api/issues"
},
"homepage": "https://github.com/logion-network/logion-api/packages/client-browser#readme",
"devDependencies": {
"@tsconfig/node18": "^1.0.1",
"@types/jasmine": "^4.0.3",
"@types/node": "^18.6.1",
"@typescript-eslint/eslint-plugin": "^6.9.1",
"@typescript-eslint/parser": "^6.9.1",
"eslint": "^8.20.0",
"jasmine": "^4.3.0",
"jasmine-spec-reporter": "^7.0.0",
"moq.ts": "^9.0.2",
"ts-node": "^10.9.1",
"typescript": "^5.2.2"
},
"engines": {
"node": ">=18"
},
"stableVersion": "0.1.0"
}
49 changes: 49 additions & 0 deletions packages/client-browser/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { Hash as Hasher } from 'fast-sha256';
import { AxiosFileUploader, File, FormDataLike, HashAndSize } from "@logion/client";
import { Hash } from "@logion/node-api";

export class BrowserFile extends File {

constructor(file: Blob) {
super();
this.file = file;
}

private file: Blob;

async getHashAndSize(): Promise<HashAndSize> {
const unknownStream: any = this.file.stream(); // eslint-disable-line @typescript-eslint/no-explicit-any
const reader = unknownStream.getReader();
const digest = new Hasher();
let size = 0n;
let chunk: {done: boolean, value: Buffer} = await reader.read();
while(!chunk.done) {
size = size + BigInt(chunk.value.length);
digest.update(chunk.value);
chunk = await reader.read();
}
return {
hash: Hash.fromDigest(digest),
size,
};
}

getBlob(): Blob {
return this.file;
}
}

export class BrowserAxiosFileUploader extends AxiosFileUploader {

override buildFormData(): FormDataLike {
return new FormData();
}

override toFormDataValue(file: File): Promise<any> { // eslint-disable-line @typescript-eslint/no-explicit-any
if(file instanceof BrowserFile) {
return Promise.resolve(file.getBlob());
} else {
return Promise.reject(new Error("Unsupported file type"));
}
}
}
36 changes: 36 additions & 0 deletions packages/client-browser/test/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { Hash } from "@logion/node-api";
import { Mock, PlayTimes } from "moq.ts";
import { BrowserFile } from "../src/index.js";

const TEST_HASH = Hash.fromHex("0x9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08");

describe("BrowserFile", () => {

it("gets hash and size", async () => {
const file = new BrowserFile(BLOB_MOCK);
const hashSize = await file.getHashAndSize();
expect(hashSize.hash).toEqual(TEST_HASH);
expect(hashSize.size).toBe(4n);
});
});

const READER_MOCK = new Mock<any>()
.setup(instance => instance.read())
.play(PlayTimes.Once())
.returns({ done: true })

.setup(instance => instance.read())
.play(PlayTimes.Once())
.returns({ done: false, value: Buffer.from("test") })

.object();

const STREAM_MOCK = new Mock<any>()
.setup(instance => instance.getReader())
.returns(READER_MOCK)
.object();

const BLOB_MOCK = new Mock<Blob>()
.setup(instance => instance.stream())
.returns(STREAM_MOCK)
.object();
11 changes: 11 additions & 0 deletions packages/client-browser/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "./dist",
"lib": ["dom"],
"baseUrl": "."
},
"include": [
"./src/**/*"
]
}
6 changes: 6 additions & 0 deletions packages/client-browser/typedoc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "Client (Browser)",
"entryPoints": [
"./src/index.ts"
]
}
5 changes: 5 additions & 0 deletions packages/client-node/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"extends": [
"../../.eslintrc.json"
]
}
13 changes: 13 additions & 0 deletions packages/client-node/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/test/**
/integration/**
/src/**
/scripts/**
docker-compose.yml
front_config.js
front_web*.conf
jasmine*.json
tsconfig*.json
/config/**
.eslintrc.json
typedoc.json
typescript.js
11 changes: 11 additions & 0 deletions packages/client-node/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Logion Client SDK (browser)

This project provides a JS/TypeScript SDK enabling an application running in a browser to interact with a logion network.

## Installation

Use your favorite package manager (e.g. yarn) and install package `@logion/client-browser` in your JavaScript/TypeScript project.

## Usage

See [core client](../client/README.md).
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { Numbers, Currency, CoinBalance } from "@logion/node-api";
import { BalanceState, waitFor } from "@logion/client";

import { State, REQUESTER_ADDRESS } from "./Utils.js";
import { ALICE } from "../test/Utils.js";
import { BalanceState } from "../src/Balance.js";
import { waitFor } from "../src/Polling.js";
import { ALICE } from "./Utils.js";

export async function transfers(state: State) {
const { client, signer, aliceAccount, requesterAccount } = state;
Expand Down Expand Up @@ -36,7 +35,7 @@ export async function transfers(state: State) {
userState = await userState.transfer({
signer,
amount: new Numbers.PrefixedNumber("2", Numbers.KILO),
destination: ALICE.address
destination: ALICE
});
checkBalance(userState, "2.99k");

Expand Down Expand Up @@ -68,7 +67,7 @@ export async function transferAndCannotPayFees(state: State) {
await expectAsync(balanceState.transfer({
signer,
amount: new Numbers.PrefixedNumber("1", Numbers.NONE),
destination: ALICE.address,
destination: ALICE,
})).toBeRejectedWithError("Not enough funds available to pay fees");
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { NodeFile } from "../src/index.js";
import {
State,
initRequesterBalance,
TEST_LOGION_CLIENT_CONFIG,
DIRECT_REQUESTER_ADDRESS,
findWithLegalOfficerClient,
} from "./Utils.js";
import { ALICE } from "../test/Utils.js";
import { ALICE } from "./Utils.js";
import {
HashOrContent,
ItemsParams,
Expand All @@ -18,7 +19,7 @@ import {
LocData,
OpenLoc,
waitFor
} from "../src/index.js";
} from "@logion/client";
import { UUID } from "@logion/node-api";

export async function openIdentityLoc(state: State): Promise<UUID> {
Expand Down Expand Up @@ -129,7 +130,7 @@ function checkData(data: LocData, items: ItemsParams) {
expect(data.requesterLocId).toBeUndefined();
expect(data.requesterAddress?.address).toEqual(DIRECT_REQUESTER_ADDRESS);
expect(data.requesterAddress?.type).toEqual("Polkadot");
expect(data.ownerAddress).toEqual(ALICE.address);
expect(data.ownerAddress).toEqual(ALICE);

expect(data.files.length).toEqual(items.files.length);
expect(data.metadata.length).toEqual(items.metadata.length);
Expand All @@ -153,12 +154,12 @@ function provideItems(name: string, linkedLocs: UUID[]): ItemsParams {
{
fileName: `${ name }-1.txt`,
nature: "Some file nature",
file: HashOrContent.fromContent(Buffer.from(name + "1")),
file: HashOrContent.fromContent(new NodeFile(`integration/${ name }-1.txt`)),
},
{
fileName: `${ name }-2.txt`,
nature: "Some other file nature",
file: HashOrContent.fromContent(Buffer.from(name + "2")),
file: HashOrContent.fromContent(new NodeFile(`integration/${ name }-2.txt`)),
},
],
metadata: [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { ALICE } from "../test/Utils.js";
import { ALICE } from "./Utils.js";
import { State, REQUESTER_ADDRESS } from "./Utils.js";

export async function fees(state: State) {
const client = state.client;
const api = client.logionApi;
const submittable = api.polkadot.tx.balances.transfer(ALICE.address, "10000000");
const submittable = api.polkadot.tx.balances.transfer(ALICE, "10000000");
const fees = await client.public.fees.estimateWithoutStorage({ origin: REQUESTER_ADDRESS, submittable });
expect(fees.totalFee).toBe(3085311440000000n);
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { ALICE } from "../test/Utils.js";
import { State } from "./Utils.js";
import { ALICE, State } from "./Utils.js";

export async function backendConfig(state: State) {
const { client, requesterAccount } = state;

const authenticatedClient = client.withCurrentAddress(requesterAccount);
const alice = authenticatedClient.getLegalOfficer(ALICE.address);
const alice = authenticatedClient.getLegalOfficer(ALICE);
const config = await alice.getConfig();

expect(config.features.iDenfy).toBe(false);
Expand Down
Loading
Loading