Beispiel #1
0
def create_asset_hashlock(bigchain, payload, secret):
    # Create a hash-locked asset without any new_owners
    hashlock_tx = bigchain.create_transaction(bigchain.me,
                                              None,
                                              None,
                                              'CREATE',
                                              payload=payload)

    hashlock_tx_condition = cc.PreimageSha256Fulfillment(
        preimage=secret.encode())

    # The conditions list is empty, so we need to append a new condition
    hashlock_tx['transaction']['conditions'].append({
        'condition': {
            'details': hashlock_tx_condition.to_dict(),
            'uri': hashlock_tx_condition.condition.serialize_uri()
        },
        'cid': 0,
        'new_owners': None
    })

    # Conditions have been updated, so hash needs updating
    hashlock_tx['id'] = bigchaindb.util.get_hash_data(hashlock_tx)

    # The asset needs to be signed by the current_owner
    hashlock_tx_signed = bigchain.sign_transaction(hashlock_tx,
                                                   bigchain.me_private)

    bigchain.validate_transaction(hashlock_tx_signed)
    # write the transaction to the bigchain
    bigchain.write_transaction(hashlock_tx_signed)
    return hashlock_tx_signed
import cryptoconditions as cc

SPACER = '*'*40

################################
# Find a good secret and hash it
################################
secret = '42'.encode()  # use bytes only
puzzle = binascii.hexlify(hashlib.sha256(secret).digest())
print(SPACER)
print('The puzzle is {}, guess the secret!'.format(puzzle))
print(SPACER)


###############################################################
# Create a Hashlock fulfillment and broadcast the condition URI
###############################################################
sha256fulfillment = cc.PreimageSha256Fulfillment(preimage=secret)
sha256condition = sha256fulfillment.condition

print(SPACER)
print('Fulfillment URI: {}'.format(sha256fulfillment.serialize_uri()))
print('hashed payload: {}'.format(binascii.hexlify(sha256condition.hash)))
print('Condition URI: {}'.format(sha256fulfillment.condition_uri))
print(SPACER)

##########################
# Validate the fulfillment
##########################
assert sha256fulfillment.validate() and sha256fulfillment.condition_uri == sha256condition.serialize_uri()
dimi = post_account('dimi').json()
mark = post_account('mark').json()

# The quizmaster receives a Question Token
tx = b.create_transaction(quizmaster['vk'], None, None, 'CREATE')
response = post_tx(tx)
tx_received = response.json()
print(response.status_code)
poll_tx_status_until_valid(tx_received['id'])

# The quizmaster creates a question
question = {'question': 'What is the answer to life the universe and everything?'}
question_tx = b.create_transaction(quizmaster['vk'], None, {'txid': tx_received['id'], 'cid': 0}, 'TRANSFER')
# Define a secret that will be hashed - fulfillments need to guess the secret
answer = b'42'
hashlock_tx_condition = cc.PreimageSha256Fulfillment(preimage=answer)

# The conditions list is empty, so we need to append a new condition
question_tx['transaction']['conditions'].append({
    'condition': {
        'uri': hashlock_tx_condition.condition.serialize_uri()
    },
    'cid': 0,
    'owners_after': None
})

# Conditions have been updated, so hash needs updating
question_tx['id'] = get_hash_data(question_tx)
question_tx_signed = b.sign_transaction(question_tx, quizmaster['sk'])

# POST the transaction to BigchainDB
SPACER = '*' * 40

# Create some keys and a message to sign
sk1, vk1 = cc.crypto.ed25519_generate_key_pair()
sk2, vk2 = cc.crypto.ed25519_generate_key_pair()
message = 'Hello World! I am a message to sign!'

##############################################################
# Create a Threshold fulfillment and
# add subfulfillments and subconditions as an object or by URI
##############################################################
# Create a 1-of-4 threshold condition (OR gate)
threshold_fulfillment = cc.ThresholdSha256Fulfillment(threshold=1)
# Add a hashlock fulfillment
threshold_fulfillment.add_subfulfillment(
    cc.PreimageSha256Fulfillment(b'much secret'))
# Add a signature condition
threshold_fulfillment.add_subcondition(
    cc.Ed25519Fulfillment(public_key=vk1).condition)
# Add a fulfillment URI
threshold_fulfillment.add_subfulfillment_uri('cf:0:')
# Add a condition URI
threshold_fulfillment.add_subcondition_uri(
    'cc:0:3:47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU:0')
print(SPACER)
print('Condition URI: {}'.format(threshold_fulfillment.condition_uri))
print('Threshold: {}'.format(threshold_fulfillment.threshold))
print('Is valid fulfillment? {}'.format(
    threshold_fulfillment.validate(message)))
if threshold_fulfillment.validate():
    print('Fulfillment URI {}'.format(threshold_fulfillment.serialize_uri()))
Beispiel #5
0
import array

import binascii

import cryptoconditions as cc

my_fulfillment = cc.PreimageSha256Fulfillment('')
print(my_fulfillment.condition_uri)
# prints 'cc:0:3:47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU:0'
print(my_fulfillment.serialize_uri())
# prints 'cf:0:'

my_fulfillment = cc.PreimageSha256Fulfillment(b'\x00')
print(my_fulfillment.condition_uri)
# prints 'cc:0:3:bjQLnP-zepicpUTmu3gKLHiQHT-zNzh2hRGjBhevoB0:1'
print(my_fulfillment.serialize_uri())
# prints 'cf:0:AA'

my_fulfillment = cc.PreimageSha256Fulfillment(b'\xff')
print(my_fulfillment.condition_uri)
# prints 'cc:0:3:qBAK5qoZQNC2Y7sxzUZhQuu9vVGHExuS2TgYmHgy64k:1'
print(my_fulfillment.serialize_uri())
# prints 'cf:0:_w'

my_fulfillment = cc.PreimageSha256Fulfillment(b'\xfe\xff')
print(my_fulfillment.condition_uri)
# prints 'cc:0:3:8ZdpKBDUV-KX_OnFZTsCWB_5mlCFI3DynX5f5H2dN-Y:2'
print(my_fulfillment.serialize_uri())
# prints 'cf:0:_v8'

my_fulfillment = cc.PreimageSha256Fulfillment(b'\xff\xfe')