Skip to content

Commit

Permalink
fine tuning errors
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonbryant12 committed Mar 25, 2024
1 parent 310d954 commit 15f9e4e
Show file tree
Hide file tree
Showing 11 changed files with 34 additions and 38 deletions.
2 changes: 1 addition & 1 deletion src/capability/p2pPaymentDestinationCapability.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const P2pPaymentDestinationCapability = new Capability({
title: 'Get no monitored payment destination (p2p payment destination)',
authors: ['Miguel Duarte (Money Button)', 'Ryan X. Charles (Money Button)', 'Ivan Mlinaric (Handcash)', 'Rafa (Handcash)'],
version: '1.1',
method: 'POST',
method: 'POST'
})

export default P2pPaymentDestinationCapability
2 changes: 1 addition & 1 deletion src/capability/p2pReceiveTransactionCapability.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const ReceiveTransactionCapability = new Capability({
title: 'Receive Transaction',
authors: ['Miguel Duarte (Money Button)', 'Ryan X. Charles (Money Button)', 'Ivan Mlinaric (Handcash)', 'Rafa (Handcash)'],
version: '1.1',
method: 'POST',
method: 'POST'
})

export default ReceiveTransactionCapability
2 changes: 1 addition & 1 deletion src/capability/pkiCapability.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const PublicKeyInfrastructureCapability = new Capability({
code: 'pki',
title: 'Public Key Infrastructure',
authors: ['andy (nChain)', 'Ryan X. Charles (Money Button)'],
version: '1',
version: '1'
})

export default PublicKeyInfrastructureCapability
2 changes: 1 addition & 1 deletion src/capability/publicProfileCapability.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ import Capability from './capability.js'
export default new Capability({
title: 'Public Profile (Name & Avatar)',
authors: ['Ryan X. Charles (Money Button)'],
version: '1',
version: '1'
})
2 changes: 1 addition & 1 deletion src/capability/verifyPublicKeyOwnerCapability.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import Capability from './capability.js'

const VerifyPublicKeyOwnerCapability = new Capability({
title: 'bsvalias public key verify (Verify Public Key Owner)',
title: 'bsvalias public key verify (Verify Public Key Owner)'
})

export default VerifyPublicKeyOwnerCapability
26 changes: 12 additions & 14 deletions src/errors/index.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
class PaymailError extends Error {
status: any
constructor (message, status) {
super(message)
this.status = status
}
status: any
constructor (message, status) {
super(message)
this.status = status
}
}

class PaymailBadRequestError extends PaymailError {
constructor (message) {
super(message, 400)
}
constructor (message) {
super(message, 400)
}
}


class PaymailServerResponseError extends PaymailError {
constructor (message) {
super(message, 503)
}
constructor (message) {
super(message, 503)
}
};

export { PaymailError, PaymailBadRequestError, PaymailServerResponseError }

4 changes: 2 additions & 2 deletions src/paymailClient/httpClient.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { PaymailServerResponseError } from '../errors/index.js'
type Fetch = typeof fetch
type FetchOptions = RequestInit & { timeout?: number }
import { PaymailError } from "../errors/index.js";

