from ecdsa.util import sigencode_der, sigencode_der_canonize, sigencode_strings from ecdsa.util import sigdecode_der, sigdecode_strings from ecdsa import SigningKey, NIST256p, SECP256k1 from hash_util import hash256, dhash256, get_len_hex, formatamount, hash160, reverse_byte_order from secp256k1 import GetUncompressedkey, ParseSignature #the message for sign signed_msg = '0a00903a1a547ea6c0cb20b1ef2e3926202f27c4dee9a656a57d8a6800ce67e3' print 'h_msg:', signed_msg #priv key priv_key = '3b8407ec451a008c92c20068a6ca6e80e95c05b1a24655c646bcdadf33e8be2d' sk = SigningKey.from_string(binascii.unhexlify(priv_key), curve=SECP256k1) sign_data_hash_single = hash256(signed_msg.decode('hex')) sign_data_hash_double = hash256(sign_data_hash_single) print 'hash1:', sign_data_hash_single.encode('hex') print 'hash2:', sign_data_hash_double.encode('hex') #create signature der = sk.sign_digest_deterministic(sign_data_hash_double, hashfunc=hashlib.sha256, sigencode=sigencode_der_canonize) print 'der_s:', der.encode('hex') #verify signature pub_key = '026225155bd431cbdd7e6d8ab07e61cd30482158b66fa11fe3c2492cd0a9dd2310' (x, y) = GetUncompressedkey(pub_key) x = hex(x)[2:].rstrip('L').zfill(64) y = hex(y)[2:].rstrip('L').zfill(64) pubkey_b = (x + y).decode('hex')
from segwit_addr import encode from segwit_addr import decode from hash_util import hash160,hash256 hrp='tb' #testnet #hrp='bc' #mainnet pubkey1='21026ccfb8061f235cc110697c0bfb3afb99d82c886672f6b9b5393b25a434c0cbf3' pubkey2='2103befa190c0c22e2f53720b1be9476dcf11917da4665c44c9c71c3a2d28a933c35' pubkey3='2102be46dc245f58085743b1cc37c82f0d63a960efa43b5336534275fc469b49f4ac' data='52'+pubkey1+pubkey2+pubkey3+'53'+'ae' script_hash=hash256(data.decode('hex')).encode('hex') witver=0 witprog=[int(x) for x in bytearray.fromhex(script_hash)] #print witprog address=encode(hrp, witver, witprog) print address data=decode(hrp, address) #print data
from hash_util import ripemd160, hash160, hash256, dhash256 from base58 import b58encode, b58encode_check, b58decode #1 Calculate the SHA256 of 2 of 3 public key data1 = '21026ccfb8061f235cc110697c0bfb3afb99d82c886672f6b9b5393b25a434c0cbf3' data2 = '2103befa190c0c22e2f53720b1be9476dcf11917da4665c44c9c71c3a2d28a933c35' data3 = '2102be46dc245f58085743b1cc37c82f0d63a960efa43b5336534275fc469b49f4ac' data = '52' + data1 + data2 + data3 + '53' + 'ae' data = hash256(data.decode('hex')).encode('hex') #print data #2 Create P2SH redeemScript as OP_PUSH publicKeyHash redeemScript = '0020' + data #3 Generate scriptPubKey as OP_HASH160 hash160(redeemScript) OP_EQUAL data = hash160(redeemScript.decode('hex')).encode('hex') print data scriptPubKey = 'a914' + data + '87' version = '05' #mainnet version = 'c4' #testnet data = version + data #print EncodeBase58Check(data.decode('hex')) #4 Generate address with 0x05 prefix and double SHA256 hash checksum(=4bytes) checksum = dhash256(data.decode('hex')) checksum = checksum.encode('hex')[0:8] addressHash = data + checksum #5 bash58encode address = b58encode(addressHash.decode('hex')) print address
amount_satoshi = long(round(float(amount * __unit))) amount = formatamount(amount_satoshi) #print output_amount pay_to_hash = '1976a914' + 'ecebae831bbfbd7827542a82da4dc136e1288f71' + '88ac' outputs = amount + pay_to_hash #amount+dest_script hashOutputs = dhash256(outputs.decode('hex')).encode('hex') #redeeScript scriptCode = '52' + '21' + '03d1531d7a373707e3057b53462f7b66e7ded258e39a1cda537bdbcb09b55b0ea0' + '21' + '02ce0309065cdad727092440f13d6e2c1dabbc66fd6e4299e1642fdc65d2da303a' + '52' + 'ae' witness_script = scriptCode p_len = (len(witness_script)) / 2 p_len = hex(p_len).lstrip('0x') witness_script = p_len + witness_script #hash256(redeemScript) redeem_public_key_hash = hash256(scriptCode.decode('hex')).encode('hex') redeem_public_key_hash = '0020' + redeem_public_key_hash p_len = (len(redeem_public_key_hash)) / 2 p_len = hex(p_len).lstrip('0x') redeem_public_key_hash = p_len + redeem_public_key_hash p_len = (len(redeem_public_key_hash)) / 2 p_len = hex(p_len).lstrip('0x') redeem_public_key_hash = p_len + redeem_public_key_hash redeem_amount = 1.2998 print "n_version :", n_version print "hashPrevouts:", hashPrevouts print "hashSequence:", hashSequence print "outpoint :", outpoint print "scriptCode :", scriptCode print "redeem_amount:", redeem_amount