Skip to content

Commit 0018e50

Browse files
author
Aleksandar Cakalic
authored
Merge pull request #194 from argentlabs/beta-ww-sdk
Beta - `connectAndSignSession` for `WebWallet`
2 parents 74fa3a7 + 39e783b commit 0018e50

File tree

13 files changed

+175
-31
lines changed

13 files changed

+175
-31
lines changed

.github/workflows/release.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ on:
55
- develop
66
- main
77
- beta-braavos-mobile
8+
- beta-ww-sdk
89
- hotfix\/v[0-9]+.[0-9]+.[0-9]+
910

1011
jobs:

.releaserc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
{
1313
"name": "beta-braavos-mobile",
1414
"prerelease": true
15+
},
16+
{
17+
"name": "beta-ww-sdk",
18+
"prerelease": true
1519
}
1620
],
1721
"plugins": [

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "starknetkit",
3-
"version": "2.7.0",
3+
"version": "2.8.0-beta-ww-sdk.1",
44
"repository": "github:argentlabs/starknetkit",
55
"private": false,
66
"browser": {
@@ -117,7 +117,7 @@
117117
"zod": "^3.20.6"
118118
},
119119
"peerDependencies": {
120-
"starknet": "^6.9.0"
120+
"starknet": "^6.11.0"
121121
},
122122
"gitHead": "b16688a8638cc138938e74e1a39d004760165d75"
123123
}

