Skip to content
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

Allow optional params for requests #38

Merged
merged 1 commit into from
Jan 13, 2024
Merged
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: 5 additions & 0 deletions packages/dapp-example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ const App = () => {
params: [],
})) as string[];

// also make sure it doesn't crash when no `params` is specified. This is allowed by EIP-1193
(await injectedProvider.request({
method: "eth_requestAccounts",
})) as string[];

alert(result);
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@ import { JSON_RPC_ERROR_CODES, ProviderRpcError } from "../errors";
import { CustomMethod, MilkomedaProvider, RequestArguments } from "../types";
import { getActorAddress } from "../utils";

const InputSchema = z.union([
z.tuple([]),
z.tuple([
z.string().refine((salt) => ethers.utils.isHexString(salt, 32), { message: "Invalid salt" }),
]),
]);
const InputSchema = z
.union([
z.tuple([]),
z.tuple([
z.string().refine((salt) => ethers.utils.isHexString(salt, 32), { message: "Invalid salt" }),
]),
])
.optional();

/**
* @dev Requests cardano address from the algorand wallet and transforms it to the Actor address
* @dev Requests algorand address from the algorand wallet and transforms it to the Actor address
*/
const eth_accounts: CustomMethod = async (
provider: MilkomedaProvider,
Expand All @@ -37,7 +39,7 @@ const eth_accounts: CustomMethod = async (
try {
if (!peraWallet.isConnected || algorandAccounts.length === 0) return [];

const [salt] = InputSchema.parse(params);
const [salt] = InputSchema.parse(params) ?? [];

const [address] = algorandAccounts;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@ import { JSON_RPC_ERROR_CODES, ProviderRpcError } from "../errors";
import { CustomMethod, MilkomedaProvider, RequestArguments } from "../types";
import { getActorAddress } from "../utils";

const InputSchema = z.union([
z.tuple([]),
z.tuple([
z.string().refine((salt) => ethers.utils.isHexString(salt, 32), { message: "Invalid salt" }),
]),
]);
const InputSchema = z
.union([
z.tuple([]),
z.tuple([
z.string().refine((salt) => ethers.utils.isHexString(salt, 32), { message: "Invalid salt" }),
]),
])
.optional();

/**
* @dev Requests cardano address from the algorand wallet and transforms it to the Actor address
* @dev Requests cardano address from the Algorand wallet and transforms it to the Actor address
*/
const eth_requestAccounts: CustomMethod = async (
provider: MilkomedaProvider,
Expand All @@ -35,7 +37,7 @@ const eth_requestAccounts: CustomMethod = async (
}

try {
const [salt] = InputSchema.parse(params);
const [salt] = InputSchema.parse(params) ?? [];

const [address] =
provider.algorandAccounts.length === 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import { JSON_RPC_ERROR_CODES, ProviderRpcError } from "../errors";
import { CustomMethod, MilkomedaProvider, RequestArguments } from "../types";
import { getActorAddress } from "../utils";

const InputSchema = z.union([
z.tuple([]),
z.tuple([
z.string().refine((salt) => ethers.utils.isHexString(salt, 32), { message: "Invalid salt" }),
]),
]);
const InputSchema = z
.union([
z.tuple([]),
z.tuple([
z.string().refine((salt) => ethers.utils.isHexString(salt, 32), { message: "Invalid salt" }),
]),
])
.optional();

/**
* @dev Requests cardano address from injected cardano provider and transforms it to the Actor address
Expand All @@ -38,7 +40,7 @@ const eth_accounts: CustomMethod = async (
return [];
}

const [salt] = InputSchema.parse(params);
const [salt] = InputSchema.parse(params) ?? [];

// After the page refresh the object needs to be enabled again
await window.cardano.enable();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@ import { JSON_RPC_ERROR_CODES, ProviderRpcError } from "../errors";
import { CustomMethod, MilkomedaProvider, RequestArguments } from "../types";
import { getActorAddress } from "../utils";

const InputSchema = z.union([
z.tuple([]),
z.tuple([
z.string().refine((salt) => ethers.utils.isHexString(salt, 32), { message: "Invalid salt" }),
]),
]);
const InputSchema = z
.union([
z.tuple([]),
z.tuple([
z.string().refine((salt) => ethers.utils.isHexString(salt, 32), { message: "Invalid salt" }),
]),
])
.optional();

/**
* @dev Requests cardano address from injected cardano provider and transforms it to the Actor address
* @dev Requests Cardano address from injected Cardano provider and transforms it to the Actor address
*/
const eth_requestAccounts: CustomMethod = async (
provider: MilkomedaProvider,
Expand All @@ -34,7 +36,7 @@ const eth_requestAccounts: CustomMethod = async (
}

try {
const [salt] = InputSchema.parse(params);
const [salt] = InputSchema.parse(params) ?? [];

await window.cardano.enable();

Expand Down
6 changes: 6 additions & 0 deletions packages/milkomeda-wsc-provider/src/errors.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/**
* Errors must be `ProviderRpcError` as per EIP1193
* https://eips.ethereum.org/EIPS/eip-1193#errors
*/
export class ProviderRpcError extends Error {
public name = "ProviderRpcError";

Expand All @@ -19,6 +23,8 @@ export const JSON_RPC_ERROR_CODES = {
METHOD_NOT_SUPPORTED: -32004,
LIMIT_EXCEEDED: -32005,
JSON_RPC_VERSION_NOT_SUPPORTED: -32006,
// errors codes below come from the EIP-1193 standard
// https://eips.ethereum.org/EIPS/eip-1193#rpc-errors
USER_REJECTED_REQUEST: 4001,
UNAUTHORIZED: 4100,
UNSUPPORTED_METHOD: 4200,
Expand Down
16 changes: 16 additions & 0 deletions packages/milkomeda-wsc-provider/src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ export const PROVIDER_TYPES = {
} as const;
export type ProviderType = (typeof PROVIDER_TYPES)[keyof typeof PROVIDER_TYPES];

/**
* A wrapper for EIP-1193
* On top of its custom functions, you can use it as a standard EIP-1193 object
* ```ts
* await wscProvider.request({
* method: "eth_requestAccounts",
* params: [],
* })
* ```
*/
class Provider extends EventEmitter implements MilkomedaProvider {
private readonly methods: { [key: string]: CustomMethod };

Expand Down Expand Up @@ -52,13 +62,19 @@ class Provider extends EventEmitter implements MilkomedaProvider {

this.actorVersion = actorVersion;

// required to emit `connect` as per EIP1193
// https://eips.ethereum.org/EIPS/eip-1193#connect-1
this.emit("connect");
}

async changeActorVersion(actorVersion: number): Promise<void> {
await this.setup(actorVersion);
}

/**
* request method from EIP-1193
* https://eips.ethereum.org/EIPS/eip-1193#request-1
*/
async request(payload: RequestArguments): Promise<unknown> {
if (payload.method in this.methods) {
return this.methods[payload.method](this, payload);
Expand Down
Loading