Skip to content

Instantly share code, notes, and snippets.

@ader1990
Created May 4, 2018 13:33
Show Gist options
  • Save ader1990/cb64d9514bf8d4f568d819c16d979afe to your computer and use it in GitHub Desktop.
Save ader1990/cb64d9514bf8d4f568d819c16d979afe to your computer and use it in GitHub Desktop.
# I have run the following and got the attachement data for block01 of webdollar blockchain
# >> blockchainDB.allDocs({include_docs: true, limit: 200, attachments: true}, function(err, doc) {row = doc.rows[199].doc._attachments.key.data})
# var data looks like this:
# >> 'MDAwMDdlMzAxMzcxMWJjMTI2YTg3MDhhMzI0NDcyZmRmY2Y2ODU1Y2VmMWY5NzliMGRmNDM0NzVjOTU2OGUzMzAwMDBlNzI0MDAwMTczMWQ0NmMxMzFlYjZkZDQ2NjdhOTZiZGMyN2JhZjkyMjNiZWM3MmMzNDY4ZGZiNmJhNTJjNDYwZTc2NDIzYTQwMDAwMDQ0NzZjMWQzNTViMDMyMDU0MTIyMDQ4MDRlNDI3ZjlhNTZkZTE1ZjEwZjBjODBmYzUzMGZlY2RlOW
# E0OGFlNmFlZGU4NjFlOTkwM2U1NDIyZjdjMTExMzU4MDMyZGZlNmNhMzdhYWQ4YmRkNWRmNmUwZTI3NjEzNTlkMzBhODI3NTA1OGUyOTlmY2MwMzgxNTM0NTQ1ZjU1Y2Y0M2U0MTk4M2Y1ZDRjOTQ1NjAwMDAwMDAwMGExY2ZjZWIyMTVhZWFjNDZjMGI3ZWVhMGQ1ZGE5ZGVmZDkyN2UyNDUwNGI3NTdlNmMzZDM3MDI4NTNjOWM5OTAxMDAwMDAxNzMxZDQ2YzEzM
# WViNmRkNDY2N2E5NmJkYzI3YmFmOTIyM2JlYzcyYzM0NjhkZmI2YmE1MmM0NjBlNzY0MjNhNA=='
# I would like now to deserialize it using the minimum non cluttered code and obtain the timestamp, for example
# Got the code from: https://github.com/WebDollar/Node-WebDollar/blob/4aadea24d7abcac91f3e1438f89162c656aa76d2/src/common/utils/Serialization.js#L100
function deserializeNumber(buffer){
if(buffer.length === 1) return buffer[0]; else
if (buffer.length === 2) return buffer[1] | (buffer[0] << 8); else
if (buffer.length === 3) return buffer[2] | (buffer[1] << 8) | (buffer[0] << 16); else
if (buffer.length === 4) return buffer[3] | (buffer[2] << 8) | (buffer[1] << 16) | (buffer[0] << 24); else
if (buffer.length === 6) return buffer[5] | (buffer[4] << 8) | (buffer[3] << 16) | (buffer[2] << 24) | (buffer[1] << 32) | (buffer[0] << 40);
}
function substr(buffer, index, count){
if (count === undefined)
count = buffer.length;
let length = Math.min(index + count, buffer.length);
if (length-index <= 0)
throw {message: "length-index <= 0...", buffer: buffer.toString("hex"), index:index, length:length, count: count};
let buf = new Buffer(length-index);
buffer.copy(buf, 0, index, length);
return buf;
}
# And now I would like to know if I am doing stuff right to get the previousHash
offset = 0
buffer = Buffer.from(data, 'base64')
# According to https://github.com/WebDollar/Node-WebDollar/blob/acdd19a5de2d968b2dd2506f9285e8b2229bec98/src/common/blockchain/interface-blockchain/blocks/Interface-Blockchain-Block.js#L293
# hashPrev should be at an offset of BLOCKCHAIN.BLOCKS_POW_LENGTH + NONCE + 2
var hashPrev = substr(buffer, offset + 32 + 4 +3, 32).toString("hex")
# Is this correct? I think I am missing something, as I obtain
# >> hashPrev
# >> '6365663166393739623064663433343735633935363865333330303030653732'
@ibudisteanu
Copy link

hi,

            this.hash = BufferExtended.substr(buffer, offset, consts.BLOCKCHAIN.BLOCKS_POW_LENGTH);
            offset += consts.BLOCKCHAIN.BLOCKS_POW_LENGTH;

            this.nonce = Serialization.deserializeNumber( BufferExtended.substr(buffer, offset, consts.BLOCKCHAIN.BLOCKS_NONCE) );
            offset += consts.BLOCKCHAIN.BLOCKS_NONCE;


            this.version = Serialization.deserializeNumber( BufferExtended.substr(buffer, offset, 2) );
            offset += 2;

            this.hashPrev = BufferExtended.substr(buffer, offset, consts.BLOCKCHAIN.BLOCKS_POW_LENGTH);
            offset += consts.BLOCKCHAIN.BLOCKS_POW_LENGTH;

hashPrev = sum of all offsets so consts.BLOCKCHAIN.BLOCKS_POW_LENGTH + consts.BLOCKCHAIN.BLOCKS_NONCE + 2

@ibudisteanu
Copy link

hashprev = BufferExtended.substr(buffer , consts.BLOCKCHAIN.BLOCKS_POW_LENGTH + consts.BLOCKCHAIN.BLOCKS_NONCE + 2, 32);

@ibudisteanu
Copy link

you can extract hash first, nonce, version, hashPrev afterwards

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment