mirror of
https://github.com/cypherstack/stack_wallet.git
synced 2025-01-10 20:54:33 +00:00
added some utility extensions
This commit is contained in:
parent
1814fb3752
commit
23d38ef4d9
6 changed files with 95 additions and 3 deletions
3
lib/utilities/extensions/extensions.dart
Normal file
3
lib/utilities/extensions/extensions.dart
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
export 'impl/big_int.dart';
|
||||||
|
export 'impl/string.dart';
|
||||||
|
export 'impl/uint8_list.dart';
|
33
lib/utilities/extensions/impl/big_int.dart
Normal file
33
lib/utilities/extensions/impl/big_int.dart
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
17
lib/utilities/extensions/impl/string.dart
Normal file
17
lib/utilities/extensions/impl/string.dart
Normal 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;
|
||||||
|
}
|
36
lib/utilities/extensions/impl/uint8_list.dart
Normal file
36
lib/utilities/extensions/impl/uint8_list.dart
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -355,14 +355,14 @@ packages:
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "1.0.0"
|
version: "1.0.0"
|
||||||
dart_bs58:
|
dart_bs58:
|
||||||
dependency: transitive
|
dependency: "direct main"
|
||||||
description:
|
description:
|
||||||
name: dart_bs58
|
name: dart_bs58
|
||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "1.0.1"
|
version: "1.0.1"
|
||||||
dart_bs58check:
|
dart_bs58check:
|
||||||
dependency: transitive
|
dependency: "direct main"
|
||||||
description:
|
description:
|
||||||
name: dart_bs58check
|
name: dart_bs58check
|
||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
|
@ -716,7 +716,7 @@ packages:
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "2.2.0"
|
version: "2.2.0"
|
||||||
hex:
|
hex:
|
||||||
dependency: transitive
|
dependency: "direct main"
|
||||||
description:
|
description:
|
||||||
name: hex
|
name: hex
|
||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
|
|
|
@ -146,6 +146,9 @@ dependencies:
|
||||||
dropdown_button2: 1.7.2
|
dropdown_button2: 1.7.2
|
||||||
string_validator: ^0.3.0
|
string_validator: ^0.3.0
|
||||||
equatable: ^2.0.5
|
equatable: ^2.0.5
|
||||||
|
dart_bs58: ^1.0.1
|
||||||
|
dart_bs58check: ^3.0.2
|
||||||
|
hex: ^0.2.0
|
||||||
|
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
flutter_test:
|
flutter_test:
|
||||||
|
|
Loading…
Reference in a new issue