diff --git a/packages/sdk/src/transactions/psbt.ts b/packages/sdk/src/transactions/psbt.ts index 4033066e..fcc73208 100644 --- a/packages/sdk/src/transactions/psbt.ts +++ b/packages/sdk/src/transactions/psbt.ts @@ -2,6 +2,7 @@ import * as ecc from "@bitcoinerlab/secp256k1"; import BIP32Factory, { BIP32API } from "bip32"; import { Network, Psbt } from "bitcoinjs-lib"; +import { OrditApi } from "../api"; import { createTransaction, getNetwork } from "../utils"; import { GetWalletOptions, getWalletWithBalances } from "../wallet"; @@ -57,13 +58,12 @@ export async function createPsbt({ } }); - ins.forEach((input, idx) => { + for (const [idx, input] of ins.entries()) { if (input.address) { - walletWithBalances.spendables.forEach((spendable: any) => { + for (const spendable of walletWithBalances.spendables) { const sats = spendable.sats; const scriptPubKeyAddress = spendable.scriptPubKey.address; const scriptPubKeyType = spendable.scriptPubKey.type as string; - let addedInputSuccessfully = false; fees = JSON.parse(JSON.stringify((80 + (inputs_used + 1) * 180) * sats_per_byte)); @@ -72,7 +72,13 @@ export async function createPsbt({ } if (input.address === scriptPubKeyAddress) { - addedInputSuccessfully = addInputToPsbtByType(spendable, scriptPubKeyType, psbt, bip32, netWorkObj); + const addedInputSuccessfully = await addInputToPsbtByType( + spendable, + scriptPubKeyType, + psbt, + bip32, + netWorkObj + ); if (addedInputSuccessfully) { unspents_to_use.push(spendable); @@ -86,9 +92,9 @@ export async function createPsbt({ unsupported_inputs.push(spendable); } } - }); + } } - }); + } if (!unspents_to_use.length) { throw new Error( @@ -118,7 +124,7 @@ export async function createPsbt({ }; } -function addInputToPsbtByType(spendable: any, type: string, psbt: Psbt, bip32: BIP32API, network: Network) { +async function addInputToPsbtByType(spendable: any, type: string, psbt: Psbt, bip32: BIP32API, network: Network) { if (type === "witness_v1_taproot") { const chainCode = Buffer.alloc(32); chainCode.fill(1); @@ -186,11 +192,12 @@ function addInputToPsbtByType(spendable: any, type: string, psbt: Psbt, bip32: B //fail silently } } else if (type === "pubkeyhash") { + const { tx } = await OrditApi.fetchTx({ txId: spendable.txid, hex: true, ordinals: false }); try { psbt.addInput({ hash: spendable.txid, - index: parseInt(spendable.n), - nonWitnessUtxo: Buffer.from(spendable.txhex, "hex") + index: spendable.n, + nonWitnessUtxo: Buffer.from(tx.hex!, "hex") }); return true; diff --git a/packages/sdk/src/wallet/index.ts b/packages/sdk/src/wallet/index.ts index e70fa6a0..f36e1561 100644 --- a/packages/sdk/src/wallet/index.ts +++ b/packages/sdk/src/wallet/index.ts @@ -1,8 +1,10 @@ -import { getAddressesFromPublicKey } from "../addresses"; -import { AddressTypes } from "../addresses/formats"; -import { OrditApi } from "../api"; -import { Network } from "../config/types"; -import { getWalletKeys } from "../keys"; +import { getAddressesFromPublicKey } from "../addresses" +import { AddressTypes } from "../addresses/formats" +import { OrditApi } from "../api" +import { Network } from "../config/types" +import { Inscription, Ordinal } from "../inscription/types" +import { getWalletKeys } from "../keys" +import { UTXO } from "../transactions/types" export async function getWallet({ pubKey, @@ -23,10 +25,10 @@ export async function getWallet({ export async function getWalletWithBalances({ pubKey, format, network, safeMode = "on" }: GetWalletOptions) { const wallet = (await getWallet({ pubKey, format, network })) as GetWalletWithBalances; - const ordinals: unknown[] = []; - const inscriptions: unknown[] = []; - const spendables: unknown[] = []; - const unspendables: unknown[] = []; + const ordinals: Ordinal[] = [] + const inscriptions: Inscription[] = [] + const spendables: UTXO[] = [] + const unspendables: UTXO[] = [] wallet.counts.unspents = 0; wallet.counts.satoshis = 0; @@ -129,10 +131,10 @@ export type GetWalletReturnType = { }; export type GetWalletWithBalances = GetWalletReturnType & { - spendables: unknown[]; - unspendables: unknown[]; - ordinals: unknown[]; - inscriptions: unknown[]; + spendables: UTXO[] + unspendables: UTXO[] + ordinals: Ordinal[] + inscriptions: Inscription[] counts: { unspents: number;