Skip to content
This repository was archived by the owner on May 22, 2018. It is now read-only.

Crypto condition example fail #12

Closed
gesaleh opened this issue Nov 1, 2017 · 23 comments
Closed

Crypto condition example fail #12

gesaleh opened this issue Nov 1, 2017 · 23 comments
Assignees

Comments

@gesaleh
Copy link

gesaleh commented Nov 1, 2017

Crypto condition examples failed due to driver and missing lib from python
Seems the bigchaindb evolved while example didn't

Any update on this?

@sbellem
Copy link
Contributor

sbellem commented Nov 1, 2017

Could you say which examples? And if possible provide a stack trace.

@gesaleh
Copy link
Author

gesaleh commented Nov 1, 2017

I'm trying to execute this example
04_cryptoconditions_transactions/cryptoconditions_transactions.py

 PYTHONPATH=. BDB_SERVER_URL='http://localhost:49984' python3 04_cryptoconditions_transactions/cryptoconditions_transactions.py
{'software': 'BigchainDB', 'public_key': 'GW1nrdZm4mbVC8ePeiGWz6DqHexqewqy5teURVHi3RG4', 'keyring': [], '_links': {'docs': 'https://docs.bigchaindb.com/projects/server/en/v0.11.0.dev/', 'api_v1': 'http://localhost:49984/api/v1/'}, 'version': '0.11.0.dev'}
Posting signed transaction{'outputs': [{'amount': 1, 'public_keys': ['3xen1aXjYt8AFy5VAV5uqZS1fUd1aEqaRqFPf5oeKyHb'], 'condition': {'details': {'bitmask': 32, 'public_key': '3xen1aXjYt8AFy5VAV5uqZS1fUd1aEqaRqFPf5oeKyHb', 'signature': None, 'type_id': 4, 'type': 'fulfillment'}, 'uri': 'cc:4:20:K_kIJ0rjM8bo6W4zELT47pZQGgS0jpYF99oQV6YE2oI:96'}}], 'asset': {'data': None}, 'metadata': None, 'inputs': [{'owners_before': ['3xen1aXjYt8AFy5VAV5uqZS1fUd1aEqaRqFPf5oeKyHb'], 'fulfillment': 'cf:4:K_kIJ0rjM8bo6W4zELT47pZQGgS0jpYF99oQV6YE2oJfE35W3k2JUSMhiDpUI7ZbrrI-UieQo6viqt0olVkIPdm20SEOK9u-ssrjQYWCyu3nJNyU9PK9TGbQConFUEEO', 'fulfills': None}], 'operation': 'CREATE', 'id': 'd25189e5ab909421ea6df4d7cfe30de5ece53bb38c3b7aa7736e0d9d53c3b728', 'version': '0.9'}
Traceback (most recent call last):
  File "04_cryptoconditions_transactions/cryptoconditions_transactions.py", line 52, in <module>
    bdb.transactions.send(tx_create_alice_divisible_signed)
  File "/home/gas/kyber/drivers/python/bigchaindb_driver/driver.py", line 305, in send
    method='POST', path=self.path, json=transaction, headers=headers)
  File "/home/gas/kyber/drivers/python/bigchaindb_driver/transport.py", line 58, in forward_request
    headers=headers,
  File "/home/gas/kyber/drivers/python/bigchaindb_driver/connection.py", line 57, in request
    raise exc_cls(response.status_code, text, json)
bigchaindb_driver.exceptions.BadRequest: (400, '{\n  "message": "Invalid transaction schema: 1 is not of type \'string\'", \n  "status": 400\n}\n', {'status': 400, 'message': "Invalid transaction schema: 1 is not of type 'string'"})

@sbellem
Copy link
Contributor

sbellem commented Nov 3, 2017

@diminator do you have time to look into this?

@diminator
Copy link
Contributor

it's a problem with the old version of BDB-driver - compatible with BDB 0.5 ot 0.6 I think.
We will migrate the tutorial examples in the near future (on a different repo)

@manolodewiner can you take note?

@future-is-present future-is-present self-assigned this Nov 4, 2017
@gesaleh
Copy link
Author

