Skip to content

Commit 797a9d5

Browse files
authored
fix: developer keys upload (#1563)
* fix developer keys upload * fix err message
1 parent fe4476b commit 797a9d5

1 file changed

Lines changed: 36 additions & 34 deletions

File tree

  • packages/wallet/backend/src/walletAddressKeys

packages/wallet/backend/src/walletAddressKeys/service.ts

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { WalletAddressKeys } from './model'
66
import { WalletAddressService } from '@/walletAddress/service'
77
import { WalletAddress } from '@/walletAddress/model'
88
import { BadRequest, NotFound } from '@shared/backend'
9+
import { UniqueViolationError } from 'objection'
910

1011
export type KeyResponse = {
1112
privateKey: string
@@ -63,46 +64,47 @@ export class WalletAddressKeyService implements IWalletAddressKeyService {
6364
base64Key,
6465
nickname
6566
}: UploadKeyArgs): Promise<void> {
66-
const walletAddress = await this.walletAddressService.getById({
67-
userId,
68-
accountId,
69-
walletAddressId
70-
})
71-
72-
const jwk = validateJwk(
73-
JSON.parse(new Buffer(base64Key, 'base64').toString())
74-
)
75-
const publicKey = createPublicKey({ key: jwk, format: 'jwk' })
76-
const publicKeyPEM = publicKey
77-
.export({ type: 'spki', format: 'pem' })
78-
.toString()
79-
80-
const isUploaded = await WalletAddress.query()
81-
.where({
82-
id: jwk.kid.toString()
67+
try {
68+
const walletAddress = await this.walletAddressService.getById({
69+
userId,
70+
accountId,
71+
walletAddressId
8372
})
84-
.first()
8573

86-
if (isUploaded)
87-
throw new BadRequest(
88-
'Same key already uploaded. Please upload a unique one.'
74+
const jwk = validateJwk(
75+
JSON.parse(new Buffer(base64Key, 'base64').toString())
8976
)
77+
const publicKey = createPublicKey({ key: jwk, format: 'jwk' })
78+
const publicKeyPEM = publicKey
79+
.export({ type: 'spki', format: 'pem' })
80+
.toString()
81+
82+
const walletAddressKey =
83+
await this.rafikiClient.createRafikiWalletAddressKey(
84+
jwk,
85+
walletAddress.id
86+
)
87+
88+
const key = {
89+
id: jwk.kid,
90+
nickname,
91+
rafikiId: walletAddressKey.id,
92+
publicKey: publicKeyPEM,
93+
walletAddressId
94+
}
95+
96+
await WalletAddressKeys.query().insert(key)
97+
} catch (e) {
98+
if (e instanceof SyntaxError)
99+
throw new BadRequest('The uploaded key is not in the correct format.')
90100

91-
const walletAddressKey =
92-
await this.rafikiClient.createRafikiWalletAddressKey(
93-
jwk,
94-
walletAddress.id
95-
)
101+
if (e instanceof UniqueViolationError)
102+
throw new BadRequest(
103+
'Same key already uploaded. Please upload a unique one.'
104+
)
96105

97-
const key = {
98-
id: jwk.kid,
99-
nickname,
100-
rafikiId: walletAddressKey.id,
101-
publicKey: publicKeyPEM,
102-
walletAddressId
106+
throw e
103107
}
104-
105-
await WalletAddressKeys.query().insert(key)
106108
}
107109

108110
async registerKey({

0 commit comments

Comments
 (0)