Skip to content

Commit

Permalink
🎨 (signer): Improve code visibility
Browse files Browse the repository at this point in the history
  • Loading branch information
jiyuzhuang committed Aug 28, 2024
1 parent 11f8f1b commit e5ddcfe
Show file tree
Hide file tree
Showing 19 changed files with 184 additions and 116 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { ExternalPluginDataSource } from "@/external-plugin/data/ExternalPluginD
import { ExternalPluginContextLoader } from "@/external-plugin/domain/ExternalPluginContextLoader";
import { DappInfos } from "@/external-plugin/model/DappInfos";
import { SelectorDetails } from "@/external-plugin/model/SelectorDetails";
import { ClearSignContextType } from "@/shared/model/ClearSignContext";
import { TransactionContext } from "@/shared/model/TransactionContext";
import { TokenDataSource } from "@/token/data/TokenDataSource";

Expand Down Expand Up @@ -135,7 +136,7 @@ describe("ExternalPluginContextLoader", () => {
// THEN
expect(result).toEqual([
{
type: "externalPlugin",
type: ClearSignContextType.EXTERNAL_PLUGIN,
payload: "1234567890",
},
]);
Expand Down Expand Up @@ -164,11 +165,11 @@ describe("ExternalPluginContextLoader", () => {
expect(result).toEqual(
expect.arrayContaining([
{
type: "externalPlugin",
type: ClearSignContextType.EXTERNAL_PLUGIN,
payload: "1234567890",
},
{
type: "token",
type: ClearSignContextType.TOKEN,
payload: "payload-0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
},
]),
Expand Down Expand Up @@ -200,11 +201,11 @@ describe("ExternalPluginContextLoader", () => {
// THEN
expect(result).toEqual([
{
type: "error",
type: ClearSignContextType.ERROR,
error: new Error("error"),
},
{
type: "externalPlugin",
type: ClearSignContextType.EXTERNAL_PLUGIN,
payload: "1234567890",
},
]);
Expand Down Expand Up @@ -234,15 +235,15 @@ describe("ExternalPluginContextLoader", () => {
expect(result).toEqual(
expect.arrayContaining([
{
type: "externalPlugin",
type: ClearSignContextType.EXTERNAL_PLUGIN,
payload: "1234567890",
},
{
type: "token",
type: ClearSignContextType.TOKEN,
payload: "payload-0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
},
{
type: "token",
type: ClearSignContextType.TOKEN,
payload: "payload-0xdAC17F958D2ee523a2206206994597C13D831ec7",
},
]),
Expand Down Expand Up @@ -280,25 +281,25 @@ describe("ExternalPluginContextLoader", () => {
// THEN
expect(result).toEqual([
{
type: "token",
type: ClearSignContextType.TOKEN,
payload: "payload-0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
},
{
type: "token",
type: ClearSignContextType.TOKEN,
payload: "payload-0xdAC17F958D2ee523a2206206994597C13D831ec7",
},
// fromToken.2
{
type: "token",
type: ClearSignContextType.TOKEN,
payload: "payload-0xB8c77482e45F1F44dE1745F52C74426C631bDD52",
},
// fromToken.-1
{
type: "token",
type: ClearSignContextType.TOKEN,
payload: "payload-0xB8c77482e45F1F44dE1745F52C74426C631bDD52",
},
{
type: "externalPlugin",
type: ClearSignContextType.EXTERNAL_PLUGIN,
payload: "1234567890",
},
]);
Expand Down Expand Up @@ -329,11 +330,11 @@ describe("ExternalPluginContextLoader", () => {
// THEN
expect(result).toEqual([
{
type: "error",
type: ClearSignContextType.ERROR,
error: new Error("error"),
},
{
type: "externalPlugin",
type: ClearSignContextType.EXTERNAL_PLUGIN,
payload: "1234567890",
},
]);
Expand Down Expand Up @@ -361,13 +362,13 @@ describe("ExternalPluginContextLoader", () => {
// THEN
expect(result).toEqual([
{
type: "error",
type: ClearSignContextType.ERROR,
error: new Error(
"[ContextModule] ExternalPluginContextLoader: Unable to parse abi",
),
},
{
type: "externalPlugin",
type: ClearSignContextType.EXTERNAL_PLUGIN,
payload: "1234567890",
},
]);
Expand Down Expand Up @@ -395,7 +396,7 @@ describe("ExternalPluginContextLoader", () => {
// THEN
expect(result).toEqual([
{
type: "error",
type: ClearSignContextType.ERROR,
error: new Error(
"[ContextModule] ExternalPluginContextLoader: Unable to get address",
),
Expand Down Expand Up @@ -429,7 +430,7 @@ describe("ExternalPluginContextLoader", () => {
// THEN
expect(result).toEqual([
{
type: "error",
type: ClearSignContextType.ERROR,
error: new RangeError("out of result range"),
},
]);
Expand Down Expand Up @@ -483,31 +484,31 @@ describe("ExternalPluginContextLoader", () => {
// THEN
expect(result).toEqual([
{
type: "token",
type: ClearSignContextType.TOKEN,
payload: "payload-0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
},
{
type: "token",
type: ClearSignContextType.TOKEN,
payload: "payload-0xdAC17F958D2ee523a2206206994597C13D831ec7",
},
{
type: "token",
type: ClearSignContextType.TOKEN,
payload: "payload-0x95aD61b0a150d79219dCF64E1E6Cc01f0B64C4cE",
},
{
type: "token",
type: ClearSignContextType.TOKEN,
payload: "payload-0x95aD61b0a150d79219dCF64E1E6Cc01f0B64C4cE",
},
{
type: "token",
type: ClearSignContextType.TOKEN,
payload: "payload-0xB8c77482e45F1F44dE1745F52C74426C631bDD52",
},
{
type: "token",
type: ClearSignContextType.TOKEN,
payload: "payload-0xB8c77482e45F1F44dE1745F52C74426C631bDD52",
},
{
type: "externalPlugin",
type: ClearSignContextType.EXTERNAL_PLUGIN,
payload: "1234567890",
},
]);
Expand All @@ -528,7 +529,7 @@ describe("ExternalPluginContextLoader", () => {
// THEN
expect(result).toEqual([
{
type: "error",
type: ClearSignContextType.ERROR,
error: new Error("error"),
},
]);
Expand All @@ -547,7 +548,7 @@ describe("ExternalPluginContextLoader", () => {
// THEN
expect(result).toEqual([
{
type: "error",
type: ClearSignContextType.ERROR,
error: new Error("Invalid selector"),
},
]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import { Either, EitherAsync, Left, Right } from "purify-ts";
import type { ExternalPluginDataSource } from "@/external-plugin/data/ExternalPluginDataSource";
import { externalPluginTypes } from "@/external-plugin/di/externalPluginTypes";
import { ContextLoader } from "@/shared/domain/ContextLoader";
import { ClearSignContext } from "@/shared/model/ClearSignContext";
import {
ClearSignContext,
ClearSignContextType,
} from "@/shared/model/ClearSignContext";
import { TransactionContext } from "@/shared/model/TransactionContext";
import type { TokenDataSource } from "@/token/data/TokenDataSource";
import { tokenTypes } from "@/token/di/tokenTypes";
Expand All @@ -25,15 +28,20 @@ export class ExternalPluginContextLoader implements ContextLoader {
this._tokenDataSource = tokenDataSource;
}

async load(transaction: TransactionContext) {
async load(transaction: TransactionContext): Promise<ClearSignContext[]> {
if (!transaction.to || !transaction.data || transaction.data === "0x") {
return [];
}

const selector = transaction.data.slice(0, 10);

if (!isHexaString(selector)) {
return [{ type: "error" as const, error: new Error("Invalid selector") }];
return [
{
type: ClearSignContextType.ERROR,
error: new Error("Invalid selector"),
},
];
}

const eitherDappInfos = await this._externalPluginDataSource.getDappInfos({
Expand All @@ -52,7 +60,7 @@ export class ExternalPluginContextLoader implements ContextLoader {
}

const externalPluginContext: ClearSignContext = {
type: "externalPlugin",
type: ClearSignContextType.EXTERNAL_PLUGIN,
payload: dappInfos.selectorDetails.serializedData.concat(
dappInfos.selectorDetails.signature,
),
Expand All @@ -68,7 +76,10 @@ export class ExternalPluginContextLoader implements ContextLoader {
// but also the externalPluginContext because it is still valid
if (decodedCallData.isLeft()) {
return [
{ type: "error", error: decodedCallData.extract() },
{
type: ClearSignContextType.ERROR,
error: decodedCallData.extract(),
},
externalPluginContext,
];
}
Expand All @@ -93,15 +104,15 @@ export class ExternalPluginContextLoader implements ContextLoader {
// map the payload or the error to a ClearSignContext
const contexts: ClearSignContext[] = tokensPayload.map((eitherToken) =>
eitherToken.caseOf<ClearSignContext>({
Left: (error) => ({ type: "error", error }),
Right: (payload) => ({ type: "token", payload }),
Left: (error) => ({ type: ClearSignContextType.ERROR, error }),
Right: (payload) => ({ type: ClearSignContextType.TOKEN, payload }),
}),
);

return [...contexts, externalPluginContext];
}).caseOf<ClearSignContext[]>({
// parse all errors into ClearSignContext
Left: (error) => [{ type: "error", error }],
Left: (error) => [{ type: ClearSignContextType.ERROR, error }],
Right: (contexts) => contexts,
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Left, Right } from "purify-ts";

import { ForwardDomainDataSource } from "@/forward-domain/data/ForwardDomainDataSource";
import { ForwardDomainContextLoader } from "@/forward-domain/domain/ForwardDomainContextLoader";
import { ClearSignContextType } from "@/shared/model/ClearSignContext";
import { TransactionContext } from "@/shared/model/TransactionContext";

describe("ForwardDomainContextLoader", () => {
Expand Down Expand Up @@ -39,7 +40,7 @@ describe("ForwardDomainContextLoader", () => {

expect(result).toEqual([
{
type: "error",
type: ClearSignContextType.ERROR,
error: new Error(
"[ContextModule] ForwardDomainLoader: invalid domain",
),
Expand All @@ -59,7 +60,7 @@ describe("ForwardDomainContextLoader", () => {

expect(result).toEqual([
{
type: "error",
type: ClearSignContextType.ERROR,
error: new Error(
"[ContextModule] ForwardDomainLoader: invalid domain",
),
Expand All @@ -80,7 +81,7 @@ describe("ForwardDomainContextLoader", () => {

expect(result).toEqual([
{
type: "domainName",
type: ClearSignContextType.DOMAIN_NAME,
payload: "payload",
},
]);
Expand All @@ -103,7 +104,9 @@ describe("ForwardDomainContextLoader", () => {
const result = await loader.load(transaction);

// THEN
expect(result).toEqual([{ type: "error", error: new Error("error") }]);
expect(result).toEqual([
{ type: ClearSignContextType.ERROR, error: new Error("error") },
]);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import { inject, injectable } from "inversify";
import type { ForwardDomainDataSource } from "@/forward-domain/data/ForwardDomainDataSource";
import { forwardDomainTypes } from "@/forward-domain/di/forwardDomainTypes";
import { ContextLoader } from "@/shared/domain/ContextLoader";
import { ClearSignContext } from "@/shared/model/ClearSignContext";
import {
ClearSignContext,
ClearSignContextType,
} from "@/shared/model/ClearSignContext";
import { TransactionContext } from "@/shared/model/TransactionContext";

@injectable()
Expand All @@ -29,7 +32,7 @@ export class ForwardDomainContextLoader implements ContextLoader {
if (!this.isDomainValid(domain)) {
return [
{
type: "error",
type: ClearSignContextType.ERROR,
error: new Error(
"[ContextModule] ForwardDomainLoader: invalid domain",
),
Expand All @@ -45,11 +48,11 @@ export class ForwardDomainContextLoader implements ContextLoader {
return [
payload.caseOf({
Left: (error): ClearSignContext => ({
type: "error",
type: ClearSignContextType.ERROR,
error: error,
}),
Right: (value): ClearSignContext => ({
type: "domainName",
type: ClearSignContextType.DOMAIN_NAME,
payload: value,
}),
}),
Expand Down
Loading

0 comments on commit e5ddcfe

Please sign in to comment.