scorechain-sdk
Version:
SDK for the Scorechain API
170 lines • 9.55 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ScorechainConnector = void 0;
const httpClients_1 = require("../utils/httpClients");
const scoringAnalysis_1 = require("../types/scoringAnalysis");
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
class ScorechainConnector {
constructor(apiKey, shouldVerifyAuthenticity = true) {
this._apiUrl = "https://api.scorechain.com/v1/";
this._header = {
"x-api-key": apiKey,
};
this.shouldVerifyAuthenticity = shouldVerifyAuthenticity;
}
/**
* Retrieve and update local non deprecated public key
* @returns A promise that resolves to the retrieved public keys.
*/
updateScorechainPublicKey() {
return __awaiter(this, void 0, void 0, function* () {
const publicKeys = yield this.getRequest(`${this._apiUrl}publicKeys`);
const validPublicKey = publicKeys.find(publicKey => !publicKey.isDeprecated);
fs_1.default.writeFileSync(path_1.default.join(__dirname, "../../scorechainPublicKey", "public.pem"), validPublicKey === null || validPublicKey === void 0 ? void 0 : validPublicKey.key);
});
}
/**
* Retrieve a transaction by its hash from the specified blockchain.
* @param transactionHash - The hash of the transaction to retrieve.
* @param blockchain - The blockchain from which to retrieve the transaction.
* @returns A promise that resolves to the retrieved transaction.
*/
getTransaction(transactionHash, blockchain) {
return __awaiter(this, void 0, void 0, function* () {
return this.getRequest(`${this._apiUrl}/blockchains/${blockchain}/transactions/${transactionHash}`);
});
}
/**
* Retrieve information about a specific address from the blockchain.
* @param address - The address to retrieve information for.
* @param blockchain - The blockchain to query.
* @returns A Promise that resolves to an Address object containing information about the address.
*/
getAddress(address, blockchain) {
return __awaiter(this, void 0, void 0, function* () {
return this.getRequest(`${this._apiUrl}/blockchains/${blockchain}/addresses/${address}`);
});
}
_scoringAnalysis(analysisType, objectType, blockchain, objectId, coin, depth) {
return this.postRequest(`${this._apiUrl}/scoringAnalysis`, {
analysisType,
objectType,
objectId,
blockchain,
coin,
depth,
});
}
/**
* Get a real-time scoring analysis for a transaction.
* @param transactionHash - The hash of the transaction.
* @param blockchain - The blockchain on which the transaction occurred.
* @param coin - The coin associated with the transaction.
* - ALL: get a global analysis (this is limited to addresses of less than 10K transactions and will return a 422 error otherwise).
* - MAIN: get an analysis on the main coin of the specified blockchain
* - [coinChainId]: get an analysis for a specific coin by providing the hash of the token contract.
* @param direction - The direction of the scoring analysis.
* @param depth - The depth of the scoring analysis (optional). For UTXO-based blockchains, default and maximum is enforced at 100. For account-based blockchains, default and maximum is enforced at 6.
* @returns A promise that resolves to the scoring analysis.
*/
getTransactionScoringAnalysis(transactionHash, blockchain, coin, direction, depth) {
return __awaiter(this, void 0, void 0, function* () {
return this._scoringAnalysis(direction, scoringAnalysis_1.ScoringObjectType.TRANSACTION, blockchain, transactionHash, coin, depth);
});
}
/**
* Get a real-time scoring analysis for a specific address.
* @param addressHash - The hash of the address to retrieve scoring analysis for.
* @param blockchain - The blockchain to query for scoring analysis.
* @param coin - The coin associated with the address.
* - ALL: get a global analysis (this is limited to addresses of less than 10K transactions and will return a 422 error otherwise).
* - MAIN: get an analysis on the main coin of the specified blockchain
* - [coinChainId]: get an analysis for a specific coin by providing the hash of the token contract.
* @param direction - The direction of the scoring analysis.
* @param depth - The depth of the scoring analysis (optional). For UTXO-based blockchains, default and maximum is enforced at 100. For account-based blockchains, default and maximum is enforced at 6.
* @returns A promise that resolves to the scoring analysis for the address.
*/
getAddressScoringAnalysis(addressHash, blockchain, coin, direction, depth) {
return __awaiter(this, void 0, void 0, function* () {
return this._scoringAnalysis(direction, scoringAnalysis_1.ScoringObjectType.ADDRESS, blockchain, addressHash, coin, depth);
});
}
/**
* Get a real-time scoring analysis for a wallet.
* @param addressHash - The hash of the wallet address.
* @param blockchain - The blockchain to retrieve the scoring analysis from.
* @param coin - The coin type for the wallet.
* - ALL: get a global analysis (this is limited to addresses of less than 10K transactions and will return a 422 error otherwise).
* - MAIN: get an analysis on the main coin of the specified blockchain
* - [coinChainId]: get an analysis for a specific coin by providing the hash of the token contract.
* @param direction - The direction of the scoring analysis.
* @param depth - The depth of the scoring analysis (optional). For UTXO-based blockchains, default and maximum is enforced at 100. For account-based blockchains, default and maximum is enforced at 6.
* @returns A promise that resolves to the scoring analysis for the wallet.
*/
getWalletScoringAnalysis(addressHash, blockchain, coin, direction, depth) {
return __awaiter(this, void 0, void 0, function* () {
return this._scoringAnalysis(direction, scoringAnalysis_1.ScoringObjectType.WALLET, blockchain, addressHash, coin, depth);
});
}
/**
* Feed a transaction for analysis by risk scenarios.
* @param transactionHash - The hash of the transaction to be scored.
* @param blockchain - The blockchain on which the transaction occurred.
* @param direction - The direction of the transaction (incoming or outgoing).
* @param address - The address associated with the transaction.
* @param customerRefId - (Optional) Identifier used to determine if multiple transactions should be considered as referring to the same customer. If not provided, the blockchain address is considered the identifier.
* @returns A promise that resolves to void.
*/
feedTransaction(transactionHash, blockchain, direction, address, customerRefId) {
return __awaiter(this, void 0, void 0, function* () {
return this.postRequest(`${this._apiUrl}/feedTransaction`, {
blockchain,
hash: transactionHash,
direction,
address,
customerRefId,
});
});
}
/**
* Feed a not approved withdrawal for analysis by risk scenarios.
* @param address - The address to send the withdrawal to.
* @param blockchain - The blockchain to use for the withdrawal.
* @param amount - The amount to withdraw.
* @param coinChainId - hash of the token address or 'MAIN' for the base asset
* @param identifier - (Optional) A unique UUID that serves as the identifier for a specific withdrawal operation.
* @returns A promise that resolves to the result of the withdrawal feed.
*/
feedWithdrawal(address, blockchain, amount, coinChainId, identifier) {
return __awaiter(this, void 0, void 0, function* () {
return this.postRequest(`${this._apiUrl}/feedWithdrawal`, {
blockchain,
address,
amount,
coinChainId,
identifier,
});
});
}
getRequest(url) {
return (0, httpClients_1._sendRequest)(url, "GET", {}, this._header, this.shouldVerifyAuthenticity);
}
postRequest(url, data) {
return (0, httpClients_1._sendRequest)(url, "POST", data, this._header, this.shouldVerifyAuthenticity);
}
}
exports.ScorechainConnector = ScorechainConnector;
//# sourceMappingURL=scorechainConnector.js.map