-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
aa6887c
commit e1e2c58
Showing
32 changed files
with
373 additions
and
13 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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 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 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 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,36 @@ | ||
{ | ||
"name": "@rango-dev/provider-tonconnect", | ||
"version": "0.1.0", | ||
"license": "MIT", | ||
"type": "module", | ||
"source": "./src/index.ts", | ||
"main": "./dist/index.js", | ||
"exports": { | ||
".": { | ||
"types": "./dist/index.d.ts", | ||
"default": "./dist/index.js" | ||
} | ||
}, | ||
"typings": "dist/index.d.ts", | ||
"files": [ | ||
"dist", | ||
"src" | ||
], | ||
"scripts": { | ||
"build": "node ../../scripts/build/command.mjs --path wallets/provider-tonconnect", | ||
"ts-check": "tsc --declaration --emitDeclarationOnly -p ./tsconfig.json", | ||
"clean": "rimraf dist", | ||
"format": "prettier --write '{.,src}/**/*.{ts,tsx}'", | ||
"lint": "eslint \"**/*.{ts,tsx}\" --ignore-path ../../.eslintignore" | ||
}, | ||
"dependencies": { | ||
"@rango-dev/wallets-shared": "^0.38.0", | ||
"@ton/core": "^0.59.0", | ||
"@ton/crypto": "3.3.0", | ||
"@tonconnect/ui": "^2.0.9", | ||
"rango-types": "^0.1.74" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
} | ||
} |
This file contains 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,3 @@ | ||
# @rango-dev/provider-tonconnect | ||
|
||
TonConnect |
This file contains 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,42 @@ | ||
import type { TonConnectUI } from '@tonconnect/ui'; | ||
|
||
import { type Connect, Networks } from '@rango-dev/wallets-shared'; | ||
import { Address } from '@ton/core'; | ||
|
||
export const waitForConnection = async ( | ||
tonConnectUI: TonConnectUI | ||
): ReturnType<Connect> => { | ||
return new Promise((resolve, reject) => { | ||
const unsubscribeStatusChange = tonConnectUI.onStatusChange( | ||
(state) => { | ||
const walletConnected = !!state?.account.address; | ||
|
||
if (walletConnected) { | ||
unsubscribeStatusChange(); | ||
resolve({ | ||
accounts: [parseAddress(state.account.address)], | ||
chainId: Networks.TON, | ||
}); | ||
} | ||
}, | ||
(error) => { | ||
unsubscribeStatusChange(); | ||
reject(error); | ||
} | ||
); | ||
|
||
const unsubscribeModalStateChange = tonConnectUI.onModalStateChange( | ||
(modalState) => { | ||
if (modalState.closeReason === 'action-cancelled') { | ||
unsubscribeStatusChange(); | ||
unsubscribeModalStateChange(); | ||
reject(new Error('The action was canceled by the user')); | ||
} | ||
} | ||
); | ||
}); | ||
}; | ||
|
||
export function parseAddress(rawAddress: string): string { | ||
return Address.parse(rawAddress).toString({ bounceable: false }); | ||
} |
This file contains 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,86 @@ | ||
import type { Environments } from './types.js'; | ||
import type { | ||
CanEagerConnect, | ||
CanSwitchNetwork, | ||
Connect, | ||
Disconnect, | ||
GetInstance, | ||
WalletInfo, | ||
} from '@rango-dev/wallets-shared'; | ||
import type { BlockchainMeta, SignerFactory } from 'rango-types'; | ||
|
||
import { Networks, WalletTypes } from '@rango-dev/wallets-shared'; | ||
import { TonConnectUI } from '@tonconnect/ui'; | ||
import { tonBlockchain } from 'rango-types'; | ||
|
||
import { parseAddress, waitForConnection } from './helpers.js'; | ||
import signer from './signer.js'; | ||
|
||
let envs: Environments = { | ||
manifestUrl: '', | ||
}; | ||
|
||
const WALLET = WalletTypes.TON_CONNECT; | ||
|
||
export const config = { | ||
type: WALLET, | ||
}; | ||
|
||
export type { Environments }; | ||
|
||
export const init = (environments: Environments) => { | ||
envs = environments; | ||
}; | ||
|
||
let instance: TonConnectUI | null = null; | ||
|
||
export const getInstance: GetInstance = () => { | ||
if (!instance) { | ||
instance = new TonConnectUI(envs); | ||
} | ||
return instance; | ||
}; | ||
|
||
export const connect: Connect = async ({ instance }) => { | ||
const tonConnectUI: TonConnectUI = instance; | ||
const connectionRestored = await tonConnectUI.connectionRestored; | ||
|
||
if (connectionRestored && tonConnectUI.account?.address) { | ||
const currentWalletAccount = parseAddress(tonConnectUI.account.address); | ||
return { accounts: [currentWalletAccount], chainId: Networks.TON }; | ||
} | ||
|
||
await tonConnectUI.openModal(); | ||
const result = await waitForConnection(tonConnectUI); | ||
|
||
return result; | ||
}; | ||
|
||
export const canEagerConnect: CanEagerConnect = async ({ instance }) => { | ||
const tonConnectUI = instance as TonConnectUI; | ||
const connectionRestored = await tonConnectUI.connectionRestored; | ||
return connectionRestored; | ||
}; | ||
|
||
export const canSwitchNetworkTo: CanSwitchNetwork = () => false; | ||
|
||
export const getSigners: (provider: TonConnectUI) => Promise<SignerFactory> = | ||
signer; | ||
|
||
export const getWalletInfo: (allBlockChains: BlockchainMeta[]) => WalletInfo = ( | ||
allBlockChains | ||
) => { | ||
const ton = tonBlockchain(allBlockChains); | ||
return { | ||
name: 'TON Connect', | ||
img: 'https://raw.githubusercontent.com/rango-exchange/assets/7fb19ed5d5019b4d6a41ce91b39cde64f86af4c6/wallets/tonconnect/icon.svg', | ||
installLink: '', | ||
color: '#fff', | ||
supportedChains: ton, | ||
}; | ||
}; | ||
|
||
export const disconnect: Disconnect = async ({ instance }) => { | ||
const tonConnectUI = instance as TonConnectUI; | ||
await tonConnectUI.disconnect(); | ||
}; |
This file contains 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,13 @@ | ||
import type { TonConnectUI } from '@tonconnect/ui'; | ||
import type { SignerFactory } from 'rango-types'; | ||
|
||
import { DefaultSignerFactory, TransactionType as TxType } from 'rango-types'; | ||
|
||
export default async function getSigners( | ||
provider: TonConnectUI | ||
): Promise<SignerFactory> { | ||
const signers = new DefaultSignerFactory(); | ||
const { CustomTonSigner } = await import('./ton-signer.js'); | ||
signers.registerSigner(TxType.TON, new CustomTonSigner(provider)); | ||
return signers; | ||
} |
This file contains 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,29 @@ | ||
import type { TonConnectUI } from '@tonconnect/ui'; | ||
import type { GenericSigner, TonTransaction } from 'rango-types'; | ||
|
||
import { Cell } from '@ton/core'; | ||
import { CHAIN } from '@tonconnect/ui'; | ||
import { SignerError } from 'rango-types'; | ||
|
||
export class CustomTonSigner implements GenericSigner<TonTransaction> { | ||
private provider: TonConnectUI; | ||
|
||
constructor(provider: TonConnectUI) { | ||
this.provider = provider; | ||
} | ||
|
||
async signMessage(): Promise<string> { | ||
throw SignerError.UnimplementedError('signMessage'); | ||
} | ||
|
||
async signAndSendTx(tx: TonTransaction): Promise<{ hash: string }> { | ||
const { blockChain, type, ...txObjectForSign } = tx; | ||
const result = await this.provider.sendTransaction({ | ||
...txObjectForSign, | ||
network: CHAIN.MAINNET, | ||
}); | ||
|
||
const hash = Cell.fromBase64(result.boc).hash().toString('hex'); | ||
return { hash }; | ||
} | ||
} |
This file contains 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,3 @@ | ||
export interface Environments extends Record<string, string | undefined> { | ||
manifestUrl: string; | ||
} |
Oops, something went wrong.