Skip to content

Instantly share code, notes, and snippets.

@athlona64
Created December 15, 2019 05:28
Show Gist options
  • Save athlona64/5809fa53ddbd6efbd22c8c718f8d7ee7 to your computer and use it in GitHub Desktop.
Save athlona64/5809fa53ddbd6efbd22c8c718f8d7ee7 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;
}
}
@oviniciusfeitosa
Copy link

Great! Thanks

@jhaym3s
Copy link

jhaym3s commented Mar 7, 2022

Thank you for this

@jhaym3s
Copy link

jhaym3s commented Mar 7, 2022

Hi @oviniciusfeitosa, Are you creating a wallet?

@oviniciusfeitosa
Copy link

@jhaymesdev Yes, I am both creating a new wallet and importing an existing one.

@jhaym3s
Copy link

jhaym3s commented Mar 8, 2022

@jhaymesdev Yes, I am both creating a new wallet and importing an existing one.

Okay, I am building with flutter. What language are you using?

@oviniciusfeitosa
Copy link

The same as you

@jhaym3s
Copy link

jhaym3s commented Mar 15, 2022

Are you having any challenges?
I could be of help

@oliverbytes
Copy link

String getPrivateKey(String mnemonic) is accepting a mnemonic variable but is never used.

@jhaym3s
Copy link

jhaym3s commented Apr 20, 2022

Use this instead @oliverbytes

Future getPrivateKey(String mnemonic) async {
final seed = bip39.mnemonicToSeedHex(mnemonic);
final master = await ED25519_HD_KEY.getMasterKeyFromSeed(hex.decode(seed),
masterSecret: 'Bitcoin seed');
final privateKey = HEX.encode(master.key);
print('private: $privateKey');
return privateKey;
}

@ttamna
Copy link

ttamna commented Sep 28, 2022

Thank you for this!
I used it like this.

class WalletAddress implements WalletAddressService {
  String hdPath = "m/44'/60'/0'/0";

  @override
  String generateMnemonic() {
    // TODO: use random bytes 
    return bip39.generateMnemonic();
  }

  @override
  Future<String> getPrivateKey(String mnemonic) async {
    final isValidMnemonic = bip39.validateMnemonic(mnemonic);
    if(!isValidMnemonic) {
      throw 'Invalid mnemonic';
    }

    final seed = bip39.mnemonicToSeed(mnemonic);
    final root = bip32.BIP32.fromSeed(seed);

    const first = 0;
    final firstChild = root.derivePath("$hdPath/$first");
    final privateKey = HEX.encode(firstChild.privateKey as List<int>);

    return privateKey;
  }
  
  ...
  

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment