importjsonimportosimporturllib.requestfromsymbolchain.CryptoTypesimportHash256fromsymbolchain.symbol.MerkleimportMerklePart,prove_merkleNODE_URL=os.getenv('NODE_URL','https://reference.symboltest.net:3001')print(f'Using node {NODE_URL}')TX_HASH=os.getenv('TRANSACTION_HASH','99011A8DBC086E0C359E9D8A38FEC6714C33726FCD0C1B5C0F772A82400D808B')print(f'Transaction hash: {TX_HASH}')try:# Fetch the confirmed transaction to get its block heighttx_path=f'/transactions/confirmed/{TX_HASH}'print(f'Fetching transaction from {tx_path}')withurllib.request.urlopen(f'{NODE_URL}{tx_path}')asresponse:tx_data=json.loads(response.read().decode())print(json.dumps(tx_data['meta'],indent=2))block_height=tx_data['meta']['height']merkle_component_hash=Hash256(tx_data['meta']['merkleComponentHash'])# Fetch the block header to get the transactions hashblock_path=f'/blocks/{block_height}'print(f'Fetching block from {block_path}')withurllib.request.urlopen(f'{NODE_URL}{block_path}')asresponse:block_data=json.loads(response.read().decode())print(json.dumps({'height':block_data['block']['height'],'transactionsHash':block_data['block']['transactionsHash'],},indent=2))transactions_hash=Hash256(block_data['block']['transactionsHash'])# Fetch the merkle proof path for the transactionmerkle_path=(f'/blocks/{block_height}'f'/transactions/{TX_HASH}/merkle')print('Fetching merkle proof:')print(f' {merkle_path}')withurllib.request.urlopen(f'{NODE_URL}{merkle_path}')asresponse:merkle_data=json.loads(response.read().decode())print(json.dumps(merkle_data,indent=2))# Convert the API response to the format expected by the SDKmerkle_proof_path=[MerklePart(Hash256(part['hash']),part['position']=='left')forpartinmerkle_data['merklePath']]print(f' Merkle path length: {len(merkle_proof_path)}')# Verify that the transaction is included in the blockis_proven=prove_merkle(merkle_component_hash,merkle_proof_path,transactions_hash)ifis_proven:print(f'Transaction {TX_HASH[:16]}...'f' proven in block {block_height}')else:raiseRuntimeError(f'Transaction {TX_HASH[:16]}...'f' NOT proven in block {block_height}')exceptExceptionase:print(e)
import{Hash256}from'symbol-sdk';import{proveMerkle}from'symbol-sdk/symbol';constNODE_URL=process.env.NODE_URL||'https://reference.symboltest.net:3001';console.log('Using node',NODE_URL);constTX_HASH=process.env.TRANSACTION_HASH||'99011A8DBC086E0C359E9D8A38FEC6714C33726FCD0C1B5C0F772A82400D808B';console.log('Transaction hash:',TX_HASH);try{// Fetch the confirmed transaction to get its block heightconsttxPath=`/transactions/confirmed/${TX_HASH}`;console.log('Fetching transaction from',txPath);consttxResponse=awaitfetch(`${NODE_URL}${txPath}`);consttxData=awaittxResponse.json();console.log(JSON.stringify(txData.meta,undefined,2));constblockHeight=txData.meta.height;constmerkleComponentHash=newHash256(txData.meta.merkleComponentHash);// Fetch the block header to get the transactions hashconstblockPath=`/blocks/${blockHeight}`;console.log('Fetching block from',blockPath);constblockResponse=awaitfetch(`${NODE_URL}${blockPath}`);constblockData=awaitblockResponse.json();console.log(JSON.stringify({height:blockData.block.height,transactionsHash:blockData.block.transactionsHash},undefined,2));consttransactionsHash=newHash256(blockData.block.transactionsHash);// Fetch the merkle proof path for the transactionconstmerklePath=`/blocks/${blockHeight}`+`/transactions/${TX_HASH}/merkle`;console.log('Fetching merkle proof:');console.log(` ${merklePath}`);constmerkleResponse=awaitfetch(`${NODE_URL}${merklePath}`);constmerkleData=awaitmerkleResponse.json();console.log(JSON.stringify(merkleData,undefined,2));// Convert the API response to the format expected by the SDKconstmerkleProofPath=merkleData.merklePath.map(part=>({hash:newHash256(part.hash),isLeft:'left'===part.position}));console.log(' Merkle path length:',merkleProofPath.length);// Verify that the transaction is included in the blockconstisProven=proveMerkle(merkleComponentHash,merkleProofPath,transactionsHash);if(isProven){console.log(`Transaction ${TX_HASH.slice(0,16)}...`+` proven in block ${blockHeight}`);}else{thrownewError(`Transaction ${TX_HASH.slice(0,16)}...`+` NOT proven in block ${blockHeight}`);}}catch(e){console.error(e.message);}
//JAVA 21+//DEPS org.symbol:symbol-sdk:3.3.1importjava.io.IOException;importjava.net.URI;importjava.net.http.HttpClient;importjava.net.http.HttpRequest;importjava.net.http.HttpResponse;importjava.net.http.HttpResponse.BodyHandlers;importjava.util.ArrayList;importjava.util.List;importcom.fasterxml.jackson.databind.JsonNode;importcom.fasterxml.jackson.databind.ObjectMapper;importorg.symbol.sdk.CryptoTypes;importorg.symbol.sdk.symbol.Merkle;importorg.symbol.sdk.symbol.Merkle.MerklePart;publicfinalclassProveTransaction{privatestaticfinalObjectMapperJSON_MAPPER=newObjectMapper();privatestaticfinalHttpClientHTTP_CLIENT=HttpClient.newHttpClient();privatefinalStringnodeUrl=System.getenv().getOrDefault("NODE_URL","https://reference.symboltest.net:3001");privatefinalStringtxHash=System.getenv().getOrDefault("TRANSACTION_HASH","99011A8DBC086E0C359E9D8A38FEC6714C33726FCD0C1B5C0F772A8240"+"0D808B");publicstaticvoidmain(finalString[]args){try{newProveTransaction().run();}catch(finalExceptionex){System.out.println(null==ex.getMessage()?ex.toString():ex.getMessage());}}privatevoidrun()throwsIOException,InterruptedException{System.out.printf("Using node %s%n",nodeUrl);System.out.printf("Transaction hash: %s%n",txHash);// Fetch the confirmed transaction to get its block heightfinalStringtxPath="/transactions/confirmed/"+txHash;System.out.printf("Fetching transaction from %s%n",txPath);finalJsonNodetxData=getJson(txPath);System.out.println(JSON_MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(txData.get("meta")));finallongblockHeight=txData.get("meta").get("height").asLong();finalCryptoTypes.Hash256merkleComponentHash=newCryptoTypes.Hash256(txData.get("meta").get("merkleComponentHash").asText());// Fetch the block header to get the transactions hashfinalStringblockPath="/blocks/"+blockHeight;System.out.printf("Fetching block from %s%n",blockPath);finalJsonNodeblockData=getJson(blockPath);finalJsonNodeblockSummary=JSON_MAPPER.createObjectNode().put("height",blockData.get("block").get("height").asText()).put("transactionsHash",blockData.get("block").get("transactionsHash").asText());System.out.println(JSON_MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(blockSummary));finalCryptoTypes.Hash256transactionsHash=newCryptoTypes.Hash256(blockData.get("block").get("transactionsHash").asText());// Fetch the merkle proof path for the transactionfinalStringmerklePath="/blocks/"+blockHeight+"/transactions/"+txHash+"/merkle";System.out.println("Fetching merkle proof:");System.out.printf(" %s%n",merklePath);finalJsonNodemerkleData=getJson(merklePath);System.out.println(JSON_MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(merkleData));// Convert the API response to the format expected by the SDKfinalList<MerklePart>merkleProofPath=newArrayList<>();for(finalJsonNodepart:merkleData.get("merklePath"))merkleProofPath.add(newMerklePart(newCryptoTypes.Hash256(part.get("hash").asText()),"left".equals(part.get("position").asText())));System.out.printf(" Merkle path length: %d%n",merkleProofPath.size());// Verify that the transaction is included in the blockfinalbooleanisProven=Merkle.proveMerkle(merkleComponentHash,merkleProofPath,transactionsHash);if(isProven){System.out.printf("Transaction %s... proven in block %d%n",txHash.substring(0,16),blockHeight);}else{thrownewIllegalStateException(String.format("Transaction %s... NOT proven in block %d",txHash.substring(0,16),blockHeight));}}privateJsonNodegetJson(finalStringpath)throwsIOException,InterruptedException{finalHttpRequestrequest=HttpRequest.newBuilder(URI.create(nodeUrl+path)).GET().build();finalHttpResponse<String>response=HTTP_CLIENT.send(request,BodyHandlers.ofString());returnJSON_MAPPER.readTree(response.body());}}
# Fetch the confirmed transaction to get its block heighttx_path=f'/transactions/confirmed/{TX_HASH}'print(f'Fetching transaction from {tx_path}')withurllib.request.urlopen(f'{NODE_URL}{tx_path}')asresponse:tx_data=json.loads(response.read().decode())print(json.dumps(tx_data['meta'],indent=2))block_height=tx_data['meta']['height']merkle_component_hash=Hash256(tx_data['meta']['merkleComponentHash'])
// Fetch the confirmed transaction to get its block heightconsttxPath=`/transactions/confirmed/${TX_HASH}`;console.log('Fetching transaction from',txPath);consttxResponse=awaitfetch(`${NODE_URL}${txPath}`);consttxData=awaittxResponse.json();console.log(JSON.stringify(txData.meta,undefined,2));constblockHeight=txData.meta.height;constmerkleComponentHash=newHash256(txData.meta.merkleComponentHash);
// Fetch the confirmed transaction to get its block heightfinalStringtxPath="/transactions/confirmed/"+txHash;System.out.printf("Fetching transaction from %s%n",txPath);finalJsonNodetxData=getJson(txPath);System.out.println(JSON_MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(txData.get("meta")));finallongblockHeight=txData.get("meta").get("height").asLong();finalCryptoTypes.Hash256merkleComponentHash=newCryptoTypes.Hash256(txData.get("meta").get("merkleComponentHash").asText());
# Fetch the block header to get the transactions hashblock_path=f'/blocks/{block_height}'print(f'Fetching block from {block_path}')withurllib.request.urlopen(f'{NODE_URL}{block_path}')asresponse:block_data=json.loads(response.read().decode())print(json.dumps({'height':block_data['block']['height'],'transactionsHash':block_data['block']['transactionsHash'],},indent=2))transactions_hash=Hash256(block_data['block']['transactionsHash'])
// Fetch the block header to get the transactions hashconstblockPath=`/blocks/${blockHeight}`;console.log('Fetching block from',blockPath);constblockResponse=awaitfetch(`${NODE_URL}${blockPath}`);constblockData=awaitblockResponse.json();console.log(JSON.stringify({height:blockData.block.height,transactionsHash:blockData.block.transactionsHash},undefined,2));consttransactionsHash=newHash256(blockData.block.transactionsHash);
// Fetch the block header to get the transactions hashfinalStringblockPath="/blocks/"+blockHeight;System.out.printf("Fetching block from %s%n",blockPath);finalJsonNodeblockData=getJson(blockPath);finalJsonNodeblockSummary=JSON_MAPPER.createObjectNode().put("height",blockData.get("block").get("height").asText()).put("transactionsHash",blockData.get("block").get("transactionsHash").asText());System.out.println(JSON_MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(blockSummary));finalCryptoTypes.Hash256transactionsHash=newCryptoTypes.Hash256(blockData.get("block").get("transactionsHash").asText());
# Fetch the merkle proof path for the transactionmerkle_path=(f'/blocks/{block_height}'f'/transactions/{TX_HASH}/merkle')print('Fetching merkle proof:')print(f' {merkle_path}')withurllib.request.urlopen(f'{NODE_URL}{merkle_path}')asresponse:merkle_data=json.loads(response.read().decode())print(json.dumps(merkle_data,indent=2))# Convert the API response to the format expected by the SDKmerkle_proof_path=[MerklePart(Hash256(part['hash']),part['position']=='left')forpartinmerkle_data['merklePath']]print(f' Merkle path length: {len(merkle_proof_path)}')
// Fetch the merkle proof path for the transactionconstmerklePath=`/blocks/${blockHeight}`+`/transactions/${TX_HASH}/merkle`;console.log('Fetching merkle proof:');console.log(` ${merklePath}`);constmerkleResponse=awaitfetch(`${NODE_URL}${merklePath}`);constmerkleData=awaitmerkleResponse.json();console.log(JSON.stringify(merkleData,undefined,2));// Convert the API response to the format expected by the SDKconstmerkleProofPath=merkleData.merklePath.map(part=>({hash:newHash256(part.hash),isLeft:'left'===part.position}));console.log(' Merkle path length:',merkleProofPath.length);
// Fetch the merkle proof path for the transactionfinalStringmerklePath="/blocks/"+blockHeight+"/transactions/"+txHash+"/merkle";System.out.println("Fetching merkle proof:");System.out.printf(" %s%n",merklePath);finalJsonNodemerkleData=getJson(merklePath);System.out.println(JSON_MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(merkleData));// Convert the API response to the format expected by the SDKfinalList<MerklePart>merkleProofPath=newArrayList<>();for(finalJsonNodepart:merkleData.get("merklePath"))merkleProofPath.add(newMerklePart(newCryptoTypes.Hash256(part.get("hash").asText()),"left".equals(part.get("position").asText())));System.out.printf(" Merkle path length: %d%n",merkleProofPath.size());
# Verify that the transaction is included in the blockis_proven=prove_merkle(merkle_component_hash,merkle_proof_path,transactions_hash)ifis_proven:print(f'Transaction {TX_HASH[:16]}...'f' proven in block {block_height}')else:raiseRuntimeError(f'Transaction {TX_HASH[:16]}...'f' NOT proven in block {block_height}')
// Verify that the transaction is included in the blockconstisProven=proveMerkle(merkleComponentHash,merkleProofPath,transactionsHash);if(isProven){console.log(`Transaction ${TX_HASH.slice(0,16)}...`+` proven in block ${blockHeight}`);}else{thrownewError(`Transaction ${TX_HASH.slice(0,16)}...`+` NOT proven in block ${blockHeight}`);}
// Verify that the transaction is included in the blockfinalbooleanisProven=Merkle.proveMerkle(merkleComponentHash,merkleProofPath,transactionsHash);if(isProven){System.out.printf("Transaction %s... proven in block %d%n",txHash.substring(0,16),blockHeight);}else{thrownewIllegalStateException(String.format("Transaction %s... NOT proven in block %d",txHash.substring(0,16),blockHeight));}