Skip to content

Instantly share code, notes, and snippets.

@xembook
Last active June 26, 2020 09:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xembook/f5622688b06289d59b403f979c19f6bd to your computer and use it in GitHub Desktop.
Save xembook/f5622688b06289d59b403f979c19f6bd to your computer and use it in GitHub Desktop.
function verifyBlock(data){
const block = JSON.parse(data);
signer = block.signer;
signature = block.signature;
v = cat.GeneratorUtils.uintToBuffer(block.version, 1) //version:8
n = cat.GeneratorUtils.uintToBuffer(block.networkType, 1) //network:8
t = cat.GeneratorUtils.uintToBuffer(block.type, 2) //type:16
h = cat.GeneratorUtils.uint64ToBuffer([block.height.lower,block.height.higher]) //height
ts = cat.GeneratorUtils.uint64ToBuffer([block.timestamp.lower,block.timestamp.higher]) //timestamp
df = cat.GeneratorUtils.uint64ToBuffer([block.difficulty.lower,block.difficulty.higher]) //difficulty
ph = nem.Convert.hexToUint8(block.previousBlockHash) //prehash
th = nem.Convert.hexToUint8(block.blockTransactionsHash) //txhash
rh = nem.Convert.hexToUint8(block.blockReceiptsHash) //rcpthash
sh = nem.Convert.hexToUint8(block.stateHash) //stathash
bp = nem.Convert.hexToUint8(block.beneficiaryPublicKey.publicKey) //benepubkey
fm = cat.GeneratorUtils.uintToBuffer(block.feeMultiplier, 4) // feeMultiplier:32
const buffer = Array.from(v)
.concat(Array.from(n))
.concat(Array.from(t))
.concat(Array.from(h))
.concat(Array.from(ts))
.concat(Array.from(df))
.concat(Array.from(ph))
.concat(Array.from(th))
.concat(Array.from(rh))
.concat(Array.from(sh))
.concat(Array.from(bp))
.concat(Array.from(fm));
const hex = nem.Convert.uint8ToHex(buffer);
var hasher = jssha3.sha3_256.create();
hasher.update(nem.Convert.hexToUint8(signature)); //signature
hasher.update(nem.Convert.hexToUint8(signer.publicKey)); //publicKey
const hash = hasher.update(buffer).hex().toUpperCase();
console.log("calc hash:" + hash);
console.log("block hash:" + block.hash);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment