Skip to content

Commit

Permalink
Improve readability and error messages (#15)
Browse files Browse the repository at this point in the history
* Improve readability

* Clean up error messages

* Remove duplicate line

* Improve error message

---------

Co-authored-by: Victor Kirov <[email protected]>
  • Loading branch information
olistic and victorkirov authored Jul 14, 2023
1 parent 830800b commit a309842
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 28 deletions.
8 changes: 4 additions & 4 deletions .prettierrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
6 changes: 3 additions & 3 deletions index.ts
Original file line number Diff line number Diff line change
@@ -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';
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
7 changes: 5 additions & 2 deletions src/address/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import { createUnsecuredToken, Json } from 'jsontokens';

import { getDefaultProvider } from '../provider';
import { GetAddressOptions } from './types';

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);
Expand Down
5 changes: 4 additions & 1 deletion src/call/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { createUnsecuredToken, Json } from 'jsontokens';

import { BitcoinNetwork, GetBitcoinProviderFunc, getDefaultProvider } from '../provider';

export interface CallWalletPayload {
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/provider/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { SignTransactionResponse } from '../transactions/signTransaction';
import { GetAddressResponse } from '../address';
import { SignTransactionResponse } from '../transactions/signTransaction';

export interface BitcoinNetwork {
type: 'Mainnet' | 'Testnet';
Expand Down
11 changes: 7 additions & 4 deletions src/signatures/index.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
import { createUnsecuredToken, Json } from 'jsontokens';

import { getDefaultProvider } from '../provider';
import { SignMessageOptions } from './types';

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?.();
}
};
Expand Down
18 changes: 10 additions & 8 deletions src/transactions/sendBtcTransaction.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { createUnsecuredToken, Json } from 'jsontokens';

import { BitcoinNetwork, GetBitcoinProviderFunc, getDefaultProvider } from '../provider';

export interface Recipient {
Expand All @@ -8,7 +9,7 @@ export interface Recipient {

export interface SendBtcTransactionPayload {
network: BitcoinNetwork;
recipients: Recipient[]
recipients: Recipient[];
senderAddress: string;
message?: string;
}
Expand All @@ -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?.();
}
};
11 changes: 7 additions & 4 deletions src/transactions/signTransaction.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { createUnsecuredToken, Json } from 'jsontokens';

import { BitcoinNetwork, GetBitcoinProviderFunc, getDefaultProvider } from '../provider';

export interface InputToSign {
Expand Down Expand Up @@ -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?.();
}
};

0 comments on commit a309842

Please sign in to comment.