Skip to content

Commit

Permalink
feat: sdk js publish (#665)
Browse files Browse the repository at this point in the history
* feat: working node example

* refactor: wasm build scripts for nodejs

* feat: build both sdk versions

* feat: correct d.ts file hopefully

* chore: indexWeb and indexNode cleanup

* refactor(extension): replace shared with sdk

* chore: sdk tsc build for node and web

* fix: sdk init functions in mocks

* fix: sdk tests after rebase

* fix: examples after rebase

* chore: cleanup path utils

* feat: release-it for sdk

* feat: release it changes

* feat: add focused yarn install before builds

* docs: typedoc instead of jsdoc2md

* feat: nodejs shielded context and wallet support

* fix: pr after rebase

* fix: code review changes

* feat(sdk): add multicore support

* fix: correct transfer source when unshielding

* fix: cr comments
  • Loading branch information
mateuszjasiuk authored Apr 4, 2024
1 parent aa96478 commit ec3dec8
Show file tree
Hide file tree
Showing 127 changed files with 3,802 additions and 2,218 deletions.
6 changes: 5 additions & 1 deletion .lintstagedrc.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
{
"*.{js,jsx,ts,tsx}": ["prettier --write", "eslint --fix"]
"*.{js,jsx,ts,tsx,json}": ["prettier --write"],
"./apps/**/src/*.{js,jsx,ts,tsx}": ["eslint --fix --max-warnings=0"],
"./packages/**/src/*.{js,jsx,ts,tsx}": ["eslint --fix --max-warnings=0"],
"./storybook/src/*.{js,jsx,ts,tsx}": ["eslint --fix --max-warnings=0"],
"./e2e/src/*.{js,jsx,ts,tsx}": ["eslint --fix --max-warnings=0"]
}
3 changes: 0 additions & 3 deletions apps/airdrop/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@
"lint:fix": "npx yarn lint -- --fix"
},
"dependencies": {
"@namada/components": "0.2.1",
"@namada/hooks": "0.2.1",
"@namada/utils": "0.2.1",
"buffer": "^6.0.3",
"dompurify": "^3.0.2",
"gsap": "^3.12.2",
Expand Down
5 changes: 0 additions & 5 deletions apps/extension/.release-it.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@ const baseConfig = require("../../.release-it.base.cjs");

const config = {
...baseConfig,
hooks: {
"after:bump": [
"yarn bump -p chains -p components -p crypto -p shared -p storage -p types -p utils",
],
},
};

module.exports = config;
15 changes: 4 additions & 11 deletions apps/extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,21 @@
"start:chrome:proxy": "NAMADA_INTERFACE_PROXY=true yarn start:chrome",
"start:firefox": "yarn clean:firefox && NODE_ENV=development TARGET=firefox yarn watch",
"start:firefox:proxy": "NAMADA_INTERFACE_PROXY=true yarn start:firefox",
"test": "./scripts/build-node.sh && yarn jest --coverage",
"test:watch": "./scripts/build-node.sh && yarn jest --watchAll=true",
"test": "yarn wasm:build:test && yarn jest --coverage",
"test:watch": "yarn wasm:build:test && yarn jest --watchAll=true",
"test:ci": "jest",
"wasm:build": "node ./scripts/build.js --release",
"wasm:build:multicore": "node ./scripts/build.js --release --multicore",
"wasm:build:dev": "node ./scripts/build.js",
"wasm:build:dev:multicore": "node ./scripts/build.js --multicore"
"wasm:build:dev:multicore": "node ./scripts/build.js --multicore",
"wasm:build:test": "./scripts/build-test.sh"
},
"dependencies": {
"@cosmjs/encoding": "^0.29.0",
"@dao-xyz/borsh": "^5.1.5",
"@ledgerhq/hw-transport": "^6.30.0",
"@ledgerhq/hw-transport-webhid": "^6.28.0",
"@ledgerhq/hw-transport-webusb": "^6.28.0",
"@namada/chains": "0.2.1",
"@namada/components": "0.2.1",
"@namada/crypto": "0.2.1",
"@namada/shared": "0.2.1",
"@namada/storage": "0.2.1",
"@namada/types": "0.2.1",
"@namada/utils": "0.2.1",
"@zondax/ledger-namada": "^0.0.4",
"bignumber.js": "^9.1.1",
"buffer": "^6.0.3",
Expand All @@ -65,7 +59,6 @@
},
"devDependencies": {
"@babel/plugin-transform-modules-commonjs": "^7.20.11",
"@namada/config": "workspace:^",
"@svgr/webpack": "^6.3.1",
"@types/chrome": "^0.0.237",
"@types/dompurify": "^3.0.2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
SCRIPT_DIR=$(cd "$(dirname "$0")"; pwd -P)
PACKAGES_PATH="../../../packages"

cd "${SCRIPT_DIR}/${PACKAGES_PATH}/crypto" && yarn wasm:build:node
cd "${SCRIPT_DIR}/${PACKAGES_PATH}/shared" && yarn wasm:build:node
cd "${SCRIPT_DIR}/${PACKAGES_PATH}/crypto" && yarn wasm:build:node:dev
cd "${SCRIPT_DIR}/${PACKAGES_PATH}/shared" && yarn wasm:build:node:dev
2 changes: 1 addition & 1 deletion apps/extension/src/App/Accounts/AddAccount.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { useNavigate } from "react-router-dom";

import { chains } from "@namada/chains";
import { ActionButton, Input, Toggle } from "@namada/components";
import { makeBip44Path } from "@namada/sdk/web";
import { AccountType, DerivedAccount } from "@namada/types";
import { makeBip44Path } from "@namada/utils";

import { TopLevelRoute } from "App/types";
import { AddLedgerAccountMsg, DeriveAccountMsg } from "background/keyring";
Expand Down
2 changes: 1 addition & 1 deletion apps/extension/src/Approvals/Approvals.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, { useState } from "react";
import { Route, Routes } from "react-router-dom";

import { Container } from "@namada/components";
import { TxType } from "@namada/shared";
import { TxType } from "@namada/sdk/web";

import { AppHeader } from "App/Common/AppHeader";
import { TopLevelRoute } from "Approvals/types";
Expand Down
2 changes: 1 addition & 1 deletion apps/extension/src/Approvals/ApproveTx/ApproveTx.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useNavigate } from "react-router-dom";

