Full Example

To demonstrate the usage of the library, you need to install sigpair-sdk2 and buffer. In this context, the Flutter library represents Party 1, while sigpair-sdk2 represents Party 2.

Install sigpair-sdk2

The .npmrc the file needs to be configured as described in Configure npm to use the GitLab registry

Install package sigpair-sdk2

With yarn

export GITLAB_AUTH_TOKEN=<You company access token> yarn add @com.silencelaboratories/sigpair-sdk2

Or using npm

export GITLAB_AUTH_TOKEN=<You company access token> npm install @com.silencelaboratories/sigpair-sdk2

Install package buffer

yarn add buffer

Or using npm

npm install buffer
```dart
void main() {
  runApp(const MyApp());
}

enum TestState {
  readyToRun,
  running,
  success,
  failure;

  @override
  String toString() {
    switch (this) {
      case readyToRun:
        return 'Test is ready to run';
      case running:
        return 'Test is running';
      case success:
        return 'Test succeeded';
      case failure:
        return 'Test failed';
    }
  }
}

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

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

const nodeUri = 'xxxx';
const adminToken = 'xxxx';
const msgHash1 = '4qFZ0Xt7txSu12ddfH05TeyNLkM3hChIEEaUv4nHHAM=';
const msgHash2 = '8sobtsfpB9Btr+Roflefznazfk6Tt2BQItpS5szCb9I=';

class _MyAppState extends State<MyApp> {
  late final sdk = dart_2_party_cloud_sdk.Dart2PartyCloudSDK();
  late final cloudClient = dart_2_party_cloud_sdk.CloudClient(nodeUri, isHttp: true, token: adminToken);
  final testUserId = 6;

  // State ivars
  TestState _testState = TestState.readyToRun;
  String? _log;

  void _updateTestState(TestState newTestState) {
    setState(() {
      _testState = newTestState;
    });
  }

  void _addLogEntry(String newEntry, [String separator = '\n\n']) {
    setState(() {
      _log = _log == null ? newEntry : '$_log$separator$newEntry';
    });
  }

  void _clearLog() {
    setState(() {
      _log = null;
    });
  }

  void testKeygen() async {
    try {
      _clearLog();
      _updateTestState(TestState.running);
      cloudClient.token = adminToken;

      final tokenResponse = await cloudClient.createUserToken(dart_2_party_cloud_sdk.CreateUserTokenRequest(testUserId));
      cloudClient.token = tokenResponse.token;
      _addLogEntry('Created user token: ${tokenResponse.token}');

      final partyKeys = sdk.partyKeys();
      final partyKeysBytes = partyKeys.toBytes();
      final restoredPartyKeys = sdk.loadPartyKeys(partyKeysBytes);
      if (partyKeys.publicKey() != restoredPartyKeys.publicKey() || partyKeysBytes != restoredPartyKeys.toBytes()) {
        throw 'Party keys serialization/deserialization failed';
      }
      _addLogEntry('Party keys serialization/deserialization succeeded');

      final keygenSession = sdk.keygenSession(cloudClient, partyKeys);
      final keyshare = await keygenSession.generate();
      _addLogEntry('Generated new keyshare:');
      _addLogEntry('\tId: ${keyshare.id}', '\n');
      _addLogEntry('\tPublic Key: ${keyshare.publicKey}', '\n');

      final keyshareBytes = keyshare.toBytes();
      var restoredKeyshare = sdk.loadKeyshare(keyshareBytes);
      if (keyshare.id != restoredKeyshare.id || keyshare.publicKey != restoredKeyshare.publicKey || keyshareBytes != restoredKeyshare.toBytes()) {
        throw 'Keyshare serialization/deserialization failed';
      }
      _addLogEntry('Keyshare serialization/deserialization succeeded');

      final signSession = sdk.signSession(cloudClient, keyshare, partyKeys);
      final signature1 = await signSession.sign(msgHash1);
      _addLogEntry('Generated signature for message 1: $signature1');

      final signature2 = await signSession.sign(msgHash1);
      _addLogEntry('Generated signature for message 2: $signature2');

      final refreshedKeyshare = await keygenSession.refresh(keyshare);
      _addLogEntry('Refreshed keyshare:');
      _addLogEntry('\tId: ${refreshedKeyshare.id}', '\n');
      _addLogEntry('\tPublic Key: ${refreshedKeyshare.publicKey}', '\n');

      final refreshedKeyshareBytes = refreshedKeyshare.toBytes();
      restoredKeyshare = sdk.loadKeyshare(refreshedKeyshareBytes);
      if (refreshedKeyshare.id != restoredKeyshare.id ||
          refreshedKeyshare.publicKey != restoredKeyshare.publicKey ||
          refreshedKeyshareBytes != restoredKeyshare.toBytes()) {
        throw 'Restored keyshare serialization/deserialization failed';
      }
      _addLogEntry('Restored keyshare serialization/deserialization succeeded');

      final newSignSession = sdk.signSession(cloudClient, refreshedKeyshare, partyKeys);
      final newSignature1 = await newSignSession.sign(msgHash1);
      _addLogEntry('Generated signature for message 1: $newSignature1');

      final newSignature2 = await newSignSession.sign(msgHash1);
      _addLogEntry('Generated signature for message 2: $newSignature2');

      _addLogEntry('Test succeeded');
      _updateTestState(TestState.success);
    } catch (e) {
      _addLogEntry('Error: ${e.toString()}');
      _addLogEntry('Test failed');
      _updateTestState(TestState.failure);
    }
  }

  @override
  Widget build(BuildContext context) {
    const infoTextStyle = TextStyle(fontSize: 16);
    const spacer = SizedBox(height: 30);
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text(_testState.toString()),
        ),
        body: SingleChildScrollView(
          child: Container(
            width: double.infinity,
            padding: const EdgeInsets.all(20),
            child: Column(
              mainAxisSize: MainAxisSize.max,
              children: [
                Row(children: [
                  Expanded(
                    child: DecoratedBox(
                      decoration: const BoxDecoration(color: Color.fromARGB(255, 211, 206, 206)),
                      child: SelectionArea(
                        child: Text(
                          _log ?? '',
                          style: infoTextStyle,
                        ),
                      ),
                    ),
                  ),
                ]),
                spacer,
                ElevatedButton(
                    onPressed: _testState == TestState.running ? null : testKeygen,
                    child: Text(_testState == TestState.readyToRun ? 'Run test' : 'Rerun test')),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

```

Last updated