简体   繁体   English

如何在 WebDart 中获取私钥

[英]How to get private key in WebDart

I'm using the module Web3dart for a mobile flutter application to interact with the ethereum blockchain.我正在将模块Web3dart用于移动 flutter 应用程序以与以太坊区块链进行交互。 However i want to get the private key from a wallet.但是我想从钱包中获取私钥。 But there is only an attribute PrivateKey which returns a uint8Array.但是只有一个属性 PrivateKey 返回一个 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.还有一个 PrivateKeyInt 返回一个 bigInt。

You can use crypto.dart from web3dart package.您可以使用 web3dart 包中的 crypto.dart。 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; 
    }

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());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM