Validating data from the blockchain

To check data stored in the blockchain, the reverse of the insertion sequence needs to be used.

Handout

It is not enough to be able to store data on the blockchain, as it needs to be possible to verify the data and check any claims made.

First, look at the block. This is block 538357 and can be fetched directly using getblockhash followed by getblock, or viewed using a blockchain explorer:

https://chainz.cryptoid.info/smly/search.dws?q=538357

Pick one of these transactions, say a40d1b13ffb741d64e6630e0726ef82397d0930f93d639fa3126a519ea49b4af to find the data which goes with the OP_RETURN operator:

6a4120e62ef86dcc6626e4a15262818dd56af7865af16a91b5a
58a2aa4a7943f0180b4d6ef117df8f7432a55e8fc9beb254b75
3765a88a79f87f32fe99bfd1d54c4cd9

(all on one line with no spaces).

As with other hex codes, each character is codes as a two-digit hexadecimal number. The first two are 6a and 41 and the rest is the actual data string. The 6a is the code for OP_RETURN (see this link).

The actual hex coding is therefore 130 hexadecimal numbers or 65 bytes and since 65 in decimal is 41 hex (or 0x41), this explains the second code, 41, being the length of the data field.

To decode the string, a few python3 commands are useful:

 codecs.encode(codecs.decode('20c077803a9cfda97887d1ac684cfb52dca91db0ab
 0fb75a436a888b4b35489860b984f427415580b9381eb314d90ea609d6df54ebd494eb7458687a720330ebb4', 'hex'),
'base64').decode()
'IMB3gDqc/al4h9GsaEz7UtypHbCrD7daQ2qIi0s1SJhguYT0J0FVgLk4HrMU2Q6mCdbfVOvUlOt0\nWGh6cgMw67Q=\n'

The two newline characters need to be removed from this string, resulting in a signature of

'IMB3gDqc/al4h9GsaEz7UtypHbCrD7daQ2qIi0s1SJhguYT0J0FVgLk4HrMU2Q6mCdbfVOvUlOt0WGh6cgMw67Q='

Recall that the original hash to be signed was

09d3b7814390b0badfdf9550d848396f9ee7be202f8c61f6c678d71169ba0f9a

and it was signed by 4 addresses:

BPbwDW2AWsE9KmFDRi1K6QrUdrHvkfbxfn
BSZNAqFuQCH3hZTqwmrqv8LDYPJuEYWfyv
BLE92S2zXshaczZ8GrojAXp8yD54UGRHDk
BMv1CU9d9ghzB5HdtahWYz9N6NGpFVpSVB

One can now verify this signature using the appropriate address:

smileycoin-cli verifymessage BMv1CU9d9ghzB5HdtahWYz9N6NGpFVpSVB
 'IMB3gDqc/al4h9GsaEz7UtypHbCrD7daQ2qIi0s1SJhguYT0J0FVgLk4HrMU2Q6mCdbfVOvUlOt0WGh6cgMw67Q='
 '09d3b7814390b0badfdf9550d848396f9ee7be202f8c61f6c678d71169ba0f9a'

which returns true as it should.