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

feat: add webwallet connect and sign method #185

Merged
merged 2 commits into from
Feb 5, 2025
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@
"zod": "^3.20.6"
},
"peerDependencies": {
"starknet": "^6.9.0"
"starknet": "^6.11.0"
},
"gitHead": "b16688a8638cc138938e74e1a39d004760165d75"
}
6 changes: 3 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions src/connectors/webwallet/helpers/trpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
RpcCallsArraySchema,
StarknetMethodArgumentsSchemas,
deployAccountContractSchema,
typedDataSchema,
} from "../../../types/window"
import { DEFAULT_WEBWALLET_URL } from "../constants"

Expand Down Expand Up @@ -84,6 +85,31 @@ const appRouter = t.router({
}),
)
.mutation(async () => ({})),
connectAndSignSession: t.procedure
//.input(z.any())
.input(
z.object({
callbackData: z.string().optional(),
approvalRequests: z.array(
z.object({
tokenAddress: z.string(),
amount: z.string(),
spender: z.string(),
}),
),
sessionTypedData: typedDataSchema,
}),
)
.output(
z.object({
account: z.string().array().optional(),
chainId: z.string().optional(),
signature: z.string().array().optional(),
approvalTransactionHash: z.string().optional(),
deploymentPayload: z.any().optional(),
}),
)
.mutation(async () => ({})),
enable: t.procedure.output(z.string()).mutation(async () => ""),
execute: t.procedure
.input(StarknetMethodArgumentsSchemas.execute)
Expand Down
32 changes: 29 additions & 3 deletions src/connectors/webwallet/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import {
type RpcTypeToMessageMap,
type AccountChangeEventHandler,
type StarknetWindowObject,
type TypedData,
} from "@starknet-io/types-js"
import {
Account,
AccountInterface,
ProviderInterface,
type AccountInterface,
type ProviderInterface,
type ProviderOptions,
} from "starknet"
import {
Expand All @@ -32,6 +33,7 @@ import {
type Theme,
type WebWalletStarknetWindowObject,
} from "./starknetWindowObject/argentStarknetWindowObject"
import type { ApprovalRequest } from "./starknetWindowObject/types"

let _wallet: StarknetWindowObject | null = null
let _address: string | null = null
Expand Down Expand Up @@ -111,6 +113,30 @@ export class WebWalletConnector extends Connector {
return "Powered by Argent"
}

async connectAndSignSession({
callbackData,
approvalRequests,
sessionTypedData,
}: {
callbackData?: string
approvalRequests: ApprovalRequest[]
sessionTypedData: TypedData
}) {
await this.ensureWallet()

if (!this._wallet) {
throw new ConnectorNotFoundError()
}

return await (
this._wallet as WebWalletStarknetWindowObject
).connectAndSignSession({
callbackData,
approvalRequests,
sessionTypedData,
})
}

async connect(_args: ConnectArgs = {}): Promise<ConnectorData> {
await this.ensureWallet()

Expand Down Expand Up @@ -244,4 +270,4 @@ export class WebWalletConnector extends Connector {
}
}

export type { WebWalletStarknetWindowObject }
export type { WebWalletStarknetWindowObject, ApprovalRequest }
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type {
NetworkChangeEventHandler,
RpcTypeToMessageMap,
StarknetWindowObject,
TypedData,
WalletEvents,
} from "@starknet-io/types-js"
import type { CreateTRPCProxyClient } from "@trpc/client"
Expand All @@ -14,6 +15,7 @@ import {
SIGN_MESSAGE_POPUP_WIDTH,
} from "../helpers/popupSizes"
import { setPopupOptions, type AppRouter } from "../helpers/trpc"
import type { ApprovalRequest } from "./types"

export const userEventHandlers: WalletEvents[] = []

Expand Down Expand Up @@ -52,6 +54,21 @@ export type WebWalletStarknetWindowObject = StarknetWindowObject & {
account?: string[]
chainId?: string
}>
connectAndSignSession({
callbackData,
approvalRequests,
sessionTypedData,
}: {
callbackData?: string
approvalRequests: ApprovalRequest[]
sessionTypedData: TypedData
}): Promise<{
account?: string[]
chainId?: string
signature?: string[]
approvalTransactionHash?: string
deploymentPayload?: any
}>
}

export const getArgentStarknetWindowObject = (
Expand All @@ -67,6 +84,10 @@ export const getArgentStarknetWindowObject = (
const { theme } = props
return proxyLink.connectWebwallet.mutate({ theme })
},
connectAndSignSession: (props) => {
console.log("connectAndSignSession", props)
return proxyLink.connectAndSignSession.mutate(props)
},
connectWebwalletSSO: (token, authorizedPartyId) => {
return proxyLink.connectWebwalletSSO.mutate({ token, authorizedPartyId })
},
Expand Down
9 changes: 9 additions & 0 deletions src/connectors/webwallet/starknetWindowObject/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export type Hex = `0x${string}`

export type Address = Hex

export type ApprovalRequest = {
tokenAddress: Address
amount: string
spender: Address
}
2 changes: 1 addition & 1 deletion src/types/window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export const typedDataSchema = z.object({
),
primaryType: z.string(),
domain: z.record(z.unknown()),
message: z.record(z.unknown()),
message: z.record(z.unknown()).or(z.object({})),
})

export const AssetSchema = z.object({
Expand Down