Skip to content

Commit 5250c93

Browse files
authored
Merge pull request #7 from codescalers/main_signer
Add Signer pkg
2 parents b92461b + 6ff61c1 commit 5250c93

19 files changed

+708
-475
lines changed

.dart_tool/package_config.json

Lines changed: 0 additions & 404 deletions
This file was deleted.

.gitignore

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
coverage/
2+
coverage.lcov
3+
4+
.dart_tool
5+
/packages/*/.dart_tool
6+
/packages/*/build
7+
/packages/*/idea
8+
9+
coverage/
10+
*.iml
11+
.idea

Makefile

Whitespace-only changes.

README.md

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,48 @@
11
# tfgrid-sdk-dart
22

3-
[![melos](https://img.shields.io/badge/maintained%20with-melos-f700ff.svg?style=flat-square)](https://github.com/invertase/melos)
3+
[![melos](https://img.shields.io/badge/maintained%20with-melos-f700ff.svg?style=flat-square)](https://github.com/invertase/melos)
4+
## Prerequisites
5+
6+
Make sure you have the following tools installed:
7+
8+
- [Dart SDK](https://dart.dev/get-dart)
9+
- [Melos](https://pub.dev/packages/melos)
10+
- [Coverde](https://pub.dev/packages/coverde)
11+
12+
## Initialize Workspace
13+
14+
To initialize workspace in all packages run the following command
15+
16+
```bash
17+
melos bootstrap
18+
```
19+
20+
## Code Analysis
21+
22+
To analyze code in all dart package run the following command
23+
24+
```bash
25+
melos run analyze
26+
```
27+
28+
## Run Unit Tests
29+
30+
To run unit tests in all packages that have `test` directory, run the following command
31+
32+
```bash
33+
melos run unit_test
34+
```
35+
36+
## Generate Coverage
37+
38+
To generate coverage for all packages and merge the results, use the following commands
39+
40+
```bash
41+
melos run unit_test_and_coverage
42+
```
43+
44+
Check results from `result.txt` with the following command
45+
46+
```bash
47+
cat coverage/result.txt
48+
```

melos.yaml

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1-
name: tfgrid-sdk-dart
1+
name: tfgrid_sdk_dart_monorepo
22
packages:
3-
- packages/*
3+
- packages/**
4+
5+
scripts:
6+
analyze:
7+
run: melos exec -- flutter analyze .
8+
description: Run `dart analyze` in all packages.
9+
unit_test:
10+
run: melos exec --dir-exists="test" --fail-fast -- flutter test --no-pub --coverage
11+
description: Run all Flutter tests in this project.
12+
unit_test_and_coverage:
13+
description: Merge all packages coverage tracefiles ignoring data related to generated files.
14+
run: |
15+
melos exec rm -rf coverage
16+
melos run unit_test
17+
melos exec dart pub global run coverde filter --input ./coverage/lcov.info --output ./coverage/filtered.lcov.info --filters .g.dart
18+
melos exec dart pub global run coverde value -i ./coverage/filtered.lcov.info > ./coverage/result.txt
19+
20+

melos_tfgrid-sdk-dart.iml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="WEB_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$" isTestSource="false" />
7+
</content>
8+
<orderEntry type="sourceFolder" forTests="false" />
9+
<orderEntry type="library" name="Dart SDK" level="project" />
10+
<orderEntry type="library" name="Dart Packages" level="project" />
11+
</component>
12+
</module>

packages/signer/.gitignore

Lines changed: 0 additions & 3 deletions
This file was deleted.

packages/signer/Makefile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.PHONY: unit_test_and_coverage
2+
3+
coverage:
4+
@rm -rf coverage && \
5+
flutter test --no-pub --coverage && \
6+
dart pub global run coverde filter --input ./coverage/lcov.info --output ./coverage/filtered.lcov.info --filters .g.dart && \
7+
dart pub global run coverde value -i ./coverage/filtered.lcov.info > ./coverage/result.txt

packages/signer/README.md

Lines changed: 83 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,83 @@
1-
A sample command-line application with an entrypoint in `bin/`, library code
2-
in `lib/`, and example unit test in `test/`.
1+
# Signer
2+
3+
[![Coverage Status](https://img.shields.io/badge/coverage-86.4%25-brightgreen.svg)](https://your-coverage-report-url)
4+
5+
Signer is a dart client created to sign and verify raw data using mnemonic or seed.
6+
7+
## Requirements
8+
9+
[Dart](https://dart.dev/get-dart) >= 3.2.0
10+
11+
## Examples
12+
13+
This is a simple example to sign and verify data using mnemonic.
14+
15+
```dart
16+
// Add your mnemonic
17+
final mnemonic = 'your-mnemonic';
18+
19+
// Create Instance of signer
20+
final signer = Signer();
21+
22+
// Initialize signer from mnemonic
23+
await signer.fromMnemonic(mnemonic);
24+
25+
// Add your data
26+
final data = "anydata123";
27+
28+
// Sign data with mnemonic
29+
final signatureFromMnemonic = await signer.sign(data);
30+
31+
// Verify signed data with your mnemonic
32+
final res = await signer.verify(signatureFromMnemonic, data);
33+
```
34+
35+
This is a simple example to sign and verify data using hex seed.
36+
37+
```dart
38+
// Create Instance of signer
39+
final signer = Signer();
40+
41+
// Add your hex seed
42+
final seed = '0x.....';
43+
44+
// Initialize signer from your seed
45+
await signer.fromHexSeed(seed);
46+
47+
// Add your data
48+
final data = "anydata123";
49+
50+
// Sign data with your seed
51+
final signatureFromSeed = await signer.sign(data);
52+
53+
// Verify signed data with your mnemonic
54+
final res = await signer.verify(signatureFromSeed, data);
55+
```
56+
57+
## Initialize Workspace
58+
To initialize workspace and get all packages, run the following command.
59+
```bash
60+
dart pub get
61+
```
62+
63+
## Run Tests
64+
65+
To run the tests, run the following command
66+
67+
```bash
68+
dart test
69+
```
70+
71+
## Generate Coverage
72+
73+
To generate coverage signer package, run the following command
74+
75+
```bash
76+
make coverage
77+
```
78+
79+
Check results from `result.txt` with the following command
80+
81+
```bash
82+
cat coverage/result.txt
83+
```

packages/signer/bin/signer.dart

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import 'package:signer/signer.dart';
2+
import 'dart:typed_data';
3+
4+
// Just for testing will be deleted soon
5+
6+
void main() async {
7+
final mnemonic =
8+
'picnic flip cigar rival risk scatter slide aware trust garlic solution token';
9+
final signer = Signer();
10+
await signer.fromMnemonic(mnemonic, KPType.ed25519);
11+
final data = "anydata123";
12+
final signatureFromMnemonic = signer.sign(data);
13+
14+
print("Signature from mnemonic: $signatureFromMnemonic");
15+
16+
final seedUint8List = Uint8List.fromList([
17+
108,
18+
29,
19+
148,
20+
202,
21+
167,
22+
191,
23+
10,
24+
13,
25+
126,
26+
240,
27+
152,
28+
60,
29+
24,
30+
35,
31+
233,
32+
172,
33+
106,
34+
190,
35+
213,
36+
114,
37+
33,
38+
213,
39+
125,
40+
9,
41+
34,
42+
101,
43+
67,
44+
82,
45+
216,
46+
92,
47+
177,
48+
228
49+
]);
50+
51+
final signer2 = Signer();
52+
await signer2.fromSeed(seedUint8List, KPType.ed25519);
53+
final signatureFromSeed = signer2.sign(data);
54+
print("Signature from Seed: $signatureFromSeed");
55+
56+
final signer3 = Signer();
57+
signer3.fromHexSeed(
58+
'0x6c1d94caa7bf0a0d7ef0983c1823e9ac6abed57221d57d0922654352d85cb1e4',
59+
KPType.ed25519);
60+
final signatureFromSeed2 = await signer3.sign(data);
61+
print("Signature from Hex Seed: $signatureFromSeed2");
62+
}

packages/signer/lib/signer.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
library signer;
2+
3+
import 'dart:convert';
4+
import 'dart:typed_data';
5+
import 'package:polkadart_keyring/polkadart_keyring.dart';
6+
import 'package:hex/hex.dart';
7+
8+
part 'src/sign.dart';
9+
part 'src/types.dart';

packages/signer/lib/src/sign.dart

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
part of '../signer.dart';
2+
3+
class Signer {
4+
KeyPair? keypair;
5+
6+
Future<KeyPair?> fromMnemonic(String mnemonic, KPType type) async {
7+
try {
8+
if (type == KPType.sr25519) {
9+
keypair = await KeyPair.sr25519.fromMnemonic(mnemonic);
10+
} else if (type == KPType.ed25519) {
11+
keypair = await KeyPair.ed25519.fromMnemonic(mnemonic);
12+
}
13+
return keypair;
14+
} catch (e) {
15+
throw Exception("Failed to create keyPair from mnemonic. Error: $e");
16+
}
17+
}
18+
19+
KeyPair? fromSeed(Uint8List seed, KPType type) {
20+
try {
21+
if (type == KPType.sr25519) {
22+
keypair = KeyPair.sr25519.fromSeed(seed);
23+
} else if (type == KPType.ed25519) {
24+
keypair = KeyPair.ed25519.fromSeed(seed);
25+
}
26+
return keypair;
27+
} catch (e) {
28+
throw Exception("Failed to create keyPair from seed. Error: $e");
29+
}
30+
}
31+
32+
void fromHexSeed(String hexSeed, KPType type) {
33+
try {
34+
final seed = HEX.decode(hexSeed.replaceAll('0x', ''));
35+
keypair = fromSeed(Uint8List.fromList(seed), type);
36+
} catch (e) {
37+
throw Exception("Failed to create keyPair from hex seed. Error: $e");
38+
}
39+
}
40+
41+
String sign(String data) {
42+
if (keypair == null) {
43+
throw Exception("keypair not initialized.");
44+
}
45+
try {
46+
final dataBytes = Uint8List.fromList(utf8.encode(data));
47+
final signature = keypair!.sign(dataBytes);
48+
return HEX.encode(signature);
49+
} catch (e) {
50+
throw Exception("Failed to sign data. Error: $e");
51+
}
52+
}
53+
54+
bool verify(String signature, String data) {
55+
if (keypair == null) {
56+
throw Exception("keypair not initialized.");
57+
}
58+
try {
59+
final dataBytes = Uint8List.fromList(utf8.encode(data));
60+
final signatureBytes = HEX.decode(signature);
61+
return keypair!.verify(dataBytes, Uint8List.fromList(signatureBytes));
62+
} catch (e) {
63+
throw Exception("Failed to verify signature. Error: $e");
64+
}
65+
}
66+
}

packages/signer/lib/src/types.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
part of '../signer.dart';
2+
3+
enum KPType {
4+
sr25519,
5+
ed25519,
6+
}

packages/signer/melos_signer.iml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="WEB_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$" isTestSource="false" />
7+
<sourceFolder url="file://$MODULE_DIR$/test" isTestSource="true" />
8+
<excludeFolder url="file://$MODULE_DIR$/.dart_tool" />
9+
<excludeFolder url="file://$MODULE_DIR$/.pub" />
10+
<excludeFolder url="file://$MODULE_DIR$/build" />
11+
</content>
12+
<orderEntry type="sourceFolder" forTests="false" />
13+
<orderEntry type="library" name="Dart SDK" level="project" />
14+
<orderEntry type="library" name="Dart Packages" level="project" />
15+
</component>
16+
</module>

0 commit comments

Comments
 (0)