Skip to content

Commit

Permalink
Merge pull request #4596 from WalletConnect/fix/metadata-type
Browse files Browse the repository at this point in the history
fix: metadata field should not be required
  • Loading branch information
lukaisailovic authored Jun 10, 2024
2 parents db0dbf8 + 28554fb commit 5c0535a
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 12 deletions.
56 changes: 49 additions & 7 deletions packages/sign-client/test/sdk/client.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { TEST_EMPTY_METADATA, TEST_INVALID_METADATA } from "./../shared/values";
import {
TEST_APP_METADATA_A,
TEST_EMPTY_METADATA,
TEST_INVALID_METADATA,
TEST_WALLET_METADATA,
} from "./../shared/values";
import {
formatJsonRpcError,
formatJsonRpcResult,
Expand Down Expand Up @@ -45,6 +50,19 @@ describe("Sign Client Integration", () => {
await deleteClients({ A: client, B: undefined });
});

it("should not initialize without metadata object", async () => {
const options = TEST_SIGN_CLIENT_OPTIONS;
delete options.metadata;

await expect(
SignClient.init({
...options,
name: "init",
signConfig: { disableRequestQueue: true },
}),
).rejects.toThrowError("name is required value in metadata");
});

it("should not initialize with empty metadata", async () => {
await expect(
SignClient.init({
Expand Down Expand Up @@ -164,8 +182,16 @@ describe("Sign Client Integration", () => {
await deleteClients(clients);
});
it("should emit session_proposal on every pair attempt with same URI as long as the proposal has not yet been approved or rejected", async () => {
const dapp = await SignClient.init({ ...TEST_SIGN_CLIENT_OPTIONS, name: "dapp" });
const wallet = await SignClient.init({ ...TEST_SIGN_CLIENT_OPTIONS, name: "wallet" });
const dapp = await SignClient.init({
...TEST_SIGN_CLIENT_OPTIONS,
name: "dapp",
metadata: TEST_APP_METADATA_A,
});
const wallet = await SignClient.init({
...TEST_SIGN_CLIENT_OPTIONS,
name: "wallet",
metadata: TEST_WALLET_METADATA,
});
const { uri, approval } = await dapp.connect(TEST_CONNECT_PARAMS);
if (!uri) throw new Error("URI is undefined");
expect(uri).to.exist;
Expand Down Expand Up @@ -211,8 +237,16 @@ describe("Sign Client Integration", () => {
await deleteClients({ A: dapp, B: wallet });
});
it("should set `sessionConfig`", async () => {
const dapp = await SignClient.init({ ...TEST_SIGN_CLIENT_OPTIONS, name: "dapp" });
const wallet = await SignClient.init({ ...TEST_SIGN_CLIENT_OPTIONS, name: "wallet" });
const dapp = await SignClient.init({
...TEST_SIGN_CLIENT_OPTIONS,
name: "dapp",
metadata: TEST_APP_METADATA_A,
});
const wallet = await SignClient.init({
...TEST_SIGN_CLIENT_OPTIONS,
name: "wallet",
metadata: TEST_WALLET_METADATA,
});
const { uri, approval } = await dapp.connect(TEST_CONNECT_PARAMS);
if (!uri) throw new Error("URI is undefined");
expect(uri).to.exist;
Expand Down Expand Up @@ -254,8 +288,16 @@ describe("Sign Client Integration", () => {
await deleteClients({ A: dapp, B: wallet });
});
it("should use rejected tag for session_propose", async () => {
const dapp = await SignClient.init({ ...TEST_SIGN_CLIENT_OPTIONS, name: "dapp" });
const wallet = await SignClient.init({ ...TEST_SIGN_CLIENT_OPTIONS, name: "wallet" });
const dapp = await SignClient.init({
...TEST_SIGN_CLIENT_OPTIONS,
name: "dapp",
metadata: TEST_APP_METADATA_A,
});
const wallet = await SignClient.init({
...TEST_SIGN_CLIENT_OPTIONS,
name: "wallet",
metadata: TEST_WALLET_METADATA,
});
const { uri } = await dapp.connect(TEST_CONNECT_PARAMS);
if (!uri) throw new Error("URI is undefined");
expect(uri).to.exist;
Expand Down
7 changes: 7 additions & 0 deletions packages/sign-client/test/shared/values.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,3 +285,10 @@ export const TEST_INVALID_METADATA: SignClientTypes.Metadata = {
url: "",
icons: ["test"],
};

export const TEST_WALLET_METADATA = {
name: "Test Wallet",
description: "Test Wallet for WalletConnect",
url: "https://walletconnect.com/",
icons: ["https://avatars.githubusercontent.com/u/37784886"],
};
2 changes: 1 addition & 1 deletion packages/types/src/sign-client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export declare namespace SignClientTypes {

interface Options extends CoreTypes.Options {
core?: ICore;
metadata: Metadata;
metadata?: Metadata;
signConfig?: SignConfig;
}
}
Expand Down
12 changes: 9 additions & 3 deletions providers/ethereum-provider/src/EthereumProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export interface EthereumRpcConfig {
optionalEvents?: string[];
rpcMap: EthereumRpcMap;
projectId: string;
metadata: Metadata;
metadata?: Metadata;
showQrModal: boolean;
qrModalOptions?: QrModalOptions;
}
Expand Down Expand Up @@ -213,7 +213,7 @@ export type EthereumProviderOptions = {
events?: string[];
optionalEvents?: string[];
rpcMap?: EthereumRpcMap;
metadata: Metadata;
metadata?: Metadata;
showQrModal: boolean;
qrModalOptions?: QrModalOptions;
disableProviderPing?: boolean;
Expand Down Expand Up @@ -559,12 +559,18 @@ export class EthereumProvider implements IEthereumProvider {

protected async initialize(opts: EthereumProviderOptions) {
this.rpc = this.getRpcConfig(opts);

const metadata = this.rpc.metadata;
if (metadata === undefined) {
throw new Error("Metadata field is required");
}

this.chainId = this.rpc.chains.length
? getEthereumChainId(this.rpc.chains)
: getEthereumChainId(this.rpc.optionalChains);
this.signer = await UniversalProvider.init({
projectId: this.rpc.projectId,
metadata: this.rpc.metadata,
metadata,
disableProviderPing: opts.disableProviderPing,
relayUrl: opts.relayUrl,
storageOptions: opts.storageOptions,
Expand Down
4 changes: 3 additions & 1 deletion providers/ethereum-provider/test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
TEST_ETHEREUM_METHODS_REQUIRED,
TEST_ETHEREUM_METHODS_OPTIONAL,
TEST_WALLET_METADATA,
TEST_APP_METADATA_A,
} from "./shared/constants";
import { EthereumProviderOptions } from "../src/EthereumProvider";
import { parseChainId } from "@walletconnect/utils";
Expand Down Expand Up @@ -53,7 +54,7 @@ describe("EthereumProvider", function () {
},
},
disableProviderPing: true,
metadata: TEST_WALLET_METADATA,
metadata: TEST_APP_METADATA_A,
});
walletClient = await WalletClient.init(provider, TEST_WALLET_CLIENT_OPTS);
await provider.connect({
Expand Down Expand Up @@ -564,6 +565,7 @@ describe("EthereumProvider", function () {
chains: [],
optionalChains: [],
showQrModal: false,
metadata: TEST_WALLET_METADATA,
}),
).rejects.toThrowError("No chains specified in either `chains` or `optionalChains`");
});
Expand Down
11 changes: 11 additions & 0 deletions providers/ethereum-provider/test/shared/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,14 @@ export const TEST_ETHEREUM_METHODS_OPTIONAL = [
"personal_sign",
"eth_signTypedData",
];

export const TEST_APP_METADATA_A = {
name: "App A (Proposer)",
description: "Description of Proposer App run by client A",
url: "https://app.a.walletconnect.com",
icons: ["https://avatars.githubusercontent.com/u/37784886"],
redirect: {
universal: "App A (Proposer)",
native: "App A Native (Proposer)",
},
};

0 comments on commit 5c0535a

Please sign in to comment.