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

release 1.1.3 #57

Merged
merged 26 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
799743b
feat: add starknetkit hook and return connector instead of wallets
bluecco Dec 7, 2023
328e015
chore: refactor useStarknetkitConnectors params
bluecco Dec 12, 2023
662d7d9
Merge branch 'develop' into BLO-1891
bluecco Dec 12, 2023
1e5cdc6
chore: refactor useStarknetkitConnector
bluecco Dec 12, 2023
8bf4047
Merge branch 'develop' into BLO-1891
bluecco Dec 13, 2023
37121fb
fix: default to mainnet for argent mobile
bluecco Jan 9, 2024
36ff10f
Merge pull request #56 from argentlabs/fix/argent-mobile-chainId
bluecco Jan 9, 2024
5e1f142
chore(release): 1.0.30 [skip ci]
semantic-release-bot Jan 9, 2024
c5f81f0
fix: implement argent mobile connector isPreauthorized method
bluecco Jan 10, 2024
4cc7157
chore: refactor argent mobile isPreauthorized
bluecco Jan 15, 2024
5186278
chore: refactor argent mobile isPreauthorized
bluecco Jan 15, 2024
67bf972
Merge pull request #58 from argentlabs/fix/argent-mobile-isPreauthorized
bluecco Jan 15, 2024
3874f74
Merge pull request #38 from argentlabs/BLO-1891
bluecco Jan 15, 2024
e88e55f
chore(release): 1.1.0 [skip ci]
semantic-release-bot Jan 15, 2024
21de91f
fix: throw UserRejectedRequestError for mobile connector when user re…
bluecco Jan 17, 2024
e2db8ef
Merge pull request #59 from argentlabs/fix/argent-mobile-connect-reject
bluecco Jan 17, 2024
79c7a1f
chore(release): 1.1.1 [skip ci]
semantic-release-bot Jan 17, 2024
c3a5169
fix: use rpc public urls
bluecco Jan 19, 2024
d0d150f
fix: use rpc public urls for mobile connector
bluecco Jan 19, 2024
e09b41b
Merge pull request #60 from argentlabs/fix/rpc-public-urls
bluecco Jan 19, 2024
e447605
chore(release): 1.1.2 [skip ci]
semantic-release-bot Jan 19, 2024
f7a7c63
fix: manage promise reject when user reject a connection in connect m…
bluecco Jan 20, 2024
5927c00
Merge pull request #62 from argentlabs/fix/reject-exception-connect
bluecco Jan 20, 2024
9209e18
chore(release): 1.1.3 [skip ci]
semantic-release-bot Jan 20, 2024
9956718
chore: update CONTRIBUTING.md
Darlington02 Jan 22, 2024
cdcbf7a
Merge pull request #64 from Darlington02/develop
gergold Jan 23, 2024
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
@@ -1,6 +1,6 @@
{
"name": "starknetkit",
"version": "1.0.29",
"version": "1.1.1",
"repository": "github:argentlabs/starknetkit",
"private": false,
"browser": {
Expand Down
6 changes: 6 additions & 0 deletions src/connectors/argentMobile/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,9 @@ export const DEFAULT_ARGENT_MOBILE_ICON = `<svg
fill="white"
/>
</svg>`

export const RPC_NODE_URL_TESTNET =
"https://api.hydrogen.argent47.net/v1/starknet/goerli/rpc/v0.5"

export const RPC_NODE_URL_MAINNET =
"https://cloud.argent-api.com/v1/starknet/mainnet/rpc/v0.5"
39 changes: 28 additions & 11 deletions src/connectors/argentMobile/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
ConnectorNotConnectedError,
ConnectorNotFoundError,
UserNotConnectedError,
UserRejectedRequestError,
} from "../../errors"
import { resetWalletConnect } from "../../helpers/resetWalletConnect"
import {
Expand All @@ -18,6 +19,7 @@ import {
} from "../connector"
import type { StarknetAdapter } from "./modal/starknet/adapter"
import { removeStarknetLastConnectedWallet } from "../../helpers/lastConnected"
import { RPC_NODE_URL_MAINNET, RPC_NODE_URL_TESTNET } from "./constants"

export interface ArgentMobileConnectorOptions {
dappName?: string
Expand All @@ -27,6 +29,7 @@ export interface ArgentMobileConnectorOptions {
url?: string
icons?: string[]
provider?: ProviderInterface
rpcUrl?: string
}

export class ArgentMobileConnector extends Connector {
Expand All @@ -43,6 +46,9 @@ export class ArgentMobileConnector extends Connector {
}

async ready(): Promise<boolean> {
// check if session is valid and retrieve the wallet
// if no sessions, it will show the login modal
await this.ensureWallet()
if (!this._wallet) {
return false
}
Expand Down Expand Up @@ -145,19 +151,32 @@ export class ArgentMobileConnector extends Connector {

private async ensureWallet(): Promise<void> {
const { getStarknetWindowObject } = await import("./modal")
const { chainId, projectId, dappName, description, url, icons } =
this._options
const {
chainId,
projectId,
dappName,
description,
url,
icons,
provider,
rpcUrl,
} = this._options

const providerRpcUrl =
rpcUrl ??
(!chainId || chainId === constants.NetworkName.SN_MAIN
? RPC_NODE_URL_MAINNET
: RPC_NODE_URL_TESTNET)

const options = {
chainId: chainId ?? constants.NetworkName.SN_MAIN,
name: dappName,
projectId: projectId ?? DEFAULT_PROJECT_ID,
description,
url,
icons,
rpcUrl:
chainId === constants.NetworkName.SN_MAIN
? "https://cloud.argent-api.com/v1/starknet/mainnet/rpc/v0.5"
: "https://api.hydrogen.argent47.net/v1/starknet/goerli/rpc/v0.5",
provider,
rpcUrl: providerRpcUrl,
}

if (projectId === DEFAULT_PROJECT_ID) {
Expand All @@ -177,11 +196,9 @@ export class ArgentMobileConnector extends Connector {

const _wallet = await getStarknetWindowObject(options)

const { provider } = this._options
if (provider) {
Object.assign(_wallet, {
provider,
})
// getStarknetWindowObject returns null when the user rejects the connection
if (!_wallet) {
throw new UserRejectedRequestError()
}

this._wallet = _wallet
Expand Down
2 changes: 2 additions & 0 deletions src/connectors/argentMobile/modal/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ import {
} from "@walletconnect/utils"

import type { EthereumRpcConfig } from "./starknet/adapter"
import { ProviderInterface } from "starknet"

export interface NamespaceAdapterOptions {
client: SignClient
chainId?: string | number
rpcUrl?: string
provider: ProviderInterface
}

export abstract class NamespaceAdapter {
Expand Down
6 changes: 4 additions & 2 deletions src/connectors/argentMobile/modal/login.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import SignClient from "@walletconnect/sign-client"
import type { SignClientTypes } from "@walletconnect/types"

import { constants } from "starknet"
import { ProviderInterface, constants } from "starknet"

// Using NetworkName as a value.
const Network: typeof constants.NetworkName = constants.NetworkName
Expand All @@ -22,6 +22,7 @@ export interface IArgentLoginOptions {
mobileUrl?: string
modalType?: "overlay" | "window"
walletConnect?: SignClientTypes.Options
provider?: ProviderInterface
}

export const login = async <TAdapter extends NamespaceAdapter>(
Expand All @@ -37,6 +38,7 @@ export const login = async <TAdapter extends NamespaceAdapter>(
url,
icons,
walletConnect,
provider,
}: IArgentLoginOptions,
Adapter: new (options: NamespaceAdapterOptions) => TAdapter,
): Promise<TAdapter | null> => {
Expand All @@ -63,7 +65,7 @@ export const login = async <TAdapter extends NamespaceAdapter>(
}

const client = await SignClient.init(signClientOptions)
const adapter = new Adapter({ client, chainId, rpcUrl })
const adapter = new Adapter({ client, chainId, rpcUrl, provider })

client.on("session_event", (_) => {
// Handle session events, such as "chainChanged", "accountsChanged", etc.
Expand Down
9 changes: 5 additions & 4 deletions src/connectors/argentMobile/modal/starknet/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ export class StarknetAdapter

private walletRpc: IStarknetRpc

constructor({ client, chainId, rpcUrl }: NamespaceAdapterOptions) {
constructor({ client, chainId, rpcUrl, provider }: NamespaceAdapterOptions) {
super()

this.chainId = String(chainId || "SN_GOERLI")
this.chainId = String(chainId ?? constants.NetworkName.SN_MAIN)
this.rpc = {
chains: chainId ? [this.formatChainId(this.chainId)] : [],
methods: this.methods,
Expand All @@ -84,7 +84,7 @@ export class StarknetAdapter

this.remoteSigner = new StarknetRemoteSigner(this.walletRpc)

this.provider = new RpcProvider({ nodeUrl: rpcUrl })
this.provider = provider ? provider : new RpcProvider({ nodeUrl: rpcUrl })
this.account = new StarknetRemoteAccount(
this.provider,
"",
Expand Down Expand Up @@ -127,7 +127,8 @@ export class StarknetAdapter
}

async isPreauthorized(): Promise<boolean> {
throw new Error("Not implemented: .isPreauthorized()")
// check if wc session is valid, if so, return true
return Boolean(this.client.session.getAll().find(this.isValidSession))
}

on: ConnectedStarknetWindowObject["on"] = (event, handleEvent) => {
Expand Down
3 changes: 2 additions & 1 deletion src/connectors/injected/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
WALLET_NOT_FOUND_ICON_DARK,
WALLET_NOT_FOUND_ICON_LIGHT,
} from "./constants"
import { removeStarknetLastConnectedWallet } from "../../helpers/lastConnected"
/** Injected connector options. */
export interface InjectedConnectorOptions {
/** The wallet id. */
Expand Down Expand Up @@ -153,7 +154,7 @@ export class InjectedConnector extends Connector {

async disconnect(): Promise<void> {
this.ensureWallet()

removeStarknetLastConnectedWallet()
if (!this.available()) {
throw new ConnectorNotFoundError()
}
Expand Down
2 changes: 2 additions & 0 deletions src/connectors/webwallet/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
} from "../../errors"
import { DEFAULT_WEBWALLET_ICON, DEFAULT_WEBWALLET_URL } from "./constants"
import { getWebWalletStarknetObject } from "./starknetWindowObject/getWebWalletStarknetObject"
import { removeStarknetLastConnectedWallet } from "../../helpers/lastConnected"

let _wallet: StarknetWindowObject | null = null

Expand Down Expand Up @@ -119,6 +120,7 @@ export class WebWalletConnector extends Connector {
}
_wallet = null
this._wallet = _wallet
removeStarknetLastConnectedWallet()
}

async account(): Promise<AccountInterface> {
Expand Down
5 changes: 4 additions & 1 deletion src/helpers/defaultConnectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ export const defaultConnectors = ({
webWalletUrl?: string
provider?: ProviderInterface
}): Connector[] => {
const isSafari = /^((?!chrome|android).)*safari/i.test(navigator.userAgent)
const isSafari =
typeof window !== "undefined"
? /^((?!chrome|android).)*safari/i.test(navigator.userAgent)
: false

const defaultConnectors: Connector[] = []

Expand Down
23 changes: 23 additions & 0 deletions src/hooks/useStarknetkitConnectModal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { connect } from "../main"
import { ConnectOptions, ModalResult } from "../types/modal"

type UseStarknetkitConnectors = {
starknetkitConnectModal: () => Promise<ModalResult>
}

const useStarknetkitConnectModal = (
options: Omit<ConnectOptions, "argentMobileOptions" | "webWalletUrl">,
): UseStarknetkitConnectors => {
const starknetkitConnectModal = async (): Promise<ModalResult> => {
return await connect({
...options,
resultType: options.resultType ?? "connector",
})
}

return {
starknetkitConnectModal,
}
}

export { useStarknetkitConnectModal }
61 changes: 46 additions & 15 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ import {
} from "./helpers/lastConnected"
import { mapModalWallets } from "./helpers/mapModalWallets"
import Modal from "./modal/Modal.svelte"
import type { ConnectOptions, ModalWallet } from "./types/modal"
import type { ConnectOptions, ModalResult, ModalWallet } from "./types/modal"

import { Connector } from "./connectors"
import { type Connector } from "./connectors"
import css from "./theme.css?inline"

let selectedConnector: Connector | null = null
Expand All @@ -31,9 +31,10 @@ export const connect = async ({
webWalletUrl = DEFAULT_WEBWALLET_URL,
argentMobileOptions,
connectors = [],
resultType = "wallet",
provider,
...restOptions
}: ConnectOptions = {}): Promise<StarknetWindowObject | null> => {
}: ConnectOptions = {}): Promise<ModalResult> => {
// force null in case it was disconnected from mobile app
selectedConnector = null
const availableConnectors =
Expand All @@ -48,9 +49,17 @@ export const connect = async ({
const lastWalletId = localStorage.getItem("starknetLastConnectedWallet")
if (modalMode === "neverAsk") {
const connector = availableConnectors.find((c) => c.id === lastWalletId)
await connector?.connect()

if (resultType === "wallet") {
await connector?.connect()
}

selectedConnector = connector ?? null
return connector?.wallet ?? null

return {
connector,
wallet: connector?.wallet ?? null,
}
}

const installedWallets = await sn.getAvailableWallets(restOptions)
Expand All @@ -69,11 +78,19 @@ export const connect = async ({

if (wallet) {
const connector = availableConnectors.find((c) => c.id === lastWalletId)
await connector?.connect()

if (resultType === "wallet") {
await connector?.connect()
}

if (connector) {
selectedConnector = connector
}
return wallet

return {
connector,
wallet: connector?.wallet ?? null,
}
} // otherwise fallback to modal
}

Expand Down Expand Up @@ -113,16 +130,26 @@ export const connect = async ({
target: getTarget(),
props: {
dappName,
callback: async (value: StarknetWindowObject | null) => {
callback: async (connector: Connector | null) => {
try {
if (value !== null && value.id !== "argentWebWallet") {
setStarknetLastConnectedWallet(value.id)
selectedConnector = connector

if (resultType === "wallet") {
await connector?.connect()

if (connector !== null && connector.id !== "argentWebWallet") {
setStarknetLastConnectedWallet(connector.id)
}

resolve({
connector,
wallet: connector?.wallet ?? null,
})
} else {
resolve({
connector,
})
}
selectedConnector =
availableConnectors.find(
(c) => value !== null && c.id === value.id,
) ?? null
resolve(value)
} finally {
setTimeout(() => modal.$destroy())
}
Expand Down Expand Up @@ -150,7 +177,11 @@ export const disconnect = async (options: DisconnectOptions = {}) => {

export type {
ConnectedStarknetWindowObject,
Connector,
DisconnectOptions,
DisconnectedStarknetWindowObject,
StarknetWindowObject,
defaultConnectors as starknetkitDefaultConnectors,
}

export { useStarknetkitConnectModal } from "./hooks/useStarknetkitConnectModal"
Loading