gesaleh commented Nov 4, 2017

thanks
is there any example that exists for cryptocondition ? with 3 parties

@diminator
Copy link
Contributor

diminator commented Nov 10, 2017

Here are some untested bits and pieces of code

// at the output of the transaction to-be-spent
let threshold = 2
let thresholdCondition = driver.Transaction.makeThresholdCondition(threshold, undefined, false )
let condition1 = driver.Transaction.makeEd25519Condition(user1.publicKey, false)
thresholdCondition.addSubconditionUri(condition1.getConditionUri())
let condition2 = driver.Transaction.makeEd25519Condition(user2.publicKey, false)
thresholdCondition.addSubconditionUri(condition2.getConditionUri())
let condition3 = driver.Transaction.makeEd25519Condition(user3.publicKey, false)
thresholdCondition.addSubconditionUri(condition3.getConditionUri())
let output = driver.Transaction.makeOutput(thresholdCondition);
output.public_keys = [user1.publicKey, user2.publicKey, user3.publicKey];
let transaction = driver.Transaction.makeCreateTransaction(
    { data: 'payload'},
    { metadata: 'test'},
    [output],
    creator.publicKey
);
// at the input of the spending transaction
let fulfillment1 = driver.Transaction.makeEd25519Condition(user1.publicKey, false)
let fulfillment2 = driver.Transaction.makeEd25519Condition(user2.publicKey, false)
let fulfillment3 = driver.Transaction.makeEd25519Condition(user3.publicKey, false)
fulfillment1.sign(
    new Buffer(driver.Transaction.serializeTransactionIntoCanonicalString(transferTransaction)),
    new Buffer(base58.decode(user1.privateKey))
);
fulfillment3.sign(
    new Buffer(driver.Transaction.serializeTransactionIntoCanonicalString(transferTransaction)),
    new Buffer(base58.decode(user3.privateKey))
);

let fulfillment = driver.Transaction.makeThresholdCondition(1, undefined, false )
fulfillment.addSubfulfillment(fulfillment1);
fulfillment.addSubfulfillment(fulfillment2);
fulfillment.addSubfulfillment(fulfillment3);
let transferTransaction = driver.Transaction.makeTransferTransaction(
    inputTransaction,
    metadata,
    [driver.Transaction.makeOutput(driver.Transaction.makeEd25519Condition(receiver.publicKey))],
    0
);
transferTransaction.inputs[0].fulfillment = fulfillment.serializeUri();

@manolodewiner can probably make this a bit more refined ;-)

@future-is-present
Copy link

future-is-present commented Nov 13, 2017

Here is an example based on the above code:

const Buffer = require('buffer').Buffer
const BigchainDB = require('bigchaindb-driver')
const base58 = require('bs58')
const cryptoconditions = require('crypto-conditions')

const user1 = new BigchainDB.Ed25519Keypair()
const user2 = new BigchainDB.Ed25519Keypair()
const user3 = new BigchainDB.Ed25519Keypair()
const creator = new BigchainDB.Ed25519Keypair()
const receiver = new BigchainDB.Ed25519Keypair()

const API_PATH = 'https://test.ipdb.io/api/v1/'
const conn = new BigchainDB.Connection(API_PATH, {
    app_id: 'xxxxxxxx',
    app_key: 'xxxxxxxxxxxxxxxxxxx'
})

console.log(BigchainDB.Transaction.makeEd25519Condition(user1.publicKey))
// at the output of the transaction to-be-spent
// Generate threshold condition 2 out of 3
const threshold = 2
const condition1 = BigchainDB.Transaction.makeEd25519Condition(user1.publicKey, false)
const condition2 = BigchainDB.Transaction.makeEd25519Condition(user2.publicKey, false)
const condition3 = BigchainDB.Transaction.makeEd25519Condition(user3.publicKey, false)

const thresholdCondition = BigchainDB.Transaction.makeThresholdCondition(threshold, [condition1, condition2, condition3])

