def test_to_bytes(self, sk_ilp, vk_ilp): sk = SigningKey(sk_ilp['b58']) assert sk.encode(encoding='base58') == sk_ilp['b58'] assert sk.encode(encoding='base64') == sk_ilp['b64'] vk = VerifyingKey(vk_ilp['b58']) assert vk.encode(encoding='base58') == vk_ilp['b58'] assert vk.encode(encoding='base64') == vk_ilp['b64']
def test_to_bytes(self, sk_ilp, vk_ilp): sk = SigningKey(sk_ilp['b58']) assert sk.encode(encoding='base58') == sk_ilp['b58'] assert sk.encode(encoding='base64') == sk_ilp['b64'] vk = VerifyingKey(vk_ilp['b58']) assert vk.encode(encoding='base58') == vk_ilp['b58'] assert vk.encode(encoding='base64') == vk_ilp['b64']
def test_sign_verify(self, sk_ilp, vk_ilp): message = b'Hello World!' sk = SigningKey(sk_ilp['b58']) vk = VerifyingKey(vk_ilp['b58']) assert vk.verify(message, sk.sign(message)) is True assert vk.verify(message, sk.sign(message + b'dummy')) is False assert vk.verify(message + b'dummy', sk.sign(message)) is False vk = VerifyingKey(vk_ilp[2]['b64'], encoding='base64') assert vk.verify(message, sk.sign(message)) is False
def test_sign_verify(self, sk_ilp, vk_ilp): message = b'Hello World!' sk = SigningKey(sk_ilp['b58']) vk = VerifyingKey(vk_ilp['b58']) assert vk.verify(message, sk.sign(message)) is True assert vk.verify(message, sk.sign(message + b'dummy')) is False assert vk.verify(message + b'dummy', sk.sign(message)) is False vk = VerifyingKey(vk_ilp[2]['b64'], encoding='base64') assert vk.verify(message, sk.sign(message)) is False
def test_ilp_keys(self, sk_ilp, vk_ilp): sk = SigningKey(sk_ilp['b58']) assert sk.encode(encoding='base64') == sk_ilp['b64'] assert binascii.hexlify( sk.encode(encoding='bytes')[:32]) == sk_ilp['hex'] vk = VerifyingKey(vk_ilp['b58']) assert vk.encode(encoding='base64') == vk_ilp['b64'] assert binascii.hexlify(vk.encode(encoding='bytes')) == vk_ilp['hex']
def test_generate_sign_verify(self, vk_ilp): sk_b58, vk_b58 = ed25519_generate_key_pair() sk = SigningKey(sk_b58) vk = VerifyingKey(vk_b58) message = b'Hello World!' assert vk.verify(message, sk.sign(message)) is True assert vk.verify(message, sk.sign(message + b'dummy')) is False assert vk.verify(message + b'dummy', sk.sign(message)) is False vk = VerifyingKey(vk_ilp[2]['b64'], encoding='base64') assert vk.verify(message, sk.sign(message)) is False
def test_generate_sign_verify(self, vk_ilp): sk_b58, vk_b58 = ed25519_generate_key_pair() sk = SigningKey(sk_b58) vk = VerifyingKey(vk_b58) message = b'Hello World!' assert vk.verify(message, sk.sign(message)) is True assert vk.verify(message, sk.sign(message + b'dummy')) is False assert vk.verify(message + b'dummy', sk.sign(message)) is False vk = VerifyingKey(vk_ilp[2]['b64'], encoding='base64') assert vk.verify(message, sk.sign(message)) is False
def test_sign_verify(self, sk_ilp, vk_ilp): message = 'Hello World!' sk = SigningKey(sk_ilp['b58']) vk = VerifyingKey(vk_ilp['b58']) assert vk.verify(message, sk.sign(message)) is True assert vk.verify(message, sk.sign(message + 'dummy')) is False assert vk.verify(message + 'dummy', sk.sign(message)) is False vk = VerifyingKey( VerifyingKey.encode( base64_add_padding(vk_ilp[2]['b64']))) assert vk.verify(message, sk.sign(message)) is False
def sign_transaction(transaction, *, public_key, private_key): ed25519 = Ed25519Fulfillment(public_key=public_key) message = json.dumps( transaction, sort_keys=True, separators=(',', ':'), ensure_ascii=False, ) ed25519.sign(message.encode(), Ed25519SigningKey(private_key)) return ed25519.serialize_uri()
def persisted_transfer_dimi_car_to_ewy(dimi_keypair, ewy_pubkey, transactions_api_full_url, persisted_transfer_carol_car_to_dimi): output_txid = persisted_transfer_carol_car_to_dimi['id'] ed25519_ewy = Ed25519Fulfillment(public_key=ewy_pubkey) transaction = { 'asset': { 'id': persisted_transfer_carol_car_to_dimi['asset']['id'] }, 'metadata': None, 'operation': 'TRANSFER', 'outputs': ({ 'amount': 1, 'condition': { 'details': ed25519_ewy.to_dict(), 'uri': ed25519_ewy.condition_uri, }, 'public_keys': (ewy_pubkey, ), }, ), 'inputs': ({ 'fulfillment': None, 'fulfills': { 'output': 0, 'txid': output_txid, }, 'owners_before': (dimi_keypair.public_key, ), }, ), 'version': '0.9', } serialized_transaction_without_id = json.dumps( transaction, sort_keys=True, separators=(',', ':'), ensure_ascii=False, ).encode() transaction['id'] = sha3_256(serialized_transaction_without_id).hexdigest() serialized_transaction_with_id = json.dumps( transaction, sort_keys=True, separators=(',', ':'), ensure_ascii=False, ).encode() ed25519_dimi = Ed25519Fulfillment(public_key=dimi_keypair.public_key) ed25519_dimi.sign(serialized_transaction_with_id, Ed25519SigningKey(dimi_keypair.private_key)) transaction['inputs'][0]['fulfillment'] = ed25519_dimi.serialize_uri() response = requests.post(transactions_api_full_url, json=transaction) return response.json()
def test_fulfill(self, driver, alice_keypair, unsigned_transaction): signed_transaction = driver.transactions.fulfill( unsigned_transaction, private_keys=alice_keypair.sk) unsigned_transaction['inputs'][0]['fulfillment'] = None message = json.dumps( unsigned_transaction, sort_keys=True, separators=(',', ':'), ensure_ascii=False, ).encode() ed25519 = Ed25519Fulfillment(public_key=alice_keypair.vk) ed25519.sign(message, Ed25519SigningKey(alice_keypair.sk)) fulfillment_uri = ed25519.serialize_uri() assert signed_transaction['inputs'][0]['fulfillment'] == fulfillment_uri # noqa
# # # This is to confirm that my company has agreed to and accepted the BigchainDB Entity Contributor Agreement # # at https://www.bigchaindb.com/cla/ and to represent and warrant that I have authority to do so. # # # # USq6mSPklezAirGcWi6ntfAlG2OhEobGhnNIstM6xH4UCZGX5KgXQemE4wLk0R6 # ################################################################################################################ import binascii import base58 import cryptoconditions as cc from cryptoconditions.crypto import Ed25519SigningKey as SigningKey message = 'Hello World! Conditions are here!' sk_b58 = base58.b58encode(binascii.unhexlify('833fe62409237b9d62ec77587520911e9a759cec1d19755b7da901b96dca3d42')) sk = SigningKey(sk_b58) ed25519_fulfillment = cc.Ed25519Sha256() ed25519_fulfillment.sign(message.encode(), base58.b58decode(sk.encode())) print(ed25519_fulfillment.condition_uri) # prints 'ni:///sha-256;U1YhFdW0lOI-SVF3PbDP4t_lVefj_-tB5P11yvfBaoE?fpt=ed25519-sha-256&cost=131072' print(ed25519_fulfillment.serialize_uri()) # prints 'pGSAIOwXK5OtXlY79JMscOEkUDTDVGfvLv1NZOv4GWg0Z-K_gUC2IpH62UMvjymLnEpIldvik_b_2hpo2t8Mze9fR6D' \ # 'HISpf6jzal6P0wD6p8uisHOyGpR1FISer26CdG28zHAcK' fulfillment_uri = 'pGSAIOwXK5OtXlY79JMscOEkUDTDVGfvLv1NZOv4GWg0Z-K_gUC2IpH62UMvjymLnEpIldvik_b_' \ '2hpo2t8Mze9fR6DHISpf6jzal6P0wD6p8uisHOyGpR1FISer26CdG28zHAcK' condition_uri = 'ni:///sha-256;U1YhFdW0lOI-SVF3PbDP4t_lVefj_-tB5P11yvfBaoE?fpt=ed25519-sha-256&cost=131072' fulfillment = cc.Fulfillment.from_uri(fulfillment_uri)
def test_get_verifying_key(self, sk_ilp, vk_ilp): sk = SigningKey(sk_ilp['b58']) vk = VerifyingKey(vk_ilp['b58']) vk_from_sk = sk.get_verifying_key() assert vk.encode(encoding='bytes') == vk_from_sk.encode(encoding='bytes')
def test_signing_key_decode(self, sk_ilp): private_value = SigningKey.decode(sk_ilp['b58']) assert private_value == base64_add_padding(sk_ilp['b64'])
def test_get_verifying_key(self, sk_ilp, vk_ilp): sk = SigningKey(sk_ilp['b58']) vk = VerifyingKey(vk_ilp['b58']) vk_from_sk = sk.get_verifying_key() assert vk.to_bytes() == vk_from_sk.to_bytes()
def test_signing_key_decode(self, sk_ilp): sk = SigningKey(sk_ilp['b58']) private_value = sk.encode(encoding='base64') assert private_value == sk_ilp['b64']
def test_signing_key_init(self, sk_ilp): sk = SigningKey(sk_ilp['b58']) assert sk.encode(encoding='base64') == sk_ilp['b64'] assert sk.encode(encoding='bytes') == sk_ilp['byt']
def test_signing_key_encode(self, sk_ilp): sk = SigningKey(sk_ilp['b64'], encoding='base64') private_value_base58 = sk.encode(encoding="base58") assert private_value_base58 == sk_ilp['b58']
def test_generate_key_pair(self): sk_b58, vk_b58 = ed25519_generate_key_pair() assert len(base58.b58decode(sk_b58)) == 32 assert len(base58.b58decode(vk_b58)) == 32 assert SigningKey.encode(SigningKey.decode(sk_b58)) == sk_b58 assert VerifyingKey.encode(VerifyingKey.decode(vk_b58)) == vk_b58
# condition details were passed around off-chain, needs signing # otherwise load the condition from the condition details in the output of the input_tx fulfillment = \ Fulfillment.from_dict( tx_transfer_custom_condition_signed['outputs'][0]['condition']['details'] ) assert condition.condition_uri == fulfillment.condition_uri # message_to_sign is the serialized transaction without the fulfillments message_to_sign = get_message_to_sign(tx_transfer_custom_fulfillment) # bob & carly create the fulfillment # by signing the message with their private key # fulfillment is updated by reference subcondition_bob = get_subcondition(fulfillment, public_key=bob.public_key) subcondition_bob.sign(message_to_sign, Ed25519SigningKey(bob.private_key)) # not valid yet, still a signature and a secret needed assert condition.validate(message_to_sign) is False subcondition_carly = get_subcondition(fulfillment, public_key=carly.public_key) subcondition_carly.sign(message_to_sign, Ed25519SigningKey(carly.private_key)) # not valid yet, still a secret needed assert condition.validate(message_to_sign) is False # update a subcondition with a new subfulfillment in a nested fulfillment # ie replace the hashlock condition by its secret fulfillment # fulfill_subcondition returns a new fulfillment with the updated subfulfillment fulfillment = \ fulfill_subcondition( fulfillment, condition_uri=get_subcondition(fulfillment, type_id=PreimageSha256Fulfillment.TYPE_ID).serialize_uri(), new_subfulfillment=PreimageSha256Fulfillment(preimage=b'secret')
import binascii import base58 import cryptoconditions as cc from cryptoconditions.crypto import Ed25519SigningKey as SigningKey message = 'Hello World! Conditions are here!' sk_b58 = base58.b58encode(binascii.unhexlify('833fe62409237b9d62ec77587520911e9a759cec1d19755b7da901b96dca3d42')) sk = SigningKey(sk_b58) ed25519_fulfillment = cc.Ed25519Sha256() ed25519_fulfillment.sign(message, sk) print(ed25519_fulfillment.condition_uri) # prints 'cc:4:20:7Bcrk61eVjv0kyxw4SRQNMNUZ-8u_U1k6_gZaDRn4r8:96' print(ed25519_fulfillment.serialize_uri()) # prints 'cf:4:7Bcrk61eVjv0kyxw4SRQNMNUZ-8u_U1k6_gZaDRn4r-2IpH62UMvjymLnEpIldvik_b_2hpo2t8Mze9fR6DHISpf6jzal6P0wD6p8ui # 'sHOyGpR1FISer26CdG28zHAcK' fulfillment_uri = 'cf:4:7Bcrk61eVjv0kyxw4SRQNMNUZ-8u_U1k6_gZaDRn4r-2IpH62UMvjymLnEpIldvik_b_2hpo2t8Mze9fR6DHISpf6jzal' \ '6P0wD6p8uisHOyGpR1FISer26CdG28zHAcK' condition_uri = 'cc:4:20:7Bcrk61eVjv0kyxw4SRQNMNUZ-8u_U1k6_gZaDRn4r8:96' fulfillment = cc.Fulfillment.from_uri(fulfillment_uri) result = fulfillment.validate(message) and condition_uri == fulfillment.condition_uri print(result) sk_b58 = base58.b58encode(binascii.unhexlify('1a3ab1a87f000348f391613930cc49529652ecf2d2c7cadfd96e87a7f6de948a')) sk = SigningKey(sk_b58)
def test_signing_key_encode(self, sk_ilp): sk = SigningKey(sk_ilp['b64'], encoding='base64') private_value_base58 = sk.encode(encoding="base58") assert private_value_base58 == sk_ilp['b58']
def test_signing_key_init(self, sk_ilp): sk = SigningKey(sk_ilp['b58']) assert sk.to_ascii(encoding='base64') == sk_ilp['b64'] assert sk.to_seed() == sk_ilp['byt']
def test_signing_key_init(self, sk_ilp): sk = SigningKey(sk_ilp['b58']) assert sk.encode(encoding='base64') == sk_ilp['b64'] assert sk.encode(encoding='bytes') == sk_ilp['byt']
def test_get_verifying_key(self, sk_ilp, vk_ilp): sk = SigningKey(sk_ilp['b58']) vk = VerifyingKey(vk_ilp['b58']) vk_from_sk = sk.get_verifying_key() assert vk.encode(encoding='bytes') == vk_from_sk.encode( encoding='bytes')
def test_signing_key_decode(self, sk_ilp): sk = SigningKey(sk_ilp['b58']) private_value = sk.encode(encoding='base64') assert private_value == sk_ilp['b64']
def create_transfer(prev_tx, vw_sk, vw_pk, vehicle_id, metadata, asset_id): prev_tx = Transaction.from_dict(prev_tx) # Create asset VW -> [VW, TEL] # Custom crypto condition multisig 1-2 threshold_fulfillment = ThresholdSha256Fulfillment(threshold=1) vw_fulfillment = Ed25519Fulfillment(public_key=vw_pk) tel_fulfillment = Ed25519Fulfillment(public_key=vehicle_id) threshold_fulfillment.add_subfulfillment(vw_fulfillment) threshold_fulfillment.add_subfulfillment(tel_fulfillment) output = { 'amount': 1, 'condition': { 'details': threshold_fulfillment.to_dict(), 'uri': threshold_fulfillment.condition.serialize_uri() }, 'public_keys': [vw_pk, vehicle_id], } # The yet to be fulfilled input: input_ = { 'fulfillment': None, 'fulfills': { 'txid': prev_tx.id, 'output': 0, }, 'owners_before': [vw_pk, vehicle_id], } # Craft the payload: transfer_tx = { 'operation': 'TRANSFER', 'asset': { 'id': asset_id }, 'metadata': metadata, 'outputs': [output], 'inputs': [input_], 'version': '0.9', } # Generate the id, by hashing the encoded json formatted string # representation of the transaction: json_str_tx = json.dumps( transfer_tx, sort_keys=True, separators=(',', ':'), ensure_ascii=False, ) txid = sha3.sha3_256(json_str_tx.encode()).hexdigest() transfer_tx['id'] = txid # Sign the transaction: message = json.dumps( transfer_tx, sort_keys=True, separators=(',', ':'), ensure_ascii=False, ) threshold_fulfillment = ThresholdSha256Fulfillment(threshold=1) vw_fulfillment.sign(message.encode(), private_key=Ed25519SigningKey(vw_sk)) threshold_fulfillment.add_subfulfillment(vw_fulfillment) threshold_fulfillment.add_subcondition(tel_fulfillment.condition) fulfillment_uri = threshold_fulfillment.serialize_uri() transfer_tx['inputs'][0]['fulfillment'] = fulfillment_uri return Transaction.from_dict(transfer_tx)
def test_signing_key_encode(self, sk_ilp): private_value_base58 = SigningKey.encode(base64_add_padding(sk_ilp['b64'])) assert private_value_base58 == sk_ilp['b58']