Skip to content

Commit

Permalink
Refactor: introduce parseTxInput
Browse files Browse the repository at this point in the history
  • Loading branch information
ppershing committed Feb 24, 2021
1 parent 948b7f8 commit f855dd9
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 18 deletions.
8 changes: 4 additions & 4 deletions src/interactions/signTx.ts
Expand Up @@ -10,8 +10,8 @@ import type {
} from "../Ada";
import { Errors, } from "../Ada"
import cardano, { SignTxIncluded } from "../cardano";
import type { ParsedCertificate, ParsedOutput, ParsedWithdrawal } from "../parsing";
import { CertificateType, parseCertificates, parseTxOutput, parseWithdrawal, validateTransaction } from "../parsing";
import type { ParsedCertificate, ParsedInput, ParsedOutput, ParsedWithdrawal } from "../parsing";
import { CertificateType, parseCertificates, parseTxInput, parseTxOutput, parseWithdrawal, validateTransaction } from "../parsing";
import utils, { Assert, invariant, Precondition, unreachable } from "../utils";
import { INS } from "./common/ins";
import { wrapRetryStillInCall } from "./common/retry";
Expand Down Expand Up @@ -131,7 +131,7 @@ const signTx_init = async (

const signTx_addInput = async (
_send: SendFn,
input: InputTypeUTxO
input: ParsedInput
): Promise<void> => {
const enum P2 {
UNUSED = 0x00,
Expand Down Expand Up @@ -593,7 +593,7 @@ export async function signTransaction(
);

// inputs
for (const input of inputs) {
for (const input of inputs.map(i => parseTxInput(i))) {
await signTx_addInput(_send, input);
}

Expand Down
33 changes: 21 additions & 12 deletions src/parsing.ts
Expand Up @@ -178,18 +178,11 @@ export function validateTransaction(

// inputs
Precondition.checkIsArray(inputs, TxErrors.INPUTS_NOT_ARRAY);
for (const input of inputs) {
Precondition.checkIsHexStringOfLength(
input.txHashHex,
TX_HASH_LENGTH,
TxErrors.INPUT_INVALID_TX_HASH
);

if (isSigningPoolRegistrationAsOwner) {
// input should not be given with a path
// the path is not used, but we check just to avoid potential confusion of developers using this
Precondition.check(!input.path, TxErrors.INPUT_WITH_PATH);
}
const _inputs = inputs.map(inp => parseTxInput(inp))
if (isSigningPoolRegistrationAsOwner) {
// input should not be given with a path
// the path is not used, but we check just to avoid potential confusion of developers using this
Precondition.check(_inputs.every(inp => inp == null), TxErrors.INPUT_WITH_PATH_WHEN_SIGNING_AS_POOL_OWNER);
}

// outputs
Expand Down Expand Up @@ -252,6 +245,22 @@ export function validateTransaction(
}
}

export type ParsedInput = {
txHashHex: FixlenHexString<typeof TX_HASH_LENGTH>
outputIndex: Uint32_t
path: BIP32Path | null
}

export function parseTxInput(input: InputTypeUTxO): ParsedInput {
const txHashHex = parseHexStringOfLength(input.txHashHex, TX_HASH_LENGTH, TxErrors.INPUT_INVALID_TX_HASH)
const outputIndex = parseUint32_t(input.outputIndex, TxErrors.INPUT_INVALID_UTXO_INDEX)
return {
txHashHex,
outputIndex,
path: input.path != null ? parseBIP32Path(input.path, TxErrors.INPUT_INVALID_PATH) : null
}
}

export type ParsedWithdrawal = {
amountStr: Uint64_str
path: ValidBIP32Path
Expand Down
6 changes: 4 additions & 2 deletions src/txErrors.ts
Expand Up @@ -4,9 +4,11 @@ export enum TxErrors {
INVALID_NETWORK_ID = "invalid network id",

INPUTS_NOT_ARRAY = "inputs not an array",
INPUT_WITH_PATH =
"stake pool registration= inputs should not contain the witness path",
INPUT_WITH_PATH_WHEN_SIGNING_AS_POOL_OWNER =
"inputs should not contain the witness path if signing stake pool certificate as owner",
INPUT_INVALID_TX_HASH = "invalid tx hash in an input",
INPUT_INVALID_PATH = "invalid input path",
INPUT_INVALID_UTXO_INDEX = "invalid input utxo index",

OUTPUTS_NOT_ARRAY = "outputs not an array",
OUTPUT_INVALID_AMOUNT = "invalid amount in an output",
Expand Down

0 comments on commit f855dd9

Please sign in to comment.