Transaction Creation and Conversion
The system provides functions for creating and converting transactions between different formats.
The coreTxToWalletStuff
function converts a core transaction body to wallet-specific format:
// src/utils/wallet-stuff.ts
// Converts a core transaction body to wallet-specific format
export const coreTxToWalletStuff = (
coreTxBody: C.TransactionBody
): Server.WalletStuff => {
const coreInputs = coreTxBody.inputs();
const coreInputsLen = coreInputs.len();
const inputs: C.TransactionInput[] = [];
for (let i = 0; i < coreInputsLen; i++) {
inputs.push(coreInputs.get(i));
}
const inputRefs: { txId: string; utxoIx: number }[] = inputs.map((input) => {
return {
txId: input.transaction_id().to_hex(),
utxoIx: Big(input.index().to_str()).toNumber(),
};
});
const inputRefIds = inputRefs.map(
(inputRef) => inputRef.txId + "#" + inputRef.utxoIx
);
const coreTxId = C.hash_transaction(coreTxBody);
const coreOutputs = coreTxBody.outputs();
const coreOutputsLen = coreOutputs.len();
const lucidUtxos: Lucid.UTxO[] = [];
for (let i = 0; i < coreOutputsLen; i++) {
const coreOutputRef = C.TransactionInput.new(
coreTxId,
C.BigNum.from_str(i.toString())
);
const coreOutput = coreOutputs.get(i);
const coreUnspentTxOutput = C.TransactionUnspentOutput.new(
coreOutputRef,
coreOutput
);
const lucidUtxo = Lucid.coreToUtxo(coreUnspentTxOutput);
lucidUtxos.push(lucidUtxo);
}
const outputUtxosRef = lucidUtxosToOutputUtxosRef(lucidUtxos);
const walletUtxoIdToOutputUtxosRef: { [walletUtxoId: string]: string } = {};
for (const inputRefId of inputRefIds) {
walletUtxoIdToOutputUtxosRef[inputRefId] = outputUtxosRef;
}
const walletUtxos = lucidUtxos.map(lucidToWalletUtxo);
const outputUtxosRefToOutputUtxos: {
[outputUtxosRef: string]: Server.Utxo[];
} = {
[outputUtxosRef]: walletUtxos,
};
const walletUtxoIdsByOutputUtxosRef: { [outputUtxosRef: string]: string[] } =
{
[outputUtxosRef]: inputRefIds,
};
return {
outputUtxosRefByWalletUtxoId: walletUtxoIdToOutputUtxosRef,
outputUtxosByOutputUtxosRef: outputUtxosRefToOutputUtxos,
walletUtxoIdsByOutputUtxosRef: walletUtxoIdsByOutputUtxosRef,
};
};
The makeVirtualWalletUtxoMap
function creates a virtual UTxO map for pending transactions:
// src/utils/wallet-stuff.ts
// Creates a virtual UTxO map for pending transactions
export const makeVirtualWalletUtxoMap = (
userInputRefs: string[],
userAddresses: Set<string>,
txBody: C.TransactionBody
): VirtualWalletUtxoMap => {
const txInputs = txBody.inputs();
const txInputsLen = txInputs.len();
const individualTxInputs: C.TransactionInput[] = [];
for (let i = 0; i < txInputsLen; i++) {
individualTxInputs.push(txInputs.get(i));
}
const txInputRefs = individualTxInputs.map((input) => {
return `${input.transaction_id().to_hex()}#${input.index().to_str()}`;
});
const userInputRefSet = new Set(userInputRefs);
const txUserInputRefs = txInputRefs.filter((inputRef) => {
return userInputRefSet.has(inputRef);
});
const txHash = C.hash_transaction(txBody);
const txId = txHash.to_hex();
const txOutputs = txBody.outputs();
const txOutputsLen = txOutputs.len();
const txLucidUtxos: UTxO[] = [];
for (let i = 0; i < txOutputsLen; i++) {
const txOutputInput = C.TransactionInput.new(
txHash,
C.BigNum.from_str(i.toString())
);
const txOutput = txOutputs.get(i);
const txUtxo = C.TransactionUnspentOutput.new(txOutputInput, txOutput);
const txLucidUtxo = Lucid.coreToUtxo(txUtxo);
txLucidUtxos.push(txLucidUtxo);
}
const txUserLucidUtxos = txLucidUtxos.filter((txLucidUtxo) => {
return userAddresses.has(txLucidUtxo.address);
});
const utxoRefToTxId: { [utxoRef: string]: string } = {};
txUserInputRefs.forEach((txUserInputRef) => {
utxoRefToTxId[txUserInputRef] = txId;
});
const txIdToUtxos: { [txId: string]: UTxO[] } = {};
if (txUserLucidUtxos.length > 0) {
txIdToUtxos[txId] = txUserLucidUtxos;
}
return {
utxoRefToTxId,
txIdToUtxos,
};
};
Last updated