pnpm-lock.yaml

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export class ConnectAndSignSessionError extends Error {
2+
code: string
3+
4+
constructor(message: string, code: string) {
5+
super(message)
6+
this.name = "ConnectAndSignSessionError"
7+
this.code = code
8+
}
9+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { z } from "zod"
2+
import { CallSchema, typedDataSchema } from "../../../types/window"
3+
4+
export const connectAndSignSessionInputSchema = z.object({
5+
callbackData: z.string().optional(),
6+
approvalRequests: z.array(
7+
z.object({
8+
tokenAddress: z.string(),
9+
amount: z.string(),
10+
spender: z.string(),
11+
}),
12+
),
13+
sessionTypedData: typedDataSchema,
14+
})
15+
16+
export const connectAndSignSessionOutputSchema = z.object({
17+
account: z.string().array().optional(),
18+
chainId: z.string().optional(),
19+
signature: z.string().array().optional(),
20+
approvalTransactionHash: z.string().optional(),
21+
deploymentPayload: z.any().optional(),
22+
approvalRequestsCalls: z.array(CallSchema).optional(),
23+
errorCode: z
24+
.enum([
25+
"USER_REJECTED",
26+
"ACCOUNT_NOT_DEPLOYED",
27+
"NOT_ENOUGH_BALANCE",
28+
"NOT_ENOUGH_BALANCE_DEPLOYMENT",
29+
"GENERIC_ERROR",
30+
])
31+
.optional(),
32+
})
33+
34+
export type ConnectAndSignSessionInput = z.infer<
35+
typeof connectAndSignSessionInputSchema
36+
>
37+
38+
export type ConnectAndSignSessionOutput = z.infer<
39+
typeof connectAndSignSessionOutputSchema
40+
>

src/connectors/webwallet/helpers/trpc.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ import {
1111
deployAccountContractSchema,
1212
} from "../../../types/window"
1313
import { DEFAULT_WEBWALLET_URL } from "../constants"
14+
import {
15+
connectAndSignSessionInputSchema,
16+
connectAndSignSessionOutputSchema,
17+
} from "./schema"
1418

1519
const t = initTRPC.create({
1620
isServer: false,
@@ -84,6 +88,10 @@ const appRouter = t.router({
8488
}),
8589
)
8690
.mutation(async () => ({})),
91+
connectAndSignSession: t.procedure
92+
.input(connectAndSignSessionInputSchema)
93+
.output(connectAndSignSessionOutputSchema)
94+
.mutation(async () => ({})),
8795
enable: t.procedure.output(z.string()).mutation(async () => ""),
8896
execute: t.procedure
8997
.input(StarknetMethodArgumentsSchemas.execute)

src/connectors/webwallet/index.ts

Lines changed: 68 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ import {
55
type RpcTypeToMessageMap,
66
type AccountChangeEventHandler,
77
type StarknetWindowObject,
8+
type TypedData,
89
} from "@starknet-io/types-js"
910
import {
1011
Account,
11-
AccountInterface,
12-
ProviderInterface,
12+
type AccountInterface,
13+
type ProviderInterface,
1314
type ProviderOptions,
1415
} from "starknet"
1516
import {
@@ -28,10 +29,13 @@ import {
2829
import { DEFAULT_WEBWALLET_ICON, DEFAULT_WEBWALLET_URL } from "./constants"
2930
import { openWebwallet } from "./helpers/openWebwallet"
3031
import { setPopupOptions } from "./helpers/trpc"
31-
import type {
32-
Theme,
33-
WebWalletStarknetWindowObject,
32+
import {
33+
type Theme,
34+
type WebWalletStarknetWindowObject,
3435
} from "./starknetWindowObject/argentStarknetWindowObject"
36+
import type { ApprovalRequest } from "./starknetWindowObject/types"
37+
import type { TRPCClientError } from "@trpc/client"
38+
import { ConnectAndSignSessionError } from "./errors"
3539

3640
let _wallet: StarknetWindowObject | null = null
3741
let _address: string | null = null
@@ -57,21 +61,25 @@ export class WebWalletConnector extends Connector {
5761
}
5862

5963
async ready(): Promise<boolean> {
60-
if (!_wallet) {
61-
this._wallet = null
62-
_address = null
63-
return false
64+
if (!this._wallet) {
65+
await this.ensureWallet()
6466
}
6567

66-
this._wallet = _wallet
67-
try {
68-
const permissions = await this._wallet.request({
69-
type: "wallet_getPermissions",
70-
})
68+
if (!this._wallet) {
69+
this._wallet = null
70+
_address = null
7171

72-
return (permissions as Permission[]).includes(Permission.ACCOUNTS)
73-
} catch {
7472
return false
73+
} else {
74+
try {
75+
const permissions = await this._wallet.request({
76+
type: "wallet_getPermissions",
77+
})
78+
79+
return (permissions as Permission[]).includes(Permission.ACCOUNTS)
80+
} catch {
81+
return false
82+
}
7583
}
7684
}
7785

@@ -107,6 +115,48 @@ export class WebWalletConnector extends Connector {
107115
return "Powered by Argent"
108116
}
109117

118+
async connectAndSignSession({
119+
callbackData,
120+
approvalRequests,
121+
sessionTypedData,
122+
}: {
123+
callbackData?: string
124+
approvalRequests: ApprovalRequest[]
125+
sessionTypedData: TypedData
126+
}) {
127+
await this.ensureWallet()
128+
129+
if (!this._wallet) {
130+
throw new ConnectorNotFoundError()
131+
}
132+
133+
try {
134+
return await (
135+
this._wallet as WebWalletStarknetWindowObject
136+
).connectAndSignSession({
137+
callbackData,
138+
approvalRequests,
139+
sessionTypedData,
140+
})
141+
} catch (error) {
142+
if (
143+
error instanceof Error &&
144+
(error.constructor.name === "TRPCClientError" ||
145+
error.name === "TRPCClientError")
146+
) {
147+
const trpcError = error as TRPCClientError<any>
148+
149+
const message =
150+
trpcError.shape.data.webwalletErrorMessage || trpcError.message
151+
const code =
152+
trpcError.shape.data.webwalletErrorCode || trpcError.shape.message
153+
154+
throw new ConnectAndSignSessionError(message, code)
155+
}
156+
throw new Error(error instanceof Error ? error.message : String(error))
157+
}
158+
}
159+
110160
async connect(_args: ConnectArgs = {}): Promise<ConnectorData> {
111161
await this.ensureWallet()
112162

@@ -240,4 +290,5 @@ export class WebWalletConnector extends Connector {
240290
}
241291
}
242292

243-
export type { WebWalletStarknetWindowObject }
293+
export type { WebWalletStarknetWindowObject, ApprovalRequest }
294+
export { ConnectAndSignSessionError }

src/connectors/webwallet/starknetWindowObject/argentStarknetWindowObject.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type {
33
NetworkChangeEventHandler,
44
RpcTypeToMessageMap,
55
StarknetWindowObject,
6+
TypedData,
67
WalletEvents,
78
} from "@starknet-io/types-js"
89
import type { CreateTRPCProxyClient } from "@trpc/client"
@@ -13,7 +14,9 @@ import {
1314
SIGN_MESSAGE_POPUP_HEIGHT,
1415
SIGN_MESSAGE_POPUP_WIDTH,
1516
} from "../helpers/popupSizes"
17+
import type { ConnectAndSignSessionOutput } from "../helpers/schema"
1618
import { setPopupOptions, type AppRouter } from "../helpers/trpc"
19+
import type { ApprovalRequest } from "./types"
1720

1821
export const userEventHandlers: WalletEvents[] = []
1922

@@ -52,6 +55,15 @@ export type WebWalletStarknetWindowObject = StarknetWindowObject & {
5255
account?: string[]
5356
chainId?: string
5457
}>
58+
connectAndSignSession({
59+
callbackData,
60+
approvalRequests,
61+
sessionTypedData,
62+
}: {
63+
callbackData?: string
64+
approvalRequests: ApprovalRequest[]
65+
sessionTypedData: TypedData
66+
}): Promise<ConnectAndSignSessionOutput>
5567
}
5668

5769
export const getArgentStarknetWindowObject = (
@@ -67,6 +79,10 @@ export const getArgentStarknetWindowObject = (
6779
const { theme } = props
6880
return proxyLink.connectWebwallet.mutate({ theme })
6981
},
82+
connectAndSignSession: (props) => {
83+
console.log("connectAndSignSession", props)
84+
return proxyLink.connectAndSignSession.mutate(props)
85+
},
7086
connectWebwalletSSO: (token, authorizedPartyId) => {
7187
return proxyLink.connectWebwalletSSO.mutate({ token, authorizedPartyId })
7288
},
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export type Hex = `0x${string}`
2+
3+
export type Address = Hex
4+
5+
export type ApprovalRequest = {
6+
tokenAddress: Address
7+
amount: string
8+
spender: Address
9+
}

0 commit comments

Comments
 (0)