update for beem: create posts more easily and draw pseudo-random numbers

View this thread on: d.buzz | hive.blog | peakd.com | ecency.com
·@holger80·
0.000 HBD
update for beem: create posts more easily and draw pseudo-random numbers
## Repository

https://github.com/holgern/beem<center>
![beem-logo](https://cdn.steemitimages.com/DQmcRrwLPSywSYMierfP6um6mejeMNGjN9Rxw7audJqTDgb/beem-logo)
</center>

[beem](https://github.com/holgern/beem) is a python library and command line tool for HIVE.  The current version is 0.23.11.

There is also a discord channel for beem: https://discord.gg/4HM592V

The newest beem version can be installed by:

```
pip install -U beem
```

Check that you are using hive nodes. The following command 

```
beempy updatenodes --hive
```

updates the nodelist and uses only hive nodes. After setting hive as default_chain, `beempy updatenodes` can be used.

The list of nodes can be checked with

```
beempy config
```

and

```
beempy currentnode
```

shows the currently connected node.

## Changelog for versions 0.23.10

* get_node_answer_time added to NodeList
* New node added
* new stored parameter: default_canonical_url
* beempy notifications sorting is reversed, a new parameter can be used to change the sorting
* New beempy createpost command, it can be used to create an empty markdown file with YAML header for a new post
* beempy post has now a canonical_url parameter, when not set, default_canonical_url is set
* New beempy draw command, can be used to generate pseudo-random number from block identifiers using hashsums
* remove enum34 dependency

## Changelog for version 0.23.11

* replace asn1 by asn1crypto

## Setting canonical URLs when posting with beempy
The `canonical_url` field in the json_metadata of a post is part of an open standard that defines which canonical URL should be set by all hive front-ends.

The canonical URL is then parsed by the search engines and shown in search results.

A default canonical URL can now be set with
```
beemy set default_canonical_url https://hive.blog
````
When not modified, `https://hive.blog` is the default value. When posting now with `beempy post` this canonical URL is set in the json_metadata field.

It is also possible to set a canonical URL by:
```
beempy post --canonical-url https://hive.blog my_post.md
```


## Create a new markdown file for posting

The following command
```
beempy createpost my_new_post.md
```
can be used to create an empty markdown file with a YAML header.
After executing the command, beempy asks the following questions: 
![beempy createpost](https://images.hive.blog/DQmPNAMxxLgrpK7YiQ6qZwaFK1N4MSXXqahgSxwLVvxcz3C/beempy%20createpost)
and creates the following YAML header in the created markdown file:
![Resulting YAML header](https://images.hive.blog/DQmZxU8ffTQwMnW8Pag9nvMMyJR4sB94D7froKHs44fW3Az/Resulting%20YAML%20header)

## Draw pseudo-random numbers and post the result as a comment

`beempy draw` can be used to generate pseudo-random numbers based on a seed which is generated as follows:
```
    if hashtype == "md5":
        seed = hashlib.md5((trx_id + block["block_id"] + block["previous"]).encode()).hexdigest()
    elif hashtype == "sha256":
        seed = hashlib.sha256((trx_id + block["block_id"] + block["previous"]).encode()).hexdigest()
    elif hashtype == "sha512":
        seed = hashlib.sha512((trx_id + block["block_id"] + block["previous"]).encode()).hexdigest()
```
`beempy draw` uses three entropy sources: the hash of the last block, the hash of the current block and the hash of a transaction inside the current block. 

When using `md5` as hashtype, the seed is used to initialize the random generator:
```
random.seed(a=seed, version=2)
number = int(random.random() * len(draw_list))
```
This is e.g. used in splinterlands to generate random numbers.

When using `sha256` or `sha512`, `,<draw_number>` is added to the seed and a pseudo-random number is generated by
```
bigRand = int.from_bytes(seed, 'big')
number = bigRand % (len(draw_list))
```
It is possible to choose the separator by using ``-s`` parameter.

The results for `sha256` can be validated [here](https://www.stat.berkeley.edu/~stark/Java/Html/sha256Rand.htm).

### Usage

The following command uses the latest block number and the last transaction for building the seed and draw three number of 1000 different ones.
```
beempy draw -d 3 -p 1000
```
creates the following output
```
+--------------+------------------------------------------+
| Key          | Value                                    |
+--------------+------------------------------------------+
| block number | 43892950                                 |
| trx id       | 38206e28dcbf15d8f9ce1be907fb60a99d427171 |
| block id     | 029dc0d6a1fe487b5f2907988ba72b3ba495589b |
| previous     | 029dc0d55023bdf2f6450083c52349c4cd838f65 |
| hash type    | sha256                                   |
| draws        | 3                                        |
| participants | 1000                                     |
| 1. draw      | 406                                      |
| 2. draw      | 568                                      |
| 3. draw      | 186                                      |
+--------------+------------------------------------------+
```
We can validate the results by adding the value of trx id, block id and previous in this order into the seed field of [this pseudo-random generator](https://www.stat.berkeley.edu/~stark/Java/Html/sha256Rand.htm):

![](https://images.hive.blog/DQmRWzsejv8b8ZEgkrNBgKHLPmcfkgNo2uDem7SrRNk1vyj/image)

We can see that the same numbers were generated.

The results can also be validated by
```
beempy draw -d 3 -p 1000 -b 43892950 -t 38206e28dcbf15d8f9ce1be907fb60a99d427171
```

Adding `--markdown` or `-m` creates a markdown output which can directly copied into a post. The command above with `-m` generates:

The following results can be checked with:
```
beempy draw -d 3 -p 1000 -b 43892950 -t 38206e28dcbf15d8f9ce1be907fb60a99d427171 -h sha256 -s ','
```

| key | value |
| --- | --- |
| block number | [43892950](https://hiveblocks.com/b/43892950#38206e28dcbf15d8f9ce1be907fb60a99d427171) |
| trx id | [38206e28dcbf15d8f9ce1be907fb60a99d427171](https://hiveblocks.com/tx/38206e28dcbf15d8f9ce1be907fb60a99d427171) |
| block id | 029dc0d6a1fe487b5f2907988ba72b3ba495589b |
| previous id | 029dc0d55023bdf2f6450083c52349c4cd838f65 |
| hash type | sha256 |
| draws | 3 |
| participants | 1000 |
| 1. draw | 406 |
| 2. draw | 568 |
| 3. draw | 186 |

### Posting the results directly as reply
This can be used to directly publish results in a comment.
```
beempy draw -d 3 -p 10 --reply @user/my-post
```
`@user/my-post` is the parent post to which the comment is replied.

### Prevent cheating
A user could rerun `beempy draw` until the results are fitting. This can be prevented by defining previously which future block number is used for generating the pseudo-random numbers. 

Or a future broadcast is used as reference, e.g. the next post. After broadcasting a new post, the trx-id and the block number can be checked and provided to ´beempy draw´.



### Creating a participants list to draw user from a list
`beempy draw` can be used to draw account names from a list of accounts. The participants can be stored in a text file where each line is one participant.

In order to draw one account, I can store all participating accounts in a file:
```
@holger80
@beembot
@beempy
```
A participant can now be selected by:
```
beempy draw -p account_list.txt
```
which results in
```
+--------------+------------------------------------------+
| Key          | Value                                    |
+--------------+------------------------------------------+
| block number | 43893720                                 |
| trx id       | c12b9911d6c9d950f02700ae70c066e105c40aba |
| block id     | 029dc3d8bc1518ad8a92c6e9fd2cce4e64095fb2 |
| previous     | 029dc3d7d7b53b31b7d389a0cc9a84e8352ddb19 |
| hash type    | sha256                                   |
| draws        | 1                                        |
| participants | 3                                        |
| 1. draw      | 2 - @beembot                             |
+--------------+------------------------------------------+
```

The order of the participants should be clear (e.g. ordered by date or ordered by letter) and defined previously.

When each participant should only be drawn once, the `--without-replacement` or `-w` parameter must be used.


## Feedback
Do you have some new ideas or improvements for beem or beempy? Let me know.
___

*If you like what I do, consider casting a vote for me as witness on [Hivesigner](https://hivesigner.com/sign/account-witness-vote?witness=holger80&approve=1) or on [PeakD](https://peakd.com/witnesses)*
👍 , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,