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 json
2836}
2937
3038export function isWalletAddress (
@@ -44,6 +52,10 @@ export function isWalletAddress(
4452 )
4553}
4654
55+ export function toWalletAddressUrl ( s : string ) : string {
56+ return s . startsWith ( '$' ) ? s . replace ( '$' , 'https://' ) : s
57+ }
58+
4759export function normalizeWalletAddress ( walletAddress : WalletAddress ) : string {
4860 const IS_INTERLEDGER_CARDS =
4961 walletAddress . authServer === 'https://auth.interledger.cards'
0 commit comments