Skip to content

Commit 9a7eed9

Browse files
s100tistmkurapov
andauthored
fix(backend): make the rafiki wallet url case insensitive (#2833)
* wallet address url stored in database converted to lower case (packages/backend/src/graphql/resolvers/wallet_address.ts). middleware.ts in packages/backend/src/open_payments/wallet_address/middleware.ts modified to look up for the walletaddress url in a case insensitive way. * correcting formating * rollback of to lower case in openpayments/walletaddress/middleware and graphqlresolvers * Max suggestion * Removing logs:( * Update service.ts * query corrected * return deleted * using toLowerCase in the request * format changes * tests for wallet address draft * tests added * duplied wallet addresses handling * Update packages/backend/src/open_payments/wallet_address/errors.ts Co-authored-by: Max Kurapov <[email protected]> --------- Co-authored-by: Max Kurapov <[email protected]>
1 parent 60c06cc commit 9a7eed9

File tree

3 files changed

+41
-6
lines changed

3 files changed

+41
-6
lines changed

packages/backend/src/open_payments/wallet_address/errors.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import { GraphQLErrorCode } from '../../graphql/errors'
33
export enum WalletAddressError {
44
InvalidUrl = 'InvalidUrl',
55
UnknownAsset = 'UnknownAsset',
6-
UnknownWalletAddress = 'UnknownWalletAddress'
6+
UnknownWalletAddress = 'UnknownWalletAddress',
7+
DuplicateWalletAddress = 'DuplicateWalletAddress'
78
}
89

910
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
@@ -15,13 +16,16 @@ export const errorToCode: {
1516
} = {
1617
[WalletAddressError.InvalidUrl]: GraphQLErrorCode.BadUserInput,
1718
[WalletAddressError.UnknownAsset]: GraphQLErrorCode.BadUserInput,
18-
[WalletAddressError.UnknownWalletAddress]: GraphQLErrorCode.NotFound
19+
[WalletAddressError.UnknownWalletAddress]: GraphQLErrorCode.NotFound,
20+
[WalletAddressError.DuplicateWalletAddress]: GraphQLErrorCode.Duplicate
1921
}
2022

2123
export const errorToMessage: {
2224
[key in WalletAddressError]: string
2325
} = {
2426
[WalletAddressError.InvalidUrl]: 'invalid url',
2527
[WalletAddressError.UnknownAsset]: 'unknown asset',
26-
[WalletAddressError.UnknownWalletAddress]: 'unknown wallet address'
28+
[WalletAddressError.UnknownWalletAddress]: 'unknown wallet address',
29+
[WalletAddressError.DuplicateWalletAddress]:
30+
'Duplicate wallet address found with the same url'
2731
}

packages/backend/src/open_payments/wallet_address/service.test.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,31 @@ describe('Open Payments Wallet Address Service', (): void => {
135135
accountingService.getBalance(walletAddress.id)
136136
).resolves.toBeUndefined()
137137
})
138+
139+
test('Creating wallet address with case insensitiveness', async (): Promise<void> => {
140+
const url = 'https://Alice.me/pay'
141+
await expect(
142+
walletAddressService.create({
143+
...options,
144+
url
145+
})
146+
).resolves.toMatchObject({ url: url.toLowerCase() })
147+
})
148+
149+
test('Wallet address cannot be created if the url is duplicated', async (): Promise<void> => {
150+
const url = 'https://Alice.me/pay'
151+
const wallet = walletAddressService.create({
152+
...options,
153+
url
154+
})
155+
assert.ok(!isWalletAddressError(wallet))
156+
await expect(
157+
walletAddressService.create({
158+
...options,
159+
url
160+
})
161+
).resolves.toEqual(WalletAddressError.DuplicateWalletAddress)
162+
})
138163
})
139164

140165
describe('Update Wallet Address', (): void => {

packages/backend/src/open_payments/wallet_address/service.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import {
22
ForeignKeyViolationError,
33
TransactionOrKnex,
4-
NotFoundError
4+
NotFoundError,
5+
UniqueViolationError
56
} from 'objection'
67
import { URL } from 'url'
78

@@ -166,7 +167,7 @@ async function createWalletAddress(
166167

167168
return await WalletAddress.query(deps.knex)
168169
.insertGraphAndFetch({
169-
url: options.url,
170+
url: options.url.toLowerCase(),
170171
publicName: options.publicName,
171172
assetId: options.assetId,
172173
additionalProperties: additionalProperties
@@ -178,6 +179,11 @@ async function createWalletAddress(
178179
return WalletAddressError.UnknownAsset
179180
}
180181
}
182+
if (err instanceof UniqueViolationError) {
183+
if (err.constraint === 'walletaddresses_url_unique') {
184+
return WalletAddressError.DuplicateWalletAddress
185+
}
186+
}
181187
throw err
182188
}
183189
}
@@ -296,7 +302,7 @@ async function getWalletAddressByUrl(
296302
url: string
297303
): Promise<WalletAddress | undefined> {
298304
const walletAddress = await WalletAddress.query(deps.knex)
299-
.findOne({ url })
305+
.findOne({ url: url.toLowerCase() })
300306
.withGraphFetched('asset')
301307
return walletAddress || undefined
302308
}

0 commit comments

Comments
 (0)