-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.dart
96 lines (69 loc) · 2.63 KB
/
main.dart
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import 'dart:math';
import 'dart:async';
import 'package:convert/convert.dart';
import 'package:pointycastle/pointycastle.dart';
import 'package:base58check/base58check.dart';
import 'package:base58check/base58.dart';
import "package:pointycastle/ecc/curves/secp256k1.dart";
import 'package:pointycastle/digests/ripemd160.dart';
import 'package:pointycastle/src/utils.dart' as p_utils;
List<int> getRandomIntList(int count, int maxRandIntValue) {
var randomGenerator;
try {
randomGenerator = Random.secure();
} catch (e) {
randomGenerator = new Random();
}
List<int> randomIntList = [];
for(var i = 0; i < count; i++) {
int randomNumber = randomGenerator.nextInt( maxRandIntValue ); // [0 ~ 2^8)
randomIntList.add(randomNumber);
}
return randomIntList;
}
const EosRandomKeyBits = 256;
const maxRandUInt8Value = 1 << 8; // [0, maxRandInt8)
class EosPrivateKey {
List<int> _randomKey; // secure random key
EosPrivateKey() {
_randomKey = getRandomIntList(EosRandomKeyBits ~/ 8, maxRandUInt8Value);
}
List<int> get randomKey => _randomKey;
String toWif() {
const version = 0x80;
final payload = new Base58CheckPayload(version, _randomKey);
final base58CheckCodec = new Base58CheckCodec.bitcoin();
return base58CheckCodec.encode(payload);
}
void fromWif(String wif) {
_randomKey = [];
final base58CheckCodec = new Base58CheckCodec.bitcoin();
_randomKey = base58CheckCodec.decode(wif).payload;
}
String toPublicKey() {
var secp256k1 = new ECCurve_secp256k1();
BigInt privateKeyNum = p_utils.decodeBigInt(this.randomKey);
ECPoint ecPoint = secp256k1.G * privateKeyNum;
var encodedBuffer = ecPoint.getEncoded(true);
var ripemd160 = new RIPEMD160Digest();
var checksum = ripemd160.process(encodedBuffer);
checksum = checksum.getRange(0, 4).toList();
var base58Codec = new Base58Codec(Base58CheckCodec.BITCOIN_ALPHABET);
String publicKey = 'EOS' + base58Codec.encode( encodedBuffer + checksum );
return publicKey;
}
}
// check your results with:
// https://github.com/webdigi/
// https://eostea.github.io/eos-generate-key/
void testEosKeys() {
final privateKey = new EosPrivateKey();
print ("random key: " + hex.encode(privateKey.randomKey));
String wif = privateKey.toWif();
print("wif private key: " + wif + " , length: " + wif.length.toString());
String publicKey = privateKey.toPublicKey();
print("public key: " + publicKey + " , length: " + publicKey.length.toString());
}
void main() {
testEosKeys();
}