def main(): h = ByteArray( "4c6f72656d20697073756d20646f6c6f722073697420616d65742c20636f6e73656374657475722061646970697363696e6720656c69742e20446f6e65632061742066617563696275732073617069656e2c2076656c20666163696c6973697320617263752e20536564207574206d61737361206e6962682e205574206d6f6c6c69732070756c76696e6172206d617373612e20557420756c6c616d636f7270657220646f6c6f7220656e696d2c20696e206d6f6c657374696520656e696d20636f6e64696d656e74756d2061632e20416c697175616d206572617420766f6c75747061742e204e756c6c6120736f64616c657320617420647569206e656320" ) print(h) print(blake_hash(h.bytes()).hex()) print(crypto.hash160(h.bytes()).hex())
def utxosource(amt, filter): nextVal = 10 total = 0 utxos = [] while total < amt: atoms = int(nextVal * 1e8) privKey = Curve.generateKey() pkHash = crypto.hash160( privKey.pub.serializeCompressed().b) addr = addrlib.AddressPubKeyHash(pkHash, testnet) addrs.append(addr) addrString = addr.string() keys[addrString] = privKey pkScript = txscript.makePayToAddrScript( addrString, testnet) txHash = rando.newHash() txid = reversed(txHash).hex() utxos.append( account.UTXO( address=addrString, txHash=txHash, vout=0, ts=int(time.time()), scriptPubKey=pkScript, satoshis=atoms, )) tx = msgtx.MsgTx.new() tx.addTxOut(msgtx.TxOut(value=atoms, pkScript=pkScript)) txs[txid] = tx total += atoms nextVal *= 2 return utxos, True
def internal(): privKey = Curve.generateKey() pkHash = crypto.hash160(privKey.pub.serializeCompressed().b) addr = addrlib.AddressPubKeyHash(pkHash, testnet) addrs.append(addr) keys[addr.string()] = privKey return addr.string()
def hash160(self): """ The hash160 of the serialized pubkey. Returns: ByteArray: The hash. """ return hash160(self.serialize().bytes())
def main(): script = ByteArray( "512103af3c24d005ca8b755e7167617f3a5b4c60a65f8318a7fcd1b0cacb1abd2a97fc21027b81bc16954e28adb832248140eb58bedb6078ae5f4dabf21fde5a8ab7135cb652ae" ) print(crypto.hash160(script.bytes()).hex()) _, addrs, _ = txscript.extractPkScriptAddrs(0, script, nets.testnet) for addr in addrs: print("addr", addr.string())
def fromScript(script, netParams): """ Create a new AddressScriptHash from a redeem script. Args: script (ByteArray): the redeem script netParams (module): the network parameters Returns: AddressScriptHash: An address object. """ return AddressScriptHash(hash160(script.b), netParams)
def address(self): """ The string encoding of the public key as a pay-to-pubkey-hash. Note that the public key format (uncompressed, compressed, etc) will change the resulting address. This is expected since pay-to-pubkey-hash is a hash of the serialized public key which obviously differs with the format. At the time of this writing, most Decred addresses are pay-to-pubkey-hash constructed from the compressed public key. Returns: str: base-58 encoded p2pkh address. """ return encodeAddress(hash160(self.serialize().bytes()), self.pubkeyHashID)
def deriveChildAddress(branchXPub, i, netParams): """ The base-58 encoded address for the i'th child. Args: i (int): Child number. netParams (module): Network parameters. Returns: str: Child address, as a base-58 encoded string. """ child = branchXPub.child(i) return AddressPubKeyHash( hash160(child.publicKey().serializeCompressed().b), netParams, STEcdsaSecp256k1, ).string()
def main(): ba = ByteArray(1, length=32) priv = crypto.privKeyFromBytes(ba) pub = priv.pub pkh = crypto.hash160(pub.serializeCompressed().b) addrPKH = addrlib.AddressPubKeyHash(pkh, mainnet) print([x for x in ba]) print(pub.serializeCompressed().b.hex()) print(addrPKH.string()) h = ByteArray("751e76e8199196d454941c45d1b3a323f1433bd6") addrPKH = addrlib.AddressPubKeyHash(h, mainnet) print(addrPKH.string()) addrPKH = addrlib.AddressPubKeyHash(h, testnet) print(addrPKH.string()) addrPKH = addrlib.AddressPubKeyHash(h, simnet) print(addrPKH.string()) h = "rltc1qzhe0hmteg5u6ap7488vq9f3wtqsx4ma9dps9t4" r = "bcrt1q322tg0y2hzyp9zztr7d2twdclhqg88anvzxwwr" #b = b58decode(h) print([x for x in h.encode("utf_8")]) print([x for x in r.encode("utf_8")])
def test_purchase_ticket(self): blockchain = dcrdata.DcrdataBlockchain(":memory:", testnet, "https://testnet.dcrdata.org") try: blockchain.connect() def broadcast(txHex): print("test skipping broadcast of transaction: %s" % txHex) return True blockchain.broadcast = broadcast txs = {} def getTx(txid): return txs[txid] blockchain.tx = getTx addrs = [] keys = {} def internal(): privKey = Curve.generateKey() pkHash = crypto.hash160(privKey.pub.serializeCompressed().b) addr = addrlib.AddressPubKeyHash(pkHash, testnet) addrs.append(addr) keys[addr.string()] = privKey return addr.string() def priv(addr): return keys[addr] class KeySource: def priv(self, *a): return priv(*a) def internal(self): return internal() def utxosource(amt, filter): nextVal = 10 total = 0 utxos = [] while total < amt: atoms = int(nextVal * 1e8) privKey = Curve.generateKey() pkHash = crypto.hash160( privKey.pub.serializeCompressed().b) addr = addrlib.AddressPubKeyHash(pkHash, testnet) addrs.append(addr) addrString = addr.string() keys[addrString] = privKey pkScript = txscript.makePayToAddrScript( addrString, testnet) txHash = rando.newHash() txid = reversed(txHash).hex() utxos.append( account.UTXO( address=addrString, txHash=txHash, vout=0, ts=int(time.time()), scriptPubKey=pkScript, satoshis=atoms, )) tx = msgtx.MsgTx.new() tx.addTxOut(msgtx.TxOut(value=atoms, pkScript=pkScript)) txs[txid] = tx total += atoms nextVal *= 2 return utxos, True poolPriv = Curve.generateKey() pkHash = crypto.hash160(poolPriv.pub.serializeCompressed().b) poolAddr = addrlib.AddressPubKeyHash(pkHash, testnet) scriptHash = crypto.hash160("some script. doesn't matter".encode()) scriptAddr = addrlib.AddressScriptHash(scriptHash, testnet) ticketPrice = blockchain.stakeDiff() request = account.TicketRequest( minConf=0, expiry=0, spendLimit=ticketPrice * 2 * 1.1, poolAddress=poolAddr.string(), votingAddress=scriptAddr.string(), ticketFee=0, poolFees=7.5, count=2, txFee=0, ) ticket, spent, newUTXOs = blockchain.purchaseTickets( KeySource(), utxosource, request) finally: blockchain.close()