Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function publicKeyFromId (id) {
// The last byte is the checksum, slice it off
return new Uint8Array(Base58.decode(id).slice(0, 32))
}
static fromWif(_private_wif) {
var private_wif = new Buffer(base58.decode(_private_wif));
var version = private_wif.readUInt8(0);
assert.equal(0x80, version, `Expected version ${0x80}, instead got ${version}`);
// checksum includes the version
var private_key = private_wif.slice(0, -4);
var checksum = private_wif.slice(-4);
var new_checksum = hash.sha256(private_key);
new_checksum = hash.sha256(new_checksum);
new_checksum = new_checksum.slice(0, 4);
if (checksum.toString() !== new_checksum.toString())
throw new Error('Invalid WIF key (checksum miss-match)')
private_key = private_key.slice(1);
return PrivateKey.fromBuffer(private_key);
}
function validateId (id) {
var idRegex = /^[1-9ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz]{40,55}$/
if (!idRegex.test(id)) return false
var bytes = Base58.decode(id)
if (bytes.length !== 33) return false
var hash = new BLAKE2s(1)
hash.update(bytes.slice(0, 32))
return hash.digest()[0] === bytes[32]
}
function isMultihash(hash) {
var formatted = convertToString(hash);
try {
var buffer = new Buffer(base58.decode(formatted));
multihash.decode(buffer);
return true;
} catch (e) {
return false;
}
}
function isMultihash (hash) {
const formatted = convertToString(hash)
try {
const buffer = Buffer.from(base58.decode(formatted))
multihash.decode(buffer)
return true
} catch (e) {
return false
}
}
exports.fromB58String = function fromB58String (hash) {
let encoded = hash
if (Buffer.isBuffer(hash)) {
encoded = hash.toString()
}
return Buffer.from(bs58.decode(encoded))
}
static decodeMultihash (multihash) {
const base16Multihash = bs58.decode(multihash)
return {
digest: `0x${base16Multihash.slice(2).toString('hex')}`,
hashFn: parseInt(base16Multihash[0]),
size: parseInt(base16Multihash[1])
}
}
function decodePrivate(encodedKey) {
const buffer = bs58.decode(encodedKey);
if (buffer[0] !== 128) throw new Error('private key network id mismatch');
return buffer.slice(0, -4);
}