Skip to content

Commit

Permalink
update utxo input and output types
Browse files Browse the repository at this point in the history
  • Loading branch information
alejoacosta74 authored and rileystephens28 committed Apr 25, 2024
1 parent 598d9e2 commit bf61138
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 27 deletions.
15 changes: 8 additions & 7 deletions src.ts/providers/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import type { AccessList, AccessListish, TransactionLike } from "../transaction/

import type { ContractRunner } from "./contracts.js";
import type { Network } from "./network.js";
import type { Outpoint, UTXOEntry, UTXOTransactionOutput } from "../transaction/utxo.js";
import type { Outpoint } from "../transaction/utxo.js";
import type { TxInput, TxOutput } from "../transaction/utxo.js";

const BN_0 = BigInt(0);

Expand Down Expand Up @@ -202,9 +203,9 @@ export interface TransactionRequest {
*/
blockTag?: BlockTag;

inputs?: null | Array<UTXOEntry>;
inputs?: null | Array<TxInput>;

outputs?: null | Array<UTXOTransactionOutput>;
outputs?: null | Array<TxOutput>;
};

/**
Expand Down Expand Up @@ -296,9 +297,9 @@ export interface PreparedTransactionRequest {
*/
blockTag?: BlockTag;

inputs?: null | Array<UTXOEntry>;
inputs?: null | Array<TxInput>;

outputs?: null | Array<UTXOTransactionOutput>;
outputs?: null | Array<TxOutput>;
}

/**
Expand Down Expand Up @@ -1350,9 +1351,9 @@ export class TransactionResponse implements TransactionLike<string>, Transaction
*/
readonly accessList!: null | AccessList;

readonly inputs?: Array<UTXOEntry>;
readonly inputs?: Array<TxInput>;

readonly outputs?: Array<UTXOTransactionOutput>;
readonly outputs?: Array<TxOutput>;

#startBlock: number;

Expand Down
2 changes: 2 additions & 0 deletions src.ts/transaction/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,5 @@ export { Transaction } from "./transaction.js";
export { FewestCoinSelector } from "./coinselector-fewest.js";

export type { TransactionLike } from "./transaction.js";

export type {TxInput, TxOutput} from "./utxo.js";
51 changes: 36 additions & 15 deletions src.ts/transaction/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { computeAddress, recoverAddress } from "./address.js";
import type { BigNumberish, BytesLike } from "../utils/index.js";
import type { SignatureLike } from "../crypto/index.js";
import type { AccessList, AccessListish } from "./index.js";
import type { UTXOEntry, UTXOTransactionOutput } from "./utxo.js";
import type { TxInput, TxOutput } from "./utxo.js";

export interface TransactionLike<A = string> {
/**
Expand Down Expand Up @@ -85,9 +85,9 @@ export interface TransactionLike<A = string> {
accessList?: null | AccessListish;


inputsUTXO?: null | Array<UTXOEntry>;
inputsUTXO?: null | Array<TxInput>;

outputsUTXO?: null | Array<UTXOTransactionOutput>;
outputsUTXO?: null | Array<TxOutput>;
}

function handleNumber(_value: string, param: string): number {
Expand Down Expand Up @@ -230,8 +230,8 @@ export class Transaction implements TransactionLike<string> {
#sig: null | Signature;
#accessList: null | AccessList;
#hash: null | string;
#inputsUTXO: null | UTXOEntry[];
#outputsUTXO: null | UTXOTransactionOutput[];
#inputsUTXO: null | Array<TxInput>;
#outputsUTXO: null | Array<TxOutput>;
from: string;

/**
Expand Down Expand Up @@ -385,11 +385,11 @@ export class Transaction implements TransactionLike<string> {
}


get inputsUTXO(): null | UTXOEntry[] { return this.#inputsUTXO; }
set inputsUTXO(value: null | UTXOEntry[]) { this.#inputsUTXO = value; }
get inputsUTXO(): null | Array<TxInput> { return this.#inputsUTXO; }
set inputsUTXO(value: null | Array<TxInput>) { this.#inputsUTXO = value; }

get outputsUTXO(): null | UTXOTransactionOutput[] { return this.#outputsUTXO; }
set outputsUTXO(value: null | UTXOTransactionOutput[]) { this.#outputsUTXO = value; }
get outputsUTXO(): null | Array<TxOutput> { return this.#outputsUTXO; }
set outputsUTXO(value: null | Array<TxOutput>) { this.#outputsUTXO = value; }


/**
Expand Down Expand Up @@ -535,13 +535,34 @@ export class Transaction implements TransactionLike<string> {
return v.toString();
};

// Adjusted function to specifically handle the conversion of 'denomination' fields in array items
const processArrayWithBigInt = (arr: UTXOEntry[] | UTXOTransactionOutput[]) => {
return arr.map(item => ({
address: item.address,
denomination: s(item.denomination) // Convert 'denomination' to string
}));
// Helper function to convert bigint or number to string for JSON output
const bigIntToString = (value: number | bigint): string => {
return value.toString();
};

function processArrayWithBigInt(items: TxOutput[]): any[];
function processArrayWithBigInt(items: TxInput[]): any[];

function processArrayWithBigInt(items: TxOutput[] | TxInput[]): any[] {
if (items.length === 0) {
return [];
}

if ('Address' in items[0]) {
// Process as Output
return (items as TxOutput[]).map(({ Address, Denomination }) => ({
Address,
Denomination: bigIntToString(Denomination)
}));
} else {
// Process as Input
return (items as TxInput[]).map(({ txhash, index, pubKey }) => ({
txhash,
index,
pubKey: hexlify(pubKey)
}));
}
}

return {
type: this.type,
Expand Down
21 changes: 16 additions & 5 deletions src.ts/transaction/utxo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { getAddress } from "../address/index";
import { getBigInt } from "../utils/index";
import type { BigNumberish } from "../utils/index";


export type Outpoint = {
Txhash: string;
Index: number;
Expand All @@ -20,13 +21,23 @@ export interface UTXOEntry {

export type UTXOTransactionOutput = UTXOEntry;

export type UTXOTransaction = {
chainId: bigint;
inputs: UTXOTransactionInput[];
outputs: UTXOTransactionOutput[];
signature?: Uint8Array;
export type TxOutput = {
Address: string;
Denomination: number;
};

export type TxInput = {
txhash: string;
index: number;
pubKey: Uint8Array;
}

export interface UTXOEntry {
denomination: null | bigint;
address: null | string;
};


export interface UTXOLike extends UTXOEntry {
txhash?: null | string;
index?: null | number;
Expand Down

0 comments on commit bf61138

Please sign in to comment.