예제 #1
0
class ContractApiTest(unittest.TestCase):
    __conn = None
    __wallet = None

    #CONGTXDtbsMjr1HzopNdttw3cK3SXQYNyWrq

    def setUp(self):
        self.__conn = RPCConnection('127.0.0.1', 12388, 'admin', 'admin')
        self.__conn.open()
        self.__wallet = Wallet("ace2", "ye1320240", self.__conn)

    def tearDown(self):
        self.__wallet.close()
        self.__wallet = None
        self.__conn.close()
        self.__conn = None

    def test_Bets(self):
        contract = Contract(self.__conn, self.__wallet,
                            "CONGTXDtbsMjr1HzopNdttw3cK3SXQYNyWrq")
        entryId, payId = contract.bets(1, 0.1, str(uuid.uuid1()))
        print entryId
        print payId
        self.assertTrue(entryId != '')
        self.assertTrue(payId != '')

    def test_deBets(self):
        contract = Contract(self.__conn, self.__wallet,
                            "CONGTXDtbsMjr1HzopNdttw3cK3SXQYNyWrq")
        entryId = contract.deBets(1, str(uuid.uuid1()))
        print "deBets " + entryId
        self.assertTrue(entryId != '')

    def test_queryResult(self):
        #2322535b42853772437c91b52bf6f441c5964292
        contract = Contract(self.__conn, self.__wallet,
                            "CONGTXDtbsMjr1HzopNdttw3cK3SXQYNyWrq")
        data = contract.queryResult('92205f3dc7fb2a13692481bfa2ec357afd261784')
        self.assertTrue(data != '')
        print data
예제 #2
0
    def test_create(self):
        with self.assertRaises(ValueError):
            conn = RPCConnection('', 12388, "admin", "admin")

        with self.assertRaises(ValueError):
            conn = RPCConnection('127.0.0.1', 12388, '', "admin")

        conn = RPCConnection('127.0.0.1', 12388, 'admin', 'admin')
        conn.open()
        self.assertTrue(conn != None)
        self.assertTrue(conn.isOpen())
        self.assertTrue(conn.isAvailable())

        conn.close()

        self.assertFalse(conn.isOpen())
        self.assertFalse(conn.isAvailable())

        conn = RPCConnection('127.0.0.1', 12388, 'adafsdf', 'asdfasf')
        conn.open()
        self.assertTrue(conn != None)
        self.assertTrue(conn.isOpen())
        self.assertFalse(conn.isAvailable())
