bitcoin_ecdsa.py
- Openssl Ecdsa Example
- Bitcoin Ecdsa Key Openssl Generate Address Free
- Generate Ecdsa Ssh Key
- Bitcoin Ecdsa Key Openssl Generate Address List
- Bitcoin Ecdsa Key Openssl Generate Address Generator
- Bitcoin Ecdsa Key Openssl Generate Address Change
- Openssl Generate Ec Key
# |
# key.py - OpenSSL wrapper |
# Source: git://github.com/joric/brutus.git |
# which was forked from git://github.com/samrushing/caesure.git |
# |
importctypes |
importctypes.util |
ssl=ctypes.cdll.LoadLibrary (ctypes.util.find_library ('ssl') or'libeay32') |
# this specifies the curve used with ECDSA. |
NID_secp256k1=714# from openssl/obj_mac.h |
# Thx to Sam Devlin for the ctypes magic 64-bit fix. |
defcheck_result (val, func, args): |
ifval0: |
raiseValueError |
else: |
returnctypes.c_void_p (val) |
ssl.EC_KEY_new_by_curve_name.restype=ctypes.c_void_p |
ssl.EC_KEY_new_by_curve_name.errcheck=check_result |
classCKey: |
def__init__(self): |
self.POINT_CONVERSION_COMPRESSED=2 |
self.POINT_CONVERSION_UNCOMPRESSED=4 |
self.k=ssl.EC_KEY_new_by_curve_name(NID_secp256k1) |
def__del__(self): |
ifssl: |
ssl.EC_KEY_free(self.k) |
self.k=None |
defgenerate(self, secret=None): |
ifsecret: |
self.prikey=secret |
priv_key=ssl.BN_bin2bn(secret, 32, ssl.BN_new()) |
group=ssl.EC_KEY_get0_group(self.k) |
pub_key=ssl.EC_POINT_new(group) |
ctx=ssl.BN_CTX_new() |
ifnotssl.EC_POINT_mul(group, pub_key, priv_key, None, None, ctx): |
raiseValueError('Could not derive public key from the supplied secret.') |
ssl.EC_POINT_mul(group, pub_key, priv_key, None, None, ctx) |
ssl.EC_KEY_set_private_key(self.k, priv_key) |
ssl.EC_KEY_set_public_key(self.k, pub_key) |
ssl.EC_POINT_free(pub_key) |
ssl.BN_CTX_free(ctx) |
returnself.k |
else: |
returnssl.EC_KEY_generate_key(self.k) |
defset_privkey(self, key): |
self.mb=ctypes.create_string_buffer(key) |
ssl.d2i_ECPrivateKey(ctypes.byref(self.k), ctypes.byref(ctypes.pointer(self.mb)), len(key)) |
defset_pubkey(self, key): |
self.mb=ctypes.create_string_buffer(key) |
ssl.o2i_ECPublicKey(ctypes.byref(self.k), ctypes.byref(ctypes.pointer(self.mb)), len(key)) |
defget_privkey(self): |
size=ssl.i2d_ECPrivateKey(self.k, 0) |
mb_pri=ctypes.create_string_buffer(size) |
ssl.i2d_ECPrivateKey(self.k, ctypes.byref(ctypes.pointer(mb_pri))) |
returnmb_pri.raw |
defget_pubkey(self): |
size=ssl.i2o_ECPublicKey(self.k, 0) |
mb=ctypes.create_string_buffer(size) |
ssl.i2o_ECPublicKey(self.k, ctypes.byref(ctypes.pointer(mb))) |
returnmb.raw |
defsign(self, hash): |
sig_size0=ctypes.c_uint32() |
sig_size0.value=ssl.ECDSA_size(self.k) |
mb_sig=ctypes.create_string_buffer(sig_size0.value) |
result=ssl.ECDSA_sign(0, hash, len(hash), mb_sig, ctypes.byref(sig_size0), self.k) |
assert1result |
returnmb_sig.raw[:sig_size0.value] |
defverify(self, hash, sig): |
returnssl.ECDSA_verify(0, hash, len(hash), sig, len(sig), self.k) 1 |
defset_compressed(self, compressed): |
ifcompressed: |
form=self.POINT_CONVERSION_COMPRESSED |
else: |
form=self.POINT_CONVERSION_UNCOMPRESSED |
ssl.EC_KEY_set_conv_form(self.k, form) |
defverifyWorking(): |
frompybitcointoolsimportbitcoin |
importtime, hashlib |
priv='5JsunC55XGVqFQj5kPGK4MWgTL26jKbnPhjnmchSNPo75XXCwtk' |
address='1N2XWu5soeppX2qUjvrf81rpdbShKJrjTr' |
sign=bitcoin.ecdsa_sign('hello', priv) |
s=time.time() |
foriinrange(1): |
pub=bitcoin.ecdsa_recover('hello', sign) |
sign_address=bitcoin.pubtoaddr(pub) |
print'pybitcointools verify', time.time()-s, sign |
assertsign_addressaddress |
k=CKey() |
k.generate(priv) |
k.set_compressed(False) |
sign=k.sign('hello') |
pub=k.get_pubkey() |
s=time.time() |
foriinrange(10): |
k=CKey() |
k.set_pubkey(pub) |
verify=k.verify('hello', sign) |
assertverify |
print'openssl verify', time.time()-s, sign.encode('base64'), verify |
if__name__'__main__': |
verifyWorking() |
# ethalone keys |
'' ec_secret = ' + |
'a0dc65ffca799873cbea0ac274015b9526505daaaed385155425f7337704883e' |
ec_private = '308201130201010420' + |
'a0dc65ffca799873cbea0ac274015b9526505daaaed385155425f7337704883e' + |
'a081a53081a2020101302c06072a8648ce3d0101022100' + |
'fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f' + |
'300604010004010704410479be667ef9dcbbac55a06295ce870b07029bfcdb2d' + |
'ce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a6' + |
'8554199c47d08ffb10d4b8022100' + |
'fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141' + |
'020101a14403420004' + |
'0791dc70b75aa995213244ad3f4886d74d61ccd3ef658243fcad14c9ccee2b0a' + |
'a762fbc6ac0921b8f17025bb8458b92794ae87a133894d70d7995fc0b6b5ab90' |
k = CKey() |
k.generate (ec_secret.decode('hex')) |
k.set_compressed(True) |
print('Priv', k.get_privkey().encode('hex')) |
print('Pub', k.get_pubkey().encode('hex')) |
# not sure this is needed any more: print k.get_secret().encode('hex') |
hash = 'Hello, world!' |
print(k.verify(hash, k.sign(hash)))'' |
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment
Elliptic Curve Digital Signature Algorithm or ECDSA is a cryptographic algorithm used by Bitcoin to ensure that funds can only be spent by their rightful owners. A few concepts related to ECDSA: private key: A secret number, known only to the person that generated it.A private key is essentially a randomly generated number. Jun 08, 2014 In just 44 lines of code, with no special functions or imports, we produce the elliptic curve public key for use in Bitcoin. Better still, we walk you through it line by line, constant by constant.
Latest version Released:
Bitcoin and Other cryptocurrency Library
Project description
Python Bitcoin Library
Bitcoin, Litecoin and Dash Crypto Currency Library for Python.
Includes a fully functional wallet, with multi signature, multi currency and multiple accounts.You this library at a high level and create and manage wallets for the command line or at a low leveland create your own custom made transactions, keys or wallets.
The BitcoinLib connects to various service providers automatically to update wallets, transactions andblockchain information. It does currently not parse the blockchain itself.
Documentation
Read the full documentation at: http://bitcoinlib.readthedocs.io/
Disclaimer
This library is still in development, please use at your own risk and test sufficiently before using it in aproduction environment.
Some Examples
Wallet
The bitcoin library contains a wallet implementation using SQLAlchemy and SQLite3 to import, create and managekeys in a Hierarchical Deterministic way.
Example: Create wallet and generate new address (key) to receive bitcoins
Now send a small transaction to your wallet and use the scan() method to update transactions and UTXO’s
When your wallet received a payment and has unspent transaction outputs, you can send bitcoins easily.If successful a transaction ID is returned
Wallet from passphrase with accounts and multiple currencies
The following code creates a wallet with two bitcoin and one litecoin account from a Mnemonic passphrase.The complete wallet can be recovered from the passphrase which is the masterkey.
Multi Signature Wallets
Create a Multisig wallet with 2 cosigner which both need to sign a transaction.
Create a transaction in the first wallet
And then import the transaction in the second wallet, sign it and push it to the network
Segregated Witness Wallet
Easily create and manage segwit wallets. Both native segwit with base32/bech32 addresses and P2SH nested segwitwallets with traditional addresses are available.
Create a native single key P2WPKH wallet:
Or create a P2SH nested single key P2SH_P2WPKH wallet:
Command Line Tool
With the command line tool you can create and manage wallet without any Python programming.
To create a new Bitcoin wallet
You can use clw to create simple or multisig wallets for various networks, manage public and private keysand managing transactions.
For the full command line wallet documentation please read
Mnemonic key generation
Allows you to use easy to remember passphrases consisting of a number of words to store private keys (BIP0039).You can password protect this passphrase (BIP0038), and use the HD Wallet structure to generate a almost infinitenumber of new private keys and bitcoin addresses (BIP0043 and BIP0044).
Example: Generate a list of words passphrase and derive a private key seed
Service providers
Communicates with pools of bitcoin service providers to retreive transaction, address, blockchain information.Can be used to push a transaction to the network, determine optimal service fee for a transaction or to update yourwallet’s balance.
When working with wallets connections to service providers are automatically managed so you don’t have to worryabout them. You can however easily use the Service object directly.
Example: Get estimated transaction fee in sathosis per Kb for confirmation within 5 blocks
Openssl Ecdsa Example
Other Databases
Bitcoinlib uses the SQLite database by default but other databases are supported as well.See http://bitcoinlib.readthedocs.io/en/latest/_static/manuals.databases.html for instructions on how to useMySQL or PostgreSQL.
More examples
For more examples see https://github.com/1200wd/bitcoinlib/tree/master/examples
Implements the following Bitcoin Improvement Proposals
- Hierarchical Deterministic Wallets (BIP0032)
- Passphrase-protected private key (BIP0038)
- Mnemonic code for generating deterministic keys (BIP0039)
- Purpose Field for Deterministic Wallets (BIP0043)
- Multi-Account Hierarchy for Deterministic Wallets (BIP0044)
- Structure for Deterministic P2SH Multisignature Wallets (BIP0045)
- Bech32/base32 address format for native v0-16 witness outputs (BIP0173)
- Native and P2SH nested Segregated Witness transactions (BIP0141 and BIP0143)
Installing and updating
Pre-requirements Linux
sudo apt install build-essentialpython-devpython3-devlibgmp3-dev
To install OpenSSL development package on Debian, Ubuntu or their derivatives
sudo apt install libssl-dev
To install OpenSSL development package on Fedora, CentOS or RHEL
sudo yum install gcc openssl-devel
Pre-requirements Windows
This library requires a Microsoft Visual C++ Compiler. Seehttp://bitcoinlib.readthedocs.io/en/latest/_static/manuals.install.html
The fastecdsa library is not enabled at this moment on windows, the slower ecdsa library is installed.
Install with pip
pip install bitcoinlib
These packages will be installed* fastecdsa (or ecdsa on Windows)* pyaes* scrypt* sqlalchemy* requests* enum34 (for older Python installations)* pathlib2 (for Python 2)* six
Install development environment
Required packages:
sudo apt install -y postgresql postgresql-contribmysql-serverlibpq-devlibmysqlclient-dev Ssh ubiquiti unifi.
Create a virtual environment for instance on linux with virtualenv:
Is primarily for news and discussion. For example, submissions like 'Buying 100 BTC' or 'Selling my computer for bitcoins' do not belong here. Bitcoin generate private key from qt 5.
Then clone the repository and install dependencies:
Troubleshooting
When you experience issues with the scrypt package when installing you can try to solve this by removing and reinstallscrypt:
Please make sure you also have the Python development and SSL development packages installed, see ‘Other requirements’above.
You can also use pyscrypt instead of scrypt. Pyscrypt is a pure Python scrypt password-based key derivation library.It works but it is slow when using BIP38 password protected keys.
If you run into issues to not hesitate to contact us or file an issue at https://github.com/1200wd/bitcoinlib/issues
Update library
Update to the latest version of the library with
To upgrade make sure everything is backuped and run updatedb.py from the installation directory.
For more information on installing, updating and maintenance seehttps://bitcoinlib.readthedocs.io/en/latest/_static/manuals.install.html#installation
Future / Roadmap
- Create Script class and support advanced scripts
- Fully support timelocks
- Support for Trezor wallet
- Improve speed and security
- Integrate in ERP and shopping solutions such as Odoo, Magento, Shopware
- Support for lightning network
Release historyRelease notifications
0.4.14
0.4.13
0.4.12
0.4.11
0.4.10
0.4.9
Bitcoin Ecdsa Key Openssl Generate Address Free
0.4.8
Generate Ecdsa Ssh Key
0.4.7
0.4.6
0.4.5
0.4.4
0.4.3
0.4.2
0.4.1
0.4.0
0.3.37
0.3.36
0.3.36a0 pre-release
0.3.35.post1
0.3.34
0.3.34a2 pre-release
0.3.34a1 pre-release
0.3.33a4 pre-release
Bitcoin Ecdsa Key Openssl Generate Address List
0.3.33a3 pre-release
0.3.33a2 pre-release
0.3.33a1 pre-release
0.3.33a0 pre-release
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Filename, size | File type | Python version | Upload date | Hashes |
---|---|---|---|---|
Filename, size bitcoinlib-0.4.14.tar.gz (418.1 kB) | File type Source | Python version None | Upload date | Hashes |
Bitcoin Ecdsa Key Openssl Generate Address Generator
CloseBitcoin Ecdsa Key Openssl Generate Address Change
Hashes for bitcoinlib-0.4.14.tar.gz
Openssl Generate Ec Key
Algorithm | Hash digest |
---|---|
SHA256 | 2bd336c7436b8d1875abcfce0a700e27b06f48c9b50b622adc8f97d7c65a5614 |
MD5 | 9744035220173e6504ddd75a3de50a77 |
BLAKE2-256 | d55d82ee2839784b79a8bbb9448298885ac36d2c865eae135ebd78ae70ca228f |