WIP null "safety"

it's as "safe" as it is "short", "straightforward", and "elegant"
This commit is contained in:
sneurlax 2023-09-18 16:03:28 -05:00
parent 1e8b5ed700
commit 309fce399f

View file

@ -242,30 +242,60 @@ class Transaction {
fusion_tx.Transaction fusionTransaction = fusion_tx.Transaction(); fusion_tx.Transaction fusionTransaction = fusion_tx.Transaction();
// Map the Inputs and Outputs to Fusion Dart's format // Map the Inputs and Outputs to Fusion Dart's format
fusionTransaction.Inputs = inputs fusionTransaction.Inputs = inputs.map((e) {
.map((e) => fusion_input.Input( return fusion_input.Input(
prevTxid: utf8.encode(e.txid), prevTxid: utf8.encode(e.txid),
prevIndex: e.vout, prevIndex: e.vout,
pubKey: utf8.encode(e.witness ?? ""), // TODO fix or failsafe. pubKey: utf8.encode(address.value.toString()), // TODO is this valid?
amount: 0, // TODO fix amount: amount, // TODO is this valid?
)) );
.toList(); }).toList();
fusionTransaction.Outputs = outputs fusionTransaction.Outputs = outputs.map((e) {
.map((e) => fusion_output.Output( /*
if (e.scriptPubKey == null) {
// TODO calculate scriptPubKey if it is null.
}
*/
fusion_address.DerivationPath? derivationPath;
List<int>? pubKey;
// Validate that we have all the required data.
if (address.value == null) {
// TODO calculate address if it is null.
throw Exception(
"address value is null for input: ${e.scriptPubKeyAddress}");
} else {
if (address.value!.publicKey.isEmpty || e.scriptPubKey != null) {
pubKey = utf8.encode(e.scriptPubKey!);
// TODO is this valid?
} else {
pubKey = address.value!.publicKey;
}
if (address.value!.derivationPath != null) {
derivationPath = fusion_address.DerivationPath(
address.value!.derivationPath!.toString());
} else {
// TODO calculate derivation path if it is null.
/*
throw Exception(
"derivationPath is null for input: ${e.scriptPubKeyAddress}");
*/
}
}
// TODO handle case where address.value.publicKey is empty and e.scriptPubKey is null
return fusion_output.Output(
addr: fusion_address.Address( addr: fusion_address.Address(
addr: e.scriptPubKeyAddress, addr: e.scriptPubKeyAddress,
publicKey: utf8.encode(e.scriptPubKey ?? publicKey: pubKey,
address!.value.toString()), // TODO fix or failsafe. derivationPath: derivationPath,
derivationPath: fusion_address.DerivationPath(address!
.value!.derivationPath!
.toString()), // TODO failsafe the (non-)null assertions.
), ),
value: e.value, value: e.value,
)) );
.toList(); }).toList();
// If any other information needs to be altered/added, do so here.
return fusionTransaction; return fusionTransaction;
} }