Skip to content

Commit

Permalink
update bchd validator
Browse files Browse the repository at this point in the history
  • Loading branch information
jcramer committed Sep 11, 2020
1 parent 24aa402 commit fe4e73d
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 8 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -837,6 +837,9 @@ Running the unit tests require node.js v8.15+.

# Change Log

### 0.27.8
- Simplify BchdValidator behavior and API

### 0.27.7
- Add BchdValidator class for leveraging BCHD SLP indexer

Expand Down
22 changes: 16 additions & 6 deletions lib/bchdtrustedvalidator.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,34 @@
import { IGrpcClient } from "grpc-bchrpc";
import { SlpValidator } from "..";
import { logger, SlpValidator } from "..";

export class BchdValidator implements SlpValidator {
public client: IGrpcClient;
constructor(client: IGrpcClient) {
private logger?: { log: (s: string) => any; };
constructor(client: IGrpcClient, logger?: logger) {
this.client = client;
if (logger) {
this.logger = logger;
}
}
public async getRawTransactions(txid: string[]): Promise<string[]> {
const res = await this.client.getRawTransaction({hash: txid[0], reversedHashOrder: true});
return [Buffer.from(res.getTransaction_asU8()).toString("hex")];
}
public async isValidSlpTxid(txid: string): Promise<boolean> {
try {
console.log(`validate: ${txid}`);
this.log(`validate: ${txid}`);
const res = await this.client.getTrustedSlpValidation({
txos: [{vout: 1, hash: txid}],
reversedHashOrder: true
});
} catch (error) {
if (!error.message.includes("Error")) {
if (! error.message.includes("txid is missing from slp validity set")) {
throw error;
}
console.log(`false (${txid})`);
this.log(`false (${txid})`);
return false;
}
console.log(`true (${txid})`);
this.log(`true (${txid})`);
return true;
}

Expand All @@ -35,4 +39,10 @@ export class BchdValidator implements SlpValidator {
}
return res.filter((id: string) => id.length > 0);
}

private log(s: string) {
if (this.logger) {
this.logger.log(s);
}
}
}
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "slpjs",
"version": "0.27.7",
"version": "0.27.8",
"description": "Simple Ledger Protocol (SLP) JavaScript Library",
"main": "index.js",
"files": [
Expand Down

0 comments on commit fe4e73d

Please sign in to comment.