def _get_confirmation_code(flagbyte, ownerentropy, factorb, derived_half1, derived_half2, addresshash): k = CKey() k.set_compressed(True) k.generate(secret=factorb) pointb = k.get_pubkey() if len(pointb) != 33: return AssertionError('pointb (' + hexlify(pointb) + ') is ' + str(len(pointb)) + ' bytes. It should be 33 bytes.') if pointb[:1] != '\x02' and pointb[:1] != '\x03': return ValueError('pointb is not correct.') pointbprefix = Bip38.xor_zip( pointb[:1], chr(ord(derived_half2[31:32]) & ord('\x01'))) cipher = AES.new(derived_half2) pointbx1 = cipher.encrypt( Bip38.xor_zip(pointb[1:17], derived_half1[:16])) pointbx2 = cipher.encrypt( Bip38.xor_zip(pointb[17:33], derived_half1[16:32])) encryptedpointb = pointbprefix + pointbx1 + pointbx2 if len(encryptedpointb) != 33: return AssertionError('encryptedpointb is not 33 bytes long.') magic = '\x3b\xf6\xa8\x9a' return str( CBase58Data( magic + flagbyte.tostring() + addresshash + ownerentropy + encryptedpointb, 0x64))
def getnewsubaccount(self): key = CKey() key.generate() private_key = key.get_privkey() public_key = key.get_pubkey() address = pubkey_to_address(public_key) return {"address": address, "public_key": public_key, "private_key": private_key, "balance": 0.0, 'height' : 0, 'received' : []}
def getnewsubaccount(self): key = CKey() key.generate() private_key = key.get_privkey() public_key = key.get_pubkey() address = pubkey_to_address(public_key) print "Address: ", address print "Private key: ", type(private_key), private_key print "Public key: ", type(public_key), public_key return {"address": address, "public_key": public_key, "private_key": private_key, "balance": 0.0, 'height' : 0, 'received' : []}
def getnewsubaccount(self): key = CKey() key.generate() private_key = key.get_privkey() public_key = key.get_pubkey() address = pubkey_to_address(public_key) return { "address": address, "public_key": public_key, "private_key": private_key, "balance": 0.0, 'height': 0, 'received': [] }
def generate_address(): # def get_addr(k,version = 0): k = CKey() version = 0 k.generate() k.set_compressed(True) pubkey = k.get_pubkey() secret = k.get_secret() hash160 = rhash(pubkey) addr = base58_check_encode(hash160, version) # payload = secret # if k.compressed: # payload = secret + chr(1) pkey = base58_check_encode(payload, 128+version) return addr, pkey
def sign(jsonbuf, privkey, role, extra_keys=[]): ''' :type jsonbuf: str. :param privkey: private key in DER form :type privkey: str. :type role: str. :type extra_keys: list. :returns: dict - JSON with signature appended. :raises: BadJSONError ''' jsondoc = json.loads(jsonbuf) signed_keys = jsondoc.get('signed_keys', []) sigdict = {} for key in signed_keys + extra_keys: # FIXME:в ключе допустимы только [a-zA-Z0-9_.\-] # FIXME:можно исключить числа с плавающей точкой value = jsondoc.get(key, None) if not value: raise BadJSONError('Missing attribute: %s' % (repr(key), )) sigdict[key] = value if not len(sigdict): raise BadJSONError('No attributes to sign') instr = json.dumps(sigdict, separators=(',', ':'), sort_keys=True) hash = hashlib.sha256(hashlib.sha256(instr).digest()).digest() ckey = CKey() ckey.set_privkey(privkey) signed = ckey.sign(hash) sigentry = { 'role': role, 'signature_type': 'secp256k1', 'pubkey': binascii.hexlify(ckey.get_pubkey()), 'signature': binascii.hexlify(signed) } if extra_keys: sigentry['extra_signed_keys'] = extra_keys jsondoc.setdefault('signatures', []).append(sigentry) return jsondoc
def sign(jsonbuf, privkey, role, extra_keys=[]): ''' :type jsonbuf: str. :param privkey: private key in DER form :type privkey: str. :type role: str. :type extra_keys: list. :returns: dict - JSON with signature appended. :raises: BadJSONError ''' jsondoc = json.loads(jsonbuf) signed_keys = jsondoc.get('signed_keys', []) sigdict = {} for key in signed_keys + extra_keys: # FIXME:в ключе допустимы только [a-zA-Z0-9_.\-] # FIXME:можно исключить числа с плавающей точкой value = jsondoc.get(key, None) if not value: raise BadJSONError('Missing attribute: %s' % (repr(key),)) sigdict[key] = value if not len(sigdict): raise BadJSONError('No attributes to sign') instr = json.dumps(sigdict, separators=(',',':'), sort_keys=True) hash = hashlib.sha256(hashlib.sha256(instr).digest()).digest() ckey = CKey() ckey.set_privkey(privkey) signed = ckey.sign(hash) sigentry = { 'role': role, 'signature_type': 'secp256k1', 'pubkey': binascii.hexlify(ckey.get_pubkey()), 'signature': binascii.hexlify(signed) } if extra_keys: sigentry['extra_signed_keys'] = extra_keys jsondoc.setdefault('signatures', []).append(sigentry) return jsondoc
def _get_confirmation_code(flagbyte, ownerentropy, factorb, derived_half1, derived_half2, addresshash): k = CKey() k.set_compressed(True) k.generate(secret=factorb) pointb = k.get_pubkey() if len(pointb) != 33: return AssertionError('pointb (' + hexlify(pointb) + ') is ' + str(len(pointb)) + ' bytes. It should be 33 bytes.') if pointb[:1] != '\x02' and pointb[:1] != '\x03': return ValueError('pointb is not correct.') pointbprefix = Bip38.xor_zip(pointb[:1], chr(ord(derived_half2[31:32])&ord('\x01'))) cipher = AES.new(derived_half2) pointbx1 = cipher.encrypt(Bip38.xor_zip(pointb[1:17], derived_half1[:16])) pointbx2 = cipher.encrypt(Bip38.xor_zip(pointb[17:33], derived_half1[16:32])) encryptedpointb = pointbprefix + pointbx1 + pointbx2 if len(encryptedpointb) != 33: return AssertionError('encryptedpointb is not 33 bytes long.') magic = '\x3b\xf6\xa8\x9a' return str(CBase58Data(magic + flagbyte.tostring() + addresshash + ownerentropy + encryptedpointb, 0x64))
def getnewaddress(): # Generate public and private keys key = Key() key.generate() key.set_compressed(True) private_key = key.get_privkey() public_key = key.get_pubkey() private_key_hex = private_key.encode('hex') public_key_hex = public_key.encode('hex') public_key_bytearray = bytearray.fromhex(public_key_hex) # Perform SHA-256 and RIPEMD-160 hashing on public key hash160_address = myhash160(public_key_bytearray) # add version byte: 0x00 for Main Network extended_address = '\x00' + hash160_address # generate double SHA-256 hash of extended address hash_address = myhash(extended_address) # Take the first 4 bytes of the second SHA-256 hash. This is the address checksum checksum = hash_address[:4] # Add the 4 checksum bytes from point 7 at the end of extended RIPEMD-160 hash from point 4. This is the 25-byte binary Bitcoin Address. binary_address = extended_address + checksum # Convert the result from a byte string into a base58 string using Base58Check encoding. address = encode(binary_address) return public_key, private_key, address
def MyHash(s): return hashlib.sha256(hashlib.sha256(s).digest()).digest() def MyHash160(s): h = hashlib.new('ripemd160') h.update(hashlib.sha256(s).digest()) return h.digest() # Generate public and private keys key = Key() # key.set_privkey(0x18E14A7B6A307F426A94F8114701E7C8E774E7F9A47E2C2035DB29A206321725) # key.set_pubkey(bytearray.fromhex("0450863AD64A87AE8A2FE83C1AF1A8403CB53F53E486D8511DAD8A04887E5B23522CD470243453A299FA9E77237716103ABC11A1DF38855ED6F2EE187E9C582BA6")) key.generate() key.set_compressed(True) private_key = key.get_privkey() public_key = key.get_pubkey() secret = key.get_secret() private_key_hex = private_key.encode('hex') public_key_hex = public_key.encode('hex') secret_hex = binascii.hexlify(secret) print "Private key: ", private_key_hex print "Public key: ", public_key_hex print "secret : ", secret_hex public_key = bytearray.fromhex(public_key_hex) # public_key = bytearray.fromhex("0450863AD64A87AE8A2FE83C1AF1A8403CB53F53E486D8511DAD8A04887E5B23522CD470243453A299FA9E77237716103ABC11A1DF38855ED6F2EE187E9C582BA6") # Perform SHA-256 and RIPEMD-160 hashing on public key hash160_address = MyHash160(public_key) # add version byte: 0x00 for Main Network
0100000001eccf7e3034189b851985d871f91384b8ee357cd47c3024736e5676eb2debb3f201 0000001976a914010966776006953d5567439e5e39f86a0d273bee88acffffffff01605af405 000000001976a914097072524438d003d23a2f23edb65aae1bb3e46988ac0000000001000000""" # print x dhash = binascii.unhexlify( "9302bda273a887cb40c13e02a50b4071a31fd3aae3ae04021b0b843dd61ad18e") PRIVATE_KEY = 0x18E14A7B6A307F426A94F8114701E7C8E774E7F9A47E2C2035DB29A206321725 HEX_TRANSACTION = "010000000126c07ece0bce7cda0ccd14d99e205f118cde27e83dd75da7b141fe487b5528fb000000008b48304502202b7e37831273d74c8b5b1956c23e79acd660635a8d1063d413c50b218eb6bc8a022100a10a3a7b5aaa0f07827207daf81f718f51eeac96695cf1ef9f2020f21a0de02f01410452684bce6797a0a50d028e9632be0c2a7e5031b710972c2a3285520fb29fcd4ecfb5fc2bf86a1e7578e4f8a305eeb341d1c6fc0173e5837e2d3c7b178aade078ffffffff02b06c191e010000001976a9143564a74f9ddb4372301c49154605573d7d1a88fe88ac00e1f505000000001976a914010966776006953d5567439e5e39f86a0d273bee88ac00000000" #output to redeem. must exist in HEX_TRANSACTION k = CKey() k.generate(('%064x' % PRIVATE_KEY).decode('hex')) #here we retrieve the public key data generated from the supplied private key pubkey_data = k.get_pubkey() #then we create a signature over the hash of the signature-less transaction signed_data = k.sign(dhash) print binascii.hexlify(signed_data) """ # Add four-byte version field: 01000000 # One-byte varint specifying the number of inputs: 01 # 32-byte hash of the transaction from which we want to redeem an output: eccf7e3034189b851985d871f91384b8ee357cd47c3024736e5676eb2debb3f2 # Four-byte field denoting the output index we want to redeem from the transaction with the above hash (output number 2 = output index 1): 01000000 Now comes the scriptSig. For the purpose of signing the transaction, this is temporarily filled with the scriptPubKey of the output we want to redeem. First we write a one-byte varint which denotes the length of the scriptSig (0x19 = 25 bytes): 19 Then we write the actual scriptSig (which is the scriptPubKey of the output we want to redeem): 76a914010966776006953d5567439e5e39f86a0d273bee88ac Then we write a four-byte field denoting the sequence. This is currently always set to 0xffffffff: ffffffff # Next comes a one-byte varint containing the number of outputs in our new transaction. We will set this to 1 in this example: 01 # We then write an 8-byte field (64 bit integer) containing the amount we want to redeem from the specified output. I will set this to the total amount available # in the output minus a fee of 0.001 BTC (0.999 BTC, or 99900000 Satoshis): 605af40500000000 # Then we start writing our transaction's output. We start with a one-byte varint denoting the length of the output script (0x19 or 25 bytes): 19
#!/usr/bin/python # -*- coding: utf-8 -*- import sys import paperwallet import paperwallet.util from bitcoin.key import CKey k = CKey() k.generate() k.set_compressed(False) public_key = paperwallet.util.public_key_to_bc_address(k.get_pubkey()) private_key = \ paperwallet.util.private_key_to_wallet_import_format(k.get_privkey()) pw = paperwallet.PaperWallet(sys.argv[1], sys.argv[2], public_key, private_key) pw.save('bar.png')
def _compute_passpoint(passfactor): k = CKey() k.set_compressed(True) k.generate(secret=passfactor) return k.get_pubkey()
def MyHash160(s): h = hashlib.new('ripemd160') h.update(hashlib.sha256(s).digest()) return h.digest() # Generate public and private keys key = Key() # key.set_privkey(0x18E14A7B6A307F426A94F8114701E7C8E774E7F9A47E2C2035DB29A206321725) # key.set_pubkey(bytearray.fromhex("0450863AD64A87AE8A2FE83C1AF1A8403CB53F53E486D8511DAD8A04887E5B23522CD470243453A299FA9E77237716103ABC11A1DF38855ED6F2EE187E9C582BA6")) key.generate() key.set_compressed(True) private_key = key.get_privkey() public_key = key.get_pubkey() secret = key.get_secret() private_key_hex = private_key.encode('hex') public_key_hex = public_key.encode('hex') secret_hex = binascii.hexlify(secret) print "Private key: ", private_key_hex print "Public key: ", public_key_hex print "secret : ", secret_hex public_key = bytearray.fromhex(public_key_hex) # public_key = bytearray.fromhex("0450863AD64A87AE8A2FE83C1AF1A8403CB53F53E486D8511DAD8A04887E5B23522CD470243453A299FA9E77237716103ABC11A1DF38855ED6F2EE187E9C582BA6") # Perform SHA-256 and RIPEMD-160 hashing on public key hash160_address = MyHash160(public_key) # add version byte: 0x00 for Main Network
x = """ 0100000001eccf7e3034189b851985d871f91384b8ee357cd47c3024736e5676eb2debb3f201 0000001976a914010966776006953d5567439e5e39f86a0d273bee88acffffffff01605af405 000000001976a914097072524438d003d23a2f23edb65aae1bb3e46988ac0000000001000000""" # print x dhash = binascii.unhexlify("9302bda273a887cb40c13e02a50b4071a31fd3aae3ae04021b0b843dd61ad18e") PRIVATE_KEY=0x18E14A7B6A307F426A94F8114701E7C8E774E7F9A47E2C2035DB29A206321725 HEX_TRANSACTION="010000000126c07ece0bce7cda0ccd14d99e205f118cde27e83dd75da7b141fe487b5528fb000000008b48304502202b7e37831273d74c8b5b1956c23e79acd660635a8d1063d413c50b218eb6bc8a022100a10a3a7b5aaa0f07827207daf81f718f51eeac96695cf1ef9f2020f21a0de02f01410452684bce6797a0a50d028e9632be0c2a7e5031b710972c2a3285520fb29fcd4ecfb5fc2bf86a1e7578e4f8a305eeb341d1c6fc0173e5837e2d3c7b178aade078ffffffff02b06c191e010000001976a9143564a74f9ddb4372301c49154605573d7d1a88fe88ac00e1f505000000001976a914010966776006953d5567439e5e39f86a0d273bee88ac00000000" #output to redeem. must exist in HEX_TRANSACTION k = CKey() k.generate(('%064x' % PRIVATE_KEY).decode('hex')) #here we retrieve the public key data generated from the supplied private key pubkey_data = k.get_pubkey() #then we create a signature over the hash of the signature-less transaction signed_data = k.sign(dhash) print binascii.hexlify(signed_data) """ # Add four-byte version field: 01000000 # One-byte varint specifying the number of inputs: 01 # 32-byte hash of the transaction from which we want to redeem an output: eccf7e3034189b851985d871f91384b8ee357cd47c3024736e5676eb2debb3f2 # Four-byte field denoting the output index we want to redeem from the transaction with the above hash (output number 2 = output index 1): 01000000 Now comes the scriptSig. For the purpose of signing the transaction, this is temporarily filled with the scriptPubKey of the output we want to redeem. First we write a one-byte varint which denotes the length of the scriptSig (0x19 = 25 bytes): 19 Then we write the actual scriptSig (which is the scriptPubKey of the output we want to redeem): 76a914010966776006953d5567439e5e39f86a0d273bee88ac Then we write a four-byte field denoting the sequence. This is currently always set to 0xffffffff: ffffffff # Next comes a one-byte varint containing the number of outputs in our new transaction. We will set this to 1 in this example: 01 # We then write an 8-byte field (64 bit integer) containing the amount we want to redeem from the specified output. I will set this to the total amount available # in the output minus a fee of 0.001 BTC (0.999 BTC, or 99900000 Satoshis): 605af40500000000