console.log(thresholdCondition)
let output = BigchainDB.Transaction.makeOutput(thresholdCondition);
output.public_keys = [user1.publicKey, user2.publicKey, user3.publicKey];

const tx = BigchainDB.Transaction.makeCreateTransaction({
        data: 'payload'
    }, {
        metadata: 'test'
    }, [output],
    creator.publicKey
)


// Sign the transaction with private keys
const txSigned = BigchainDB.Transaction.signTransaction(tx, creator.privateKey)

// Send the transaction off to BigchainDB
const btn = document.getElementById('btn-transfer')
const txtTransfer = document.getElementById('txt-transfer')

conn.postTransaction(txSigned)
    .then(() => conn.pollStatusAndFetchTransaction(txSigned.id))
    .then(res => {
        console.log('Create Transaction', txSigned.id, 'accepted')
        const elem = document.getElementById('lastTransaction')
        elem.href = API_PATH + 'transactions/' + txSigned.id
        elem.innerText = txSigned.id
        btn.disabled = false
    })


btn.onclick = function() {
    txtTransfer.style.visibility = "visible"



    let createTranfer = BigchainDB.Transaction.makeTransferTransaction(
      [{ tx: txSigned,
        output_index: 0
     }],
      [BigchainDB.Transaction.makeOutput(
            BigchainDB.Transaction.makeEd25519Condition(receiver.publicKey))],
      {
          what: "Transfer transaction"
      }
    );


    // at the input of the spending transaction
    let fulfillment1 = BigchainDB.Transaction.makeEd25519Condition(user1.publicKey, false)
    let fulfillment2 = BigchainDB.Transaction.makeEd25519Condition(user2.publicKey, false)
    fulfillment1.sign(
        new Buffer(BigchainDB.Transaction.serializeTransactionIntoCanonicalString(createTranfer)),
        new Buffer(base58.decode(user1.privateKey))
    );
    fulfillment2.sign(
        new Buffer(BigchainDB.Transaction.serializeTransactionIntoCanonicalString(createTranfer)),
        new Buffer(base58.decode(user2.privateKey))
    );

    // 2 out of 3 need to sign the fulfillment. Still condition3 is needed as the "circuit definition" is needed.
    // See https://github.com/bigchaindb/cryptoconditions/issues/94
    let fulfillment = new cryptoconditions.ThresholdSha256()
    fulfillment.threshold = 2
    fulfillment.addSubfulfillment(fulfillment1.serializeUri())
    fulfillment.addSubfulfillment(fulfillment2.serializeUri())
    fulfillment.addSubconditionUri(condition3.getConditionUri())

		//Sign the transaction
    const fulfillmentUri = fulfillment.serializeUri()
    createTranfer.inputs[0].fulfillment = fulfillmentUri

    conn.postTransaction(createTranfer)
        .then(() => conn.pollStatusAndFetchTransaction(createTranfer.id))
        .then(res => {
          	const transfTransaction = document.getElementById('transfTransaction')
            transfTransaction.href = API_PATH + 'transactions/' + createTranfer.id
            transfTransaction.innerText = createTranfer.id
            console.log('Transfer Transaction', createTranfer.id, 'accepted')
        })


}

Fyi: There is also a pull request created to simplify the creation of the fulfillment for the inputs: js-bigchaindb-driver/pull/119

@gesaleh
Copy link
Author

gesaleh commented Nov 19, 2017

