@reown/appkit-solana-react-native 0.0.0-accounts-canary.1-20251023174733
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/commonjs/adapter.js +271 -0
- package/lib/commonjs/adapter.js.map +1 -0
- package/lib/commonjs/connectors/DeeplinkConnector.js +271 -0
- package/lib/commonjs/connectors/DeeplinkConnector.js.map +1 -0
- package/lib/commonjs/connectors/PhantomConnector.js +39 -0
- package/lib/commonjs/connectors/PhantomConnector.js.map +1 -0
- package/lib/commonjs/connectors/SolflareConnector.js +39 -0
- package/lib/commonjs/connectors/SolflareConnector.js.map +1 -0
- package/lib/commonjs/helpers.js +102 -0
- package/lib/commonjs/helpers.js.map +1 -0
- package/lib/commonjs/index.js +27 -0
- package/lib/commonjs/index.js.map +1 -0
- package/lib/commonjs/package.json +1 -0
- package/lib/commonjs/providers/DeeplinkProvider.js +432 -0
- package/lib/commonjs/providers/DeeplinkProvider.js.map +1 -0
- package/lib/commonjs/types.js +6 -0
- package/lib/commonjs/types.js.map +1 -0
- package/lib/commonjs/utils/createSPLTokenTransaction.js +96 -0
- package/lib/commonjs/utils/createSPLTokenTransaction.js.map +1 -0
- package/lib/commonjs/utils/createSendTransaction.js +30 -0
- package/lib/commonjs/utils/createSendTransaction.js.map +1 -0
- package/lib/module/adapter.js +265 -0
- package/lib/module/adapter.js.map +1 -0
- package/lib/module/connectors/DeeplinkConnector.js +265 -0
- package/lib/module/connectors/DeeplinkConnector.js.map +1 -0
- package/lib/module/connectors/PhantomConnector.js +34 -0
- package/lib/module/connectors/PhantomConnector.js.map +1 -0
- package/lib/module/connectors/SolflareConnector.js +34 -0
- package/lib/module/connectors/SolflareConnector.js.map +1 -0
- package/lib/module/helpers.js +95 -0
- package/lib/module/helpers.js.map +1 -0
- package/lib/module/index.js +11 -0
- package/lib/module/index.js.map +1 -0
- package/lib/module/providers/DeeplinkProvider.js +426 -0
- package/lib/module/providers/DeeplinkProvider.js.map +1 -0
- package/lib/module/types.js +4 -0
- package/lib/module/types.js.map +1 -0
- package/lib/module/utils/createSPLTokenTransaction.js +92 -0
- package/lib/module/utils/createSPLTokenTransaction.js.map +1 -0
- package/lib/module/utils/createSendTransaction.js +26 -0
- package/lib/module/utils/createSendTransaction.js.map +1 -0
- package/lib/typescript/adapter.d.ts +23 -0
- package/lib/typescript/adapter.d.ts.map +1 -0
- package/lib/typescript/connectors/DeeplinkConnector.d.ts +30 -0
- package/lib/typescript/connectors/DeeplinkConnector.d.ts.map +1 -0
- package/lib/typescript/connectors/PhantomConnector.d.ts +12 -0
- package/lib/typescript/connectors/PhantomConnector.d.ts.map +1 -0
- package/lib/typescript/connectors/SolflareConnector.d.ts +12 -0
- package/lib/typescript/connectors/SolflareConnector.d.ts.map +1 -0
- package/lib/typescript/helpers.d.ts +31 -0
- package/lib/typescript/helpers.d.ts.map +1 -0
- package/lib/typescript/index.d.ts +5 -0
- package/lib/typescript/index.d.ts.map +1 -0
- package/lib/typescript/providers/DeeplinkProvider.d.ts +59 -0
- package/lib/typescript/providers/DeeplinkProvider.d.ts.map +1 -0
- package/lib/typescript/types.d.ts +99 -0
- package/lib/typescript/types.d.ts.map +1 -0
- package/lib/typescript/utils/createSPLTokenTransaction.d.ts +4 -0
- package/lib/typescript/utils/createSPLTokenTransaction.d.ts.map +1 -0
- package/lib/typescript/utils/createSendTransaction.d.ts +10 -0
- package/lib/typescript/utils/createSendTransaction.d.ts.map +1 -0
- package/package.json +54 -0
- package/readme.md +9 -0
- package/src/adapter.ts +315 -0
- package/src/connectors/DeeplinkConnector.ts +353 -0
- package/src/connectors/PhantomConnector.ts +36 -0
- package/src/connectors/SolflareConnector.ts +36 -0
- package/src/helpers.ts +102 -0
- package/src/index.ts +9 -0
- package/src/providers/DeeplinkProvider.ts +605 -0
- package/src/types.ts +132 -0
- package/src/utils/createSPLTokenTransaction.ts +152 -0
- package/src/utils/createSendTransaction.ts +41 -0
package/src/types.ts
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
CaipNetworkId,
|
|
3
|
+
Namespaces,
|
|
4
|
+
Storage,
|
|
5
|
+
WalletInfo
|
|
6
|
+
} from '@reown/appkit-common-react-native';
|
|
7
|
+
import type nacl from 'tweetnacl';
|
|
8
|
+
import type { Connection } from '@solana/web3.js';
|
|
9
|
+
|
|
10
|
+
// --- From helpers ---
|
|
11
|
+
|
|
12
|
+
export interface TokenInfo {
|
|
13
|
+
address: string;
|
|
14
|
+
symbol: string;
|
|
15
|
+
name: string;
|
|
16
|
+
decimals: number;
|
|
17
|
+
logoURI?: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export type SPLTokenTransactionArgs = {
|
|
21
|
+
connection: Connection;
|
|
22
|
+
fromAddress: string;
|
|
23
|
+
toAddress: string;
|
|
24
|
+
amount: number;
|
|
25
|
+
tokenMint: string;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
// --- From PhantomProvider ---
|
|
29
|
+
|
|
30
|
+
export type Cluster = 'mainnet-beta' | 'testnet' | 'devnet';
|
|
31
|
+
|
|
32
|
+
export type DeeplinkConnectResult = DeeplinkSession;
|
|
33
|
+
|
|
34
|
+
export interface DeeplinkSession {
|
|
35
|
+
sessionToken: string;
|
|
36
|
+
userPublicKey: string;
|
|
37
|
+
walletEncryptionPublicKeyBs58: string;
|
|
38
|
+
cluster: Cluster;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export interface SignTransactionRequestParams {
|
|
42
|
+
transaction: string;
|
|
43
|
+
}
|
|
44
|
+
export interface SignMessageRequestParams {
|
|
45
|
+
message: Uint8Array | string;
|
|
46
|
+
display?: 'utf8' | 'hex';
|
|
47
|
+
}
|
|
48
|
+
export interface SignAllTransactionsRequestParams {
|
|
49
|
+
transactions: string[];
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export interface DeeplinkResponse {
|
|
53
|
+
wallet_encryption_public_key?: string;
|
|
54
|
+
nonce: string;
|
|
55
|
+
data: string;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export interface DecryptedConnectData {
|
|
59
|
+
public_key: string;
|
|
60
|
+
session: string;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export interface DeeplinkProviderConfig {
|
|
64
|
+
appScheme: string;
|
|
65
|
+
dappUrl: string;
|
|
66
|
+
storage: Storage;
|
|
67
|
+
cluster?: Cluster;
|
|
68
|
+
dappEncryptionKeyPair: nacl.BoxKeyPair;
|
|
69
|
+
type: 'phantom' | 'solflare';
|
|
70
|
+
baseUrl: string;
|
|
71
|
+
encryptionKeyFieldName: string;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// Actual method names used in deeplink URLs
|
|
75
|
+
export type DeeplinkRpcMethod =
|
|
76
|
+
| 'connect'
|
|
77
|
+
| 'disconnect'
|
|
78
|
+
| 'signTransaction'
|
|
79
|
+
| 'signAndSendTransaction'
|
|
80
|
+
| 'signAllTransactions'
|
|
81
|
+
| 'signMessage';
|
|
82
|
+
|
|
83
|
+
export interface DeeplinkSignTransactionParams {
|
|
84
|
+
dapp_encryption_public_key: string;
|
|
85
|
+
redirect_link: string;
|
|
86
|
+
payload: string; // Encrypted JSON: { session: string, transaction: string }
|
|
87
|
+
nonce: string;
|
|
88
|
+
cluster?: Cluster;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export interface DeeplinkSignAllTransactionsParams {
|
|
92
|
+
dapp_encryption_public_key: string;
|
|
93
|
+
redirect_link: string;
|
|
94
|
+
payload: string; // Encrypted JSON: { session: string, transactions: string[] }
|
|
95
|
+
nonce: string;
|
|
96
|
+
cluster?: Cluster;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export interface DeeplinkSignMessageParams {
|
|
100
|
+
dapp_encryption_public_key: string;
|
|
101
|
+
redirect_link: string;
|
|
102
|
+
payload: string; // Encrypted JSON string: { message: string, session: string, display: 'utf8'|'hex' }
|
|
103
|
+
nonce: string;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export interface DeeplinkConnectParams {
|
|
107
|
+
app_url: string;
|
|
108
|
+
dapp_encryption_public_key: string;
|
|
109
|
+
redirect_link: string;
|
|
110
|
+
cluster?: Cluster;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export interface DeeplinkDisconnectParams {
|
|
114
|
+
dapp_encryption_public_key: string;
|
|
115
|
+
redirect_link: string;
|
|
116
|
+
payload: string; // Encrypted { session: string }
|
|
117
|
+
nonce: string;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export interface DeeplinkConnectorConfig {
|
|
121
|
+
type: 'phantom' | 'solflare';
|
|
122
|
+
cluster?: Cluster;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
export interface DeeplinkConnectorSessionData {
|
|
126
|
+
namespaces: Namespaces;
|
|
127
|
+
wallet: WalletInfo;
|
|
128
|
+
currentCaipNetworkId: CaipNetworkId;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export type PhantomConnectorConfig = Pick<DeeplinkConnectorConfig, 'cluster'>;
|
|
132
|
+
export type SolflareConnectorConfig = Pick<DeeplinkConnectorConfig, 'cluster'>;
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import {
|
|
2
|
+
TOKEN_2022_PROGRAM_ID,
|
|
3
|
+
TOKEN_PROGRAM_ID,
|
|
4
|
+
TokenAccountNotFoundError,
|
|
5
|
+
createAssociatedTokenAccountInstruction,
|
|
6
|
+
createTransferCheckedInstruction,
|
|
7
|
+
getAccount,
|
|
8
|
+
getAssociatedTokenAddressSync,
|
|
9
|
+
getMint
|
|
10
|
+
} from '@solana/spl-token';
|
|
11
|
+
import {
|
|
12
|
+
ComputeBudgetProgram,
|
|
13
|
+
Connection,
|
|
14
|
+
PublicKey,
|
|
15
|
+
Transaction,
|
|
16
|
+
type TransactionInstruction
|
|
17
|
+
} from '@solana/web3.js';
|
|
18
|
+
import type { SPLTokenTransactionArgs } from '../types';
|
|
19
|
+
|
|
20
|
+
const SPL_COMPUTE_BUDGET_CONSTANTS = {
|
|
21
|
+
UNIT_PRICE_MICRO_LAMPORTS: 1000000,
|
|
22
|
+
UNIT_LIMIT_WITH_ATA_CREATION: 400000
|
|
23
|
+
} as const;
|
|
24
|
+
|
|
25
|
+
async function getMintOwnerProgramId(connection: Connection, mint: PublicKey) {
|
|
26
|
+
const info = await connection.getAccountInfo(mint);
|
|
27
|
+
|
|
28
|
+
if (!info) {
|
|
29
|
+
throw new Error('Mint account not found');
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
if (info.owner.equals(TOKEN_PROGRAM_ID)) {
|
|
33
|
+
return TOKEN_PROGRAM_ID;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
if (info.owner.equals(TOKEN_2022_PROGRAM_ID)) {
|
|
37
|
+
return TOKEN_2022_PROGRAM_ID;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
throw new Error('Unknown mint owner program');
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export async function createSPLTokenTransaction({
|
|
44
|
+
fromAddress,
|
|
45
|
+
toAddress,
|
|
46
|
+
amount,
|
|
47
|
+
tokenMint,
|
|
48
|
+
connection
|
|
49
|
+
}: SPLTokenTransactionArgs): Promise<Transaction> {
|
|
50
|
+
if (!fromAddress) {
|
|
51
|
+
throw new Error('No public key found');
|
|
52
|
+
}
|
|
53
|
+
if (amount <= 0) {
|
|
54
|
+
throw new Error('Amount must be greater than 0');
|
|
55
|
+
}
|
|
56
|
+
try {
|
|
57
|
+
const fromPubkey = new PublicKey(fromAddress);
|
|
58
|
+
const toPubkey = new PublicKey(toAddress);
|
|
59
|
+
const mintPubkey = new PublicKey(tokenMint);
|
|
60
|
+
|
|
61
|
+
const programId = await getMintOwnerProgramId(connection, mintPubkey);
|
|
62
|
+
|
|
63
|
+
const mintInfo = await getMint(connection, mintPubkey, undefined, programId);
|
|
64
|
+
const decimals = mintInfo.decimals;
|
|
65
|
+
if (decimals < 0) {
|
|
66
|
+
throw new Error('Invalid token decimals');
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const tokenAmount = Math.floor(amount * 10 ** decimals);
|
|
70
|
+
|
|
71
|
+
const fromTokenAccount = getAssociatedTokenAddressSync(
|
|
72
|
+
mintPubkey,
|
|
73
|
+
fromPubkey,
|
|
74
|
+
false,
|
|
75
|
+
programId
|
|
76
|
+
);
|
|
77
|
+
const toTokenAccount = getAssociatedTokenAddressSync(mintPubkey, toPubkey, false, programId);
|
|
78
|
+
|
|
79
|
+
try {
|
|
80
|
+
const fromAccount = await getAccount(connection, fromTokenAccount, undefined, programId);
|
|
81
|
+
if (fromAccount.amount < BigInt(tokenAmount)) {
|
|
82
|
+
throw new Error('Insufficient token balance');
|
|
83
|
+
}
|
|
84
|
+
} catch (error) {
|
|
85
|
+
if (error instanceof TokenAccountNotFoundError) {
|
|
86
|
+
throw new Error('Sender does not have a token account for this mint');
|
|
87
|
+
}
|
|
88
|
+
throw error;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
let shouldCreateATA = false;
|
|
92
|
+
try {
|
|
93
|
+
await getAccount(connection, toTokenAccount, undefined, programId);
|
|
94
|
+
} catch (error) {
|
|
95
|
+
if (error instanceof TokenAccountNotFoundError) {
|
|
96
|
+
shouldCreateATA = true;
|
|
97
|
+
} else {
|
|
98
|
+
throw error;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const instructions: TransactionInstruction[] = [];
|
|
103
|
+
|
|
104
|
+
if (shouldCreateATA) {
|
|
105
|
+
instructions.push(
|
|
106
|
+
ComputeBudgetProgram.setComputeUnitPrice({
|
|
107
|
+
microLamports: SPL_COMPUTE_BUDGET_CONSTANTS.UNIT_PRICE_MICRO_LAMPORTS
|
|
108
|
+
}),
|
|
109
|
+
ComputeBudgetProgram.setComputeUnitLimit({
|
|
110
|
+
units: SPL_COMPUTE_BUDGET_CONSTANTS.UNIT_LIMIT_WITH_ATA_CREATION
|
|
111
|
+
})
|
|
112
|
+
);
|
|
113
|
+
|
|
114
|
+
instructions.push(
|
|
115
|
+
createAssociatedTokenAccountInstruction(
|
|
116
|
+
fromPubkey,
|
|
117
|
+
toTokenAccount,
|
|
118
|
+
toPubkey,
|
|
119
|
+
mintPubkey,
|
|
120
|
+
programId
|
|
121
|
+
)
|
|
122
|
+
);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
instructions.push(
|
|
126
|
+
createTransferCheckedInstruction(
|
|
127
|
+
fromTokenAccount,
|
|
128
|
+
mintPubkey,
|
|
129
|
+
toTokenAccount,
|
|
130
|
+
fromPubkey,
|
|
131
|
+
tokenAmount,
|
|
132
|
+
decimals,
|
|
133
|
+
[],
|
|
134
|
+
programId
|
|
135
|
+
)
|
|
136
|
+
);
|
|
137
|
+
|
|
138
|
+
const { blockhash, lastValidBlockHeight } = await connection.getLatestBlockhash();
|
|
139
|
+
|
|
140
|
+
return new Transaction({
|
|
141
|
+
feePayer: fromPubkey,
|
|
142
|
+
blockhash,
|
|
143
|
+
lastValidBlockHeight
|
|
144
|
+
}).add(...instructions);
|
|
145
|
+
} catch (error) {
|
|
146
|
+
throw new Error(
|
|
147
|
+
`Failed to create SPL token transaction: ${
|
|
148
|
+
error instanceof Error ? error.message : 'Unknown error'
|
|
149
|
+
}`
|
|
150
|
+
);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type Connection,
|
|
3
|
+
LAMPORTS_PER_SOL,
|
|
4
|
+
PublicKey,
|
|
5
|
+
SystemProgram,
|
|
6
|
+
Transaction
|
|
7
|
+
} from '@solana/web3.js';
|
|
8
|
+
|
|
9
|
+
type SendTransactionArgs = {
|
|
10
|
+
connection: Connection;
|
|
11
|
+
fromAddress: string;
|
|
12
|
+
toAddress: string;
|
|
13
|
+
amount: number;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export async function createSendTransaction({
|
|
17
|
+
fromAddress,
|
|
18
|
+
toAddress,
|
|
19
|
+
amount,
|
|
20
|
+
connection
|
|
21
|
+
}: SendTransactionArgs): Promise<Transaction> {
|
|
22
|
+
const fromPubkey = new PublicKey(fromAddress);
|
|
23
|
+
const toPubkey = new PublicKey(toAddress);
|
|
24
|
+
const lamports = Math.floor(amount * LAMPORTS_PER_SOL);
|
|
25
|
+
|
|
26
|
+
const { blockhash } = await connection.getLatestBlockhash();
|
|
27
|
+
|
|
28
|
+
const instructions = [
|
|
29
|
+
SystemProgram.transfer({
|
|
30
|
+
fromPubkey,
|
|
31
|
+
toPubkey,
|
|
32
|
+
lamports
|
|
33
|
+
})
|
|
34
|
+
];
|
|
35
|
+
|
|
36
|
+
const transaction = new Transaction().add(...instructions);
|
|
37
|
+
transaction.feePayer = fromPubkey;
|
|
38
|
+
transaction.recentBlockhash = blockhash;
|
|
39
|
+
|
|
40
|
+
return transaction;
|
|
41
|
+
}
|