Skip to content

Commit

Permalink
feat: add safeMode flag to allow bypassing the utxo safety check
Browse files Browse the repository at this point in the history
  • Loading branch information
iamcrazycoder committed Jul 25, 2023
1 parent df1fb68 commit a2d9241
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 8 deletions.
2 changes: 1 addition & 1 deletion packages/sdk/src/inscription/commit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export async function generateCommitAddress(options: GenerateCommitAddressOption
}
}

export type GenerateCommitAddressOptions = Omit<GetWalletOptions, "format"> & {
export type GenerateCommitAddressOptions = Omit<GetWalletOptions, "format" | "safeMode"> & {
satsPerByte: number;
mediaType: string;
mediaContent: string;
Expand Down
4 changes: 3 additions & 1 deletion packages/sdk/src/inscription/psbt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ export async function createRevealPsbt(options: CreateRevealPsbtOptions) {
const networkObj = getNetwork(options.network);
const key = (await getAddresses({ ...options, format: "p2tr" }))[0];
const xkey = key.xkey;

options.safeMode = !options.safeMode ? "on": options.safeMode

if (!xkey) {
throw new Error("Failed to build createRevealPsbt");
Expand Down Expand Up @@ -58,7 +60,7 @@ export async function createRevealPsbt(options: CreateRevealPsbtOptions) {
let sutableUnspent: any = null;

unspents.forEach((unspent) => {
if (unspent.sats >= options.postage + feesForWitnessData && unspent.safeToSpend === true) {
if (unspent.sats >= options.postage + feesForWitnessData && (options.safeMode === "off" || (options.safeMode === "on" && unspent.safeToSpend === true))) {
sutableUnspent = unspent;
}
});
Expand Down
8 changes: 6 additions & 2 deletions packages/sdk/src/transactions/OrdTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {
createTransaction,
getAddressesFromPublicKey,
getNetwork,
GetWalletOptions,
OnOffUnion,
OrditApi
} from "..";
import { Network } from "../config/types";
Expand All @@ -33,6 +35,7 @@ export class OrdTransaction {
#suitableUnspent: any = null;
#recovery = false;
#outs: Outputs = [];
#safeMode: OnOffUnion

constructor({
feeRate = 10,
Expand All @@ -57,6 +60,7 @@ export class OrdTransaction {
this.meta = otherOptions.meta;
this.postage = postage;
this.#outs = outs;
this.#safeMode = !otherOptions.safeMode ? "on": otherOptions.safeMode

const xKey = getAddressesFromPublicKey(publicKey, network, "p2tr")[0].xkey;

Expand Down Expand Up @@ -308,7 +312,7 @@ export class OrdTransaction {
}, 0);

const suitableUnspent = unspents.find((unspent) => {
if (unspent.sats >= this.postage + this.#feeForWitnessData! + customOutsAmount && unspent.safeToSpend === true) {
if (unspent.sats >= this.postage + this.#feeForWitnessData! + customOutsAmount && (this.#safeMode === 'off' || (this.#safeMode === 'on' && unspent.safeToSpend === true))) {
return true;
}
}, this);
Expand All @@ -324,7 +328,7 @@ export class OrdTransaction {
}
}

export type OrdTransactionOptions = {
export type OrdTransactionOptions = Pick<GetWalletOptions, 'safeMode'> & {
feeRate?: number;
postage?: number;
mediaType?: string;
Expand Down
5 changes: 3 additions & 2 deletions packages/sdk/src/transactions/psbt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import { Network, Psbt } from "bitcoinjs-lib";
import { createTransaction, getNetwork } from "../utils";
import { GetWalletOptions, getWalletWithBalances } from "../wallet";

export async function createPsbt({ network, format, pubKey, ins, outs }: CreatePsbtOptions) {
export async function createPsbt({ network, format, pubKey, ins, outs, safeMode = "on" }: CreatePsbtOptions) {
const netWorkObj = getNetwork(network);
const bip32 = BIP32Factory(ecc);

const walletWithBalances = await getWalletWithBalances({
pubKey,
format,
network
network,
safeMode
});

if (walletWithBalances?.spendables === undefined) {
Expand Down
7 changes: 5 additions & 2 deletions packages/sdk/src/wallet/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export async function getWallet({
};
}

export async function getWalletWithBalances({ pubKey, format, network }: GetWalletOptions) {
export async function getWalletWithBalances({ pubKey, format, network, safeMode = "on" }: GetWalletOptions) {
const wallet = (await getWallet({ pubKey, format, network })) as GetWalletWithBalances;

const ordinals: unknown[] = [];
Expand Down Expand Up @@ -70,7 +70,7 @@ export async function getWalletWithBalances({ pubKey, format, network }: GetWall
wallet.counts.satoshis += unspentObj.sats;
wallet_satoshis += unspentObj.sats;

if (unspentObj.safeToSpend) {
if (safeMode === "off" || (safeMode === "on" && unspentObj.safeToSpend)) {
wallet.counts.cardinals += unspentObj.sats;
wallet_cardinals += unspentObj.sats;

Expand Down Expand Up @@ -121,10 +121,13 @@ export async function getWalletWithBalances({ pubKey, format, network }: GetWall
return wallet;
}

export type OnOffUnion = "on" | "off"

export type GetWalletOptions = {
pubKey: string;
network: Network;
format: AddressTypes | "all";
safeMode?: OnOffUnion
};

export type GetWalletReturnType = {
Expand Down

0 comments on commit a2d9241

Please sign in to comment.