From 23d38ef4d9ff9a73e759bb8769060f16c6a6f6d5 Mon Sep 17 00:00:00 2001 From: julian Date: Fri, 3 Mar 2023 11:35:43 -0600 Subject: [PATCH] added some utility extensions --- lib/utilities/extensions/extensions.dart | 3 ++ lib/utilities/extensions/impl/big_int.dart | 33 +++++++++++++++++ lib/utilities/extensions/impl/string.dart | 17 +++++++++ lib/utilities/extensions/impl/uint8_list.dart | 36 +++++++++++++++++++ pubspec.lock | 6 ++-- pubspec.yaml | 3 ++ 6 files changed, 95 insertions(+), 3 deletions(-) create mode 100644 lib/utilities/extensions/extensions.dart create mode 100644 lib/utilities/extensions/impl/big_int.dart create mode 100644 lib/utilities/extensions/impl/string.dart create mode 100644 lib/utilities/extensions/impl/uint8_list.dart diff --git a/lib/utilities/extensions/extensions.dart b/lib/utilities/extensions/extensions.dart new file mode 100644 index 000000000..792ebe0b3 --- /dev/null +++ b/lib/utilities/extensions/extensions.dart @@ -0,0 +1,3 @@ +export 'impl/big_int.dart'; +export 'impl/string.dart'; +export 'impl/uint8_list.dart'; diff --git a/lib/utilities/extensions/impl/big_int.dart b/lib/utilities/extensions/impl/big_int.dart new file mode 100644 index 000000000..c9b78ab55 --- /dev/null +++ b/lib/utilities/extensions/impl/big_int.dart @@ -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; + } +} diff --git a/lib/utilities/extensions/impl/string.dart b/lib/utilities/extensions/impl/string.dart new file mode 100644 index 000000000..4e996e05b --- /dev/null +++ b/lib/utilities/extensions/impl/string.dart @@ -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; +} diff --git a/lib/utilities/extensions/impl/uint8_list.dart b/lib/utilities/extensions/impl/uint8_list.dart new file mode 100644 index 000000000..5dcf0b435 --- /dev/null +++ b/lib/utilities/extensions/impl/uint8_list.dart @@ -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; + } +} diff --git a/pubspec.lock b/pubspec.lock index 0f83b1eca..a7948d64a 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -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" diff --git a/pubspec.yaml b/pubspec.yaml index f102ebf55..b0b362827 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -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: