youwallet / wallet

🚀 基于Flutter开发的跨终端数字货币交易所✨
https://youwallet.github.io/
116 stars 55 forks source link

【接口】生成钱包 #41

Open zhaobinglong opened 4 years ago

zhaobinglong commented 4 years ago

生成助记词:

import 'package:bip39/bip39.dart' as bip39;
import 'package:ed25519_hd_key/ed25519_hd_key.dart';
import "package:hex/hex.dart";

计算钱包地址

import 'package:web3dart/credentials.dart';

    final private = EthPrivateKey.fromHex(privateKey);
    final address = await private.extractAddress();
    print("address: $address");
zhaobinglong commented 4 years ago

deadline: 2019-12-20 size:1

zhaobinglong commented 4 years ago

github找到一个库,它使用了助记词才生成钱包:

stackoverflow上的链接: https://stackoverflow.com/questions/55363842/ethereum-hd-wallet-implementation-in-flutter-is-there-any-library-in-flutter

github仓库: https://github.com/allanclempe/ether-wallet-flutter

zhaobinglong commented 4 years ago

APP截图: image

image

image

zhaobinglong commented 4 years ago

使用bip39生成助记词和种子

String  mnemonic = bip39.generateMnemonic();
String randomMnemonic = bip39.generateMnemonic();
String seed = bip39.mnemonicToSeedHex(randomMnemonic);
zhaobinglong commented 4 years ago

如何把种子转换为钱包的私钥呢?这是一个问题

参考:https://github.com/anicdh/bitcoin_flutter

zhaobinglong commented 4 years ago

image

zhaobinglong commented 4 years ago

image

zhaobinglong commented 4 years ago

image

zhaobinglong commented 4 years ago

9854f39ea66919065169c2fc154cecad7b82f9fcf0bd6723cea0921fedbe357d f759af1461151e4c6ba0ba38c0d8749df3bb97ef30723e391c2bebe833fabcf3

zhaobinglong commented 4 years ago

使用dart的HMAC-SHA512算法: image

https://github.com/dart-lang/crypto/blob/ae27765b640f87ebb1a8cf3bc882b5883654fc8a/README.md

zhaobinglong commented 4 years ago

https://github.com/anicdh/bitcoin_flutter/blob/master/test/integration/bip32_test.dart#L30

test('can create a BIP44, bitcoin, account 0, external address', () {
      final root = bip32.BIP32.fromSeed(HEX.decode('dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd'));
      final child1 = root.derivePath("m/44'/0'/0'/0/0");
      // option 2, manually
      final child1b = root.deriveHardened(44)
          .deriveHardened(0)
          .deriveHardened(0)
          .derive(0)
          .derive(0);
      expect(getAddress(child1), '12Tyvr1U8A3ped6zwMEU5M8cx3G38sP5Au');
      expect(getAddress(child1b), '12Tyvr1U8A3ped6zwMEU5M8cx3G38sP5Au');
    });
zhaobinglong commented 4 years ago

生成钱包私钥和公钥

const mnemonic = 'praise you muffin lion enable neck grocery crumble super myself license ghost';
final seed = bip39.mnemonicToSeed(mnemonic);
final root = bip32.BIP32.fromSeed(seed);
final child1 = root.derivePath("m/44'/60'/0'/0/0");
print( bytesToHex(child1.publicKey));
print( bytesToHex(child1.privateKey));