def getinfo(args): for filename in args: print "----------------" print "Filename: ", filename privateKey = readPrivateKey(filename) k = Key() k.setPrivateKey(privateKey) print "Public key: ", k.getPublicKey().encode("hex") print "Address: ", getAddress(k)
def sendToMultiSigPubKey(bitcoind, amount, toPubKey1, toPubKey2, changeHash, fee): """ Make a transaction to send funds from ourself to a 2-of-2 multi-signature scriptPubKey. The transaction is returned, and is NOT published on the Bitcoin network by this function. Arguments: bitcoind: Bitcoind; the bitcoin daemon from which to retrieve this information amount: int; the amount to be sent, not including the fee (in Satoshi) toPubKey1: str; the first public key toPubKey2: str; the second public key changeHash: str; the SHA256- and RIPEMD160-hashed public key to which any change should be sent (equivalent to the bitcoin address) fee: int; the transaction fee (in Satoshi) Return value: Transaction; the transaction that sends funds as specified. Exceptions: Exception: insufficient funds """ totalIn, inputs = getInputsForAmount(bitcoind, amount+fee) change = totalIn - fee - amount #print "%d -> %d, %d, %d" % (totalIn, amount, change, fee) tx = Transaction( tx_in = [ TxIn(x[0], x[1]) for x in inputs ], tx_out = [ TxOut(amount, Script.multiSigPubKey([toPubKey1, toPubKey2])), TxOut(change, Script.standardPubKey(changeHash)) ] ) for i in range(len(inputs)): scriptPubKey = Script.deserialize(inputs[i][2]) key = Key() key.setPrivateKey(inputs[i][3]) tx.signInput(i, scriptPubKey, [None, key.getPublicKey()], [key]) return tx
def sendToDataPubKey(bitcoind, data, changeHash, fee): """ Make a transaction to publish data on the block chain with an OP_RETURN output. No funds are sent to the OP_RETURN output: everything goes to fee and to the change address. The transaction is returned, and is NOT published on the Bitcoin network by this function. Arguments: bitcoind: Bitcoind; the bitcoin daemon from which to retrieve this information data: str; the data to be included in the scriptPubKey (max. 40 bytes) changeHash: str; the SHA256- and RIPEMD160-hashed public key to which any change should be sent (equivalent to the bitcoin address) fee: int; the transaction fee (in Satoshi) Return value: Transaction; the transaction that sends funds as specified. Exceptions: Exception: insufficient funds """ totalIn, inputs = getInputsForAmount(bitcoind, fee) change = totalIn - fee print "%d -> %d, %d" % (totalIn, change, fee) tx = Transaction( tx_in = [ TxIn(x[0], x[1]) for x in inputs ], tx_out = [ TxOut(0, Script.dataPubKey(data)), TxOut(change, Script.standardPubKey(changeHash)) ] ) for i in range(len(inputs)): scriptPubKey = Script.deserialize(inputs[i][2]) key = Key() key.setPrivateKey(inputs[i][3]) tx.signInput(i, scriptPubKey, [None, key.getPublicKey()], [key]) return tx
def sendToMultiSigPubKey(bitcoind, amount, toPubKey1, toPubKey2, changeHash, fee): """ Make a transaction to send funds from ourself to a 2-of-2 multi-signature scriptPubKey. The transaction is returned, and is NOT published on the Bitcoin network by this function. Arguments: bitcoind: Bitcoind; the bitcoin daemon from which to retrieve this information amount: int; the amount to be sent, not including the fee (in Satoshi) toPubKey1: str; the first public key toPubKey2: str; the second public key changeHash: str; the SHA256- and RIPEMD160-hashed public key to which any change should be sent (equivalent to the bitcoin address) fee: int; the transaction fee (in Satoshi) Return value: Transaction; the transaction that sends funds as specified. Exceptions: Exception: insufficient funds """ totalIn, inputs = getInputsForAmount(bitcoind, amount + fee) change = totalIn - fee - amount #print "%d -> %d, %d, %d" % (totalIn, amount, change, fee) tx = Transaction(tx_in=[TxIn(x[0], x[1]) for x in inputs], tx_out=[ TxOut(amount, Script.multiSigPubKey([toPubKey1, toPubKey2])), TxOut(change, Script.standardPubKey(changeHash)) ]) for i in range(len(inputs)): scriptPubKey = Script.deserialize(inputs[i][2]) key = Key() key.setPrivateKey(inputs[i][3]) tx.signInput(i, scriptPubKey, [None, key.getPublicKey()], [key]) return tx
def sendToDataPubKey(bitcoind, data, changeHash, fee): """ Make a transaction to publish data on the block chain with an OP_RETURN output. No funds are sent to the OP_RETURN output: everything goes to fee and to the change address. The transaction is returned, and is NOT published on the Bitcoin network by this function. Arguments: bitcoind: Bitcoind; the bitcoin daemon from which to retrieve this information data: str; the data to be included in the scriptPubKey (max. 40 bytes) changeHash: str; the SHA256- and RIPEMD160-hashed public key to which any change should be sent (equivalent to the bitcoin address) fee: int; the transaction fee (in Satoshi) Return value: Transaction; the transaction that sends funds as specified. Exceptions: Exception: insufficient funds """ totalIn, inputs = getInputsForAmount(bitcoind, fee) change = totalIn - fee print "%d -> %d, %d" % (totalIn, change, fee) tx = Transaction(tx_in=[TxIn(x[0], x[1]) for x in inputs], tx_out=[ TxOut(0, Script.dataPubKey(data)), TxOut(change, Script.standardPubKey(changeHash)) ]) for i in range(len(inputs)): scriptPubKey = Script.deserialize(inputs[i][2]) key = Key() key.setPrivateKey(inputs[i][3]) tx.signInput(i, scriptPubKey, [None, key.getPublicKey()], [key]) return tx
address1 = base58.encodeBase58Check(keyHash1, 0) address2 = base58.encodeBase58Check(keyHash2, 0) print address1 print address2 #Note: this will fail, unless you change toe above addresses to some of your own privKey1 = base58.decodeBase58Check(d.getPrivateKey(address1), 128) privKey2 = base58.decodeBase58Check(d.getPrivateKey(address2), 128) key1 = Key() key1.setPrivateKey(privKey1) key2 = Key() key2.setPrivateKey(privKey2) print key1.getPublicKey().encode("hex") print key2.getPublicKey().encode("hex") amount = int(100000 * float(raw_input("Amount to be transferred (mBTC): "))) fee = 10000 #0.1 mBTC outputHash = binascii.unhexlify(raw_input("Input hash (empty: create multisig): "))[::-1] if outputHash == "": tx = sendToMultiSigPubKey(d, amount, key1.getPublicKey(), key2.getPublicKey(), keyHash2, fee=fee) else:
from crypto import Key, SHA256, RIPEMD160 import base58 def readPrivateKey(filename): with open(filename, "rb") as f: privateKey = f.read() privateKey = privateKey.split("\n")[0] #first line privateKey = privateKey.strip() #ignore whitespace return base58.decodeBase58Check(privateKey, 128) #PRIVKEY = 128 def getTraditionalAddress(key): publicKeyHash = RIPEMD160(SHA256(key.getPublicKey())) return base58.encodeBase58Check(publicKeyHash, 0) #PUBKEY_ADDRESS = 0 # get traditional address using a public key (given a private key) privateKey = readPrivateKey('key.txt') k = Key() k.setPrivateKey(privateKey) trad_address = getTraditionalAddress(k) print "Public key: ", k.getPublicKey().encode("hex") print "Public Key Hash " + RIPEMD160(SHA256(k.getPublicKey())).encode("hex") print "Traditional Address: ", trad_address