@moosty/lisk-htlc
Version:
Hashed Time Lock Contract transaction for Lisk SDK based blockchain applications
43 lines • 2.1 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const lisk_cryptography_1 = require("@liskhq/lisk-cryptography");
const lisk_transactions_1 = require("@liskhq/lisk-transactions");
const crypto = tslib_1.__importStar(require("crypto"));
const ripemd160_1 = tslib_1.__importDefault(require("ripemd160"));
const constants_1 = require("./constants");
exports.verifyContractAddress = (id, asset, sender) => {
if (asset.type && exports.getContractAddress(asset, sender) !== asset.contractId) {
return new lisk_transactions_1.TransactionError('Invalid contractId', id, '.contractId', asset.contractId, exports.getContractAddress(asset, sender));
}
return undefined;
};
exports.getContractAddress = (asset, sender) => {
return lisk_cryptography_1.getAddressFromPublicKey(exports.assetsToPublicKey(asset, sender));
};
exports.assetsToPublicKey = (asset, sender) => {
const hash = crypto.createHash('sha256')
.update(Buffer.from(asset.data + asset.recipientPublicKey + sender + asset.time, 'utf8'))
.digest();
const pubKey = Buffer.concat([Buffer.alloc(4, "0199"), hash.slice(4, 32)]);
return pubKey.toString("hex");
};
exports.verifyKey = (hash, key, type = 'OP_HASH256') => {
return exports.hashKey(key, type) === hash;
};
exports.hashKey = (key, type = 'OP_HASH256') => {
const sha256Hash = crypto.createHash('sha256')
.update(Buffer.from(key, 'utf8'))
.digest("hex");
return type === "OP_HASH160" ? new ripemd160_1.default().update(sha256Hash).digest("hex") : sha256Hash;
};
exports.getTimeFromBlockchainEpoch = (givenTimestamp) => {
const startingPoint = givenTimestamp || new Date().getTime();
return Math.floor((startingPoint - constants_1.EPOCH_TIME_MILLISECONDS) / constants_1.MS_FACTOR);
};
exports.getTimeWithOffset = (offset) => {
const now = new Date().getTime();
const timeWithOffset = offset ? now + offset * constants_1.MS_FACTOR : now;
return exports.getTimeFromBlockchainEpoch(timeWithOffset);
};
//# sourceMappingURL=utils.js.map