Split hash library files up.

This commit is contained in:
Richard Moore 2020-10-18 21:45:32 -04:00
parent 5a4dd5a703
commit 3e676f21b0
No known key found for this signature in database
GPG Key ID: 665176BE8E9DC651
3 changed files with 65 additions and 6 deletions

View File

@ -0,0 +1,15 @@
import { Bytes, concat } from "@ethersproject/bytes";
import { keccak256 } from "@ethersproject/keccak256";
import { toUtf8Bytes } from "@ethersproject/strings";
export const messagePrefix = "\x19Ethereum Signed Message:\n";
export function hashMessage(message: Bytes | string): string {
if (typeof(message) === "string") { message = toUtf8Bytes(message); }
return keccak256(concat([
toUtf8Bytes(messagePrefix),
toUtf8Bytes(String(message.length)),
message
]));
}

View File

@ -0,0 +1,44 @@
import { concat, hexlify } from "@ethersproject/bytes";
import { nameprep, toUtf8Bytes } from "@ethersproject/strings";
import { keccak256 } from "@ethersproject/keccak256";
import { Logger } from "@ethersproject/logger";
import { version } from "./_version";
const logger = new Logger(version);
const Zeros = new Uint8Array(32);
Zeros.fill(0);
const Partition = new RegExp("^((.*)\\.)?([^.]+)$");
export function isValidName(name: string): boolean {
try {
const comps = name.split(".");
for (let i = 0; i < comps.length; i++) {
if (nameprep(comps[i]).length === 0) {
throw new Error("empty")
}
}
return true;
} catch (error) { }
return false;
}
export function namehash(name: string): string {
/* istanbul ignore if */
if (typeof(name) !== "string") {
logger.throwArgumentError("invalid address - " + String(name), "name", name);
}
let result: string | Uint8Array = Zeros;
while (name.length) {
const partition = name.match(Partition);
const label = toUtf8Bytes(nameprep(partition[3]));
result = keccak256(concat([result, keccak256(label)]));
name = partition[2] || "";
}
return hexlify(result);
}

View File

@ -57,12 +57,6 @@
{
"path": "./packages/strings"
},
{
"path": "./packages/hash"
},
{
"path": "./packages/abi"
},
{
"path": "./packages/solidity"
},
@ -81,6 +75,12 @@
{
"path": "./packages/abstract-signer"
},
{
"path": "./packages/hash"
},
{
"path": "./packages/abi"
},
{
"path": "./packages/contracts"
},