Skip to content

Commit

Permalink
add gs++ example
Browse files Browse the repository at this point in the history
  • Loading branch information
jcramer committed Oct 16, 2019
1 parent cd92519 commit 9df4fb5
Show file tree
Hide file tree
Showing 4 changed files with 606 additions and 57 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ The following examples are provided in the examples directory:
- `2-validate-tx-rpc-burn-valid-allow.ts`: Allows sending a valid burn transaction
- `3-validate-txid-rpc.ts`: Traditional SLP validate method by txid (offers no extra burn protection).
- `4-validate-txid-bchd.ts`: Similar to example 3, but uses BCHD's gRPC instead of JSON RPC.
- `5-validate-txid-gs++.ts`: Validate more quickly by downloading transactions in bulk from SLP graph search instead of downloading transactions individually via RPC.



Expand Down
41 changes: 41 additions & 0 deletions examples/5-validate-txid-gs++.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/***************************************************************************************
*
* Example 5: Validate using gs++ server.
*
* Instructions:
* (1) - Optional: Setup SLP graph search server URL
* (2) - Optional: set custom txid.
*
* ************************************************************************************/

import { ValidatorType1, Crypto } from '../index';
import { GraphSearchClient } from 'grpc-slp-graphsearch-node';

const txid = "ecaaf0a4de119a59a440089c99a2c103791dbd06086472ff8ff4229c5cd7cc4f";

(async function() {
console.time("SLP-VALIDATE-W-GRAPH-SEARCH");

// perform graph search
let gs = new GraphSearchClient(); // optional set server url
let dag = new Map<string, Buffer>();
(await gs.graphSearchFor(txid)).getTxdataList_asU8().forEach(txn => {
let txnBuf = Buffer.from(txn);
let id = Crypto.hash256(txnBuf).toString('hex');
dag.set(id, txnBuf);
});

// create SLP validator
let getRawTransaction = async (id: string) => {
if(dag.has(id)) return dag.get(id)!;
else return Buffer.alloc(60);
}
const slpValidator = new ValidatorType1({ getRawTransaction });

console.log("Validating:", txid);
console.log("This may take a several seconds...");
let isValid = await slpValidator.isValidSlpTxid({ txid });
console.log("Final Result:", isValid);
console.log("WARNING: THIS VALIDATION METHOD COMES WITH NO BURN PROTECTION.")
console.timeEnd("SLP-VALIDATE-W-GRAPH-SEARCH");
})();
Loading

0 comments on commit 9df4fb5

Please sign in to comment.