如何使用私钥创建比特币钱包?

How Create Bitcoin wallet from a private key works?

我想了解如何创建一对比特币私钥和 public 密钥并将它们导入电子钱包

1 次尝试:https://github.com/Destiner/blocksmith

例如:

 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

如何导入电子钱包?


2 次尝试: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

如何导入电子钱包?

例如,我看不出我如何无法键入我的公钥以将其导入电子钱包? 你能帮助理解我的错误或者我混淆了什么吗?

谢谢

您在 Electrum 中有两个选择:

  1. 导入HD钱包(需要种子,或者助记词)

  2. 导入单个地址(需要该地址的密钥对)

导入HD钱包

使用第二个示例中的 cryptotools 库:

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

然后在 Electrum 中(来自文件 > New/Restore):

并完成。

使用密钥对将单个地址导入 Electrum

要导入地址,Electrum 希望用户在 Wallet Import Format 中提供一个私钥,但还要以地址类型为前缀(脚本类型:“p2pkh”、“p2wpkh”、...)你想康复。

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

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

然后

KwcQEum4qYHcfFyWYKsQoRccgnPhhAx1n4b6LfzZVcNA5Eqt8AXP 粘贴到 Electrum 导入对话框中,前面没有脚本类型,将默认为 'P2PKH',并将导入旧地址(以 1 开头) .

如果您从头开始生成钱包,您可以自由选择您喜欢的任何脚本类型,'P2WPKH' 是最新使用的类型(bc1 个地址),但是如果您实际上是导入现有钱包,您需要确保此前缀与其中有资金的脚本类型相对应。