Skip to content

Commit

Permalink
fix: developer keys upload (#1563)
Browse files Browse the repository at this point in the history
* fix developer keys upload

* fix err message
  • Loading branch information
HessamMosleh authored Sep 9, 2024
1 parent fe4476b commit 797a9d5
Showing 1 changed file with 36 additions and 34 deletions.
70 changes: 36 additions & 34 deletions packages/wallet/backend/src/walletAddressKeys/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { WalletAddressKeys } from './model'
import { WalletAddressService } from '@/walletAddress/service'
import { WalletAddress } from '@/walletAddress/model'
import { BadRequest, NotFound } from '@shared/backend'
import { UniqueViolationError } from 'objection'

export type KeyResponse = {
privateKey: string
Expand Down Expand Up @@ -63,46 +64,47 @@ export class WalletAddressKeyService implements IWalletAddressKeyService {
base64Key,
nickname
}: UploadKeyArgs): Promise<void> {
const walletAddress = await this.walletAddressService.getById({
userId,
accountId,
walletAddressId
})

const jwk = validateJwk(
JSON.parse(new Buffer(base64Key, 'base64').toString())
)
const publicKey = createPublicKey({ key: jwk, format: 'jwk' })
const publicKeyPEM = publicKey
.export({ type: 'spki', format: 'pem' })
.toString()

const isUploaded = await WalletAddress.query()
.where({
id: jwk.kid.toString()
try {
const walletAddress = await this.walletAddressService.getById({
userId,
accountId,
walletAddressId
})
.first()

if (isUploaded)
throw new BadRequest(
'Same key already uploaded. Please upload a unique one.'
const jwk = validateJwk(
JSON.parse(new Buffer(base64Key, 'base64').toString())
)
const publicKey = createPublicKey({ key: jwk, format: 'jwk' })
const publicKeyPEM = publicKey
.export({ type: 'spki', format: 'pem' })
.toString()

const walletAddressKey =
await this.rafikiClient.createRafikiWalletAddressKey(
jwk,
walletAddress.id
)

const key = {
id: jwk.kid,
nickname,
rafikiId: walletAddressKey.id,
publicKey: publicKeyPEM,
walletAddressId
}

await WalletAddressKeys.query().insert(key)
} catch (e) {
if (e instanceof SyntaxError)
throw new BadRequest('The uploaded key is not in the correct format.')

const walletAddressKey =
await this.rafikiClient.createRafikiWalletAddressKey(
jwk,
walletAddress.id
)
if (e instanceof UniqueViolationError)
throw new BadRequest(
'Same key already uploaded. Please upload a unique one.'
)

const key = {
id: jwk.kid,
nickname,
rafikiId: walletAddressKey.id,
publicKey: publicKeyPEM,
walletAddressId
throw e
}

await WalletAddressKeys.query().insert(key)
}

async registerKey({
Expand Down

0 comments on commit 797a9d5

Please sign in to comment.