hive-keychain-commons
Version:
Platform-agnostic functions used in Hive Keychain mobile and extensions
59 lines (58 loc) • 2.24 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.isWif = void 0;
const bs58 = __importStar(require("bs58"));
const crypto_js_1 = require("crypto-js");
const sha256 = (input) => {
if (typeof input !== 'string') {
input = crypto_js_1.lib.WordArray.create(input);
}
const hash = Buffer.from((0, crypto_js_1.SHA256)(input).toString(crypto_js_1.enc.Hex), 'hex');
return hash;
};
const ripemd160 = (input) => {
// return crypto.createHash('ripemd160').update(input).digest()
if (typeof input !== 'string') {
input = crypto_js_1.lib.WordArray.create(input);
}
const hash = Buffer.from((0, crypto_js_1.RIPEMD160)(input).toString(crypto_js_1.enc.Hex), 'hex');
return hash;
};
const isWif = (privWif) => {
try {
const bufWif = new Buffer(bs58.decode(privWif));
const privKey = bufWif.slice(0, -4);
const checksum = bufWif.slice(-4);
let newChecksum = sha256(privKey);
newChecksum = sha256(newChecksum);
newChecksum = newChecksum.slice(0, 4);
return checksum.toString() === newChecksum.toString();
}
catch (e) {
return false;
}
};
exports.isWif = isWif;