Skip to content

refactor: use @namada/sdk package in the extension #2288

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

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
5 changes: 0 additions & 5 deletions .github/actions/build-extension/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@ runs:
- name: Install Dependencies
uses: ./.github/actions/prepare-build

- name: Build WASM dependencies
working-directory: ./apps/extension
run: yarn wasm:build
shell: bash

- name: Build ${{ inputs.browser }} extension
working-directory: ./apps/extension
env:
Expand Down
24 changes: 0 additions & 24 deletions .github/workflows/build-extension-demo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,6 @@ jobs:
- name: Install yarn dependencies
uses: ./.github/actions/yarn-cache

- name: Restore Rust cache
uses: ./.github/actions/rust-cache
with:
cache-name: build

- name: Install protoc
run: sudo apt-get install -y protobuf-compiler

- name: Build WASM dependencies
working-directory: ./apps/extension
run: yarn wasm:build

- name: Build the extension
working-directory: ./apps/extension
env:
Expand All @@ -79,18 +67,6 @@ jobs:
- name: Install yarn dependencies
uses: ./.github/actions/yarn-cache

- name: Restore Rust cache
uses: ./.github/actions/rust-cache
with:
cache-name: build

- name: Install protoc
run: sudo apt-get install -y protobuf-compiler

- name: Build WASM dependencies
working-directory: ./apps/extension
run: yarn wasm:build

- name: Build the extension
working-directory: ./apps/extension
env:
Expand Down
37 changes: 0 additions & 37 deletions .github/workflows/verify-pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,24 +56,10 @@ jobs:
- name: Install yarn dependencies
uses: ./.github/actions/yarn-cache

- name: Setup Rust toolchain
run: rustup toolchain add nightly-2025-03-27

- name: Restore Rust cache
uses: ./.github/actions/rust-cache
with:
cache-name: unit-tests-js

- name: Apt update
run: sudo apt update
shell: bash

- name: Install protoc
run: sudo apt-get install -y protobuf-compiler

- name: Compile WASM
run: yarn wasm:build-test

- name: Run unit tests
id: run-unit-tests
run: yarn test:ci
Expand All @@ -89,14 +75,6 @@ jobs:
- name: Install yarn dependencies
uses: ./.github/actions/yarn-cache

- name: Setup Rust toolchain
run: rustup toolchain add nightly-2025-03-27

- name: Restore Rust cache
uses: ./.github/actions/rust-cache
with:
cache-name: build

- name: Apt update
run: sudo apt update
shell: bash
Expand All @@ -123,25 +101,10 @@ jobs:
- name: Install yarn dependencies
uses: ./.github/actions/yarn-cache

- name: Setup Rust toolchain
run: rustup toolchain add nightly-2025-03-27

- name: Restore Rust cache
uses: ./.github/actions/rust-cache
with:
cache-name: build-multicore

- name: Apt update
run: sudo apt update
shell: bash

- name: Install protoc
run: sudo apt-get install -y protobuf-compiler

- name: Build WASM dependencies
working-directory: ./apps/extension
run: yarn wasm:build:multicore