예제 #3
0
class MiddlewareWorker:
    __Running = False
    __Client = None
    __KeyPool = {}
    __Conn = None
    __contractAddress = ''
    __chain_dir = ''

    def __init__(self, contractAddress, chain_dir):
        self.__contractAddress = contractAddress
        self.__chain_dir = chain_dir
        self.__Conn = RPCConnection('127.0.0.1', 12388, 'admin', 'admin')
        self.__Conn.open()

    def on_message(self, msg):
        if msg.topic == Contants.CLIENT_REQ_TOPIC:
            data = msg.payload.decode("utf-8")
            msgJSON = json.loads(data)
            params = msgJSON.params
            resp = {
                "clientID": params.clientID,
                "reqID": params.reqID,
                "result": ""
            }
            if msgJSON.cmd == "Create":
                if self.__KeyPool.has_key(params.clientID):
                    key = self.__KeyPool[params.clientID]
                    rsakey = RSA.importKey(key)  # 导入读取到的私钥
                    cipher = Cipher_pkcs1_v1_5.new(rsakey)  # 生成对象
                    text = cipher.decrypt(base64.b64decode(params.data), None)

                    if text != None:
                        userinfo = json.loads(text)
                        if userinfo.has_key("name") and userinfo.has_key(
                                "password"):
                            r = os.path.exists(self.__chain_dir + "/wallets/" +
                                               userinfo.name)
                            if r == True:
                                resp.result = {"code": Contants.CODE_FAILURE}
                            else:
                                wallet = Wallet.newWallet(
                                    userinfo.name, userinfo.password,
                                    self.__conn)
                                if wallet != None:
                                    resp.result = {
                                        "code": Contants.CODE_SUCCESS,
                                        "address": wallet.getAddress()
                                    }
                                else:
                                    resp.result = {
                                        "code": Contants.CODE_FAILURE
                                    }
                        else:
                            resp.result = {"code": Contants.CODE_FAILURE}
                    else:
                        resp.result = {"code": Contants.CODE_FAILURE}
                else:
                    resp.result = {"code": Contants.CODE_FAILURE}

                self.__Client.publish(params.clientID, json.dumps(resp), True)
            elif msgJSON.cmd == "Auth_Key":
                if params.has_key('key'):
                    self.__KeyPool[params.clientID] = params.key
                    resp.result = {"code": Contants.CODE_SUCCESS}
                else:
                    resp.result = {"code": Contants.CODE_FAILURE}

                self.__Client.publish(params.clientID, json.dumps(resp), True)
            elif msgJSON.cmd == "Login":
                if self.__KeyPool.has_key(params.clientID):
                    key = self.__KeyPool[params.clientID]
                    rsakey = RSA.importKey(key)  # 导入读取到的私钥
                    cipher = Cipher_pkcs1_v1_5.new(rsakey)  # 生成对象
                    text = cipher.decrypt(base64.b64decode(params.data), None)

                    if text != None:
                        userinfo = json.loads(text)
                        wallet = Wallet(userinfo.name, userinfo.password,
                                        self.__Conn)
                        ret = wallet.auth()
                        if ret == True:
                            resp.result = json.dumps(
                                {"code": Contants.CODE_SUCCESS})
                        else:
                            resp.result = json.dumps(
                                {"code": Contants.CODE_FAILURE})
                    else:
                        resp.result = json.dumps(
                            {"code": Contants.CODE_DECRYPT_ERROR})
                else:
                    resp.result = json.dumps(
                        {"code": Contants.CODE_MISS_PUBLIC_KEY})

                self.__Client.publish(params.clientID, json.dumps(resp), True)
            elif msgJSON.cmd == "Bets":
                if self.__KeyPool.has_key(params.clientID):
                    key = self.__KeyPool[params.clientID]
                    rsakey = RSA.importKey(key)  # 导入读取到的私钥
                    cipher = Cipher_pkcs1_v1_5.new(rsakey)  # 生成对象
                    text = cipher.decrypt(base64.b64decode(params.data), None)

                    if text != None:
                        userinfo = json.loads(text)
                        wallet = Wallet(userinfo.name, userinfo.password,
                                        self.__Conn)
                        ret = wallet.auth()
                        if ret == True:
                            contract = Contract(self.__conn, wallet,
                                                self.__contractAddress)
                            entryId, payId = contract.bets(
                                paprams.target, params.amount, params.reqId)
                            if entryId != '' and payId != '':
                                resp.result = json.dumps({
                                    "code": Contants.CODE_SUCCESS,
                                    "entryId": entryId,
                                    "payId": payId
                                })
                            else:
                                resp.result = json.dumps(
                                    {"code": Contants.CODE_FAILURE})
                        else:
                            resp.result = json.dumps(
                                {"code": Contants.CODE_FAILURE})
                    else:
                        resp.result = json.dumps(
                            {"code": Contants.CODE_DECRYPT_ERROR})
                else:
                    resp.result = json.dumps(
                        {"code": Contants.CODE_MISS_PUBLIC_KEY})

                self.__Client.publish(params.clientID, json.dumps(resp), True)
            elif msgJSON.cmd == "deBets":
                if self.__KeyPool.has_key(params.clientID):
                    key = self.__KeyPool[params.clientID]
                    rsakey = RSA.importKey(key)  # 导入读取到的私钥
                    cipher = Cipher_pkcs1_v1_5.new(rsakey)  # 生成对象
                    text = cipher.decrypt(base64.b64decode(params.data), None)

                    if text != None:
                        userinfo = json.loads(text)
                        wallet = Wallet(userinfo.name, userinfo.password,
                                        self.__Conn)
                        ret = wallet.auth()
                        if ret == True:
                            contract = Contract(self.__conn, wallet,
                                                self.__contractAddress)
                            entryId = contract.deBets(paprams.target,
                                                      params.reqId)
                            if entryId != '':
                                resp.result = json.dumps({
                                    "code": Contants.CODE_SUCCESS,
                                    "entryId": entryId
                                })
                            else:
                                resp.result = json.dumps(
                                    {"code": Contants.CODE_FAILURE})
                        else:
                            resp.result = json.dumps(
                                {"code": Contants.CODE_FAILURE})
                    else:
                        resp.result = json.dumps(
                            {"code": Contants.CODE_DECRYPT_ERROR})
                else:
                    resp.result = json.dumps(
                        {"code": Contants.CODE_MISS_PUBLIC_KEY})

                self.__Client.publish(params.clientID, json.dumps(resp), True)
            elif msgJSON.cmd == "Transfer_to":
                if self.__KeyPool.has_key(params.clientID):
                    key = self.__KeyPool[params.clientID]
                    rsakey = RSA.importKey(key)  # 导入读取到的私钥
                    cipher = Cipher_pkcs1_v1_5.new(rsakey)  # 生成对象
                    text = cipher.decrypt(base64.b64decode(params.data), None)

                    if text != None:
                        userinfo = json.loads(text)
                        wallet = Wallet(userinfo.name, userinfo.password,
                                        self.__Conn)
                        ret = wallet.auth()
                        if ret == True:
                            entryId = wallet.pay(userinfo.address, False,
                                                 userinfo.amount, 'ACT')
                            if entryId == '':
                                resp.result = json.dumps(
                                    {"code": Contants.CODE_TRANSFER_FAILURE})
                            else:
                                resp.result = json.dumps({
                                    "entryId":
                                    entryId,
                                    "code":
                                    Contants.CODE_SUCCESS
                                })
                        else:
                            resp.result = json.dumps(
                                {"code": Contants.CODE_FAILURE})
                    else:
                        resp.result = json.dumps(
                            {"code": Contants.CODE_DECRYPT_ERROR})
                else:
                    resp.result = json.dumps(
                        {"code": Contants.CODE_MISS_PUBLIC_KEY})

                self.__Client.publish(params.clientID, json.dumps(resp), True)
            elif msgJSON.cmd == "backup_pk":
                if self.__KeyPool.has_key(params.clientID):
                    key = self.__KeyPool[params.clientID]
                    rsakey = RSA.importKey(key)  # 导入读取到的私钥
                    cipher = Cipher_pkcs1_v1_5.new(rsakey)  # 生成对象
                    text = cipher.decrypt(base64.b64decode(params.data), None)

                    if text != None:
                        userinfo = json.loads(text)
                        wallet = Wallet(userinfo.name, userinfo.password,
                                        self.__Conn)
                        ret = wallet.auth()
                        if ret == True:
                            key = wallet.getPrivateKey()
                            resp.result = json.dumps({
                                "pk":
                                key,
                                "code":
                                Contants.CODE_SUCCESS
                            })
                        else:
                            resp.result = json.dumps(
                                {"code": Contants.CODE_FAILURE})
                    else:
                        resp.result = json.dumps(
                            {"code": Contants.CODE_DECRYPT_ERROR})
                else:
                    resp.result = json.dumps(
                        {"code": Contants.CODE_MISS_PUBLIC_KEY})

                self.__Client.publish(Contants.SERVER_RESP_TOPIC,
                                      json.dumps(resp), True)
            elif msgJSON.cmd == "Query_balance":
                if self.__KeyPool.has_key(params.clientID):
                    key = self.__KeyPool[params.clientID]
                    rsakey = RSA.importKey(key)  # 导入读取到的私钥
                    cipher = Cipher_pkcs1_v1_5.new(rsakey)  # 生成对象
                    text = cipher.decrypt(base64.b64decode(params.data), None)

                    if text != None:
                        userinfo = json.loads(text)
                        wallet = Wallet(userinfo.name, userinfo.password,
                                        self.__Conn)
                        ret = wallet.auth()
                        if ret == True:
                            amount = wallet.amount()

                            resp.result = json.dumps({
                                "amount":
                                amount,
                                "code":
                                Contants.CODE_SUCCESS
                            })
                        else:
                            resp.result = json.dumps(
                                {"code": Contants.CODE_FAILURE})
                    else:
                        resp.result = json.dumps(
                            {"code": Contants.CODE_DECRYPT_ERROR})
                else:
                    resp.result = json.dumps(
                        {"code": Contants.CODE_MISS_PUBLIC_KEY})

                self.__Client.publish(Contants.SERVER_RESP_TOPIC,
                                      json.dumps(resp), True)
            else:
                resp.result = json.dumps({"code": Contants.CODE_FAILURE})
                self.__Client.publish(Contants.SERVER_RESP_TOPIC,
                                      json.dumps(resp), True)

    def isRunning(self):
        return self.__Running

    def on_contract_resp(self, data):
        pass

    def setClient(self, client):
        self.__Client = client

    def disconnect(self):
        if self.__Client != None:
            self.__Client.disconnect()
            self.__Client = None

        if self.__Conn != None:
            self.__Conn.close()
            self.__Conn = None

    def start(self):
        self.__Running = True
        thread.start_new_thread(PtyWorker, (self, 1))
        MQTTWorker(self)

    def stop(self):
        __Running = False