import { ActionButton, Alert, Stack } from "@namada/components";
import { useSanitizedParams } from "@namada/hooks";
import { TxType, TxTypeLabel } from "@namada/shared";
import { TxType, TxTypeLabel } from "@namada/sdk/web";
import { AccountType, Tokens } from "@namada/types";
import { shortenAddress } from "@namada/utils";
import { ApprovalDetails } from "Approvals/Approvals";
Expand Down
2 changes: 1 addition & 1 deletion apps/extension/src/Approvals/ApproveTx/ConfirmLedgerTx.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import BigNumber from "bignumber.js";
import { useCallback, useEffect, useState } from "react";

import { ActionButton, Alert, Stack } from "@namada/components";
import { TxType, TxTypeLabel } from "@namada/shared";
import { TxType, TxTypeLabel } from "@namada/sdk/web";
import { Message, TxMsgValue, TxProps } from "@namada/types";
import { LedgerError } from "@zondax/ledger-namada";
import { ApprovalDetails, Status } from "Approvals/Approvals";
Expand Down
2 changes: 1 addition & 1 deletion apps/extension/src/Approvals/ApproveTx/ConfirmTx.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useCallback, useEffect, useState } from "react";
import { useNavigate } from "react-router-dom";

import { ActionButton, Alert, Input, Stack } from "@namada/components";
import { SupportedTx, TxType, TxTypeLabel } from "@namada/shared";
import { SupportedTx, TxType, TxTypeLabel } from "@namada/sdk/web";
import { shortenAddress } from "@namada/utils";
import { ApprovalDetails, Status } from "Approvals/Approvals";
import { SubmitApprovedTxMsg } from "background/approvals";
Expand Down
2 changes: 1 addition & 1 deletion apps/extension/src/background/approvals/handler.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { TxType } from "@namada/shared";
import { TxType } from "@namada/sdk/web";
import { AccountType } from "@namada/types";
import createMockInstance from "jest-create-mock-instance";
import {
Expand Down
2 changes: 1 addition & 1 deletion apps/extension/src/background/approvals/messages.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { TxType } from "@namada/shared";
import { TxType } from "@namada/sdk/web";
import { ROUTE } from "./constants";
import {
ConnectInterfaceResponseMsg,
Expand Down
2 changes: 1 addition & 1 deletion apps/extension/src/background/approvals/messages.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SupportedTx } from "@namada/shared";
import { SupportedTx } from "@namada/sdk/web";
import { Message } from "router";
import { ROUTE } from "./constants";

Expand Down
9 changes: 8 additions & 1 deletion apps/extension/src/background/approvals/service.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import * as borsh from "@dao-xyz/borsh";
import { TxType } from "@namada/shared";
import { TxType } from "@namada/sdk/web";
import {
AccountType,
BondMsgValue,
Expand Down Expand Up @@ -42,6 +42,13 @@ jest.mock("@namada/utils", () => {
};
});

// Because we run tests in node environment, we need to mock web-init as node-init
jest.mock(
"@namada/sdk/web-init",
() => () =>
Promise.resolve(jest.requireActual("@namada/sdk/node-init").default())
);

describe("approvals service", () => {
let service: ApprovalsService;
let keyRingService: jest.Mocked<KeyRingService>;
Expand Down
2 changes: 1 addition & 1 deletion apps/extension/src/background/approvals/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import BigNumber from "bignumber.js";
import { v4 as uuid } from "uuid";
import browser, { Windows } from "webextension-polyfill";

import { SupportedTx, TxType } from "@namada/shared";
import { SupportedTx, TxType } from "@namada/sdk/web";
import { KVStore } from "@namada/storage";
import {
AccountType,
Expand Down
2 changes: 1 addition & 1 deletion apps/extension/src/background/approvals/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SupportedTx } from "@namada/shared";
import { SupportedTx } from "@namada/sdk/web";

export type ApprovedOriginsStore = string[];

Expand Down
9 changes: 6 additions & 3 deletions apps/extension/src/background/chains/service.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { chains } from "@namada/chains";
import { Query } from "@namada/shared";
import { Chain } from "@namada/types";
import { SdkService } from "background/sdk";
import { ExtensionBroadcaster } from "extension";
import { LocalStorage } from "storage";

export const CHAINS_KEY = "chains";

export class ChainsService {
constructor(
protected readonly sdkService: SdkService,
protected readonly localStorage: LocalStorage,
protected readonly broadcaster: ExtensionBroadcaster
) {
Expand All @@ -16,9 +17,9 @@ export class ChainsService {
}

private async _queryNativeToken(chain: Chain): Promise<Chain> {
const query = new Query(chain.rpc);
try {
const nativeToken = await query.query_native_token();
const nativeToken = await this.sdkService.getSdk().rpc.queryNativeToken();

if (nativeToken) {
chain.currency.address = nativeToken;
}
Expand Down Expand Up @@ -59,6 +60,8 @@ export class ChainsService {
chainId,
rpc: url,
};

this.sdkService.syncChain(chain);
await this.localStorage.setChain(await this._queryNativeToken(chain));
await this.broadcaster.updateNetwork();
}
Expand Down
23 changes: 8 additions & 15 deletions apps/extension/src/background/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { init as initCrypto } from "@namada/crypto/src/init";
import { init as initShared } from "@namada/shared/src/init";
import {
ExtensionKVStore,
IndexedDBKVStore,
Expand Down Expand Up @@ -54,19 +52,12 @@ router.addGuard(ExtensionGuards.checkOriginIsValid);
router.addGuard(ExtensionGuards.checkMessageIsInternal);

const init = new Promise<void>(async (resolve) => {
const cryptoWasm = await fetch("crypto.namada.wasm").then((wasm) =>
wasm.arrayBuffer()
);
const { memory: cryptoMemory } = await initCrypto(cryptoWasm);

const sharedWasm = await fetch("shared.namada.wasm").then((wasm) =>
wasm.arrayBuffer()
);
await initShared(sharedWasm);

const routerId = await getNamadaRouterId(localStorage);
const requester = new ExtensionRequester(messenger, routerId);
const broadcaster = new ExtensionBroadcaster(localStorage, requester);
const sdkService = await SdkService.init(localStorage);

const { cryptoMemory } = sdkService.getSdk();

const vaultService = new VaultService(
vaultStorage,
Expand All @@ -75,16 +66,18 @@ const init = new Promise<void>(async (resolve) => {
broadcaster
);
await vaultService.initialize();
const chainsService = new ChainsService(localStorage, broadcaster);
const sdkService = new SdkService(chainsService);
const chainsService = new ChainsService(
sdkService,
localStorage,
broadcaster
);
const keyRingService = new KeyRingService(
vaultService,
sdkService,
chainsService,
utilityStore,
localStorage,
vaultStorage,
cryptoMemory,
requester,
broadcaster
);
Expand Down
Loading

0 comments on commit ec3dec8

Please sign in to comment.