Skip to content

Instantly share code, notes, and snippets.

@oviniciusfeitosa
Forked from athlona64/OKAddress.dart
Created October 13, 2022 04:07
Show Gist options
  • Save oviniciusfeitosa/03480223dc4519f21582c4648b2194f8 to your computer and use it in GitHub Desktop.
Save oviniciusfeitosa/03480223dc4519f21582c4648b2194f8 to your computer and use it in GitHub Desktop.
Flutter + Ethereum BIP39, BIP32
import 'package:bip39/bip39.dart' as bip39;
import "package:hex/hex.dart";
import 'package:web3dart/credentials.dart';
import 'package:bip32/bip32.dart' as bip32;
abstract class OKAddressETH {
String generateMnemonic();
String getPrivateKey(String mnemonic);
Future<EthereumAddress> getPublicAddress(String privateKey);
}
class OKAddress implements OKAddressETH {
@override
String generateMnemonic() {
return bip39.generateMnemonic();
}
@override
String getPrivateKey(String mnemonic) {
final privateKey = HEX.encode(master.key);
final root = bip32.BIP32.fromSeed(HEX.decode(seed));
final child1 = root.derivePath("m/44'/60'/0'/0/0");
final privateKey = HEX.encode(child1.privateKey);
return privateKey;
}
@override
Future<EthereumAddress> getPublicAddress(String privateKey) async {
final private = EthPrivateKey.fromHex(privateKey);
final address = await private.extractAddress();
return address;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment