def withdrawfromvault(self, fromvaultaddress, toaddress, amount): vault = self.getvault(fromvaultaddress) received = self.chaindb.listreceivedbyvault(fromvaultaddress) received = received.values()[0] if received['value'] < amount + 2 * utils.calculate_fees(None): self.logger.warning("Insufficient funds in vault, exiting, return") return # create transaction tx = CTransaction() # to the receiver txout = CTxOut() txout.nValue = amount txout.scriptPubKey = utils.address_to_pay_to_pubkey_hash(toaddress) tx.vout.append(txout) # from the sender nValueIn = 0 nValueOut = amount txin = CTxIn() txin.prevout = COutPoint() txin.prevout.hash = received['txhash'] txin.prevout.n = received['n'] txin.scriptSig = received['scriptPubKey'] tx.vin.append(txin) # calculate nValueIn nValueIn = received['value'] # calculate the total excess amount excessAmount = nValueIn - nValueOut # calculate the fees fees = 2 * utils.calculate_fees(tx) # create change transaction, if there is any change left if excessAmount > fees: change_txout = CTxOut() change_txout.nValue = excessAmount - fees account = self.getaccount() changeaddress = fromvaultaddress self.logger.debug("Change address: %s" % changeaddress) change_txout.scriptPubKey = \ utils.vault_address_to_pay_to_vault_script(changeaddress) tx.vout.append(change_txout) # calculate txhash tx.calc_sha256() txhash = str(tx.sha256) key = CKey() key.set_pubkey(vault['public_key']) key.set_privkey(vault['private_key']) signature = key.sign(txhash) vaultscript = utils.create_vault_script(vault['address'], \ vault['master_address'], vault['timeout'], vault['maxfees']) scriptSig = chr(OP_VAULT_WITHDRAW) + chr(len(signature)) + signature + \ chr(len(vaultscript)) + vaultscript self.logger.debug("Adding signature: %s" % binascii.hexlify(scriptSig)) txin.scriptSig = scriptSig return tx
def overridevaulttx(self, fromvaultaddress, toaddress): vault = self.getvault(fromvaultaddress) # select the input addresses received = self.chaindb.listallreceivedbyvault(fromvaultaddress) if not received: self.logger.warning("Empty vault, exiting, return") return None, None received = received.values()[0] if received['value'] < 2 * utils.calculate_fees(None): self.logger.warning("Insufficient funds in vault, exiting, return") return None, None # calculate remaining amount amount = received['value'] - 2 * utils.calculate_fees(None) # create transaction tx = CTransaction() # to the receiver txout = CTxOut() txout.nValue = amount txout.scriptPubKey = utils.address_to_pay_to_pubkey_hash(toaddress) tx.vout.append(txout) # from the sender nValueIn = 0 nValueOut = amount txin = CTxIn() txin.prevout = COutPoint() txin.prevout.hash = received['txhash'] txin.prevout.n = received['n'] txin.scriptSig = received['scriptPubKey'] tx.vin.append(txin) # calculate nValueIn nValueIn = received['value'] # calculate the total excess amount excessAmount = nValueIn - nValueOut # calculate the fees fees = utils.calculate_fees(tx) # calculate txhash tx.calc_sha256() txhash = str(tx.sha256) key = CKey() key.set_pubkey(vault['public_key']) key.set_privkey(vault['private_key']) signature = key.sign(txhash) # get the script vaultscript = utils.create_vault_script(vault['address'], \ vault['master_address'], vault['timeout'], vault['maxfees']) scriptSig = chr(OP_VAULT_OVERRIDE) + chr(len(vault['master_public_key'])) + \ vault['master_public_key'] + chr(len(signature)) + signature + \ chr(len(vaultscript)) + vaultscript self.logger.debug("Adding signature: %s" % binascii.hexlify(scriptSig)) txin.scriptSig = scriptSig return amount, tx
def sign(jsonbuf, privkey, role, extra_keys=[]): ''' :type jsonbuf: str. :param privkey: private key in DER form :type privkey: str. :type role: str. :type extra_keys: list. :returns: dict - JSON with signature appended. :raises: BadJSONError ''' jsondoc = json.loads(jsonbuf) signed_keys = jsondoc.get('signed_keys', []) sigdict = {} for key in signed_keys + extra_keys: # FIXME:в ключе допустимы только [a-zA-Z0-9_.\-] # FIXME:можно исключить числа с плавающей точкой value = jsondoc.get(key, None) if not value: raise BadJSONError('Missing attribute: %s' % (repr(key), )) sigdict[key] = value if not len(sigdict): raise BadJSONError('No attributes to sign') instr = json.dumps(sigdict, separators=(',', ':'), sort_keys=True) hash = hashlib.sha256(hashlib.sha256(instr).digest()).digest() ckey = CKey() ckey.set_privkey(privkey) signed = ckey.sign(hash) sigentry = { 'role': role, 'signature_type': 'secp256k1', 'pubkey': binascii.hexlify(ckey.get_pubkey()), 'signature': binascii.hexlify(signed) } if extra_keys: sigentry['extra_signed_keys'] = extra_keys jsondoc.setdefault('signatures', []).append(sigentry) return jsondoc
def sign(jsonbuf, privkey, role, extra_keys=[]): ''' :type jsonbuf: str. :param privkey: private key in DER form :type privkey: str. :type role: str. :type extra_keys: list. :returns: dict - JSON with signature appended. :raises: BadJSONError ''' jsondoc = json.loads(jsonbuf) signed_keys = jsondoc.get('signed_keys', []) sigdict = {} for key in signed_keys + extra_keys: # FIXME:в ключе допустимы только [a-zA-Z0-9_.\-] # FIXME:можно исключить числа с плавающей точкой value = jsondoc.get(key, None) if not value: raise BadJSONError('Missing attribute: %s' % (repr(key),)) sigdict[key] = value if not len(sigdict): raise BadJSONError('No attributes to sign') instr = json.dumps(sigdict, separators=(',',':'), sort_keys=True) hash = hashlib.sha256(hashlib.sha256(instr).digest()).digest() ckey = CKey() ckey.set_privkey(privkey) signed = ckey.sign(hash) sigentry = { 'role': role, 'signature_type': 'secp256k1', 'pubkey': binascii.hexlify(ckey.get_pubkey()), 'signature': binascii.hexlify(signed) } if extra_keys: sigentry['extra_signed_keys'] = extra_keys jsondoc.setdefault('signatures', []).append(sigentry) return jsondoc
def sendtoaddress(self, toaddress, amount): # select the input addresses funds = 0 subaccounts = [] accounts = self.getaccounts() for account in accounts: for address, subaccount in account.iteritems(): if subaccount['balance'] == 0: continue else: subaccounts.append(subaccount) # print "got one subaccount", subaccount # print "subaccounts: ", subaccounts funds = funds + subaccount['balance'] if funds >= amount + utils.calculate_fees(None): break # print "subaccounts 2: ", subaccounts # incase of insufficient funds, return if funds < amount + utils.calculate_fees(None): print "In sufficient funds, exiting, return" return # create transaction tx = CTransaction() # print "subaccounts 3: ", subaccounts # to the receiver txout = CTxOut() txout.nValue = amount txout.scriptPubKey = utils.address_to_pay_to_pubkey_hash(toaddress) tx.vout.append(txout) # from the sender nValueIn = 0 nValueOut = amount public_keys = [] private_keys = [] # secrets = [] # print "subaccounts 4: ", subaccounts for subaccount in subaccounts: # print "subaccount: ", subaccount # get received by from address previous_txouts = subaccount['received'] # print "Previous txouts", previous_txouts for received in previous_txouts: txin = CTxIn() txin.prevout = COutPoint() txin.prevout.hash = received['txhash'] txin.prevout.n = received['n'] txin.scriptSig = binascii.unhexlify(received['scriptPubKey']) tx.vin.append(txin) nValueIn = nValueIn + received['value'] public_keys.append(subaccount['public_key']) private_keys.append(subaccount['private_key']) # secrets.append(subaccount['secret']) if nValueIn >= amount + utils.calculate_fees(tx): break if nValueIn >= amount + utils.calculate_fees(tx): break # calculate the total excess amount excessAmount = nValueIn - nValueOut # calculate the fees fees = utils.calculate_fees(tx) # create change transaction, if there is any change left if excessAmount > fees: change_txout = CTxOut() change_txout.nValue = excessAmount - fees changeaddress = subaccounts[0]['address'] change_txout.scriptPubKey = utils.address_to_pay_to_pubkey_hash(changeaddress) tx.vout.append(change_txout) # calculate txhash tx.calc_sha256() txhash = str(tx.sha256) # sign the transaction for public_key, private_key, txin in zip(public_keys, private_keys, tx.vin): key = CKey() key.set_pubkey(public_key) key.set_privkey(private_key) signature = key.sign(txhash) # scriptSig = chr(len(signature)) + hash_type + signature + chr(len(public_key)) + public_key scriptSig = chr(len(signature)) + signature + chr(len(public_key)) + public_key print "Adding signature: ", binascii.hexlify(scriptSig) txin.scriptSig = scriptSig print "Tx Validity: ", tx.is_valid() return tx
def sendtovault(self, vault_address, amount): # select the input addresses funds = 0 subaccounts = [] accounts = self.getaccounts() for account in accounts: for address, subaccount in account.iteritems(): if subaccount['balance'] == 0: continue else: subaccounts.append(subaccount) funds = funds + subaccount['balance'] if funds >= amount + utils.calculate_fees(None): break # incase of insufficient funds, return if funds < amount + utils.calculate_fees(None): self.logger.warning("Insufficient funds, exiting, return") raise exceptions.InsufficientBalanceException # create transaction tx = CTransaction() # to the receiver txout = CTxOut() txout.nValue = amount txout.scriptPubKey = utils.vault_address_to_pay_to_vault_script(vault_address) tx.vout.append(txout) # from the sender nValueIn = 0 nValueOut = amount public_keys = [] private_keys = [] for subaccount in subaccounts: # get received by from address previous_txouts = subaccount['received'] for received in previous_txouts: txin = CTxIn() txin.prevout = COutPoint() txin.prevout.hash = received['txhash'] txin.prevout.n = received['n'] txin.scriptSig = received['scriptPubKey'] tx.vin.append(txin) nValueIn = nValueIn + received['value'] public_keys.append(subaccount['public_key']) private_keys.append(subaccount['private_key']) if nValueIn >= amount + utils.calculate_fees(tx): break if nValueIn >= amount + utils.calculate_fees(tx): break # calculate the total excess amount excessAmount = nValueIn - nValueOut # calculate the fees fees = utils.calculate_fees(tx) # create change transaction, if there is any change left if excessAmount > fees: change_txout = CTxOut() change_txout.nValue = excessAmount - fees changeaddress = self.getnewaddress()[1] change_txout.scriptPubKey = utils.address_to_pay_to_pubkey_hash(changeaddress) tx.vout.append(change_txout) # calculate txhash tx.calc_sha256() txhash = str(tx.sha256) self.logger.debug("Sending to vault %064x" % tx.sha256) # sign the transaction for public_key, private_key, txin in zip(public_keys, private_keys, tx.vin): key = CKey() key.set_pubkey(public_key) key.set_privkey(private_key) signature = key.sign(txhash) # scriptSig = chr(len(signature)) + hash_type + signature + chr(len(public_key)) + public_key scriptSig = chr(len(signature)) + signature + chr(len(public_key)) + public_key self.logger.debug("Adding signature: %s" % binascii.hexlify(scriptSig)) txin.scriptSig = scriptSig self.logger.debug("Tx Validity: %064x" % tx.is_valid()) # push data to vault tx.calc_sha256() self.set(str("vault:" + vault_address), {'txhash': tx.sha256}) return (vault_address, tx)
000000001976a914097072524438d003d23a2f23edb65aae1bb3e46988ac0000000001000000""" # print x dhash = binascii.unhexlify( "9302bda273a887cb40c13e02a50b4071a31fd3aae3ae04021b0b843dd61ad18e") PRIVATE_KEY = 0x18E14A7B6A307F426A94F8114701E7C8E774E7F9A47E2C2035DB29A206321725 HEX_TRANSACTION = "010000000126c07ece0bce7cda0ccd14d99e205f118cde27e83dd75da7b141fe487b5528fb000000008b48304502202b7e37831273d74c8b5b1956c23e79acd660635a8d1063d413c50b218eb6bc8a022100a10a3a7b5aaa0f07827207daf81f718f51eeac96695cf1ef9f2020f21a0de02f01410452684bce6797a0a50d028e9632be0c2a7e5031b710972c2a3285520fb29fcd4ecfb5fc2bf86a1e7578e4f8a305eeb341d1c6fc0173e5837e2d3c7b178aade078ffffffff02b06c191e010000001976a9143564a74f9ddb4372301c49154605573d7d1a88fe88ac00e1f505000000001976a914010966776006953d5567439e5e39f86a0d273bee88ac00000000" #output to redeem. must exist in HEX_TRANSACTION k = CKey() k.generate(('%064x' % PRIVATE_KEY).decode('hex')) #here we retrieve the public key data generated from the supplied private key pubkey_data = k.get_pubkey() #then we create a signature over the hash of the signature-less transaction signed_data = k.sign(dhash) print binascii.hexlify(signed_data) """ # Add four-byte version field: 01000000 # One-byte varint specifying the number of inputs: 01 # 32-byte hash of the transaction from which we want to redeem an output: eccf7e3034189b851985d871f91384b8ee357cd47c3024736e5676eb2debb3f2 # Four-byte field denoting the output index we want to redeem from the transaction with the above hash (output number 2 = output index 1): 01000000 Now comes the scriptSig. For the purpose of signing the transaction, this is temporarily filled with the scriptPubKey of the output we want to redeem. First we write a one-byte varint which denotes the length of the scriptSig (0x19 = 25 bytes): 19 Then we write the actual scriptSig (which is the scriptPubKey of the output we want to redeem): 76a914010966776006953d5567439e5e39f86a0d273bee88ac Then we write a four-byte field denoting the sequence. This is currently always set to 0xffffffff: ffffffff # Next comes a one-byte varint containing the number of outputs in our new transaction. We will set this to 1 in this example: 01 # We then write an 8-byte field (64 bit integer) containing the amount we want to redeem from the specified output. I will set this to the total amount available # in the output minus a fee of 0.001 BTC (0.999 BTC, or 99900000 Satoshis): 605af40500000000 # Then we start writing our transaction's output. We start with a one-byte varint denoting the length of the output script (0x19 or 25 bytes): 19 # Then the actual output script: 76a914097072524438d003d23a2f23edb65aae1bb3e46988ac # Then we write the four-byte "lock time" field: 00000000
0000001976a914010966776006953d5567439e5e39f86a0d273bee88acffffffff01605af405 000000001976a914097072524438d003d23a2f23edb65aae1bb3e46988ac0000000001000000""" # print x dhash = binascii.unhexlify("9302bda273a887cb40c13e02a50b4071a31fd3aae3ae04021b0b843dd61ad18e") PRIVATE_KEY=0x18E14A7B6A307F426A94F8114701E7C8E774E7F9A47E2C2035DB29A206321725 HEX_TRANSACTION="010000000126c07ece0bce7cda0ccd14d99e205f118cde27e83dd75da7b141fe487b5528fb000000008b48304502202b7e37831273d74c8b5b1956c23e79acd660635a8d1063d413c50b218eb6bc8a022100a10a3a7b5aaa0f07827207daf81f718f51eeac96695cf1ef9f2020f21a0de02f01410452684bce6797a0a50d028e9632be0c2a7e5031b710972c2a3285520fb29fcd4ecfb5fc2bf86a1e7578e4f8a305eeb341d1c6fc0173e5837e2d3c7b178aade078ffffffff02b06c191e010000001976a9143564a74f9ddb4372301c49154605573d7d1a88fe88ac00e1f505000000001976a914010966776006953d5567439e5e39f86a0d273bee88ac00000000" #output to redeem. must exist in HEX_TRANSACTION k = CKey() k.generate(('%064x' % PRIVATE_KEY).decode('hex')) #here we retrieve the public key data generated from the supplied private key pubkey_data = k.get_pubkey() #then we create a signature over the hash of the signature-less transaction signed_data = k.sign(dhash) print binascii.hexlify(signed_data) """ # Add four-byte version field: 01000000 # One-byte varint specifying the number of inputs: 01 # 32-byte hash of the transaction from which we want to redeem an output: eccf7e3034189b851985d871f91384b8ee357cd47c3024736e5676eb2debb3f2 # Four-byte field denoting the output index we want to redeem from the transaction with the above hash (output number 2 = output index 1): 01000000 Now comes the scriptSig. For the purpose of signing the transaction, this is temporarily filled with the scriptPubKey of the output we want to redeem. First we write a one-byte varint which denotes the length of the scriptSig (0x19 = 25 bytes): 19 Then we write the actual scriptSig (which is the scriptPubKey of the output we want to redeem): 76a914010966776006953d5567439e5e39f86a0d273bee88ac Then we write a four-byte field denoting the sequence. This is currently always set to 0xffffffff: ffffffff # Next comes a one-byte varint containing the number of outputs in our new transaction. We will set this to 1 in this example: 01 # We then write an 8-byte field (64 bit integer) containing the amount we want to redeem from the specified output. I will set this to the total amount available # in the output minus a fee of 0.001 BTC (0.999 BTC, or 99900000 Satoshis): 605af40500000000 # Then we start writing our transaction's output. We start with a one-byte varint denoting the length of the output script (0x19 or 25 bytes): 19 # Then the actual output script: 76a914097072524438d003d23a2f23edb65aae1bb3e46988ac
def sendtovault(self, vault_address, amount): # select the input addresses funds = 0 subaccounts = [] accounts = self.getaccounts() for account in accounts: for address, subaccount in account.iteritems(): if subaccount['balance'] == 0: continue else: subaccounts.append(subaccount) funds = funds + subaccount['balance'] if funds >= amount + utils.calculate_fees(None): break # incase of insufficient funds, return if funds < amount + utils.calculate_fees(None): self.logger.warning("Insufficient funds, exiting, return") raise exceptions.InsufficientBalanceException # create transaction tx = CTransaction() # to the receiver txout = CTxOut() txout.nValue = amount txout.scriptPubKey = utils.vault_address_to_pay_to_vault_script( vault_address) tx.vout.append(txout) # from the sender nValueIn = 0 nValueOut = amount public_keys = [] private_keys = [] for subaccount in subaccounts: # get received by from address previous_txouts = subaccount['received'] for received in previous_txouts: txin = CTxIn() txin.prevout = COutPoint() txin.prevout.hash = received['txhash'] txin.prevout.n = received['n'] txin.scriptSig = received['scriptPubKey'] tx.vin.append(txin) nValueIn = nValueIn + received['value'] public_keys.append(subaccount['public_key']) private_keys.append(subaccount['private_key']) if nValueIn >= amount + utils.calculate_fees(tx): break if nValueIn >= amount + utils.calculate_fees(tx): break # calculate the total excess amount excessAmount = nValueIn - nValueOut # calculate the fees fees = utils.calculate_fees(tx) # create change transaction, if there is any change left if excessAmount > fees: change_txout = CTxOut() change_txout.nValue = excessAmount - fees changeaddress = self.getnewaddress()[1] change_txout.scriptPubKey = utils.address_to_pay_to_pubkey_hash( changeaddress) tx.vout.append(change_txout) # calculate txhash tx.calc_sha256() txhash = str(tx.sha256) self.logger.debug("Sending to vault %064x" % tx.sha256) # sign the transaction for public_key, private_key, txin in zip(public_keys, private_keys, tx.vin): key = CKey() key.set_pubkey(public_key) key.set_privkey(private_key) signature = key.sign(txhash) # scriptSig = chr(len(signature)) + hash_type + signature + chr(len(public_key)) + public_key scriptSig = chr(len(signature)) + signature + chr( len(public_key)) + public_key self.logger.debug("Adding signature: %s" % binascii.hexlify(scriptSig)) txin.scriptSig = scriptSig self.logger.debug("Tx Validity: %064x" % tx.is_valid()) # push data to vault tx.calc_sha256() self.set(str("vault:" + vault_address), {'txhash': tx.sha256}) return (vault_address, tx)
def sendtoaddress(self, toaddress, amount): # select the input addresses funds = 0 subaccounts = [] accounts = self.getaccounts() for account in accounts: for address, subaccount in account.iteritems(): if subaccount['balance'] == 0: continue else: subaccounts.append(subaccount) funds = funds + subaccount['balance'] if funds >= amount + utils.calculate_fees(None): break # incase of insufficient funds, return if funds < amount + utils.calculate_fees(None): self.logger.warning("Insufficient funds, exiting, return") return None, None # create transaction tx = CTransaction() # to the receiver txout = CTxOut() txout.nValue = amount txout.scriptPubKey = utils.address_to_pay_to_pubkey_hash(toaddress) tx.vout.append(txout) # from the sender nValueIn = 0 nValueOut = amount public_keys = [] private_keys = [] # secrets = [] for subaccount in subaccounts: # get received by from address previous_txouts = subaccount['received'] for received in previous_txouts: txin = CTxIn() txin.prevout = COutPoint() txin.prevout.hash = received['txhash'] txin.prevout.n = received['n'] txin.scriptSig = received['scriptPubKey'] tx.vin.append(txin) nValueIn = nValueIn + received['value'] public_keys.append(subaccount['public_key']) private_keys.append(subaccount['private_key']) # secrets.append(subaccount['secret']) if nValueIn >= amount + utils.calculate_fees(tx): break if nValueIn >= amount + utils.calculate_fees(tx): break # calculate the total excess amount excessAmount = nValueIn - nValueOut # calculate the fees fees = utils.calculate_fees(tx) # create change transaction, if there is any change left if excessAmount > fees: change_txout = CTxOut() change_txout.nValue = excessAmount - fees changeaddress = subaccounts[0]['address'] change_txout.scriptPubKey = utils.address_to_pay_to_pubkey_hash( changeaddress) tx.vout.append(change_txout) # calculate txhash tx.calc_sha256() txhash = str(tx.sha256) # sign the transaction for public_key, private_key, txin in zip(public_keys, private_keys, tx.vin): key = CKey() key.set_pubkey(public_key) key.set_privkey(private_key) signature = key.sign(txhash) # scriptSig = chr(len(signature)) + hash_type + signature + chr(len(public_key)) + public_key scriptSig = chr(len(signature)) + signature + chr( len(public_key)) + public_key txin.scriptSig = scriptSig return amount, tx