fromTxRep method
Implementation
static AbstractTransaction fromTxRep(String txRep) {
if (txRep == null) {
throw Exception('txRep can not be null');
}
List<String> lines = txRep.split('\n'); //TODO: handle newline within string
Map<String, String> map = Map<String, String>();
for (String line in lines) {
var parts = line.split(':');
if (parts.length > 1) {
String key = parts[0].trim();
String value = parts.sublist(1).join(':').trim();
map.addAll({key: value});
}
}
String sourceAccountId = _removeComment(map['tx.sourceAccount']);
if (sourceAccountId == null) {
throw Exception('missing tx.sourceAccount');
}
String seqNr = _removeComment(map['tx.seqNum']);
int sequenceNumber;
if (seqNr == null) {
throw Exception('missing tx.seqNum');
} else {
try {
sequenceNumber = int.parse(seqNr);
} catch (e) {
throw Exception('invalid tx.seqNum');
}
}
if (sequenceNumber == null) {
throw Exception('invalid tx.seqNum');
}
KeyPair sourceKeyPair;
try {
sourceKeyPair = KeyPair.fromAccountId(sourceAccountId);
} catch (e) {
throw Exception('invalid tx.sourceAccount');
}
if (sourceKeyPair == null) {
throw Exception('invalid tx.sourceAccount');
}
Account sourceAccount = Account(sourceKeyPair, sequenceNumber - 1);
TransactionBuilder txBuilder = TransactionBuilder(sourceAccount);
// TimeBounds
if (_removeComment(map['tx.timeBounds._present']) == 'true' &&
map['tx.timeBounds.minTime'] != null &&
map['tx.timeBounds.maxTime'] != null) {
try {
int minTime = int.parse(_removeComment(map['tx.timeBounds.minTime']));
int maxTime = int.parse(_removeComment(map['tx.timeBounds.maxTime']));
TimeBounds timeBounds = TimeBounds(minTime, maxTime);
txBuilder.addTimeBounds(timeBounds);
} catch (e) {
throw Exception('invalid tx.timeBounds');
}
} else if (_removeComment(map['tx.timeBounds._present']) == 'true') {
throw Exception('invalid tx.timeBounds');
}
// Memo
String memoType = _removeComment(map['tx.memo.type']);
if (memoType == null) {
throw Exception('missing tx.memo.type');
}
try {
if (memoType == 'MEMO_TEXT' && map['tx.memo.text'] != null) {
txBuilder.addMemo(
MemoText(_removeComment(map['tx.memo.text']).replaceAll('"', '')));
} else if (memoType == 'MEMO_ID' && map['tx.memo.id'] != null) {
txBuilder.addMemo(MemoId(int.parse(_removeComment(map['tx.memo.id']))));
} else if (memoType == 'MEMO_HASH' && map['tx.memo.hash'] != null) {
txBuilder.addMemo(
MemoHash(Util.hexToBytes(_removeComment(map['tx.memo.hash']))));
} else if (memoType == 'MEMO_RETURN' && map['tx.memo.return'] != null) {
txBuilder.addMemo(
MemoReturnHash.string(_removeComment(map['tx.memo.return'])));
} else {
txBuilder.addMemo(MemoNone());
}
} catch (e) {
throw Exception('invalid tx.memo');
}
// Operations
String operationsLen = _removeComment(map['tx.operations.len']);
if (operationsLen == null) {
throw Exception('missing tx.operations.len');
}
int nrOfOperations;
try {
nrOfOperations = int.parse(operationsLen);
} catch (e) {
throw Exception('invalid tx.operations.len');
}
if (nrOfOperations > 100) {
throw Exception('invalid tx.operations.len - greater than 100');
}
for (int i = 0; i < nrOfOperations; i++) {
Operation operation = _getOperation(i, map);
if (operation != null) {
txBuilder.addOperation(operation);
}
}
AbstractTransaction transaction = txBuilder.build();
// Signatures
String signaturesLen = _removeComment(map['tx.signatures.len']);
if (signaturesLen != null) {
int nrOfSignatures;
try {
nrOfSignatures = int.parse(signaturesLen);
} catch (e) {
throw Exception('invalid tx.signatures.len');
}
if (nrOfSignatures > 20) {
throw Exception('invalid tx.signatures.len - greater than 20');
}
List<XdrDecoratedSignature> signatures = List<XdrDecoratedSignature>();
for (int i = 0; i < nrOfSignatures; i++) {
XdrDecoratedSignature signature = _getSignature(i, map);
if (signature != null) {
signatures.add(signature);
}
}
transaction.signatures = signatures;
}
return transaction;
}