Skip to content

Commit

Permalink
Replace hash libs
Browse files Browse the repository at this point in the history
  • Loading branch information
DancingAxolotl committed Jul 19, 2022
1 parent ecee48f commit 4c40938
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 21 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
"devDependencies": {
"@ethersproject/providers": "^5.4.5",
"@types/bn.js": "^4.11.6",
"@types/crypto-js": "^4.1.1",
"@types/elliptic": "^6.4.13",
"@types/jest": "27.0.2",
"@types/lodash": "^4.14.158",
Expand Down Expand Up @@ -99,6 +100,7 @@
"@ethersproject/abi": "^5.0.1",
"bn.js": "^4.4.0",
"cross-fetch": "^3.1.4",
"crypto-js": "^4.1.1",
"elliptic": "^6.5.4",
"js-sha256": "^0.9.0",
"js-sha3": "^0.8.0"
Expand Down
40 changes: 22 additions & 18 deletions src/utils/namehash.ts
Original file line number Diff line number Diff line change
@@ -1,51 +1,55 @@
import {Hash, keccak_256 as sha3} from 'js-sha3';
import {sha256} from 'js-sha256';
import {Buffer} from 'buffer';
import sha256 from 'crypto-js/sha256';
import sha3 from 'crypto-js/sha3';
import hex from 'crypto-js/enc-hex';
import WordArray from 'crypto-js/lib-typedarrays';
import BN from 'bn.js';

export function eip137Namehash(domain: string): string {
const arr = hashArray(domain, sha3);
const arr = hashArray(domain, 'sha3');
return arrayToHex(arr);
}

export function eip137Childhash(parentHash: string, label: string): string {
return childhash(parentHash, label, sha3);
return childhash(parentHash, label, 'sha3');
}

export function znsNamehash(domain: string): string {
const arr = hashArray(domain, sha256);
const arr = hashArray(domain, 'sha256');
return arrayToHex(arr);
}

export function znsChildhash(parentHash: string, label: string): string {
return childhash(parentHash, label, sha256);
return childhash(parentHash, label, 'sha256');
}

function childhash(
parentHash: string,
label: string,
hashingAlgo: Hash,
hashingAlgo: 'sha256' | 'sha3',
): string {
const hash = hashingAlgo === 'sha256' ? sha256 : sha3;
const opts = {outputLength: 256};
const parent = parentHash.replace(/^0x/, '');
const childHash = hashingAlgo.hex(label);
return `0x${hashingAlgo.hex(Buffer.from(`${parent}${childHash}`, 'hex'))}`;
const childHash = hex.stringify(hash(label, opts));
return `0x${hex.stringify(hash(hex.parse(`${parent}${childHash}`), opts))}`;
}

function hashArray(domain: string, hashingAlgo: Hash): number[] {
function hashArray(domain: string, hashingAlgo: 'sha256' | 'sha3'): WordArray {
if (!domain) {
return Array.from(new Uint8Array(32));
return WordArray.create(Array.from(new Uint8Array(8)));
}

const hash = hashingAlgo === 'sha256' ? sha256 : sha3;
const opts = {outputLength: 256};

const [label, ...remainder] = domain.split('.');
const labelHash = hashingAlgo.array(label);
const labelHash = hash(label, opts);
const remainderHash = hashArray(remainder.join('.'), hashingAlgo);
return hashingAlgo.array(new Uint8Array([...remainderHash, ...labelHash]));
return hash(remainderHash.concat(labelHash), opts);
}

function arrayToHex(arr: number[]) {
return `0x${Array.prototype.map
.call(arr, (x) => ('00' + x.toString(16)).slice(-2))
.join('')}`;
function arrayToHex(arr: WordArray) {
return `0x${hex.stringify(arr)}`;
}

export function fromHexStringToDecimals(value: string): string {
Expand Down
6 changes: 3 additions & 3 deletions src/utils/znsUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
*/

import BN from 'bn.js';
import {sha256} from 'js-sha256';
import {hexToBytes} from './index';
import sha256 from 'crypto-js/sha256';
import hex from 'crypto-js/enc-hex';

const CHARSET = 'qpzry9x8gf2tvdw0s3jn54khce6mua7l';

Expand Down Expand Up @@ -193,7 +193,7 @@ export const toChecksumAddress = (address: string): string => {
}

address = address.toLowerCase().replace('0x', '');
const hash = sha256(hexToBytes(address));
const hash = hex.stringify(sha256(hex.parse(address)));
const v = new BN(hash, 'hex', 'be');
let ret = '0x';

Expand Down

0 comments on commit 4c40938

Please sign in to comment.