Skip to content

Commit

Permalink
🐛 Fix toBigInt and fromBigInt calculations
Browse files Browse the repository at this point in the history
  • Loading branch information
redDwarf03 committed Nov 20, 2023
1 parent 0527f21 commit 665e1c1
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
Changelog
=========
#### Version 3.3.4
* Fix `toBigInt` and `fromBigInt` calculations

#### Version 3.3.3
* Fix error transaction signature when a value has the 8th decimal > 5

Expand Down
8 changes: 6 additions & 2 deletions lib/src/utils/utils.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/// SPDX-License-Identifier: AGPL-3.0-or-later
import 'dart:math';
import 'package:decimal/decimal.dart';
import 'dart:typed_data';
import 'package:pinenacl/encoding.dart' show Base16Encoder;

Expand Down Expand Up @@ -42,7 +43,9 @@ int toBigInt(num? number) {
if (number == null) {
return 0;
}
return (number * 100000000).floor();
return (Decimal.parse(number.toString()) * Decimal.parse('100000000'))
.toDouble()
.floor();
}

/// Convert big int of 10^8 decimals to any number
Expand All @@ -51,7 +54,8 @@ num fromBigInt(int? number) {
if (number == null) {
return 0;
}
return number / 100000000;
return (Decimal.parse(number.toString()) / Decimal.parse('100000000'))
.toDouble();
}

/// Convert any number into a byte array
Expand Down
16 changes: 16 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.3.3"
decimal:
dependency: "direct main"
description:
name: decimal
sha256: "24a261d5d5c87e86c7651c417a5dbdf8bcd7080dd592533910e8d0505a279f21"
url: "https://pub.dev"
source: hosted
version: "2.3.3"
dependency_validator:
dependency: "direct dev"
description:
Expand Down Expand Up @@ -545,6 +553,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "3.2.1"
rational:
dependency: transitive
description:
name: rational
sha256: ba58e9e18df9abde280e8b10051e4bce85091e41e8e7e411b6cde2e738d357cf
url: "https://pub.dev"
source: hosted
version: "2.2.2"
rxdart:
dependency: transitive
description:
Expand Down
5 changes: 4 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: archethic_lib_dart
description: Archethic dart library for Flutter for Node and Browser. This library aims to provide a easy way to create Archethic transaction and to send them over the network
homepage: https://github.com/archethic-foundation/libdart

version: 3.3.3
version: 3.3.4

environment:
sdk: ">=3.1.0 <4.0.0"
Expand All @@ -15,6 +15,9 @@ dependencies:
# Implementations of SHA, MD5, and HMAC cryptographic functions
crypto: ^3.0.3

# The decimal package allows you to deal with decimal numbers without losing precision
decimal: ^2.3.3

# Package ecdsa implements the Elliptic Curve Digital Signature Algorithm, as defined in FIPS 186-3
ecdsa: ^0.0.4

Expand Down
22 changes: 22 additions & 0 deletions test/utils_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,28 @@ void main() {
test('should convert a double into a big integer with 10^8', () {
expect(toBigInt(1.234567), 123456700);
});

test('should convert a double into a big integer with 10^8', () {
expect(toBigInt(9.95), 995000000);
});

test('should convert a double into a big integer with 10^8', () {
expect(toBigInt(0.617714895), 61771489);
});
});

group('fromBigInt', () {
test('should convert a big integer with 10^8 into double', () {
expect(1.234567, fromBigInt(123456700));
});

test('should convert a big integer with 10^8 into double', () {
expect(9.95, fromBigInt(995000000));
});

test('should convert a big integer with 10^8 into double', () {
expect(0.61771489, fromBigInt(61771489));
});
});

group('toByteArray', () {
Expand Down

0 comments on commit 665e1c1

Please sign in to comment.