Skip to content

Commit

Permalink
Merge pull request #51 from argentlabs/develop
Browse files Browse the repository at this point in the history
Release 1.0.29
  • Loading branch information
bluecco authored Jan 4, 2024
2 parents 6f59c47 + 43ea9a3 commit 81b069d
Show file tree
Hide file tree
Showing 13 changed files with 89 additions and 32 deletions.
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.22",
"version": "1.0.29",
"repository": "github:argentlabs/starknetkit",
"private": false,
"browser": {
Expand Down
20 changes: 18 additions & 2 deletions src/connectors/argentMobile/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type {
AccountChangeEventHandler,
StarknetWindowObject,
} from "get-starknet-core"
import type { AccountInterface } from "starknet"
import type { AccountInterface, ProviderInterface } from "starknet"
import { constants } from "starknet"
import { DEFAULT_ARGENT_MOBILE_ICON, DEFAULT_PROJECT_ID } from "./constants"
import {
Expand All @@ -25,6 +25,8 @@ export interface ArgentMobileConnectorOptions {
chainId?: constants.NetworkName
description?: string
url?: string
icons?: string[]
provider?: ProviderInterface
}

export class ArgentMobileConnector extends Connector {
Expand Down Expand Up @@ -143,13 +145,19 @@ export class ArgentMobileConnector extends Connector {

private async ensureWallet(): Promise<void> {
const { getStarknetWindowObject } = await import("./modal")
const { chainId, projectId, dappName, description, url } = this._options
const { chainId, projectId, dappName, description, url, icons } =
this._options
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",
}

if (projectId === DEFAULT_PROJECT_ID) {
Expand All @@ -168,6 +176,14 @@ export class ArgentMobileConnector extends Connector {
}

const _wallet = await getStarknetWindowObject(options)

const { provider } = this._options
if (provider) {
Object.assign(_wallet, {
provider,
})
}

this._wallet = _wallet

// wallet connect rpc enable
Expand Down
3 changes: 2 additions & 1 deletion src/connectors/argentMobile/modal/argentModal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const iframeStyle = {
}

const overlayHtml = `
<div style="position: relative">
<div id="argent-mobile-modal-container" style="position: relative">
<iframe class="argent-iframe" allow="clipboard-write"></iframe>
<div class="argent-close-button" style="position: absolute; top: 24px; right: 24px; cursor: pointer;">
<svg width="32" height="32" viewBox="0 0 32 32" fill="none" xmlns="http://www.w3.org/2000/svg">
Expand Down Expand Up @@ -141,6 +141,7 @@ class ArgentModal {
// type=overlay, device=desktop
const overlay = document.createElement("div")
overlay.innerHTML = overlayHtml
overlay.id = "argent-mobile-modal-overlay"
for (const [key, value] of Object.entries(overlayStyle)) {
overlay.style[key as any] = value
}
Expand Down
4 changes: 3 additions & 1 deletion src/connectors/argentMobile/modal/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export interface IArgentLoginOptions {
name?: string
description?: string
url?: string
icons?: string[]
chainId?: string | number
rpcUrl?: string
bridgeUrl?: string
Expand All @@ -34,6 +35,7 @@ export const login = async <TAdapter extends NamespaceAdapter>(
mobileUrl = getMobileUrl(chainId),
modalType = "overlay",
url,
icons,
walletConnect,
}: IArgentLoginOptions,
Adapter: new (options: NamespaceAdapterOptions) => TAdapter,
Expand All @@ -55,7 +57,7 @@ export const login = async <TAdapter extends NamespaceAdapter>(
name: name ?? "Unknown dapp",
description: description ?? "Unknown dapp description",
url: url ?? "#",
icons: ["https://walletconnect.com/walletconnect-logo.png"],
icons: icons ?? [],
...walletConnect?.metadata,
},
}
Expand Down
9 changes: 2 additions & 7 deletions src/connectors/argentMobile/modal/starknet/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import type {
ProviderInterface,
SignerInterface,
} from "starknet"
import { Provider, constants } from "starknet"
import { RpcProvider, constants } from "starknet"

import type { NamespaceAdapterOptions } from "../adapter"
import { NamespaceAdapter } from "../adapter"
Expand Down Expand Up @@ -84,12 +84,7 @@ export class StarknetAdapter

this.remoteSigner = new StarknetRemoteSigner(this.walletRpc)

if (rpcUrl) {
this.provider = new Provider({ rpc: { nodeUrl: rpcUrl } })
} else {
const network = this.getNetworkName(this.chainId)
this.provider = new Provider({ sequencer: { network } })
}
this.provider = new RpcProvider({ nodeUrl: rpcUrl })
this.account = new StarknetRemoteAccount(
this.provider,
"",
Expand Down
11 changes: 10 additions & 1 deletion src/connectors/injected/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { StarknetWindowObject } from "get-starknet-core"
import { AccountInterface, constants } from "starknet"
import { AccountInterface, ProviderInterface, constants } from "starknet"
import {
ConnectorNotConnectedError,
ConnectorNotFoundError,
Expand All @@ -23,6 +23,8 @@ export interface InjectedConnectorOptions {
name?: string
/** Wallet icons. */
icon?: ConnectorIcons
/** Provider */
provider?: ProviderInterface
}

export class InjectedConnector extends Connector {
Expand Down Expand Up @@ -211,6 +213,13 @@ export class InjectedConnector extends Connector {
const installed = getAvailableWallets(globalThis)
const wallet = installed.filter((w) => w.id === this._options.id)[0]
if (wallet) {
const { provider } = this._options
if (provider) {
Object.assign(wallet, {
provider,
})
}

this._wallet = wallet
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/connectors/webwallet/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ 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/goerli/rpc/v0.5"
"https://cloud.argent-api.com/v1/starknet/mainnet/rpc/v0.5"
10 changes: 8 additions & 2 deletions src/connectors/webwallet/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type {
AccountChangeEventHandler,
StarknetWindowObject,
} from "get-starknet-core"
import type { AccountInterface } from "starknet"
import type { AccountInterface, ProviderInterface } from "starknet"
import {
Connector,
type ConnectorData,
Expand All @@ -23,6 +23,7 @@ let _wallet: StarknetWindowObject | null = null

interface WebWalletConnectorOptions {
url?: string
provider?: ProviderInterface
}

export class WebWalletConnector extends Connector {
Expand Down Expand Up @@ -163,11 +164,16 @@ export class WebWalletConnector extends Connector {

private async ensureWallet(): Promise<void> {
const origin = this._options.url || DEFAULT_WEBWALLET_URL
const provider = this._options.provider
setPopupOptions({
origin,
location: "/interstitialLogin",
})
const wallet = await getWebWalletStarknetObject(origin, trpcProxyClient({}))
const wallet = await getWebWalletStarknetObject(
origin,
trpcProxyClient({}),
provider,
)

_wallet = wallet ?? null
this._wallet = _wallet
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { CreateTRPCProxyClient } from "@trpc/client"
import { RpcProvider } from "starknet"
import { ProviderInterface, RpcProvider } from "starknet"

import { mapTargetUrlToNodeUrl } from "../helpers/mapTargetUrlToNodeUrl"
import type { AppRouter } from "../helpers/trpc"
Expand All @@ -9,14 +9,15 @@ import { getArgentStarknetWindowObject } from "./argentStarknetWindowObject"
export const getWebWalletStarknetObject = async (
target: string,
proxyLink: CreateTRPCProxyClient<AppRouter>,
provider?: ProviderInterface,
): Promise<WebWalletStarknetWindowObject> => {
const globalWindow = typeof window !== "undefined" ? window : undefined
if (!globalWindow) {
throw new Error("window is not defined")
}

const nodeUrl = mapTargetUrlToNodeUrl(target)
const defaultProvider = new RpcProvider({ nodeUrl })
const defaultProvider = provider ?? new RpcProvider({ nodeUrl })
const starknetWindowObject = getArgentStarknetWindowObject(
{
host: globalWindow.location.origin,
Expand Down
15 changes: 11 additions & 4 deletions src/helpers/defaultConnectors.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ProviderInterface } from "starknet"
import { type Connector } from "../connectors"
import {
ArgentMobileConnector,
Expand All @@ -9,25 +10,31 @@ import { WebWalletConnector } from "../connectors/webwallet"
export const defaultConnectors = ({
argentMobileOptions,
webWalletUrl,
provider,
}: {
argentMobileOptions?: ArgentMobileConnectorOptions
webWalletUrl?: string
provider?: ProviderInterface
}): Connector[] => {
const isSafari = /^((?!chrome|android).)*safari/i.test(navigator.userAgent)

const defaultConnectors: Connector[] = []

if (!isSafari) {
defaultConnectors.push(
new InjectedConnector({ options: { id: "argentX" } }),
new InjectedConnector({ options: { id: "argentX", provider } }),
)
defaultConnectors.push(
new InjectedConnector({ options: { id: "braavos" } }),
new InjectedConnector({ options: { id: "braavos", provider } }),
)
}

defaultConnectors.push(new ArgentMobileConnector(argentMobileOptions))
defaultConnectors.push(new WebWalletConnector({ url: webWalletUrl }))
defaultConnectors.push(
new ArgentMobileConnector({ ...argentMobileOptions, provider }),
)
defaultConnectors.push(
new WebWalletConnector({ url: webWalletUrl, provider }),
)

return defaultConnectors
}
30 changes: 25 additions & 5 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export const connect = async ({
webWalletUrl = DEFAULT_WEBWALLET_URL,
argentMobileOptions,
connectors = [],
provider,
...restOptions
}: ConnectOptions = {}): Promise<StarknetWindowObject | null> => {
// force null in case it was disconnected from mobile app
Expand All @@ -40,6 +41,7 @@ export const connect = async ({
? defaultConnectors({
argentMobileOptions,
webWalletUrl,
provider,
})
: connectors

Expand Down Expand Up @@ -82,15 +84,33 @@ export const connect = async ({
storeVersion,
})

const element = document.createElement("div")
document.body.appendChild(element)
const target = element.attachShadow({ mode: "open" })
const getTarget = (): ShadowRoot => {
const modalId = "starknetkit-modal-container"
const existingElement = document.getElementById(modalId)

target.innerHTML = `<style>${css}</style>`
if (existingElement) {
if (existingElement.shadowRoot) {
// element already exists, use the existing as target
return existingElement.shadowRoot
}
// element exists but shadowRoot cannot be accessed
// delete the element and create new
existingElement.remove()
}

const element = document.createElement("div")
// set id for future retrieval
element.id = modalId
document.body.appendChild(element)
const target = element.attachShadow({ mode: "open" })
target.innerHTML = `<style>${css}</style>`

return target
}

return new Promise((resolve) => {
const modal = new Modal({
target,
target: getTarget(),
props: {
dappName,
callback: async (value: StarknetWindowObject | null) => {
Expand Down
8 changes: 3 additions & 5 deletions src/modal/Modal.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@
export let theme: "light" | "dark" | null = null
let loadingItem: string | false = false
let emailOnly =
modalWallets.length === 1 &&
modalWallets[0].id.toLowerCase().includes("webwallet")
let starknetMobile = window?.starknet_argentX as StarknetWindowObject & {
isInAppBrowser: boolean
Expand Down Expand Up @@ -59,7 +56,7 @@
return
}
if (emailOnly) {
if (modalWallets.length === 1) {
try {
const [wallet] = modalWallets
await wallet.connector?.connect()
Expand All @@ -71,9 +68,10 @@
})
</script>

{#if !isInAppBrowser && !emailOnly}
{#if !isInAppBrowser && modalWallets.length > 1}
<!-- svelte-ignore a11y-no-static-element-interactions -->
<div
part="starknetkit-modal"
class={`modal-font backdrop-blur-sm fixed inset-0 flex items-center
justify-center bg-black/25 z-[9999] ${darkModeControlClass}`}
on:click={() => cb(null)}
Expand Down
2 changes: 2 additions & 0 deletions src/types/modal.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { GetWalletOptions } from "get-starknet-core"
import type { Connector, ConnectorIcons } from "../connectors/connector"
import type { ArgentMobileConnectorOptions } from "../connectors/argentMobile"
import { ProviderInterface } from "starknet"

export type StoreVersion = "chrome" | "firefox" | "edge"

Expand All @@ -12,6 +13,7 @@ export interface ConnectOptions extends GetWalletOptions {
modalTheme?: "light" | "dark" | "system"
storeVersion?: StoreVersion | null
webWalletUrl?: string
provider?: ProviderInterface
}

export type ModalWallet = {
Expand Down

0 comments on commit 81b069d

Please sign in to comment.