export default class HttpClient {
private readonly fetch: Fetch
Expand Down Expand Up @@ -37,7 +37,7 @@ export default class HttpClient {
try {
const response = await this.fetch(url, requestOptions)
if (!response.ok) {
throw new PaymailError(await response.text(), 503)
throw new PaymailServerResponseError(await response.text())
}
return response
} catch (error) {
Expand Down
27 changes: 13 additions & 14 deletions src/paymailClient/paymailClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import DNSResolver, { DNSResolverOptions } from './resolver/dnsResolver.js'
import HttpClient from './httpClient.js'
import Capability from '../capability/capability.js'
import Joi from 'joi'
import { PaymailServerResponseError } from '../errors/index.js'
import { PaymailError, PaymailServerResponseError } from '../errors/index.js'

import PublicProfileCapability from '../capability/publicProfileCapability.js'
import PublicKeyInfrastructureCapability from '../capability/pkiCapability.js'
Expand Down Expand Up @@ -36,15 +36,15 @@ export default class PaymailClient {

const url = `${protocol}${domain}:${port}/.well-known/bsvalias`
const response = await fetch(url)

if (!response.ok) {
throw new Error(`Failed to fetch well-known for "${aDomain}" with URL: ${url}`)
}

const json = await response.json()
if (json.bsvalias !== '1.0') throw new Error(`Domain "${aDomain}" is not on bsvalias version 1.0`)
if (!json.capabilities) throw new Error(`Domain "${aDomain}" invalid response does not have any capabilities`)

const schema = Joi.object({
bsvalias: Joi.string().required(),
capabilities: Joi.object().required()
})
const { error } = schema.validate(json)
if (error) {
throw new PaymailServerResponseError(`Validation error: ${error.message}`)
}
return json.capabilities
}

Expand All @@ -63,7 +63,7 @@ export default class PaymailClient {
public ensureCapabilityFor = async (aDomain, aCapability) => {
const capabilities = await this.getDomainCapabilities(aDomain)
if (!capabilities[aCapability]) {
throw new Error(`Domain "${aDomain}" does not support capability "${aCapability}"`)
throw new PaymailError(`Domain "${aDomain}" does not support capability "${aCapability}"`, 401)
}
return capabilities[aCapability]
}
Expand Down Expand Up @@ -111,7 +111,7 @@ export default class PaymailClient {
public getP2pPaymentDestination = async (paymail, satoshis: number) => {
const response = await this.request(paymail, P2pPaymentDestinationCapability, {
satoshis
});
})

const schema = Joi.object({
outputs: Joi.array().items(
Expand Down Expand Up @@ -142,7 +142,7 @@ export default class PaymailClient {
txHex,
reference,
metadata
});
})

const schema = Joi.object({
txid: Joi.string().required(),
Expand All @@ -153,7 +153,6 @@ export default class PaymailClient {
throw new PaymailServerResponseError(`Validation error: ${error.message}`)
}
return value

}

public verifyPublicKey = async (paymail, pubkey) => {
Expand All @@ -173,6 +172,6 @@ export default class PaymailClient {
if (error) {
throw new PaymailServerResponseError(`Validation error: ${error.message}`)
}
return responseBody;
return responseBody
}
}
2 changes: 1 addition & 1 deletion src/paymailClient/resolver/dnsResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class DNSResolver extends AbstractResolver {
}
}
if (dohResponse.Status !== 0 || !dohResponse.Answer) {
throw new Error(`${this.domainWithoutBsvAliasPrefix(aDomain)} is not correctly configured: insecure domain`)
throw new PaymailServerResponseError(`${this.domainWithoutBsvAliasPrefix(aDomain)} is not correctly configured: insecure domain`)
}

const data = dohResponse.Answer[0].data.split(' ')
Expand Down
2 changes: 1 addition & 1 deletion src/paymailRouter/paymailRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import express, { Router, ErrorRequestHandler } from 'express'
import bodyParser from 'body-parser'
import PaymailRoute from './paymailRoutes/paymailRoute.js'
import RequestSenderValidationCapability from '../capability/requestSenderValidationCapability.js'
import { PaymailBadRequestError } from '../errors/index.js';
import { PaymailBadRequestError } from '../errors/index.js'

export default class PaymailRouter {
private readonly router: Router
Expand Down
1 change: 0 additions & 1 deletion src/paymailRouter/paymailRoutes/pki.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ export default class PublicKeyInfrastructureRoute extends PaymailRoute {
protected serializeResponse (domainLogicResponse: PkiResponse): string {
return JSON.stringify({
bsvalias: '1.0',

handle: domainLogicResponse.handle,
pubkey: domainLogicResponse.pubkey
})
Expand Down

0 comments on commit 15f9e4e

Please sign in to comment.