示例#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)
示例#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, steem_instance=self.steem)
        info = self.steem.info()
        meta = dict(timestamp=info["time"],
                    block=info["head_block_number"],
                    memokey=account["memo_key"],
                    account=account["name"])

        # wif key
        wif = self.steem.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())
    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)
    def upload(self, image, account, image_name=None):
        """ Uploads an image

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

            .. code-block:: python

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

        """
        account = Account(account, steem_instance=self.steem)
        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.steem.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()
示例#5
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``)
            :raises ValueError: If not account for signing is provided
            :returns: the signed message encapsulated in a known format
        """
        if not account:
            if "default_account" in self.blockchain.config:
                account = self.blockchain.config["default_account"]
        if not account:
            raise ValueError("You need to provide an account")

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

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

        # We strip the message here so we know for sure there are no trailing
        # whitespaces or returns
        message = self.message.strip()

        enc_message = self.SIGNED_MESSAGE_META.format(**locals())

        # signature
        signature = hexlify(sign_message(enc_message, wif)).decode("ascii")

        self.signed_by_account = account
        self.signed_by_name = account["name"]
        self.meta = meta
        self.plain_message = message

        return self.SIGNED_MESSAGE_ENCAPSULATED.format(
            MESSAGE_SPLIT=self.MESSAGE_SPLIT, **locals())
示例#6
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: Steem 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()
示例#7
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``)
            :raises ValueError: If not account for signing is provided
            :returns: the signed message encapsulated in a known format
        """
        if not account:
            if "default_account" in self.blockchain.config:
                account = self.blockchain.config["default_account"]
        if not account:
            raise ValueError("You need to provide an account")

        # Data for message
        account = Account(account, blockchain_instance=self.blockchain)

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

        payload = [
            "from",
            account["name"],
            "key",
            account["memo_key"],
            "time",
            str(datetime.utcnow()),
            "text",
            self.message,
        ]
        enc_message = json.dumps(payload, separators=(",", ":"))

        # signature
        signature = hexlify(sign_message(enc_message, wif)).decode("ascii")

        return dict(signed=enc_message, payload=payload, signature=signature)