本文整理汇总了Python中pycoin.serialize.b2h函数的典型用法代码示例。如果您正苦于以下问题:Python b2h函数的具体用法?Python b2h怎么用?Python b2h使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了b2h函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: make_transaction
def make_transaction( src_priv_key, dst_address, value, data ):
src_address = b2h( utils.privtoaddr(src_priv_key) )
nonce = get_num_transactions( src_address )
gas_price = get_gas_price_in_wei()
data_as_string = b2h(data)
start_gas = eval_startgas( src_address, dst_address, value, data_as_string, gas_price )
nonce = int( nonce, 16 )
gas_price = int( gas_price, 16 )
start_gas = int( start_gas, 16 ) + 100000
tx = transactions.Transaction( nonce,
gas_price,
start_gas,
dst_address,
value,
data ).sign(src_priv_key)
tx_hex = b2h(rlp.encode(tx))
tx_hash = b2h( tx.hash )
if use_ether_scan:
params = [{"hex" : "0x" + tx_hex }]
else:
params = ["0x" + tx_hex]
return_value = json_call( "eth_sendRawTransaction", params )
if return_value == "0x0000000000000000000000000000000000000000000000000000000000000000":
print "Transaction failed"
return False
wait_for_confirmation(tx_hash)
return return_value
示例2: solve_finalize_commit
def solve_finalize_commit(self, **kwargs):
hash160_lookup = kwargs.get("hash160_lookup")
sign_value = kwargs.get("sign_value")
signature_type = kwargs.get("signature_type")
existing_script = kwargs.get("existing_script")
# FIXME validate on receiving the commit
# validate payer sig
opcode, data, pc = tools.get_opcode(existing_script, 0) # OP_0
opcode, payer_sig, pc = tools.get_opcode(existing_script, pc)
sig_pair, actual_signature_type = parse_signature_blob(payer_sig)
try:
public_pair = encoding.sec_to_public_pair(self.payer_sec)
sig_pair, signature_type = parse_signature_blob(payer_sig)
valid = ecdsa.verify(ecdsa.generator_secp256k1, public_pair,
sign_value, sig_pair)
if not valid:
raise Exception("Invalid payer public_pair!")
except (encoding.EncodingError, UnexpectedDER):
raise Exception("Invalid payer public_pair!")
# sign
private_key = hash160_lookup.get(encoding.hash160(self.payee_sec))
secret_exponent, public_pair, compressed = private_key
payee_sig = self._create_script_signature(
secret_exponent, sign_value, signature_type
)
script_text = "OP_0 {payer_sig} {payee_sig} OP_1".format(
payer_sig=b2h(payer_sig), payee_sig=b2h(payee_sig)
)
return tools.compile(script_text)
示例3: __init__
def __init__(self, payer_sec, payee_sec, spend_secret_hash, expire_time):
self.payer_sec = payer_sec
self.payee_sec = payee_sec
self.spend_secret_hash = spend_secret_hash
self.expire_time = expire_time
self.script = compile_deposit_script(b2h(payer_sec), b2h(payee_sec),
spend_secret_hash, expire_time)
示例4: main
def main():
parser = argparse.ArgumentParser(description="Bitcoin utilities. WARNING: obsolete. Use ku instead.")
parser.add_argument('-a', "--address", help='show as Bitcoin address', action='store_true')
parser.add_argument('-1', "--hash160", help='show as hash 160', action='store_true')
parser.add_argument('-v', "--verbose", help='dump all information available', action='store_true')
parser.add_argument('-w', "--wif", help='show as Bitcoin WIF', action='store_true')
parser.add_argument('-n', "--uncompressed", help='show in uncompressed form', action='store_true')
parser.add_argument('item', help='a WIF, secret exponent, X/Y public pair, SEC (as hex), hash160 (as hex), Bitcoin address', nargs="+")
args = parser.parse_args()
for c in args.item:
# figure out what it is:
# - secret exponent
# - WIF
# - X/Y public key (base 10 or hex)
# - sec
# - hash160
# - Bitcoin address
secret_exponent = parse_as_private_key(c)
if secret_exponent:
public_pair = ecdsa.public_pair_for_secret_exponent(secp256k1.generator_secp256k1, secret_exponent)
print("secret exponent: %d" % secret_exponent)
print(" hex: %x" % secret_exponent)
print("WIF: %s" % encoding.secret_exponent_to_wif(secret_exponent, compressed=True))
print(" uncompressed: %s" % encoding.secret_exponent_to_wif(secret_exponent, compressed=False))
else:
public_pair = parse_as_public_pair(c)
if public_pair:
bitcoin_address_uncompressed = encoding.public_pair_to_bitcoin_address(public_pair, compressed=False)
bitcoin_address_compressed = encoding.public_pair_to_bitcoin_address(public_pair, compressed=True)
print("public pair x: %d" % public_pair[0])
print("public pair y: %d" % public_pair[1])
print(" x as hex: %x" % public_pair[0])
print(" y as hex: %x" % public_pair[1])
print("y parity: %s" % "odd" if (public_pair[1] & 1) else "even")
print("key pair as sec: %s" % b2h(encoding.public_pair_to_sec(public_pair, compressed=True)))
s = b2h(encoding.public_pair_to_sec(public_pair, compressed=False))
print(" uncompressed: %s\\\n %s" % (s[:66], s[66:]))
hash160 = encoding.public_pair_to_hash160_sec(public_pair, compressed=True)
hash160_unc = encoding.public_pair_to_hash160_sec(public_pair, compressed=False)
myeccpoint = encoding.public_pair_to_sec(public_pair, compressed=True)
myhash = encoding.ripemd160( myeccpoint ).digest( )
print("BTSX PubKey: %s" % BTS_ADDRESS_PREFIX + encoding.b2a_base58(myeccpoint + myhash[ :4 ]))
else:
hash160 = parse_as_address(c)
hash160_unc = None
if not hash160:
sys.stderr.write("can't decode input %s\n" % c)
sys.exit(1)
print("hash160: %s" % b2h(hash160))
if hash160_unc:
print(" uncompressed: %s" % b2h(hash160_unc))
print("Bitcoin address: %s" % encoding.hash160_sec_to_bitcoin_address(hash160))
if hash160_unc:
print(" uncompressed: %s" % encoding.hash160_sec_to_bitcoin_address(hash160_unc))
示例5: fix_input_script
def fix_input_script(inp, redeem_script):
"""replace dummy signatures with OP_0 and add redeem script for digitaloracle compatibility"""
dummy = b2h(dummy_signature(1))
ops1 = []
for op in opcode_list(inp.script):
if op == dummy:
op = 'OP_0'
ops1.append(op)
# FIXME hack to add redeem script omitted by pycoin
ops1.append(b2h(redeem_script))
inp.script = compile(' '.join(ops1))
示例6: test_verify_transaction
def test_verify_transaction(self):
bitcoin.SelectParams('testnet')
b = h2b('8443b07464c762d7fb404ea918a5ac9b3618d5cd6a0c5ea6e4dd5d7bbe28b154')
tx_input = Spendable(200, b'18eKkAWyU9kvRNHPKxnZb6wwtPMrNmRRRA', b, 0)
tx_outs = [tx_utils.create_transaction_output('mgAqW5ZCnEp7fjvpj8RUL3WxsBy8rcDcCi', 0.0000275)]
op_return_val = h2b('e9cee71ab932fde863338d08be4de9dfe39ea049bdafb342ce659ec5450b69ae')
tx = tx_utils.create_trx(op_return_val, 3, 'mgAqW5ZCnEp7fjvpj8RUL3WxsBy8rcDcCi', tx_outs, [tx_input])
hextx = b2h(tx.serialize())
tx_utils.verify_transaction(hextx, b2h(op_return_val))
示例7: create_public_pair_output
def create_public_pair_output(key, add_output):
public_pair = key.public_pair()
if public_pair:
add_output("public_pair_x", '%d' % public_pair[0])
add_output("public_pair_y", '%d' % public_pair[1])
add_output("public_pair_x_hex", '%x' % public_pair[0], " x as hex")
add_output("public_pair_y_hex", '%x' % public_pair[1], " y as hex")
add_output("y_parity", "odd" if (public_pair[1] & 1) else "even")
add_output("key_pair_as_sec", b2h(key.sec(use_uncompressed=False)))
add_output("key_pair_as_sec_uncompressed", b2h(key.sec(use_uncompressed=True)), " uncompressed")
示例8: solve_create_commit
def solve_create_commit(self, **kwargs):
hash160_lookup = kwargs["hash160_lookup"]
private_key = hash160_lookup.get(encoding.hash160(self.payer_sec))
secret_exponent, public_pair, compressed = private_key
sig = self._create_script_signature(
secret_exponent, kwargs["sign_value"], kwargs["signature_type"]
)
signature_placeholder = kwargs.get("signature_placeholder",
DEFAULT_PLACEHOLDER_SIGNATURE)
script_text = "OP_0 {payer_sig} {payee_sig} OP_1".format(
payer_sig=b2h(sig), payee_sig=b2h(signature_placeholder)
)
return tools.compile(script_text)
示例9: from_script
def from_script(cls, script):
r = cls.match(script)
if r:
delay_time = get_commit_delay_time(cls.TEMPLATE)
spend_secret_hash = b2h(r["PUBKEYHASH_LIST"][0])
payee_sec = r["PUBKEY_LIST"][0]
revoke_secret_hash = b2h(r["PUBKEYHASH_LIST"][1])
payer_sec = r["PUBKEY_LIST"][1]
obj = cls(delay_time, spend_secret_hash,
payee_sec, payer_sec, revoke_secret_hash)
assert(obj.script == script)
return obj
raise ValueError("bad script")
示例10: test_batch_handler_web_prepare
def test_batch_handler_web_prepare(self):
web_request = json.dumps([{'allyourbasearebelongtous': True}])
web_handler, certificates_to_issue = self._get_certificate_batch_web_handler()
web_handler.set_certificates_in_batch(web_request)
single_item_batch = web_handler.prepare_batch()
self.assertEqual(
b2h(single_item_batch),
'38451f557bc2b5ad74012d3389798281b993fc7375c024615ed73fb147670ba7')
web_handler, certificates_to_issue = self._get_certificate_batch_web_handler()
web_handler.set_certificates_in_batch(
[{'allyourbasearebelongtous': True}, {'allyourbasearebelongtous': False}])
multi_batch = web_handler.prepare_batch()
self.assertNotEqual(b2h(single_item_batch), b2h(multi_batch))
示例11: solve_change
def solve_change(self, **kwargs):
hash160_lookup = kwargs["hash160_lookup"]
spend_secret = kwargs["spend_secret"]
private_key = hash160_lookup.get(encoding.hash160(self.payer_sec))
secret_exponent, public_pair, compressed = private_key
sig = self._create_script_signature(
secret_exponent, kwargs["sign_value"], kwargs["signature_type"]
)
spend_secret_hash = get_deposit_spend_secret_hash(self.script)
provided_spend_secret_hash = b2h(hash160(h2b(spend_secret)))
assert(spend_secret_hash == provided_spend_secret_hash)
script_text = "{sig} {secret} OP_1 OP_0".format(
sig=b2h(sig), secret=spend_secret
)
return tools.compile(script_text)
示例12: verify_script
def verify_script(script_signature, script_public_key, signature_for_hash_type_f, expected_hash_type=None):
stack = []
is_p2h = (len(script_public_key) == 23 and script_public_key[0] == opcodes.OP_HASH160
and script_public_key[-1] == opcodes.OP_EQUAL)
if not eval_script(script_signature, signature_for_hash_type_f, expected_hash_type, stack):
logging.debug("script_signature did not evaluate")
return False
if is_p2h:
signatures, alt_script_public_key = stack[:-1], stack[-1]
from pycoin.tx.script import tools
from pycoin import serialize
def sub(x):
if x == '00':
return '0'
return x
s1 = [sub(serialize.b2h(s)) for s in signatures]
alt_script_signature = tools.compile(" ".join(s1))
if not eval_script(script_public_key, signature_for_hash_type_f, expected_hash_type, stack):
logging.debug("script_public_key did not evaluate")
return False
if is_p2h and stack[-1] == VCH_TRUE:
return verify_script(alt_script_signature, alt_script_public_key,
signature_for_hash_type_f, expected_hash_type=expected_hash_type)
return stack[-1] == VCH_TRUE
示例13: create_key
def create_key(asset, netcode="BTC"):
secure_random_data = os.urandom(32)
key = BIP32Node.from_master_secret(secure_random_data, netcode=netcode)
return {
"asset": asset, "pubkey": b2h(key.sec()),
"wif": key.wif(), "address": key.address(),
}
示例14: reformat
def reformat(spendable):
return {
"txid": b2h_rev(spendable.tx_hash),
"index": spendable.tx_out_index,
"value": spendable.coin_value,
"script": b2h(spendable.script),
}
示例15: issue_transaction
def issue_transaction(self, blockchain_bytes):
eth_data_field = b2h(blockchain_bytes)
prepared_tx = self.create_transaction(blockchain_bytes)
signed_tx = self.sign_transaction(prepared_tx)
self.verify_transaction(signed_tx, eth_data_field)
txid = self.broadcast_transaction(signed_tx)
return txid