def __init__(self, secret = None): if not secret: secret = os.urandom(32) self.secret = ecdsa.util.string_to_number(secret) self.pubkey = ecdsa.ecdsa.Public_key(ecdsa.ecdsa.generator_secp256k1, ecdsa.ecdsa.generator_secp256k1 * self.secret) self.privkey = ecdsa.ecdsa.Private_key(self.pubkey, secret) pubhex = ('04' + '%064x' % self.pubkey.point.x() + '%064x' % self.pubkey.point.y()).decode('hex') self.pub = base58_check_encode(rhash(pubhex)) if self.pubkey.point.y() % 2: pubhex = ('03' + '%064x' % self.pubkey.point.x()).decode('hex') else: pubhex = ('02' + '%064x' % self.pubkey.point.x()).decode('hex') self.pubc = base58_check_encode(rhash(pubhex)) self.priv = base58_check_encode(self.privkey.secret_multiplier, 0x80)
def instance(self): if not self.wallet_exists(): self.create_new_wallet() self.private_key_hex = self.read_private_key() self.private_key_wif = base58.base58_check_encode(0x80, self.private_key_hex.decode("hex")) self.public_key=bitcoin_asymmetric_encrypt.private_key_to_public_key(self.private_key_wif) self.public_address = bitcoin_asymmetric_encrypt.public_key_to_address(self.public_key)
def download_file(self, document_hash, storing_file_name, encrypted=False): if encrypted: reg_status = self.register_user_status() private_key_hex = str(reg_status['file_encryption_key']) private_key_wif = base58.base58_check_encode(0x80, private_key_hex.decode("hex")) private_key = CBitcoinSecret(private_key_wif) public_key = private_key.pub cookies = self.authenticate() if cookies is not None: download_response = requests.get(self.notary_server.get_upload_url(self.address, document_hash), cookies=cookies, allow_redirects=True, verify=False) if download_response.status_code == 200: # Need to add error handling ultimate_file_name = str(storing_file_name) if encrypted: ultimate_file_name = storing_file_name+".download_encrypted" with open(ultimate_file_name, 'wb') as f: for chunk in download_response.iter_content(chunk_size=1024): if chunk: # filter out keep-alive new chunks f.write(chunk) if encrypted: file_stream_encrypt.decrypt_file(storing_file_name+".download_encrypted", storing_file_name, private_key_wif) return storing_file_name return None
def instance(self): if not self.wallet_exists(): self.create_new_wallet() self.private_key_hex = self.read_private_key() self.private_key_wif = base58.base58_check_encode( 0x80, self.private_key_hex.decode("hex")) self.private_key = CBitcoinSecret(self.private_key_wif)
def __init__(self, secret=None): if not secret: secret = os.urandom(32) self.secret = ecdsa.util.string_to_number(secret) self.pubkey = ecdsa.ecdsa.Public_key( ecdsa.ecdsa.generator_secp256k1, ecdsa.ecdsa.generator_secp256k1 * self.secret) self.privkey = ecdsa.ecdsa.Private_key(self.pubkey, secret) pubhex = ('04' + '%064x' % self.pubkey.point.x() + '%064x' % self.pubkey.point.y()).decode('hex') self.pub = base58_check_encode(rhash(pubhex)) if self.pubkey.point.y() % 2: pubhex = ('03' + '%064x' % self.pubkey.point.x()).decode('hex') else: pubhex = ('02' + '%064x' % self.pubkey.point.x()).decode('hex') self.pubc = base58_check_encode(rhash(pubhex)) self.priv = base58_check_encode(self.privkey.secret_multiplier, 0x80)
def get_private_key(self, format='base58'): bn = ssl.EC_KEY_get0_private_key(self.eckey); bytes = (ssl.BN_num_bits(bn) + 7) / 8 mb = ctypes.create_string_buffer(bytes) n = ssl.BN_bn2bin(bn, mb); if format=='base58': payload = mb.raw if self.compressed: payload = mb.raw + chr(1) return base58_check_encode(payload, 128+self.version) return mb.raw.encode('hex')
def get_private_key(self, format='base58'): bn = ssl.EC_KEY_get0_private_key(self.eckey) bytes = (ssl.BN_num_bits(bn) + 7) / 8 mb = ctypes.create_string_buffer(bytes) n = ssl.BN_bn2bin(bn, mb) if format == 'base58': payload = mb.raw if self.compressed: payload = mb.raw + chr(1) return base58_check_encode(payload, 128 + self.version) return mb.raw.encode('hex')
def instance(self): try: self.kms = boto3.client('kms', region_name='us-east-1') self.dynamodb = resource_factory.get_dynamodb(self.config) except botocore.exceptions.ClientError as e: self.logger.exception("Error creating server wallet %s " % e.message) if not self.wallet_exists(): self.create_new_wallet() try: self.private_key_hex = self.read_private_key() self.private_key_wif = base58.base58_check_encode(0x80, self.private_key_hex.decode("hex")) self.private_key = CBitcoinSecret(self.private_key_wif) except ValueError as e: self.logger.exception("Problem with wallet %s " % e.message)
def upload_file(self, path_to_file, encrypted=False): ''' uploads a file to server Parameters ---------- path_to_file : give a file pointer,i.e. file pointer. Need change code support file full path name. Returns ------- the http status from the server ''' if encrypted: reg_status = self.register_user_status() private_key_hex = str(reg_status['file_encryption_key']) private_key_wif = base58.base58_check_encode(0x80, private_key_hex.decode("hex")) private_key = CBitcoinSecret(private_key_wif) public_key = private_key.pub if type(path_to_file) is str: document_hash = hashfile.hash_file(path_to_file) else: document_hash = hashfile.hash_file_fp(path_to_file) cookies = self.authenticate() if cookies is not None: check_notarized = requests.get(self.notary_server.get_notarization_status_url(self.address, document_hash), cookies=cookies, verify=False) if check_notarized is not None: if check_notarized.status_code == 404: return None elif check_notarized.status_code == 200: try: cookies = requests.utils.dict_from_cookiejar(check_notarized.cookies) if encrypted: file_stream_encrypt.encrypt_file(path_to_file,path_to_file+".encrypted", public_key) files = {'document_content': open(path_to_file+".encrypted", 'rb')} else: files = {'document_content': open(path_to_file, 'rb')} upload_response = requests.put( self.notary_server.get_upload_url(self.address, document_hash), cookies=cookies, files=files, verify=False) return upload_response.status_code except requests.ConnectionError as e: print (e.message) return None
def instance(self): try: self.kms = boto3.client('kms', region_name='us-east-1') self.dynamodb = resource_factory.get_dynamodb(self.config) except botocore.exceptions.ClientError as e: self.logger.exception("Error creating server wallet %s " % e.message) if not self.wallet_exists(): self.create_new_wallet() try: self.private_key_hex = self.read_private_key() self.private_key_wif = base58.base58_check_encode( 0x80, self.private_key_hex.decode("hex")) self.private_key = CBitcoinSecret(self.private_key_wif) except ValueError as e: self.logger.exception("Problem with wallet %s " % e.message)
def get_address(self): pubkey = self.get_public_key(encode='raw') hash160 = rhash(pubkey) addr = base58_check_encode(hash160, self.version) return addr
def verify_public_key(pubkey): hash160 = rhash(pubkey.decode('hex')) addr = base58_check_encode(hash160, version=0) return CryptoAddress.verify_address(addr)
def instance(self): self.private_key_wif = base58.base58_check_encode(0x80, self.private_key_hex.decode("hex")) self.private_key = CBitcoinSecret(self.private_key_wif)
def instance(self): self.private_key_wif = base58.base58_check_encode( 0x80, self.private_key_hex.decode("hex")) self.private_key = CBitcoinSecret(self.private_key_wif)
def build_token(nonce): nonce_hash = hashlib.sha256(nonce).digest() fingerprint_hash = hashlib.sha256(build_fingerprint()).digest() token = hashlib.sha256(nonce_hash + fingerprint_hash).digest() encoded = base58.base58_check_encode(0x80, token.encode("hex")) return encoded
def privateKeyToWif(key_hex): return base58_check_encode(0x80, key_hex.decode('hex'))
def instance(self): if not self.wallet_exists(): self.create_new_wallet() self.private_key_hex = self.read_private_key() self.private_key_wif = base58.base58_check_encode(0x80, self.private_key_hex.decode("hex")) self.private_key = CBitcoinSecret(self.private_key_wif)
def instance(self): self.private_key_wif = base58.base58_check_encode(0x80, self.private_key_hex.decode("hex")) self.private_key = None