tagion_dart_api 1.0.0 copy "tagion_dart_api: ^1.0.0" to clipboard
tagion_dart_api: ^1.0.0 copied to clipboard

Tagion dart api plugin.

example/lib/main.dart

import 'dart:ffi';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:tagion_dart_api/exception/crypto_exception.dart';
import 'package:tagion_dart_api/module/basic/basic.dart';
import 'package:tagion_dart_api/module/crypto/crypto.dart';
import 'package:tagion_dart_api/module/crypto/crypto_interface.dart';
import 'package:tagion_dart_api/module/crypto/ffi/crypto_ffi.dart';
import 'package:tagion_dart_api/pointer_manager/pointer_manager.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  //
  late final TGNBasic _basic;
  late final ICrypto _crypto;
  //
  Pointer<SecureNet> pointerSecureNet = const PointerManager().allocate<SecureNet>();
  String passPhrase = 'passPhrase';
  String pinCode = 'pinCode';
  String salt = 'salt';
  Uint8List dataToSign = Uint8List.fromList([
    0,
    1,
    2,
    3,
    4,
    5,
    6,
    7,
    8,
    9,
    10,
    11,
    12,
    13,
    14,
    15,
    16,
    17,
    18,
    19,
    20,
    21,
    22,
    23,
    24,
    25,
    26,
    27,
    28,
    29,
    30,
    31
  ]);
  //
  Uint8List _devicePin = Uint8List.fromList([]);
  String decrypted = '';
  String signature = '';

  @override
  void initState() {
    super.initState();
    //
    _basic = TGNBasic.init();
    _basic.startDRuntime();
    //
    _crypto = TGNCrypto.init();
  }

  void generateKeypair() {
    setState(() {
      _devicePin = _crypto.generateKeypair(
        passPhrase,
        pinCode,
        salt,
        pointerSecureNet,
      );
    });
  }

  void zeroOutSecureNetPointer() {
    setState(() {
      const PointerManager().zeroOutAndFree(pointerSecureNet, 1);
    });
  }

  void decryptDevicePin() {
    try {
      _crypto.decryptDevicePin(pinCode, _devicePin, pointerSecureNet);
      setState(() {
        decrypted = 'Success';
      });
    } on CryptoApiException catch (e) {
      setState(() {
        decrypted = '${e.runtimeType}: ${e.errorCode.toString()} - ${e.message}';
      });
    }
  }

  void signData() {
    try {
      setState(() {
        signature = _crypto.sign(dataToSign, pointerSecureNet).toString();
      });
    } on CryptoApiException catch (e) {
      setState(() {
        signature = '${e.runtimeType}: ${e.errorCode.toString()} - ${e.message}';
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Center(
          child: SingleChildScrollView(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              children: [
                Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceAround,
                      children: [
                        Text('Passphrase: $passPhrase\nPincode: $pinCode\nSalt: $salt'),
                        OutlinedButton(
                          onPressed: () => generateKeypair(),
                          child: const Text('Generate keypair'),
                        ),
                      ],
                    ),
                    const SizedBox(height: 20),
                    Text('Device pin: $_devicePin\n'),
                  ],
                ),
                Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceAround,
                      children: [
                        OutlinedButton(
                          onPressed: () => zeroOutSecureNetPointer(),
                          child: const Text('Clear secure net pointer'),
                        ),
                      ],
                    ),
                    const SizedBox(height: 20),
                    Text('SecureNet pointer bytes: ${pointerSecureNet.cast<Uint8>().asTypedList(1)}'),
                  ],
                ),
                Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceAround,
                      children: [
                        Text('Pincode: $pinCode\nDevicePin: See above'),
                        OutlinedButton(
                          onPressed: () => decryptDevicePin(),
                          child: const Text('Decrypt device pin'),
                        ),
                      ],
                    ),
                    const SizedBox(height: 20),
                    Text('Decrypted: $decrypted'),
                  ],
                ),
                Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Text('Data: $dataToSign'),
                    OutlinedButton(
                      onPressed: () => signData(),
                      child: const Text('Sign data'),
                    ),
                    const SizedBox(height: 20),
                    Text('Signature: $signature'),
                  ],
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}