Skip to content

Commit

Permalink
Extracted the hash into a separate function for better readability
Browse files Browse the repository at this point in the history
  • Loading branch information
vsubhuman committed Jan 11, 2022
1 parent 58b50be commit 2c852b3
Showing 1 changed file with 30 additions and 8 deletions.
38 changes: 30 additions & 8 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,39 @@ export function textPartFromWalletChecksumImagePart(walletChecksum: string): str
return textPartFromBytes(toBytesInt32(fast1a32(walletChecksum)))
}

export function walletChecksum(publicKeyHash: string /* note: lowercase hex representation */): WalletChecksum {
// ImagePart
const output = new Uint8Array(64)
const input = Buffer.from(publicKeyHash)
const ImagePart = blake2b(
output.length,
function hash(len: number, inp: string, msg: string): string {
return blake2b(
len,
undefined,
undefined,
Buffer.from('wallets checksum') // personal
).update(input).digest('hex');
Buffer.from(msg) // personal
).update(Buffer.from(inp)).digest('hex');
}

export function hash44(s: string): string {
const shortHash = hash(33, s, 'shorten checksum');
const buff1 = Array.from(Buffer.from(shortHash, 'hex').toString('base64'));
const requiredReplacementPositions = buff1
.map((c, i) => c === '+' || c === '/' ? i : -1)
.filter(i => i >= 0);
const reverse = s.split('').reverse().join('');
for (let i = 0; i < 100; i++) {
const shortHash2 = hash(39, `${reverse}${i}`, 'shorten checksum');
const buff2 = Array.from(Buffer.from(shortHash2, 'hex').toString('base64'));
const availableReplacements = buff2.filter(c => c !== '+' && c !== '/');
if (availableReplacements.length >= requiredReplacementPositions.length) {
requiredReplacementPositions.forEach((position, i) => {
buff1[position] = availableReplacements[i];
});
return buff1.join('');
}
}
return buff1.map(c => c === '+' ? 'a' : (c === '/' ? 'b' : c)).join('');
}

export function walletChecksum(publicKeyHash: string /* note: lowercase hex representation */): WalletChecksum {
// ImagePart
const ImagePart = hash(64, publicKeyHash, 'wallets checksum');
// TextPart
const TextPart = textPartFromWalletChecksumImagePart(ImagePart);
return { ImagePart, TextPart };
Expand Down

0 comments on commit 2c852b3

Please sign in to comment.