Thank you for the example i'll try to translate this to python driver,
question (I'm still new to cryptocondition) , if the exapmle there are 3 conditions and one thresholds = 1
this means transaction is accepted only if 1 ofut 3 accepted ?
what i need is to understand the conditions ? is it like smart contract

my need is :
if user1 and user2 then give user1 10% user 2 80% if only accepted by user3 who get 10%

@future-is-present
Copy link

@gesaleh I do not fully understand your question, could you clarify a little bit more?
Yes, you are right, if you want that 3 out of 3 have to sign the transfer transaction, then you should set threshold to 3.

cc: @sbellem

@future-is-present
Copy link

I have just updated my previous comment with a code example, now using a threshold condition '2 out of 3'

@gesaleh
Copy link
Author

gesaleh commented Nov 20, 2017

Thank you this make it clear for me
But thus also means that condition threshold is about quorum right?
If I want to add a different condition if I transfer to user 1 then price is 10 and if it is user2 then price is 15

Or in my previous message the idea is to use condition and threshold not only to have the signed quorum number but also to add other conditions on the transfer itself

I really appreciate your reactive feedback

@future-is-present
Copy link

Crytpconditions are useful to "describe a signed message such that multiple actors in a distributed system can all verify the same signed message and agree on whether it matches the description".
You are speaking about price which is a different feature and I do not see the point to use cryptoconditions for that. One possible use case of threshold conditions is to have different users with different weights. Imagine the example that 7 votes are needed for an election in a startup, the CEO and CTO have 5 votes each of them, the rest of the employees have 1 vote. That scenario is possible to create with cryptoconditions by creating are/or subconditions.
Does it clarify something for you? I have just created a gitter channel with @sbellem and I have invited you so we can discuss there further.

@imdineshgrewal
Copy link

Hi @manolodewiner ,

while executing the following code :
conn.postTransaction(createTranfer)
.then(() => conn.pollStatusAndFetchTransaction(createTranfer.id))
.then(res => {
const transfTransaction = document.getElementById('transfTransaction')
transfTransaction.href = API_PATH + 'transactions/' + createTranfer.id
transfTransaction.innerText = createTranfer.id
console.log('Transfer Transaction', createTranfer.id, 'accepted')
})

I'm getting an error:
Object
message: "HTTP Error: Requested page not reachable"
requestURI: "https://test.bigchaindb.com/api/v1/transactions"
status: "400 BAD REQUEST"

Please suggest!!
Thanks

@future-is-present
Copy link

Hi @imdineshgrewal do not really know your problem, please try this code with node. If it does not work check that you have the BigchainDB 1.3 version and the latest version of the BigchainDB driver as well

const Buffer = require('buffer').Buffer
const BigchainDB = require('bigchaindb-driver')
const base58 = require('bs58')
const cryptoconditions = require('crypto-conditions')

const user1 = new BigchainDB.Ed25519Keypair()
const user2 = new BigchainDB.Ed25519Keypair()
const user3 = new BigchainDB.Ed25519Keypair()
const creator = new BigchainDB.Ed25519Keypair()
const receiver = new BigchainDB.Ed25519Keypair()

const API_PATH = 'http://localhost:9984/api/v1/'
const conn = new BigchainDB.Connection(API_PATH, {
    app_id: '',
    app_key: ''
})

console.log(BigchainDB.Transaction.makeEd25519Condition(user1.publicKey))
// at the output of the transaction to-be-spent
// Generate threshold condition 2 out of 3
const threshold = 2
const condition1 = BigchainDB.Transaction.makeEd25519Condition(user1.publicKey, false)
const condition2 = BigchainDB.Transaction.makeEd25519Condition(user2.publicKey, false)
const condition3 = BigchainDB.Transaction.makeEd25519Condition(user3.publicKey, false)

const thresholdCondition = BigchainDB.Transaction.makeThresholdCondition(threshold, [condition1, condition2, condition3])

console.log(thresholdCondition)
let output = BigchainDB.Transaction.makeOutput(thresholdCondition);
output.public_keys = [user1.publicKey, user2.publicKey, user3.publicKey];

const tx = BigchainDB.Transaction.makeCreateTransaction({
        data: 'payload'
    }, {
        metadata: 'test'
    }, [output],
    creator.publicKey
)


// Sign the transaction with private keys
const txSigned = BigchainDB.Transaction.signTransaction(tx, creator.privateKey)

// Send the transaction off to BigchainDB

conn.postTransaction(txSigned)
    .then(() => conn.pollStatusAndFetchTransaction(txSigned.id))
    .then(res => {
        console.log('Create Transaction', txSigned.id, 'accepted')
        const elem = document.getElementById('lastTransaction')
        elem.href = API_PATH + 'transactions/' + txSigned.id
        elem.innerText = txSigned.id
        transfer()
    })


function transfer() {



    let createTranfer = BigchainDB.Transaction.makeTransferTransaction(
      [{ tx: txSigned,
        output_index: 0
     }],
      [BigchainDB.Transaction.makeOutput(
            BigchainDB.Transaction.makeEd25519Condition(receiver.publicKey))],
      {
          what: "Transfer transaction"
      }
    );


    // at the input of the spending transaction
    let fulfillment1 = BigchainDB.Transaction.makeEd25519Condition(user1.publicKey, false)
    let fulfillment2 = BigchainDB.Transaction.makeEd25519Condition(user2.publicKey, false)
    fulfillment1.sign(
        new Buffer(BigchainDB.Transaction.serializeTransactionIntoCanonicalString(createTranfer)),
        new Buffer(base58.decode(user1.privateKey))
    );
    fulfillment2.sign(
        new Buffer(BigchainDB.Transaction.serializeTransactionIntoCanonicalString(createTranfer)),
        new Buffer(base58.decode(user2.privateKey))
    );

    // 2 out of 3 need to sign the fulfillment. Still condition3 is needed as the "circuit definition" is needed.
    // See https://github.com/bigchaindb/cryptoconditions/issues/94
    let fulfillment = new cryptoconditions.ThresholdSha256()
    fulfillment.threshold = 2
    fulfillment.addSubfulfillment(fulfillment1.serializeUri())
    fulfillment.addSubfulfillment(fulfillment2.serializeUri())
    fulfillment.addSubconditionUri(condition3.getConditionUri())

		//Sign the transaction
    const fulfillmentUri = fulfillment.serializeUri()
    createTranfer.inputs[0].fulfillment = fulfillmentUri

    conn.postTransaction(createTranfer)
        .then(() => conn.pollStatusAndFetchTransaction(createTranfer.id))
        .then(res => {
          	const transfTransaction = document.getElementById('transfTransaction')
            transfTransaction.href = API_PATH + 'transactions/' + createTranfer.id
            transfTransaction.innerText = createTranfer.id
            console.log('Transfer Transaction', createTranfer.id, 'accepted')
        })


}

@future-is-present
Copy link

Btw, this code is now living here along with other examples:
https://github.com/bigchaindb/project-jannowitz/blob/code-examples/js-examples/crypto-conditions.js

@imdineshgrewal
Copy link

@manolodewiner
Thank you so much, Really appreciate your help! The code is working fine.

@imdineshgrewal
Copy link

Hi @manolodewiner,

How to use the following in the javascript(browser):
const Buffer = require('buffer').Buffer
const BigchainDB = require('bigchaindb-driver')
const base58 = require('bs58')
const cryptoconditions = require('crypto-conditions')
-Thanks

@future-is-present
Copy link

Hi @imdineshgrewal I would like to help you, but I don't understand what you mean by how to use it. Please use our Gitter to ask questions, this is an issue in the Kyber repo.

@imdineshgrewal
Copy link

Hi @manolodewiner , i want to use the following node.js modules in the javascript
Buffer,
bs58,
crypto-conditions
in node we can use them by using "require" but in javascript, I'm not able to understand the usage. Please help - Thanks

@future-is-present
Copy link

Okay, can you please use Gitter.
This is an issue with a completely different purpose.

@imdineshgrewal
Copy link

@manolodewiner Sure, Thanks

@KrishnaPG
Copy link

The line conn.postTransaction(createTranfer) is failing with error:

{
  "message": "Invalid transaction (InvalidHash): The transaction's id 'None' isn't equal to the hash of its body, i.e. it's not valid.", 
  "status": 400
}

The POST request shows createTranfer with id null. How to correct this?
image

@ttmc
Copy link
Contributor

ttmc commented May 22, 2018

This repository (bigchaindb/kyber) contains examples that worked with older versions of BigchainDB, but the stuff in this repository hasn't been maintained and the examples almost certainly won't work with newer versions of BigchainDB. Therefore we are closing all the old issues, marking the repo as old/not-supported, and archiving the repo. You can find newer examples in other places.

@ttmc ttmc closed this as completed May 22, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants