stack_wallet/lib/utilities/extensions/impl/big_int.dart
2024-05-27 18:01:41 -06:00

43 lines
1,020 B
Dart

/*
* This file is part of Stack Wallet.
*
* Copyright (c) 2023 Cypher Stack
* All Rights Reserved.
* The code is distributed under GPLv3 license, see LICENSE file for details.
* Generated by Cypher Stack on 2023-05-26
*
*/
import 'dart:typed_data';
extension BigIntExtensions on BigInt {
String get toHex {
if (this < BigInt.zero) {
throw Exception("BigInt value is negative");
}
final String hex = toRadixString(16);
if (hex.length % 2 == 0) {
return hex;
} else {
return "0$hex";
}
}
String get toHexUppercase => toHex.toUpperCase();
Uint8List get toBytes {
if (this < BigInt.zero) {
throw Exception("BigInt value is negative");
}
BigInt number = this;
final int bytes = (number.bitLength + 7) >> 3;
final b256 = BigInt.from(256);
final result = Uint8List(bytes);
for (int i = 0; i < bytes; i++) {
result[bytes - 1 - i] = number.remainder(b256).toInt();
number = number >> 8;
}
return result;
}
}