Example #1
0
 def time_sign(self):
     wif = "5J4KCbg1G3my9b9hCaQXnHSm6vrwW9xQTJS6ZciW2Kek7cCkCEk"
     message = '576b2c99564392ed50e36c80654224953fdf8b5259528a1a4342c19be2da9b133c44429ac2be4d5dd588ec28e97015c34db80b7e8d8915e023c2501acd3eafe0'
     signature = ecda.sign_message(message, wif)
     message = 'foo'
     signature = ecda.sign_message(message, wif)
     message = 'This is a short Message'
     signature = ecda.sign_message(message, wif)
     message = '1234567890'
     signature = ecda.sign_message(message, wif)
Example #2
0
    def sign(self, account=None, **kwargs):
        """ Sign a message with an account's memo key

            :param str account: (optional) the account that owns the bet
                (defaults to ``default_account``)

            :returns: the signed message encapsulated in a known format

        """
        if not account:
            if "default_account" in config:
                account = config["default_account"]
        if not account:
            raise ValueError("You need to provide an account")

        # Data for message
        account = Account(account, dpay_instance=self.dpay)
        info = self.dpay.info()
        meta = dict(timestamp=info["time"],
                    block=info["head_block_number"],
                    memokey=account["memo_key"],
                    account=account["name"])

        # wif key
        wif = self.dpay.wallet.getPrivateKeyForPublicKey(account["memo_key"])

        # signature
        message = self.message.strip()
        signature = hexlify(
            sign_message(SIGNED_MESSAGE_META.format(**locals()),
                         wif)).decode("ascii")

        message = self.message
        return SIGNED_MESSAGE_ENCAPSULATED.format(MESSAGE_SPLIT=MESSAGE_SPLIT,
                                                  **locals())
Example #3
0
    def test_sign_message_cross(self, module):
        if module == "cryptography":
            if not ecda.CRYPTOGRAPHY_AVAILABLE:
                return
            ecda.SECP256K1_MODULE = "cryptography"
        elif module == "secp256k1":
            if not ecda.SECP256K1_AVAILABLE:
                return
            ecda.SECP256K1_MODULE = "secp256k1"

        pub_key = py23_bytes(repr(PrivateKey(wif).pubkey), "latin")
        signature = ecda.sign_message("Foobar", wif)
        ecda.SECP256K1_MODULE = "ecdsa"
        pub_key_sig = ecda.verify_message("Foobar", signature)
        self.assertEqual(hexlify(pub_key_sig), pub_key)
        signature = ecda.sign_message("Foobar", wif)
        ecda.SECP256K1_MODULE = module
        pub_key_sig = ecda.verify_message("Foobar", signature)
        self.assertEqual(hexlify(pub_key_sig), pub_key)
Example #4
0
    def upload(self, image, account, image_name=None):
        """ Uploads an image

            :param str/bytes image: path to the image or image in bytes representation which should be uploaded
            :param str account: Account which is used to upload. A posting key must be provided.
            :param str image_name: optional

            .. code-block:: python

                from dpaycli import DPay
                from dpaycli.imageuploader import ImageUploader
                stm = DPay(keys=["5xxx"]) # private posting key
                iu = ImageUploader(dpay_instance=stm)
                iu.upload("path/to/image.png", "account_name") # "private posting key belongs to account_name

        """
        account = Account(account, dpay_instance=self.dpay)
        if "posting" not in account:
            account.refresh()
        if "posting" not in account:
            raise AssertionError("Could not access posting permission")
        for authority in account["posting"]["key_auths"]:
            posting_wif = self.dpay.wallet.getPrivateKeyForPublicKey(
                authority[0])

        if isinstance(image, string_types):
            image_data = open(image, 'rb').read()
        elif isinstance(image, io.BytesIO):
            image_data = image.read()
        else:
            image_data = image

        message = py23_bytes(self.challenge, "ascii") + image_data
        signature = sign_message(message, posting_wif)
        signature_in_hex = hexlify(signature).decode("ascii")

        files = {image_name or 'image': image_data}
        url = "%s/%s/%s" % (self.base_url, account["name"], signature_in_hex)
        r = requests.post(url, files=files)
        return r.json()
Example #5
0
    def _request(self, account, method, params, key):
        """Assemble the request, hash it, sign it and send it to the Conveyor
            instance. Returns the server response as JSON.

            :param str account: account name
            :param str method: Conveyor method name to be called
            :param dict params: request parameters as `dict`
            :param str key: DPay posting key for signing

        """
        params_bytes = py23_bytes(json.dumps(params), self.ENCODING)
        params_enc = base64.b64encode(params_bytes).decode(self.ENCODING)
        timestamp = datetime.utcnow().strftime(self.TIMEFORMAT)[:-3] + "Z"
        nonce_int = random.getrandbits(64)
        nonce_bytes = struct.pack('>Q', nonce_int)  # 64bit ULL, big endian
        nonce_str = "%016x" % (nonce_int)

        message = self.prehash_message(timestamp, account, method,
                                       params_enc, nonce_bytes)
        signature = sign_message(message, key)
        signature_hex = hexlify(signature).decode(self.ENCODING)

        request = {
            "jsonrpc": "2.0",
            "id": self.id,
            "method": method,
            "params": {
                "__signed": {
                    "account": account,
                    "nonce": nonce_str,
                    "params": params_enc,
                    "signatures": [signature_hex],
                    "timestamp": timestamp
                }
            }
        }
        r = requests.post(self.url, data=json.dumps(request))
        self.id += 1
        return r.json()