如何在WebDart中获取私钥



我正在将模块Web3dart用于移动flutter应用程序,以与以太坊区块链进行交互。不过我想从钱包里取私钥。但是只有一个属性PrivateKey返回uint8Array。

有人知道我如何将其作为十六进制,以便我可以使用它将其导入其他钱包吗?

还有一个PrivateKeyInt,它返回一个bigInt。

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

您可以使用web3dart包中的crypto.dart。这是示例代码:

import 'package:web3dart/crypto.dart';
String revealPrivateKey () {
var rng = Random.secure();
EthePrivateKey priKey = EthPrivateKey.createRandom(rng);
String s = bytesToHex(priKey.privateKey);
return s; 
}

最新更新