Skip to content

Commit

Permalink
fix: retrieve hex from GetTransaction RPC
Browse files Browse the repository at this point in the history
  • Loading branch information
iamcrazycoder committed Aug 2, 2023
1 parent 5f61122 commit 496f370
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 22 deletions.
25 changes: 16 additions & 9 deletions packages/sdk/src/transactions/psbt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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));

Expand All @@ -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);
Expand All @@ -86,9 +92,9 @@ export async function createPsbt({
unsupported_inputs.push(spendable);
}
}
});
}
}
});
}

if (!unspents_to_use.length) {
throw new Error(
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
28 changes: 15 additions & 13 deletions packages/sdk/src/wallet/index.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 496f370

Please sign in to comment.