简体   繁体   中英

How Create Bitcoin wallet from a private key works?

I'm trying to understand how i can create a pair of bitcoin private key and public key and import them in electrum wallet

1 try with: https://github.com/Destiner/blocksmith

for example:

 import blocksmith
 
 kg = blocksmith.KeyGenerator()

 kg.seed_input("(/sgroxa)n;[&1ox7fioqg4*v8")
 #seed is the private key ?

 key = kg.generate_key()
 #returned key : 7e659f56d6f20a64258b65445c825e62edf6b89219d4b3eb3ebec6ecb8f62859
 #this key is a formated "(/sgroxa)n;[&1ox7fioqg4*v8" version  ?

 address = blocksmith.BitcoinWallet.generate_address(key)
 #returned address : 112qrCJdHgudHmsKKKwvj3AM3aN2eN9wpP

how to import this in electrum wallet?


2 try: https://github.com/mcdallas/cryptotools

#python38 setup.py install

import os
os.environ['CRYPTOTOOLS_NETWORK'] = 'main'

from cryptotools.BTC import PrivateKey, Address

#private = PrivateKey.random()

mystring='supersecret'.encode('utf-8')
mystring2 = mystring.hex()
private = PrivateKey.from_hex(mystring2)
#private = PrivateKey.random()
print("PRIVATE NOR: "+str(mystring))
print("PRIVATE HEX: "+mystring2)
#private = PrivateKey.from_wif('yourwifhere')

#and then to get the hex representation use private.hex() of private.wif()


public = private.to_public()
print("PUBLIC NOR: "+str(public))
print("PUBLIC HEX: "+public.hex())
#public.hex(compressed=True)

addrp2pkh = public.to_address('P2PKH')
print("ADDR addrp2pkh: "+addrp2pkh)
#'16B2Ghyu5C1CofKfXF2Zi9JycqaAyaE8cd'
addrp2wpkh = public.to_address('P2WPKH')
print("ADDR addrp2wpkh: "+addrp2wpkh)

return

PRIVATE NOR: b'supersecret'
PRIVATE HEX: 7375706572736563726574
PUBLIC NOR: PublicKey(8168986507460029097250523306678031295706525425592031410379320603875628502471, 68217540890844357698499484021605168903182743958311172870030299256854130530932)
PUBLIC HEX: 04120f7b85fbc8d10d7564fe6dcb99291661014e87d2579068448f8d0b54bf4dc796d1c2869226dfb3609808158d1c4cec9a3a1ebee12994a2779e1d2dac851a74
ADDR addrp2pkh: 15nTqJEGWZg9DTjj7KSjwwKf3xHhpSiQyQ
ADDR addrp2wpkh: bc1q3pv43a0tujsy9kp526su2fz93at70demcj4phw

how to import this in electrum wallet?

i cant see how i cant key my pub key for import it into electrum wallet for example? Cant you help to understand my mistake or if i confuse something?

Thanks

You have two choices in Electrum:

  1. Importing a HD wallet (requires a seed, or mnemonic)

  2. Importing a single address (requires the keypair for that address)

Importing a HD wallet

Using the cryptotools library from your second example:

from cryptotools.BTC import Xprv
private = Xprv.from_seed("adfadaaaaaaaaaaaaaaaaaaaaaaaaafa")
# private = Xprv.from_mnemonic(...)
print(private.encode()) 
# 'xprv9s21ZrQH143K3RYhJCmDhwpJ4cpXxj7JfUSChYD9rwyZqkTtKb5Y1sc3SzcwgFZ6EiMC38EdQJUKSmc2oni98m3XA8dpnXaMRd8GEQxW6KA'

Then in Electrum (from File > New/Restore):

在 Electrum 中导入高清钱包

在此处输入图像描述

在此处输入图像描述

and finish.

Importing a single addresses into Electrum using a key pair

在 Electrum 中导入比特币地址对话框

To import an address, Electrum expects users to provide a private key in the Wallet Import Format but also prefixed with the type of address (script type: "p2pkh", "p2wpkh", ...) that you wish to recover.

from cryptotools.BTC import generate_keypair
private, public = generate_keypair()

wif = private.wif(compressed=True)
print(wif) 
# 'KwcQEum4qYHcfFyWYKsQoRccgnPhhAx1n4b6LfzZVcNA5Eqt8AXP'

Then

在 Electrum 中导入单个地址

在此处输入图像描述

Pasting KwcQEum4qYHcfFyWYKsQoRccgnPhhAx1n4b6LfzZVcNA5Eqt8AXP into the Electrum import dialog without a script type in front of it, will default to 'P2PKH', and will import a legacy address (starts with a 1 ).

If you are generating a wallet from scratch, you are free to pick any script type you like, 'P2WPKH' being the newest type in use ( bc1 addresses), however if you were actually importing an existing wallet, you would need to make sure this prefix corresponds with the script type that has funds in it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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