MochimoTransaction.signWithKey constructor
MochimoTransaction.signWithKey({})
Implementation
factory MochimoTransaction.signWithKey({
required Uint8List signingSeed,
required Uint8List sourceWots,
required Uint8List accountTag,
required Uint8List changeAddress,
required List<TxDestination> destinations,
required BigInt changeAmount,
required BigInt fee,
BigInt? blockToLive,
}) {
if (signingSeed.length != 32) {
throw ArgumentError('Invalid signing seed');
}
if (sourceWots.length != 2208) {
throw ArgumentError('Invalid WOTS address');
}
if (accountTag.length != 20 || changeAddress.length != 40) {
throw ArgumentError('Invalid address');
}
if (destinations.isEmpty || destinations.length > 256) {
throw ArgumentError('A transaction needs 1 to 256 destinations');
}
final publicKey = sourceWots.sublist(0, WOTS.WOTSSIGBYTES);
final sourceHash = WotsAddress.addrHashGenerate(publicKey);
final sourceAddress = Uint8List(40);
sourceAddress.setRange(0, 20, accountTag);
sourceAddress.setRange(20, 40, sourceHash);
if (!_sameBytes(accountTag, changeAddress.sublist(0, 20))) {
throw ArgumentError('Change address must use the source account tag');
}
if (_sameBytes(sourceHash, changeAddress.sublist(20))) {
throw ArgumentError(
'Change address hash must differ from the source hash');
}
final entries = <Uint8List>[];
for (final destination in destinations) {
if (destination.tag.length != 20) {
throw ArgumentError('Invalid destination tag');
}
if (_sameBytes(destination.tag, accountTag)) {
throw ArgumentError(
'A destination tag must differ from the source tag');
}
if (destination.amount <= BigInt.zero) {
throw ArgumentError('A destination amount must be greater than zero');
}
if (!TxDestination.isValidReference(destination.reference)) {
throw ArgumentError('Invalid destination reference');
}
entries.add(destination.toBytes());
}
entries.sort(_compareBytes);
var sendTotal = BigInt.zero;
for (final destination in destinations) {
sendTotal += destination.amount;
}
final count = entries.length;
final signedLength = headerLength + destinationLength * count;
final body = Uint8List(signedLength + wotsValueLength);
body[0] = 0; // TXDAT_MDST
body[1] = 0; // TXDSA_WOTS
body[2] = count - 1;
body.setRange(4, 44, sourceAddress);
body.setRange(44, 84, changeAddress);
body.setRange(84, 92, uint64(sendTotal));
body.setRange(92, 100, uint64(changeAmount));
body.setRange(100, 108, uint64(fee));
body.setRange(108, 116, uint64(blockToLive ?? BigInt.zero));
var offset = headerLength;
for (final entry in entries) {
body.setRange(offset, offset + destinationLength, entry);
offset += destinationLength;
}
final message = MochimoHasher.hash(body.sublist(0, signedLength));
final publicSeed =
sourceWots.sublist(WOTS.WOTSSIGBYTES, WOTS.WOTSSIGBYTES + 32);
final addressSeed = Uint8List.fromList(
sourceWots.sublist(WOTS.WOTSSIGBYTES + 32, WOTS.WOTSSIGBYTES + 64));
final signature = Uint8List(WOTS.WOTSSIGBYTES);
WOTS.wotsSign(signature, message, signingSeed, publicSeed, 0, addressSeed);
final recovered =
WOTS.wotsPkFromSig(signature, message, publicSeed, addressSeed);
if (!_sameBytes(recovered, publicKey)) {
// Reached only when this library signs a key it cannot verify.
throw StateError(// coverage:ignore-line
'WOTS signature does not recover the source public key'); // coverage:ignore-line
}
// tx.c stamps the published address seed after signing.
WOTS.stampAddressSuffix(addressSeed);
body.setRange(signedLength, signedLength + WOTS.WOTSSIGBYTES, signature);
body.setRange(signedLength + WOTS.WOTSSIGBYTES,
signedLength + WOTS.WOTSSIGBYTES + 32, publicSeed);
body.setRange(
signedLength + WOTS.WOTSSIGBYTES + 32, body.length, addressSeed);
return MochimoTransaction(body);
}