예제 #4
0
class WalletTest(unittest.TestCase):
    __conn = None

    def setUp(self):
        self.__conn = RPCConnection('127.0.0.1', 12388, 'admin', 'admin')
        self.__conn.open()

    def tearDown(self):
        self.__conn.close()
        self.__conn = None

    def test_create(self):
        with self.assertRaises(ValueError):
            wallet = Wallet.newWallet("ace2", "1234", self.__conn)

        with self.assertRaises(ValueError):
            wallet = Wallet.newWallet("", "123456789", self.__conn)

        with self.assertRaises(ValueError):
            wallet = Wallet.newWallet("ace2", "123456789", None)

        wallet = Wallet.newWallet("ace" + str(random.randint(3, 3000)),
                                  "ye1320240", self.__conn)
        self.assertTrue(wallet.getAddress() != '')
        print wallet.getAddress()
        #ACTGFnDVEgn8GcC3ZdexfnEa6EBrU67wAocH

    def test_getAddress(self):
        wallet = Wallet("ace2", "ye1320240", self.__conn)
        self.assertTrue(
            wallet.getAddress() == 'ACTGFnDVEgn8GcC3ZdexfnEa6EBrU67wAocH')
        wallet.close()

    def test_getPrivateKey(self):
        #5JnAudSiYXeatFAUR9HRhR9ZQCfF7bPT4KBJPdWMsdLVnUCVHzS
        wallet = Wallet("ace2", "ye1320240", self.__conn)
        self.assertTrue(wallet.getPrivateKey() ==
                        '5JnAudSiYXeatFAUR9HRhR9ZQCfF7bPT4KBJPdWMsdLVnUCVHzS')
        wallet.close()

    def test_amount(self):
        wallet = Wallet("ace2", "ye1320240", self.__conn)
        self.assertTrue(wallet.amount() > 1)
        wallet.close()

    def test_pay(self):
        #receiver ACT6b6VTNjRaJVTxYMfAZfB1TZkZ7jyc3eJU
        #contract CONKtJjMJ4CkrL1gMQhLbuWsyScYchgdrnYL
        wallet = Wallet("ace2", "ye1320240", self.__conn)
        entryId = wallet.pay("ACT6b6VTNjRaJVTxYMfAZfB1TZkZ7jyc3eJU", False,
                             "101", 'ACT')
        self.assertFalse(entryId != '')

        entryId = wallet.pay("ACT6b6VTNjRaJVTxYMfAZfB1TZkZ7jyc3eJU", False,
                             "0.1", 'ACT')
        self.assertTrue(entryId != '')
        print "Entry Id : " + entryId

        entryId = wallet.pay("CONKtJjMJ4CkrL1gMQhLbuWsyScYchgdrnYL", True,
                             "0.1", 'ACT')
        self.assertTrue(entryId != '')
        print "Contract Entry Id : " + entryId

    def test_op(self):
        with self.assertRaises(ValueError):
            wallet = Wallet("ace2", "1234", self.__conn)
        with self.assertRaises(ValueError):
            wallet = Wallet("", "123456789", self.__conn)
        with self.assertRaises(ValueError):
            wallet = Wallet("ace2", "123456789", None)

        wallet = Wallet("ace2", "ye1320240", self.__conn)
        wallet.close()
        self.assertTrue(wallet.open())
        self.assertTrue(wallet.unlock())

        wallet.close()
        self.assertTrue(wallet.auth())