Skip to content

Commit

Permalink
feat/922: Update namadillo for wasm checksums (#931)
Browse files Browse the repository at this point in the history
* feat: move checksums to sign call, don't persist in extension

* feat: point to indexers for checksums, update docs
  • Loading branch information
jurevans authored Jul 23, 2024
1 parent 7c7f118 commit af9e3a0
Show file tree
Hide file tree
Showing 72 changed files with 430 additions and 697 deletions.
10 changes: 8 additions & 2 deletions apps/extension/src/background/approvals/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,14 @@ const handleRevokeConnectionMsg: (
const handleApproveSignTxMsg: (
service: ApprovalsService
) => InternalHandler<ApproveSignTxMsg> = (service) => {
return async (_, { signer, tx, txType, wrapperTxMsg }) => {
return await service.approveSignTx(txType, signer, tx, wrapperTxMsg);
return async (_, { signer, tx, txType, wrapperTxMsg, checksums }) => {
return await service.approveSignTx(
txType,
signer,
tx,
wrapperTxMsg,
checksums
);
};
};

Expand Down
4 changes: 1 addition & 3 deletions apps/extension/src/background/approvals/service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { LocalStorage } from "storage";
import { KVStoreMock } from "test/init";
import * as webextensionPolyfill from "webextension-polyfill";
import { ApprovalsService } from "./service";
import { PendingTx, WasmHashesStore } from "./types";
import { PendingTx } from "./types";

jest.mock("webextension-polyfill", () => ({
runtime: {
Expand Down Expand Up @@ -46,7 +46,6 @@ describe("approvals service", () => {
let chainService: jest.Mocked<ChainsService>;
let dataStore: KVStoreMock<string>;
let txStore: KVStoreMock<PendingTx>;
let wasmHashesStore: KVStoreMock<WasmHashesStore>;
let localStorage: LocalStorage;

afterEach(() => {
Expand All @@ -69,7 +68,6 @@ describe("approvals service", () => {
txStore,
dataStore,
localStorage,
wasmHashesStore,
sdkService,
keyRingService,
vaultService,
Expand Down
13 changes: 6 additions & 7 deletions apps/extension/src/background/approvals/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { SdkService } from "background/sdk";
import { VaultService } from "background/vault";
import { ExtensionBroadcaster } from "extension";
import { LocalStorage } from "storage";
import { EncodedTxData, PendingTx, WasmHashesStore } from "./types";
import { EncodedTxData, PendingTx } from "./types";

export class ApprovalsService {
// holds promises which can be resolved with a message from a pop-up window
Expand All @@ -28,7 +28,6 @@ export class ApprovalsService {
protected readonly txStore: KVStore<PendingTx>,
protected readonly dataStore: KVStore<string>,
protected readonly localStorage: LocalStorage,
protected readonly wasmHashesStore: KVStore<WasmHashesStore>,
protected readonly sdkService: SdkService,
protected readonly keyRingService: KeyRingService,
protected readonly vaultService: VaultService,
Expand All @@ -40,7 +39,8 @@ export class ApprovalsService {
txType: TxType,
signer: string,
tx: EncodedTxData,
wrapperTxMsg: string
wrapperTxMsg: string,
checksums?: Record<string, string>
): Promise<Uint8Array> {
const msgId = uuid();

Expand All @@ -54,11 +54,13 @@ export class ApprovalsService {
signingDataBytes: tx.signingDataBytes.map((bytes) => fromBase64(bytes)),
};

// Set PendingTx
await this.txStore.set(msgId, {
txType,
tx: pendingTx,
signer,
wrapperTxMsg: fromBase64(wrapperTxMsg),
checksums,
});

const url = `${browser.runtime.getURL(
Expand Down Expand Up @@ -270,11 +272,8 @@ export class ApprovalsService {
throw new Error(`No transaction found for ${msgId}`);
}

const { chainId } = await this.chainService.getChain();
const wasmHashes = await this.wasmHashesStore.get(chainId);

const { tx } = this.sdkService.getSdk();
return tx.deserialize(pendingTx.tx.txBytes, wasmHashes || []);
return tx.deserialize(pendingTx.tx.txBytes, pendingTx.checksums || {});
}

async querySignArbitraryDetails(msgId: string): Promise<string> {
Expand Down
12 changes: 3 additions & 9 deletions apps/extension/src/background/approvals/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { SupportedTx, TxType } from "@heliax/namada-sdk/web";
import { TxData, WasmHash } from "@namada/types";
import { TxType } from "@heliax/namada-sdk/web";
import { TxData } from "@namada/types";

export type ApprovedOriginsStore = string[];

Expand All @@ -8,6 +8,7 @@ export type PendingTx = {
tx: TxData;
wrapperTxMsg: Uint8Array;
signer: string;
checksums?: Record<string, string>;
};

export type PendingSignArbitrary = string;
Expand All @@ -16,10 +17,3 @@ export type EncodedTxData = {
txBytes: string;
signingDataBytes: string[];
};

export type TxStore = {
txType: SupportedTx;
tx: PendingTx[];
};

export type WasmHashesStore = WasmHash[];
32 changes: 1 addition & 31 deletions apps/extension/src/background/chains/handler.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import { GetChainMsg } from "provider/messages";
import { Env, Handler, InternalHandler, Message } from "router";
import {
AddTxWasmHashesMsg,
GetTxWasmHashesMsg,
UpdateChainMsg,
} from "./messages";
import { UpdateChainMsg } from "./messages";
import { ChainsService } from "./service";

export const getHandler: (service: ChainsService) => Handler = (service) => {
Expand All @@ -14,16 +10,6 @@ export const getHandler: (service: ChainsService) => Handler = (service) => {
return handleGetChainMsg(service)(env, msg as GetChainMsg);
case UpdateChainMsg:
return handleUpdateChainMsg(service)(env, msg as UpdateChainMsg);
case AddTxWasmHashesMsg:
return handleAddTxWasmHashesMsg(service)(
env,
msg as AddTxWasmHashesMsg
);
case GetTxWasmHashesMsg:
return handleGetTxWasmHashesMsg(service)(
env,
msg as GetTxWasmHashesMsg
);
default:
throw new Error("Unknown msg type");
}
Expand All @@ -45,19 +31,3 @@ const handleUpdateChainMsg: (
return await service.updateChain(chainId);
};
};

const handleAddTxWasmHashesMsg: (
service: ChainsService
) => InternalHandler<AddTxWasmHashesMsg> = (service) => {
return async (_, { chainId, wasmHashes }) => {
return await service.addTxWasmHashes(chainId, wasmHashes);
};
};

const handleGetTxWasmHashesMsg: (
service: ChainsService
) => InternalHandler<GetTxWasmHashesMsg> = (service) => {
return async (_, { chainId }) => {
return await service.getTxWasmHashes(chainId);
};
};
8 changes: 1 addition & 7 deletions apps/extension/src/background/chains/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,12 @@ import { GetChainMsg } from "provider/messages";
import { Router } from "router";
import { ROUTE } from "./constants";
import { getHandler } from "./handler";
import {
AddTxWasmHashesMsg,
GetTxWasmHashesMsg,
UpdateChainMsg,
} from "./messages";
import { UpdateChainMsg } from "./messages";
import { ChainsService } from "./service";

export function init(router: Router, service: ChainsService): void {
router.registerMessage(GetChainMsg);
router.registerMessage(UpdateChainMsg);
router.registerMessage(AddTxWasmHashesMsg);
router.registerMessage(GetTxWasmHashesMsg);

router.addHandler(ROUTE, getHandler(service));
}
48 changes: 0 additions & 48 deletions apps/extension/src/background/chains/messages.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { WasmHash } from "@namada/types";
import { Message } from "router";
import { validateProps } from "utils";
import { ROUTE } from "./constants";
Expand Down Expand Up @@ -30,50 +29,3 @@ export class UpdateChainMsg extends Message<void> {
return UpdateChainMsg.type();
}
}

export class AddTxWasmHashesMsg extends Message<void> {
public static type(): MessageType {
return MessageType.AddTxWasmHashes;
}

constructor(
public readonly chainId: string,
public readonly wasmHashes: WasmHash[]
) {
super();
}

validate(): void {
validateProps(this, ["chainId", "wasmHashes"]);
}

route(): string {
return ROUTE;
}

type(): string {
return AddTxWasmHashesMsg.type();
}
}

export class GetTxWasmHashesMsg extends Message<WasmHash[] | undefined> {
public static type(): MessageType {
return MessageType.GetTxWasmHashes;
}

constructor(public readonly chainId: string) {
super();
}

validate(): void {
validateProps(this, ["chainId"]);
}

route(): string {
return ROUTE;
}

type(): string {
return GetTxWasmHashesMsg.type();
}
}
16 changes: 1 addition & 15 deletions apps/extension/src/background/chains/service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { chains } from "@namada/chains";
import { KVStore } from "@namada/storage";
import { Chain, WasmHash } from "@namada/types";
import { WasmHashesStore } from "background/approvals";
import { Chain } from "@namada/types";
import { SdkService } from "background/sdk";
import { ExtensionBroadcaster } from "extension";
import { LocalStorage } from "storage";
Expand All @@ -12,7 +10,6 @@ export class ChainsService {
constructor(
protected readonly sdkService: SdkService,
protected readonly localStorage: LocalStorage,
protected readonly wasmHashesStore: KVStore<WasmHashesStore>,
protected readonly broadcaster: ExtensionBroadcaster
) {}

Expand All @@ -34,15 +31,4 @@ export class ChainsService {
await this.localStorage.setChain(chain);
await this.broadcaster.updateNetwork();
}

async addTxWasmHashes(
chainId: string,
wasmHashes: WasmHash[]
): Promise<void> {
await this.wasmHashesStore.set(chainId, wasmHashes);
}

async getTxWasmHashes(chainId: string): Promise<WasmHash[] | undefined> {
return await this.wasmHashesStore.get(chainId);
}
}
11 changes: 1 addition & 10 deletions apps/extension/src/background/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@ import {
} from "extension";
import { KVPrefix, Ports } from "router";
import { LocalStorage, VaultStorage } from "storage";
import {
ApprovalsService,
WasmHashesStore,
init as initApprovals,
} from "./approvals";
import { ApprovalsService, init as initApprovals } from "./approvals";
import { ChainsService, init as initChains } from "./chains";
import { KeyRingService, UtilityStore, init as initKeyRing } from "./keyring";
import { SdkService } from "./sdk/service";
Expand All @@ -35,9 +31,6 @@ const localStorage = new LocalStorage(
//IDB storages
const vaultStorage = new VaultStorage(new IndexedDBKVStore(KVPrefix.IndexedDB));
const utilityStore = new IndexedDBKVStore<UtilityStore>(KVPrefix.Utility);
const wasmHashesStore = new IndexedDBKVStore<WasmHashesStore>(
KVPrefix.WasmHashesStorage
);

// Memory/transient storages
const sessionStore = new SessionKVStore(KVPrefix.SessionStorage);
Expand Down Expand Up @@ -70,7 +63,6 @@ const init = new Promise<void>(async (resolve) => {
const chainsService = new ChainsService(
sdkService,
localStorage,
wasmHashesStore,
broadcaster
);
const keyRingService = new KeyRingService(
Expand All @@ -87,7 +79,6 @@ const init = new Promise<void>(async (resolve) => {
txStore,
dataStore,
localStorage,
wasmHashesStore,
sdkService,
keyRingService,
vaultService,
Expand Down
18 changes: 0 additions & 18 deletions apps/extension/src/provider/InjectedNamada.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import {
SignArbitraryResponse,
SignProps,
VerifyArbitraryProps,
WasmHash,
WasmHashProps,
} from "@namada/types";
import { InjectedProxy } from "./InjectedProxy";
import { Signer } from "./Signer";
Expand Down Expand Up @@ -63,22 +61,6 @@ export class InjectedNamada implements INamada {
return await InjectedProxy.requestMethod<void, Chain>("getChain");
}

public async addTxWasmHashes(props: WasmHashProps): Promise<void> {
return await InjectedProxy.requestMethod<WasmHashProps, void>(
"addTxWasmHashes",
props
);
}

public async getTxWasmHashes(
chainId: string
): Promise<WasmHash[] | undefined> {
return await InjectedProxy.requestMethod<string, WasmHash[] | undefined>(
"getTxWasmHashes",
chainId
);
}

public getSigner(): ISigner | undefined {
return new Signer(this);
}
Expand Down
Loading

0 comments on commit af9e3a0

Please sign in to comment.