added some utility extensions

This commit is contained in:
julian 2023-03-03 11:35:43 -06:00
parent 1814fb3752
commit 23d38ef4d9
6 changed files with 95 additions and 3 deletions

View file

@ -0,0 +1,3 @@
export 'impl/big_int.dart';
export 'impl/string.dart';
export 'impl/uint8_list.dart';

View file

@ -0,0 +1,33 @@
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;
}
}

View file

@ -0,0 +1,17 @@
import 'dart:typed_data';
import 'package:dart_bs58/dart_bs58.dart';
import 'package:dart_bs58check/dart_bs58check.dart';
import 'package:hex/hex.dart';
import 'package:stackwallet/utilities/extensions/extensions.dart';
extension StringExtensions on String {
Uint8List get toUint8ListFromHex =>
Uint8List.fromList(HEX.decode(startsWith("0x") ? substring(2) : this));
Uint8List get toUint8ListFromBase58Encoded => bs58.decode(this);
Uint8List get toUint8ListFromBase58CheckEncoded => bs58check.decode(this);
BigInt get toBigIntFromHex => toUint8ListFromHex.toBigInt;
}

View file

@ -0,0 +1,36 @@
import 'dart:typed_data';
import 'package:dart_bs58/dart_bs58.dart';
import 'package:dart_bs58check/dart_bs58check.dart';
import 'package:hex/hex.dart';
extension Uint8ListExtensions on Uint8List {
String get toHex {
return HEX.encode(this);
}
String get toBase58Encoded {
return bs58.encode(this);
}
String get toBase58CheckEncoded {
return bs58check.encode(this);
}
/// returns copy of byte list in reverse order
Uint8List get reversed {
final reversed = Uint8List(length);
for (final byte in this) {
reversed.insert(0, byte);
}
return reversed;
}
BigInt get toBigInt {
BigInt number = BigInt.zero;
for (final byte in this) {
number = (number << 8) | BigInt.from(byte & 0xff);
}
return number;
}
}

View file

@ -355,14 +355,14 @@ packages:
source: hosted
version: "1.0.0"
dart_bs58:
dependency: transitive
dependency: "direct main"
description:
name: dart_bs58
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.1"
dart_bs58check:
dependency: transitive
dependency: "direct main"
description:
name: dart_bs58check
url: "https://pub.dartlang.org"
@ -716,7 +716,7 @@ packages:
source: hosted
version: "2.2.0"
hex:
dependency: transitive
dependency: "direct main"
description:
name: hex
url: "https://pub.dartlang.org"

View file

@ -146,6 +146,9 @@ dependencies:
dropdown_button2: 1.7.2
string_validator: ^0.3.0
equatable: ^2.0.5
dart_bs58: ^1.0.1
dart_bs58check: ^3.0.2
hex: ^0.2.0
dev_dependencies:
flutter_test: