Skip to content

Commit

Permalink
fix(backend): make the rafiki wallet url case insensitive (#2833)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
s100tist and mkurapov authored Sep 18, 2024
1 parent 60c06cc commit 9a7eed9
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
10 changes: 7 additions & 3 deletions packages/backend/src/open_payments/wallet_address/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { GraphQLErrorCode } from '../../graphql/errors'
export enum WalletAddressError {
InvalidUrl = 'InvalidUrl',
UnknownAsset = 'UnknownAsset',
UnknownWalletAddress = 'UnknownWalletAddress'
UnknownWalletAddress = 'UnknownWalletAddress',
DuplicateWalletAddress = 'DuplicateWalletAddress'
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
Expand All @@ -15,13 +16,16 @@ export const errorToCode: {
} = {
[WalletAddressError.InvalidUrl]: GraphQLErrorCode.BadUserInput,
[WalletAddressError.UnknownAsset]: GraphQLErrorCode.BadUserInput,
[WalletAddressError.UnknownWalletAddress]: GraphQLErrorCode.NotFound
[WalletAddressError.UnknownWalletAddress]: GraphQLErrorCode.NotFound,
[WalletAddressError.DuplicateWalletAddress]: GraphQLErrorCode.Duplicate
}

export const errorToMessage: {
[key in WalletAddressError]: string
} = {
[WalletAddressError.InvalidUrl]: 'invalid url',
[WalletAddressError.UnknownAsset]: 'unknown asset',
[WalletAddressError.UnknownWalletAddress]: 'unknown wallet address'
[WalletAddressError.UnknownWalletAddress]: 'unknown wallet address',
[WalletAddressError.DuplicateWalletAddress]:
'Duplicate wallet address found with the same url'
}
25 changes: 25 additions & 0 deletions packages/backend/src/open_payments/wallet_address/service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,31 @@ describe('Open Payments Wallet Address Service', (): void => {
accountingService.getBalance(walletAddress.id)
).resolves.toBeUndefined()
})

test('Creating wallet address with case insensitiveness', async (): Promise<void> => {
const url = 'https://Alice.me/pay'
await expect(
walletAddressService.create({
...options,
url
})
).resolves.toMatchObject({ url: url.toLowerCase() })
})

test('Wallet address cannot be created if the url is duplicated', async (): Promise<void> => {
const url = 'https://Alice.me/pay'
const wallet = walletAddressService.create({
...options,
url
})
assert.ok(!isWalletAddressError(wallet))
await expect(
walletAddressService.create({
...options,
url
})
).resolves.toEqual(WalletAddressError.DuplicateWalletAddress)
})
})

describe('Update Wallet Address', (): void => {
Expand Down
12 changes: 9 additions & 3 deletions packages/backend/src/open_payments/wallet_address/service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import {
ForeignKeyViolationError,
TransactionOrKnex,
NotFoundError
NotFoundError,
UniqueViolationError
} from 'objection'
import { URL } from 'url'

Expand Down Expand Up @@ -166,7 +167,7 @@ async function createWalletAddress(

return await WalletAddress.query(deps.knex)
.insertGraphAndFetch({
url: options.url,
url: options.url.toLowerCase(),
publicName: options.publicName,
assetId: options.assetId,
additionalProperties: additionalProperties
Expand All @@ -178,6 +179,11 @@ async function createWalletAddress(
return WalletAddressError.UnknownAsset
}
}
if (err instanceof UniqueViolationError) {
if (err.constraint === 'walletaddresses_url_unique') {
return WalletAddressError.DuplicateWalletAddress
}
}
throw err
}
}
Expand Down Expand Up @@ -296,7 +302,7 @@ async function getWalletAddressByUrl(
url: string
): Promise<WalletAddress | undefined> {
const walletAddress = await WalletAddress.query(deps.knex)
.findOne({ url })
.findOne({ url: url.toLowerCase() })
.withGraphFetched('asset')
return walletAddress || undefined
}
Expand Down

0 comments on commit 9a7eed9

Please sign in to comment.