Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
public static async createFromStream(readableStream: NodeJS.ReadableStream): Promise {
const hasher = keccak256.create();
return new Promise((resolve, reject) => {
readableStream.on('end', chunk => {
resolve(base58check.encode(hasher.hex()));
});
readableStream.on('error', err => {
reject(err);
});
// data resumes the paused stream so end/error have to be added
// first.
readableStream.on('data', chunk => {
hasher.update(chunk);
});
public static isValidAddress = (decoded: Uint8Array, networkType: NetworkType): boolean => {
if (RawAddress.constants.sizes.addressDecoded !== decoded.length) {
return false;
}
const signSchema = SHA3Hasher.resolveSignSchema(networkType);
const hash = signSchema === SignSchema.SHA3 ? sha3_256.create() : keccak256.create();
const checksumBegin = RawAddress.constants.sizes.addressDecoded - RawAddress.constants.sizes.checksum;
hash.update(decoded.subarray(0, checksumBegin));
const checksum = new Uint8Array(RawAddress.constants.sizes.checksum);
RawArray.copy(checksum, RawArray.uint8View(hash.arrayBuffer()), RawAddress.constants.sizes.checksum);
return RawArray.deepEqual(checksum, decoded.subarray(checksumBegin));
}
}
return new Promise((resolve, reject) => {
const hasher = keccak256.create();
const fileReadStream = fs.createReadStream(path);
fileReadStream.on('end', () => {
const actualHash = hasher.hex();
if (actualHash !== expectedHash) {
reject(`Hashes don't match: expected ${expectedHash}, got ${actualHash}`);
} else {
resolve();
}
});
fileReadStream.on('data', (chunk) => {
hasher.update(chunk);
});
});
public static async createFromInputSource(inputSource: InputSource): Promise {
const hasher = keccak256.create();
return new Promise((resolve, reject) => {
InputSources.open(inputSource, (data: InputData | undefined, err: Error | undefined) => {
if (err) {
reject(err);
}
if (data) {
hasher.update(data);
} else {
resolve(base58check.encode(hasher.hex()));
}
});