Skip to content

Commit d7fdd8f

Browse files
committed
feat: integrate solana mobile adapter
1 parent 1b8d5e6 commit d7fdd8f

File tree

15 files changed

+74
-57
lines changed

15 files changed

+74
-57
lines changed

wallets/core/src/hub/store/providers.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export type ProviderInfo = {
1515
name: string;
1616
icon: string;
1717
extensions: Partial<Record<Browsers, string>>;
18+
showOnDesktop?: boolean;
1819
properties?: DetachedInstances[];
1920
};
2021

wallets/core/src/legacy/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ export type WalletInfo = {
124124
showOnMobile?: boolean;
125125
isContractWallet?: boolean;
126126
mobileWallet?: boolean;
127+
showOnDesktop?: boolean;
127128

128129
needsDerivationPath?: NeedsDerivationPath;
129130
needsNamespace?: NeedsNamespace;

wallets/provider-mobile-wallet-adapter/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
"lint": "eslint \"**/*.{ts,tsx}\""
2222
},
2323
"dependencies": {
24-
"@rango-dev/signer-solana": "^0.38.1-next.1",
25-
"@rango-dev/wallets-shared": "^0.43.1-next.5",
24+
"@rango-dev/signer-solana": "^0.39.1-next.0",
25+
"@rango-dev/wallets-shared": "^0.44.2-next.0",
2626
"@solana/web3.js": "^1.98.2",
2727
"@solana/wallet-adapter-base": "^0.9.26",
2828
"@solana-mobile/wallet-adapter-mobile": "^2.2.0",

wallets/provider-mobile-wallet-adapter/src/constants.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,9 @@ export const WALLET_ID = 'mobile-wallet-adapter';
44

55
export const info: ProviderInfo = {
66
name: 'Mobile Wallet Adapter',
7-
icon: 'https://raw.githubusercontent.com/rango-exchange/assets/main/wallets/phantom/icon.svg',
8-
extensions: {
9-
chrome:
10-
'https://chrome.google.com/webstore/detail/phantom/bfnaelmomeimhlpmgjnjophhpkkoljpa',
11-
homepage: 'https://phantom.app/',
12-
},
7+
icon: 'https://raw.githubusercontent.com/rango-exchange/assets/main/wallets/mobile-wallet-adapter/icon.svg',
8+
extensions: {},
9+
showOnDesktop: false,
1310
properties: [
1411
{
1512
name: 'detached',

wallets/provider-mobile-wallet-adapter/src/legacy/index.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,12 @@ import { chains as solanaChains } from '@rango-dev/wallets-core/namespaces/solan
77
import { getSolanaAccounts, WalletTypes } from '@rango-dev/wallets-shared';
88
import { solanaBlockchain } from 'rango-types';
99

10+
import signer from '../signers/signer.js';
1011
import {
1112
mobileWalletAdapter as phantom_instance,
1213
type Provider,
1314
} from '../utils.js';
1415

15-
import signer from './signer.js';
16-
1716
const WALLET = WalletTypes.MOBILE_WALLET_ADAPTER;
1817

1918
export const config = {
@@ -47,14 +46,12 @@ export const getWalletInfo: (allBlockChains: BlockchainMeta[]) => WalletInfo = (
4746

4847
return {
4948
name: 'Mobile Wallet Adapter',
50-
img: 'https://raw.githubusercontent.com/rango-exchange/assets/main/wallets/phantom/icon.svg',
51-
installLink: {
52-
CHROME:
53-
'https://chrome.google.com/webstore/detail/phantom/bfnaelmomeimhlpmgjnjophhpkkoljpa',
54-
55-
DEFAULT: 'https://phantom.app/',
56-
},
49+
img: 'https://raw.githubusercontent.com/rango-exchange/assets/main/wallets/mobile-wallet-adapter/icon.svg',
50+
installLink: '',
5751
color: '#4d40c6',
52+
showOnMobile: true,
53+
mobileWallet: true,
54+
showOnDesktop: false,
5855
// if you are adding a new namespace, don't forget to also update `properties`
5956
needsNamespace: {
6057
selection: 'multiple',
Lines changed: 44 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,74 @@
11
import type { CaipAccount } from '@rango-dev/wallets-core/namespaces/common';
22
import type { SolanaActions } from '@rango-dev/wallets-core/namespaces/solana';
3+
import type { PublicKey } from '@solana/web3.js';
34

4-
import { ActionBuilder, NamespaceBuilder } from '@rango-dev/wallets-core';
5+
import { NamespaceBuilder } from '@rango-dev/wallets-core';
56
import { builders as commonBuilders } from '@rango-dev/wallets-core/namespaces/common';
67
import {
78
builders,
89
CAIP_NAMESPACE,
910
CAIP_SOLANA_CHAIN_ID,
1011
} from '@rango-dev/wallets-core/namespaces/solana';
1112
import { CAIP } from '@rango-dev/wallets-core/utils';
13+
import {
14+
type WalletError,
15+
WalletReadyState,
16+
} from '@solana/wallet-adapter-base';
1217

1318
import { WALLET_ID } from '../constants.js';
1419
import { mobileWalletAdapter } from '../utils.js';
1520

1621
const connect = builders
1722
.connect()
1823
.action(async function () {
19-
const phantomInstance = mobileWalletAdapter();
20-
await phantomInstance.connect();
24+
const adapterInstance = mobileWalletAdapter();
25+
26+
const connectHandler = new Promise((resolve, reject) => {
27+
const handleConnect = (publicKey: PublicKey) => {
28+
const account = publicKey?.toString();
2129

22-
const account = phantomInstance.publicKey?.toString();
23-
if (!account) {
24-
throw new Error('Connected account is not found!');
30+
if (!account) {
31+
return reject(new Error('Connected account is not found!'));
32+
}
33+
return resolve([
34+
CAIP.AccountId.format({
35+
address: account,
36+
chainId: {
37+
namespace: CAIP_NAMESPACE,
38+
reference: CAIP_SOLANA_CHAIN_ID,
39+
},
40+
}) as CaipAccount,
41+
]);
42+
};
43+
const handleError = (error: WalletError) => {
44+
return reject(error);
45+
};
46+
adapterInstance.on('connect', handleConnect);
47+
adapterInstance.on('error', handleError);
48+
});
49+
50+
if (
51+
adapterInstance.readyState === WalletReadyState.Loadable ||
52+
adapterInstance.connected === true
53+
) {
54+
await adapterInstance.disconnect();
2555
}
26-
return [
27-
CAIP.AccountId.format({
28-
address: account,
29-
chainId: {
30-
namespace: CAIP_NAMESPACE,
31-
reference: CAIP_SOLANA_CHAIN_ID,
32-
},
33-
}) as CaipAccount,
34-
];
56+
await adapterInstance.connect();
57+
return connectHandler;
3558
})
3659
.build();
3760

38-
const disconnect = commonBuilders.disconnect<SolanaActions>().build();
39-
40-
export const canEagerConnectAction = async () => {
41-
const phantomInstance = mobileWalletAdapter();
42-
try {
43-
await phantomInstance.autoConnect();
44-
} catch {
45-
return false;
46-
}
47-
};
48-
49-
const canEagerConnect = new ActionBuilder<SolanaActions, 'canEagerConnect'>(
50-
'canEagerConnect'
51-
)
52-
.action(canEagerConnectAction)
61+
const disconnect = commonBuilders
62+
.disconnect<SolanaActions>()
63+
.before(async () => {
64+
const adapterInstance = mobileWalletAdapter();
65+
await adapterInstance.disconnect();
66+
})
5367
.build();
5468

5569
const solana = new NamespaceBuilder<SolanaActions>('Solana', WALLET_ID)
5670
.action(connect)
5771
.action(disconnect)
58-
.action(canEagerConnect)
5972
.build();
6073

6174
export { solana };

wallets/provider-mobile-wallet-adapter/src/provider.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,15 @@ import { ProviderBuilder } from '@rango-dev/wallets-core';
22

33
import { info, WALLET_ID } from './constants.js';
44
import { solana } from './namespaces/solana.js';
5-
import { mobileWalletAdapter as phantomInstance } from './utils.js';
5+
import { mobileWalletAdapter } from './utils.js';
66

77
const buildProvider = () =>
88
new ProviderBuilder(WALLET_ID)
99
.init(function (context) {
1010
const [, setState] = context.state();
1111

12-
if (phantomInstance()) {
12+
if (mobileWalletAdapter()) {
1313
setState('installed', true);
14-
console.debug('[phantom] instance detected.', context);
1514
}
1615
})
1716
.config('info', info)

wallets/provider-mobile-wallet-adapter/src/utils.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import type { Adapter } from '@solana/wallet-adapter-base';
2+
13
import { WalletAdapterNetwork } from '@solana/wallet-adapter-base';
24
import {
35
type Cluster,
@@ -13,26 +15,26 @@ import {
1315
} from '@solana-mobile/wallet-adapter-mobile';
1416

1517
export type Provider = Map<string, unknown>;
16-
let _mobileWalletAdapter: SolanaMobileWalletAdapter;
1718
const CLUSTER = WalletAdapterNetwork.Mainnet;
1819
const CONNECTION_CONFIG: ConnectionConfig = { commitment: 'processed' };
1920
const ENDPOINT = /*#__PURE__*/ clusterApiUrl(CLUSTER);
21+
let _mobileWalletAdapter: Adapter;
2022
let _connection: Connection;
2123
export function connection(): Connection {
2224
if (!_connection) {
2325
_connection = new Connection(ENDPOINT, CONNECTION_CONFIG);
2426
}
2527
return _connection;
2628
}
27-
export function mobileWalletAdapter(): SolanaMobileWalletAdapter {
29+
export function mobileWalletAdapter(): Adapter {
2830
if (!_mobileWalletAdapter) {
2931
_mobileWalletAdapter = new SolanaMobileWalletAdapter({
3032
addressSelector: createDefaultAddressSelector(),
3133
appIdentity: {
3234
uri: getUriForAppIdentity(),
3335
},
3436
authorizationResultCache: createDefaultAuthorizationResultCache(),
35-
cluster: getInferredClusterFromEndpoint(connection()?.rpcEndpoint),
37+
chain: getInferredClusterFromEndpoint(connection()?.rpcEndpoint),
3638
onWalletNotFound: createDefaultWalletNotFoundHandler(),
3739
});
3840
}

wallets/provider-mobile-wallet-adapter/tsconfig.build.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
"compilerOptions": {
77
"outDir": "dist",
88
"rootDir": "./src",
9-
"lib": ["dom", "esnext"]
9+
"target": "esnext",
10+
"lib": ["dom", "dom.iterable", "esnext"],
11+
"skipLibCheck": true
12+
1013
// match output dir to input dir. e.g. dist/index instead of dist/src/index
1114
}
1215
}

0 commit comments

Comments
 (0)