Skip to content
This repository was archived by the owner on Sep 29, 2025. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 46 additions & 3 deletions src/lib/parseAdaTx.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,49 @@
import { Transaction } from "@emurgo/cardano-serialization-lib-nodejs";
import {
type Certificate,
Transaction,
} from "@emurgo/cardano-serialization-lib-nodejs";

const decodeOneCert = (cert: Certificate): Record<string, any> => {
const returnedCert: Record<string, any> = cert.to_js_value();
const keys = Object.keys(returnedCert);
for (let i = 0; i < keys.length; i++) {
const type = keys[i];
switch (type) {
case "StakeDelegation":
returnedCert[type].warning =
"⚠️ This certificate delegates stake to the given pool";
break;
case "StakeDeregistration":
returnedCert[type].warning =
"⚠️ This certificate deregisters stake from the given pool";
break;
case "...":
returnedCert[type].warning = "⚠️ ...";
}
}
return returnedCert;
};

export const parseAdaTx = async (txRaw: string): Promise<object> => {
const tx = Transaction.from_hex(txRaw);
return tx.to_js_value();
const tx = Transaction.from_hex(txRaw);
const body = tx.body();
const deserializedJson: Record<string, any> = {};
if (body.inputs().len()) {
// to force deserialization
deserializedJson.inputs = body.inputs().to_js_value();
}
if (body.outputs().len()) {
// to force deserialization
deserializedJson.outputs = body.outputs().to_js_value();
}
const certs = body.certs();
if (certs && certs.len() > 0) {
for (let i = 0; i < certs.len(); i++) {
const cert = certs.get(i);
deserializedJson.certs = deserializedJson.certs || [];
deserializedJson.certs.push(decodeOneCert(cert));
}
}

return deserializedJson;
};