-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
328 additions
and
148 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import 'dart:typed_data'; | ||
|
||
import 'package:polkadart_keyring/polkadart_keyring.dart'; | ||
|
||
void main() { | ||
KeyPair.ed25519.fromSeed( | ||
Uint8List.fromList('12345678901234567890123456789012'.codeUnits)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,16 @@ | ||
library polkadart_keyring; | ||
|
||
import 'dart:convert'; | ||
import 'dart:typed_data'; | ||
import 'package:substrate_bip39/substrate_bip39.dart'; | ||
import 'package:ed25519_edwards/ed25519_edwards.dart' as ed; | ||
import 'package:ss58/ss58.dart'; | ||
import 'package:sr25519/sr25519.dart' as sr25519; | ||
import 'package:merlin/merlin.dart' as merlin; | ||
|
||
part 'src/keyring.dart'; | ||
part 'src/keypair.dart'; | ||
part 'src/pairs.dart'; | ||
part 'src/extensions.dart'; | ||
part 'src/ed25519.dart'; | ||
part 'src/sr25519.dart'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
part of polkadart_keyring; | ||
|
||
class Ed25519KeyPair extends KeyPair { | ||
late ed.PublicKey _publicKey; | ||
late ed.PrivateKey _privateKey; | ||
bool _locked = false; | ||
|
||
Ed25519KeyPair() : super(KeyPairType.ed25519); | ||
|
||
@override | ||
KeyPair fromSeed(Uint8List seed) { | ||
_privateKey = ed.newKeyFromSeed(seed); | ||
_publicKey = ed.public(_privateKey); | ||
return this; | ||
} | ||
|
||
@override | ||
Future<KeyPair> fromMnemonic(String mnemonic, [String? password]) async { | ||
final seed = | ||
await SubstrateBip39.ed25519.seedFromUri(mnemonic, password: password); | ||
return KeyPair.ed25519.fromSeed(Uint8List.fromList(seed)); | ||
} | ||
|
||
@override | ||
Uint8List sign(Uint8List message) { | ||
if (_locked) { | ||
throw Exception('KeyPair is locked. Unlock it before signing.'); | ||
} | ||
return ed.sign(_privateKey, message); | ||
} | ||
|
||
@override | ||
bool verify(Uint8List message, Uint8List signature) { | ||
return ed.verify(_publicKey, message, signature); | ||
} | ||
|
||
@override | ||
String get address { | ||
return Address(prefix: 42, pubkey: bytes).encode(); | ||
} | ||
|
||
@override | ||
Future<void> unlockFromMemonic(String mnemonic, [String? password]) async { | ||
final seed = | ||
await SubstrateBip39.ed25519.seedFromUri(mnemonic, password: password); | ||
_unlock(ed.newKeyFromSeed(Uint8List.fromList(seed))); | ||
} | ||
|
||
@override | ||
void unlockFromSeed(Uint8List seed) { | ||
_unlock(ed.newKeyFromSeed(seed)); | ||
} | ||
|
||
void _unlock(ed.PrivateKey privateKey) { | ||
if (ed.public(privateKey).bytes.toString() != bytes.toString()) { | ||
throw Exception('Public_Key_Mismatch: Invalid seed for given KeyPair.'); | ||
} | ||
_privateKey = privateKey; | ||
_locked = false; | ||
} | ||
|
||
@override | ||
void lock() { | ||
_isLocked = true; | ||
_privateKey = ed.PrivateKey(Uint8List(0)); | ||
} | ||
|
||
@override | ||
Uint8List get bytes => Uint8List.fromList(_publicKey.bytes); | ||
|
||
/// | ||
/// Returns `true` if the `KeyPair` matches with the other object. | ||
@override | ||
bool operator ==(Object other) { | ||
if (other is KeyPair) { | ||
return bytes == other.bytes; | ||
} | ||
return false; | ||
} | ||
|
||
/// | ||
/// Returns the hash code of the `KeyPair`. | ||
@override | ||
int get hashCode => bytes.hashCode; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
part of polkadart_keyring; | ||
|
||
/// Represents a cryptographic key pair type for signing and verifying data. | ||
enum KeyPairType { | ||
/// Ed25519 key pair type. | ||
ed25519, | ||
|
||
/// Sr25519 key pair type. | ||
sr25519, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.