diff --git a/packages/sdk/src/fee/FeeEstimator.ts b/packages/sdk/src/fee/FeeEstimator.ts index 380aefcb..5e0fb705 100644 --- a/packages/sdk/src/fee/FeeEstimator.ts +++ b/packages/sdk/src/fee/FeeEstimator.ts @@ -3,6 +3,7 @@ import { Psbt } from "bitcoinjs-lib" import { AddressFormats, getNetwork, getScriptType } from ".." import { Network } from "../config/types" import { MAXIMUM_FEE } from "../constants" +import { OrditSDKError } from "../utils/errors" import { FeeEstimatorOptions } from "./types" export default class FeeEstimator { @@ -16,7 +17,7 @@ export default class FeeEstimator { constructor({ feeRate, network, psbt, witness }: FeeEstimatorOptions) { if (feeRate < 0 || !Number.isSafeInteger(feeRate)) { - throw new Error("Invalid feeRate") + throw new OrditSDKError("Invalid feeRate") } this.feeRate = +feeRate // convert decimal to whole number that might have passed Number.isSafeInteger check due to precision loss @@ -35,7 +36,7 @@ export default class FeeEstimator { private sanityCheckFee() { if (this.fee > MAXIMUM_FEE) { - throw new Error("Error while calculating fees") + throw new OrditSDKError("Error while calculating fees") } } @@ -53,18 +54,18 @@ export default class FeeEstimator { const outputTypes: AddressFormats[] = [] if (inputs.length === 0) { - throw new Error("PSBT must have at least one input") + throw new OrditSDKError("PSBT must have at least one input") } if (outputs.length === 0) { - throw new Error("PSBT must have at least one output") + throw new OrditSDKError("PSBT must have at least one output") } inputs.forEach((input) => { const script = input.witnessUtxo && input.witnessUtxo.script ? input.witnessUtxo.script : null if (!script) { - throw new Error("Invalid script") + throw new OrditSDKError("Invalid script") } inputTypes.push(getScriptType(script, this.network).format) @@ -141,7 +142,7 @@ export default class FeeEstimator { return { input: 148, output: 34, txHeader: 10, witness: 0 } default: - throw new Error("Invalid type") + throw new OrditSDKError("Invalid type") } } } diff --git a/packages/sdk/src/inscription/collection.ts b/packages/sdk/src/inscription/collection.ts index d0c40f48..0b93de7e 100644 --- a/packages/sdk/src/inscription/collection.ts +++ b/packages/sdk/src/inscription/collection.ts @@ -1,6 +1,7 @@ import { BaseDatasource, GetWalletOptions, Inscriber, JsonRpcDatasource, verifyMessage } from ".." import { Network } from "../config/types" import { MAXIMUM_ROYALTY_PERCENTAGE } from "../constants" +import { OrditSDKError } from "../utils/errors" export async function publishCollection({ title, @@ -14,13 +15,13 @@ export async function publishCollection({ ...options }: PublishCollectionOptions) { if (!validateInscriptions(inscriptions)) { - throw new Error("Invalid inscriptions supplied.") + throw new OrditSDKError("Invalid inscriptions supplied.") } if (royalty) { // 0 = 0%, 0.1 = 10% if (isNaN(royalty.pct) || royalty.pct < 0 || royalty.pct > MAXIMUM_ROYALTY_PERCENTAGE) { - throw new Error("Invalid royalty %") + throw new OrditSDKError("Invalid royalty %") } royalty.pct = +new Intl.NumberFormat("en", { @@ -50,7 +51,7 @@ export async function publishCollection({ export async function mintFromCollection(options: MintFromCollectionOptions) { if (!options.collectionOutpoint || !options.inscriptionIid || !options.destinationAddress) { - throw new Error("Invalid options supplied.") + throw new OrditSDKError("Invalid options supplied.") } const [colTxId, colVOut] = options.collectionOutpoint.split(":").map((v, i) => { @@ -61,12 +62,12 @@ export async function mintFromCollection(options: MintFromCollectionOptions) { }) as [string, number | false] if (!colTxId || colVOut === false) { - throw new Error("Invalid collection outpoint supplied.") + throw new OrditSDKError("Invalid collection outpoint supplied.") } const datasource = options.datasource || new JsonRpcDatasource({ network: options.network }) const collection = await datasource.getInscription({ id: options.collectionOutpoint }) if (!collection) { - throw new Error("Invalid collection") + throw new OrditSDKError("Invalid collection") } const colMeta = collection.meta @@ -83,7 +84,7 @@ export async function mintFromCollection(options: MintFromCollectionOptions) { } if (!validInscription) { - throw new Error("Invalid inscription iid supplied.") + throw new OrditSDKError("Invalid inscription iid supplied.") } const meta: any = { @@ -101,7 +102,7 @@ export async function mintFromCollection(options: MintFromCollectionOptions) { const validSignature = verifyMessage({ address: meta.publ, message: message, signature: options.signature }) if (!validSignature) { - throw new Error("Invalid signature supplied.") + throw new OrditSDKError("Invalid signature supplied.") } meta.sig = options.signature diff --git a/packages/sdk/src/inscription/witness.ts b/packages/sdk/src/inscription/witness.ts index 297d718c..3cce6eef 100644 --- a/packages/sdk/src/inscription/witness.ts +++ b/packages/sdk/src/inscription/witness.ts @@ -2,11 +2,12 @@ import * as ecc from "@bitcoinerlab/secp256k1" import * as bitcoin from "bitcoinjs-lib" import { MAXIMUM_SCRIPT_ELEMENT_SIZE } from "../constants" +import { OrditSDKError } from "../utils/errors" export function buildWitnessScript({ recover = false, ...options }: WitnessScriptOptions) { bitcoin.initEccLib(ecc) if (!options.mediaType || !options.mediaContent || !options.xkey) { - throw new Error("Failed to build witness script") + throw new OrditSDKError("Failed to build witness script") } if (recover) { @@ -61,7 +62,7 @@ export function buildWitnessScript({ recover = false, ...options }: WitnessScrip function opPush(data: string | Buffer) { const buff = Buffer.isBuffer(data) ? data : Buffer.from(data, "utf8") if (buff.byteLength > MAXIMUM_SCRIPT_ELEMENT_SIZE) - throw new Error("Data is too large to push. Use chunkContent to split data into smaller chunks") + throw new OrditSDKError("Data is too large to push. Use chunkContent to split data into smaller chunks") return Buffer.concat([buff]) } diff --git a/packages/sdk/src/instant-trade/InstantTradeBuilder.ts b/packages/sdk/src/instant-trade/InstantTradeBuilder.ts index a2d5c833..f87c3c19 100644 --- a/packages/sdk/src/instant-trade/InstantTradeBuilder.ts +++ b/packages/sdk/src/instant-trade/InstantTradeBuilder.ts @@ -1,6 +1,7 @@ import { Inscription } from ".." import { MINIMUM_AMOUNT_IN_SATS } from "../constants" import { PSBTBuilder, PSBTBuilderOptions } from "../transactions/PSBTBuilder" +import { OrditSDKError } from "../utils/errors" export interface InstantTradeBuilderArgOptions extends Pick { @@ -90,18 +91,18 @@ export default class InstantTradeBuilder extends PSBTBuilder { protected async verifyAndFindInscriptionUTXO() { if (!this.inscriptionOutpoint) { - throw new Error("set inscription outpoint to the class") + throw new OrditSDKError("set inscription outpoint to the class") } const inscriptions = await this.datasource.getInscriptions({ outpoint: this.inscriptionOutpoint }) this.inscription = inscriptions.find((inscription) => inscription.outpoint === this.inscriptionOutpoint) if (!this.inscription) { - throw new Error("Inscription not found") + throw new OrditSDKError("Inscription not found") } const utxo = await this.datasource.getInscriptionUTXO({ id: this.inscription.genesis }) if (!utxo) { - throw new Error(`Unable to find UTXO: ${this.inscription.outpoint}`) + throw new OrditSDKError(`Unable to find UTXO: ${this.inscription.outpoint}`) } this.postage = utxo.sats @@ -110,7 +111,7 @@ export default class InstantTradeBuilder extends PSBTBuilder { protected validatePrice(price: number) { if (isNaN(price) || price < MINIMUM_AMOUNT_IN_SATS) { - throw new Error("Invalid price") + throw new OrditSDKError("Invalid price") } } } diff --git a/packages/sdk/src/instant-trade/InstantTradeBuyerTxBuilder.ts b/packages/sdk/src/instant-trade/InstantTradeBuyerTxBuilder.ts index 76bab690..61e004b4 100644 --- a/packages/sdk/src/instant-trade/InstantTradeBuyerTxBuilder.ts +++ b/packages/sdk/src/instant-trade/InstantTradeBuyerTxBuilder.ts @@ -4,6 +4,7 @@ import reverseBuffer from "buffer-reverse" import { decodePSBT, generateTxUniqueIdentifier, getScriptType, INSTANT_BUY_SELLER_INPUT_INDEX } from ".." import { MINIMUM_AMOUNT_IN_SATS } from "../constants" import { InjectableInput, InjectableOutput } from "../transactions/PSBTBuilder" +import { OrditSDKError } from "../utils/errors" import InstantTradeBuilder, { InstantTradeBuilderArgOptions } from "./InstantTradeBuilder" interface InstantTradeBuyerTxBuilderArgOptions extends InstantTradeBuilderArgOptions { @@ -46,13 +47,13 @@ export default class InstantTradeBuyerTxBuilder extends InstantTradeBuilder { const [input] = this.sellerPSBT.data.inputs if (!input?.witnessUtxo) { - throw new Error("invalid seller psbt") + throw new OrditSDKError("invalid seller psbt") } const data = getScriptType(input.witnessUtxo.script, this.network) this.sellerAddress = data.payload && data.payload.address ? data.payload.address : undefined if (!this.sellerAddress) { - throw new Error("invalid seller psbt") + throw new OrditSDKError("invalid seller psbt") } } @@ -126,7 +127,7 @@ export default class InstantTradeBuyerTxBuilder extends InstantTradeBuilder { // 3 = 2 refundables + (at least) 1 to cover for purchase if (utxos.length < 3) { - throw new Error("No suitable UTXOs found") + throw new OrditSDKError("No suitable UTXOs found") } // bind minimum utxos. PSBTBuilder will add more if needed @@ -135,7 +136,7 @@ export default class InstantTradeBuyerTxBuilder extends InstantTradeBuilder { private async isEligible() { if (!this.inscriptionOutpoint) { - throw new Error("decode seller PSBT to check eligiblity") + throw new OrditSDKError("decode seller PSBT to check eligiblity") } const [utxos, [inscription]] = await Promise.all([ @@ -143,7 +144,7 @@ export default class InstantTradeBuyerTxBuilder extends InstantTradeBuilder { this.datasource.getInscriptions({ outpoint: this.inscriptionOutpoint }) ]) if (!inscription) { - throw new Error("Inscription no longer available for trade") + throw new OrditSDKError("Inscription no longer available for trade") } const inscriptionUTXO = await this.datasource.getInscriptionUTXO({ id: inscription.id }) this.postage = inscriptionUTXO.sats @@ -163,7 +164,7 @@ export default class InstantTradeBuyerTxBuilder extends InstantTradeBuilder { async build() { const eligible = await this.isEligible() if (!eligible) { - throw new Error("Not eligible") + throw new OrditSDKError("Not eligible") } this.decodePrice() diff --git a/packages/sdk/src/instant-trade/InstantTradeSellerTxBuilder.ts b/packages/sdk/src/instant-trade/InstantTradeSellerTxBuilder.ts index 1fb3f6b3..17603654 100644 --- a/packages/sdk/src/instant-trade/InstantTradeSellerTxBuilder.ts +++ b/packages/sdk/src/instant-trade/InstantTradeSellerTxBuilder.ts @@ -3,6 +3,7 @@ import * as bitcoin from "bitcoinjs-lib" import { processInput } from ".." import { MINIMUM_AMOUNT_IN_SATS } from "../constants" import { UTXO } from "../transactions/types" +import { OrditSDKError } from "../utils/errors" import InstantTradeBuilder, { InstantTradeBuilderArgOptions } from "./InstantTradeBuilder" interface InstantTradeSellerTxBuilderArgOptions extends InstantTradeBuilderArgOptions { @@ -36,7 +37,7 @@ export default class InstantTradeSellerTxBuilder extends InstantTradeBuilder { private async generatSellerInputs() { if (!this.utxo) { - throw new Error("UTXO not found") + throw new OrditSDKError("UTXO not found") } const input = await processInput({ @@ -85,13 +86,13 @@ export default class InstantTradeSellerTxBuilder extends InstantTradeBuilder { private validateOwnership() { if (this.inscription?.owner !== this.address) { - throw new Error(`Inscription does not belong to the address: ${this.address}`) + throw new OrditSDKError(`Inscription does not belong to the address: ${this.address}`) } } async build() { if (isNaN(this.price) || this.price < MINIMUM_AMOUNT_IN_SATS) { - throw new Error("Invalid price") + throw new OrditSDKError("Invalid price") } this.utxo = await this.verifyAndFindInscriptionUTXO() diff --git a/packages/sdk/src/modules/JsonRpcDatasource.ts b/packages/sdk/src/modules/JsonRpcDatasource.ts index 01f4cd35..d4a62fa2 100644 --- a/packages/sdk/src/modules/JsonRpcDatasource.ts +++ b/packages/sdk/src/modules/JsonRpcDatasource.ts @@ -15,6 +15,7 @@ import { } from "../api/types" import { Network } from "../config/types" import { Transaction, UTXO, UTXOLimited } from "../transactions/types" +import { OrditSDKError } from "../utils/errors" import { BaseDatasource, DatasourceUtility } from "." import { JsonRpcPagination } from "./types" @@ -29,7 +30,7 @@ export default class JsonRpcDatasource extends BaseDatasource { async getBalance({ address }: GetBalanceOptions) { if (!address) { - throw new Error("Invalid request") + throw new OrditSDKError("Invalid request") } return rpc[this.network].call("Address.GetBalance", { address }, rpc.id) @@ -37,7 +38,7 @@ export default class JsonRpcDatasource extends BaseDatasource { async getInscription({ id, decodeMetadata }: GetInscriptionOptions) { if (!id) { - throw new Error("Invalid request") + throw new OrditSDKError("Invalid request") } id = id.includes(":") ? id.replace(":", "i") : !id.includes("i") ? `${id}i0` : id @@ -52,7 +53,7 @@ export default class JsonRpcDatasource extends BaseDatasource { async getInscriptionUTXO({ id }: GetInscriptionUTXOOptions) { if (!id) { - throw new Error("Invalid request") + throw new OrditSDKError("Invalid request") } id = id.includes(":") ? id.replace(":", "i") : !id.includes("i") ? `${id}i0` : id @@ -100,7 +101,7 @@ export default class JsonRpcDatasource extends BaseDatasource { type = "spendable" }: GetSpendablesOptions) { if (!address || isNaN(value) || !value) { - throw new Error("Invalid request") + throw new OrditSDKError("Invalid request") } return rpc[this.network].call( @@ -119,7 +120,7 @@ export default class JsonRpcDatasource extends BaseDatasource { async getTransaction({ txId, ordinals = true, hex = false, witness = true, decodeMetadata = true }: GetTxOptions) { if (!txId) { - throw new Error("Invalid request") + throw new OrditSDKError("Invalid request") } const tx = await rpc[this.network].call( @@ -157,7 +158,7 @@ export default class JsonRpcDatasource extends BaseDatasource { next = null }: GetUnspentsOptions): Promise { if (!address) { - throw new Error("Invalid request") + throw new OrditSDKError("Invalid request") } let utxos: UTXO[] = [] @@ -191,11 +192,11 @@ export default class JsonRpcDatasource extends BaseDatasource { async relay({ hex, maxFeeRate, validate = true }: RelayOptions) { if (!hex) { - throw new Error("Invalid request") + throw new OrditSDKError("Invalid request") } if (maxFeeRate && (maxFeeRate < 0 || isNaN(maxFeeRate))) { - throw new Error("Invalid max fee rate") + throw new OrditSDKError("Invalid max fee rate") } return rpc[this.network].call("Transactions.Relay", { hex, maxFeeRate, validate }, rpc.id) diff --git a/packages/sdk/src/signatures/message.ts b/packages/sdk/src/signatures/message.ts index 041d27ae..1434b775 100644 --- a/packages/sdk/src/signatures/message.ts +++ b/packages/sdk/src/signatures/message.ts @@ -4,13 +4,14 @@ import { sign, verify } from "bitcoinjs-message" import { Network } from "../config/types" import { getDerivedNode } from "../keys" import { createTransaction, getNetwork } from "../utils" +import { OrditSDKError } from "../utils/errors" export async function signMessage(options: SignMessageOptions) { const network = getNetwork(options.network) options.format = "core" if (!options.message || !(options.bip39 || options.seed)) { - throw new Error("Invalid options provided.") + throw new OrditSDKError("Invalid options provided.") } const seedValue = options.bip39 || options.seed @@ -31,7 +32,7 @@ export async function signMessage(options: SignMessageOptions) { address } } catch (error) { - throw new Error("Unable to sign message.") + throw new OrditSDKError("Unable to sign message.") } } diff --git a/packages/sdk/src/signatures/psbt.ts b/packages/sdk/src/signatures/psbt.ts index 0a61558f..91f13849 100644 --- a/packages/sdk/src/signatures/psbt.ts +++ b/packages/sdk/src/signatures/psbt.ts @@ -3,6 +3,7 @@ import * as bitcoin from "bitcoinjs-lib" import { Network } from "../config/types" import { getDerivedNode } from "../keys" +import { OrditSDKError } from "../utils/errors" export async function signPsbt(options: SignPsbtOptions) { bitcoin.initEccLib(ecc) @@ -36,7 +37,7 @@ export async function signPsbt(options: SignPsbtOptions) { } if (error) { - throw new Error(error.message) + throw new OrditSDKError(error.message) } const psbtHex = psbt.toHex() @@ -61,7 +62,7 @@ export async function signPsbt(options: SignPsbtOptions) { } } } else { - throw new Error("Signed PSBT is same as input PSBT.") + throw new OrditSDKError("Signed PSBT is same as input PSBT.") } } } diff --git a/packages/sdk/src/transactions/Inscriber.ts b/packages/sdk/src/transactions/Inscriber.ts index 1bc5b87f..4833ed0d 100644 --- a/packages/sdk/src/transactions/Inscriber.ts +++ b/packages/sdk/src/transactions/Inscriber.ts @@ -13,6 +13,7 @@ import { } from ".." import { Network } from "../config/types" import { MINIMUM_AMOUNT_IN_SATS } from "../constants" +import { OrditSDKError } from "../utils/errors" import { NestedObject } from "../utils/types" import { PSBTBuilder } from "./PSBTBuilder" import { SkipStrictSatsCheckOptions, UTXOLimited } from "./types" @@ -66,7 +67,7 @@ export class Inscriber extends PSBTBuilder { autoAdjustment: false }) if (!publicKey || !changeAddress || !mediaContent) { - throw new Error("Invalid options provided") + throw new OrditSDKError("Invalid options provided") } this.destinationAddress = destinationAddress @@ -96,7 +97,7 @@ export class Inscriber extends PSBTBuilder { async build() { if (!this.suitableUnspent || !this.payment) { - throw new Error("Failed to build PSBT. Transaction not ready") + throw new OrditSDKError("Failed to build PSBT. Transaction not ready") } this.inputs = [ @@ -140,7 +141,7 @@ export class Inscriber extends PSBTBuilder { private isBuilt() { if (!this.commitAddress || !this.fee) { - throw new Error("Invalid tx! Make sure you generate commit address or recover and finally build") + throw new OrditSDKError("Invalid tx! Make sure you generate commit address or recover and finally build") } } @@ -189,7 +190,7 @@ export class Inscriber extends PSBTBuilder { : getDummyP2TRInput() if (this.recovery && !this.suitableUnspent) { - throw new Error("No UTXO found to recover") + throw new OrditSDKError("No UTXO found to recover") } this.ready = true @@ -205,7 +206,7 @@ export class Inscriber extends PSBTBuilder { private restrictUsageInPreviewMode() { if (this.previewMode) { - throw new Error("Unable to process request in preview mode") + throw new OrditSDKError("Unable to process request in preview mode") } } @@ -273,13 +274,13 @@ export class Inscriber extends PSBTBuilder { // Output to be paid to user if (amount < MINIMUM_AMOUNT_IN_SATS) { - throw new Error("Requested output amount is lower than minimum dust amount") + throw new OrditSDKError("Requested output amount is lower than minimum dust amount") } const utxos = await this.retrieveSelectedUTXOs(this.commitAddress!, amount) if (utxos.length === 0) { - throw new Error("No selected utxos retrieved") + throw new OrditSDKError("No selected utxos retrieved") } this.suitableUnspent = utxos[0] diff --git a/packages/sdk/src/transactions/PSBTBuilder.ts b/packages/sdk/src/transactions/PSBTBuilder.ts index 02c047e3..ddcc119d 100644 --- a/packages/sdk/src/transactions/PSBTBuilder.ts +++ b/packages/sdk/src/transactions/PSBTBuilder.ts @@ -15,6 +15,7 @@ import { import { Network } from "../config/types" import { MINIMUM_AMOUNT_IN_SATS } from "../constants" import FeeEstimator from "../fee/FeeEstimator" +import { OrditSDKError } from "../utils/errors" import { InputType, processInput } from "." import { Output, UTXOLimited } from "./types" @@ -212,7 +213,7 @@ export class PSBTBuilder extends FeeEstimator { private validateOutputAmount() { if (this.outputAmount < MINIMUM_AMOUNT_IN_SATS) { - throw new Error(`Output amount too low. Minimum output amount needs to be ${MINIMUM_AMOUNT_IN_SATS} sats`) + throw new OrditSDKError(`Output amount too low. Minimum output amount needs to be ${MINIMUM_AMOUNT_IN_SATS} sats`) } } @@ -269,7 +270,7 @@ export class PSBTBuilder extends FeeEstimator { await this.prepare() if (this.noMoreUTXOS) { - throw new Error(`Insufficient balance. Decrease the output amount by ${this.changeAmount * -1} sats`) + throw new OrditSDKError(`Insufficient balance. Decrease the output amount by ${this.changeAmount * -1} sats`) } } diff --git a/packages/sdk/src/transactions/psbt.ts b/packages/sdk/src/transactions/psbt.ts index 9788d14e..a59ba46b 100644 --- a/packages/sdk/src/transactions/psbt.ts +++ b/packages/sdk/src/transactions/psbt.ts @@ -4,6 +4,7 @@ import { BIP32Factory } from "bip32" import { Network } from "../config/types" import { BaseDatasource, JsonRpcDatasource } from "../modules" import { createTransaction, getNetwork, toXOnly } from "../utils" +import { OrditSDKError } from "../utils/errors" import { OnOffUnion } from "../wallet" import { PSBTBuilder } from "./PSBTBuilder" import { Output, UTXO, UTXOLimited } from "./types" @@ -19,7 +20,7 @@ export async function createPsbt({ enableRBF = true }: CreatePsbtOptions) { if (!outputs.length) { - throw new Error("Invalid request") + throw new OrditSDKError("Invalid request") } const psbt = new PSBTBuilder({ @@ -63,7 +64,7 @@ export async function processInput({ return generateLegacyInput({ utxo, sighashType, network, pubKey, datasource }) default: - throw new Error("invalid script pub type") + throw new OrditSDKError("invalid script pub type") } } @@ -75,7 +76,7 @@ function generateTaprootInput({ utxo, pubKey, network, sighashType, witness }: P const xOnlyPubKey = toXOnly(key.publicKey) if (!utxo.scriptPubKey.hex) { - throw new Error("Unable to process p2tr input") + throw new OrditSDKError("Unable to process p2tr input") } return { @@ -94,7 +95,7 @@ function generateTaprootInput({ utxo, pubKey, network, sighashType, witness }: P function generateSegwitInput({ utxo, sighashType }: Omit): SegwitInputType { if (!utxo.scriptPubKey.hex) { - throw new Error("Unable to process Segwit input") + throw new OrditSDKError("Unable to process Segwit input") } return { @@ -112,7 +113,7 @@ function generateSegwitInput({ utxo, sighashType }: Omit>): Promise { const { rawTx } = await datasource.getTransaction({ txId: utxo.txid, hex: true }) if (!rawTx) { - throw new Error("Unable to process legacy input") + throw new OrditSDKError("Unable to process legacy input") } const p2pkh = createTransaction(Buffer.from(pubKey, "hex"), "p2pkh", network) diff --git a/packages/sdk/src/utils/errors.ts b/packages/sdk/src/utils/errors.ts new file mode 100644 index 00000000..ff8647a7 --- /dev/null +++ b/packages/sdk/src/utils/errors.ts @@ -0,0 +1,6 @@ +export class OrditSDKError extends Error { + constructor(message: string) { + super(message) + this.name = "OrditSDKError" + } +} \ No newline at end of file diff --git a/packages/sdk/src/utils/index.ts b/packages/sdk/src/utils/index.ts index 154d0c59..ee5145e9 100644 --- a/packages/sdk/src/utils/index.ts +++ b/packages/sdk/src/utils/index.ts @@ -6,6 +6,7 @@ import ECPairFactory from "ecpair" import { AddressFormats, AddressTypes, addressTypeToName } from "../addresses/formats" import { Network } from "../config/types" import { UTXO } from "../transactions/types" +import { OrditSDKError } from "./errors" import { BufferOrHex, EncodeDecodeObjectOptions, @@ -78,7 +79,7 @@ export function tweakSigner(signer: bitcoin.Signer, opts: any = {}): bitcoin.Sig // @ts-ignore let privateKey: Uint8Array | undefined = signer.privateKey! if (!privateKey) { - throw new Error("Private key is required for tweaking signer!") + throw new OrditSDKError("Private key is required for tweaking signer!") } if (signer.publicKey[0] === 3) { privateKey = ecc.privateNegate(privateKey) @@ -86,7 +87,7 @@ export function tweakSigner(signer: bitcoin.Signer, opts: any = {}): bitcoin.Sig const tweakedPrivateKey = ecc.privateAdd(privateKey, tapTweakHash(toXOnly(signer.publicKey), opts.tweakHash)) if (!tweakedPrivateKey) { - throw new Error("Invalid tweaked private key!") + throw new OrditSDKError("Invalid tweaked private key!") } return ECPair.fromPrivateKey(Buffer.from(tweakedPrivateKey), { @@ -105,7 +106,7 @@ function encodeDecodeObject(obj: NestedObject, { encode, depth = 0 }: EncodeDeco const maxDepth = 5 if (depth > maxDepth) { - throw new Error("Object too deep") + throw new OrditSDKError("Object too deep") } for (const key in obj) { @@ -148,14 +149,14 @@ export function decodePSBT({ hex, base64, buffer }: OneOfAllDataFormats): bitcoi if (base64) return bitcoin.Psbt.fromBase64(base64) if (buffer) return bitcoin.Psbt.fromBuffer(buffer) - throw new Error("Invalid options") + throw new OrditSDKError("Invalid options") } export function decodeTx({ hex, buffer }: BufferOrHex): bitcoin.Transaction { if (hex) return bitcoin.Transaction.fromHex(hex) if (buffer) return bitcoin.Transaction.fromBuffer(buffer) - throw new Error("Invalid options") + throw new OrditSDKError("Invalid options") } function isPaymentFactory(payment: bitcoin.PaymentCreator, network: Network) { @@ -236,7 +237,7 @@ export function getScriptType(script: Buffer, network: Network): GetScriptTypeRe } } - throw new Error("Unsupported input") + throw new OrditSDKError("Unsupported input") } export function getDummyP2TRInput(): UTXO { diff --git a/packages/sdk/src/utxos/UTXOManager.ts b/packages/sdk/src/utxos/UTXOManager.ts index 9257c559..f7f6c354 100644 --- a/packages/sdk/src/utxos/UTXOManager.ts +++ b/packages/sdk/src/utxos/UTXOManager.ts @@ -1,5 +1,6 @@ import { processInput, PSBTBuilder } from ".." import { MINIMUM_AMOUNT_IN_SATS } from "../constants" +import { OrditSDKError } from "../utils/errors" import { UTXOManagerOptions } from "./types" export default class UTXOManager extends PSBTBuilder { @@ -19,7 +20,7 @@ export default class UTXOManager extends PSBTBuilder { address: this.address }) if (!totalUTXOs) { - throw new Error("No UTXOs found") + throw new OrditSDKError("No UTXOs found") } const utxo = spendableUTXOs.sort((a, b) => b.sats - a.sats)[0] // Largest UTXO @@ -37,7 +38,7 @@ export default class UTXOManager extends PSBTBuilder { const remainingAmount = utxo.sats - usedAmount const amount = remainingAmount - MINIMUM_AMOUNT_IN_SATS if (amount < MINIMUM_AMOUNT_IN_SATS) { - throw new Error( + throw new OrditSDKError( `Not enough sats to generate ${totalOutputs} UTXOs with at least ${MINIMUM_AMOUNT_IN_SATS} sats per UTXO. Try decreasing the count or deposit more BTC` ) } diff --git a/packages/sdk/src/wallet/Ordit.ts b/packages/sdk/src/wallet/Ordit.ts index 974ae9a4..dba34bd5 100644 --- a/packages/sdk/src/wallet/Ordit.ts +++ b/packages/sdk/src/wallet/Ordit.ts @@ -20,6 +20,7 @@ import { tweakSigner } from ".." import { Network } from "../config/types" +import { OrditSDKError } from "../utils/errors" bitcoin.initEccLib(ecc) const ECPair = ECPairFactory(ecc) @@ -85,7 +86,7 @@ export class Ordit { this.#initialize(accounts) } else { - throw new Error("Invalid options provided.") + throw new OrditSDKError("Invalid options provided.") } } @@ -99,12 +100,12 @@ export class Ordit { getAddressByType(type: AddressFormats) { if (!this.#initialized || !this.allAddresses.length) { - throw new Error("Wallet not fully initialized.") + throw new OrditSDKError("Wallet not fully initialized.") } const result = this.allAddresses.filter((address) => address.format === type) if (!result) { - throw new Error(`Address of type ${type} not found in the instance.`) + throw new OrditSDKError(`Address of type ${type} not found in the instance.`) } return result @@ -112,7 +113,7 @@ export class Ordit { getAllAddresses() { if (!this.#keyPair) { - throw new Error("Keypair not found") + throw new OrditSDKError("Keypair not found") } return this.allAddresses @@ -124,7 +125,8 @@ export class Ordit { const result = this.getAddressByType(type) as Account[] const addressToSelect = result[index] - if (!addressToSelect) throw new Error("Address not found. Please add an address with the type and try again.") + if (!addressToSelect) + throw new OrditSDKError("Address not found. Please add an address with the type and try again.") this.selectedAddress = addressToSelect.address this.publicKey = addressToSelect.pub @@ -136,7 +138,7 @@ export class Ordit { } generateAddress(type: AddressFormats, account: number, addressIndex: number) { - if (!this.#hdNode) throw new Error("No HD node found. Please reinitialize with BIP39 words or seed.") + if (!this.#hdNode) throw new OrditSDKError("No HD node found. Please reinitialize with BIP39 words or seed.") return getAccountDataFromHdNode({ hdNode: this.#hdNode, @@ -152,7 +154,7 @@ export class Ordit { let psbt: bitcoin.Psbt | null = null if (!this.#keyPair || !this.#initialized) { - throw new Error("Wallet not fully initialized.") + throw new OrditSDKError("Wallet not fully initialized.") } try { @@ -162,7 +164,7 @@ export class Ordit { } if (!psbt || !psbt.inputCount) { - throw new Error("Invalid PSBT provided.") + throw new OrditSDKError("Invalid PSBT provided.") } const inputsToSign: Input[] = [] @@ -194,7 +196,7 @@ export class Ordit { }) if (!inputsToSign.length) { - throw new Error("Cannot sign PSBT with no signable inputs.") + throw new OrditSDKError("Cannot sign PSBT with no signable inputs.") } let psbtHasBeenSigned = false