Search code examples
flutterdartethereumweb3dart

How to get private key in WebDart


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.


Solution

  • 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; 
        }