def sign_donation_tx(tx, i, priv): from bitcoin.main import fast_multiply, decode_privkey, G, inv, N from bitcoin.transaction import der_encode_sig k = sign_k hashcode = btc.SIGHASH_ALL i = int(i) if len(priv) <= 33: priv = btc.safe_hexlify(priv) pub = btc.privkey_to_pubkey(priv) address = btc.pubkey_to_address(pub) signing_tx = btc.signature_form( tx, i, btc.mk_pubkey_script(address), hashcode) msghash = btc.bin_txhash(signing_tx, hashcode) z = btc.hash_to_int(msghash) # k = deterministic_generate_k(msghash, priv) r, y = fast_multiply(G, k) s = inv(k, N) * (z + r * decode_privkey(priv)) % N rawsig = 27 + (y % 2), r, s sig = der_encode_sig(*rawsig) + btc.encode(hashcode, 16, 2) # sig = ecdsa_tx_sign(signing_tx, priv, hashcode) txobj = btc.deserialize(tx) txobj["ins"][i]["script"] = btc.serialize_script([sig, pub]) return btc.serialize(txobj)
def sign_donation_tx(tx, i, priv): from bitcoin.main import fast_multiply, decode_privkey, G, inv, N from bitcoin.transaction import der_encode_sig k = sign_k hashcode = btc.SIGHASH_ALL i = int(i) if len(priv) <= 33: priv = btc.safe_hexlify(priv) pub = btc.privkey_to_pubkey(priv) address = btc.pubkey_to_address(pub) signing_tx = btc.signature_form(tx, i, btc.mk_pubkey_script(address), hashcode) msghash = btc.bin_txhash(signing_tx, hashcode) z = btc.hash_to_int(msghash) # k = deterministic_generate_k(msghash, priv) r, y = fast_multiply(G, k) s = inv(k, N) * (z + r * decode_privkey(priv)) % N rawsig = 27 + (y % 2), r, s sig = der_encode_sig(*rawsig) + btc.encode(hashcode, 16, 2) # sig = ecdsa_tx_sign(signing_tx, priv, hashcode) txobj = btc.deserialize(tx) txobj["ins"][i]["script"] = btc.serialize_script([sig, pub]) return btc.serialize(txobj)
def insecure_ecdsa_sign(msghash, priv): global insecure_k z = hash_to_int(msghash) k = insecure_k r, y = fast_multiply(G, k) s = inv(k, N) * (z + r * decode_privkey(priv)) % N v, r, s = 27 + ((y % 2) ^ (0 if s * 2 < N else 1)), r, s if s * 2 < N else N - s if 'compressed' in get_privkey_format(priv): print("COmpressed \a") v += 4 return v, r, s
# 파이썬 실습 파일: 4-5.Bech32Address.py # https://github.com/sipa/bech32/blob/master/ref/python/segwit_addr.py # 배포용 실습 코드의 bitcoin 폴더가 있는 곳에서 실행한다. import binascii import bitcoin.main as btc import bitcoin.segwit_addr as bech32 # 개인키를 생성한다 while (1): privKey = btc.random_key() # 256 bit Random number를 생성한다 dPrivKey = btc.decode_privkey(privKey, 'hex') # 16진수 문자열을 10진수 숫자로 변환한다 if dPrivKey < btc.N: # secp256k1 의 N 보다 작으면 OK break privKey='860ef116221744a5299c99a0ed726c15a2148a21a341fe522399c84a59771cfe01' # 개인키로 공개키를 생성한다. Compressed format. pubKey = btc.privkey_to_pubkey(privKey) cPubKey = btc.compress(pubKey) # 공개키로 160-bit public key hash를 생성한다 witprog = btc.bin_hash160(binascii.unhexlify(cPubKey)) # BIP-173 주소를 생성한다. (Base32 address format for native v0-16 witness outputs) # P2WPKH mainnetAddr = bech32.encode('bc', 0, witprog) testnetAddr = bech32.encode('tb', 0, witprog) # 결과 print("\n\n공개키 :", cPubKey) print("Bech32 주소 (Mainnet P2WPKH) :", mainnetAddr) print("Bech32 주소 (Testnet P2WPKH) :", testnetAddr)
print("\n ===the result to encode this message===\n", en_m) v, r, s = btc.ecdsa_raw_sign(btc.electrum_sig_hash(en_m), d) print("\n ===ECDSA raw Signature Result(v)=== \n", v) print("\n ===ECDSA raw Signature Result(r)=== \n", r) print("\n ===ECDSA Signature Result(s)=== \n", s) sig1 = btc.encode_sig(v, r, s) print("\n===Signature Result(sig1)===\n", sig1) sig2 = btc.ecdsa_sign(en_m, d) print("\n ===Signature Result(sig2)=== \n", sig2) v = btc.ecdsa_verify(en_m, sig2, Q) print("\n===v's value===\t", v) print("\nMessage =", en_m.decode()) if v: print("\n Valid Signature") else: print("\n Invalid Signature") passphrase = 'Brain wallet\'s test private key. forget it' privKey = btc.sha256(passphrase) dprivkey = btc.decode_privkey(privKey, 'hex') print("\n === PassPhrase ====\n", passphrase) print("\n === privKey ====\n", privKey) print("\n === decimal of privkey ====\n", dprivkey) input("\n\n\t\t if you wanna stop it, pls enter")