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

refactor: webwallet trpc #33

Draft
wants to merge 1 commit into
base: develop
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
43 changes: 23 additions & 20 deletions src/connectors/webwallet/helpers/trpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,11 @@ export const setPopupOptions = ({

// TODO: abstract AppRouter in order to have one single source of truth
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor: update the comment

// At the moment, this is needed
const appRouter = t.router({
authorize: t.procedure.output(z.boolean()).mutation(async () => {
return true
}),
connect: t.procedure.mutation(async () => ""),
enable: t.procedure.output(z.string()).mutation(async () => ""),
const starknetRouter = t.router({
enable: t.procedure
.input(StarknetMethodArgumentsSchemas.enable)
.output(z.string())
.mutation(async () => ""),
execute: t.procedure
.input(StarknetMethodArgumentsSchemas.execute)
.output(z.string())
Expand All @@ -63,6 +62,18 @@ const appRouter = t.router({
.input(StarknetMethodArgumentsSchemas.signMessage)
.output(z.string().array())
.mutation(async () => []),
request: t.procedure
.input(StarknetMethodArgumentsSchemas.request)
.output(z.boolean())
.mutation(async () => false),
})

const walletRouter = t.router({
authorize: t.procedure.output(z.boolean()).mutation(async () => false),
connect: t.procedure.output(z.boolean()).mutation(async () => false),
updateModal: t.procedure.subscription(async () => {
return undefined as any
}),
getLoginStatus: t.procedure
.output(
z.object({
Expand All @@ -72,23 +83,15 @@ const appRouter = t.router({
}),
)
.mutation(async () => {
// placeholder
return {
isLoggedIn: true,
isLoggedIn: false,
}
}),
addStarknetChain: t.procedure.mutation((_) => {
throw Error("not implemented")
}),
switchStarknetChain: t.procedure.mutation((_) => {
throw Error("not implemented")
}),
watchAsset: t.procedure.mutation((_) => {
throw Error("not implemented")
}),
updateModal: t.procedure.subscription(async () => {
return
}),
})

const appRouter = t.router({
wallet: walletRouter,
starknet: starknetRouter,
})

export type AppRouter = typeof appRouter
Expand Down
25 changes: 16 additions & 9 deletions src/connectors/webwallet/starknetWindowObject/account.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import type { CreateTRPCProxyClient } from "@trpc/client"
import type { Signature } from "starknet"
import type {
Abi,
AllowArray,
Call,
InvocationsDetails,
InvokeFunctionResponse,
Signature,
} from "starknet"
import {
Account,
AccountInterface,
Expand Down Expand Up @@ -50,11 +57,11 @@ export class MessageAccount extends Account implements AccountInterface {
super(provider, address, new UnimplementedSigner())
}

execute: StarknetMethods["execute"] = async (
calls,
abis,
transactionsDetail,
) => {
async execute(
calls: AllowArray<Call>,
abis?: Abi[],
transactionsDetail?: InvocationsDetails,
): Promise<InvokeFunctionResponse> {
try {
setPopupOptions({
width: EXECUTE_POPUP_WIDTH,
Expand All @@ -74,8 +81,8 @@ export class MessageAccount extends Account implements AccountInterface {
})
}

const txHash = await this.proxyLink.execute.mutate([
calls,
const txHash = await this.proxyLink.starknet.execute.mutate([
calls as any,
abis,
transactionsDetail,
])
Expand All @@ -99,7 +106,7 @@ export class MessageAccount extends Account implements AccountInterface {
height: SIGN_MESSAGE_POPUP_HEIGHT,
location: "/signMessage",
})
return await this.proxyLink.signMessage.mutate([typedData])
return await this.proxyLink.starknet.signMessage.mutate([typedData])
} catch (error) {
if (error instanceof Error) {
throw new Error(error.message)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,30 +44,9 @@ export const getArgentStarknetWindowObject = (
isConnected: false,
provider,
getLoginStatus: () => {
return proxyLink.getLoginStatus.mutate()
},
async request(call) {
switch (call.type) {
case "wallet_addStarknetChain": {
//TODO: add with implementation
//const params = call.params as AddStarknetChainParameters
return await proxyLink.addStarknetChain.mutate()
}
case "wallet_switchStarknetChain": {
//TODO: add with implementation
//const params = call.params as SwitchStarknetChainParameter
return await proxyLink.switchStarknetChain.mutate()
}
case "wallet_watchAsset": {
//TODO: add with implementation
//const params = call.params as WatchAssetParameters
/* return remoteHandle.call("watchAsset", params) */
return await proxyLink.watchAsset.mutate()
}
default:
throw new Error("not implemented")
}
return proxyLink.wallet.getLoginStatus.mutate()
},
request: (call) => proxyLink.starknet.request.mutate(call as any),
async enable(ops) {
if (ops?.starknetVersion !== "v4") {
throw Error("not implemented")
Expand All @@ -79,7 +58,7 @@ export const getArgentStarknetWindowObject = (
height: ENABLE_POPUP_HEIGHT,
location: "/interstitialLogin",
})
const enablePromise = proxyLink.enable.mutate()
const enablePromise = proxyLink.starknet.enable.mutate()
const selectedAddress: string = await enablePromise

await updateStarknetWindowObject(
Expand All @@ -99,7 +78,7 @@ export const getArgentStarknetWindowObject = (
},
async isPreauthorized() {
const { isLoggedIn, isPreauthorized } =
await proxyLink.getLoginStatus.mutate()
await proxyLink.wallet.getLoginStatus.mutate()
return Boolean(isLoggedIn && isPreauthorized)
},
on: (event, handleEvent) => {
Expand Down
81 changes: 41 additions & 40 deletions src/types/window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,44 +68,51 @@ export const StarknetMethodArgumentsSchemas = {
z
.object({
starknetVersion: z
.union([z.literal("v4"), z.literal("v5")])
.union([z.literal("v3"), z.literal("v4")])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same question as @vladutjs here

.optional(),
})
.optional(),
])
.or(z.tuple([])),
addStarknetChain: z.tuple([
z.object({
id: z.string(),
chainId: z.string(),
chainName: z.string(),
rpcUrls: z.array(z.string()).optional(),
nativeCurrency: z
.object({
name: z.string(),
symbol: z.string(),
decimals: z.number(),
})
.optional(),
blockExplorerUrls: z.array(z.string()).optional(),
}),
]),
switchStarknetChain: z.tuple([
z.object({
chainId: z.string(),
}),
]),
watchAsset: z.tuple([
z.object({
type: z.literal("ERC20"),
options: z.object({
address: z.string(),
symbol: z.string().optional(),
decimals: z.number().optional(),
image: z.string().optional(),
name: z.string().optional(),
request: z.tuple([
z.union([
z.object({
type: z.literal("wallet_addStarknetChain"),
params: z.object({
id: z.string(),
chainId: z.string(),
chainName: z.string(),
rpcUrls: z.array(z.string()).optional(),
nativeCurrency: z
.object({
name: z.string(),
symbol: z.string(),
decimals: z.number(),
})
.optional(),
blockExplorerUrls: z.array(z.string()).optional(),
}),
}),
z.object({
type: z.literal("wallet_switchStarknetChain"),
params: z.object({
chainId: z.string(),
}),
}),
}),
z.object({
type: z.literal("wallet_watchAsset"),
params: z.object({
type: z.literal("ERC20"),
options: z.object({
address: z.string(),
symbol: z.string().optional(),
decimals: z.number().optional(),
image: z.string().optional(),
name: z.string().optional(),
}),
}),
}),
]),
]),
execute: z.tuple([
z.array(CallSchema).nonempty().or(CallSchema),
Expand All @@ -125,14 +132,8 @@ export type StarknetMethods = {
enable: (
...args: z.infer<typeof StarknetMethodArgumentsSchemas.enable>
) => Promise<string[]>
addStarknetChain: (
...args: z.infer<typeof StarknetMethodArgumentsSchemas.addStarknetChain>
) => Promise<boolean>
switchStarknetChain: (
...args: z.infer<typeof StarknetMethodArgumentsSchemas.switchStarknetChain>
) => Promise<boolean>
watchAsset: (
...args: z.infer<typeof StarknetMethodArgumentsSchemas.watchAsset>
request: (
...args: z.infer<typeof StarknetMethodArgumentsSchemas.request>
) => Promise<boolean>

execute: (
Expand Down