Skip to content

Commit 4e41c4f

Browse files
committed
merge getwalletaddress util
1 parent 263b549 commit 4e41c4f

File tree

3 files changed

+37
-52
lines changed

3 files changed

+37
-52
lines changed

frontend/app/routes/api.config.$type.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import {
1616
getValidWalletAddress
1717
} from '~/utils/open-payments.server.js'
1818
import { APP_BASEPATH } from '~/lib/constants.js'
19-
import { normalizeWalletAddress } from '@shared/utils'
2019

2120
export async function loader({ request, params, context }: LoaderFunctionArgs) {
2221
try {
@@ -97,7 +96,7 @@ export async function action({ request, params, context }: ActionFunctionArgs) {
9796
return json({ errors, success: false, intent }, { status: 400 })
9897
}
9998

100-
let ownerWalletAddress: string = payload.walletAddress
99+
const ownerWalletAddress: string = payload.walletAddress
101100
const walletAddress = await getValidWalletAddress(env, ownerWalletAddress)
102101

103102
const session = await getSession(request.headers.get('Cookie'))
@@ -136,7 +135,6 @@ export async function action({ request, params, context }: ActionFunctionArgs) {
136135
}
137136
}
138137

139-
ownerWalletAddress = normalizeWalletAddress(walletAddress)
140138
const storageService = new ConfigStorageService(env)
141139
switch (request.method) {
142140
case 'POST':

frontend/app/utils/validate.server.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { z } from 'zod'
22
import { CornerType, PositionType, SlideAnimationType } from '@shared/types'
33
import {
44
checkHrefFormat,
5-
isValidWalletAddress,
5+
getWalletAddress,
66
toWalletAddressUrl,
77
WalletAddressFormatError
88
} from '@shared/utils'
@@ -19,7 +19,7 @@ export const walletSchema = z.object({
1919

2020
try {
2121
checkHrefFormat(updatedUrl)
22-
await isValidWalletAddress(updatedUrl)
22+
await getWalletAddress(updatedUrl)
2323
} catch (e) {
2424
ctx.addIssue({
2525
code: z.ZodIssueCode.custom,

shared/utils/index.ts

Lines changed: 34 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,38 @@
11
import type { WalletAddress } from '@interledger/open-payments'
22

3-
export function toWalletAddressUrl(s: string): string {
4-
return s.startsWith('$') ? s.replace('$', 'https://') : s
5-
}
6-
73
export 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

3038
export 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

6885
export 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

Comments
 (0)