Skip to content

Instantly share code, notes, and snippets.

@xembook
Created December 18, 2020 17:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xembook/a949ee5ded22e2a11bcac1211a524bf7 to your computer and use it in GitHub Desktop.
Save xembook/a949ee5ded22e2a11bcac1211a524bf7 to your computer and use it in GitHub Desktop.
Merkle Proof AccountInfo
https://d3rmzi6ltfh1jy.cloudfront.net
(script = document.createElement('script')).src = 'https://xembook.github.io/nem2-browserify/symbol-sdk-pack-0.22.2.js';
document.getElementsByTagName('head')[0].appendChild(script);
NODE = 'https://d3rmzi6ltfh1jy.cloudfront.net'
GENERATION_HASH = '6C1B92391CCB41C96478471C2634C111D9E989DECD66130C0430B5B8D20117CD';
nem = require("/node_modules/symbol-sdk");
sha3_256 = require('/node_modules/js-sha3').sha3_256;
repo = new nem.RepositoryFactoryHttp(NODE, nem.NetworkType.TEST_NET,GENERATION_HASH);
accountRepo = repo.createAccountRepository()
stateProofService = new nem.StateProofService(repo);
alice = nem.Account.generateNewAccount(nem.NetworkType.TEST_NET);
"http://faucet-0.10.0.x-01.symboldev.network/?recipient=" + alice.address.plain() +"&amount=2"
//ノードからAliceのマークル証明ツリーを取得
proof = await stateProofService.accountById(alice.address).toPromise();
//クライアント側でAliceのAccountInfoのハッシュを生成(検証のため一部ノードから取得)
aliceInfo = await accountRepo.getAccountInfo(alice.address).toPromise();
hasher = sha3_256.create();
aliceInfoHash = hasher.update(aliceInfo.serialize()).hex().toUpperCase();
//aliceInfo stateHashが等しいことを確認
aliceInfoHash === proof.stateHash
//stateHash が merkleTreeのleaf valueと等しいことを確認
proof.stateHash === proof.merkleTree.leaf.value;
//leafHashを算出
hasher = sha3_256.create();
leafHash = hasher.update(nem.Convert.hexToUint8(proof.merkleTree.leaf.encodedPath + proof.merkleTree.leaf.value)).hex().toUpperCase();
//leafの一つ上のbranchを取得
branchLength = proof.merkleTree.branches.length;
proof.merkleTree.branches[branchLength - 1];
//branch.links(keys?)の内部にleafHashが含まれることを確認
links = proof.merkleTree.branches[branchLength - 1].links
merkleTreeBranchLink = links.find(_ => _.link === leafHash)
console.log(merkleTreeBranchLink.bit)
bitって何??
仮説
aliceAccountInfoの存在検証についてbitが何であるかを知る必要はない。
proof.merkleTree.branchesの中にleafHashが含まれてさえいればよい?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment