-
-
Save athlona64/5809fa53ddbd6efbd22c8c718f8d7ee7 to your computer and use it in GitHub Desktop.
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; | |
} | |
} |
Thank you for this
Hi @oviniciusfeitosa, Are you creating a wallet?
@jhaymesdev Yes, I am both creating a new wallet and importing an existing one.
@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?
The same as you
Are you having any challenges?
I could be of help
String getPrivateKey(String mnemonic) is accepting a mnemonic variable but is never used.
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;
}
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;
}
...
Great! Thanks