From 5e802426dd86d44121f0b51f6c0994a3c96be618 Mon Sep 17 00:00:00 2001
From: vjrj <vjrj@comunes.org>
Date: Fri, 29 Mar 2024 23:53:46 +0100
Subject: [PATCH] First v2 works. envied use

---
 lib/env.dart             |  32 ++++++
 lib/env.g.dart           | 211 +++++++++++++++++++++++++++++++++++++++
 lib/g1/g1_v2_helper.dart |  34 +++++++
 test/g1_v2_test.dart     |  29 ++++++
 4 files changed, 306 insertions(+)
 create mode 100644 lib/env.dart
 create mode 100644 lib/env.g.dart
 create mode 100644 lib/g1/g1_v2_helper.dart
 create mode 100644 test/g1_v2_test.dart

diff --git a/lib/env.dart b/lib/env.dart
new file mode 100644
index 00000000..64562757
--- /dev/null
+++ b/lib/env.dart
@@ -0,0 +1,32 @@
+// lib/env/env.dart
+// ignore_for_file: avoid_classes_with_only_static_members
+
+import 'package:envied/envied.dart';
+import 'package:flutter/foundation.dart';
+
+part 'env.g.dart';
+
+@Envied(path: kReleaseMode ? '.env' : '.env.dev', allowOptionalFields: true)
+abstract class Env {
+  @EnviedField(varName: 'CURRENCY')
+  static const String currency = _Env.currency;
+  @EnviedField(varName: 'SENTRY_DSN', obfuscate: true)
+  static final String? sentryDsn = _Env.sentryDsn;
+
+  @EnviedField(varName: 'GITLAB_TOKEN', obfuscate: true)
+  static final String gitLabToken = _Env.gitLabToken;
+
+  // Nodes
+  // v1
+  // @EnviedField(defaultValue: 'test_')
+  @EnviedField(varName: 'DUNITER_NODES')
+  static const String duniterNodes = _Env.duniterNodes;
+  @EnviedField(varName: 'CESIUM_PLUS_NODES')
+  static const String cesiumPlusNodes = _Env.cesiumPlusNodes;
+  @EnviedField(varName: 'GVA_NODES')
+  static const String gvaNodes = _Env.gvaNodes;
+
+  // v2
+  @EnviedField(varName: 'ENDPOINTS')
+  static const String endPoints = _Env.endPoints;
+}
diff --git a/lib/env.g.dart b/lib/env.g.dart
new file mode 100644
index 00000000..367a0096
--- /dev/null
+++ b/lib/env.g.dart
@@ -0,0 +1,211 @@
+// GENERATED CODE - DO NOT MODIFY BY HAND
+
+part of 'env.dart';
+
+// **************************************************************************
+// EnviedGenerator
+// **************************************************************************
+
+// coverage:ignore-file
+// ignore_for_file: type=lint
+final class _Env {
+  static const String currency = 'g1';
+
+  static const List<int> _enviedkeysentryDsn = <int>[
+    200271289,
+    3853299757,
+    287108020,
+    576680077,
+    3865571294,
+    588150570,
+    1088974894,
+    3323932897,
+    1683844751,
+    3139416939,
+    2121525008,
+    1971345953,
+    1088674386,
+    2216131141,
+    2206133945,
+    2497063502,
+    718537447,
+    616817305,
+    2450457417,
+    3075743221,
+    3459850159,
+    1075152594,
+    1129970435,
+    1140079130,
+    3980179363,
+    443052024,
+    2760731322,
+    176996108,
+    220265319,
+    1182281172,
+    3355338761,
+    980147574,
+    4230465661,
+    1398330458,
+    4049740450,
+    708449756,
+    521772679,
+    3300411179,
+    3984754181,
+    3317372762,
+    2706207692,
+    2229912853,
+    1753922129,
+    935756054,
+    2906195746,
+    1889056666,
+    864194104,
+    1148935166,
+    3981128709,
+    4027446070,
+    756827599,
+    151034948,
+    1968120052,
+    3500060273,
+    2396974716,
+    847756793,
+    688479154,
+    2983309035,
+    1375842159,
+    2650911002,
+    1162025921,
+  ];
+
+  static const List<int> _envieddatasentryDsn = <int>[
+    200271313,
+    3853299801,
+    287108032,
+    576680189,
+    3865571245,
+    588150544,
+    1088974849,
+    3323932878,
+    1683844796,
+    3139416923,
+    2121525030,
+    1971345938,
+    1088674406,
+    2216131184,
+    2206133978,
+    2497063468,
+    718537439,
+    616817326,
+    2450457388,
+    3075743120,
+    3459850139,
+    1075152567,
+    1129970482,
+    1140079225,
+    3980179393,
+    443051930,
+    2760731359,
+    176996149,
+    220265303,
+    1182281190,
+    3355338810,
+    980147472,
+    4230465567,
+    1398330478,
+    4049740483,
+    708449722,
+    521772772,
+    3300411166,
+    3984754275,
+    3317372729,
+    2706207628,
+    2229912934,
+    1753922100,
+    935756152,
+    2906195798,
+    1889056744,
+    864194113,
+    1148935120,
+    3981128806,
+    4027446105,
+    756827554,
+    151034929,
+    1968119962,
+    3500060180,
+    2396974607,
+    847756759,
+    688479197,
+    2983308953,
+    1375842056,
+    2650911029,
+    1162025975,
+  ];
+
+  static final String? sentryDsn = String.fromCharCodes(List<int>.generate(
+    _envieddatasentryDsn.length,
+    (int i) => i,
+    growable: false,
+  ).map((int i) => _envieddatasentryDsn[i] ^ _enviedkeysentryDsn[i]));
+
+  static const List<int> _enviedkeygitLabToken = <int>[
+    1820632391,
+    2741614692,
+    1221080240,
+    3077052417,
+    1175689579,
+    1408005707,
+    2511761521,
+    3777920400,
+    1914320043,
+    1690394812,
+    2356598653,
+    1355401136,
+    186536105,
+    1096726632,
+    3734241214,
+    1916313220,
+    978141910,
+    4078787033,
+    4272166551,
+    3640101148,
+  ];
+
+  static const List<int> _envieddatagitLabToken = <int>[
+    1820632383,
+    2741614606,
+    1221080264,
+    3077052505,
+    1175689535,
+    1408005693,
+    2511761474,
+    3777920458,
+    1914320121,
+    1690394822,
+    2356598582,
+    1355401155,
+    186536138,
+    1096726620,
+    3734241261,
+    1916313300,
+    978141826,
+    4078786967,
+    4272166563,
+    3640101231,
+  ];
+
+  static final String gitLabToken = String.fromCharCodes(List<int>.generate(
+    _envieddatagitLabToken.length,
+    (int i) => i,
+    growable: false,
+  ).map((int i) => _envieddatagitLabToken[i] ^ _enviedkeygitLabToken[i]));
+
+  static const String duniterNodes =
+      'https://g1.duniter.fr https://g1.le-sou.org https://g1.cgeek.fr https://g1.monnaielibreoccitanie.org https://g1.duniter.fr https://g1.le-sou.org https://g1.cgeek.fr';
+
+  static const String cesiumPlusNodes =
+      'https://g1.data.le-sou.org https://g1.data.e-is.pro https://g1.data.presles.fr https://g1.data.mithril.re https://g1.data.brussels.ovh https://g1.data.pini.fr';
+
+  static const String gvaNodes =
+      'https://g1v1.p2p.legal:443/gva https://g1.asycn.io:443/gva https://duniter.pini.fr:443/gva';
+
+  static const String endPoints =
+      'wss://gdev.komun.org/ws wss://1000i100.fr/ws wss://gdev.librelois.fr/ws wss://gdev.p2p.legal/ws wss://gdev.coinduf.eu/ws wss://vit.fdn.org/ws wss://gdev.cgeek.fr/ws wss://gdev.pini.fr/ws';
+}
diff --git a/lib/g1/g1_v2_helper.dart b/lib/g1/g1_v2_helper.dart
new file mode 100644
index 00000000..33d2daa0
--- /dev/null
+++ b/lib/g1/g1_v2_helper.dart
@@ -0,0 +1,34 @@
+import 'dart:typed_data';
+
+import 'package:polkadart_keyring/polkadart_keyring.dart';
+
+Uint8List hexToU8a(String hexString) {
+  hexString = hexString.startsWith('0x') ? hexString.substring(2) : hexString;
+  if (hexString.length % 2 != 0) {
+    hexString = '0$hexString';
+  }
+
+  return Uint8List.fromList(List<int>.generate(hexString.length ~/ 2, (int i) {
+    return int.parse(hexString.substring(i * 2, i * 2 + 2), radix: 16);
+  }));
+}
+
+bool isHex(String value, [int bitLength = -1]) {
+  final RegExp hexRegEx = RegExp(r'^0x[a-fA-F0-9]+$');
+
+  return hexRegEx.hasMatch(value) &&
+      (bitLength == -1 || value.length == 2 + bitLength ~/ 4);
+}
+
+// From:
+// https://polkadot.js.org/docs/util-crypto/examples/validate-address/
+bool isValidV2Address(String address) {
+  try {
+    final Keyring keyring = Keyring();
+    keyring.encodeAddress(
+        isHex(address) ? hexToU8a(address) : keyring.decodeAddress(address));
+    return true;
+  } catch (error) {
+    return false;
+  }
+}
diff --git a/test/g1_v2_test.dart b/test/g1_v2_test.dart
new file mode 100644
index 00000000..2c9fd595
--- /dev/null
+++ b/test/g1_v2_test.dart
@@ -0,0 +1,29 @@
+import 'package:flutter_test/flutter_test.dart';
+import 'package:ginkgo/g1/g1_v2_helper.dart';
+
+void main() {
+  group('isValidAddress', () {
+    test('returns true for valid addresses', () {
+      final List<String> validAddresses = <String>[
+        '5GrpknVvGGrGH3EFuURXeMrWHvbpj3VfER1oX5jFtuGbfzCE',
+        '5FLdosNUhAJ4zW8NKp65yaXEECUwkuNqVRcmjTEsZ8vvkxuP'
+      ];
+
+      for (final String address in validAddresses) {
+        expect(isValidV2Address(address), isTrue);
+      }
+    });
+
+    test('returns false for invalid addresses', () {
+      final List<String> invalidAddresses = <String>[
+        '1G9tTobcmjgjSg2CEGjmFqZBbB3LQ85PhpMXD7NfnKhhJd3',
+        '1KjvmrF1uVSaJvmjF1uVSaJF1uVSaJZ9QZ5',
+        'BrgsSYK3xUzDyztGBHmxq69gfNxBfe2UKpxG21oZUBr5'
+      ];
+
+      for (final String address in invalidAddresses) {
+        expect(isValidV2Address(address), isFalse);
+      }
+    });
+  });
+}
-- 
GitLab