# Path: ledgerblue/ecWrapper.py
# class PrivateKey(object):
# 
# 	def __init__(self, privkey=None, raw=True):
# 		if USE_SECP:
# 			self.obj = secp256k1.PrivateKey(privkey, raw)
# 			self.pubkey = self.obj.pubkey
# 		else:
# 			if not raw:
# 				raise Exception("Non raw init unsupported")
# 			if privkey == None:
# 				privkey = ecpy.ecrand.rnd(CURVE_SECP256K1.order)
# 			else:
# 				privkey = int.from_bytes(privkey,'big')
# 			self.obj = ECPrivateKey(privkey, CURVE_SECP256K1)
# 			pubkey = self.obj.get_public_key().W
# 			out = b"\x04"
# 			out += pubkey.x.to_bytes(32, 'big')
# 			out += pubkey.y.to_bytes(32, 'big')
# 			self.pubkey = PublicKey(out, raw=True)
# 
# 	def serialize(self):
# 		if USE_SECP:
# 			return self.obj.serialize()
# 		else:
# 			return "%.64x"%self.obj.d
# 
# 	def ecdsa_serialize(self, raw_sig):
# 		if USE_SECP:
# 			return self.obj.ecdsa_serialize(raw_sig)
# 		else:
# 			return raw_sig
# 
# 	def ecdsa_sign(self, msg, raw=False, digest=hashlib.sha256, rfc6979=False):
# 		if USE_SECP:
# 			return self.obj.ecdsa_sign(msg, raw, digest)
# 		else:
# 			if not raw:
# 				h = digest()
# 				h.update(msg)
# 				msg = h.digest()
# 			if rfc6979:
# 				signature = SIGNER.sign_rfc6979(msg, self.obj, digest, True)
# 			else:
# 				signature = SIGNER.sign(msg, self.obj, True)
# 			return signature


from Cryptodome.Cipher import AES
from .ecWrapper import PrivateKey
from ecpy.curves import Curve
import sys
import struct
import hashlib
import binascii
import os
		s += struct.pack(">H", l)
	else:
		raise Exception("Unimplemented TLV encoding")
	s += v
	return s

def str2bool(v):
	if v is not None:
		return v.lower() in ("yes", "true", "t", "1")
	return False
SCP_DEBUG = str2bool(os.getenv("SCP_DEBUG"))

class HexLoader:

	def scp_derive_key(self, ecdh_secret, keyindex):
		retry = 0
		# di = sha256(i || retrycounter || ecdh secret)
		while True:
			sha256 = hashlib.new('sha256')
			sha256.update(struct.pack(">IB", keyindex, retry))
			sha256.update(ecdh_secret)

			# compare di with order
			CURVE_SECP256K1 = Curve.get_curve('secp256k1')
			if int.from_bytes(sha256.digest(), 'big') < CURVE_SECP256K1.order:
				break
			#regenerate a new di satisfying order upper bound
			retry+=1

		# Pi = di*G

# Path: ledgerblue/ecWrapper.py
# class PrivateKey(object):
# 
# 	def __init__(self, privkey=None, raw=True):
# 		if USE_SECP:
# 			self.obj = secp256k1.PrivateKey(privkey, raw)
# 			self.pubkey = self.obj.pubkey
# 		else:
# 			if not raw:
# 				raise Exception("Non raw init unsupported")
# 			if privkey == None:
# 				privkey = ecpy.ecrand.rnd(CURVE_SECP256K1.order)
# 			else:
# 				privkey = int.from_bytes(privkey,'big')
# 			self.obj = ECPrivateKey(privkey, CURVE_SECP256K1)
# 			pubkey = self.obj.get_public_key().W
# 			out = b"\x04"
# 			out += pubkey.x.to_bytes(32, 'big')
# 			out += pubkey.y.to_bytes(32, 'big')
# 			self.pubkey = PublicKey(out, raw=True)
# 
# 	def serialize(self):
# 		if USE_SECP:
# 			return self.obj.serialize()
# 		else:
# 			return "%.64x"%self.obj.d
# 
# 	def ecdsa_serialize(self, raw_sig):
# 		if USE_SECP:
# 			return self.obj.ecdsa_serialize(raw_sig)
# 		else:
# 			return raw_sig
# 
# 	def ecdsa_sign(self, msg, raw=False, digest=hashlib.sha256, rfc6979=False):
# 		if USE_SECP:
# 			return self.obj.ecdsa_sign(msg, raw, digest)
# 		else:
# 			if not raw:
# 				h = digest()
# 				h.update(msg)
# 				msg = h.digest()
# 			if rfc6979:
# 				signature = SIGNER.sign_rfc6979(msg, self.obj, digest, True)
# 			else:
# 				signature = SIGNER.sign(msg, self.obj, True)
# 			return signature


# Path: ledgerblue/hexLoader.py
from Cryptodome.Cipher import AES
from .ecWrapper import PrivateKey
from ecpy.curves import Curve
import sys
import struct
import hashlib
import binascii
import os

		s += struct.pack(">H", l)
	else:
		raise Exception("Unimplemented TLV encoding")
	s += v
	return s

def str2bool(v):
	if v is not None:
		return v.lower() in ("yes", "true", "t", "1")
	return False
SCP_DEBUG = str2bool(os.getenv("SCP_DEBUG"))

class HexLoader:

	def scp_derive_key(self, ecdh_secret, keyindex):
		retry = 0
		# di = sha256(i || retrycounter || ecdh secret)
		while True:
			sha256 = hashlib.new('sha256')
			sha256.update(struct.pack(">IB", keyindex, retry))
			sha256.update(ecdh_secret)

			# compare di with order
			CURVE_SECP256K1 = Curve.get_curve('secp256k1')
			if int.from_bytes(sha256.digest(), 'big') < CURVE_SECP256K1.order:
				break
			#regenerate a new di satisfying order upper bound
			retry+=1

		# Pi = di*G

		privkey = PrivateKey(bytes(sha256.digest()))