How to get private key in WebDart

2k views Asked by At

I'm using the module Web3dart for a mobile flutter application to interact with the ethereum blockchain. However i want to get the private key from a wallet. But there is only an attribute PrivateKey which returns a uint8Array.

Does someone know how I can get it as a hex so I can use it to import it into other wallets?

There is also a PrivateKeyInt which returns a bigInt.

2

There are 2 answers

0
tjh543 On BEST ANSWER

You can use crypto.dart from web3dart package. Here is the sample code:

    import 'package:web3dart/crypto.dart';

    String revealPrivateKey () {
      var rng = Random.secure();
      EthePrivateKey priKey = EthPrivateKey.createRandom(rng);
      String s = bytesToHex(priKey.privateKey);
      return s; 
    }

1
Yauheni Prakapenka On
pubcspec.yaml
dart_bip32_bip44: ^0.2.0 # getting a private and a public keys for Ethereum
web3dart: ^2.4.1 # getting a balance and a public address for Ethereum
http: ^0.13.5
bip39: ^1.0.6 # generate a mnemomic
flutter_secure_storage: ^6.0.0

import 'package:bip39/bip39.dart';
import 'package:dart_bip32_bip44/dart_bip32_bip44.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:http/http.dart';
import 'package:web3dart/web3dart.dart';    

  // Somewhere in the code
  final String mnemonic = generateMnemonic();
  SecureMnemonicProvider().saveMnemonic(mnemonic);


class EthereumCryptoProvider {
  final Web3Client _web3client;
  final SecureMnemonicProvider _mnemonicProvider;
  static const String _pathForPublicKey = "m/44'/60'/0'/0";
  static const String _pathForPrivateKey = "m/44'/60'/0'/0/0";

  const EthereumCryptoProvider({
    required Web3Client web3client,
    required SecureMnemonicProvider mnemonicProvider,
  })  : _web3client = web3client,
        _mnemonicProvider = mnemonicProvider;

  Future<double> getBalance() async {
    final publicAddress = await getPublicAddress();
    final EthereumAddress ethereumAddress = EthereumAddress.fromHex(publicAddress);
    final EtherAmount etherAmount = await _web3client.getBalance(ethereumAddress);
    return etherAmount.getValueInUnit(EtherUnit.ether);
  }

  Future<String> getPublicAddress() async {
    final String privateKey = await getPrivateKey();
    final EthPrivateKey ethPrivateKey = EthPrivateKey.fromHex(privateKey);
    final EthereumAddress ethereumAddress = await ethPrivateKey.extractAddress();
    return ethereumAddress.hex;
  }

  Future<String> getPrivateKey() async {
    final String mnemonic = await _mnemonicProvider.getMnemonic();
    final Chain chain = _getChainByMnemonic(mnemonic);
    final ExtendedKey extendedKey = chain.forPath(_pathForPrivateKey);
    return extendedKey.privateKeyHex();
  }

  /// Returns BIP32 Extended Public Key
  Future<String> getPublicKey() async {
    final String mnemonic = await _mnemonicProvider.getMnemonic();
    final Chain chain = _getChainByMnemonic(mnemonic);
    final ExtendedKey extendedKey = chain.forPath(_pathForPublicKey);
    return extendedKey.publicKey().toString();
  }

  /// Returns BIP32 Root Key
  Chain _getChainByMnemonic(String mnemonic) {
    final String seed = mnemonicToSeedHex(mnemonic); // Returns BIP39 Seed
    return Chain.seed(seed);
  }
}

class SecureMnemonicProvider {
  static const FlutterSecureStorage _storage = FlutterSecureStorage();
  static const String _seedPhraseKey = 'seed_phrase';

  Future<String> getMnemonic() async {
    return await _storage.read(key: _seedPhraseKey) ?? '';
  }

  Future<void> saveMnemonic(String mnemonic) async {
    await _storage.write(key: _seedPhraseKey, value: mnemonic);
  }
}

Web3Client getWeb3Client() {
  const String infuraUrl = 'https://mainnet.infura.io/v3/<project_id>';
  return Web3Client(infuraUrl, Client());
}