- name: Build the extension
working-directory: ./apps/extension
run: yarn build:chrome
Expand Down
5 changes: 3 additions & 2 deletions apps/extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@
"start:chrome:proxy": "NAMADA_INTERFACE_PROXY=true yarn start:chrome",
"start:firefox": "yarn clean:firefox && NODE_ENV=development TARGET=firefox NAMADA_INTERFACE_REVISION=$(git rev-parse HEAD) yarn watch",
"start:firefox:proxy": "NAMADA_INTERFACE_PROXY=true yarn start:firefox",
"test": "yarn wasm:build:test && yarn jest --coverage",
"test:only": "yarn jest --coverage",
"test": "jest --coverage --runInBand",
"test:watch": "yarn wasm:build:test && yarn jest --watchAll=true",
"test:ci": "jest",
"wasm:build": "node ./scripts/build.js --release",
Expand All @@ -41,6 +40,7 @@
"@dao-xyz/borsh": "^5.1.5",
"@ledgerhq/hw-transport": "^6.31.4",
"@ledgerhq/hw-transport-webusb": "^6.29.4",
"@namada/sdk": "^0.20.6",
"@zondax/ledger-namada": "^2.0.0",
"bignumber.js": "^9.1.1",
"buffer": "^6.0.3",
Expand All @@ -59,6 +59,7 @@
},
"devDependencies": {
"@babel/plugin-transform-modules-commonjs": "^7.20.11",
"@namada/sdk-node": "^0.20.5",
"@svgr/webpack": "^6.3.1",
"@types/chrome": "^0.0.237",
"@types/firefox-webext-browser": "^94.0.1",
Expand Down
7 changes: 0 additions & 7 deletions apps/extension/scripts/build-test.sh

This file was deleted.

2 changes: 1 addition & 1 deletion apps/extension/src/App/Accounts/ViewAccount.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { chains } from "@namada/chains";
import { ActionButton, GapPatterns, Stack, ViewKeys } from "@namada/components";
import { makeBip44Path, makeSaplingPath } from "@namada/sdk/web";
import { makeBip44Path, makeSaplingPath } from "@namada/sdk";
import { AccountType, DerivedAccount } from "@namada/types";
import { PageHeader } from "App/Common";
import routes from "App/routes";
Expand Down
3 changes: 2 additions & 1 deletion apps/extension/src/App/Common/AppHeaderNavigation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { useVaultContext } from "context";
import { FaDiscord, FaXTwitter } from "react-icons/fa6";
import { GoAlert, GoLinkExternal, GoQuestion } from "react-icons/go";
import { useNavigate } from "react-router-dom";
import sdkPackage from "../../../../../packages/sdk/package.json";
// TODO: use Sdk.version after its changed to static
import sdkPackage from "../../../../../node_modules/@namada/sdk/package.json";
import extensionPackage from "../../../package.json";

const { NAMADA_INTERFACE_REVISION: revision = "" } = process.env;
Expand Down
6 changes: 3 additions & 3 deletions apps/extension/src/App/Settings/Network.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { sanitize } from "isomorphic-dompurify";
import dompurify from "isomorphic-dompurify";

import {
ActionButton,
Expand Down Expand Up @@ -37,7 +37,7 @@ export const Network = (): JSX.Element => {
Ports.Background,
new GetChainMsg()
);
const santizedChainId = sanitize(chainId);
const santizedChainId = dompurify.sanitize(chainId);
setChainId(santizedChainId);
} catch (e) {
setErrorMessage(`${e}`);
Expand All @@ -50,7 +50,7 @@ export const Network = (): JSX.Element => {
e.preventDefault();
setStatus(Status.Pending);
setErrorMessage("");
const sanitizedChainId = sanitize(chainId);
const sanitizedChainId = dompurify.sanitize(chainId);

// Validate sanitized chain ID
if (!sanitizedChainId) {
Expand Down
2 changes: 1 addition & 1 deletion apps/extension/src/Approvals/ApproveSignTx.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ActionButton, GapPatterns, Stack } from "@namada/components";
import { useSanitizedParams } from "@namada/hooks";
import { TxType, TxTypeLabel } from "@namada/sdk/web";
import { TxType, TxTypeLabel } from "@namada/sdk";
import {
AccountType,
CommitmentDetailProps,
Expand Down
2 changes: 1 addition & 1 deletion apps/extension/src/Approvals/Commitment.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { TxType } from "@namada/sdk/web";
import { TxType } from "@namada/sdk";
import {
BondProps,
ClaimRewardsProps,
Expand Down
23 changes: 7 additions & 16 deletions apps/extension/src/Approvals/ConfirmSignLedgerTx.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
import { getSdk } from "@namada/sdk/web";
import sdkInit from "@namada/sdk/web-init";
import { initSdk } from "@namada/sdk";
import clsx from "clsx";
import { ReactNode, useCallback, useEffect, useState } from "react";

import { ActionButton, Stack } from "@namada/components";
import {
Ledger,
makeBip44Path,
makeSaplingPath,
TxType,
} from "@namada/sdk/web";
import { Ledger, makeBip44Path, makeSaplingPath, TxType } from "@namada/sdk";
import { LedgerError, ResponseSign } from "@zondax/ledger-namada";

import { fromBase64, toBase64 } from "@cosmjs/encoding";
Expand Down Expand Up @@ -104,15 +98,12 @@ export const ConfirmSignLedgerTx: React.FC<Props> = ({ details }) => {
throw new Error("Shielded hash is required for MASP transactions");
}

const { cryptoMemory } = await sdkInit();
// TODO: Find a better way to init the sdk, token has to be any valid token
const sdk = getSdk(
cryptoMemory,
"",
"",
"",
"tnam1q9gr66cvu4hrzm0sd5kmlnjje82gs3xlfg3v6nu7"
);
const sdk = await initSdk({
rpcUrl: "",
token: "tnam1q9gr66cvu4hrzm0sd5kmlnjje82gs3xlfg3v6nu7",
});

const descriptors = await sdk
.getMasp()
.getDescriptorMap(bytes, fromBase64(shieldedHash));
Expand Down
2 changes: 1 addition & 1 deletion apps/extension/src/Setup/Common/Completion.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import browser from "webextension-polyfill";

import { chains } from "@namada/chains";
import { ActionButton, Alert, Loading, ViewKeys } from "@namada/components";
import { makeBip44Path } from "@namada/sdk/web";
import { makeBip44Path } from "@namada/sdk";
import { Bip44Path } from "@namada/types";
import {
AccountSecret,
Expand Down
11 changes: 7 additions & 4 deletions apps/extension/src/Setup/Ledger/LedgerConnect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import { chains } from "@namada/chains";
import { ActionButton, Alert, Image, Stack } from "@namada/components";
import {
ExtendedViewingKey,
initSdk,
LEDGER_MIN_VERSION_ZIP32,
Ledger as LedgerApp,
makeBip44Path,
makeSaplingPath,
ProofGenerationKey,
PseudoExtendedKey,
} from "@namada/sdk/web";
import initWasm from "@namada/sdk/web-init";
} from "@namada/sdk";
import { Bip44Path, Zip32Path } from "@namada/types";
import { LedgerError } from "@zondax/ledger-namada";
import { LedgerStep } from "Setup/Common";
Expand Down Expand Up @@ -80,8 +80,11 @@ export const LedgerConnect: React.FC<Props> = ({
setCurrentApprovalStep(3);
const { ak, nsk } = await ledger.getProofGenerationKey(path);

// SDK wasm init must be called
await initWasm();
// We only do this to initialize wasm, props don't matter
await initSdk({
rpcUrl: "",
token: "tnam1q9gr66cvu4hrzm0sd5kmlnjje82gs3xlfg3v6nu7",
});

const extendedViewingKey = new ExtendedViewingKey(xfvk);
encodedExtendedViewingKey = extendedViewingKey.encode();
Expand Down
7 changes: 0 additions & 7 deletions apps/extension/src/background/approvals/service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,6 @@ 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 sdkService: jest.Mocked<SdkService>;
Expand Down
2 changes: 1 addition & 1 deletion apps/extension/src/background/keyring/keyring.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
MODIFIED_ZIP32_PATH,
PhraseSize,
ShieldedKeys,
} from "@namada/sdk/web";
} from "@namada/sdk";
import { KVStore } from "@namada/storage";
import {
AccountType,
Expand Down
2 changes: 1 addition & 1 deletion apps/extension/src/background/keyring/messages.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PhraseSize } from "@namada/sdk/web";
import { PhraseSize } from "@namada/sdk";
import {
AccountType,
Bip44Path,
Expand Down
2 changes: 1 addition & 1 deletion apps/extension/src/background/keyring/service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PhraseSize } from "@namada/sdk/web";
import { PhraseSize } from "@namada/sdk";
import { IndexedDBKVStore, KVStore } from "@namada/storage";
import {
AccountType,
Expand Down
2 changes: 1 addition & 1 deletion apps/extension/src/background/keyring/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CryptoRecord } from "@namada/sdk/web";
import { CryptoRecord } from "@namada/sdk";
import { StoredRecord } from "@namada/storage";
import { AccountType, DerivedAccount, Path } from "@namada/types";

Expand Down
26 changes: 9 additions & 17 deletions apps/extension/src/background/sdk/service.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,26 @@
import { Sdk, getSdk } from "@namada/sdk/web";
import sdkInit from "@namada/sdk/web-init";
import { initSdk, Sdk } from "@namada/sdk";

const {
NAMADA_INTERFACE_NAMADA_TOKEN:
defaultTokenAddress = "tnam1qxgfw7myv4dh0qna4hq0xdg6lx77fzl7dcem8h7e",
} = process.env;

// Extension does not care about the MASP indexer - this will map to None in Rust
const MASP_INDEXER_URL = "";
// Extension does not use RPC URL
const RPC_URL = "";

export class SdkService {
private constructor(
private readonly token: string,
private readonly cryptoMemory: WebAssembly.Memory
) {}
private constructor(private readonly sdk: Sdk) {}

static async init(): Promise<SdkService> {
const { cryptoMemory } = await sdkInit();
return new SdkService(defaultTokenAddress, cryptoMemory);
const sdk = await initSdk({
rpcUrl: RPC_URL,
token: defaultTokenAddress,
});

return new SdkService(sdk);
}

getSdk(): Sdk {
return getSdk(
this.cryptoMemory,
RPC_URL,
MASP_INDEXER_URL,
"NOT USED DB NAME",
this.token
);
return this.sdk;
}
}
2 changes: 1 addition & 1 deletion apps/extension/src/background/vault/service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CryptoRecord } from "@namada/sdk/web";
import { CryptoRecord } from "@namada/sdk";
import { KVStore } from "@namada/storage";
import { Result } from "@namada/utils";
import { SdkService } from "background/sdk";
Expand Down
7 changes: 0 additions & 7 deletions apps/extension/src/background/vault/tests/service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,6 @@ import { SessionValues } from "../types";

jest.mock("webextension-polyfill", () => ({}));

// 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())
);

type VaultPublicObj = { id: string; alias: string };
type VaultSensitiveValue = { bar: string };

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

export enum ResetPasswordError {
BadPassword,
Expand Down
Loading
Loading