예제 #1
0
    def _generate_signature(self, tx_hash: bytes) -> Signature:
        _, _, signature_der = blocksec2go.generate_signature(
            self._reader, self._key_id, tx_hash)
        self._logger.debug('Generated signature')

        rs_sig = _utils.sigdecode_der(signature_der)
        v = _utils.get_v(rs_sig, tx_hash, self._pub_key, self._chain_id)
        r, s = rs_sig

        return v, r, s
예제 #2
0
def generateSignature(reader, json_object=None):
    if json_object is None:
        hash = (hashlib.sha256(b'Hash' +
                               bytearray(os.urandom(10000)))).digest()
    else:
        block_string = json.dumps(json_object, sort_keys=True)
        hash_object = hashlib.sha256(block_string.encode())
        hash = hash_object.digest()
    try:
        global_counter, counter, signature = blocksec2go.generate_signature(
            reader, 1, hash)
        return hash, signature
    except:
        return None, None
def _generate_signature(args):
    reader = args.reader
    if args.pin is not None:
        verify_pin(reader, args.pin)
    (global_counter, counter,
     signature) = generate_signature(reader, args.key_id, args.hash)

    if args.machine_readable:
        json.dump(
            {
                'status': 'success',
                'global_counter': global_counter,
                'counter': counter,
                'signature': signature.hex()
            },
            fp=sys.stdout)
    else:
        print('Remaining signatures with card: {}'.format(global_counter))
        print('Remaining signatures with key {}: {}'.format(
            args.key_id, counter))
        print('Signature (hex): ' + signature.hex())
예제 #4
0
 def _generate_signature_der(self, data):
     _, _, signature = blocksec2go.generate_signature(
         self.reader, self.key_id, data)
     self.logger.debug('generated signature')
     return signature
예제 #5
0
public_key = get_public_key( reader, key_id )
inf_card_addr = w3.toChecksumAddress( w3.keccak( public_key[1:] )[-20:].hex() )
print( f'Address of account {key_id} on Infineon card: { inf_card_addr }' )
print( f'Balance of account {key_id} on Infineon card: { w3.fromWei( w3.eth.getBalance( inf_card_addr ), "ether" ) }' )
print()

# create the transaction as a dictionary
nonce = w3.eth.getTransactionCount( inf_card_addr )
transaction = { 'to': w3.eth.accounts[0], 'value': w3.toWei( 1, "ether" ), 'gasPrice': w3.eth.gasPrice, 'nonce': nonce, 'chainId': w3.eth.chainId }
transaction['gas'] = w3.eth.estimateGas( transaction )

# serialize the transaction with the RLP encoding scheme
unsigned_encoded_transaction = serializable_unsigned_transaction_from_dict( transaction )

# sign the hash of the serialized transaction
global_counter, counter, signature = blocksec2go.generate_signature( reader, key_id, bytes( unsigned_encoded_transaction.hash() ) )
print( "Remaining signatures with card:", global_counter )
print( f"Remaining signatures with key {key_id}:", counter )
print( "Signature (hex):" + signature.hex() )
print()

# Extract r and s from the signature
try: 
	r, s = get_signature_components( signature )
except: 
	print( "Invalid signature components!" )
	raise SystemExit()
	
# determine the signature prefix value 
v = get_signature_prefix( ( r, s ), inf_card_addr, bytes( unsigned_encoded_transaction.hash() ), w3.eth.chainId )
		
        print('ERROR: ' + str(details))
        raise SystemExit


if ('__main__' == __name__):
    reader = get_reader()
    activate_card(reader)

    hash_object = hashlib.sha256(b'Hello World!')
    hash = hash_object.digest()
    print('Hashed message:', hash.hex())

    key_id = int(
        input('Which key would you like to use? (Numbers 1 - 255 only!)\n'))
    public_key = get_public_key(reader, key_id)

    try:
        global_counter, counter, signature = blocksec2go.generate_signature(
            reader, key_id, hash)
        print('Remaining signatures with card: ', global_counter)
        print('Remaining signatures with key 1: ', counter)
        print('Signature (hex): ' + signature.hex())

        print('Is signature correct?',
              blocksec2go.verify_signature(public_key, hash, signature))

    except crypto_except.InvalidSignature:
        print('Verification failed!')
    except Exception as details:
        print('ERROR: ' + str(details))
        raise SystemExit
예제 #7
0
 def generate_signature_der(self, data):
     _, _, signature = blocksec2go.generate_signature(
         self.reader, self.key_id, data)
     return signature