tutorial: extract file from blockchain
We have uploaded the file hello.txt and this is the resulting Pigi Upload API Response:
A bitcon transaction contains INPUTS and OUTPUTS. See https://learnmeabitcoin.com/technical/transaction-data for more detail. The file that we just uploaded is stored the first OUTPUT
It's possible to fetch the raw transaction from any txid by calling public APIs.
Popular API's for Bitcoin SV are:
To increase our understanding of the transaction that we just created. Let's inspect its JSON-decoded form. A bitcoin transaction is often serialized in hexadecimal format. This hexadecimal format can be parsed and represented as a JSON object. Blockchair's API will return the JSON represenation of the transaction in the data.<txid>.decoded_raw_transaction field.
Take a closer look at decoded_raw_transaction.vout.scriptPubKey.asm. This is a Bitcoin Script and it contains an assembly language that is executed by miners in order to verify that a specific output can be spent. In this case, the output is unspendable and simply contains data. Let's dissect this script:
Now that we understand how a Pigi binary blob is stored within a transaction, lets parse the Pigi binary blob and decrypt its contents.
- Read the first byte (version)
- Read the second byte (algorithm)
- Read the next 32 bytes (file id)
- Read the next 4 bytes (length)
- Read the next 16 bytes (IV)
- Read the next encryptedSize bytes (encryptedData)
The upload reponse returned a secret key inside the identifiers.raw.secret field. In our case: WmH5w6eCzNrYSAc80Hgtd2GZq8qwAtt6PS3Lct6yzzA0
This is a base62-encoded representation of a 32 byte secret key. To convert the base62 key to binary, use any base62 library and use the following alphabet:
Now that the key is in binary form. We can decrypt encryptedData as follows:
The decrypted data is an LZMA compressed binary blob. We must now decompress it
The decompressed data contains a CBOR encoded array. We must use CBOR to decode this decompressed data. After we have CBOR decoded the decompressed data, we have access to the filename, file type and file contents.