Skip to content

Commit

Permalink
Merge pull request #57 from argentlabs/develop
Browse files Browse the repository at this point in the history
release 1.1.3
  • Loading branch information
bluecco authored Jan 24, 2024
2 parents 81b069d + cdcbf7a commit 0c00bce
Show file tree
Hide file tree
Showing 16 changed files with 233 additions and 59 deletions.
71 changes: 71 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# Starknetkit Contribution Guide

Thank you for investing your time in contributing to Starknetkit!

We love pull requests and this guide aims to provide an overview of the contribution workflow to help us make the contribution process effective for everyone involved.

If you want to contribute but don’t know what to do, take a look at issues labelled `good first issue`.

## Getting started

You can contribute to this repo in many ways:

- Solve open issues
- Report bugs or feature requests
- Add new features such as new connectors
- Improve the documentation

Contributions are made via Issues and Pull Requests (PRs). A few general guidelines for contributions:

- Search for existing Issues and PRs before creating your own.
- If you're running into an error, please give context. Explain what you're trying to do and how to reproduce the error.
- Please use the same formatting in the code repository. You can configure your IDE to do it by using the prettier / linting config files included in each package.
- If applicable, please edit the README.md file to reflect the changes.

### Issues

Issues should be used to report problems, request a new feature, or discuss potential changes before a PR is created.

#### Solve an issue

Scan through our existing issues to find one that interests you.

If a contributor is working on the issue, they will be assigned to the individual. If you find an issue to work on, you are welcome to assign it to yourself and open a PR with a fix for it.

### Pull Requests

#### Pull Request Process

We follow the ["fork-and-pull" Git workflow](https://github.com/susam/gitpr)

1. Fork the repo
2. Clone the project
3. Create a new branch with a descriptive name
4. Commit your changes to the new branch
5. Push changes to your fork
6. Open a PR in our repository and tag one of the maintainers to review your PR

Here are some tips for a high-quality pull request:

- Create a title for the PR that accurately defines the work done.
- Structure the description neatly to make it easy to consume by the readers. For example, you can include bullet points and screenshots instead of having one large paragraph.
- Add the link to the issue if applicable.
- Have a good commit message that summarises the work done.

Once you submit your PR:

- We may ask questions, request additional information or ask for changes to be made before a PR can be merged. Please note that these are to make the PR clear for everyone involved and aims to create a frictionless interaction process.
- As you update your PR and apply changes, mark each conversation resolved.
- Once approved, your PR will be merged.

#### Pull request targets
For the most common pull requests such as bug fixes, feature additions, documentation changes, etc., target the develop branch.

### Other notes
- If you have commit access to the repository and want to make a big change or are unsure about something, make a new branch and open a pull request.
- We’re using Prettier to format code, so don’t worry much about code formatting.
- Don’t commit generated files, like minified JavaScript.
- Don’t change the version number or changelog.

### Need help?
If you want to contribute but have any questions, concerns or doubts, feel free to ping maintainers. Ideally create a pull request with WIP (Work in progress) in its title and ask questions in the pull request description.
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.3",
"repository": "github:argentlabs/starknetkit",
"private": false,
"browser": {
Expand Down
40 changes: 29 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 { getRandomPublicRPCNode } from "../../helpers/publicRcpNodes"

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,33 @@ 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 publicRPCNode = getRandomPublicRPCNode()
const providerRpcUrl =
rpcUrl ??
(!chainId || chainId === constants.NetworkName.SN_MAIN
? publicRPCNode.mainnet
: publicRPCNode.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 +197,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 @@ -6,6 +6,7 @@ import {
UserNotConnectedError,
UserRejectedRequestError,
} from "../../errors"
import { removeStarknetLastConnectedWallet } from "../../helpers/lastConnected"
import {
Connector,
type ConnectorData,
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
6 changes: 0 additions & 6 deletions src/connectors/webwallet/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,3 @@ export const DEFAULT_WEBWALLET_ICON = `<svg
fill="currentColor"
/>
</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"
13 changes: 7 additions & 6 deletions src/connectors/webwallet/helpers/mapTargetUrlToNodeUrl.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
import { RPC_NODE_URL_MAINNET, RPC_NODE_URL_TESTNET } from "../constants"
import { getRandomPublicRPCNode } from "../../../helpers/publicRcpNodes"

export function mapTargetUrlToNodeUrl(target: string): string {
const publicRPCNode = getRandomPublicRPCNode()
try {
const { origin } = new URL(target)
if (origin.includes("localhost") || origin.includes("127.0.0.1")) {
return RPC_NODE_URL_TESTNET
return publicRPCNode.testnet
}
if (origin.includes("hydrogen")) {
return RPC_NODE_URL_TESTNET
return publicRPCNode.testnet
}
if (origin.includes("staging")) {
return RPC_NODE_URL_MAINNET
return publicRPCNode.mainnet
}
if (origin.includes("argent.xyz")) {
return RPC_NODE_URL_MAINNET
return publicRPCNode.mainnet
}
} catch (e) {
console.warn(
"Could not determine rpc nodeUrl from target URL, defaulting to mainnet",
)
}
return RPC_NODE_URL_MAINNET
return publicRPCNode.mainnet
}
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
22 changes: 22 additions & 0 deletions src/helpers/publicRcpNodes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
export type PublicRpcNode = {
mainnet: string
testnet: string
}

// Public RPC nodes
export const BLAST_RPC_NODE: PublicRpcNode = {
mainnet: "https://starknet-mainnet.public.blastapi.io",
testnet: "https://starknet-testnet.public.blastapi.io",
} as const

export const LAVA_RPC_NODE: PublicRpcNode = {
mainnet: "https://rpc.starknet.lava.build",
testnet: "https://rpc.starknet-testnet.lava.build",
} as const

export const PUBLIC_RPC_NODES = [BLAST_RPC_NODE, LAVA_RPC_NODE] as const

export function getRandomPublicRPCNode() {
const randomIndex = Math.floor(Math.random() * PUBLIC_RPC_NODES.length)
return PUBLIC_RPC_NODES[randomIndex]
}
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 }
Loading

0 comments on commit 0c00bce

Please sign in to comment.