コード例 #1
0
def generateRS(addressA, addressC, sendAmount, dappData, gasPrice, gasLimit):
    '''
    Internal Function
    Calculates R & S in a way that

    0 < R < secp256k1n
    0 < S < secp256k1n / 2

    if not recursively runs itself til satisfied \_:)_/

    :param addressA: Sender's Address
    :param addressC: Smart Contracts Address
    :param sendAmount: Send Amount (in Wei)
    :param data: Data for smart contract
    :return:

    commit, randw, R, S

    '''
    #TODO: validate AddressA and AddressC
    commit, randw = generateCommit(addressA, addressC, sendAmount, dappData,
                                   gasPrice, gasLimit)

    R = bytearray_to_int(sha3_256(commit + bytes(1)))
    S = bytearray_to_int(sha3_256(commit + bytes(0)))

    if (0 < R < secp256k1n) & (0 < S < (secp256k1n / 2)):
        return commit, randw, R, S
    else:
        log.info("Invalid R,S. Regenerating the hashes...")
        return generateRS(addressA, addressC, sendAmount, dappData, gasPrice,
                          gasLimit)
コード例 #2
0
def generateCommit(addressA, addressC, sendAmount, dappData, gasPrice,
                   gasLimit):
    '''
    Internal Function
    Generates a random number (w for witness) and calculates the Keccak256 hash of (AddressA | Address C | sendAmount | data | w)
    Called from generateRS()
    :param addressA: Sender's Address
    :param addressC: Smart Contracts Address
    :param sendAmount: Send Amount (in Wei)
    :param data: Data for smart contract
    :return:

    FullCommit : Keccak256 (sha3_256) hash of full commit (AddressA | Address C | sendAmount | data | w)
    w: Random number w for witness

    '''
    rand_gen = random.SystemRandom()  # This uses os.urandom() . Secure enough?
    w = bytes([rand_gen.randrange(256)
               for _ in range(256 // 8)])  # random bytes

    def aux(x):
        return x.to_bytes(32, byteorder='big')

    fullCommit = (addressA + addressC + aux(sendAmount) + dappData + w +
                  aux(gasPrice) + aux(gasLimit))

    return sha3_256(fullCommit), w
コード例 #3
0
    def test_dishonest_party(self):

        ADDR_A = rec_hex(t.a1)
        PKEY_A = t.k1
        ADDR_B = rec_hex(t.a2)
        PKEY_B = t.k2
        DAPP_ADDRESS = "0xDEADbEeF000000000000000000000000DeaDbeEf"
        fake_tx_commit_object = transactions.Transaction(
            0, OURGASPRICE, OURGASLIMIT, ADDR_B,
            (UNLOCK_AMOUNT + extraTransactionFees), b'').sign(PKEY_A)

        self.chain.direct_tx(fake_tx_commit_object)
        self.chain.mine(1)
        witness = "0x03"
        fakecommitBlockNumber, fakecommitBlockIndex = self.chain.chain.get_tx_position(
            fake_tx_commit_object)
        print("tx block number {} and tx block index {}".format(
            fakecommitBlockNumber, fakecommitBlockIndex))

        def _listener(log):
            print('LOG:', )
            print('LOG:', log)
            print(rec_hex(log['data']))

        self.chain.head_state.log_listeners.append(_listener)

        self.verifier_contract.reveal(fakecommitBlockNumber,
                                      fakecommitBlockIndex,
                                      DAPP_ADDRESS,
                                      UNLOCK_AMOUNT,
                                      b'',
                                      rec_bin(witness),
                                      OURGASPRICE,
                                      OURGASLIMIT,
                                      sender=PKEY_A,
                                      to=self.verifier_contract.address,
                                      value=1009,
                                      gasprice=OURGASPRICE,
                                      startgas=OURGASLIMIT)
        self.chain.mine(1)

        #revealBlockNumber, revealBlockIndex = self.chain.chain.get_tx_position()
        #print("reveal block number {} and reveal block index {}".format(revealBlockNumber, revealBlockIndex))

        def aux(x):
            return x.to_bytes(32, byteorder='big')

        computedfakecommit = (rec_bin(ADDR_A) +
                              self.verifier_contract.address +
                              aux(UNLOCK_AMOUNT) + b'' + rec_bin(witness) +
                              aux(OURGASPRICE) + aux(OURGASLIMIT))
        sessionID = sha3_256(computedfakecommit)
        print(rec_hex(sessionID))

        revealTestSession = self.verifier_contract.getSession(
            rec_bin(
                "000000000000000000000000000000000000000000000000128dfa6a90b28000"
            ))
        print(revealTestSession)
コード例 #4
0
ファイル: utils.py プロジェクト: kevjue/plasma-dex
def hashPersonalMessage(message_hex):
    message = bytearray.fromhex(u.remove_0x_head(message_hex))
    prefix = u.str_to_bytes(
        chr(25) + 'Ethereum Signed Message:\n' + str(len(message)))
    return u.sha3_256(prefix + decode_hex(u.remove_0x_head(message_hex)))