Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Comments and minor improvements for utils.ts #83

Open
wants to merge 13 commits into
base: staging
Choose a base branch
from
66 changes: 37 additions & 29 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ import { bytesToHex } from '@noble/curves/abstract/utils';
import { sha256 } from '@noble/hashes/sha256';
import { Buffer } from 'buffer/';

function splitAmount(value: number): Array<number> {
/**
* Splits a number into its constituent powers of 2.
* @param value The number to split
* @returns An array containing the constituent powers of 2
*/
export function splitAmount(value: number): number[] {
const chunks: Array<number> = [];
for (let i = 0; i < 32; i++) {
const mask: number = 1 << i;
Expand All @@ -17,16 +22,21 @@ function splitAmount(value: number): Array<number> {
return chunks;
}

function bytesToNumber(bytes: Uint8Array): bigint {
/**
* Converts a Uint8Array of bytes to a BigInt number.
* @param bytes The byte array to convert
* @returns The BigInt representation
*/
export function bytesToNumber(bytes: Uint8Array): bigint {
return hexToNumber(bytesToHex(bytes));
}

function hexToNumber(hex: string): bigint {
export function hexToNumber(hex: string): bigint {
return BigInt(`0x${hex}`);
}

//used for json serialization
function bigIntStringify<T>(_key: unknown, value: T) {
export function bigIntStringify<T>(_key: unknown, value: T) {
return typeof value === 'bigint' ? value.toString() : value;
}

Expand All @@ -35,7 +45,7 @@ function bigIntStringify<T>(_key: unknown, value: T) {
* @param token
* @returns
*/
function getEncodedToken(token: Token): string {
export function getEncodedToken(token: Token): string {
return TOKEN_PREFIX + TOKEN_VERSION + encodeJsonToBase64(token);
}

Expand All @@ -44,7 +54,7 @@ function getEncodedToken(token: Token): string {
* @param token an encoded cashu token (cashuAey...)
* @returns cashu token object
*/
function getDecodedToken(token: string): Token {
export function getDecodedToken(token: string): Token {
// remove prefixes
const uriPrefixes = ['web+cashu://', 'cashu://', 'cashu:', 'cashuA'];
uriPrefixes.forEach((prefix) => {
Expand Down Expand Up @@ -119,34 +129,32 @@ export function sortProofsById(proofs: Array<Proof>) {
return proofs.sort((a, b) => a.id.localeCompare(b.id));
}

export function isObj(v: unknown): v is object {
return typeof v === 'object';
export function isObj(v: unknown): v is Record<string, unknown> {
return typeof v === 'object' && v !== null;
}

export function checkResponse(data: { error?: string; detail?: string }) {
/**
* Checks if a response object has any error fields.
* Throws an Error if so.
* @param data The response data to check
*/
export function checkResponse(data: { error?: string; detail?: string }): void {
if (!isObj(data)) return;
if ('error' in data && data.error) {
throw new Error(data.error);
}
if ('detail' in data && data.detail) {
throw new Error(data.detail);
const message = data.error ?? data.detail;
if (message) {
throw new Error(message);
}
}
export function checkResponseError(err: unknown) {
if (axios.isAxiosError(err) && err?.response?.data) {
if ('error' in err.response.data) {
throw new Error(err.response.data.error);
}
if ('detail' in err.response.data) {
throw new Error(err.response.data.detail);

/**
* Checks for Axios errors and throws custom Error.
* @param err The Axios error
*/
export function checkResponseError(err: unknown): void {
if (axios.isAxiosError(err)) {
const message = err?.response?.data?.error ?? err?.response?.data?.detail;
if (message) {
throw new Error(message);
}
}
}
export {
hexToNumber,
splitAmount,
bytesToNumber,
bigIntStringify,
getDecodedToken,
getEncodedToken
};