Skip to content

Commit

Permalink
chore: removed isPreauthorized, selectedAddress, chainId, `isCo…
Browse files Browse the repository at this point in the history
…nnected`
  • Loading branch information
avimak committed Dec 14, 2023
1 parent d152cce commit d8d479b
Show file tree
Hide file tree
Showing 12 changed files with 212 additions and 163 deletions.
48 changes: 24 additions & 24 deletions packages/core/src/StarknetWindowObject.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
export enum StarknetChainId {
SN_MAIN = "0x534e5f4d41494e",
SN_GOERLI = "0x534e5f474f45524c49",
}

export enum Permission {
Accounts = "accounts",
}

type FELT = string

type Call = {
Expand Down Expand Up @@ -51,7 +60,10 @@ export interface TypedData {

export type AccountChangeEventHandler = (accounts?: string[]) => void

export type NetworkChangeEventHandler = (network?: string) => void
export type NetworkChangeEventHandler = (
chainId?: StarknetChainId,
accounts?: string[],
) => void

export type WalletEvents =
| {
Expand Down Expand Up @@ -205,6 +217,11 @@ export interface SwitchStarknetChainParameters {
}

export type RpcMessage =
| {
type: "wallet_getPermissions"
params: never
result: Permission[]
}
| {
type: "wallet_requestAccounts"
params?: RequestAccountsParameters
Expand All @@ -225,6 +242,11 @@ export type RpcMessage =
params: SwitchStarknetChainParameters
result: boolean
}
| {
type: "wallet_requestChainId"
params: never
result: StarknetChainId // returns the chain ID of the current network
}
| {
type: "starknet_addInvokeTransaction"
params: AddInvokeTransactionParameters
Expand All @@ -246,15 +268,14 @@ export type RpcMessage =
result: string[]
}

export interface IStarknetWindowObject {
export interface StarknetWindowObject {
id: string
name: string
version: string
icon: string
request: <T extends RpcMessage>(
call: Omit<T, "result">,
) => Promise<T["result"]>
isPreauthorized: () => Promise<boolean>
on: <E extends WalletEvents>(
event: E["type"],
handleEvent: E["handler"],
Expand All @@ -263,31 +284,10 @@ export interface IStarknetWindowObject {
event: E["type"],
handleEvent: E["handler"],
) => void
selectedAddress?: string
chainId?: string
isConnected: boolean
}

export interface ConnectedStarknetWindowObject extends IStarknetWindowObject {
selectedAddress: string
chainId: string
isConnected: true
}

export interface DisconnectedStarknetWindowObject
extends IStarknetWindowObject {
isConnected: false
}

export type StarknetWindowObject =
| ConnectedStarknetWindowObject
| DisconnectedStarknetWindowObject

declare global {
interface Window {
starknet?: StarknetWindowObject
starknet_braavos?: StarknetWindowObject
starknet_argentX?: StarknetWindowObject
[key: `starknet_${string}`]: StarknetWindowObject | undefined
}
}
30 changes: 15 additions & 15 deletions packages/core/src/__test__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { mockStorageFunction } from "./storage.mock"
import {
ArgentXMock,
BraavosMock,
makeAuthorized,
UnknownWalletAMock,
UnknownWalletBMock,
makePreAuthorized,
} from "./wallet.mock"
import { describe, expect, it } from "vitest"

Expand Down Expand Up @@ -105,25 +105,25 @@ describe("getAvailableWallets()", () => {
})
})

describe("getPreAuthorizedWallets()", () => {
it("should return all preauthorized wallets", async () => {
describe("getAuthorizedWallets()", () => {
it("should return all authorized wallets", async () => {
const sn = getWallet({
"starknet-argent": makePreAuthorized(true)(ArgentXMock),
"starknet-braavos": makePreAuthorized(true)(BraavosMock),
"starknet-argent": makeAuthorized(true)(ArgentXMock),
"starknet-braavos": makeAuthorized(true)(BraavosMock),
})
const preauthorizedWallets = await sn.getPreAuthorizedWallets()
expect(preauthorizedWallets.length).toBe(2)
expect(preauthorizedWallets.map((w) => w.id)).contains(ArgentXMock.id)
expect(preauthorizedWallets.map((w) => w.id)).contains(BraavosMock.id)
const authorizedWallets = await sn.getAuthorizedWallets()
expect(authorizedWallets.length).toBe(2)
expect(authorizedWallets.map((w) => w.id)).contains(ArgentXMock.id)
expect(authorizedWallets.map((w) => w.id)).contains(BraavosMock.id)
})
it("should return one preauthorized wallet", async () => {
it("should return one authorized wallet", async () => {
const sn = getWallet({
"starknet-argent": makePreAuthorized(true)(ArgentXMock),
"starknet-braavos": makePreAuthorized(false)(BraavosMock),
"starknet-argent": makeAuthorized(true)(ArgentXMock),
"starknet-braavos": makeAuthorized(false)(BraavosMock),
})
const preauthorizedWallets = await sn.getPreAuthorizedWallets()
expect(preauthorizedWallets.length).toBe(1)
expect(preauthorizedWallets.map((w) => w.id)).contains(ArgentXMock.id)
const authorizedWallets = await sn.getAuthorizedWallets()
expect(authorizedWallets.length).toBe(1)
expect(authorizedWallets.map((w) => w.id)).contains(ArgentXMock.id)
})
})

Expand Down
32 changes: 16 additions & 16 deletions packages/core/src/__test__/storage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import { mockStorageFunction } from "./storage.mock"
import {
ArgentXMock,
BraavosMock,
makeAuthorized,
makeConnected,
UnknownWalletAMock,
UnknownWalletBMock,
makeConnected,
makePreAuthorized,
} from "./wallet.mock"
import { describe, expect, it } from "vitest"

Expand All @@ -24,17 +24,17 @@ function getWallet(
describe("getLastConnectedWallet()", () => {
it("should return null if no last wallet set", async () => {
const sn = getWallet({
"starknet-walletA": makePreAuthorized(false)(UnknownWalletAMock),
"starknet-walletB": makePreAuthorized(false)(UnknownWalletBMock),
"starknet-walletA": makeAuthorized(false)(UnknownWalletAMock),
"starknet-walletB": makeAuthorized(false)(UnknownWalletBMock),
})
const lastConnectedWallet = await sn.getLastConnectedWallet()
expect(lastConnectedWallet).toBe(null)
})
it("should return null if the last connected wallet is not available", async () => {
const sn = getWallet(
{
"starknet-walletA": makePreAuthorized(false)(UnknownWalletAMock),
"starknet-walletB": makePreAuthorized(false)(UnknownWalletBMock),
"starknet-walletA": makeAuthorized(false)(UnknownWalletAMock),
"starknet-walletB": makeAuthorized(false)(UnknownWalletBMock),
},
mockStorageFunction({
"gsw-last": "braavos",
Expand All @@ -46,8 +46,8 @@ describe("getLastConnectedWallet()", () => {
it("should not return the last connected wallet if not preauthorized", async () => {
const sn = getWallet(
{
"starknet-argentX": makePreAuthorized(false)(ArgentXMock),
"starknet-braavos": makePreAuthorized(false)(BraavosMock),
"starknet-argentX": makeAuthorized(false)(ArgentXMock),
"starknet-braavos": makeAuthorized(false)(BraavosMock),
},
mockStorageFunction({
"gsw-last": "braavos",
Expand All @@ -59,8 +59,8 @@ describe("getLastConnectedWallet()", () => {
it("should return the last connected wallet if still preauthorized", async () => {
const sn = getWallet(
{
"starknet-argentX": makePreAuthorized(false)(ArgentXMock),
"starknet-braavos": makePreAuthorized(true)(BraavosMock),
"starknet-argentX": makeAuthorized(false)(ArgentXMock),
"starknet-braavos": makeAuthorized(true)(BraavosMock),
},
mockStorageFunction({
"gsw-last": "braavos",
Expand All @@ -72,8 +72,8 @@ describe("getLastConnectedWallet()", () => {
it("should return the last connected wallet when last is available", async () => {
const sn = getWallet(
{
"starknet-argentX": makePreAuthorized(true)(ArgentXMock),
"starknet-braavos": makePreAuthorized(true)(BraavosMock),
"starknet-argentX": makeAuthorized(true)(ArgentXMock),
"starknet-braavos": makeAuthorized(true)(BraavosMock),
},
mockStorageFunction({ "gsw-last": "braavos" }),
)
Expand Down Expand Up @@ -106,8 +106,8 @@ describe("getLastConnectedWallet()", () => {
it("should default disable", async () => {
const sn = getWallet(
{
"starknet-argentX": makePreAuthorized(true)(ArgentXMock),
"starknet-braavos": makePreAuthorized(true)(BraavosMock),
"starknet-argentX": makeAuthorized(true)(ArgentXMock),
"starknet-braavos": makeAuthorized(true)(BraavosMock),
},
mockStorageFunction({
"gsw-last": "braavos",
Expand All @@ -123,8 +123,8 @@ describe("getLastConnectedWallet()", () => {
it("should disable with clearLastWallet", async () => {
const sn = getWallet(
{
"starknet-argentX": makePreAuthorized(true)(ArgentXMock),
"starknet-braavos": makePreAuthorized(true)(BraavosMock),
"starknet-argentX": makeAuthorized(true)(ArgentXMock),
"starknet-braavos": makeAuthorized(true)(BraavosMock),
},
mockStorageFunction({
"gsw-last": "braavos",
Expand Down
65 changes: 46 additions & 19 deletions packages/core/src/__test__/wallet.mock.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,72 @@
import type { StarknetWindowObject } from "../StarknetWindowObject"
import { Permission, StarknetWindowObject } from "../StarknetWindowObject"
import wallets from "../discovery"

type WalletMock = Pick<
StarknetWindowObject,
"id" | "name" | "icon" | "isPreauthorized"
>
type WalletMock = Pick<StarknetWindowObject, "id" | "name" | "icon" | "request">

export const UnknownWalletAMock: WalletMock = {
id: "wallet-a",
name: "Wallet A",
icon: "https://avatars.dicebear.com/api/initials/Wallet%20A.svg",
isPreauthorized: async () => false,
request: async () => false,
}
export const UnknownWalletBMock: WalletMock = {
id: "wallet-b",
name: "Wallet B",
icon: "https://avatars.dicebear.com/api/initials/Wallet%20B.svg",
isPreauthorized: async () => false,
request: async () => false,
}

export const ArgentXMock: WalletMock = {
...wallets.find((w) => w.id === "argentX")!,
isPreauthorized: async () => false,
request: async (request) => {
switch (request.type) {
case "wallet_getPermissions":
return []
default:
return undefined as any
}
},
}

export const BraavosMock: WalletMock = {
...wallets.find((w) => w.id === "braavos")!,
isPreauthorized: async () => false,
request: async (request) => {
switch (request.type) {
case "wallet_getPermissions":
return []
default:
return undefined as any
}
},
}

export function makePreAuthorized(isPreauthorized: boolean) {
return (wallet: WalletMock) => ({
...wallet,
isPreauthorized: async () => isPreauthorized,
})
export function makeAuthorized(authorized: boolean) {
return (wallet: WalletMock) =>
({
...wallet,
request: async (request) => {
switch (request.type) {
case "wallet_getPermissions":
return authorized ? [Permission.Accounts] : []
default:
return wallet.request(request)
}
},
} as WalletMock)
}

export function makeConnected(isConnected: boolean) {
return (wallet: WalletMock) => ({
...makePreAuthorized(true)(wallet),
request: async () => [],
isConnected,
})
return (wallet: WalletMock) => {
return {
...makeAuthorized(true)(wallet),
request: async ({ type }) => {
switch (type) {
case "wallet_getPermissions":
return isConnected ? [Permission.Accounts] : []
default:
return []
}
},
} as WalletMock
}
}
Loading

0 comments on commit d8d479b

Please sign in to comment.