forked from Txio-labs/txio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstellar.ts
More file actions
264 lines (233 loc) · 6.09 KB
/
Copy pathstellar.ts
File metadata and controls
264 lines (233 loc) · 6.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
import {
getAddress as getFreighterAddress,
isConnected as isFreighterConnected,
requestAccess
} from '@stellar/freighter-api';
import {
getPublicKey as getLobstrPublicKey,
isConnected as isLobstrConnected
} from '@lobstrco/signer-extension-api';
import type {
ConnectedWallet,
WalletBalanceInfo,
WalletChainInfo,
WalletId
} from './types';
export type StellarNetworkName =
| 'public'
| 'testnet';
const stellarNetwork =
process.env
.NEXT_PUBLIC_STELLAR_NETWORK ===
'testnet'
? 'testnet'
: 'public';
const STELLAR_NETWORKS: Record<
StellarNetworkName,
{
chain: WalletChainInfo;
horizonUrl: string;
}
> = {
public: {
chain: {
id: 'stellar:public',
family: 'stellar',
name: 'Stellar',
network: 'public',
isSupported: true
},
horizonUrl:
'https://horizon.stellar.org'
},
testnet: {
chain: {
id: 'stellar:testnet',
family: 'stellar',
name: 'Stellar Testnet',
network: 'testnet',
isSupported: true
},
horizonUrl:
'https://horizon-testnet.stellar.org'
}
};
const getChainConfig = () =>
STELLAR_NETWORKS[stellarNetwork];
const buildWallet = (
walletId: WalletId,
address: string
): ConnectedWallet => ({
id: walletId,
name:
walletId === 'lobstr'
? 'LOBSTR'
: 'Freighter',
address,
family: 'stellar',
chain: getChainConfig().chain,
connectorName:
walletId === 'lobstr'
? 'LOBSTR Signer'
: 'Freighter API',
connectedAt: Date.now()
});
export const detectStellarWallets =
async () => {
const [lobstr, freighter] =
await Promise.allSettled([
isLobstrConnected(),
isFreighterConnected()
]);
const browserWindow =
typeof window !== 'undefined'
? window
: undefined;
return {
lobstr:
lobstr.status ===
'fulfilled' &&
Boolean(lobstr.value),
freighter:
freighter.status ===
'fulfilled' &&
Boolean(
freighter.value
.isConnected
),
albedo: Boolean(
browserWindow?.albedo
),
xbull: Boolean(
browserWindow?.xBullSDK
),
rabet: Boolean(
browserWindow?.rabet
),
hana: Boolean(
browserWindow?.hana
)
};
};
export const connectStellarWallet =
async (
walletId: WalletId
): Promise<ConnectedWallet> => {
if (walletId === 'lobstr') {
const installed =
await isLobstrConnected();
if (!installed) {
throw new Error(
'LOBSTR signer extension is not installed.'
);
}
const address =
await getLobstrPublicKey();
if (!address) {
throw new Error(
'LOBSTR did not return a public key.'
);
}
return buildWallet(
'lobstr',
address
);
}
const access =
await requestAccess();
if (access.error) {
throw new Error(
access.error.message ||
'Freighter denied the connection request.'
);
}
if (!access.address) {
throw new Error(
'Freighter did not return an address.'
);
}
return buildWallet(
'freighter',
access.address
);
};
export const restoreStellarWallet =
async (
walletId: WalletId
): Promise<ConnectedWallet | null> => {
if (walletId === 'lobstr') {
const installed =
await isLobstrConnected();
if (!installed) {
return null;
}
const address =
await getLobstrPublicKey();
return address
? buildWallet(
'lobstr',
address
)
: null;
}
const installed =
await isFreighterConnected();
if (!installed.isConnected) {
return null;
}
const address =
await getFreighterAddress();
if (address.error || !address.address) {
return null;
}
return buildWallet(
'freighter',
address.address
);
};
export const fetchStellarBalance =
async (
address: string
): Promise<WalletBalanceInfo> => {
const response =
await fetch(
`${getChainConfig().horizonUrl}/accounts/${address}`,
{
headers: {
Accept: 'application/json'
}
}
);
if (!response.ok) {
throw new Error(
'Unable to fetch Stellar balance.'
);
}
const account =
await response.json();
const nativeBalance =
Array.isArray(
account?.balances
)
? account.balances.find(
(
entry: {
asset_type?: string;
}
) =>
entry.asset_type ===
'native'
)
: null;
const value =
nativeBalance?.balance ||
'0';
return {
symbol: 'XLM',
formatted: `${Number(
value
).toFixed(4)} XLM`,
value,
decimals: 7
};
};