本文整理汇总了Python中pycoin.key.Key.sec方法的典型用法代码示例。如果您正苦于以下问题:Python Key.sec方法的具体用法?Python Key.sec怎么用?Python Key.sec使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pycoin.key.Key
的用法示例。
在下文中一共展示了Key.sec方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_master_public_and_private
# 需要导入模块: from pycoin.key import Key [as 别名]
# 或者: from pycoin.key.Key import sec [as 别名]
def test_master_public_and_private(self):
# these addresses were generated by hand using electrum with a master public key
# corresponding to secret exponent 1
RECEIVING_ADDRESSES = [
"1AYPdHLna6bKFUbeXoAEVbaXUxifUwCMay",
"13UeuWJba5epizAKyfCfiFKY5Kbxfdxe7B",
"19f6KJUTL5AGBRvLBGiL6Zpcx53QA7zaKT",
"1Cm33VuSkoUETwx5nsF1wgmGqYwJZxpZdY",
"14Z6ErkETixQMUeivsYbrdoUFns2J1iSct",
]
CHANGE_ADDRESSES = [
"1JVYsmjrqSy1BKvo1gYpNjX7AYea74nQYe",
"1Cc7itfQaDqZK3vHYphFsySujQjBNba8mw",
"15wrXvrAnyv3usGeQRohnnZ8tz9XAekbag",
"1MnWCEjE5YiZpZrkP8HcXEeDqwg43RxLwu",
"1Fgyp3PUx9AAg8yJe1zGXHP5dVC6i1tXbs",
"12XTLd4u9jeqw4egLAUhoKLxHARCdKWkty",
]
k = Key(secret_exponent=1)
master_public_key = k.sec(use_uncompressed=True)[1:]
wallet = ElectrumWallet(master_public_key=master_public_key)
for idx, address in enumerate(RECEIVING_ADDRESSES):
subkey = wallet.subkey("%s/0" % idx)
calculated_address = subkey.address()
self.assertEqual(address, calculated_address)
for idx, address in enumerate(CHANGE_ADDRESSES):
subkey = wallet.subkey("%s/1" % idx)
calculated_address = subkey.address()
self.assertEqual(address, calculated_address)
wallet = ElectrumWallet(master_private_key=1)
for idx, address in enumerate(RECEIVING_ADDRESSES):
subkey = wallet.subkey("%s/0" % idx)
calculated_address = subkey.address()
self.assertEqual(address, calculated_address)
wif = subkey.wif()
key = Key.from_text(wif)
self.assertEqual(key.address(use_uncompressed=True), address)
for idx, address in enumerate(CHANGE_ADDRESSES):
subkey = wallet.subkey("%s/1" % idx)
calculated_address = subkey.address()
self.assertEqual(address, calculated_address)
wif = subkey.wif()
key = Key.from_text(wif)
self.assertEqual(key.address(use_uncompressed=True), address)
示例2: _test_sighash_single
# 需要导入模块: from pycoin.key import Key [as 别名]
# 或者: from pycoin.key.Key import sec [as 别名]
def _test_sighash_single(self, netcode):
k0 = Key(secret_exponent=PRIV_KEYS[0], is_compressed=True, netcode=netcode)
k1 = Key(secret_exponent=PRIV_KEYS[1], is_compressed=True, netcode=netcode)
k2 = Key(secret_exponent=PRIV_KEYS[2], is_compressed=True, netcode=netcode)
k3 = Key(secret_exponent=PRIV_KEYS[3], is_compressed=True, netcode=netcode)
k4 = Key(secret_exponent=PRIV_KEYS[4], is_compressed=True, netcode=netcode)
k5 = Key(secret_exponent=PRIV_KEYS[5], is_compressed=True, netcode=netcode)
# Fake a coinbase transaction
coinbase_tx = Tx.coinbase_tx(k0.sec(), 500000000)
coinbase_tx.txs_out.append(TxOut(1000000000, pycoin_compile('%s OP_CHECKSIG' % b2h(k1.sec()))))
coinbase_tx.txs_out.append(TxOut(1000000000, pycoin_compile('%s OP_CHECKSIG' % b2h(k2.sec()))))
self.assertEqual('2acbe1006f7168bad538b477f7844e53de3a31ffddfcfc4c6625276dd714155a',
b2h_rev(coinbase_tx.hash()))
# Make the test transaction
txs_in = [
TxIn(coinbase_tx.hash(), 0),
TxIn(coinbase_tx.hash(), 1),
TxIn(coinbase_tx.hash(), 2),
]
txs_out = [
TxOut(900000000, standard_tx_out_script(k3.address())),
TxOut(800000000, standard_tx_out_script(k4.address())),
TxOut(800000000, standard_tx_out_script(k5.address())),
]
tx = Tx(1, txs_in, txs_out)
tx.set_unspents(coinbase_tx.txs_out)
self.assertEqual('791b98ef0a3ac87584fe273bc65abd89821569fd7c83538ac0625a8ca85ba587', b2h_rev(tx.hash()))
sig_type = SIGHASH_SINGLE
sig_hash = tx.signature_hash(coinbase_tx.txs_out[0].script, 0, sig_type)
self.assertEqual('cc52d785a3b4133504d1af9e60cd71ca422609cb41df3a08bbb466b2a98a885e', b2h(to_bytes_32(sig_hash)))
sig = sigmake(k0, sig_hash, sig_type)
self.assertTrue(sigcheck(k0, sig_hash, sig[:-1]))
tx.txs_in[0].script = pycoin_compile(b2h(sig))
self.assertTrue(tx.is_signature_ok(0))
sig_hash = tx.signature_hash(coinbase_tx.txs_out[1].script, 1, sig_type)
self.assertEqual('93bb883d70fccfba9b8aa2028567aca8357937c65af7f6f5ccc6993fd7735fb7', b2h(to_bytes_32(sig_hash)))
sig = sigmake(k1, sig_hash, sig_type)
self.assertTrue(sigcheck(k1, sig_hash, sig[:-1]))
tx.txs_in[1].script = pycoin_compile(b2h(sig))
self.assertTrue(tx.is_signature_ok(1))
sig_hash = tx.signature_hash(coinbase_tx.txs_out[2].script, 2, sig_type)
self.assertEqual('53ef7f67c3541bffcf4e0d06c003c6014e2aa1fb38ff33240b3e1c1f3f8e2a35', b2h(to_bytes_32(sig_hash)))
sig = sigmake(k2, sig_hash, sig_type)
self.assertTrue(sigcheck(k2, sig_hash, sig[:-1]))
tx.txs_in[2].script = pycoin_compile(b2h(sig))
self.assertTrue(tx.is_signature_ok(2))
sig_type = SIGHASH_SINGLE | SIGHASH_ANYONECANPAY
sig_hash = tx.signature_hash(coinbase_tx.txs_out[0].script, 0, sig_type)
self.assertEqual('2003393d246a7f136692ce7ab819c6eadc54ffea38eb4377ac75d7d461144e75', b2h(to_bytes_32(sig_hash)))
sig = sigmake(k0, sig_hash, sig_type)
self.assertTrue(sigcheck(k0, sig_hash, sig[:-1]))
tx.txs_in[0].script = pycoin_compile(b2h(sig))
self.assertTrue(tx.is_signature_ok(0))
sig_hash = tx.signature_hash(coinbase_tx.txs_out[1].script, 1, sig_type)
self.assertEqual('e3f469ac88e9f35e8eff0bd8ad4ad3bf899c80eb7645947d60860de4a08a35df', b2h(to_bytes_32(sig_hash)))
sig = sigmake(k1, sig_hash, sig_type)
self.assertTrue(sigcheck(k1, sig_hash, sig[:-1]))
tx.txs_in[1].script = pycoin_compile(b2h(sig))
self.assertTrue(tx.is_signature_ok(1))
sig_hash = tx.signature_hash(coinbase_tx.txs_out[2].script, 2, sig_type)
self.assertEqual('bacd7c3ab79cad71807312677c1788ad9565bf3c00ab9a153d206494fb8b7e6a', b2h(to_bytes_32(sig_hash)))
sig = sigmake(k2, sig_hash, sig_type)
self.assertTrue(sigcheck(k2, sig_hash, sig[:-1]))
tx.txs_in[2].script = pycoin_compile(b2h(sig))
self.assertTrue(tx.is_signature_ok(2))
示例3: main
# 需要导入模块: from pycoin.key import Key [as 别名]
# 或者: from pycoin.key.Key import sec [as 别名]
def main():
parser = argparse.ArgumentParser(
description='ECkey2coin.py by [email protected] for UTXO based Certificates UTXOC.',
epilog='Known networks codes:\n ' \
+ ', '.join(['%s (%s)'%(i, full_network_name_for_netcode(i)) for i in NETWORK_NAMES])
)
parser.add_argument('-k', '--key', required=False, type=argparse.FileType('r'), help='The EC private key in PEM format')
parser.add_argument('-q', '--qrfilename', required=False, help='QR code output filename')
parser.add_argument('-n', "--network", help='specify network (default: BTC = Bitcoin)',
default='BTC', choices=NETWORK_NAMES)
args = parser.parse_args()
network = args.network
inputprivatekey = ''
if args.key:
keyfile = args.key
while True:
line = keyfile.readline().strip()
if not line: break
inputprivatekey += line + '\n'
print 'Loaded EC Key from %s' % keyfile
else:
print ('Please enter EC KEY in pem format:')
inputprivatekey = ''
while True:
line = raw_input().strip()
if not line: break
inputprivatekey += line + '\n'
if not args.qrfilename:
qrfilename = raw_input("Please enter qrcode output filename: ")
else:
qrfilename = args.qrfilename
pkey = decoder.decode(read_pem(inputprivatekey), asn1Spec=ECPrivateKey())
print 'Key loaded'
if not isValidECKey(pkey[0]):
print "EC Key Supplied cannot be used"
exit
print "Key Validated OK"
inputkey = encoding.to_long(256, pycoin.encoding.byte_to_int, pkey[0][1].asOctets())[0]
if inputkey:
key = Key(secret_exponent=inputkey, netcode=network)
btcsecret = key.secret_exponent()
btcpublic = key.public_pair()
hash160_c = key.hash160(use_uncompressed=False)
hash160_u = key.hash160(use_uncompressed=True)
qrimg = qrcode.QRCode (
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_L,
box_size=10,
border=4,
)
qrimg.add_data(key.address(use_uncompressed=False))
qrimg.make(fit=True)
img = qrimg.make_image()
img.save(qrfilename)
print"----------------- BEGIN EC PRIVATE KEYS -----------------"
print "Secret: %d" % btcsecret
print "Secret hex: %x" % btcsecret
print "wif: %s" % key.wif(use_uncompressed=False)
print "----------------- END EC PRIVATE KEYS -----------------------------"
print "----------------- BEGIN PUBLIC KEY -----------------------------"
print "Public X: %d" % btcpublic[0]
print "Public Y: %d" % btcpublic[1]
print "hash160 uncompressed: %s" % b2h(hash160_u)
print "Sec: (uncompressed): %s" % b2h(key.sec(use_uncompressed=True))
print "%s address: %s (uncompressed)" % (key._netcode, key.address(use_uncompressed=True))
print "Public X (hex): %x" % btcpublic[0]
print "Public Y (hex): %x" % btcpublic[1]
print "Sec: %s" % b2h(key.sec(use_uncompressed=False))
print "hash160 compressed: %s" % b2h(hash160_c)
print "----------------- END PUBLIC KEYS -----------------------------"
print "------------------ BEGIN %s ADDRESSES -------------------------" % key._netcode
print "%s address: %s" % (key._netcode, key.address(use_uncompressed=False))
print "------------------ END %s ADDRESSES -------------------------" % key._netcode