-
Notifications
You must be signed in to change notification settings - Fork 2
feat: ton connector #495
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
Merged
Merged
feat: ton connector #495
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0f088bd
feat: deps bump, migrate test to browser mode
petar-omni 3241ea1
Merge branch 'main' into feat/deps-bump
petar-omni 69a75a0
feat: init
petar-omni 0f82265
Merge branch 'main' into feat/ton-connector
petar-omni d94a757
feat: tonconnect manifest url
petar-omni File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@stakekit/widget": patch | ||
| --- | ||
|
|
||
| feat: ton connector |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| import type { EitherAsync } from "purify-ts"; | ||
| import type { Connector } from "wagmi"; | ||
| import type { ConnectorWithFilteredChains } from "../../domain/types/connectors"; | ||
|
|
||
| export const configMeta = { | ||
| type: "tonWallet", | ||
| } as const; | ||
|
|
||
| export type ExtraProps = ConnectorWithFilteredChains & { | ||
| signTransaction: (tx: string) => EitherAsync<Error, string>; | ||
| }; | ||
|
|
||
| type TonConnector = Connector & ExtraProps; | ||
|
|
||
| export type StorageItem = { | ||
| "ton.disconnected": boolean; | ||
| }; | ||
|
|
||
| export const isTonConnector = ( | ||
| connector: Connector | ||
| ): connector is TonConnector => connector.type === "tonWallet"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,197 @@ | ||
| import { MiscNetworks } from "@stakekit/common"; | ||
| import type { WalletDetailsParams, WalletList } from "@stakekit/rainbowkit"; | ||
| import { | ||
| Cell, | ||
| type CommonMessageInfoRelaxedInternal, | ||
| loadMessageRelaxed, | ||
| } from "@ton/core"; | ||
| import { | ||
| TonConnectUI, | ||
| toUserFriendlyAddress, | ||
| type Wallet, | ||
| } from "@tonconnect/ui"; | ||
| import { Either, EitherAsync } from "purify-ts"; | ||
| import { BehaviorSubject } from "rxjs"; | ||
| import type { Address, Chain } from "viem"; | ||
| import { createConnector } from "wagmi"; | ||
| import { ton } from "../../domain/types/chains/misc"; | ||
| import { unsignedTonTransactionTonConnectCodec } from "../../domain/types/transaction"; | ||
| import { getNetworkLogo } from "../../utils"; | ||
| import { | ||
| configMeta, | ||
| type ExtraProps, | ||
| type StorageItem, | ||
| } from "./ton-connector-meta"; | ||
|
|
||
| const createTonConnector = ( | ||
| walletDetailsParams: WalletDetailsParams, | ||
| manifestUrl: string | undefined | ||
| ) => | ||
| createConnector<unknown, ExtraProps, StorageItem>((config) => { | ||
| const tonconnectUI = new TonConnectUI({ | ||
| manifestUrl: | ||
| manifestUrl ?? "https://dapp.stakek.it/tonconnect-manifest.json", | ||
| }); | ||
|
|
||
| let deferred: { | ||
| resolve: (wallet: Wallet) => void; | ||
| reject: () => void; | ||
| } | null = null; | ||
| let connectedWallet: Wallet | null = null; | ||
|
|
||
| tonconnectUI.onStatusChange((wallet) => { | ||
| connectedWallet = wallet; | ||
| if (wallet) { | ||
| deferred?.resolve(wallet); | ||
| } | ||
| }); | ||
|
|
||
| tonconnectUI.onModalStateChange((state) => { | ||
| if (state.status === "closed") { | ||
| deferred?.reject(); | ||
| } | ||
| }); | ||
|
|
||
| return { | ||
| ...walletDetailsParams, | ||
| id: "tonconnect", | ||
| name: "TonConnect", | ||
| type: configMeta.type, | ||
| signTransaction: (tx: string) => | ||
| EitherAsync(async ({ throwE, liftEither }) => { | ||
| if (!connectedWallet) { | ||
| return throwE(new Error("No wallet connected")); | ||
| } | ||
|
|
||
| const parsedTx = await liftEither( | ||
| Either.encase(() => JSON.parse(tx)).chain((val) => | ||
| unsignedTonTransactionTonConnectCodec | ||
| .decode(val) | ||
| .mapLeft((e) => new Error(e)) | ||
| ) | ||
| ).then(({ message }) => | ||
| loadMessageRelaxed(Cell.fromBase64(message).beginParse()) | ||
| ); | ||
|
|
||
| const info = parsedTx.info as CommonMessageInfoRelaxedInternal; | ||
|
|
||
| const result = await tonconnectUI.sendTransaction({ | ||
| messages: [ | ||
| { | ||
| address: info.dest.toString(), | ||
| amount: info.value.coins.toString(), | ||
| payload: parsedTx.body.toBoc().toString("base64"), | ||
| }, | ||
| ], | ||
| validUntil: Date.now() + 1000 * 60 * 60 * 24, | ||
| }); | ||
|
|
||
| const externalMessageCell = Cell.fromBase64(result.boc); | ||
| const txHash = externalMessageCell.hash().toString("hex"); | ||
|
|
||
| return txHash; | ||
| }), | ||
| connect: async (args) => { | ||
| config.emitter.emit("message", { type: "connecting" }); | ||
|
|
||
| config.storage?.removeItem("ton.disconnected"); | ||
|
|
||
| const wallet: Wallet = | ||
| connectedWallet ?? | ||
| (await tonconnectUI | ||
| .openModal() | ||
| .then( | ||
| () => | ||
| new Promise<Wallet>((resolve, reject) => { | ||
| deferred = { resolve, reject }; | ||
| }) | ||
| ) | ||
| .then((wallet) => { | ||
| deferred = null; | ||
| return wallet; | ||
| })); | ||
|
|
||
| const userFriendlyAddress = toUserFriendlyAddress( | ||
| wallet.account.address | ||
| ); | ||
|
|
||
| return { | ||
| accounts: args?.withCapabilities | ||
| ? [ | ||
| { | ||
| address: userFriendlyAddress as Address, | ||
| capabilities: {}, | ||
| }, | ||
| ] | ||
| : [userFriendlyAddress as Address], | ||
| chainId: ton.id, | ||
| } as never; | ||
| }, | ||
| disconnect: async () => { | ||
| config.storage?.setItem("ton.disconnected", true); | ||
| await tonconnectUI.disconnect(); | ||
| connectedWallet = null; | ||
| }, | ||
| getAccounts: async () => { | ||
| await tonconnectUI.connectionRestored; | ||
|
|
||
| if (!connectedWallet) throw new Error("No wallet connected"); | ||
|
|
||
| return [ | ||
| toUserFriendlyAddress(connectedWallet.account.address) as Address, | ||
| ]; | ||
| }, | ||
| switchChain: async () => ton, | ||
| getChainId: async () => ton.id, | ||
| isAuthorized: async () => { | ||
| await tonconnectUI.connectionRestored; | ||
|
|
||
| const isDisconnected = | ||
| await config.storage?.getItem("ton.disconnected"); | ||
|
|
||
| if (isDisconnected) return false; | ||
|
|
||
| return !!connectedWallet; | ||
| }, | ||
| onAccountsChanged: (accounts: string[]) => { | ||
| if (accounts.length === 0) { | ||
| config.emitter.emit("disconnect"); | ||
| } else { | ||
| config.emitter.emit("change", { accounts: accounts as Address[] }); | ||
| } | ||
| }, | ||
| onChainChanged: (chainId) => { | ||
| config.emitter.emit("change", { | ||
| chainId: chainId as unknown as number, | ||
| }); | ||
| }, | ||
| onDisconnect: () => { | ||
| config.emitter.emit("disconnect"); | ||
| }, | ||
| getProvider: async () => ({}), | ||
| $filteredChains: new BehaviorSubject<Chain[]>([ton]).asObservable(), | ||
| }; | ||
| }); | ||
|
|
||
| export const getTonConnectors = ({ | ||
| tonConnectManifestUrl, | ||
| }: { | ||
| tonConnectManifestUrl: string | undefined; | ||
| }): WalletList[number] => ({ | ||
| groupName: "Ton", | ||
| wallets: [ | ||
| () => ({ | ||
| id: "tonconnect", | ||
| name: "TonConnect", | ||
| iconUrl: getNetworkLogo(MiscNetworks.Ton), | ||
| iconBackground: "transparent", | ||
| chainGroup: { | ||
| id: "ton", | ||
| title: "Ton", | ||
| iconUrl: getNetworkLogo(MiscNetworks.Ton), | ||
| }, | ||
| createConnector: (walletDetailsParams) => | ||
| createTonConnector(walletDetailsParams, tonConnectManifestUrl), | ||
| }), | ||
| ], | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.