stack_wallet/lib/utilities/extensions/impl/big_int.dart

44 lines
1,014 B
Dart
Raw Normal View History

2023-05-26 21:21:16 +00:00
/*
* 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
*
*/
2023-03-03 17:35:43 +00:00
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;
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;
}
}