11import type { WalletAddress } from '@interledger/open-payments'
22
3- export function toWalletAddressUrl ( s : string ) : string {
4- return s . startsWith ( '$' ) ? s . replace ( '$' , 'https://' ) : s
5- }
6-
73export async function getWalletAddress (
84 walletAddressUrl : string
95) : Promise < WalletAddress > {
106 const url = toWalletAddressUrl ( walletAddressUrl )
117
12- const res = await fetch ( url )
13- if ( ! res . ok ) {
14- throw new Error ( 'Unable to fetch wallet details' , {
15- cause : new Error ( res . statusText || `HTTP ${ res . status } ` )
8+ const response = await fetch ( url )
9+ if ( ! response . ok ) {
10+ if ( response . status === 404 ) {
11+ throw new WalletAddressFormatError ( 'This wallet address does not exist' )
12+ }
13+ throw new WalletAddressFormatError ( 'Unable to fetch wallet details' , {
14+ cause : new WalletAddressFormatError (
15+ response . statusText || `HTTP ${ response . status } `
16+ )
1617 } )
1718 }
1819
20+ let json : Record < string , unknown >
1921 try {
20- const json : Record < string , unknown > = await res . json ( )
21- if ( ! isWalletAddress ( json ) ) {
22- throw new Error ( 'Invalid wallet address format' )
23- }
24- return json
22+ json = await response . json ( )
2523 } catch ( error ) {
26- throw new Error ( 'Failed to parse wallet address content' , { cause : error } )
24+ throw new WalletAddressFormatError (
25+ 'Provided URL is not a valid wallet address' ,
26+ {
27+ cause : error
28+ }
29+ )
30+ }
31+ if ( ! isWalletAddress ( json ) ) {
32+ throw new WalletAddressFormatError ( 'Invalid wallet address format' )
2733 }
34+
35+ return normalizeWalletAddress ( json )
2836}
2937
3038export function isWalletAddress (
@@ -44,7 +52,13 @@ export function isWalletAddress(
4452 )
4553}
4654
47- export function normalizeWalletAddress ( walletAddress : WalletAddress ) : string {
55+ export function toWalletAddressUrl ( s : string ) : string {
56+ return s . startsWith ( '$' ) ? s . replace ( '$' , 'https://' ) : s
57+ }
58+
59+ export function normalizeWalletAddress (
60+ walletAddress : WalletAddress
61+ ) : WalletAddress {
4862 const IS_INTERLEDGER_CARDS =
4963 walletAddress . authServer === 'https://auth.interledger.cards'
5064 const url = new URL ( toWalletAddressUrl ( walletAddress . id ) )
@@ -60,9 +74,12 @@ export function normalizeWalletAddress(walletAddress: WalletAddress): string {
6074 //
6175 // Not all `ilp.interledger.cards` wallet addresses can be used with `ilp.dev`.
6276 // Manually created wallet addresses cannot be used with `ilp.dev`.
63- return walletAddress . id . replace ( 'ilp.dev' , 'ilp.interledger.cards' )
77+ return {
78+ ...walletAddress ,
79+ id : walletAddress . id . replace ( 'ilp.dev' , 'ilp.interledger.cards' )
80+ }
6481 }
65- return walletAddress . id
82+ return walletAddress
6683}
6784
6885export class WalletAddressFormatError extends Error {
@@ -100,33 +117,3 @@ export function checkHrefFormat(href: string): string {
100117
101118 return href
102119}
103-
104- export async function isValidWalletAddress (
105- walletAddressUrl : string
106- ) : Promise < boolean > {
107- const response = await fetch ( walletAddressUrl , {
108- headers : {
109- Accept : 'application/json'
110- }
111- } )
112-
113- if ( ! response . ok ) {
114- if ( response . status === 404 ) {
115- throw new WalletAddressFormatError ( 'This wallet address does not exist.' )
116- }
117- throw new WalletAddressFormatError ( 'Failed to fetch wallet address.' )
118- }
119-
120- const msgInvalidWalletAddress = 'Provided URL is not a valid wallet address.'
121- const json = await response . json ( ) . catch ( ( error ) => {
122- throw new WalletAddressFormatError ( msgInvalidWalletAddress , {
123- cause : error
124- } )
125- } )
126-
127- if ( ! isWalletAddress ( json as Record < string , unknown > ) ) {
128- throw new WalletAddressFormatError ( msgInvalidWalletAddress )
129- }
130-
131- return true
132- }
0 commit comments