Skip to content

Commit

Permalink
Merge pull request #21 from Bitcoin-com/validate
Browse files Browse the repository at this point in the history
Update Utils.validateTxid to use REST
  • Loading branch information
cgcardona authored Feb 18, 2019
2 parents ea874d6 + 8303e06 commit e306040
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 75 deletions.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const repl = require("repl")
const SLP = require("./lib/SLP").default
const clone = require("git-clone")

program.version("1.3.0", "-v, --version")
program.version("1.3.1", "-v, --version")

program
.command("new <name>")
Expand Down
47 changes: 20 additions & 27 deletions lib/Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,45 +127,38 @@ var Utils = /** @class */ (function () {
});
});
};
Utils.prototype.validateTxid = function (txid, network, getRawTransactions) {
if (getRawTransactions === void 0) { getRawTransactions = null; }
Utils.prototype.validateTxid = function (txid) {
return __awaiter(this, void 0, void 0, function () {
var tmpBITBOX, slpValidator, isValid;
var path, txids, response, error_4;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (network === "mainnet") {
//tmpBITBOX = new BITBOXSDK({ restURL: "https://rest.bitcoin.com/v2/" })
tmpBITBOX = new BITBOXSDK({ restURL: "http://localhost:3000/v2/" });
path = this.restURL + "slp/validateTxid";
if (typeof txid === "string") {
txids = [txid];
}
else {
tmpBITBOX = new BITBOXSDK({ restURL: "https://trest.bitcoin.com/v2/" });
txids = txid;
}
slpValidator = new slpjs.LocalValidator(tmpBITBOX, getRawTransactions
? getRawTransactions
: tmpBITBOX.RawTransactions.getRawTransaction.bind(this));
return [4 /*yield*/, slpValidator.isValidSlpTxid(txid)];
_a.label = 1;
case 1:
isValid = _a.sent();
return [2 /*return*/, isValid];
_a.trys.push([1, 3, , 4]);
return [4 /*yield*/, axios_1.default.post(path, {
txids: txids
})];
case 2:
response = _a.sent();
return [2 /*return*/, response.data];
case 3:
error_4 = _a.sent();
if (error_4.response && error_4.response.data)
throw error_4.response.data;
throw error_4;
case 4: return [2 /*return*/];
}
});
});
};
Utils.prototype.createValidator = function (network, getRawTransactions) {
if (getRawTransactions === void 0) { getRawTransactions = null; }
var tmpBITBOX;
if (network === "mainnet") {
tmpBITBOX = new BITBOXSDK({ restURL: "https://rest.bitcoin.com/v2/" });
}
else {
tmpBITBOX = new BITBOXSDK({ restURL: "https://trest.bitcoin.com/v2/" });
}
var slpValidator = new slpjs.LocalValidator(tmpBITBOX, getRawTransactions
? getRawTransactions
: tmpBITBOX.RawTransactions.getRawTransaction.bind(this));
return slpValidator;
};
return Utils;
}());
exports.default = Utils;
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": "slp-sdk",
"version": "1.3.0",
"version": "1.3.1",
"description": "SLP SDK powered by BITBOX",
"main": "index.js",
"scripts": {
Expand Down
54 changes: 14 additions & 40 deletions src/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,51 +59,25 @@ class Utils {
}
}

async validateTxid(
txid: string,
network: string,
getRawTransactions: any = null
): Promise<Object> {
let tmpBITBOX: any
async validateTxid(txid: string | string[]): Promise<Object> {
let path: string = `${this.restURL}slp/validateTxid`

if (network === "mainnet") {
//tmpBITBOX = new BITBOXSDK({ restURL: "https://rest.bitcoin.com/v2/" })
tmpBITBOX = new BITBOXSDK({ restURL: "http://localhost:3000/v2/" })
let txids: string[]
if (typeof txid === "string") {
txids = [txid]
} else {
tmpBITBOX = new BITBOXSDK({ restURL: "https://trest.bitcoin.com/v2/" })
txids = txid
}

const slpValidator: any = new slpjs.LocalValidator(
tmpBITBOX,
getRawTransactions
? getRawTransactions
: tmpBITBOX.RawTransactions.getRawTransaction.bind(this)
)

let isValid: boolean = await slpValidator.isValidSlpTxid(txid)
return isValid
}

createValidator(
network: string,
getRawTransactions: any = null
): Promise<Object> {
let tmpBITBOX: any

if (network === "mainnet") {
tmpBITBOX = new BITBOXSDK({ restURL: "https://rest.bitcoin.com/v2/" })
} else {
tmpBITBOX = new BITBOXSDK({ restURL: "https://trest.bitcoin.com/v2/" })
try {
const response = await axios.post(path, {
txids: txids
})
return response.data
} catch (error) {
if (error.response && error.response.data) throw error.response.data
throw error
}

const slpValidator: any = new slpjs.LocalValidator(
tmpBITBOX,
getRawTransactions
? getRawTransactions
: tmpBITBOX.RawTransactions.getRawTransaction.bind(this)
)

return slpValidator
}
}

Expand Down
13 changes: 9 additions & 4 deletions test/Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,19 @@ describe("#Utils", () => {
if (process.env.TEST === "unit") {
nock(SERVER)
.post(uri => uri.includes("/"))
.reply(200, mockData.mockRawTx)
.reply(200, mockData.mockIsValidTxid)
}

const isValid = await SLP.Utils.validateTxid(
"df808a41672a0a0ae6475b44f272a107bc9961b90f29dc918d71301f24fe92fb",
"mainnet"
"df808a41672a0a0ae6475b44f272a107bc9961b90f29dc918d71301f24fe92fb"
)
assert.equal(isValid, true)
assert.deepEqual(isValid, [
{
txid:
"df808a41672a0a0ae6475b44f272a107bc9961b90f29dc918d71301f24fe92fb",
valid: true
}
])
})
})
})
10 changes: 9 additions & 1 deletion test/fixtures/mock-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,17 @@ const mockRawTx = [
"0100000002b3b54b72de3cdff8d00a5a26e2aa7897f730c2889c46b41a83294004a2c6c9c6020000006a47304402202fff3979f9cf0a5052655c8699081a77a653903de41547928db0b94601aa082502207cdb909e3a7b2b7f8a3eb80243a1bd2fd8ad9449a0ec30242ae4b187436d11a0412103b30e7096c6e3a3b45e5aba4ad8fe48a1fdd7c04de0de55a43095e7560b52e19dfeffffffd25817de09517a6af6c3dbb332041f85d844052b32ea1dbca123365b18953726000000006a473044022011a39acbbb80c4723822d434445fc4b3d72ad0212902fdb183a5408af00e158c02200eb3778b1af9f3a8fe28b6670f5fe543fb4c190f79f349273860125be05269b2412103b30e7096c6e3a3b45e5aba4ad8fe48a1fdd7c04de0de55a43095e7560b52e19dfeffffff030000000000000000336a04534c500001010747454e45534953084e414b414d4f544f084e414b414d4f544f4c004c0001084c0008000775f05a07400022020000000000001976a91433c0448680ca324225eeca7a230cf191ab88400288ac8afc0000000000001976a91433c0448680ca324225eeca7a230cf191ab88400288ac967a0800"
]

const mockIsValidTxid = [
{
txid: "df808a41672a0a0ae6475b44f272a107bc9961b90f29dc918d71301f24fe92fb",
valid: true
}
]

module.exports = {
mockList,
mockToken,
balancesForAddress,
mockRawTx
mockRawTx,
mockIsValidTxid
}

0 comments on commit e306040

Please sign in to comment.