def check_all_redeem_script(): print("-----------TEST-----------") # get the redeem script signing_key = [ ecdsa.SigningKey.generate(curve=ecdsa.SECP256k1) for i in range(Params.P2SH_PUBLIC_KEY) ] verifying_key = [ signing_key[i].get_verifying_key().to_string() for i in range(Params.P2SH_PUBLIC_KEY) ] redeem_script = scriptBuild.get_redeem_script(verifying_key) to_address = pubkey_to_address(verifying_key) print(to_address) print("----------GET THE ADDR AND REDEEM SCRIPT-----------") pk_script = scriptBuild.get_pk_script(to_address) # print(script.Tokenizer(pk_script)) # get the tokens of pk_script outpoint = OutPoint( txid='c47852b74825cc2bbd39faafb588b50243de9bb453dd9f48167d12bb360848cc', txout_idx=0) txouts = [ TxOut(value=110, pk_script=scriptBuild.get_pk_script( '1NY36FKZqM97oEobfCewhUpHsbzAUSifzo')), TxOut(value=4999999890, pk_script=scriptBuild.get_pk_script( '1BP9KYivYyhPEbg2WDFTf83i8wnyFYuRGH')) ] sequence = 0 sign_a = signing_key[0].sign( build_spend_message(outpoint, verifying_key[0], sequence, txouts)) sign_b = signing_key[1].sign( build_spend_message(outpoint, verifying_key[1], sequence, txouts)) aa = ecdsa.SigningKey.generate(curve=ecdsa.SECP256k1) bb = aa.get_verifying_key().to_string() sign_c = aa.sign(build_spend_message(outpoint, bb, sequence, txouts)) sign = [sign_a, sign_c] signature = scriptBuild.get_signature_script_without_hashtype( sign, redeem_script) valid = script.Script(b'', b'').process(signature, pk_script, b'', 0) print(valid) print("----------DONE-----------")
def create_coinbase(cls, pay_to_addr, value, height): """ Only mining can create a new block with first tx is coinbase. """ return cls( txins=[ TxIn( to_spend=None, # Push current block height into unlock_sig so that this # transaction's ID is unique relative to other coinbase txns. # first param is unlock_sig, another is unlock_pk signature_script=scriptBuild. get_signature_script_without_hashtype( str(height).encode(), b"") if Params.SCRIPT_TYPE == 0 else scriptBuild.get_signature_script_without_hashtype( [str(height).encode()], b""), sequence=0, ) ], txouts=[ TxOut(value=value, pk_script=scriptBuild.get_pk_script(pay_to_addr)) ], serviceId=None, actionId=None, postId=None, data=None, locktime=None, )
def genesis_block(cls): return cls( version="5465616d3a20456467656e63650a4c65616465723a20776f6c6662726f746865720a4d656d626572733a2063626f7a69" "2c204c6561684c69752c207069616f6c69616e676b622c2053616c7661746f7265303632362c2053696c7669614c69313" "232352c204a69617169204c69752c2078696179756e696c0a", prev_block_hash=None, merkle_hash="a578b7a3bdc2d1385bce32a445a8ec4ffb9ab78b76afc30f53787b3189be289c", timestamp=1554460209, bits=Params.INITIAL_DIFFICULTY_BITS, nonce=41912381, txns=[ Transaction( txins=[TxIn(to_spend=None, signature_script=b"0", sequence=0)], txouts=[ TxOut( value=5000000000, pk_script=scriptBuild.get_pk_script( "1NY36FKZqM97oEobfCewhUpHsbzAUSifzo" ), ) ], locktime=None, serviceId=None, postId=None, actionId=None, data=None, ) ], )
def create_coinbase(cls, pay_to_addr, value, height): return cls( txins=[ TxIn( to_spend=None, # Push current block height into unlock_sig so that this # transaction's ID is unique relative to other coinbase txns. # first param is unlock_sig, another is unlock_pk signature_script=scriptBuild. get_signature_script_without_hashtype( str(height).encode(), b'') if Params.SCRIPT_TYPE == 0 else scriptBuild.get_signature_script_without_hashtype( [str(height).encode()], b''), sequence=0) ], txouts=[ TxOut(value=value, pk_script=scriptBuild.get_pk_script(pay_to_addr)) ], )
def genesis_block(cls): return cls( version= '5465616d3a20456467656e63650a4c65616465723a20776f6c6662726f746865720a4d656d626572733a2063626f7a69' '2c204c6561684c69752c207069616f6c69616e676b622c2053616c7661746f7265303632362c2053696c7669614c69313' '232352c204a69617169204c69752c2078696179756e696c0a', prev_block_hash=None, merkle_hash= 'b7b51d3818055321711cf3049b7067afb7d5cbddacc6c106ca92906974316b14', timestamp=1554460209, bits=Params.INITIAL_DIFFICULTY_BITS, nonce=5808524, txns=[ Transaction(txins=[ TxIn(to_spend=None, signature_script=b'0', sequence=0) ], txouts=[ TxOut( value=5000000000, pk_script=scriptBuild.get_pk_script( '1NY36FKZqM97oEobfCewhUpHsbzAUSifzo')) ], locktime=None) ])
def _make_pk_script(self, to_addr): # make template return scriptBuild.get_pk_script(to_addr)
from consensus.Consensus import PoW from params.Params import Params from script import scriptBuild from utils.Utils import Utils import time txns = [ Transaction( txins=[TxIn(to_spend=None, signature_script=b"0", sequence=0)], txouts=[ TxOut( value=5000000000, pk_script=scriptBuild.get_pk_script( "1NY36FKZqM97oEobfCewhUpHsbzAUSifzo" ), ) ], serviceId=None, postId=None, actionId=None, data=None, locktime=None, ) ] merkle_hash = MerkleNode.get_merkle_root_of_txns(txns) print(f"merkle_hash={merkle_hash.val}") genesis_block = Block( version="5465616d3a20456467656e63650a4c65616465723a20776f6c6662726f746865720a4d656d626572733a2063626f7a69"
def test_token_sig_tokenizer(): print("---------TEST----------") # 产生签名脚本 signature = b'a200548f8a634812284ad548e908feafe713b290215ed0273a5f28de622e40cb' signing_key = ecdsa.SigningKey.generate(curve=ecdsa.SECP256k1) pk = signing_key.get_verifying_key() my_address = pubkey_to_address(pk.to_string()) print("receiver(B)'s address: ", end="") print(my_address) pk_script = scriptBuild.get_pk_script(my_address) # 产生hash值以供验证 public_key_hash_de = b58decode_check(my_address)[1:] print("解码得到的hash值为(二进制): ", end="") print(binascii.hexlify(public_key_hash_de)) print(type(pk.to_string())) signature_script = scriptBuild.get_signature_script_without_hashtype( signature, pk.to_string()) tokens = script.Tokenizer(signature_script) print("签名脚本进入tokens:", end="") print(tokens) print("获取签名脚本中公钥部分:", end="") print(tokens.get_value(1)) print("公钥hash值: ", end="") print(hex(tokens[1])) print("获取公钥对应的hash值:", end="") pk_hash = scriptUtils.hash160(tokens.get_value(1)) print(pk_hash) print("二进制:", end="") print(binascii.hexlify(pk_hash)) print("判断相等:", end="") print(bool(public_key_hash_de == pk_hash)) print("---------TEST Script----------") print("检验栈操作是否可运行,注释check_signature部分:") print(script.Script.process(signature_script, pk_script, b'0', 1)) print("---------DONE----------") # ---------TEST - --------- # receiver(B) # 's address: 1QJdAcCpy9ckG4tnDBq2EJmLBGsLGB2ZQ8 # 解码得到的hash值为(二进制): b'ffa02768058ca2305f449f2da4619e71a08b5db0' # <class 'bytes'> # # 签名脚本进入tokens:5090570079643391295836653930619568056975108647454484232055555845918370907568237548385703324612871250494025243147705324820182025617872473178512831315731298 # 12437925785449238801746067915282852552951852862076944773142389933553787641793819115456526667601775483989502627061209560045577172510877154717230929234166842 # 获取签名脚本中公钥部分:b'\xed{P\x98\xab\x9em\xee\xd2\x9a+P\xbey\xe2G)m\xbc@:@\x1eO\xfd\x9f\x82\x01c\xc5O\xfb\xa79A\xf8Z\x9d|\xeb\xd8\x1f\x95\xc4a\x18\xc6\xad\xebi)\x95\xb1v\x86\xf0\xcc\x04\xc28!5\x04:' # 公钥hash值: 0x1ff # 获取公钥对应的hash值:b"\xff\xa0'h\x05\x8c\xa20_D\x9f-\xa4a\x9eq\xa0\x8b]\xb0" # 二进制:b'ffa02768058ca2305f449f2da4619e71a08b5db0' # 判断相等:True # ---------TEST Script - --------- # 检验栈操作是否可运行,注释check_signature部分: # ok # True # ---------DONE - --------- return None