From a30984284794a45f304dd76207b39ec0054c61de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C3=ADas=20Olivera?= Date: Fri, 14 Jul 2023 15:30:19 -0600 Subject: [PATCH] Improve readability and error messages (#15) * Improve readability * Clean up error messages * Remove duplicate line * Improve error message --------- Co-authored-by: Victor Kirov --- .prettierrc.json | 8 ++++---- index.ts | 6 +++--- package.json | 4 +++- src/address/index.ts | 7 +++++-- src/call/index.ts | 5 ++++- src/provider/index.ts | 2 +- src/signatures/index.ts | 11 +++++++---- src/transactions/sendBtcTransaction.ts | 18 ++++++++++-------- src/transactions/signTransaction.ts | 11 +++++++---- 9 files changed, 44 insertions(+), 28 deletions(-) diff --git a/.prettierrc.json b/.prettierrc.json index a70616f..35e12d9 100644 --- a/.prettierrc.json +++ b/.prettierrc.json @@ -3,11 +3,11 @@ "bracketSpacing": true, "jsxBracketSameLine": false, "jsxSingleQuote": false, + "printWidth": 100, "quoteProps": "as-needed", - "singleQuote": true, "semi": true, - "printWidth": 100, - "useTabs": false, + "singleQuote": true, "tabWidth": 2, - "trailingComma": "es5" + "trailingComma": "es5", + "useTabs": false } diff --git a/index.ts b/index.ts index f08de06..9b1cc9c 100644 --- a/index.ts +++ b/index.ts @@ -1,6 +1,6 @@ export * from './src/address/index'; -export * from './src/provider'; export * from './src/call'; -export * from './src/transactions/signTransaction'; -export * from './src/transactions/sendBtcTransaction'; +export * from './src/provider'; export * from './src/signatures'; +export * from './src/transactions/sendBtcTransaction'; +export * from './src/transactions/signTransaction'; diff --git a/package.json b/package.json index c2686f0..1a8f8ba 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,9 @@ "name": "sats-connect", "version": "0.1.11", "main": "dist/index.js", - "files": ["dist"], + "files": [ + "dist" + ], "scripts": { "test": "jest", "build-debug": "webpack --mode development", diff --git a/src/address/index.ts b/src/address/index.ts index 120f400..623d7cf 100644 --- a/src/address/index.ts +++ b/src/address/index.ts @@ -1,4 +1,5 @@ import { createUnsecuredToken, Json } from 'jsontokens'; + import { getDefaultProvider } from '../provider'; import { GetAddressOptions } from './types'; @@ -6,12 +7,14 @@ export const getAddress = async (options: GetAddressOptions) => { const { getProvider = getDefaultProvider } = options; const provider = await getProvider(); if (!provider) { - throw new Error('No Bitcoin Wallet installed'); + throw new Error('No Bitcoin wallet installed'); } - const { message, network, purposes } = options.payload; + + const { purposes } = options.payload; if (!purposes) { throw new Error('Address purposes are required'); } + try { const request = createUnsecuredToken(options.payload as unknown as Json); const addressResponse = await provider.connect(request); diff --git a/src/call/index.ts b/src/call/index.ts index 1000381..7401e62 100644 --- a/src/call/index.ts +++ b/src/call/index.ts @@ -1,4 +1,5 @@ import { createUnsecuredToken, Json } from 'jsontokens'; + import { BitcoinNetwork, GetBitcoinProviderFunc, getDefaultProvider } from '../provider'; export interface CallWalletPayload { @@ -23,12 +24,14 @@ export const callWalletPopup = async (options: CallWalletOptions) => { const { getProvider = getDefaultProvider } = options; const provider = await getProvider(); if (!provider) { - throw new Error('No Bitcoin Wallet installed'); + throw new Error('No Bitcoin wallet installed'); } + const { method } = options.payload; if (!method) { throw new Error('A wallet method is required'); } + const request = createUnsecuredToken(options.payload as unknown as Json); try { const callResponse = await provider.call(request); diff --git a/src/provider/index.ts b/src/provider/index.ts index a141054..834bbae 100644 --- a/src/provider/index.ts +++ b/src/provider/index.ts @@ -1,5 +1,5 @@ -import { SignTransactionResponse } from '../transactions/signTransaction'; import { GetAddressResponse } from '../address'; +import { SignTransactionResponse } from '../transactions/signTransaction'; export interface BitcoinNetwork { type: 'Mainnet' | 'Testnet'; diff --git a/src/signatures/index.ts b/src/signatures/index.ts index cba4c7a..0ad3559 100644 --- a/src/signatures/index.ts +++ b/src/signatures/index.ts @@ -1,4 +1,5 @@ import { createUnsecuredToken, Json } from 'jsontokens'; + import { getDefaultProvider } from '../provider'; import { SignMessageOptions } from './types'; @@ -6,21 +7,23 @@ export const signMessage = async (options: SignMessageOptions) => { const { getProvider = getDefaultProvider } = options; const provider = await getProvider(); if (!provider) { - throw new Error('No Bitcoin Wallet installed'); + throw new Error('No Bitcoin wallet installed'); } + const { address, message } = options.payload; if (!address) { - throw new Error('An Address is required to sign a message'); + throw new Error('An address is required to sign a message'); } if (!message) { - throw new Error('you need to provide a message to be signed'); + throw new Error('A message to be signed is required'); } + try { const request = createUnsecuredToken(options.payload as unknown as Json); const response = await provider.signMessage(request); options.onFinish?.(response); } catch (error) { - console.error('[Connect] Error during Signing request', error); + console.error('[Connect] Error during sign message request', error); options.onCancel?.(); } }; diff --git a/src/transactions/sendBtcTransaction.ts b/src/transactions/sendBtcTransaction.ts index 8cfbba8..eca434f 100644 --- a/src/transactions/sendBtcTransaction.ts +++ b/src/transactions/sendBtcTransaction.ts @@ -1,4 +1,5 @@ import { createUnsecuredToken, Json } from 'jsontokens'; + import { BitcoinNetwork, GetBitcoinProviderFunc, getDefaultProvider } from '../provider'; export interface Recipient { @@ -8,7 +9,7 @@ export interface Recipient { export interface SendBtcTransactionPayload { network: BitcoinNetwork; - recipients: Recipient[] + recipients: Recipient[]; senderAddress: string; message?: string; } @@ -21,25 +22,26 @@ export interface SendBtcTransactionOptions { } export const sendBtcTransaction = async (options: SendBtcTransactionOptions) => { - const { recipients, senderAddress } = options.payload; const { getProvider = getDefaultProvider } = options; const provider = await getProvider(); - if (!provider) { - throw new Error('No Bitcoin Wallet installed'); + throw new Error('No Bitcoin wallet installed'); } - if (!recipients) { - throw new Error('Recipient is required'); + + const { recipients, senderAddress } = options.payload; + if (!recipients || recipients.length === 0) { + throw new Error('At least one recipient is required'); } if (!senderAddress) { - throw new Error('Sender address is required'); + throw new Error('The sender address is required'); } + try { const request = createUnsecuredToken(options.payload as unknown as Json); const addressResponse = await provider.sendBtcTransaction(request); options.onFinish?.(addressResponse); } catch (error) { - console.error('[Connect] Error during send btc request', error); + console.error('[Connect] Error during send BTC transaction request', error); options.onCancel?.(); } }; diff --git a/src/transactions/signTransaction.ts b/src/transactions/signTransaction.ts index 7f47f91..8cb303c 100644 --- a/src/transactions/signTransaction.ts +++ b/src/transactions/signTransaction.ts @@ -1,4 +1,5 @@ import { createUnsecuredToken, Json } from 'jsontokens'; + import { BitcoinNetwork, GetBitcoinProviderFunc, getDefaultProvider } from '../provider'; export interface InputToSign { @@ -31,21 +32,23 @@ export const signTransaction = async (options: SignTransactionOptions) => { const { getProvider = getDefaultProvider } = options; const provider = await getProvider(); if (!provider) { - throw new Error('No Bitcoin Wallet installed'); + throw new Error('No Bitcoin wallet installed'); } + const { psbtBase64, inputsToSign } = options.payload; if (!psbtBase64) { - throw new Error('a value for psbtBase64 representing the tx hash is required'); + throw new Error('A value for psbtBase64 representing the tx hash is required'); } if (!inputsToSign) { - throw new Error('an array specifying the inputs to be signed by the wallet is required'); + throw new Error('An array specifying the inputs to be signed by the wallet is required'); } + try { const request = createUnsecuredToken(options.payload as unknown as Json); const addressResponse = await provider.signTransaction(request); options.onFinish?.(addressResponse); } catch (error) { - console.error('[Connect] Error during signPsbt request', error); + console.error('[Connect] Error during sign transaction request', error); options.onCancel?.(); } };