Beispiel #1
0
def vote_helper(api_url = 'http://big-indexer-1:50000/api',
                via_cli = False,
                ):
    """
    USAGE: python offchain.py vote_helper user_pub_key user_priv_key vote_secret vote_json
    """
    
    user_key = sys.argv[2]
    secret_key = sys.argv[3]
    vote_secret = sys.argv[4]
    rq = sys.argv[5]
    
    rq = json.loads(rq)
    
    num_votes = len(rq['votes'])
        
    sig1 = hmac.new(vote_secret, dumps_compact(rq), hashlib.sha512).hexdigest()
    
    post_data = dumps_compact({'command':'vote_blind', 'sig':sig1, 'num_votes':num_votes, 'nonce':int(time()*1000)})
    
    sig2 = hmac.new(secret_key, post_data, hashlib.sha512).hexdigest()
    
    print ('REQUEST:')
    print ("curl -S " + api_url + " -d " + pipes.quote(post_data) + ' -H ' + pipes.quote('Sig: ' + sig2) + ' -H ' + pipes.quote('Key: ' + user_key))
Beispiel #2
0
def auth_test(rq,
              user_key,
              secret_key,
              api_url = 'http://127.0.0.1:50000/api',
              ):
    """
    HMAC authenticated calls, with nonce.
    """
    rq['nonce'] = int(time()*1000)
    #post_data = urllib.urlencode(rq)
    post_data = dumps_compact(rq)
    sig = hmac.new(secret_key, post_data, hashlib.sha512).hexdigest()
    headers = {'Sig': sig,
               'Key': user_key,
               }
    
    print ("REQUEST:\n\ncurl -S " + api_url + " -d " + pipes.quote(post_data) + ' -H ' + pipes.quote('Sig: ' + headers['Sig']) + ' -H ' + pipes.quote('Key: ' + headers['Key']))

    return

    ret = urllib2.urlopen(urllib2.Request(api_url, post_data, headers))
    hh = json.loads(ret.read())
    return hh
Beispiel #3
0
def test_1(via_cli=False):
    """
    Test CCCoin logging and rewards functions.
    """

    code = \
    """
    pragma solidity ^0.4.6;

    contract CCCoinToken {
        event TheLog(bytes);
        function addLog(bytes val) payable { 
            TheLog(val);
        }
    }
    """

    cw = ContractWrapper(
        code,
        settings_confirm_states=DEFAULT_CONFIRM_STATES,
    )

    cont_addr = cw.deploy()

    events = [
        {
            'payload_decoded': {
                u'num_items': 1,
                u'item_type': u'votes',
                u'blind_hash':
                u'59f4132fb7d6e430c591cd14a9d1423126dca1ec3f75a3ea1ebed4d2d4454471',
                u'command': u'blind',
                u'nonce': 1485934064014
            },
            u'sig': {
                u'sig_s':
                u'492f15906be6bb924e7d9b9d954bc989a14c85f5c3282bb4bd23dbf2ad37c206',
                u'sig_r':
                u'abc17a3e61ed708a34a2af8bfad3270863f4ee02dd0e009e80119262087015d4',
                u'sig_v': 28
            },
            u'payload':
            u'{"command":"blind","item_type":"votes","blind_hash":"59f4132fb7d6e430c591cd14a9d1423126dca1ec3f75a3ea1ebed4d2d4454471","num_items":1,"nonce":1485934064014}',
            u'pub':
            u'f2e642e8a5ead4fc8bb3b8776b949e52b23317f1e6a05e99619330cca0fc6f87de28131e696ba7f9d9876d99c952e3ccceda6d3324cdfaf5452cf8ea01372dc1'
        },
        {
            'payload_decoded': {
                u'nonce': 1485934064181,
                u'item_type': u'votes',
                u'blind_hash':
                u'59f4132fb7d6e430c591cd14a9d1423126dca1ec3f75a3ea1ebed4d2d4454471',
                u'blind_reveal':
                u'{"votes":[{"item_id":"1","direction":0}],"rand":"tLKFUfvh0McIDUhr"}',
                u'command': u'unblind'
            },
            u'sig': {
                u'sig_s':
                u'7d0e0d70f1d440e86487881893e27f12192dd23549daa4dc89bb4530aee35c3b',
                u'sig_r':
                u'9f074305e710c458ee556f7c6ba236cc57869ad9348c75ce1a47094b9dbaa6dc',
                u'sig_v': 28
            },
            u'payload':
            u'{"command":"unblind","item_type":"votes","blind_hash":"59f4132fb7d6e430c591cd14a9d1423126dca1ec3f75a3ea1ebed4d2d4454471","blind_reveal":"{\\"votes\\":[{\\"item_id\\":\\"1\\",\\"direction\\":0}],\\"rand\\":\\"tLKFUfvh0McIDUhr\\"}","nonce":1485934064181}',
            u'pub':
            u'f2e642e8a5ead4fc8bb3b8776b949e52b23317f1e6a05e99619330cca0fc6f87de28131e696ba7f9d9876d99c952e3ccceda6d3324cdfaf5452cf8ea01372dc1'
        },
    ]

    #events = ['test' + str(x) for x in xrange(3)]

    events = [dumps_compact(x) for x in events[-1:]]

    for xx in events:
        cw.send_transaction(
            'addLog(bytes)',
            [xx],
            block=True,
            gas_limit=1000000,
            gas_price=100,
            value=web3.utils.currency.to_wei(1, 'ether'),
        )
        if False:
            print('SIZE', len(xx))
            tx = cw.c.call_with_transaction(
                cw.c.eth_coinbase(),
                cw.contract_address,
                'addLog(bytes)',
                [xx],
                gas=1000000,
                gas_price=100,
                value=web3.utils.currency.to_wei(1, 'ether'),
            )
            receipt = cw.c.eth_getTransactionReceipt(
                tx)  ## blocks to ensure transaction is mined

    def cb(msg, receipt, confirm_level):
        msg['data'] = solidity_string_decode(msg['data'])
        print('GOT_LOG:')
        print json.dumps(msg, indent=4)

    #cw.events_callback = cb

    cc2 = CCCoinCore(
        cw,
        settings_rewards=CORE_SETTINGS,
        #offline_testing_mode = True,
    )

    cw.events_callback = cc2.process_event

    logs = cw.poll_incoming()

    if False:
        print('XXXXXXXXX')
        params = {
            "fromBlock": "0x01",
            "address": cw.contract_address,
        }
        filter = str(cw.c.eth_newFilter(params))

        for xlog in cw.c.eth_getFilterLogs(filter):
            print json.dumps(xlog, indent=4)